CKM: Update GCM IV tests
[platform/core/test/security-tests.git] / src / ckm / unprivileged / encryption-decryption.cpp
index c183c81..037ff08 100644 (file)
@@ -23,6 +23,7 @@
 #include <string>
 #include <vector>
 #include <unordered_map>
+#include <thread>
 
 #include <dpl/test/test_runner.h>
 #include <ckm-common.h>
@@ -151,6 +152,7 @@ enum KeyIdx {
 RawBufferPtr PLAIN_DATA;
 RawBufferPtr BIG_DATA;
 ckmc_raw_buffer_s* DEFAULT_IV;
+ckmc_raw_buffer_s* IV1;
 ckmc_raw_buffer_s* IV11;
 ckmc_raw_buffer_s* IV12;
 ckmc_raw_buffer_s* IV15;
@@ -203,6 +205,7 @@ public:
         BIG_DATA = create_raw_buffer(createRandomBufferCAPI(5000000));
 #endif
         DEFAULT_IV = createRandomBufferCAPI(DEFAULT_IV_LEN);
+        IV1 = createRandomBufferCAPI(1);
         IV11 = createRandomBufferCAPI(11);
         IV12 = createRandomBufferCAPI(12);
         IV15 = createRandomBufferCAPI(15);
@@ -275,6 +278,7 @@ public:
         ckmc_buffer_free(IV15);
         ckmc_buffer_free(IV12);
         ckmc_buffer_free(IV11);
+        ckmc_buffer_free(IV1);
         ckmc_buffer_free(DEFAULT_IV);
 
         int ret = ckmc_lock_user_key(UID);
@@ -287,21 +291,6 @@ private:
     PolicyBackend m_backend;
 };
 
-
-void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
-{
-    int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
-    RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
-                      "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
-}
-
-void setParam(ParamListPtr& params, ckmc_param_name_e name, int integer)
-{
-    int ret = ckmc_param_list_set_integer(params.get(), name, integer);
-    RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
-                      "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
-}
-
 struct EncryptionResult
 {
     RawBufferPtr encrypted;
@@ -490,7 +479,7 @@ void encryptionWithCustomData(const Algo& algo, ckmc_param_name_e name)
     RawBufferPtr tmpDec = create_raw_buffer(decrypted);
 
     // check
-    assert_buffers_equal(*PLAIN_DATA.get(), *tmpDec.get());
+    assert_buffers_equal(PLAIN_DATA.get(), tmpDec.get());
     tmpDec.reset();
     decrypted = nullptr;
 
@@ -546,7 +535,7 @@ void testGcmIvSize(ckmc_raw_buffer_s* iv,
                            &decryptedTmp);
     decrypted = create_raw_buffer(decryptedTmp);
 
-    assert_buffers_equal(*PLAIN_DATA.get(), *decrypted.get());
+    assert_buffers_equal(PLAIN_DATA.get(), decrypted.get());
 }
 
 void testIntegrity(const Algo& algo)
@@ -569,7 +558,7 @@ void testIntegrity(const Algo& algo)
                            &decrypted);
 
     RawBufferPtr tmp = create_raw_buffer(decrypted);
-    assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
+    assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
 }
 
 void testCtrEncryptionInvalidLength(const Algo& algo)
@@ -598,9 +587,7 @@ void testCtrEncryptionInvalidLength(const Algo& algo)
         encryptedTmp = nullptr;
     };
     // invalid counter size
-    setParam(params, CKMC_PARAM_ED_CTR_LEN, -1);
-    test();
-    setParam(params, CKMC_PARAM_ED_CTR_LEN, 0);
+    setParam(params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
     test();
     setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
     test();
@@ -662,9 +649,7 @@ void testCtrDecryptionInvalidLength(const Algo& algo)
         decrypted = nullptr;
     };
     // invalid counter size
-    setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, -1);
-    test();
-    setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0);
+    setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
     test();
     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
     test();
@@ -688,7 +673,7 @@ void testCtrDecryptionValidLength(const Algo& algo)
                                &decrypted);
         ckmc_buffer_free(decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted);
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted);
     };
     // invalid counter size
     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
@@ -833,7 +818,8 @@ void testGcmDifferentIvSizes(const Algo& algo)
     // add AES GCM key
     KeyAliasPair aliases = getKey(algo, PRIMARY);
 
-    testGcmIvSize(IV11,  aliases, EncryptionError::SERVER_ERROR); // 12B is the smallest
+    testGcmIvSize(IV1,   aliases);
+    testGcmIvSize(IV11,  aliases);
     testGcmIvSize(IV12,  aliases);
     testGcmIvSize(IV17,  aliases);
     testGcmIvSize(IV128, aliases);
@@ -855,7 +841,7 @@ void testEncryptDecryptBigData(const Algo& algo)
                     &decrypted);
     RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-    assert_buffers_equal(*BIG_DATA.get(), *decrypted);
+    assert_buffers_equal(BIG_DATA.get(), decrypted);
 }
 
 void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
@@ -879,7 +865,7 @@ void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
                                &decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
     } else {
         assert_crypto_result(EncryptionError::INVALID_PARAM,
                              apiDecrypt,
@@ -908,7 +894,7 @@ void testRsaLongestData(const Algo& algo, size_t dataSize)
                            &decrypted);
     RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-    assert_buffers_equal(*plain.get(), *decrypted);
+    assert_buffers_equal(plain.get(), decrypted);
 }
 
 void testRsaDataTooLong(const Algo& algo, size_t dataSize)
@@ -1164,7 +1150,7 @@ RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, SyncEnv, AsyncEnv)
                                &decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted);
     });
 }
 
@@ -1195,7 +1181,7 @@ RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
                                &decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
 
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted);
     });
 }
 
@@ -1464,3 +1450,16 @@ RUNNER_TEST(TED_2010_dec_no_observer_async, AsyncEnv)
                          encrypted);
     });
 }
+
+/////////////////////////////////////////
+// Mulithreaded test for synchronous API
+/////////////////////////////////////////
+RUNNER_TEST(TED_3000_muliple_threads, SyncEnv)
+{
+    std::vector<std::thread> threads;
+    threads.reserve(10);
+    for(unsigned i = 0; i < 10;++i)
+        threads.emplace_back([]{ testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 256}); });
+    for (auto& thread : threads)
+        thread.join();
+}