*/
#include <stddef.h>
#include <dpl/encryption/resource_decryption.h>
-#ifdef Try
-#undef Try
-#endif
-#include <FSecSecretKey.h>
-#include <security/FSec_DeviceKeyGenerator.h>
#include <fcntl.h>
#include <string>
#include <dpl/exception.h>
namespace {
-#define BITS_SIZE 128
-#define KEY_SIZE 16
+inline std::string GetDefaultEncryptKeyPath() {
+ return "/opt/share/widget/data/";
+}
}
-
namespace WRTDecryptor{
-ResourceDecryptor::ResourceDecryptor()
+ResourceDecryptor::ResourceDecryptor() :
+ m_decKey(NULL)
{
LogDebug("Started Decryption");
}
-ResourceDecryptor::ResourceDecryptor(std::string userKey)
+ResourceDecryptor::ResourceDecryptor(std::string userKey) :
+ m_decKey(NULL)
{
LogDebug("Finished Decryption");
SetDecryptionKey(userKey);
ResourceDecryptor::~ResourceDecryptor()
{
+ delete m_decKey;
}
void ResourceDecryptor::SetDecryptionKey(std::string userKey)
{
- if (userKey.empty()) {
- return;
- }
- using namespace Tizen;
- using namespace Tizen::Base;
-
- Tizen::Base::String appId;
- appId.Format(userKey.size(), L"%s", userKey.c_str());
- Tizen::Security::ISecretKey* pSecretKey =
- Tizen::Security::_DeviceKeyGenerator::GenerateDeviceKeyN(appId, KEY_SIZE);
+ /* TODO : get key from secure storage */
+ std::string keyPath = GetDefaultEncryptKeyPath() + userKey + "_dec";
+ LogDebug("Description Key path : " << keyPath);
- Tizen::Base::ByteBuffer* bf = pSecretKey->GetEncodedN();
- unsigned char *key = new unsigned char[KEY_SIZE+1];
-
- int i=0;
- while(bf->HasRemaining()) {
- byte b;
- bf->GetByte(b);
- key[i] = b;
- i++;
+ FILE* fp = fopen(keyPath.c_str(), "rb");
+ if (fp == NULL) {
+ ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
+ "Failed to get decryption key");
}
- key[KEY_SIZE] = '\n';
- if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &m_decKey)) {
- delete key;
+ m_decKey = new AES_KEY;
+ size_t resultSize =fread(m_decKey, 1, sizeof(AES_KEY),fp);
+ if (resultSize!= sizeof(AES_KEY))
ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
- "Failed to create decryption key");
- }
- delete key;
+ "Failed to get AES key");
+
+ fclose(fp);
}
AES_KEY* ResourceDecryptor::GetDecryptionKey()
{
- return &m_decKey;
+ return m_decKey;
}
void ResourceDecryptor::GetDecryptedChunk(unsigned char*
inBuf, unsigned char* decBuf, size_t inBufSize)
{
Assert(decBuf);
- if (decBuf == NULL) {
+ Assert(m_decKey);
+ if (decBuf == NULL || m_decKey == NULL) {
ThrowMsg(ResourceDecryptor::Exception::EncryptionFailed,
"Failed to Get Decryption Chunk");
}
unsigned char ivec[16] = {0, };
- AES_cbc_encrypt(inBuf, decBuf, inBufSize, &m_decKey, ivec, AES_DECRYPT);
+ AES_cbc_encrypt(inBuf, decBuf, inBufSize, m_decKey, ivec, AES_DECRYPT);
LogDebug("Success decryption");
}
#include <stddef.h>
#include <dpl/encryption/resource_encryption.h>
-#ifdef Try
-#undef Try
-#endif
-#include <FSecSecretKey.h>
-#include <security/FSec_DeviceKeyGenerator.h>
-
#include <fcntl.h>
#include <dpl/log/log.h>
namespace {
#define BITS_SIZE 128
-#define KEY_SIZE 16
-}
+const char* ENCRYPTION_FILE = "_enc";
+const char* DECRYPTION_FILE = "_dec";
+inline std::string GetDefaultEncryptKeyPath() {
+ return "/opt/share/widget/data";
+}
+}
namespace WRTEncryptor{
ResourceEncryptor::ResourceEncryptor()
{
return;
}
- using namespace Tizen;
- using namespace Tizen::Base;
- Tizen::Base::String appId;
- appId.Format(userKey.size(), L"%s", userKey.c_str());
- Tizen::Security::ISecretKey* pSecretKey =
- Tizen::Security::_DeviceKeyGenerator::GenerateDeviceKeyN(appId, KEY_SIZE);
-
- Tizen::Base::ByteBuffer* bf = pSecretKey->GetEncodedN();
- unsigned char *key = new unsigned char[KEY_SIZE+1];
-
- int i=0;
- while(bf->HasRemaining()) {
- byte b;
- bf->GetByte(b);
- key[i] = b;
- i++;
- }
- key[KEY_SIZE] = '\n';
+ AES_KEY decKey;
+ const unsigned char* key = reinterpret_cast<unsigned char*>(
+ const_cast<char*>(userKey.c_str()));
if ( 0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
- delete key;
ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
"Failed to create encryption key");
}
- delete key;
+ if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &decKey)) {
+ ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFailed,
+ "Failed to create decryption key");
+ }
+
+ std::string encPath, decPath;
+
+ encPath = GetDefaultEncryptKeyPath() + "/" + userKey + ENCRYPTION_FILE;
+ decPath = GetDefaultEncryptKeyPath() + "/" + userKey + DECRYPTION_FILE;
+
+ /* TODO : save keys to secure storage */
+ LogDebug("Encryption Key path " << encPath);
+ LogDebug("Decryption Key path " << decPath);
+
+ FILE* encFp = fopen(encPath.c_str(), "wb");
+ if (encFp == NULL) {
+ ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFileFailed,
+ "Failed to save encryption key");
+ }
+ fwrite(&m_encKey, 1, sizeof(m_encKey), encFp);
+ fclose(encFp);
+
+ FILE* decFp = fopen(decPath.c_str(), "wb");
+ if (decFp == NULL) {
+ ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFileFailed,
+ "Failed to save decryption key");
+ }
+ fwrite(&decKey, 1, sizeof(decKey), decFp);
+ fclose(decFp);
LogDebug("Success to create ecryption and decryption key");
}