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>
28 #include <dpl/test/test_runner.h>
29 #include <ckm-common.h>
30 #include <ckmc/ckmc-manager.h>
31 #include <ckmc/ckmc-control.h>
32 #include <ckm/ckm-type.h>
33 #include <ckm/ckm-manager.h>
34 #include <encryption-decryption-env.h>
40 const char* PASSWORD = "test-password";
41 const uid_t UID = 5001;
42 const size_t CTR_DEFAULT_LEN = 16*8;
43 const size_t DEFAULT_IV_LEN = 16;
44 const size_t BUF_LEN = 86; // must be less than 1024/8-41 to support RSA OAEP 1024
50 EncryptionApi* g_api = &g_syncApi;
52 EncryptionError apiEncrypt(ckmc_param_list_h params,
53 const char *key_alias,
55 const ckmc_raw_buffer_s decrypted,
56 ckmc_raw_buffer_s **ppencrypted) {
57 RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
58 return g_api->encrypt(params, key_alias, password, decrypted, ppencrypted);
61 inline CKM::Password _tostring(const char *str)
63 return (str == nullptr) ? Password() : Password(str);
66 inline CKM::Policy _toCkmPolicy(const ckmc_policy_s &policy, PolicyBackend backend)
68 return CKM::Policy(_tostring(policy.password), policy.extractable, backend);
71 EncryptionError apiDecrypt(ckmc_param_list_h params,
72 const char *key_alias,
74 const ckmc_raw_buffer_s encrypted,
75 ckmc_raw_buffer_s **ppdecrypted) {
76 RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
77 return g_api->decrypt(params, key_alias, password, encrypted, ppdecrypted);
81 template <typename F, typename... Args>
82 void assert_crypto_result(EncryptionError expected, F&& func, Args... args)
84 EncryptionError ret = func(args...);
85 RUNNER_ASSERT_MSG(ret == expected,
86 "Expected: " << static_cast<int>(expected) <<
87 " got: " << static_cast<int>(ret));
90 template <typename F, typename... Args>
91 void assert_crypto_positive(F&& func, Args... args)
93 assert_crypto_result(EncryptionError::SUCCESS, std::move(func), args...);
96 template <typename F, typename... Args>
97 void assert_crypto_invalid_param(F&& func, Args... args)
99 assert_crypto_result(EncryptionError::INVALID_PARAM, std::move(func), args...);
104 EncryptionError expected;
114 void init(const std::string&) {
122 static std::string suffix() { return "_sync"; }
126 void init(const std::string&) {
134 static std::string suffix() { return "_async"; }
138 ckmc_algo_type_e type;
142 std::unordered_map<size_t, std::vector<Alias>> g_symKeys;
143 std::unordered_map<size_t, std::vector<KeyAliasPair>> g_asymKeys;
147 PASSWORD_PROTECTED = 1,
152 RawBufferPtr PLAIN_DATA;
153 RawBufferPtr BIG_DATA;
154 ckmc_raw_buffer_s* DEFAULT_IV;
155 ckmc_raw_buffer_s* IV1;
156 ckmc_raw_buffer_s* IV11;
157 ckmc_raw_buffer_s* IV12;
158 ckmc_raw_buffer_s* IV15;
159 ckmc_raw_buffer_s* IV17;
160 ckmc_raw_buffer_s* IV128;
161 ckmc_raw_buffer_s* AAD32;
162 ckmc_raw_buffer_s* AAD64;
164 KeyAliasPair getKey(const Algo& algo, KeyIdx idx)
166 if (algo.type == CKMC_ALGO_RSA_OAEP)
167 return g_asymKeys[algo.keyLen][idx];
170 pair.prv = g_symKeys[algo.keyLen][idx];
175 class EncGroupFixture: public DPL::Test::TestGroup
180 remove_user_data(UID);
181 int ret = ckmc_unlock_user_key(UID, "db-pass");
182 if (ret != CKMC_ERROR_NONE)
183 RUNNER_ERROR_MSG("DB unlock failed: " << CKMCErrorToString(ret));
185 // Policy backend to use in subsequent operations (global for each test case)
187 m_backend = PolicyBackend::FORCE_HARDWARE;
189 m_backend = PolicyBackend::FORCE_SOFTWARE;
193 m_manager = Manager::create();
194 generateSymmetricKeys(128);
195 generateSymmetricKeys(192);
196 generateSymmetricKeys(256);
197 generateRsaKeys(1024);
198 generateRsaKeys(2048);
199 generateRsaKeys(4096);
201 PLAIN_DATA = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
203 BIG_DATA = create_raw_buffer(createRandomBufferCAPI(1000));
205 BIG_DATA = create_raw_buffer(createRandomBufferCAPI(5000000));
207 DEFAULT_IV = createRandomBufferCAPI(DEFAULT_IV_LEN);
208 IV1 = createRandomBufferCAPI(1);
209 IV11 = createRandomBufferCAPI(11);
210 IV12 = createRandomBufferCAPI(12);
211 IV15 = createRandomBufferCAPI(15);
212 IV17 = createRandomBufferCAPI(17);
213 IV128 = createRandomBufferCAPI(128);
214 AAD32 = createRandomBufferCAPI(32);
215 AAD64 = createRandomBufferCAPI(64);
218 void generateSymmetricKeys(size_t bitLen)
220 for (int i = 0; i < KEY_IDX_MAX; i++)
222 Policy p(Password(), false, m_backend);
223 if (i == PASSWORD_PROTECTED)
224 p.password.assign(PASSWORD);
226 std::string alias = std::string("skey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
227 int ret = m_manager->createKeyAES(bitLen, alias, p);
228 if (ret != CKM_API_SUCCESS)
229 RUNNER_ERROR_MSG("AES key creation failed");
231 g_symKeys[bitLen].push_back(alias);
235 void generateRsaKeys(size_t bitLen)
237 for (int i = 0; i < KEY_IDX_MAX; i++)
239 Policy prvPolicy(Password(), false, m_backend);
240 Policy pubPolicy(Password(), true, m_backend);
241 if (i == PASSWORD_PROTECTED) {
242 prvPolicy.password.assign(PASSWORD);
243 pubPolicy.password.assign(PASSWORD);
247 alias.prv = std::string("akey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
248 alias.pub = std::string("pub") + alias.prv;
249 int ret = m_manager->createKeyPairRSA(bitLen, alias.prv, alias.pub, prvPolicy, pubPolicy);
250 if (ret != CKM_API_SUCCESS)
251 RUNNER_ERROR_MSG("RSA key creation failed");
253 g_asymKeys[bitLen].push_back(alias);
257 void Finish() override
259 for (const auto &entry : g_asymKeys) {
260 for (const auto &keyPair : entry.second) {
261 m_manager->removeAlias(keyPair.prv);
262 m_manager->removeAlias(keyPair.pub);
266 for (const auto &entry : g_symKeys) {
267 for (const auto &key : entry.second) {
268 m_manager->removeAlias(key);
274 ckmc_buffer_free(AAD64);
275 ckmc_buffer_free(AAD32);
276 ckmc_buffer_free(IV128);
277 ckmc_buffer_free(IV17);
278 ckmc_buffer_free(IV15);
279 ckmc_buffer_free(IV12);
280 ckmc_buffer_free(IV11);
281 ckmc_buffer_free(IV1);
282 ckmc_buffer_free(DEFAULT_IV);
284 int ret = ckmc_lock_user_key(UID);
285 if (ret != CKMC_ERROR_NONE)
286 RUNNER_ERROR_MSG("DB lock failed: " << CKMCErrorToString(ret));
287 remove_user_data(UID);
290 ManagerShPtr m_manager;
291 PolicyBackend m_backend;
294 struct EncryptionResult
296 RawBufferPtr encrypted;
302 EncryptionResult encrypt(const Algo& algo,
303 const RawBufferPtr& plain,
304 const char* pass = nullptr)
306 EncryptionResult ret;
307 ckmc_raw_buffer_s* encrypted = nullptr;
308 KeyAliasPair aliases = getKey(algo, pass == nullptr ? PRIMARY : PASSWORD_PROTECTED);
310 ckmc_param_list_h handle = NULL;
311 assert_positive(ckmc_generate_new_params, algo.type, &handle);
312 ret.params = ParamListPtr(handle, ckmc_param_list_free);
313 setParam(ret.params, CKMC_PARAM_ED_IV, DEFAULT_IV);
315 assert_crypto_positive(apiEncrypt,
322 ret.encrypted = create_raw_buffer(encrypted);
323 ret.prvKey = aliases.prv;
324 ret.pubKey = aliases.pub;
328 void testAllAlgorithms(
329 const std::function<void(const Algo& algo)>& test)
331 test( { CKMC_ALGO_AES_CBC, 128 });
332 test( { CKMC_ALGO_AES_CBC, 192 });
333 test( { CKMC_ALGO_AES_CBC, 256 });
334 test( { CKMC_ALGO_AES_GCM, 128 });
335 test( { CKMC_ALGO_AES_GCM, 192 });
336 test( { CKMC_ALGO_AES_GCM, 256 });
337 test( { CKMC_ALGO_AES_CTR, 128 });
338 test( { CKMC_ALGO_AES_CTR, 192 });
339 test( { CKMC_ALGO_AES_CTR, 256 });
340 test( { CKMC_ALGO_AES_CFB, 128 });
341 test( { CKMC_ALGO_AES_CFB, 192 });
342 test( { CKMC_ALGO_AES_CFB, 256 });
343 test( { CKMC_ALGO_RSA_OAEP, 1024 });
344 test( { CKMC_ALGO_RSA_OAEP, 2048 });
345 test( { CKMC_ALGO_RSA_OAEP, 4096 });
348 void testNoIvEnc(const Algo& algo)
351 ckmc_raw_buffer_s* encrypted = nullptr;
354 KeyAliasPair aliases = getKey(algo, PRIMARY);
356 // param list with algo type only
357 ParamListPtr params = createParamListPtr();
358 setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
359 assert_crypto_invalid_param(apiEncrypt,
367 void testNoIvDec(const Algo& algo)
370 ckmc_raw_buffer_s* decrypted = nullptr;
373 auto ret = encrypt(algo, PLAIN_DATA);
375 // param list with algo type only
376 ParamListPtr params = createParamListPtr();
377 setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
378 assert_crypto_invalid_param(apiDecrypt,
382 *ret.encrypted.get(),
386 void testInvalidIvEnc(const Algo& algo)
389 ckmc_raw_buffer_s* encryptedTmp = nullptr;
392 KeyAliasPair aliases = getKey(algo, PRIMARY);
395 ckmc_param_list_h handle = NULL;
396 assert_positive(ckmc_generate_new_params, algo.type, &handle);
397 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
399 // invalid encryption
401 assert_crypto_invalid_param(apiEncrypt,
407 ckmc_buffer_free(encryptedTmp);
408 encryptedTmp = nullptr;
411 setParam(params, CKMC_PARAM_ED_IV, IV15);
413 setParam(params, CKMC_PARAM_ED_IV, IV17);
417 void testInvalidIvDec(const Algo& algo)
420 ckmc_raw_buffer_s* decrypted = nullptr;
423 auto ret = encrypt(algo, PLAIN_DATA);
427 assert_crypto_invalid_param(apiDecrypt,
431 *ret.encrypted.get(),
433 ckmc_buffer_free(decrypted);
438 setParam(ret.params, CKMC_PARAM_ED_IV, IV15);
440 setParam(ret.params, CKMC_PARAM_ED_IV, IV17);
444 void encryptionWithCustomData(const Algo& algo, ckmc_param_name_e name)
447 ckmc_raw_buffer_s* encrypted = nullptr;
448 ckmc_raw_buffer_s* decrypted = nullptr;
451 KeyAliasPair aliases = getKey(algo, PRIMARY);
454 ckmc_param_list_h handle = NULL;
455 assert_positive(ckmc_generate_new_params, algo.type, &handle);
456 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
458 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
461 setParam(params, name, AAD64);
464 assert_crypto_positive(apiEncrypt,
470 RawBufferPtr tmpEnc = create_raw_buffer(encrypted);
473 assert_crypto_positive(apiDecrypt,
479 RawBufferPtr tmpDec = create_raw_buffer(decrypted);
482 assert_buffers_equal(PLAIN_DATA.get(), tmpDec.get());
487 setParam(params, name, AAD32);
490 assert_crypto_result(EncryptionError::INVALID_PARAM,
499 void testGcmIvSize(ckmc_raw_buffer_s* iv,
500 const KeyAliasPair& aliases,
501 EncryptionError error = EncryptionError::SUCCESS)
504 RawBufferPtr encrypted;
505 RawBufferPtr decrypted;
506 ckmc_raw_buffer_s* encryptedTmp = nullptr;
507 ckmc_raw_buffer_s* decryptedTmp = nullptr;
510 ckmc_param_list_h handle = NULL;
511 assert_positive(ckmc_generate_new_params, CKMC_ALGO_AES_GCM, &handle);
512 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
513 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
514 setParam(params, CKMC_PARAM_ED_IV, iv);
517 assert_crypto_result(error,
525 if(error != EncryptionError::SUCCESS)
527 encrypted = create_raw_buffer(encryptedTmp);
530 assert_crypto_positive(apiDecrypt,
536 decrypted = create_raw_buffer(decryptedTmp);
538 assert_buffers_equal(PLAIN_DATA.get(), decrypted.get());
541 void testIntegrity(const Algo& algo)
544 ckmc_raw_buffer_s* decrypted = nullptr;
547 auto ret = encrypt(algo, PLAIN_DATA);
549 // break the encrypted data
550 ret.encrypted->data[BUF_LEN/2]++;
552 // no data integrity check
553 assert_crypto_positive(apiDecrypt,
557 *ret.encrypted.get(),
560 RawBufferPtr tmp = create_raw_buffer(decrypted);
561 assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
564 void testCtrEncryptionInvalidLength(const Algo& algo)
567 ckmc_raw_buffer_s* encryptedTmp = nullptr;
570 KeyAliasPair aliases = getKey(algo, PRIMARY);
573 ckmc_param_list_h handle = NULL;
574 assert_positive(ckmc_generate_new_params, algo.type, &handle);
575 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
576 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
580 assert_crypto_invalid_param(apiEncrypt,
586 ckmc_buffer_free(encryptedTmp);
587 encryptedTmp = nullptr;
589 // invalid counter size
590 setParam(params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
592 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
596 void testCtrEncryptionValidLength(const Algo& algo)
599 ckmc_raw_buffer_s* encryptedTmp = nullptr;
602 KeyAliasPair aliases = getKey(algo, PRIMARY);
605 ckmc_param_list_h handle = NULL;
606 assert_positive(ckmc_generate_new_params, algo.type, &handle);
607 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
608 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
612 assert_crypto_positive(apiEncrypt,
618 ckmc_buffer_free(encryptedTmp);
619 encryptedTmp = nullptr;
621 // valid counter sizez
622 setParam(params, CKMC_PARAM_ED_CTR_LEN, 1);
624 setParam(params, CKMC_PARAM_ED_CTR_LEN, 4);
626 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
628 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
632 void testCtrDecryptionInvalidLength(const Algo& algo)
635 ckmc_raw_buffer_s* decrypted = nullptr;
637 // add AES CTR key & encrypt
638 auto ret = encrypt(algo, PLAIN_DATA);
642 assert_crypto_invalid_param(apiDecrypt,
646 *ret.encrypted.get(),
648 ckmc_buffer_free(decrypted);
651 // invalid counter size
652 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
654 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
658 void testCtrDecryptionValidLength(const Algo& algo)
661 ckmc_raw_buffer_s* decrypted = nullptr;
663 // add AES CTR key & encrypt
664 auto ret = encrypt(algo, PLAIN_DATA);
668 assert_crypto_positive(apiDecrypt,
672 *ret.encrypted.get(),
674 ckmc_buffer_free(decrypted);
675 RawBufferPtr tmp = create_raw_buffer(decrypted);
676 assert_buffers_equal(PLAIN_DATA.get(), decrypted);
678 // invalid counter size
679 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
681 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 4);
683 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
685 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
689 void testGcmEncryptionTagLen(const Algo& algo)
692 ckmc_raw_buffer_s* encryptedTmp = nullptr;
695 KeyAliasPair aliases = getKey(algo, PRIMARY);
698 ckmc_param_list_h handle = NULL;
699 assert_positive(ckmc_generate_new_params, algo.type, &handle);
700 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
701 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
703 std::vector<TagTest> testData = {
704 // illegal tag lengths
705 { -1, EncryptionError::INVALID_PARAM },
706 { 0, EncryptionError::INVALID_PARAM },
707 { 16, EncryptionError::INVALID_PARAM },
708 { 48, EncryptionError::INVALID_PARAM },
709 { 72, EncryptionError::INVALID_PARAM },
710 { 100, EncryptionError::INVALID_PARAM },
711 { 108, EncryptionError::INVALID_PARAM },
712 { 116, EncryptionError::INVALID_PARAM },
713 { 124, EncryptionError::INVALID_PARAM },
714 { 256, EncryptionError::INVALID_PARAM },
716 { 32, EncryptionError::INVALID_PARAM },
717 { 64, EncryptionError::INVALID_PARAM },
721 { 32, EncryptionError::SUCCESS },
722 { 64, EncryptionError::SUCCESS },
725 { 96, EncryptionError::SUCCESS },
726 { 104, EncryptionError::SUCCESS },
727 { 112, EncryptionError::SUCCESS },
728 { 120, EncryptionError::SUCCESS },
729 { 128, EncryptionError::SUCCESS },
733 for(const auto& it : testData)
735 setParam(params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
736 assert_crypto_result(it.expected,
743 ckmc_buffer_free(encryptedTmp);
744 encryptedTmp = nullptr;
748 void testGcmDecryptionTagLen(const Algo& algo)
751 ckmc_raw_buffer_s* decrypted = nullptr;
753 // add AES GCM key & encrypt
754 auto ret = encrypt(algo, PLAIN_DATA);
756 std::vector<TagTest> testData = {
757 // illegal tag lengths
758 { -1, EncryptionError::INVALID_PARAM },
759 { 0, EncryptionError::INVALID_PARAM },
760 { 16, EncryptionError::INVALID_PARAM },
761 { 48, EncryptionError::INVALID_PARAM },
762 { 72, EncryptionError::INVALID_PARAM },
763 { 100, EncryptionError::INVALID_PARAM },
764 { 108, EncryptionError::INVALID_PARAM },
765 { 116, EncryptionError::INVALID_PARAM },
766 { 124, EncryptionError::INVALID_PARAM },
767 { 256, EncryptionError::INVALID_PARAM },
768 // legal tag lengths but different than the one used for encryption
769 { 32, EncryptionError::INVALID_PARAM },
770 { 64, EncryptionError::INVALID_PARAM },
771 { 96, EncryptionError::INVALID_PARAM },
772 { 104, EncryptionError::INVALID_PARAM },
773 { 112, EncryptionError::INVALID_PARAM },
774 { 120, EncryptionError::INVALID_PARAM },
775 // legal tag length that was actually used for encryption (default)
776 { 128, EncryptionError::SUCCESS },
780 for(const auto& it : testData)
782 setParam(ret.params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
783 assert_crypto_result(it.expected,
788 *ret.encrypted.get(),
790 ckmc_buffer_free(decrypted);
795 void testGcmWrongTag(const Algo& algo)
798 ckmc_raw_buffer_s* decrypted = nullptr;
800 // encrypt with AES GCM
801 auto ret = encrypt(algo, PLAIN_DATA);
803 // modify tag (last 16B of encrypted message)
804 ret.encrypted->data[ret.encrypted->size-1]++;
806 // EVP_CipherFinal fails because of an authentication error
807 assert_crypto_result(EncryptionError::INVALID_PARAM,
812 *ret.encrypted.get(),
816 void testGcmDifferentIvSizes(const Algo& algo)
819 KeyAliasPair aliases = getKey(algo, PRIMARY);
821 testGcmIvSize(IV1, aliases);
822 testGcmIvSize(IV11, aliases);
823 testGcmIvSize(IV12, aliases);
824 testGcmIvSize(IV17, aliases);
825 testGcmIvSize(IV128, aliases);
828 void testEncryptDecryptBigData(const Algo& algo)
831 ckmc_raw_buffer_s* decrypted = nullptr;
834 auto ret = encrypt(algo, BIG_DATA);
836 assert_positive(apiDecrypt,
840 *ret.encrypted.get(),
842 RawBufferPtr tmp = create_raw_buffer(decrypted);
844 assert_buffers_equal(BIG_DATA.get(), decrypted);
847 void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
850 ckmc_raw_buffer_s* decrypted = nullptr;
853 auto ret = encrypt(algo, PLAIN_DATA);
855 // get different keys
856 KeyAliasPair differentKeys = getKey(algo, PASSWORD_PROTECTED);
859 // some algorithms don't verify key validity
860 assert_crypto_positive(apiDecrypt,
862 differentKeys.prv.c_str(),
864 *ret.encrypted.get(),
866 RawBufferPtr tmp = create_raw_buffer(decrypted);
868 assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
870 assert_crypto_result(EncryptionError::INVALID_PARAM,
873 differentKeys.prv.c_str(),
875 *ret.encrypted.get(),
880 void testRsaLongestData(const Algo& algo, size_t dataSize)
883 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
884 ckmc_raw_buffer_s* decrypted = nullptr;
887 auto ret = encrypt(algo, plain);
889 assert_crypto_positive(apiDecrypt,
893 *ret.encrypted.get(),
895 RawBufferPtr tmp = create_raw_buffer(decrypted);
897 assert_buffers_equal(plain.get(), decrypted);
900 void testRsaDataTooLong(const Algo& algo, size_t dataSize)
903 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
906 EncryptionResult ret;
907 ckmc_raw_buffer_s* encrypted = nullptr;
908 KeyAliasPair aliases = getKey(algo, PRIMARY);
910 ckmc_param_list_h handle = NULL;
911 assert_positive(ckmc_generate_new_params, algo.type, &handle);
912 ret.params = ParamListPtr(handle, ckmc_param_list_free);
913 assert_crypto_result(EncryptionError::INVALID_PARAM,
922 } // namespace anonymous
924 RUNNER_TEST_GROUP_INIT_ENV(CKM_ENCRYPTION_DECRYPTION, EncGroupFixture);
926 /////////////////////////////////////////
927 // Generic encryption decryption tests
928 /////////////////////////////////////////
930 RUNNER_TEST_MULTIPLE(TED_0010_encrypt_invalid_param_list, SyncEnv, AsyncEnv)
932 testAllAlgorithms([](const Algo& algo){
934 ckmc_raw_buffer_s* encrypted = nullptr;
937 KeyAliasPair aliases = getKey(algo, PRIMARY);
940 assert_crypto_invalid_param(apiEncrypt,
948 ParamListPtr params = createParamListPtr();
949 assert_crypto_invalid_param(apiEncrypt,
958 RUNNER_TEST_MULTIPLE(TED_0020_encrypt_missing_key, SyncEnv, AsyncEnv)
960 testAllAlgorithms([](const Algo& algo){
962 ckmc_raw_buffer_s* encrypted = nullptr;
965 ckmc_param_list_h handle = NULL;
966 assert_positive(ckmc_generate_new_params, algo.type, &handle);
967 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
968 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
970 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
973 "non-existing-key-alias",
980 RUNNER_TEST_MULTIPLE(TED_0030_encrypt_no_plain_text, SyncEnv, AsyncEnv)
982 testAllAlgorithms([](const Algo& algo){
984 ckmc_raw_buffer_s plain = { nullptr, 0 };
985 ckmc_raw_buffer_s* encrypted = nullptr;
988 KeyAliasPair aliases = getKey(algo, PRIMARY);
991 ckmc_param_list_h handle = NULL;
992 assert_positive(ckmc_generate_new_params, algo.type, &handle);
993 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
994 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
996 assert_crypto_invalid_param(apiEncrypt,
1005 RUNNER_TEST_MULTIPLE(TED_0040_encrypt_no_output_buffer, SyncEnv, AsyncEnv)
1007 testAllAlgorithms([](const Algo& algo){
1009 ckmc_raw_buffer_s** encrypted = nullptr;
1012 KeyAliasPair aliases = getKey(algo, PRIMARY);
1015 ckmc_param_list_h handle = NULL;
1016 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1017 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1018 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1020 assert_crypto_invalid_param(apiEncrypt,
1022 aliases.pub.c_str(),
1029 RUNNER_TEST_MULTIPLE(TED_0110_decrypt_invalid_param_list, SyncEnv, AsyncEnv)
1031 testAllAlgorithms([](const Algo& algo){
1033 ckmc_raw_buffer_s* decrypted = nullptr;
1036 auto ret = encrypt(algo, PLAIN_DATA);
1039 assert_crypto_invalid_param(apiDecrypt,
1043 *ret.encrypted.get(),
1047 ParamListPtr params = createParamListPtr();
1048 assert_crypto_invalid_param(apiDecrypt,
1052 *ret.encrypted.get(),
1057 RUNNER_TEST_MULTIPLE(TED_0120_decrypt_missing_key, SyncEnv, AsyncEnv)
1059 testAllAlgorithms([](const Algo& algo){
1061 ckmc_raw_buffer_s* decrypted = nullptr;
1064 auto ret = encrypt(algo, PLAIN_DATA);
1067 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
1072 *ret.encrypted.get(),
1077 RUNNER_TEST_MULTIPLE(TED_0130_decrypt_no_encrypted_text, SyncEnv, AsyncEnv)
1079 testAllAlgorithms([](const Algo& algo){
1081 ckmc_raw_buffer_s encrypted = { nullptr, 0 };
1082 ckmc_raw_buffer_s* decrypted = nullptr;
1085 KeyAliasPair aliases = getKey(algo, PRIMARY);
1088 ckmc_param_list_h handle = NULL;
1089 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1090 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1091 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1093 assert_crypto_invalid_param(apiDecrypt,
1095 aliases.prv.c_str(),
1102 RUNNER_TEST_MULTIPLE(TED_0140_decrypt_no_output_buffer, SyncEnv, AsyncEnv)
1104 testAllAlgorithms([](const Algo& algo){
1106 ckmc_raw_buffer_s** decrypted = nullptr;
1109 auto ret = encrypt(algo, PLAIN_DATA);
1111 assert_crypto_invalid_param(apiDecrypt,
1115 *ret.encrypted.get(),
1120 RUNNER_TEST_MULTIPLE(TED_0200_encrypt_decrypt_different_keys, SyncEnv, AsyncEnv)
1122 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 128}, false);
1123 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 192}, false);
1124 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 256}, false);
1125 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 128}, true);
1126 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 192}, true);
1127 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 256}, true);
1128 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 128}, true);
1129 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 192}, true);
1130 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 256}, true);
1131 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 1024}, false);
1132 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 2048}, false);
1133 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 4096}, false);
1136 RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, SyncEnv, AsyncEnv)
1138 testAllAlgorithms([](const Algo& algo){
1140 ckmc_raw_buffer_s* decrypted = nullptr;
1143 auto ret = encrypt(algo, PLAIN_DATA);
1145 assert_crypto_positive(apiDecrypt,
1149 *ret.encrypted.get(),
1151 RawBufferPtr tmp = create_raw_buffer(decrypted);
1153 assert_buffers_equal(PLAIN_DATA.get(), decrypted);
1157 RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
1159 testAllAlgorithms([](const Algo& algo){
1161 ckmc_raw_buffer_s* decrypted = nullptr;
1164 auto ret = encrypt(algo, PLAIN_DATA, PASSWORD);
1167 assert_crypto_result(EncryptionError::AUTH_FAILED,
1172 *ret.encrypted.get(),
1176 assert_crypto_positive(apiDecrypt,
1180 *ret.encrypted.get(),
1182 RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
1184 assert_buffers_equal(PLAIN_DATA.get(), decrypted);
1188 // long test split into smaller ones
1189 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_128, SyncEnv, AsyncEnv)
1191 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 128});
1194 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_192, SyncEnv, AsyncEnv)
1196 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 192});
1199 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_256, SyncEnv, AsyncEnv)
1201 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 256});
1204 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_128, SyncEnv, AsyncEnv)
1206 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 128});
1209 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_192, SyncEnv, AsyncEnv)
1211 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 192});
1214 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_256, SyncEnv, AsyncEnv)
1216 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 256});
1219 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_128, SyncEnv, AsyncEnv)
1221 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 128});
1224 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_192, SyncEnv, AsyncEnv)
1226 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 192});
1229 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_256, SyncEnv, AsyncEnv)
1231 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 256});
1234 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_128, SyncEnv, AsyncEnv)
1236 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 128});
1239 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_192, SyncEnv, AsyncEnv)
1241 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 192});
1244 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_256, SyncEnv, AsyncEnv)
1246 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 256});
1249 /////////////////////////////////////////
1250 // Algorithm specific tests
1251 /////////////////////////////////////////
1253 RUNNER_TEST_MULTIPLE(TED_1005_no_iv_enc, SyncEnv, AsyncEnv)
1255 testNoIvEnc({CKMC_ALGO_AES_CTR, 128});
1256 testNoIvEnc({CKMC_ALGO_AES_CTR, 192});
1257 testNoIvEnc({CKMC_ALGO_AES_CTR, 256});
1258 testNoIvEnc({CKMC_ALGO_AES_CBC, 128});
1259 testNoIvEnc({CKMC_ALGO_AES_CBC, 192});
1260 testNoIvEnc({CKMC_ALGO_AES_CBC, 256});
1261 testNoIvEnc({CKMC_ALGO_AES_CFB, 128});
1262 testNoIvEnc({CKMC_ALGO_AES_CFB, 192});
1263 testNoIvEnc({CKMC_ALGO_AES_CFB, 256});
1264 testNoIvEnc({CKMC_ALGO_AES_GCM, 128});
1265 testNoIvEnc({CKMC_ALGO_AES_GCM, 192});
1266 testNoIvEnc({CKMC_ALGO_AES_GCM, 256});
1269 RUNNER_TEST_MULTIPLE(TED_1010_invalid_iv_enc, SyncEnv, AsyncEnv)
1271 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 128});
1272 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 192});
1273 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 256});
1274 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 128});
1275 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 192});
1276 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 256});
1277 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 128});
1278 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 192});
1279 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 256});
1282 RUNNER_TEST_MULTIPLE(TED_1015_no_iv_dec, SyncEnv, AsyncEnv)
1284 testNoIvDec({CKMC_ALGO_AES_CTR, 128});
1285 testNoIvDec({CKMC_ALGO_AES_CTR, 192});
1286 testNoIvDec({CKMC_ALGO_AES_CTR, 256});
1287 testNoIvDec({CKMC_ALGO_AES_CBC, 128});
1288 testNoIvDec({CKMC_ALGO_AES_CBC, 192});
1289 testNoIvDec({CKMC_ALGO_AES_CBC, 256});
1290 testNoIvDec({CKMC_ALGO_AES_CFB, 128});
1291 testNoIvDec({CKMC_ALGO_AES_CFB, 192});
1292 testNoIvDec({CKMC_ALGO_AES_CFB, 256});
1293 testNoIvDec({CKMC_ALGO_AES_GCM, 128});
1294 testNoIvDec({CKMC_ALGO_AES_GCM, 192});
1295 testNoIvDec({CKMC_ALGO_AES_GCM, 256});
1298 RUNNER_TEST_MULTIPLE(TED_1020_invalid_iv_dec, SyncEnv, AsyncEnv)
1300 testInvalidIvDec({CKMC_ALGO_AES_CTR, 128});
1301 testInvalidIvDec({CKMC_ALGO_AES_CTR, 192});
1302 testInvalidIvDec({CKMC_ALGO_AES_CTR, 256});
1303 testInvalidIvDec({CKMC_ALGO_AES_CBC, 128});
1304 testInvalidIvDec({CKMC_ALGO_AES_CBC, 192});
1305 testInvalidIvDec({CKMC_ALGO_AES_CBC, 256});
1306 testInvalidIvDec({CKMC_ALGO_AES_CFB, 128});
1307 testInvalidIvDec({CKMC_ALGO_AES_CFB, 192});
1308 testInvalidIvDec({CKMC_ALGO_AES_CFB, 256});
1311 RUNNER_TEST_MULTIPLE(TED_1050_data_integrity, SyncEnv, AsyncEnv)
1313 testIntegrity({CKMC_ALGO_AES_CTR, 128});
1314 testIntegrity({CKMC_ALGO_AES_CTR, 192});
1315 testIntegrity({CKMC_ALGO_AES_CTR, 256});
1316 testIntegrity({CKMC_ALGO_AES_CBC, 128});
1317 testIntegrity({CKMC_ALGO_AES_CBC, 192});
1318 testIntegrity({CKMC_ALGO_AES_CBC, 256});
1319 testIntegrity({CKMC_ALGO_AES_CFB, 128});
1320 testIntegrity({CKMC_ALGO_AES_CFB, 192});
1321 testIntegrity({CKMC_ALGO_AES_CFB, 256});
1324 RUNNER_TEST_MULTIPLE(TED_1100_ctr_encryption_invalid_length, SyncEnv, AsyncEnv)
1326 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1327 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1328 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1331 RUNNER_TEST_MULTIPLE(TED_1105_ctr_encryption_valid_length, SyncEnv, AsyncEnv)
1333 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1334 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1335 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1336 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1339 RUNNER_TEST_MULTIPLE(TED_1110_ctr_decryption_invalid_length, SyncEnv, AsyncEnv)
1341 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1342 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1343 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1346 RUNNER_TEST_MULTIPLE(TED_1115_ctr_decryption_valid_length, SyncEnv, AsyncEnv)
1348 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1349 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1350 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1351 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1354 RUNNER_TEST_MULTIPLE(TED_1200_gcm_encryption_tag_len, SyncEnv, AsyncEnv)
1356 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1357 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1358 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1361 RUNNER_TEST_MULTIPLE(TED_1210_gcm_decryption_tag_len, SyncEnv, AsyncEnv)
1363 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1364 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1365 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1368 RUNNER_TEST_MULTIPLE(TED_1230_gcm_wrong_tag, SyncEnv, AsyncEnv)
1370 testGcmWrongTag({CKMC_ALGO_AES_GCM, 128});
1371 testGcmWrongTag({CKMC_ALGO_AES_GCM, 192});
1372 testGcmWrongTag({CKMC_ALGO_AES_GCM, 256});
1375 RUNNER_TEST_MULTIPLE(TED_1240_gcm_different_iv_sizes, SyncEnv, AsyncEnv)
1377 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 128});
1378 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 192});
1379 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 256});
1382 RUNNER_TEST_MULTIPLE(TED_1250_gcm_aad, SyncEnv, AsyncEnv)
1384 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 128}, CKMC_PARAM_ED_AAD);
1385 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 192}, CKMC_PARAM_ED_AAD);
1386 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 256}, CKMC_PARAM_ED_AAD);
1389 RUNNER_TEST_MULTIPLE(TED_1300_rsa_label, SyncEnv, AsyncEnv)
1391 RUNNER_IGNORED_MSG("RSA-OAEP labels are not supported in openssl");
1392 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 1024}, CKMC_PARAM_ED_LABEL);
1393 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 2048}, CKMC_PARAM_ED_LABEL);
1394 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 4096}, CKMC_PARAM_ED_LABEL);
1397 RUNNER_TEST_MULTIPLE(TED_1330_rsa_longest_data, SyncEnv, AsyncEnv)
1399 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 1024}, 86);
1400 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 2048}, 214);
1401 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 4096}, 470);
1404 RUNNER_TEST_MULTIPLE(TED_1350_rsa_data_too_long, SyncEnv, AsyncEnv)
1406 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 1024}, 87);
1407 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 2048}, 215);
1408 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 4096}, 471);
1411 /////////////////////////////////////////
1412 // Asynchronous only tests
1413 /////////////////////////////////////////
1414 RUNNER_TEST(TED_2000_enc_no_observer_async, AsyncEnv)
1416 testAllAlgorithms([](const Algo& algo){
1418 RawBuffer plain = createRandomBuffer(BUF_LEN);
1421 KeyAliasPair aliases = getKey(algo, PRIMARY);
1424 ckmc_param_list_h handle = NULL;
1425 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1426 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1427 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1430 test_no_observer(&ManagerAsync::encrypt,
1431 *reinterpret_cast<CryptoAlgorithm*>(params.get()),
1438 RUNNER_TEST(TED_2010_dec_no_observer_async, AsyncEnv)
1440 testAllAlgorithms([](const Algo& algo){
1442 auto ret = encrypt(algo, PLAIN_DATA);
1443 RawBuffer encrypted(ret.encrypted->data, ret.encrypted->data + ret.encrypted->size);
1446 test_no_observer(&ManagerAsync::decrypt,
1447 *reinterpret_cast<CryptoAlgorithm*>(ret.params.get()),
1454 /////////////////////////////////////////
1455 // Mulithreaded test for synchronous API
1456 /////////////////////////////////////////
1457 RUNNER_TEST(TED_3000_muliple_threads, SyncEnv)
1459 std::vector<std::thread> threads;
1460 threads.reserve(10);
1461 for(unsigned i = 0; i < 10;++i)
1462 threads.emplace_back([]{ testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 256}); });
1463 for (auto& thread : threads)