Unification of import methods in gstore 05/189905/7
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 21 Sep 2018 10:41:37 +0000 (12:41 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 3 Oct 2018 12:58:27 +0000 (12:58 +0000)
Change-Id: I31dca502533360b759d6aea20e75a9e823eccc34

15 files changed:
src/manager/crypto/generic-backend/gstore.h
src/manager/crypto/sw-backend/store.cpp
src/manager/crypto/sw-backend/store.h
src/manager/crypto/tz-backend/internals.cpp
src/manager/crypto/tz-backend/internals.h
src/manager/crypto/tz-backend/store.cpp
src/manager/crypto/tz-backend/store.h
src/manager/crypto/tz-backend/tz-context.cpp
src/manager/crypto/tz-backend/tz-context.h
src/manager/initial-values/BufferHandler.h
src/manager/initial-values/InitialValueHandler.cpp
src/manager/service/ckm-logic.cpp
tests/test_crypto-logic.cpp
tests/test_generic-backend.cpp
tests/test_tz-backend.cpp

index 564e4dd..2984fdc 100644 (file)
@@ -55,13 +55,13 @@ public:
        {
                ThrowErr(Exc::Crypto::OperationNotSupported);
        }
-       virtual Token import(const Data &, const Password &)
-       {
-               ThrowErr(Exc::Crypto::OperationNotSupported);
-       }
-       virtual Token importEncrypted(const Data &,
-                                     const Password &,
-                                     const RawBuffer & /* iv */)
+
+       /*
+        * IV parameter makes sense only on device with built in key.
+        * IV parameter is used for decryption of Data.
+        * If Data is not encrypted it's ok to pass empty IV.
+        */
+       virtual Token import(const Data &, const Password &, const RawBuffer & /* iv */)
        {
                ThrowErr(Exc::Crypto::OperationNotSupported);
        }
index 6930873..b41b8fd 100644 (file)
@@ -219,8 +219,12 @@ Token Store::generateSKey(const CryptoAlgorithm &algorithm,
        return Token(m_backendId, ret.type, pack(ret.buffer, pass));
 }
 
-Token Store::import(const Data &data, const Password &pass)
+Token Store::import(const Data &data, const Password &pass, const RawBuffer &iv)
 {
+       if (!iv.empty())
+               ThrowErr(Exc::Crypto::OperationNotSupported,
+                       "Encrypted import is not yet supported on software backend!");
+
        return Token(m_backendId, data.type, pack(data.data, pass));
 }
 
index a12e561..82798cd 100644 (file)
@@ -35,7 +35,7 @@ public:
        virtual TokenPair generateAKey(const CryptoAlgorithm &, const Password &,
                                                                   const Password &);
        virtual Token generateSKey(const CryptoAlgorithm &, const Password &);
-       virtual Token import(const Data &data, const Password &);
+       virtual Token import(const Data &data, const Password &, const RawBuffer &);
        virtual void destroy(const Token &) {}
 
 private:
index 578241d..03189d4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -119,8 +119,9 @@ void destroyKey(const RawBuffer &key)
 }
 
 RawBuffer importKey(const Data &data,
+                                       const RawBuffer &encIV,
                                        const Password &pwd,
-                                       const RawBuffer &iv,
+                                       const RawBuffer &pwdIV,
                                        RawBuffer &tag)
 {
        tz_algo_type algo = getAlgType(data.type);
@@ -129,8 +130,9 @@ RawBuffer importKey(const Data &data,
        RawBuffer pwdBuf(pwd.begin(), pwd.end());
        TrustZoneContext::Instance().importKey(algo,
                                                                                data.data,
+                                                                               encIV,
                                                                                pwdBuf,
-                                                                               iv,
+                                                                               pwdIV,
                                                                                result,
                                                                                tag);
        return result;
index d9748e9..1fed4b0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -46,8 +46,9 @@ Data generateSKey(const CryptoAlgorithm &alg,
                                const RawBuffer &iv,
                                RawBuffer &tag);
 RawBuffer importKey(const Data &key,
+                                       const RawBuffer &encIV,
                                        const Password &pwd,
-                                       const RawBuffer &iv,
+                                       const RawBuffer &pwdIV,
                                        RawBuffer &tag);
 
 RawBuffer importData(const Data &data,
index 3404acb..c688024 100644 (file)
@@ -142,7 +142,7 @@ Token Store::generateSKey(const CryptoAlgorithm &alg, const Password &pass)
        return Token(m_backendId, ret.type, pack(ret.data, pass, iv, tag));
 }
 
-Token Store::import(const Data &data, const Password &pass)
+Token Store::import(const Data &data, const Password &pass, const RawBuffer &encIV)
 {
        if (data.type.isBinaryData()) {
                RawBuffer iv;
@@ -168,18 +168,10 @@ Token Store::import(const Data &data, const Password &pass)
                iv = Internals::generateIV();
        }
 
-       RawBuffer keyId = Internals::importKey(data, pass, iv, tag);
+       RawBuffer keyId = Internals::importKey(data, encIV, pass, iv, tag);
        return Token(m_backendId, data.type, pack(keyId, pass, iv, tag));
 }
 
-Token Store::importEncrypted(const Data &,
-                             const Password &,
-                             const RawBuffer &)
-{
-       ThrowErr(Exc::Crypto::OperationNotSupported,
-               "Encrypted import is not yet supported on TrustZone backend!");
-}
-
 void Store::destroy(const Token &token)
 {
        RawBuffer id = unpackData(token.data);
index 706c341..4d44b6b 100644 (file)
@@ -35,10 +35,7 @@ public:
        virtual TokenPair generateAKey(const CryptoAlgorithm &, const Password &,
                                                                   const Password &);
        virtual Token generateSKey(const CryptoAlgorithm &, const Password &);
-       virtual Token import(const Data &, const Password &);
-       virtual Token importEncrypted(const Data &,
-                                     const Password &,
-                                     const RawBuffer &);
+       virtual Token import(const Data &, const Password &, const RawBuffer &);
        virtual void destroy(const Token &);
 
        // TODO device key ID is needed here to support importEncrypted
index 60dcd80..1387491 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -257,11 +257,13 @@ void TrustZoneContext::generateSKeyPwd(tz_algo_type algo,
 
 void TrustZoneContext::importKey(tz_algo_type algo,
                                                                const RawBuffer &key,
+                                                               const RawBuffer &encIV,
                                                                const RawBuffer &pwd,
-                                                               const RawBuffer &iv,
+                                                               const RawBuffer &pwdIV,
                                                                RawBuffer &keyId,
                                                                RawBuffer &pwdTag)
 {
+       (void)encIV;
        // command ID = CMD_IMPORT_KEY
        //
        // TEEC_Operation layout:
@@ -280,7 +282,7 @@ void TrustZoneContext::importKey(tz_algo_type algo,
        if (!pwd.empty()) {
                bufSize.with_pwd_data = true;
                bufSize.pwd_size = static_cast<uint32_t>(pwd.size());
-               bufSize.pwd_iv_size = static_cast<uint32_t>(iv.size());
+               bufSize.pwd_iv_size = static_cast<uint32_t>(pwdIV.size());
        }
        uint32_t inMemorySize = KM_CalcBufferSize(bufSize);
        TrustZoneMemory inMemory(m_Context, inMemorySize, TEEC_MEM_INPUT);
@@ -303,7 +305,7 @@ void TrustZoneContext::importKey(tz_algo_type algo,
        }
 
        if (!pwd.empty()) {
-               ret = KM_ParamsSerializePwdData(input, pwd.data(), pwd.size(), iv.data(), iv.size(),
+               ret = KM_ParamsSerializePwdData(input, pwd.data(), pwd.size(), pwdIV.data(), pwdIV.size(),
                                                                                nullptr, 0, Params::DERIVED_KEY_LENGTH_BITS,
                                                                                Params::DERIVED_KEY_ITERATIONS, bufSize.tag_size * 8);
                if (ret) {
index 2dd2497..cd7ae74 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -49,8 +49,9 @@ public:
                                                RawBuffer &pwdTag);
        void importKey(tz_algo_type algo,
                                        const RawBuffer &key,
+                                       const RawBuffer &encIV,
                                        const RawBuffer &pwd,
-                                       const RawBuffer &iv,
+                                       const RawBuffer &pwdIV,
                                        RawBuffer &keyId,
                                        RawBuffer &pwdTag);
 
index 572244d..08bca52 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2015 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -47,14 +47,6 @@ public:
                return m_data;
        }
 
-       bool isEncrypted() const
-       {
-               if (m_encoding == EncodingType::ENCRYPTED)
-                       return true;
-
-               return false;
-       }
-
        const RawBuffer &getIV() const
        {
                return m_IV;
index acb2e4b..7853d39 100644 (file)
@@ -78,14 +78,9 @@ void InitialValueHandler::End()
        // save data
        Policy policy(m_password, m_exportable, m_backend);
 
-       RawBuffer iv;
-
-       if (m_bufferHandler->isEncrypted())
-               iv = m_bufferHandler->getIV();
-
        int ec = m_db_logic.importInitialData(m_name,
                                              Crypto::Data(getDataType(), m_bufferHandler->getData()),
-                                             iv,
+                                             m_bufferHandler->getIV(),
                                              policy);
 
        if (CKM_API_SUCCESS != ec) {
index e988b7f..98248ec 100644 (file)
@@ -404,7 +404,8 @@ DB::Row CKMLogic::createEncryptedRow(
 
        // do not encrypt data with password during cc_mode on
        Token token = store.import(data,
-                                                          m_accessControl.isCCMode() ? "" : policy.password);
+                                                          m_accessControl.isCCMode() ? "" : policy.password,
+                                                          RawBuffer());
        DB::Row row(std::move(token), name, owner,
                                static_cast<int>(policy.extractable));
        crypto.encryptRow(row);
@@ -803,7 +804,7 @@ Crypto::GObjUPtr CKMLogic::rowToObject(
                store.destroy(row);
 
                // import it to store with new scheme: data -> pass(data)
-               Token token = store.import(Crypto::Data(row.dataType, row.data), pass);
+               Token token = store.import(Crypto::Data(row.dataType, row.data), pass, RawBuffer());
 
                // get it from the store (it can be different than the data we imported into store)
                obj = store.getObject(token, pass);
@@ -1180,17 +1181,19 @@ int CKMLogic::importInitialData(
                Token token;
 
                if (iv.empty()) {
+            // Data are not encrypted, let's try to verify them
                        Crypto::Data binaryData;
 
                        if (CKM_API_SUCCESS != (retCode = toBinaryData(data, binaryData)))
                                return retCode;
 
                        token = store.import(binaryData,
-                                            m_accessControl.isCCMode() ? "" : policy.password);
+                                                                m_accessControl.isCCMode() ? "" : policy.password,
+                                                                iv);
                } else {
-                       token = store.importEncrypted(data,
-                                                     m_accessControl.isCCMode() ? "" : policy.password,
-                                                     iv);
+                       token = store.import(data,
+                                                                m_accessControl.isCCMode() ? "" : policy.password,
+                                                                iv);
                }
 
                DB::Row row(std::move(token), name, CLIENT_ID_SYSTEM,
index 5784365..f519e27 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2017 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -91,7 +91,7 @@ BOOST_AUTO_TEST_CASE(row_encryption)
        Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10));
        Crypto::Decider decider;
        Crypto::GStore &store = decider.getStore(data.type, policy);
-       Token token = store.import(data, policy.password);
+       Token token = store.import(data, policy.password, RawBuffer());
 
        Name name = "test_data";
        ClientId owner = "test_owner";
@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(row_encryption_negatives)
        Crypto::Data data(DataType(DataType::Type::BINARY_DATA), createRandom(10));
        Crypto::Decider decider;
        Crypto::GStore &store = decider.getStore(data.type, policy);
-       Token token = store.import(data, policy.password);
+       Token token = store.import(data, policy.password, RawBuffer());
 
        Name name = "test_data";
        ClientId owner = "test_owner";
index 114d794..0b39bf3 100644 (file)
@@ -72,10 +72,7 @@ BOOST_AUTO_TEST_CASE(gstore)
                                                Exc::Crypto::OperationNotSupported);
        BOOST_REQUIRE_THROW(store.generateSKey(CryptoAlgorithm(), Password()),
                                                Exc::Crypto::OperationNotSupported);
-       BOOST_REQUIRE_THROW(store.import(Crypto::Data(), Password()),
-                                               Exc::Crypto::OperationNotSupported);
-       BOOST_REQUIRE_THROW(store.importEncrypted(Crypto::Data(), Password(),
-                                                 RawBuffer()),
+       BOOST_REQUIRE_THROW(store.import(Crypto::Data(), Password(), RawBuffer()),
                                                Exc::Crypto::OperationNotSupported);
        BOOST_REQUIRE_THROW(store.destroy(Token()),
                                                Exc::Crypto::OperationNotSupported);
index 853326c..880211b 100644 (file)
@@ -30,9 +30,7 @@ BOOST_AUTO_TEST_CASE(store)
                                                Exc::Crypto::OperationNotSupported);
        BOOST_REQUIRE_THROW(store.generateAKey(CryptoAlgorithm(), Password(), Password()),
                                                Exc::Crypto::OperationNotSupported);
-       BOOST_REQUIRE_THROW(store.import(Data(), Password()),
-                                               Exc::Crypto::OperationNotSupported);
-       BOOST_REQUIRE_THROW(store.importEncrypted(Data(), Password(), RawBuffer()),
+       BOOST_REQUIRE_THROW(store.import(Data(), Password(), RawBuffer()),
                                                Exc::Crypto::OperationNotSupported);
        BOOST_REQUIRE_NO_THROW(store.destroy(Token()));
 }