Additional Include: stddef.h
[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 <stddef.h>
23 #include <dpl/encryption/resource_encryption.h>
24
25 #include <fcntl.h>
26 #include <dpl/log/log.h>
27
28 namespace {
29 #define BITS_SIZE 128
30 const char* ENCRYPTION_FILE = "_enc";
31 const char* DECRYPTION_FILE = "_dec";
32
33 inline std::string GetDefaultEncryptKeyPath() {
34     return "/opt/share/widget/data";
35 }
36 }
37 namespace WRTEncryptor{
38 ResourceEncryptor::ResourceEncryptor()
39 {
40     LogDebug("Started Encrytion");
41 }
42
43 ResourceEncryptor::~ResourceEncryptor()
44 {
45     LogDebug("Finished Encrytion");
46 }
47
48 int ResourceEncryptor::GetBlockSize(int inSize)
49 {
50     if ((inSize % AES_BLOCK_SIZE) != 0) {
51        return (( inSize/ AES_BLOCK_SIZE) +1) * AES_BLOCK_SIZE;
52     }
53     return inSize;
54 }
55
56 void ResourceEncryptor::CreateEncryptionKey(std::string userKey)
57 {
58     if (userKey.empty()) {
59         return;
60     }
61
62     AES_KEY decKey;
63     const unsigned char* key = reinterpret_cast<unsigned char*>(
64                                     const_cast<char*>(userKey.c_str()));
65
66     if ( 0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
67         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
68                 "Failed to create encryption key");
69     }
70     if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &decKey)) {
71         ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFailed,
72                 "Failed to create decryption key");
73     }
74
75     std::string encPath, decPath;
76
77     encPath = GetDefaultEncryptKeyPath() + "/" + userKey + ENCRYPTION_FILE;
78     decPath = GetDefaultEncryptKeyPath() + "/" + userKey + DECRYPTION_FILE;
79
80     /* TODO : save keys to secure storage */
81     LogDebug("Encryption Key path " << encPath);
82     LogDebug("Decryption Key path " << decPath);
83
84     FILE* encFp = fopen(encPath.c_str(), "wb");
85     if (encFp == NULL) {
86         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFileFailed,
87                 "Failed to save encryption key");
88     }
89     fwrite(&m_encKey, 1, sizeof(m_encKey), encFp);
90     fclose(encFp);
91
92     FILE* decFp = fopen(decPath.c_str(), "wb");
93     if (decFp == NULL) {
94         ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFileFailed,
95                 "Failed to save decryption key");
96     }
97
98     fwrite(&decKey, 1, sizeof(decKey), decFp);
99     fclose(decFp);
100     LogDebug("Success to create ecryption and decryption key");
101 }
102
103 AES_KEY ResourceEncryptor::GetEncryptionkey()
104 {
105     return m_encKey;
106 }
107
108 void ResourceEncryptor::EncryptChunk(unsigned char*
109         inputBuf, unsigned char* encBuf, size_t chunkSize)
110 {
111     Assert(inputBuf);
112     Assert(encBuf);
113
114     unsigned char ivec[16] = {0, };
115
116     AES_cbc_encrypt(inputBuf, encBuf, chunkSize, &m_encKey, ivec, AES_ENCRYPT);
117 }
118 } //namespace ResourceEnc