[Release] wrt-commons_0.2.139
[framework/web/wrt-commons.git] / modules / encryption / src / resource_decryption.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_decryption.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for resource decryption
21  */
22 #include <stddef.h>
23 #include <dpl/encryption/resource_decryption.h>
24
25 #include <fcntl.h>
26 #include <string>
27 #include <dpl/log/log.h>
28 #include <dpl/exception.h>
29 #include <dukgen.h>
30
31 namespace {
32 #define BITS_SIZE 128
33 #define KEY_SIZE 16
34 }
35 namespace WRTDecryptor {
36 ResourceDecryptor::ResourceDecryptor()
37 {
38     LogDebug("Started Decryption");
39 }
40
41 ResourceDecryptor::ResourceDecryptor(std::string userKey)
42 {
43     LogDebug("Finished Decryption");
44     SetDecryptionKey(userKey);
45 }
46
47 ResourceDecryptor::~ResourceDecryptor()
48 {}
49
50 void ResourceDecryptor::SetDecryptionKey(std::string userKey)
51 {
52     if (userKey.empty()) {
53         return;
54     }
55
56     char* pKey = GetDeviceUniqueKey(const_cast<char*>(userKey.c_str()),
57             userKey.size(), KEY_SIZE);
58
59     unsigned char *key = reinterpret_cast<unsigned char*>(pKey);
60
61     if (0 > AES_set_decrypt_key(key, BITS_SIZE, &m_decKey)) {
62         ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
63                  "Failed to create decryption key");
64     }
65 }
66
67 AES_KEY* ResourceDecryptor::GetDecryptionKey()
68 {
69     return &m_decKey;
70 }
71
72 void ResourceDecryptor::GetDecryptedChunk(unsigned char*
73                                           inBuf,
74                                           unsigned char* decBuf,
75                                           size_t inBufSize)
76 {
77     Assert(decBuf);
78     if (decBuf == NULL) {
79         ThrowMsg(ResourceDecryptor::Exception::EncryptionFailed,
80                  "Failed to Get Decryption Chunk");
81     }
82     unsigned char ivec[16] = { 0, };
83
84     AES_cbc_encrypt(inBuf, decBuf, inBufSize, &m_decKey, ivec, AES_DECRYPT);
85     LogDebug("Success decryption");
86 }
87 } //namespace WRTDecryptor