8f6de46606b28bd33d61636793716a493c671664
[platform/framework/web/wrt-commons.git] / modules / encryption / src / resource_encryption.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file    resource_encryption.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for resource encryption
21  */
22 #include <dpl/encryption/resource_encryption.h>
23
24 #include <fcntl.h>
25 #include <dpl/log/log.h>
26
27 namespace {
28 #define BITS_SIZE 128
29 const char* ENCRYPTION_FILE = "_enc";
30 const char* DECRYPTION_FILE = "_dec";
31
32 inline std::string GetDefaultEncryptKeyPath() {
33     return "/opt/share/widget/data";
34 }
35 }
36 namespace WRTEncryptor{
37 ResourceEncryptor::ResourceEncryptor()
38 {
39     LogDebug("Started Encrytion");
40 }
41
42 ResourceEncryptor::~ResourceEncryptor()
43 {
44     LogDebug("Finished Encrytion");
45 }
46
47 int ResourceEncryptor::GetBlockSize(int inSize)
48 {
49     if ((inSize % AES_BLOCK_SIZE) != 0) {
50        return (( inSize/ AES_BLOCK_SIZE) +1) * AES_BLOCK_SIZE;
51     }
52     return inSize;
53 }
54
55 void ResourceEncryptor::CreateEncryptionKey(std::string userKey)
56 {
57     if (userKey.empty()) {
58         return;
59     }
60
61     AES_KEY decKey;
62     const unsigned char* key = reinterpret_cast<unsigned char*>(
63                                     const_cast<char*>(userKey.c_str()));
64
65     if ( 0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
66         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
67                 "Failed to create encryption key");
68     }
69     if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &decKey)) {
70         ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFailed,
71                 "Failed to create decryption key");
72     }
73
74     std::string encPath, decPath;
75
76     encPath = GetDefaultEncryptKeyPath() + "/" + userKey + ENCRYPTION_FILE;
77     decPath = GetDefaultEncryptKeyPath() + "/" + userKey + DECRYPTION_FILE;
78
79     /* TODO : save keys to secure storage */
80     LogDebug("Encryption Key path " << encPath);
81     LogDebug("Decryption Key path " << decPath);
82
83     FILE* encFp = fopen(encPath.c_str(), "wb");
84     if (encFp == NULL) {
85         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFileFailed,
86                 "Failed to save encryption key");
87     }
88     fwrite(&m_encKey, 1, sizeof(m_encKey), encFp);
89     fclose(encFp);
90
91     FILE* decFp = fopen(decPath.c_str(), "wb");
92     if (decFp == NULL) {
93         ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFileFailed,
94                 "Failed to save decryption key");
95     }
96
97     fwrite(&decKey, 1, sizeof(decKey), decFp);
98     fclose(decFp);
99     LogDebug("Success to create ecryption and decryption key");
100 }
101
102 AES_KEY ResourceEncryptor::GetEncryptionkey()
103 {
104     return m_encKey;
105 }
106
107 void ResourceEncryptor::EncryptChunk(unsigned char*
108         inputBuf, unsigned char* encBuf, size_t chunkSize)
109 {
110     Assert(inputBuf);
111     Assert(encBuf);
112
113     unsigned char ivec[16] = {0, };
114
115     AES_cbc_encrypt(inputBuf, encBuf, chunkSize, &m_encKey, ivec, AES_ENCRYPT);
116 }
117 } //namespace ResourceEnc