2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 * @author Dongsun Lee (ds73.lee@samsung.com)
20 * @brief provides fucntions for encryption and decryption of web application.
22 #include "web_app_enc.h"
27 #include "key_handler.h"
28 #include "crypto_service.h"
31 int _wae_encrypt_downloaded_web_application(const char *pPkgId, wae_app_type_e appType,
32 const unsigned char *pData, size_t dataLen,
33 unsigned char **ppEncryptedData, size_t *pEncDataLen)
35 int ret = WAE_ERROR_NONE;
36 unsigned char *pDek = NULL;
40 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
41 ret = WAE_ERROR_INVALID_PARAMETER;
45 if (pData == NULL || dataLen <= 0) {
46 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
47 ret = WAE_ERROR_INVALID_PARAMETER;
51 if (ppEncryptedData == NULL || pEncDataLen == NULL) {
52 WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
53 ret = WAE_ERROR_INVALID_PARAMETER;
58 // if not exists, create APP_DEK
59 ret = get_app_dek(pPkgId, appType, &pDek, &dekLen);
61 if (ret == WAE_ERROR_NO_KEY) {
62 ret = create_app_dek(pPkgId, appType, &pDek, &dekLen);
65 if (ret != WAE_ERROR_NONE) {
70 ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
72 if (ret != WAE_ERROR_NONE) {
83 int _wae_decrypt_downloaded_web_application(const char *pPkgId, wae_app_type_e appType,
84 const unsigned char *pData, size_t dataLen,
85 unsigned char **ppDecryptedData, size_t *pDecDataLen)
87 int ret = WAE_ERROR_NONE;
88 unsigned char *pDek = NULL;
92 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
93 ret = WAE_ERROR_INVALID_PARAMETER;
97 if (pData == NULL || dataLen <= 0) {
98 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
99 ret = WAE_ERROR_INVALID_PARAMETER;
103 if (ppDecryptedData == NULL || pDecDataLen == NULL) {
104 WAE_SLOGE("Invalid Parameter. ppDecryptedData or pDecDataLen is NULL");
105 ret = WAE_ERROR_INVALID_PARAMETER;
109 ret = get_app_dek(pPkgId, appType, &pDek, &dekLen);
111 if (ret != WAE_ERROR_NONE) {
116 ret = decrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppDecryptedData, pDecDataLen);
118 if (ret != WAE_ERROR_NONE) {
129 int _wae_encrypt_preloaded_web_application(const char *pPkgId,
130 const unsigned char *pData, size_t dataLen,
131 unsigned char **ppEncryptedData, size_t *pEncDataLen)
134 int ret = WAE_ERROR_NONE;
135 unsigned char *pDek = NULL;
138 if (pPkgId == NULL) {
139 WAE_SLOGE("Invalid Parameter. pPkgId is NULL");
140 ret = WAE_ERROR_INVALID_PARAMETER;
144 if (pData == NULL || dataLen <= 0) {
145 WAE_SLOGE("Invalid Parameter. pData is NULL or invalid dataLen(%d)", dataLen);
146 ret = WAE_ERROR_INVALID_PARAMETER;
150 if (ppEncryptedData == NULL || pEncDataLen == NULL) {
151 WAE_SLOGE("Invalid Parameter. ppEncryptedData or pEncDataLen is NULL");
152 ret = WAE_ERROR_INVALID_PARAMETER;
156 ret = get_preloaded_app_dek(pPkgId, &pDek, &dekLen);
158 if (ret == WAE_ERROR_NO_KEY) {
159 ret = create_preloaded_app_dek(pPkgId, &pDek, &dekLen);
162 if (ret != WAE_ERROR_NONE) {
167 ret = encrypt_aes_cbc(pDek, dekLen, pData, dataLen, ppEncryptedData, pEncDataLen);
169 if (ret != WAE_ERROR_NONE) {
180 int _wae_decrypt_preloaded_web_application(const char *pPkgId, wae_app_type_e appType,
181 const unsigned char *pData, size_t dataLen,
182 unsigned char **ppDecryptedData, size_t *pDecDataLen)
184 // same with the decryption of downloaded web application
185 return _wae_decrypt_downloaded_web_application(pPkgId, appType,
186 pData, dataLen, ppDecryptedData, pDecDataLen);
189 int wae_encrypt_web_application(const char *pPkgId, wae_app_type_e appType,
190 const unsigned char *pData, size_t dataLen,
191 unsigned char **ppEncryptedData, size_t *pEncDataLen)
193 int ret = WAE_ERROR_NONE;
195 if (appType == WAE_PRELOADED_APP)
196 ret = _wae_encrypt_preloaded_web_application(pPkgId,
197 pData, dataLen, ppEncryptedData, pEncDataLen);
199 ret = _wae_encrypt_downloaded_web_application(pPkgId, appType,
200 pData, dataLen, ppEncryptedData, pEncDataLen);
202 WAE_SLOGI("Encrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
203 pPkgId, appType, dataLen, ret);
207 int wae_decrypt_web_application(const char *pPkgId, wae_app_type_e appType,
208 const unsigned char *pData, size_t dataLen,
209 unsigned char **ppDecryptedData, size_t *pDecDataLen)
211 int ret = WAE_ERROR_NONE;
213 if (appType == WAE_PRELOADED_APP)
214 ret = _wae_decrypt_preloaded_web_application(pPkgId, appType,
215 pData, dataLen, ppDecryptedData, pDecDataLen);
217 ret = _wae_decrypt_downloaded_web_application(pPkgId, appType,
218 pData, dataLen, ppDecryptedData, pDecDataLen);
220 WAE_SLOGI("Decrypt Web App. pkgId=%s, appType=%d, dataLen=%d, ret=%d",
221 pPkgId, appType, dataLen, ret);
226 int wae_remove_app_dek(const char *pPkgId, wae_app_type_e appType)
228 int ret = remove_app_dek(pPkgId, appType);
229 WAE_SLOGI("Remove APP DEK. pkgId=%s, appType=%d, ret=%d", pPkgId, appType, ret);