Add encrypt/decrypt API from trust zone
authorSoyoung Kim <sy037.kim@samsung.com>
Tue, 5 Mar 2013 06:35:59 +0000 (15:35 +0900)
committerGerrit Code Review <gerrit2@kim11>
Fri, 8 Mar 2013 08:00:19 +0000 (17:00 +0900)
[Issue#] N/A
[Problem] N/A
[Cause] N/A
[Solution] Add API for encryption/decryption from trust zone.
[SCMRequest] N/A

CMakeLists.txt
build/encryption/CMakeLists.txt
modules/encryption/include/dpl/encryption/resource_decryption.h
modules/encryption/include/dpl/encryption/resource_encryption.h
modules/encryption/src/resource_decryption.cpp
modules/encryption/src/resource_encryption.cpp
packaging/wrt-commons.spec

index 45cc829..d36d0dc 100644 (file)
@@ -46,6 +46,7 @@ ELSE(DPL_LOG AND NOT CMAKE_BUILD_TYPE MATCHES "profiling")
 ENDIF(DPL_LOG AND NOT CMAKE_BUILD_TYPE MATCHES "profiling")
 
 OPTION(WITH_TESTS "Build tests" OFF)
+    ADD_DEFINITIONS("-DDPL_LOGS_ENABLED") #@@@@@@@@@@@@@
 
 # Compiler flags
 SET(CMAKE_C_FLAGS_PROFILING    "-O2")
index 7949864..e96b3dd 100644 (file)
@@ -23,6 +23,7 @@ INCLUDE(FindPkgConfig)
 PKG_CHECK_MODULES(SYS_ENCRYPTION
     dlog
     openssl
+    osp-appfw
     REQUIRED
 )
 
@@ -56,6 +57,7 @@ TARGET_LINK_LIBRARIES(${TARGET_DPL_ENCRYPTION}
 
 TARGET_LINK_LIBRARIES(${TARGET_DPL_ENCRYPTION} "-ldukgenerator" )
 TARGET_LINK_LIBRARIES(${TARGET_DPL_ENCRYPTION} "-lcryptsvc" )
+TARGET_LINK_LIBRARIES(${TARGET_DPL_ENCRYPTION} -L/usr/lib/osp -losp-appfw )
 
 # Target library properties
 SET_TARGET_PROPERTIES(${TARGET_DPL_ENCRYPTION} PROPERTIES
index a6d7af0..1f10fdb 100644 (file)
@@ -28,8 +28,6 @@
 #include <openssl/sha.h>
 #include <dpl/exception.h>
 
-extern char** calculate(char*pappId, int idLen, int keyLen);
-
 namespace WRTDecryptor {
 class ResourceDecryptor
 {
@@ -51,9 +49,17 @@ class ResourceDecryptor
                            unsigned char* decBuf,
                            size_t chunkSize);
 
+    /* TrustZone */
+    int DecryptChunkByTrustZone(
+            std::string pkgid,
+            const unsigned char *inBuffer,
+            int inBufSize);
+    void getDecryptStringByTrustZone(unsigned char *decBuffer);
+
   private:
     AES_KEY* GetDecryptionKey();
     AES_KEY m_decKey;
+    void *m_getBuffer;
 };
 } //namespace WRTDecryptor
 
index f2e4988..32d7132 100644 (file)
@@ -28,8 +28,6 @@
 #include <openssl/sha.h>
 #include <dpl/exception.h>
 
-extern char** calculate(char*pappId, int idLen, int keyLen);
-
 namespace WRTEncryptor {
 class ResourceEncryptor
 {
@@ -53,9 +51,17 @@ class ResourceEncryptor
     void EncryptChunk(unsigned char* inputBuf, unsigned char* encBuf, size_t
                       chunkSize);
 
+    /* TrustZone */
+    int EncryptChunkByTrustZone(
+            std::string pkgid,
+            const unsigned char *plainBuffer,
+            int pBufSize);
+    void getEncStringByTrustZone(unsigned char *encBuffer);
+
   private:
     AES_KEY GetEncryptionkey();
     AES_KEY m_encKey;
+    void *m_getBuffer;
 };
 } //namespace WRTEncryptor
 
index c599e64..d51adfb 100644 (file)
 #include <dpl/log/log.h>
 #include <dpl/exception.h>
 #include <dukgen.h>
+#include <FBaseByteBuffer.h>
+#include <security/FSecCrypto_TrustZoneService.h>
 
 namespace {
 #define BITS_SIZE 128
 #define KEY_SIZE 16
 }
 namespace WRTDecryptor {
-ResourceDecryptor::ResourceDecryptor()
+ResourceDecryptor::ResourceDecryptor() :
+    m_getBuffer(NULL)
 {
     LogDebug("Started Decryption");
 }
@@ -84,4 +87,41 @@ void ResourceDecryptor::GetDecryptedChunk(unsigned char*
     AES_cbc_encrypt(inBuf, decBuf, inBufSize, &m_decKey, ivec, AES_DECRYPT);
     LogDebug("Success decryption");
 }
+
+int ResourceDecryptor::DecryptChunkByTrustZone(
+        std::string pkgid, const unsigned char* inBuffer,
+        int inBufSize)
+{
+    using namespace Tizen::Base;
+
+    const byte *b_pkgid = reinterpret_cast<const byte*>(pkgid.c_str());
+    ByteBuffer appInfo;
+    appInfo.Construct(pkgid.length());
+    appInfo.SetArray(b_pkgid, 0, pkgid.length());
+    appInfo.Flip();
+
+    Tizen::Security::Crypto::_TrustZoneService* pInstance;
+    pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance();
+
+    ByteBuffer pBuf;
+    pBuf.Construct(inBufSize);
+    const byte *pByte = reinterpret_cast<const byte*>(inBuffer);
+    pBuf.SetArray(pByte, 0, inBufSize);
+    pBuf.Flip();
+
+    ByteBuffer *getBuffer = pInstance->_TrustZoneService::DecryptN(appInfo, pBuf);
+
+    m_getBuffer = reinterpret_cast<void*>(getBuffer);
+    return getBuffer->GetRemaining();
+}
+
+void ResourceDecryptor::getDecryptStringByTrustZone(unsigned char *decBuffer)
+{
+    using namespace Tizen::Base;
+    LogDebug("Get decrypted string");
+    ByteBuffer *buffer = reinterpret_cast<ByteBuffer*>(m_getBuffer);
+    memcpy(decBuffer, buffer->GetPointer(), buffer->GetRemaining());
+    buffer->Reset();
+}
+
 } //namespace WRTDecryptor
index 8dc5284..67f9061 100644 (file)
 #include <fcntl.h>
 #include <dpl/log/log.h>
 #include <dukgen.h>
+#include <FBaseByteBuffer.h>
+#include <security/FSecCrypto_TrustZoneService.h>
 
 namespace {
 #define BITS_SIZE 128
 #define KEY_SIZE 16
 }
 namespace WRTEncryptor {
-ResourceEncryptor::ResourceEncryptor()
+ResourceEncryptor::ResourceEncryptor() :
+    m_getBuffer(NULL)
 {
     LogDebug("Started Encrytion");
 }
@@ -83,4 +86,42 @@ void ResourceEncryptor::EncryptChunk(unsigned char*
 
     AES_cbc_encrypt(inputBuf, encBuf, chunkSize, &m_encKey, ivec, AES_ENCRYPT);
 }
+
+int ResourceEncryptor::EncryptChunkByTrustZone(
+        std::string pkgid,
+        const unsigned char *plainBuffer,
+        int pBufSize)
+{
+    using namespace Tizen::Base;
+
+    const byte *b_pkgid = reinterpret_cast<const byte*>(pkgid.c_str());
+    ByteBuffer appInfo;
+    appInfo.Construct(pkgid.length());
+    appInfo.SetArray(b_pkgid, 0, pkgid.length());
+    appInfo.Flip();
+
+    Tizen::Security::Crypto::_TrustZoneService* pInstance;
+    pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance();
+
+    ByteBuffer pBuf;
+    pBuf.Construct(pBufSize);
+    const byte *pByte = reinterpret_cast<const byte*>(plainBuffer);
+    pBuf.SetArray(pByte, 0, pBufSize);
+    pBuf.Flip();
+
+    ByteBuffer *getBuffer =
+        pInstance->_TrustZoneService::EncryptN(appInfo, pBuf);
+    m_getBuffer = reinterpret_cast<void*>(getBuffer);
+
+    return getBuffer->GetRemaining();
+}
+
+void ResourceEncryptor::getEncStringByTrustZone(unsigned char *encBuffer)
+{
+    using namespace Tizen::Base;
+    LogDebug("Get encrypted String");
+    ByteBuffer *buffer = reinterpret_cast<ByteBuffer*>(m_getBuffer);
+    memcpy(encBuffer, buffer->GetPointer(), buffer->GetRemaining());
+    buffer->Reset();
+}
 } //namespace ResourceEnc
index b072049..38f1302 100644 (file)
@@ -23,9 +23,12 @@ BuildRequires:  pkgconfig(libxml-2.0)
 BuildRequires:  pkgconfig(openssl)
 BuildRequires:  pkgconfig(libiri)
 BuildRequires:  pkgconfig(libidn)
+BuildRequires:  pkgconfig(osp-appfw)
 BuildRequires:  libcryptsvc-devel
 BuildRequires:  dukgenerator-devel
+BuildRequires:  osp-appfw-internal-devel
 Requires: libcryptsvc
+Requires: osp-appfw
 
 %description
 Wrt common library