Modify to get encryption key requested by osp security team
[framework/web/wrt-commons.git] / modules / encryption / src / resource_encryption.cpp
index e89940e..8dc5284 100644 (file)
 
 #include <fcntl.h>
 #include <dpl/log/log.h>
+#include <dukgen.h>
 
 namespace {
 #define BITS_SIZE 128
-const char* ENCRYPTION_FILE = "_enc";
-const char* DECRYPTION_FILE = "_dec";
-
-inline std::string GetDefaultEncryptKeyPath() {
-    return "/opt/share/widget/data";
-}
+#define KEY_SIZE 16
 }
-namespace WRTEncryptor{
+namespace WRTEncryptor {
 ResourceEncryptor::ResourceEncryptor()
 {
     LogDebug("Started Encrytion");
@@ -48,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;
 }
@@ -59,44 +55,14 @@ void ResourceEncryptor::CreateEncryptionKey(std::string userKey)
         return;
     }
 
-    AES_KEY decKey;
-    const unsigned char* key = reinterpret_cast<unsigned char*>(
-                                    const_cast<char*>(userKey.c_str()));
+    char* pKey = GetDeviceUniqueKey(const_cast<char*>(userKey.c_str()),
+            userKey.size(), KEY_SIZE);
+    unsigned char *key = reinterpret_cast<unsigned char*>(pKey);
 
-    if ( 0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
+    if (0 > AES_set_encrypt_key(key, BITS_SIZE, &m_encKey)) {
         ThrowMsg(ResourceEncryptor::Exception::CreateEncKeyFailed,
-                "Failed to create encryption key");
-    }
-    if ( 0 > AES_set_decrypt_key(key, BITS_SIZE, &decKey)) {
-        ThrowMsg(ResourceEncryptor::Exception::CreateDecKeyFailed,
-                "Failed to create decryption key");
+                 "Failed to create encryption 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");
 }
 
@@ -106,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);
 }