Modify to get encryption key requested by osp security team
[framework/web/wrt-commons.git] / modules / encryption / src / resource_encryption.cpp
index 9110b7f..8dc5284 100644 (file)
 #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>
+#include <dukgen.h>
 
 namespace {
 #define BITS_SIZE 128
 #define KEY_SIZE 16
 }
-
-namespace WRTEncryptor{
+namespace WRTEncryptor {
 ResourceEncryptor::ResourceEncryptor()
 {
     LogDebug("Started Encrytion");
@@ -50,7 +44,7 @@ ResourceEncryptor::~ResourceEncryptor()
 int ResourceEncryptor::GetBlockSize(int inSize)
 {
     if ((inSize % AES_BLOCK_SIZE) != 0) {
-       return (( inSize/ AES_BLOCK_SIZE) +1) * AES_BLOCK_SIZE;
+        return (( inSize / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
     }
     return inSize;
 }
@@ -61,32 +55,14 @@ void ResourceEncryptor::CreateEncryptionKey(std::string userKey)
         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];
+    char* pKey = GetDeviceUniqueKey(const_cast<char*>(userKey.c_str()),
+            userKey.size(), KEY_SIZE);
+    unsigned char *key = reinterpret_cast<unsigned char*>(pKey);
 
-    int i=0;
-    while(bf->HasRemaining()) {
-        byte b;
-        bf->GetByte(b);
-        key[i] = b;
-        i++;
-    }
-    key[KEY_SIZE] = '\n';
-
-    if ( 0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
-        delete key;
+    if (0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
-                "Failed to create encryption key");
+                 "Failed to create encryption key");
     }
-    delete key;
-
     LogDebug("Success to create ecryption and decryption key");
 }
 
@@ -96,12 +72,14 @@ AES_KEY ResourceEncryptor::GetEncryptionkey()
 }
 
 void ResourceEncryptor::EncryptChunk(unsigned char*
-        inputBuf, unsigned char* encBuf, size_t chunkSize)
+                                     inputBuf,
+                                     unsigned char* encBuf,
+                                     size_t chunkSize)
 {
     Assert(inputBuf);
     Assert(encBuf);
 
-    unsigned char ivec[16] = {0, };
+    unsigned char ivec[16] = { 0, };
 
     AES_cbc_encrypt(inputBuf, encBuf, chunkSize, &m_encKey, ivec, AES_ENCRYPT);
 }