2 * Copyright (c) 2011 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.
17 * @file resource_encryption.cpp
18 * @author Soyoung Kim (sy037.kim@samsung.com)
20 * @brief Implementation file for resource encryption
22 #include <dpl/encryption/resource_encryption.h>
25 #include <dpl/log/log.h>
29 const char* ENCRYPTION_FILE = "_enc";
30 const char* DECRYPTION_FILE = "_dec";
32 inline std::string GetDefaultEncryptKeyPath() {
33 return "/opt/share/widget/data";
36 namespace WRTEncryptor{
37 ResourceEncryptor::ResourceEncryptor()
39 LogDebug("Started Encrytion");
42 ResourceEncryptor::~ResourceEncryptor()
44 LogDebug("Finished Encrytion");
47 int ResourceEncryptor::GetBlockSize(int inSize)
49 if ((inSize % AES_BLOCK_SIZE) != 0) {
50 return (( inSize/ AES_BLOCK_SIZE) +1) * AES_BLOCK_SIZE;
55 void ResourceEncryptor::CreateEncryptionKey(std::string userKey)
57 if (userKey.empty()) {
62 const unsigned char* key = reinterpret_cast<unsigned char*>(
63 const_cast<char*>(userKey.c_str()));
65 if ( 0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
66 ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
67 "Failed to create encryption key");
69 if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &decKey)) {
70 ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFailed,
71 "Failed to create decryption key");
74 std::string encPath, decPath;
76 encPath = GetDefaultEncryptKeyPath() + "/" + userKey + ENCRYPTION_FILE;
77 decPath = GetDefaultEncryptKeyPath() + "/" + userKey + DECRYPTION_FILE;
79 /* TODO : save keys to secure storage */
80 LogDebug("Encryption Key path " << encPath);
81 LogDebug("Decryption Key path " << decPath);
83 FILE* encFp = fopen(encPath.c_str(), "wb");
85 ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFileFailed,
86 "Failed to save encryption key");
88 fwrite(&m_encKey, 1, sizeof(m_encKey), encFp);
91 FILE* decFp = fopen(decPath.c_str(), "wb");
93 ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFileFailed,
94 "Failed to save decryption key");
97 fwrite(&decKey, 1, sizeof(decKey), decFp);
99 LogDebug("Success to create ecryption and decryption key");
102 AES_KEY ResourceEncryptor::GetEncryptionkey()
107 void ResourceEncryptor::EncryptChunk(unsigned char*
108 inputBuf, unsigned char* encBuf, size_t chunkSize)
113 unsigned char ivec[16] = {0, };
115 AES_cbc_encrypt(inputBuf, encBuf, chunkSize, &m_encKey, ivec, AES_ENCRYPT);
117 } //namespace ResourceEnc