2 * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file encryption-decryption.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
25 #include <unordered_map>
27 #include <dpl/test/test_runner.h>
28 #include <ckm-common.h>
29 #include <ckmc/ckmc-manager.h>
30 #include <ckmc/ckmc-control.h>
31 #include <ckm/ckm-type.h>
32 #include <ckm/ckm-manager.h>
33 #include <encryption-decryption-env.h>
39 const char* PASSWORD = "test-password";
40 const uid_t UID = 5001;
41 const size_t CTR_DEFAULT_LEN = 16*8;
42 const size_t DEFAULT_IV_LEN = 16;
43 const size_t BUF_LEN = 86; // must be less than 1024/8-41 to support RSA OAEP 1024
49 EncryptionApi* g_api = &g_syncApi;
51 EncryptionError apiEncrypt(ckmc_param_list_h params,
52 const char *key_alias,
54 const ckmc_raw_buffer_s decrypted,
55 ckmc_raw_buffer_s **ppencrypted) {
56 RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
57 return g_api->encrypt(params, key_alias, password, decrypted, ppencrypted);
60 inline CKM::Password _tostring(const char *str)
62 return (str == nullptr) ? Password() : Password(str);
65 inline CKM::Policy _toCkmPolicy(const ckmc_policy_s &policy, PolicyBackend backend)
67 return CKM::Policy(_tostring(policy.password), policy.extractable, backend);
70 EncryptionError apiDecrypt(ckmc_param_list_h params,
71 const char *key_alias,
73 const ckmc_raw_buffer_s encrypted,
74 ckmc_raw_buffer_s **ppdecrypted) {
75 RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
76 return g_api->decrypt(params, key_alias, password, encrypted, ppdecrypted);
80 template <typename F, typename... Args>
81 void assert_crypto_result(EncryptionError expected, F&& func, Args... args)
83 EncryptionError ret = func(args...);
84 RUNNER_ASSERT_MSG(ret == expected,
85 "Expected: " << static_cast<int>(expected) <<
86 " got: " << static_cast<int>(ret));
89 template <typename F, typename... Args>
90 void assert_crypto_positive(F&& func, Args... args)
92 assert_crypto_result(EncryptionError::SUCCESS, std::move(func), args...);
95 template <typename F, typename... Args>
96 void assert_crypto_invalid_param(F&& func, Args... args)
98 assert_crypto_result(EncryptionError::INVALID_PARAM, std::move(func), args...);
103 EncryptionError expected;
113 void init(const std::string&) {
121 static std::string suffix() { return "_sync"; }
125 void init(const std::string&) {
133 static std::string suffix() { return "_async"; }
137 ckmc_algo_type_e type;
141 std::unordered_map<size_t, std::vector<Alias>> g_symKeys;
142 std::unordered_map<size_t, std::vector<KeyAliasPair>> g_asymKeys;
146 PASSWORD_PROTECTED = 1,
151 RawBufferPtr PLAIN_DATA;
152 RawBufferPtr BIG_DATA;
153 ckmc_raw_buffer_s* DEFAULT_IV;
154 ckmc_raw_buffer_s* IV11;
155 ckmc_raw_buffer_s* IV12;
156 ckmc_raw_buffer_s* IV15;
157 ckmc_raw_buffer_s* IV17;
158 ckmc_raw_buffer_s* IV128;
159 ckmc_raw_buffer_s* AAD32;
160 ckmc_raw_buffer_s* AAD64;
162 KeyAliasPair getKey(const Algo& algo, KeyIdx idx)
164 if (algo.type == CKMC_ALGO_RSA_OAEP)
165 return g_asymKeys[algo.keyLen][idx];
168 pair.prv = g_symKeys[algo.keyLen][idx];
173 class EncGroupFixture: public DPL::Test::TestGroup
178 remove_user_data(UID);
179 int ret = ckmc_unlock_user_key(UID, "db-pass");
180 if (ret != CKMC_ERROR_NONE)
181 RUNNER_ERROR_MSG("DB unlock failed: " << CKMCErrorToString(ret));
183 // Policy backend to use in subsequent operations (global for each test case)
185 m_backend = PolicyBackend::FORCE_HARDWARE;
187 m_backend = PolicyBackend::FORCE_SOFTWARE;
191 m_manager = Manager::create();
192 generateSymmetricKeys(128);
193 generateSymmetricKeys(192);
194 generateSymmetricKeys(256);
195 generateRsaKeys(1024);
196 generateRsaKeys(2048);
197 generateRsaKeys(4096);
199 PLAIN_DATA = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
201 BIG_DATA = create_raw_buffer(createRandomBufferCAPI(1000));
203 BIG_DATA = create_raw_buffer(createRandomBufferCAPI(5000000));
205 DEFAULT_IV = createRandomBufferCAPI(DEFAULT_IV_LEN);
206 IV11 = createRandomBufferCAPI(11);
207 IV12 = createRandomBufferCAPI(12);
208 IV15 = createRandomBufferCAPI(15);
209 IV17 = createRandomBufferCAPI(17);
210 IV128 = createRandomBufferCAPI(128);
211 AAD32 = createRandomBufferCAPI(32);
212 AAD64 = createRandomBufferCAPI(64);
215 void generateSymmetricKeys(size_t bitLen)
217 for (int i = 0; i < KEY_IDX_MAX; i++)
219 Policy p(Password(), false, m_backend);
220 if (i == PASSWORD_PROTECTED)
221 p.password.assign(PASSWORD);
223 std::string alias = std::string("skey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
224 int ret = m_manager->createKeyAES(bitLen, alias, p);
225 if (ret != CKM_API_SUCCESS)
226 RUNNER_ERROR_MSG("AES key creation failed");
228 g_symKeys[bitLen].push_back(alias);
232 void generateRsaKeys(size_t bitLen)
234 for (int i = 0; i < KEY_IDX_MAX; i++)
236 Policy prvPolicy(Password(), false, m_backend);
237 Policy pubPolicy(Password(), true, m_backend);
238 if (i == PASSWORD_PROTECTED) {
239 prvPolicy.password.assign(PASSWORD);
240 pubPolicy.password.assign(PASSWORD);
244 alias.prv = std::string("akey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
245 alias.pub = std::string("pub") + alias.prv;
246 int ret = m_manager->createKeyPairRSA(bitLen, alias.prv, alias.pub, prvPolicy, pubPolicy);
247 if (ret != CKM_API_SUCCESS)
248 RUNNER_ERROR_MSG("RSA key creation failed");
250 g_asymKeys[bitLen].push_back(alias);
254 void Finish() override
256 for (const auto &entry : g_asymKeys) {
257 for (const auto &keyPair : entry.second) {
258 m_manager->removeAlias(keyPair.prv);
259 m_manager->removeAlias(keyPair.pub);
263 for (const auto &entry : g_symKeys) {
264 for (const auto &key : entry.second) {
265 m_manager->removeAlias(key);
271 ckmc_buffer_free(AAD64);
272 ckmc_buffer_free(AAD32);
273 ckmc_buffer_free(IV128);
274 ckmc_buffer_free(IV17);
275 ckmc_buffer_free(IV15);
276 ckmc_buffer_free(IV12);
277 ckmc_buffer_free(IV11);
278 ckmc_buffer_free(DEFAULT_IV);
280 int ret = ckmc_lock_user_key(UID);
281 if (ret != CKMC_ERROR_NONE)
282 RUNNER_ERROR_MSG("DB lock failed: " << CKMCErrorToString(ret));
283 remove_user_data(UID);
286 ManagerShPtr m_manager;
287 PolicyBackend m_backend;
291 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
293 int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
294 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
295 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
298 void setParam(ParamListPtr& params, ckmc_param_name_e name, int integer)
300 int ret = ckmc_param_list_set_integer(params.get(), name, integer);
301 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
302 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
305 struct EncryptionResult
307 RawBufferPtr encrypted;
313 EncryptionResult encrypt(const Algo& algo,
314 const RawBufferPtr& plain,
315 const char* pass = nullptr)
317 EncryptionResult ret;
318 ckmc_raw_buffer_s* encrypted = nullptr;
319 KeyAliasPair aliases = getKey(algo, pass == nullptr ? PRIMARY : PASSWORD_PROTECTED);
321 ckmc_param_list_h handle = NULL;
322 assert_positive(ckmc_generate_new_params, algo.type, &handle);
323 ret.params = ParamListPtr(handle, ckmc_param_list_free);
324 setParam(ret.params, CKMC_PARAM_ED_IV, DEFAULT_IV);
326 assert_crypto_positive(apiEncrypt,
333 ret.encrypted = create_raw_buffer(encrypted);
334 ret.prvKey = aliases.prv;
335 ret.pubKey = aliases.pub;
339 void testAllAlgorithms(
340 const std::function<void(const Algo& algo)>& test)
342 test( { CKMC_ALGO_AES_CBC, 128 });
343 test( { CKMC_ALGO_AES_CBC, 192 });
344 test( { CKMC_ALGO_AES_CBC, 256 });
345 test( { CKMC_ALGO_AES_GCM, 128 });
346 test( { CKMC_ALGO_AES_GCM, 192 });
347 test( { CKMC_ALGO_AES_GCM, 256 });
348 test( { CKMC_ALGO_AES_CTR, 128 });
349 test( { CKMC_ALGO_AES_CTR, 192 });
350 test( { CKMC_ALGO_AES_CTR, 256 });
351 test( { CKMC_ALGO_AES_CFB, 128 });
352 test( { CKMC_ALGO_AES_CFB, 192 });
353 test( { CKMC_ALGO_AES_CFB, 256 });
354 test( { CKMC_ALGO_RSA_OAEP, 1024 });
355 test( { CKMC_ALGO_RSA_OAEP, 2048 });
356 test( { CKMC_ALGO_RSA_OAEP, 4096 });
359 void testNoIvEnc(const Algo& algo)
362 ckmc_raw_buffer_s* encrypted = nullptr;
365 KeyAliasPair aliases = getKey(algo, PRIMARY);
367 // param list with algo type only
368 ParamListPtr params = createParamListPtr();
369 setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
370 assert_crypto_invalid_param(apiEncrypt,
378 void testNoIvDec(const Algo& algo)
381 ckmc_raw_buffer_s* decrypted = nullptr;
384 auto ret = encrypt(algo, PLAIN_DATA);
386 // param list with algo type only
387 ParamListPtr params = createParamListPtr();
388 setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
389 assert_crypto_invalid_param(apiDecrypt,
393 *ret.encrypted.get(),
397 void testInvalidIvEnc(const Algo& algo)
400 ckmc_raw_buffer_s* encryptedTmp = nullptr;
403 KeyAliasPair aliases = getKey(algo, PRIMARY);
406 ckmc_param_list_h handle = NULL;
407 assert_positive(ckmc_generate_new_params, algo.type, &handle);
408 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
410 // invalid encryption
412 assert_crypto_invalid_param(apiEncrypt,
418 ckmc_buffer_free(encryptedTmp);
419 encryptedTmp = nullptr;
422 setParam(params, CKMC_PARAM_ED_IV, IV15);
424 setParam(params, CKMC_PARAM_ED_IV, IV17);
428 void testInvalidIvDec(const Algo& algo)
431 ckmc_raw_buffer_s* decrypted = nullptr;
434 auto ret = encrypt(algo, PLAIN_DATA);
438 assert_crypto_invalid_param(apiDecrypt,
442 *ret.encrypted.get(),
444 ckmc_buffer_free(decrypted);
449 setParam(ret.params, CKMC_PARAM_ED_IV, IV15);
451 setParam(ret.params, CKMC_PARAM_ED_IV, IV17);
455 void encryptionWithCustomData(const Algo& algo, ckmc_param_name_e name)
458 ckmc_raw_buffer_s* encrypted = nullptr;
459 ckmc_raw_buffer_s* decrypted = nullptr;
462 KeyAliasPair aliases = getKey(algo, PRIMARY);
465 ckmc_param_list_h handle = NULL;
466 assert_positive(ckmc_generate_new_params, algo.type, &handle);
467 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
469 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
472 setParam(params, name, AAD64);
475 assert_crypto_positive(apiEncrypt,
481 RawBufferPtr tmpEnc = create_raw_buffer(encrypted);
484 assert_crypto_positive(apiDecrypt,
490 RawBufferPtr tmpDec = create_raw_buffer(decrypted);
493 assert_buffers_equal(*PLAIN_DATA.get(), *tmpDec.get());
498 setParam(params, name, AAD32);
501 assert_crypto_result(EncryptionError::INVALID_PARAM,
510 void testGcmIvSize(ckmc_raw_buffer_s* iv,
511 const KeyAliasPair& aliases,
512 EncryptionError error = EncryptionError::SUCCESS)
515 RawBufferPtr encrypted;
516 RawBufferPtr decrypted;
517 ckmc_raw_buffer_s* encryptedTmp = nullptr;
518 ckmc_raw_buffer_s* decryptedTmp = nullptr;
521 ckmc_param_list_h handle = NULL;
522 assert_positive(ckmc_generate_new_params, CKMC_ALGO_AES_GCM, &handle);
523 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
524 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
525 setParam(params, CKMC_PARAM_ED_IV, iv);
528 assert_crypto_result(error,
536 if(error != EncryptionError::SUCCESS)
538 encrypted = create_raw_buffer(encryptedTmp);
541 assert_crypto_positive(apiDecrypt,
547 decrypted = create_raw_buffer(decryptedTmp);
549 assert_buffers_equal(*PLAIN_DATA.get(), *decrypted.get());
552 void testIntegrity(const Algo& algo)
555 ckmc_raw_buffer_s* decrypted = nullptr;
558 auto ret = encrypt(algo, PLAIN_DATA);
560 // break the encrypted data
561 ret.encrypted->data[BUF_LEN/2]++;
563 // no data integrity check
564 assert_crypto_positive(apiDecrypt,
568 *ret.encrypted.get(),
571 RawBufferPtr tmp = create_raw_buffer(decrypted);
572 assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
575 void testCtrEncryptionInvalidLength(const Algo& algo)
578 ckmc_raw_buffer_s* encryptedTmp = nullptr;
581 KeyAliasPair aliases = getKey(algo, PRIMARY);
584 ckmc_param_list_h handle = NULL;
585 assert_positive(ckmc_generate_new_params, algo.type, &handle);
586 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
587 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
591 assert_crypto_invalid_param(apiEncrypt,
597 ckmc_buffer_free(encryptedTmp);
598 encryptedTmp = nullptr;
600 // invalid counter size
601 setParam(params, CKMC_PARAM_ED_CTR_LEN, -1);
603 setParam(params, CKMC_PARAM_ED_CTR_LEN, 0);
605 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
609 void testCtrEncryptionValidLength(const Algo& algo)
612 ckmc_raw_buffer_s* encryptedTmp = nullptr;
615 KeyAliasPair aliases = getKey(algo, PRIMARY);
618 ckmc_param_list_h handle = NULL;
619 assert_positive(ckmc_generate_new_params, algo.type, &handle);
620 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
621 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
625 assert_crypto_positive(apiEncrypt,
631 ckmc_buffer_free(encryptedTmp);
632 encryptedTmp = nullptr;
634 // valid counter sizez
635 setParam(params, CKMC_PARAM_ED_CTR_LEN, 1);
637 setParam(params, CKMC_PARAM_ED_CTR_LEN, 4);
639 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
641 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
645 void testCtrDecryptionInvalidLength(const Algo& algo)
648 ckmc_raw_buffer_s* decrypted = nullptr;
650 // add AES CTR key & encrypt
651 auto ret = encrypt(algo, PLAIN_DATA);
655 assert_crypto_invalid_param(apiDecrypt,
659 *ret.encrypted.get(),
661 ckmc_buffer_free(decrypted);
664 // invalid counter size
665 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, -1);
667 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0);
669 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
673 void testCtrDecryptionValidLength(const Algo& algo)
676 ckmc_raw_buffer_s* decrypted = nullptr;
678 // add AES CTR key & encrypt
679 auto ret = encrypt(algo, PLAIN_DATA);
683 assert_crypto_positive(apiDecrypt,
687 *ret.encrypted.get(),
689 ckmc_buffer_free(decrypted);
690 RawBufferPtr tmp = create_raw_buffer(decrypted);
691 assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
693 // invalid counter size
694 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
696 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 4);
698 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
700 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
704 void testGcmEncryptionTagLen(const Algo& algo)
707 ckmc_raw_buffer_s* encryptedTmp = nullptr;
710 KeyAliasPair aliases = getKey(algo, PRIMARY);
713 ckmc_param_list_h handle = NULL;
714 assert_positive(ckmc_generate_new_params, algo.type, &handle);
715 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
716 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
718 std::vector<TagTest> testData = {
719 // illegal tag lengths
720 { -1, EncryptionError::INVALID_PARAM },
721 { 0, EncryptionError::INVALID_PARAM },
722 { 16, EncryptionError::INVALID_PARAM },
723 { 48, EncryptionError::INVALID_PARAM },
724 { 72, EncryptionError::INVALID_PARAM },
725 { 100, EncryptionError::INVALID_PARAM },
726 { 108, EncryptionError::INVALID_PARAM },
727 { 116, EncryptionError::INVALID_PARAM },
728 { 124, EncryptionError::INVALID_PARAM },
729 { 256, EncryptionError::INVALID_PARAM },
731 { 32, EncryptionError::SUCCESS },
732 { 64, EncryptionError::SUCCESS },
733 { 96, EncryptionError::SUCCESS },
734 { 104, EncryptionError::SUCCESS },
735 { 112, EncryptionError::SUCCESS },
736 { 120, EncryptionError::SUCCESS },
737 { 128, EncryptionError::SUCCESS },
741 for(const auto& it : testData)
743 setParam(params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
744 assert_crypto_result(it.expected,
751 ckmc_buffer_free(encryptedTmp);
752 encryptedTmp = nullptr;
756 void testGcmDecryptionTagLen(const Algo& algo)
759 ckmc_raw_buffer_s* decrypted = nullptr;
761 // add AES GCM key & encrypt
762 auto ret = encrypt(algo, PLAIN_DATA);
764 std::vector<TagTest> testData = {
765 // illegal tag lengths
766 { -1, EncryptionError::INVALID_PARAM },
767 { 0, EncryptionError::INVALID_PARAM },
768 { 16, EncryptionError::INVALID_PARAM },
769 { 48, EncryptionError::INVALID_PARAM },
770 { 72, EncryptionError::INVALID_PARAM },
771 { 100, EncryptionError::INVALID_PARAM },
772 { 108, EncryptionError::INVALID_PARAM },
773 { 116, EncryptionError::INVALID_PARAM },
774 { 124, EncryptionError::INVALID_PARAM },
775 { 256, EncryptionError::INVALID_PARAM },
776 // legal tag lengths but different than the one used for encryption
777 { 32, EncryptionError::INVALID_PARAM },
778 { 64, EncryptionError::INVALID_PARAM },
779 { 96, EncryptionError::INVALID_PARAM },
780 { 104, EncryptionError::INVALID_PARAM },
781 { 112, EncryptionError::INVALID_PARAM },
782 { 120, EncryptionError::INVALID_PARAM },
783 // legal tag length that was actually used for encryption (default)
784 { 128, EncryptionError::SUCCESS },
788 for(const auto& it : testData)
790 setParam(ret.params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
791 assert_crypto_result(it.expected,
796 *ret.encrypted.get(),
798 ckmc_buffer_free(decrypted);
803 void testGcmWrongTag(const Algo& algo)
806 ckmc_raw_buffer_s* decrypted = nullptr;
808 // encrypt with AES GCM
809 auto ret = encrypt(algo, PLAIN_DATA);
811 // modify tag (last 16B of encrypted message)
812 ret.encrypted->data[ret.encrypted->size-1]++;
814 // EVP_CipherFinal fails because of an authentication error
815 assert_crypto_result(EncryptionError::INVALID_PARAM,
820 *ret.encrypted.get(),
824 void testGcmDifferentIvSizes(const Algo& algo)
827 KeyAliasPair aliases = getKey(algo, PRIMARY);
829 testGcmIvSize(IV11, aliases, EncryptionError::SERVER_ERROR); // 12B is the smallest
830 testGcmIvSize(IV12, aliases);
831 testGcmIvSize(IV17, aliases);
832 testGcmIvSize(IV128, aliases);
835 void testEncryptDecryptBigData(const Algo& algo)
838 ckmc_raw_buffer_s* decrypted = nullptr;
841 auto ret = encrypt(algo, BIG_DATA);
843 assert_positive(apiDecrypt,
847 *ret.encrypted.get(),
849 RawBufferPtr tmp = create_raw_buffer(decrypted);
851 assert_buffers_equal(*BIG_DATA.get(), *decrypted);
854 void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
857 ckmc_raw_buffer_s* decrypted = nullptr;
860 auto ret = encrypt(algo, PLAIN_DATA);
862 // get different keys
863 KeyAliasPair differentKeys = getKey(algo, PASSWORD_PROTECTED);
866 // some algorithms don't verify key validity
867 assert_crypto_positive(apiDecrypt,
869 differentKeys.prv.c_str(),
871 *ret.encrypted.get(),
873 RawBufferPtr tmp = create_raw_buffer(decrypted);
875 assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
877 assert_crypto_result(EncryptionError::INVALID_PARAM,
880 differentKeys.prv.c_str(),
882 *ret.encrypted.get(),
887 void testRsaLongestData(const Algo& algo, size_t dataSize)
890 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
891 ckmc_raw_buffer_s* decrypted = nullptr;
894 auto ret = encrypt(algo, plain);
896 assert_crypto_positive(apiDecrypt,
900 *ret.encrypted.get(),
902 RawBufferPtr tmp = create_raw_buffer(decrypted);
904 assert_buffers_equal(*plain.get(), *decrypted);
907 void testRsaDataTooLong(const Algo& algo, size_t dataSize)
910 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
913 EncryptionResult ret;
914 ckmc_raw_buffer_s* encrypted = nullptr;
915 KeyAliasPair aliases = getKey(algo, PRIMARY);
917 ckmc_param_list_h handle = NULL;
918 assert_positive(ckmc_generate_new_params, algo.type, &handle);
919 ret.params = ParamListPtr(handle, ckmc_param_list_free);
920 assert_crypto_result(EncryptionError::INVALID_PARAM,
929 } // namespace anonymous
931 RUNNER_TEST_GROUP_INIT_ENV(CKM_ENCRYPTION_DECRYPTION, EncGroupFixture);
933 /////////////////////////////////////////
934 // Generic encryption decryption tests
935 /////////////////////////////////////////
937 RUNNER_TEST_MULTIPLE(TED_0010_encrypt_invalid_param_list, SyncEnv, AsyncEnv)
939 testAllAlgorithms([](const Algo& algo){
941 ckmc_raw_buffer_s* encrypted = nullptr;
944 KeyAliasPair aliases = getKey(algo, PRIMARY);
947 assert_crypto_invalid_param(apiEncrypt,
955 ParamListPtr params = createParamListPtr();
956 assert_crypto_invalid_param(apiEncrypt,
965 RUNNER_TEST_MULTIPLE(TED_0020_encrypt_missing_key, SyncEnv, AsyncEnv)
967 testAllAlgorithms([](const Algo& algo){
969 ckmc_raw_buffer_s* encrypted = nullptr;
972 ckmc_param_list_h handle = NULL;
973 assert_positive(ckmc_generate_new_params, algo.type, &handle);
974 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
975 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
977 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
980 "non-existing-key-alias",
987 RUNNER_TEST_MULTIPLE(TED_0030_encrypt_no_plain_text, SyncEnv, AsyncEnv)
989 testAllAlgorithms([](const Algo& algo){
991 ckmc_raw_buffer_s plain = { nullptr, 0 };
992 ckmc_raw_buffer_s* encrypted = nullptr;
995 KeyAliasPair aliases = getKey(algo, PRIMARY);
998 ckmc_param_list_h handle = NULL;
999 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1000 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1001 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1003 assert_crypto_invalid_param(apiEncrypt,
1005 aliases.pub.c_str(),
1012 RUNNER_TEST_MULTIPLE(TED_0040_encrypt_no_output_buffer, SyncEnv, AsyncEnv)
1014 testAllAlgorithms([](const Algo& algo){
1016 ckmc_raw_buffer_s** encrypted = nullptr;
1019 KeyAliasPair aliases = getKey(algo, PRIMARY);
1022 ckmc_param_list_h handle = NULL;
1023 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1024 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1025 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1027 assert_crypto_invalid_param(apiEncrypt,
1029 aliases.pub.c_str(),
1036 RUNNER_TEST_MULTIPLE(TED_0110_decrypt_invalid_param_list, SyncEnv, AsyncEnv)
1038 testAllAlgorithms([](const Algo& algo){
1040 ckmc_raw_buffer_s* decrypted = nullptr;
1043 auto ret = encrypt(algo, PLAIN_DATA);
1046 assert_crypto_invalid_param(apiDecrypt,
1050 *ret.encrypted.get(),
1054 ParamListPtr params = createParamListPtr();
1055 assert_crypto_invalid_param(apiDecrypt,
1059 *ret.encrypted.get(),
1064 RUNNER_TEST_MULTIPLE(TED_0120_decrypt_missing_key, SyncEnv, AsyncEnv)
1066 testAllAlgorithms([](const Algo& algo){
1068 ckmc_raw_buffer_s* decrypted = nullptr;
1071 auto ret = encrypt(algo, PLAIN_DATA);
1074 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
1079 *ret.encrypted.get(),
1084 RUNNER_TEST_MULTIPLE(TED_0130_decrypt_no_encrypted_text, SyncEnv, AsyncEnv)
1086 testAllAlgorithms([](const Algo& algo){
1088 ckmc_raw_buffer_s encrypted = { nullptr, 0 };
1089 ckmc_raw_buffer_s* decrypted = nullptr;
1092 KeyAliasPair aliases = getKey(algo, PRIMARY);
1095 ckmc_param_list_h handle = NULL;
1096 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1097 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1098 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1100 assert_crypto_invalid_param(apiDecrypt,
1102 aliases.prv.c_str(),
1109 RUNNER_TEST_MULTIPLE(TED_0140_decrypt_no_output_buffer, SyncEnv, AsyncEnv)
1111 testAllAlgorithms([](const Algo& algo){
1113 ckmc_raw_buffer_s** decrypted = nullptr;
1116 auto ret = encrypt(algo, PLAIN_DATA);
1118 assert_crypto_invalid_param(apiDecrypt,
1122 *ret.encrypted.get(),
1127 RUNNER_TEST_MULTIPLE(TED_0200_encrypt_decrypt_different_keys, SyncEnv, AsyncEnv)
1129 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 128}, false);
1130 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 192}, false);
1131 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 256}, false);
1132 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 128}, true);
1133 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 192}, true);
1134 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 256}, true);
1135 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 128}, true);
1136 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 192}, true);
1137 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 256}, true);
1138 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 1024}, false);
1139 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 2048}, false);
1140 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 4096}, false);
1143 RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, SyncEnv, AsyncEnv)
1145 testAllAlgorithms([](const Algo& algo){
1147 ckmc_raw_buffer_s* decrypted = nullptr;
1150 auto ret = encrypt(algo, PLAIN_DATA);
1152 assert_crypto_positive(apiDecrypt,
1156 *ret.encrypted.get(),
1158 RawBufferPtr tmp = create_raw_buffer(decrypted);
1160 assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
1164 RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
1166 testAllAlgorithms([](const Algo& algo){
1168 ckmc_raw_buffer_s* decrypted = nullptr;
1171 auto ret = encrypt(algo, PLAIN_DATA, PASSWORD);
1174 assert_crypto_result(EncryptionError::AUTH_FAILED,
1179 *ret.encrypted.get(),
1183 assert_crypto_positive(apiDecrypt,
1187 *ret.encrypted.get(),
1189 RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
1191 assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
1195 // long test split into smaller ones
1196 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_128, SyncEnv, AsyncEnv)
1198 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 128});
1201 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_192, SyncEnv, AsyncEnv)
1203 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 192});
1206 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_256, SyncEnv, AsyncEnv)
1208 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 256});
1211 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_128, SyncEnv, AsyncEnv)
1213 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 128});
1216 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_192, SyncEnv, AsyncEnv)
1218 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 192});
1221 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_256, SyncEnv, AsyncEnv)
1223 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 256});
1226 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_128, SyncEnv, AsyncEnv)
1228 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 128});
1231 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_192, SyncEnv, AsyncEnv)
1233 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 192});
1236 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_256, SyncEnv, AsyncEnv)
1238 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 256});
1241 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_128, SyncEnv, AsyncEnv)
1243 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 128});
1246 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_192, SyncEnv, AsyncEnv)
1248 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 192});
1251 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_256, SyncEnv, AsyncEnv)
1253 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 256});
1256 /////////////////////////////////////////
1257 // Algorithm specific tests
1258 /////////////////////////////////////////
1260 RUNNER_TEST_MULTIPLE(TED_1005_no_iv_enc, SyncEnv, AsyncEnv)
1262 testNoIvEnc({CKMC_ALGO_AES_CTR, 128});
1263 testNoIvEnc({CKMC_ALGO_AES_CTR, 192});
1264 testNoIvEnc({CKMC_ALGO_AES_CTR, 256});
1265 testNoIvEnc({CKMC_ALGO_AES_CBC, 128});
1266 testNoIvEnc({CKMC_ALGO_AES_CBC, 192});
1267 testNoIvEnc({CKMC_ALGO_AES_CBC, 256});
1268 testNoIvEnc({CKMC_ALGO_AES_CFB, 128});
1269 testNoIvEnc({CKMC_ALGO_AES_CFB, 192});
1270 testNoIvEnc({CKMC_ALGO_AES_CFB, 256});
1271 testNoIvEnc({CKMC_ALGO_AES_GCM, 128});
1272 testNoIvEnc({CKMC_ALGO_AES_GCM, 192});
1273 testNoIvEnc({CKMC_ALGO_AES_GCM, 256});
1276 RUNNER_TEST_MULTIPLE(TED_1010_invalid_iv_enc, SyncEnv, AsyncEnv)
1278 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 128});
1279 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 192});
1280 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 256});
1281 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 128});
1282 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 192});
1283 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 256});
1284 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 128});
1285 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 192});
1286 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 256});
1289 RUNNER_TEST_MULTIPLE(TED_1015_no_iv_dec, SyncEnv, AsyncEnv)
1291 testNoIvDec({CKMC_ALGO_AES_CTR, 128});
1292 testNoIvDec({CKMC_ALGO_AES_CTR, 192});
1293 testNoIvDec({CKMC_ALGO_AES_CTR, 256});
1294 testNoIvDec({CKMC_ALGO_AES_CBC, 128});
1295 testNoIvDec({CKMC_ALGO_AES_CBC, 192});
1296 testNoIvDec({CKMC_ALGO_AES_CBC, 256});
1297 testNoIvDec({CKMC_ALGO_AES_CFB, 128});
1298 testNoIvDec({CKMC_ALGO_AES_CFB, 192});
1299 testNoIvDec({CKMC_ALGO_AES_CFB, 256});
1300 testNoIvDec({CKMC_ALGO_AES_GCM, 128});
1301 testNoIvDec({CKMC_ALGO_AES_GCM, 192});
1302 testNoIvDec({CKMC_ALGO_AES_GCM, 256});
1305 RUNNER_TEST_MULTIPLE(TED_1020_invalid_iv_dec, SyncEnv, AsyncEnv)
1307 testInvalidIvDec({CKMC_ALGO_AES_CTR, 128});
1308 testInvalidIvDec({CKMC_ALGO_AES_CTR, 192});
1309 testInvalidIvDec({CKMC_ALGO_AES_CTR, 256});
1310 testInvalidIvDec({CKMC_ALGO_AES_CBC, 128});
1311 testInvalidIvDec({CKMC_ALGO_AES_CBC, 192});
1312 testInvalidIvDec({CKMC_ALGO_AES_CBC, 256});
1313 testInvalidIvDec({CKMC_ALGO_AES_CFB, 128});
1314 testInvalidIvDec({CKMC_ALGO_AES_CFB, 192});
1315 testInvalidIvDec({CKMC_ALGO_AES_CFB, 256});
1318 RUNNER_TEST_MULTIPLE(TED_1050_data_integrity, SyncEnv, AsyncEnv)
1320 testIntegrity({CKMC_ALGO_AES_CTR, 128});
1321 testIntegrity({CKMC_ALGO_AES_CTR, 192});
1322 testIntegrity({CKMC_ALGO_AES_CTR, 256});
1323 testIntegrity({CKMC_ALGO_AES_CBC, 128});
1324 testIntegrity({CKMC_ALGO_AES_CBC, 192});
1325 testIntegrity({CKMC_ALGO_AES_CBC, 256});
1326 testIntegrity({CKMC_ALGO_AES_CFB, 128});
1327 testIntegrity({CKMC_ALGO_AES_CFB, 192});
1328 testIntegrity({CKMC_ALGO_AES_CFB, 256});
1331 RUNNER_TEST_MULTIPLE(TED_1100_ctr_encryption_invalid_length, SyncEnv, AsyncEnv)
1333 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1334 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1335 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1338 RUNNER_TEST_MULTIPLE(TED_1105_ctr_encryption_valid_length, SyncEnv, AsyncEnv)
1340 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1341 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1342 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1343 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1346 RUNNER_TEST_MULTIPLE(TED_1110_ctr_decryption_invalid_length, SyncEnv, AsyncEnv)
1348 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1349 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1350 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1353 RUNNER_TEST_MULTIPLE(TED_1115_ctr_decryption_valid_length, SyncEnv, AsyncEnv)
1355 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1356 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1357 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1358 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1361 RUNNER_TEST_MULTIPLE(TED_1200_gcm_encryption_tag_len, SyncEnv, AsyncEnv)
1363 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1364 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1365 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1368 RUNNER_TEST_MULTIPLE(TED_1210_gcm_decryption_tag_len, SyncEnv, AsyncEnv)
1370 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1371 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1372 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1375 RUNNER_TEST_MULTIPLE(TED_1230_gcm_wrong_tag, SyncEnv, AsyncEnv)
1377 testGcmWrongTag({CKMC_ALGO_AES_GCM, 128});
1378 testGcmWrongTag({CKMC_ALGO_AES_GCM, 192});
1379 testGcmWrongTag({CKMC_ALGO_AES_GCM, 256});
1382 RUNNER_TEST_MULTIPLE(TED_1240_gcm_different_iv_sizes, SyncEnv, AsyncEnv)
1384 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 128});
1385 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 192});
1386 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 256});
1389 RUNNER_TEST_MULTIPLE(TED_1250_gcm_aad, SyncEnv, AsyncEnv)
1391 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 128}, CKMC_PARAM_ED_AAD);
1392 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 192}, CKMC_PARAM_ED_AAD);
1393 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 256}, CKMC_PARAM_ED_AAD);
1396 RUNNER_TEST_MULTIPLE(TED_1300_rsa_label, SyncEnv, AsyncEnv)
1398 RUNNER_IGNORED_MSG("RSA-OAEP labels are not supported in openssl");
1399 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 1024}, CKMC_PARAM_ED_LABEL);
1400 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 2048}, CKMC_PARAM_ED_LABEL);
1401 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 4096}, CKMC_PARAM_ED_LABEL);
1404 RUNNER_TEST_MULTIPLE(TED_1330_rsa_longest_data, SyncEnv, AsyncEnv)
1406 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 1024}, 86);
1407 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 2048}, 214);
1408 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 4096}, 470);
1411 RUNNER_TEST_MULTIPLE(TED_1350_rsa_data_too_long, SyncEnv, AsyncEnv)
1413 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 1024}, 87);
1414 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 2048}, 215);
1415 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 4096}, 471);
1418 /////////////////////////////////////////
1419 // Asynchronous only tests
1420 /////////////////////////////////////////
1421 RUNNER_TEST(TED_2000_enc_no_observer_async, AsyncEnv)
1423 testAllAlgorithms([](const Algo& algo){
1425 RawBuffer plain = createRandomBuffer(BUF_LEN);
1428 KeyAliasPair aliases = getKey(algo, PRIMARY);
1431 ckmc_param_list_h handle = NULL;
1432 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1433 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1434 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1437 test_no_observer(&ManagerAsync::encrypt,
1438 *reinterpret_cast<CryptoAlgorithm*>(params.get()),
1445 RUNNER_TEST(TED_2010_dec_no_observer_async, AsyncEnv)
1447 testAllAlgorithms([](const Algo& algo){
1449 auto ret = encrypt(algo, PLAIN_DATA);
1450 RawBuffer encrypted(ret.encrypted->data, ret.encrypted->data + ret.encrypted->size);
1453 test_no_observer(&ManagerAsync::decrypt,
1454 *reinterpret_cast<CryptoAlgorithm*>(ret.params.get()),