Modify get encryption/decryption key from device unique key
[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 #ifdef Try
25 #undef Try
26 #endif
27 #include <FSecSecretKey.h>
28 #include <security/FSec_DeviceKeyGenerator.h>
29
30 #include <fcntl.h>
31 #include <string>
32 #include <dpl/log/log.h>
33 #include <dpl/exception.h>
34
35 namespace {
36 #define BITS_SIZE 128
37 #define KEY_SIZE 16
38 }
39
40 namespace WRTDecryptor{
41 ResourceDecryptor::ResourceDecryptor()
42 {
43     LogDebug("Started Decryption");
44 }
45
46 ResourceDecryptor::ResourceDecryptor(std::string userKey)
47 {
48     LogDebug("Finished Decryption");
49     SetDecryptionKey(userKey);
50 }
51
52 ResourceDecryptor::~ResourceDecryptor()
53 {
54 }
55
56 void ResourceDecryptor::SetDecryptionKey(std::string userKey)
57 {
58     if (userKey.empty()) {
59         return;
60     }
61     using namespace Tizen;
62     using namespace Tizen::Base;
63
64     Tizen::Base::String appId;
65     appId.Format(userKey.size(), L"%s", userKey.c_str());
66     Tizen::Security::ISecretKey* pSecretKey =
67         Tizen::Security::_DeviceKeyGenerator::GenerateDeviceKeyN(appId, KEY_SIZE);
68
69     Tizen::Base::ByteBuffer* bf = pSecretKey->GetEncodedN();
70     unsigned char *key = new unsigned char[KEY_SIZE+1];
71
72     int i=0;
73     while(bf->HasRemaining()) {
74         byte b;
75         bf->GetByte(b);
76         key[i] = b;
77         i++;
78     }
79     key[KEY_SIZE] = '\n';
80
81     if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &m_decKey)) {
82         delete key;
83         ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
84                 "Failed to create decryption key");
85     }
86     delete key;
87 }
88
89 AES_KEY* ResourceDecryptor::GetDecryptionKey()
90 {
91     return &m_decKey;
92 }
93
94 void ResourceDecryptor::GetDecryptedChunk(unsigned char*
95         inBuf, unsigned char* decBuf, size_t inBufSize)
96 {
97     Assert(decBuf);
98     if (decBuf == NULL) {
99         ThrowMsg(ResourceDecryptor::Exception::EncryptionFailed,
100                 "Failed to Get Decryption Chunk");
101     }
102     unsigned char ivec[16] = {0, };
103
104     AES_cbc_encrypt(inBuf, decBuf, inBufSize, &m_decKey, ivec, AES_DECRYPT);
105     LogDebug("Success decryption");
106 }
107
108 } //namespace WRTDecryptor