2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file resource_decryption.cpp
18 * @author Soyoung Kim (sy037.kim@samsung.com)
20 * @brief Implementation file for resource decryption
23 #include <dpl/encryption/resource_decryption.h>
27 #include <FSecSecretKey.h>
28 #include <security/FSec_DeviceKeyGenerator.h>
32 #include <dpl/log/log.h>
33 #include <dpl/exception.h>
40 namespace WRTDecryptor{
41 ResourceDecryptor::ResourceDecryptor()
43 LogDebug("Started Decryption");
46 ResourceDecryptor::ResourceDecryptor(std::string userKey)
48 LogDebug("Finished Decryption");
49 SetDecryptionKey(userKey);
52 ResourceDecryptor::~ResourceDecryptor()
56 void ResourceDecryptor::SetDecryptionKey(std::string userKey)
58 if (userKey.empty()) {
61 using namespace Tizen;
62 using namespace Tizen::Base;
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);
69 Tizen::Base::ByteBuffer* bf = pSecretKey->GetEncodedN();
70 unsigned char *key = new unsigned char[KEY_SIZE+1];
73 while(bf->HasRemaining()) {
81 if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &m_decKey)) {
83 ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
84 "Failed to create decryption key");
89 AES_KEY* ResourceDecryptor::GetDecryptionKey()
94 void ResourceDecryptor::GetDecryptedChunk(unsigned char*
95 inBuf, unsigned char* decBuf, size_t inBufSize)
99 ThrowMsg(ResourceDecryptor::Exception::EncryptionFailed,
100 "Failed to Get Decryption Chunk");
102 unsigned char ivec[16] = {0, };
104 AES_cbc_encrypt(inBuf, decBuf, inBufSize, &m_decKey, ivec, AES_DECRYPT);
105 LogDebug("Success decryption");
108 } //namespace WRTDecryptor