a2387051c094a58ae04953ef9ab53e535c2f9d6d
[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 <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 #define KEY_SIZE 16
31 }
32 namespace WRTEncryptor{
33 ResourceEncryptor::ResourceEncryptor()
34 {
35     LogDebug("Started Encrytion");
36 }
37
38 ResourceEncryptor::~ResourceEncryptor()
39 {
40     LogDebug("Finished Encrytion");
41 }
42
43 int ResourceEncryptor::GetBlockSize(int inSize)
44 {
45     if ((inSize % AES_BLOCK_SIZE) != 0) {
46        return (( inSize/ AES_BLOCK_SIZE) +1) * AES_BLOCK_SIZE;
47     }
48     return inSize;
49 }
50
51 void ResourceEncryptor::CreateEncryptionKey(std::string userKey)
52 {
53     if (userKey.empty()) {
54         return;
55     }
56
57     char **duk = calculate(const_cast<char*>(userKey.c_str()), userKey.size(), KEY_SIZE);
58     unsigned char *key = reinterpret_cast<unsigned char*>(*duk);
59
60     if ( 0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
61         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
62                 "Failed to create encryption key");
63     }
64     LogDebug("Success to create ecryption and decryption key");
65 }
66
67 AES_KEY ResourceEncryptor::GetEncryptionkey()
68 {
69     return m_encKey;
70 }
71
72 void ResourceEncryptor::EncryptChunk(unsigned char*
73         inputBuf, unsigned char* encBuf, size_t chunkSize)
74 {
75     Assert(inputBuf);
76     Assert(encBuf);
77
78     unsigned char ivec[16] = {0, };
79
80     AES_cbc_encrypt(inputBuf, encBuf, chunkSize, &m_encKey, ivec, AES_ENCRYPT);
81 }
82 } //namespace ResourceEnc