Add encrypt/decrypt API from trust zone
[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 #include <FBaseByteBuffer.h>
31 #include <security/FSecCrypto_TrustZoneService.h>
32
33 namespace {
34 #define BITS_SIZE 128
35 #define KEY_SIZE 16
36 }
37 namespace WRTDecryptor {
38 ResourceDecryptor::ResourceDecryptor() :
39     m_getBuffer(NULL)
40 {
41     LogDebug("Started Decryption");
42 }
43
44 ResourceDecryptor::ResourceDecryptor(std::string userKey)
45 {
46     LogDebug("Finished Decryption");
47     SetDecryptionKey(userKey);
48 }
49
50 ResourceDecryptor::~ResourceDecryptor()
51 {}
52
53 void ResourceDecryptor::SetDecryptionKey(std::string userKey)
54 {
55     if (userKey.empty()) {
56         return;
57     }
58
59     char* pKey = GetDeviceUniqueKey(const_cast<char*>(userKey.c_str()),
60             userKey.size(), KEY_SIZE);
61
62     unsigned char *key = reinterpret_cast<unsigned char*>(pKey);
63
64     if (0 > AES_set_decrypt_key(key, BITS_SIZE, &m_decKey)) {
65         ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
66                  "Failed to create decryption key");
67     }
68 }
69
70 AES_KEY* ResourceDecryptor::GetDecryptionKey()
71 {
72     return &m_decKey;
73 }
74
75 void ResourceDecryptor::GetDecryptedChunk(unsigned char*
76                                           inBuf,
77                                           unsigned char* decBuf,
78                                           size_t inBufSize)
79 {
80     Assert(decBuf);
81     if (decBuf == NULL) {
82         ThrowMsg(ResourceDecryptor::Exception::EncryptionFailed,
83                  "Failed to Get Decryption Chunk");
84     }
85     unsigned char ivec[16] = { 0, };
86
87     AES_cbc_encrypt(inBuf, decBuf, inBufSize, &m_decKey, ivec, AES_DECRYPT);
88     LogDebug("Success decryption");
89 }
90
91 int ResourceDecryptor::DecryptChunkByTrustZone(
92         std::string pkgid, const unsigned char* inBuffer,
93         int inBufSize)
94 {
95     using namespace Tizen::Base;
96
97     const byte *b_pkgid = reinterpret_cast<const byte*>(pkgid.c_str());
98     ByteBuffer appInfo;
99     appInfo.Construct(pkgid.length());
100     appInfo.SetArray(b_pkgid, 0, pkgid.length());
101     appInfo.Flip();
102
103     Tizen::Security::Crypto::_TrustZoneService* pInstance;
104     pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance();
105
106     ByteBuffer pBuf;
107     pBuf.Construct(inBufSize);
108     const byte *pByte = reinterpret_cast<const byte*>(inBuffer);
109     pBuf.SetArray(pByte, 0, inBufSize);
110     pBuf.Flip();
111
112     ByteBuffer *getBuffer = pInstance->_TrustZoneService::DecryptN(appInfo, pBuf);
113
114     m_getBuffer = reinterpret_cast<void*>(getBuffer);
115     return getBuffer->GetRemaining();
116 }
117
118 void ResourceDecryptor::getDecryptStringByTrustZone(unsigned char *decBuffer)
119 {
120     using namespace Tizen::Base;
121     LogDebug("Get decrypted string");
122     ByteBuffer *buffer = reinterpret_cast<ByteBuffer*>(m_getBuffer);
123     memcpy(decBuffer, buffer->GetPointer(), buffer->GetRemaining());
124     buffer->Reset();
125 }
126
127 } //namespace WRTDecryptor