Additional Include: stddef.h
[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
30 namespace {
31 inline std::string GetDefaultEncryptKeyPath() {
32     return "/opt/share/widget/data/";
33 }
34 }
35 namespace WRTDecryptor{
36 ResourceDecryptor::ResourceDecryptor() :
37     m_decKey(NULL)
38 {
39     LogDebug("Started Decryption");
40 }
41
42 ResourceDecryptor::ResourceDecryptor(std::string userKey) :
43     m_decKey(NULL)
44 {
45     LogDebug("Finished Decryption");
46     SetDecryptionKey(userKey);
47 }
48
49 ResourceDecryptor::~ResourceDecryptor()
50 {
51     delete m_decKey;
52 }
53
54 void ResourceDecryptor::SetDecryptionKey(std::string userKey)
55 {
56     /* TODO : get key from secure storage */
57     std::string keyPath = GetDefaultEncryptKeyPath() + userKey + "_dec";
58     LogDebug("Description Key path : " << keyPath);
59
60     FILE* fp = fopen(keyPath.c_str(), "rb");
61     if (fp == NULL) {
62         ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
63                 "Failed to get decryption key");
64     }
65
66     m_decKey = new AES_KEY;
67     size_t resultSize =fread(m_decKey, 1, sizeof(AES_KEY),fp);
68     if (resultSize!= sizeof(AES_KEY))
69         ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
70                 "Failed to get AES key");
71
72     fclose(fp);
73 }
74
75 AES_KEY* ResourceDecryptor::GetDecryptionKey()
76 {
77     return m_decKey;
78 }
79
80 void ResourceDecryptor::GetDecryptedChunk(unsigned char*
81         inBuf, unsigned char* decBuf, size_t inBufSize)
82 {
83     Assert(decBuf);
84     Assert(m_decKey);
85     if (decBuf == NULL || m_decKey == NULL) {
86         ThrowMsg(ResourceDecryptor::Exception::EncryptionFailed,
87                 "Failed to Get Decryption Chunk");
88     }
89     unsigned char ivec[16] = {0, };
90
91     AES_cbc_encrypt(inBuf, decBuf, inBufSize, m_decKey, ivec, AES_DECRYPT);
92     LogDebug("Success decryption");
93 }
94
95 } //namespace WRTDecryptor