Add encrypt/decrypt API from trust zone
[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 #include <dukgen.h>
28 #include <FBaseByteBuffer.h>
29 #include <security/FSecCrypto_TrustZoneService.h>
30
31 namespace {
32 #define BITS_SIZE 128
33 #define KEY_SIZE 16
34 }
35 namespace WRTEncryptor {
36 ResourceEncryptor::ResourceEncryptor() :
37     m_getBuffer(NULL)
38 {
39     LogDebug("Started Encrytion");
40 }
41
42 ResourceEncryptor::~ResourceEncryptor()
43 {
44     LogDebug("Finished Encrytion");
45 }
46
47 int ResourceEncryptor::GetBlockSize(int inSize)
48 {
49     if ((inSize % AES_BLOCK_SIZE) != 0) {
50         return (( inSize / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
51     }
52     return inSize;
53 }
54
55 void ResourceEncryptor::CreateEncryptionKey(std::string userKey)
56 {
57     if (userKey.empty()) {
58         return;
59     }
60
61     char* pKey = GetDeviceUniqueKey(const_cast<char*>(userKey.c_str()),
62             userKey.size(), KEY_SIZE);
63     unsigned char *key = reinterpret_cast<unsigned char*>(pKey);
64
65     if (0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
66         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
67                  "Failed to create encryption key");
68     }
69     LogDebug("Success to create ecryption and decryption key");
70 }
71
72 AES_KEY ResourceEncryptor::GetEncryptionkey()
73 {
74     return m_encKey;
75 }
76
77 void ResourceEncryptor::EncryptChunk(unsigned char*
78                                      inputBuf,
79                                      unsigned char* encBuf,
80                                      size_t chunkSize)
81 {
82     Assert(inputBuf);
83     Assert(encBuf);
84
85     unsigned char ivec[16] = { 0, };
86
87     AES_cbc_encrypt(inputBuf, encBuf, chunkSize, &m_encKey, ivec, AES_ENCRYPT);
88 }
89
90 int ResourceEncryptor::EncryptChunkByTrustZone(
91         std::string pkgid,
92         const unsigned char *plainBuffer,
93         int pBufSize)
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(pBufSize);
108     const byte *pByte = reinterpret_cast<const byte*>(plainBuffer);
109     pBuf.SetArray(pByte, 0, pBufSize);
110     pBuf.Flip();
111
112     ByteBuffer *getBuffer =
113         pInstance->_TrustZoneService::EncryptN(appInfo, pBuf);
114     m_getBuffer = reinterpret_cast<void*>(getBuffer);
115
116     return getBuffer->GetRemaining();
117 }
118
119 void ResourceEncryptor::getEncStringByTrustZone(unsigned char *encBuffer)
120 {
121     using namespace Tizen::Base;
122     LogDebug("Get encrypted String");
123     ByteBuffer *buffer = reinterpret_cast<ByteBuffer*>(m_getBuffer);
124     memcpy(encBuffer, buffer->GetPointer(), buffer->GetRemaining());
125     buffer->Reset();
126 }
127 } //namespace ResourceEnc