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* IV11;
156 ckmc_raw_buffer_s* IV12;
157 ckmc_raw_buffer_s* IV15;
158 ckmc_raw_buffer_s* IV17;
159 ckmc_raw_buffer_s* IV128;
160 ckmc_raw_buffer_s* AAD32;
161 ckmc_raw_buffer_s* AAD64;
163 KeyAliasPair getKey(const Algo& algo, KeyIdx idx)
165 if (algo.type == CKMC_ALGO_RSA_OAEP)
166 return g_asymKeys[algo.keyLen][idx];
169 pair.prv = g_symKeys[algo.keyLen][idx];
174 class EncGroupFixture: public DPL::Test::TestGroup
179 remove_user_data(UID);
180 int ret = ckmc_unlock_user_key(UID, "db-pass");
181 if (ret != CKMC_ERROR_NONE)
182 RUNNER_ERROR_MSG("DB unlock failed: " << CKMCErrorToString(ret));
184 // Policy backend to use in subsequent operations (global for each test case)
186 m_backend = PolicyBackend::FORCE_HARDWARE;
188 m_backend = PolicyBackend::FORCE_SOFTWARE;
192 m_manager = Manager::create();
193 generateSymmetricKeys(128);
194 generateSymmetricKeys(192);
195 generateSymmetricKeys(256);
196 generateRsaKeys(1024);
197 generateRsaKeys(2048);
198 generateRsaKeys(4096);
200 PLAIN_DATA = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
202 BIG_DATA = create_raw_buffer(createRandomBufferCAPI(1000));
204 BIG_DATA = create_raw_buffer(createRandomBufferCAPI(5000000));
206 DEFAULT_IV = createRandomBufferCAPI(DEFAULT_IV_LEN);
207 IV11 = createRandomBufferCAPI(11);
208 IV12 = createRandomBufferCAPI(12);
209 IV15 = createRandomBufferCAPI(15);
210 IV17 = createRandomBufferCAPI(17);
211 IV128 = createRandomBufferCAPI(128);
212 AAD32 = createRandomBufferCAPI(32);
213 AAD64 = createRandomBufferCAPI(64);
216 void generateSymmetricKeys(size_t bitLen)
218 for (int i = 0; i < KEY_IDX_MAX; i++)
220 Policy p(Password(), false, m_backend);
221 if (i == PASSWORD_PROTECTED)
222 p.password.assign(PASSWORD);
224 std::string alias = std::string("skey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
225 int ret = m_manager->createKeyAES(bitLen, alias, p);
226 if (ret != CKM_API_SUCCESS)
227 RUNNER_ERROR_MSG("AES key creation failed");
229 g_symKeys[bitLen].push_back(alias);
233 void generateRsaKeys(size_t bitLen)
235 for (int i = 0; i < KEY_IDX_MAX; i++)
237 Policy prvPolicy(Password(), false, m_backend);
238 Policy pubPolicy(Password(), true, m_backend);
239 if (i == PASSWORD_PROTECTED) {
240 prvPolicy.password.assign(PASSWORD);
241 pubPolicy.password.assign(PASSWORD);
245 alias.prv = std::string("akey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
246 alias.pub = std::string("pub") + alias.prv;
247 int ret = m_manager->createKeyPairRSA(bitLen, alias.prv, alias.pub, prvPolicy, pubPolicy);
248 if (ret != CKM_API_SUCCESS)
249 RUNNER_ERROR_MSG("RSA key creation failed");
251 g_asymKeys[bitLen].push_back(alias);
255 void Finish() override
257 for (const auto &entry : g_asymKeys) {
258 for (const auto &keyPair : entry.second) {
259 m_manager->removeAlias(keyPair.prv);
260 m_manager->removeAlias(keyPair.pub);
264 for (const auto &entry : g_symKeys) {
265 for (const auto &key : entry.second) {
266 m_manager->removeAlias(key);
272 ckmc_buffer_free(AAD64);
273 ckmc_buffer_free(AAD32);
274 ckmc_buffer_free(IV128);
275 ckmc_buffer_free(IV17);
276 ckmc_buffer_free(IV15);
277 ckmc_buffer_free(IV12);
278 ckmc_buffer_free(IV11);
279 ckmc_buffer_free(DEFAULT_IV);
281 int ret = ckmc_lock_user_key(UID);
282 if (ret != CKMC_ERROR_NONE)
283 RUNNER_ERROR_MSG("DB lock failed: " << CKMCErrorToString(ret));
284 remove_user_data(UID);
287 ManagerShPtr m_manager;
288 PolicyBackend m_backend;
291 struct EncryptionResult
293 RawBufferPtr encrypted;
299 EncryptionResult encrypt(const Algo& algo,
300 const RawBufferPtr& plain,
301 const char* pass = nullptr)
303 EncryptionResult ret;
304 ckmc_raw_buffer_s* encrypted = nullptr;
305 KeyAliasPair aliases = getKey(algo, pass == nullptr ? PRIMARY : PASSWORD_PROTECTED);
307 ckmc_param_list_h handle = NULL;
308 assert_positive(ckmc_generate_new_params, algo.type, &handle);
309 ret.params = ParamListPtr(handle, ckmc_param_list_free);
310 setParam(ret.params, CKMC_PARAM_ED_IV, DEFAULT_IV);
312 assert_crypto_positive(apiEncrypt,
319 ret.encrypted = create_raw_buffer(encrypted);
320 ret.prvKey = aliases.prv;
321 ret.pubKey = aliases.pub;
325 void testAllAlgorithms(
326 const std::function<void(const Algo& algo)>& test)
328 test( { CKMC_ALGO_AES_CBC, 128 });
329 test( { CKMC_ALGO_AES_CBC, 192 });
330 test( { CKMC_ALGO_AES_CBC, 256 });
331 test( { CKMC_ALGO_AES_GCM, 128 });
332 test( { CKMC_ALGO_AES_GCM, 192 });
333 test( { CKMC_ALGO_AES_GCM, 256 });
334 test( { CKMC_ALGO_AES_CTR, 128 });
335 test( { CKMC_ALGO_AES_CTR, 192 });
336 test( { CKMC_ALGO_AES_CTR, 256 });
337 test( { CKMC_ALGO_AES_CFB, 128 });
338 test( { CKMC_ALGO_AES_CFB, 192 });
339 test( { CKMC_ALGO_AES_CFB, 256 });
340 test( { CKMC_ALGO_RSA_OAEP, 1024 });
341 test( { CKMC_ALGO_RSA_OAEP, 2048 });
342 test( { CKMC_ALGO_RSA_OAEP, 4096 });
345 void testNoIvEnc(const Algo& algo)
348 ckmc_raw_buffer_s* encrypted = nullptr;
351 KeyAliasPair aliases = getKey(algo, PRIMARY);
353 // param list with algo type only
354 ParamListPtr params = createParamListPtr();
355 setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
356 assert_crypto_invalid_param(apiEncrypt,
364 void testNoIvDec(const Algo& algo)
367 ckmc_raw_buffer_s* decrypted = nullptr;
370 auto ret = encrypt(algo, PLAIN_DATA);
372 // param list with algo type only
373 ParamListPtr params = createParamListPtr();
374 setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
375 assert_crypto_invalid_param(apiDecrypt,
379 *ret.encrypted.get(),
383 void testInvalidIvEnc(const Algo& algo)
386 ckmc_raw_buffer_s* encryptedTmp = nullptr;
389 KeyAliasPair aliases = getKey(algo, PRIMARY);
392 ckmc_param_list_h handle = NULL;
393 assert_positive(ckmc_generate_new_params, algo.type, &handle);
394 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
396 // invalid encryption
398 assert_crypto_invalid_param(apiEncrypt,
404 ckmc_buffer_free(encryptedTmp);
405 encryptedTmp = nullptr;
408 setParam(params, CKMC_PARAM_ED_IV, IV15);
410 setParam(params, CKMC_PARAM_ED_IV, IV17);
414 void testInvalidIvDec(const Algo& algo)
417 ckmc_raw_buffer_s* decrypted = nullptr;
420 auto ret = encrypt(algo, PLAIN_DATA);
424 assert_crypto_invalid_param(apiDecrypt,
428 *ret.encrypted.get(),
430 ckmc_buffer_free(decrypted);
435 setParam(ret.params, CKMC_PARAM_ED_IV, IV15);
437 setParam(ret.params, CKMC_PARAM_ED_IV, IV17);
441 void encryptionWithCustomData(const Algo& algo, ckmc_param_name_e name)
444 ckmc_raw_buffer_s* encrypted = nullptr;
445 ckmc_raw_buffer_s* decrypted = nullptr;
448 KeyAliasPair aliases = getKey(algo, PRIMARY);
451 ckmc_param_list_h handle = NULL;
452 assert_positive(ckmc_generate_new_params, algo.type, &handle);
453 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
455 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
458 setParam(params, name, AAD64);
461 assert_crypto_positive(apiEncrypt,
467 RawBufferPtr tmpEnc = create_raw_buffer(encrypted);
470 assert_crypto_positive(apiDecrypt,
476 RawBufferPtr tmpDec = create_raw_buffer(decrypted);
479 assert_buffers_equal(PLAIN_DATA.get(), tmpDec.get());
484 setParam(params, name, AAD32);
487 assert_crypto_result(EncryptionError::INVALID_PARAM,
496 void testGcmIvSize(ckmc_raw_buffer_s* iv,
497 const KeyAliasPair& aliases,
498 EncryptionError error = EncryptionError::SUCCESS)
501 RawBufferPtr encrypted;
502 RawBufferPtr decrypted;
503 ckmc_raw_buffer_s* encryptedTmp = nullptr;
504 ckmc_raw_buffer_s* decryptedTmp = nullptr;
507 ckmc_param_list_h handle = NULL;
508 assert_positive(ckmc_generate_new_params, CKMC_ALGO_AES_GCM, &handle);
509 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
510 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
511 setParam(params, CKMC_PARAM_ED_IV, iv);
514 assert_crypto_result(error,
522 if(error != EncryptionError::SUCCESS)
524 encrypted = create_raw_buffer(encryptedTmp);
527 assert_crypto_positive(apiDecrypt,
533 decrypted = create_raw_buffer(decryptedTmp);
535 assert_buffers_equal(PLAIN_DATA.get(), decrypted.get());
538 void testIntegrity(const Algo& algo)
541 ckmc_raw_buffer_s* decrypted = nullptr;
544 auto ret = encrypt(algo, PLAIN_DATA);
546 // break the encrypted data
547 ret.encrypted->data[BUF_LEN/2]++;
549 // no data integrity check
550 assert_crypto_positive(apiDecrypt,
554 *ret.encrypted.get(),
557 RawBufferPtr tmp = create_raw_buffer(decrypted);
558 assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
561 void testCtrEncryptionInvalidLength(const Algo& algo)
564 ckmc_raw_buffer_s* encryptedTmp = nullptr;
567 KeyAliasPair aliases = getKey(algo, PRIMARY);
570 ckmc_param_list_h handle = NULL;
571 assert_positive(ckmc_generate_new_params, algo.type, &handle);
572 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
573 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
577 assert_crypto_invalid_param(apiEncrypt,
583 ckmc_buffer_free(encryptedTmp);
584 encryptedTmp = nullptr;
586 // invalid counter size
587 setParam(params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
589 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
593 void testCtrEncryptionValidLength(const Algo& algo)
596 ckmc_raw_buffer_s* encryptedTmp = nullptr;
599 KeyAliasPair aliases = getKey(algo, PRIMARY);
602 ckmc_param_list_h handle = NULL;
603 assert_positive(ckmc_generate_new_params, algo.type, &handle);
604 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
605 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
609 assert_crypto_positive(apiEncrypt,
615 ckmc_buffer_free(encryptedTmp);
616 encryptedTmp = nullptr;
618 // valid counter sizez
619 setParam(params, CKMC_PARAM_ED_CTR_LEN, 1);
621 setParam(params, CKMC_PARAM_ED_CTR_LEN, 4);
623 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
625 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
629 void testCtrDecryptionInvalidLength(const Algo& algo)
632 ckmc_raw_buffer_s* decrypted = nullptr;
634 // add AES CTR key & encrypt
635 auto ret = encrypt(algo, PLAIN_DATA);
639 assert_crypto_invalid_param(apiDecrypt,
643 *ret.encrypted.get(),
645 ckmc_buffer_free(decrypted);
648 // invalid counter size
649 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
651 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
655 void testCtrDecryptionValidLength(const Algo& algo)
658 ckmc_raw_buffer_s* decrypted = nullptr;
660 // add AES CTR key & encrypt
661 auto ret = encrypt(algo, PLAIN_DATA);
665 assert_crypto_positive(apiDecrypt,
669 *ret.encrypted.get(),
671 ckmc_buffer_free(decrypted);
672 RawBufferPtr tmp = create_raw_buffer(decrypted);
673 assert_buffers_equal(PLAIN_DATA.get(), decrypted);
675 // invalid counter size
676 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
678 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 4);
680 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
682 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
686 void testGcmEncryptionTagLen(const Algo& algo)
689 ckmc_raw_buffer_s* encryptedTmp = nullptr;
692 KeyAliasPair aliases = getKey(algo, PRIMARY);
695 ckmc_param_list_h handle = NULL;
696 assert_positive(ckmc_generate_new_params, algo.type, &handle);
697 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
698 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
700 std::vector<TagTest> testData = {
701 // illegal tag lengths
702 { -1, EncryptionError::INVALID_PARAM },
703 { 0, EncryptionError::INVALID_PARAM },
704 { 16, EncryptionError::INVALID_PARAM },
705 { 48, EncryptionError::INVALID_PARAM },
706 { 72, EncryptionError::INVALID_PARAM },
707 { 100, EncryptionError::INVALID_PARAM },
708 { 108, EncryptionError::INVALID_PARAM },
709 { 116, EncryptionError::INVALID_PARAM },
710 { 124, EncryptionError::INVALID_PARAM },
711 { 256, EncryptionError::INVALID_PARAM },
713 { 32, EncryptionError::INVALID_PARAM },
714 { 64, EncryptionError::INVALID_PARAM },
718 { 32, EncryptionError::SUCCESS },
719 { 64, EncryptionError::SUCCESS },
722 { 96, EncryptionError::SUCCESS },
723 { 104, EncryptionError::SUCCESS },
724 { 112, EncryptionError::SUCCESS },
725 { 120, EncryptionError::SUCCESS },
726 { 128, EncryptionError::SUCCESS },
730 for(const auto& it : testData)
732 setParam(params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
733 assert_crypto_result(it.expected,
740 ckmc_buffer_free(encryptedTmp);
741 encryptedTmp = nullptr;
745 void testGcmDecryptionTagLen(const Algo& algo)
748 ckmc_raw_buffer_s* decrypted = nullptr;
750 // add AES GCM key & encrypt
751 auto ret = encrypt(algo, PLAIN_DATA);
753 std::vector<TagTest> testData = {
754 // illegal tag lengths
755 { -1, EncryptionError::INVALID_PARAM },
756 { 0, EncryptionError::INVALID_PARAM },
757 { 16, EncryptionError::INVALID_PARAM },
758 { 48, EncryptionError::INVALID_PARAM },
759 { 72, EncryptionError::INVALID_PARAM },
760 { 100, EncryptionError::INVALID_PARAM },
761 { 108, EncryptionError::INVALID_PARAM },
762 { 116, EncryptionError::INVALID_PARAM },
763 { 124, EncryptionError::INVALID_PARAM },
764 { 256, EncryptionError::INVALID_PARAM },
765 // legal tag lengths but different than the one used for encryption
766 { 32, EncryptionError::INVALID_PARAM },
767 { 64, EncryptionError::INVALID_PARAM },
768 { 96, EncryptionError::INVALID_PARAM },
769 { 104, EncryptionError::INVALID_PARAM },
770 { 112, EncryptionError::INVALID_PARAM },
771 { 120, EncryptionError::INVALID_PARAM },
772 // legal tag length that was actually used for encryption (default)
773 { 128, EncryptionError::SUCCESS },
777 for(const auto& it : testData)
779 setParam(ret.params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
780 assert_crypto_result(it.expected,
785 *ret.encrypted.get(),
787 ckmc_buffer_free(decrypted);
792 void testGcmWrongTag(const Algo& algo)
795 ckmc_raw_buffer_s* decrypted = nullptr;
797 // encrypt with AES GCM
798 auto ret = encrypt(algo, PLAIN_DATA);
800 // modify tag (last 16B of encrypted message)
801 ret.encrypted->data[ret.encrypted->size-1]++;
803 // EVP_CipherFinal fails because of an authentication error
804 assert_crypto_result(EncryptionError::INVALID_PARAM,
809 *ret.encrypted.get(),
813 void testGcmDifferentIvSizes(const Algo& algo)
816 KeyAliasPair aliases = getKey(algo, PRIMARY);
818 testGcmIvSize(IV11, aliases, EncryptionError::SERVER_ERROR); // 12B is the smallest
819 testGcmIvSize(IV12, aliases);
820 testGcmIvSize(IV17, aliases);
821 testGcmIvSize(IV128, aliases);
824 void testEncryptDecryptBigData(const Algo& algo)
827 ckmc_raw_buffer_s* decrypted = nullptr;
830 auto ret = encrypt(algo, BIG_DATA);
832 assert_positive(apiDecrypt,
836 *ret.encrypted.get(),
838 RawBufferPtr tmp = create_raw_buffer(decrypted);
840 assert_buffers_equal(BIG_DATA.get(), decrypted);
843 void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
846 ckmc_raw_buffer_s* decrypted = nullptr;
849 auto ret = encrypt(algo, PLAIN_DATA);
851 // get different keys
852 KeyAliasPair differentKeys = getKey(algo, PASSWORD_PROTECTED);
855 // some algorithms don't verify key validity
856 assert_crypto_positive(apiDecrypt,
858 differentKeys.prv.c_str(),
860 *ret.encrypted.get(),
862 RawBufferPtr tmp = create_raw_buffer(decrypted);
864 assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
866 assert_crypto_result(EncryptionError::INVALID_PARAM,
869 differentKeys.prv.c_str(),
871 *ret.encrypted.get(),
876 void testRsaLongestData(const Algo& algo, size_t dataSize)
879 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
880 ckmc_raw_buffer_s* decrypted = nullptr;
883 auto ret = encrypt(algo, plain);
885 assert_crypto_positive(apiDecrypt,
889 *ret.encrypted.get(),
891 RawBufferPtr tmp = create_raw_buffer(decrypted);
893 assert_buffers_equal(plain.get(), decrypted);
896 void testRsaDataTooLong(const Algo& algo, size_t dataSize)
899 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
902 EncryptionResult ret;
903 ckmc_raw_buffer_s* encrypted = nullptr;
904 KeyAliasPair aliases = getKey(algo, PRIMARY);
906 ckmc_param_list_h handle = NULL;
907 assert_positive(ckmc_generate_new_params, algo.type, &handle);
908 ret.params = ParamListPtr(handle, ckmc_param_list_free);
909 assert_crypto_result(EncryptionError::INVALID_PARAM,
918 } // namespace anonymous
920 RUNNER_TEST_GROUP_INIT_ENV(CKM_ENCRYPTION_DECRYPTION, EncGroupFixture);
922 /////////////////////////////////////////
923 // Generic encryption decryption tests
924 /////////////////////////////////////////
926 RUNNER_TEST_MULTIPLE(TED_0010_encrypt_invalid_param_list, SyncEnv, AsyncEnv)
928 testAllAlgorithms([](const Algo& algo){
930 ckmc_raw_buffer_s* encrypted = nullptr;
933 KeyAliasPair aliases = getKey(algo, PRIMARY);
936 assert_crypto_invalid_param(apiEncrypt,
944 ParamListPtr params = createParamListPtr();
945 assert_crypto_invalid_param(apiEncrypt,
954 RUNNER_TEST_MULTIPLE(TED_0020_encrypt_missing_key, SyncEnv, AsyncEnv)
956 testAllAlgorithms([](const Algo& algo){
958 ckmc_raw_buffer_s* encrypted = nullptr;
961 ckmc_param_list_h handle = NULL;
962 assert_positive(ckmc_generate_new_params, algo.type, &handle);
963 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
964 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
966 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
969 "non-existing-key-alias",
976 RUNNER_TEST_MULTIPLE(TED_0030_encrypt_no_plain_text, SyncEnv, AsyncEnv)
978 testAllAlgorithms([](const Algo& algo){
980 ckmc_raw_buffer_s plain = { nullptr, 0 };
981 ckmc_raw_buffer_s* encrypted = nullptr;
984 KeyAliasPair aliases = getKey(algo, PRIMARY);
987 ckmc_param_list_h handle = NULL;
988 assert_positive(ckmc_generate_new_params, algo.type, &handle);
989 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
990 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
992 assert_crypto_invalid_param(apiEncrypt,
1001 RUNNER_TEST_MULTIPLE(TED_0040_encrypt_no_output_buffer, SyncEnv, AsyncEnv)
1003 testAllAlgorithms([](const Algo& algo){
1005 ckmc_raw_buffer_s** encrypted = nullptr;
1008 KeyAliasPair aliases = getKey(algo, PRIMARY);
1011 ckmc_param_list_h handle = NULL;
1012 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1013 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1014 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1016 assert_crypto_invalid_param(apiEncrypt,
1018 aliases.pub.c_str(),
1025 RUNNER_TEST_MULTIPLE(TED_0110_decrypt_invalid_param_list, SyncEnv, AsyncEnv)
1027 testAllAlgorithms([](const Algo& algo){
1029 ckmc_raw_buffer_s* decrypted = nullptr;
1032 auto ret = encrypt(algo, PLAIN_DATA);
1035 assert_crypto_invalid_param(apiDecrypt,
1039 *ret.encrypted.get(),
1043 ParamListPtr params = createParamListPtr();
1044 assert_crypto_invalid_param(apiDecrypt,
1048 *ret.encrypted.get(),
1053 RUNNER_TEST_MULTIPLE(TED_0120_decrypt_missing_key, SyncEnv, AsyncEnv)
1055 testAllAlgorithms([](const Algo& algo){
1057 ckmc_raw_buffer_s* decrypted = nullptr;
1060 auto ret = encrypt(algo, PLAIN_DATA);
1063 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
1068 *ret.encrypted.get(),
1073 RUNNER_TEST_MULTIPLE(TED_0130_decrypt_no_encrypted_text, SyncEnv, AsyncEnv)
1075 testAllAlgorithms([](const Algo& algo){
1077 ckmc_raw_buffer_s encrypted = { nullptr, 0 };
1078 ckmc_raw_buffer_s* decrypted = nullptr;
1081 KeyAliasPair aliases = getKey(algo, PRIMARY);
1084 ckmc_param_list_h handle = NULL;
1085 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1086 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1087 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1089 assert_crypto_invalid_param(apiDecrypt,
1091 aliases.prv.c_str(),
1098 RUNNER_TEST_MULTIPLE(TED_0140_decrypt_no_output_buffer, SyncEnv, AsyncEnv)
1100 testAllAlgorithms([](const Algo& algo){
1102 ckmc_raw_buffer_s** decrypted = nullptr;
1105 auto ret = encrypt(algo, PLAIN_DATA);
1107 assert_crypto_invalid_param(apiDecrypt,
1111 *ret.encrypted.get(),
1116 RUNNER_TEST_MULTIPLE(TED_0200_encrypt_decrypt_different_keys, SyncEnv, AsyncEnv)
1118 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 128}, false);
1119 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 192}, false);
1120 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 256}, false);
1121 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 128}, true);
1122 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 192}, true);
1123 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 256}, true);
1124 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 128}, true);
1125 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 192}, true);
1126 testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 256}, true);
1127 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 1024}, false);
1128 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 2048}, false);
1129 testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 4096}, false);
1132 RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, SyncEnv, AsyncEnv)
1134 testAllAlgorithms([](const Algo& algo){
1136 ckmc_raw_buffer_s* decrypted = nullptr;
1139 auto ret = encrypt(algo, PLAIN_DATA);
1141 assert_crypto_positive(apiDecrypt,
1145 *ret.encrypted.get(),
1147 RawBufferPtr tmp = create_raw_buffer(decrypted);
1149 assert_buffers_equal(PLAIN_DATA.get(), decrypted);
1153 RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
1155 testAllAlgorithms([](const Algo& algo){
1157 ckmc_raw_buffer_s* decrypted = nullptr;
1160 auto ret = encrypt(algo, PLAIN_DATA, PASSWORD);
1163 assert_crypto_result(EncryptionError::AUTH_FAILED,
1168 *ret.encrypted.get(),
1172 assert_crypto_positive(apiDecrypt,
1176 *ret.encrypted.get(),
1178 RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
1180 assert_buffers_equal(PLAIN_DATA.get(), decrypted);
1184 // long test split into smaller ones
1185 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_128, SyncEnv, AsyncEnv)
1187 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 128});
1190 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_192, SyncEnv, AsyncEnv)
1192 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 192});
1195 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_256, SyncEnv, AsyncEnv)
1197 testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 256});
1200 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_128, SyncEnv, AsyncEnv)
1202 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 128});
1205 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_192, SyncEnv, AsyncEnv)
1207 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 192});
1210 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_256, SyncEnv, AsyncEnv)
1212 testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 256});
1215 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_128, SyncEnv, AsyncEnv)
1217 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 128});
1220 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_192, SyncEnv, AsyncEnv)
1222 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 192});
1225 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_256, SyncEnv, AsyncEnv)
1227 testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 256});
1230 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_128, SyncEnv, AsyncEnv)
1232 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 128});
1235 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_192, SyncEnv, AsyncEnv)
1237 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 192});
1240 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_256, SyncEnv, AsyncEnv)
1242 testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 256});
1245 /////////////////////////////////////////
1246 // Algorithm specific tests
1247 /////////////////////////////////////////
1249 RUNNER_TEST_MULTIPLE(TED_1005_no_iv_enc, SyncEnv, AsyncEnv)
1251 testNoIvEnc({CKMC_ALGO_AES_CTR, 128});
1252 testNoIvEnc({CKMC_ALGO_AES_CTR, 192});
1253 testNoIvEnc({CKMC_ALGO_AES_CTR, 256});
1254 testNoIvEnc({CKMC_ALGO_AES_CBC, 128});
1255 testNoIvEnc({CKMC_ALGO_AES_CBC, 192});
1256 testNoIvEnc({CKMC_ALGO_AES_CBC, 256});
1257 testNoIvEnc({CKMC_ALGO_AES_CFB, 128});
1258 testNoIvEnc({CKMC_ALGO_AES_CFB, 192});
1259 testNoIvEnc({CKMC_ALGO_AES_CFB, 256});
1260 testNoIvEnc({CKMC_ALGO_AES_GCM, 128});
1261 testNoIvEnc({CKMC_ALGO_AES_GCM, 192});
1262 testNoIvEnc({CKMC_ALGO_AES_GCM, 256});
1265 RUNNER_TEST_MULTIPLE(TED_1010_invalid_iv_enc, SyncEnv, AsyncEnv)
1267 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 128});
1268 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 192});
1269 testInvalidIvEnc({CKMC_ALGO_AES_CTR, 256});
1270 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 128});
1271 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 192});
1272 testInvalidIvEnc({CKMC_ALGO_AES_CBC, 256});
1273 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 128});
1274 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 192});
1275 testInvalidIvEnc({CKMC_ALGO_AES_CFB, 256});
1278 RUNNER_TEST_MULTIPLE(TED_1015_no_iv_dec, SyncEnv, AsyncEnv)
1280 testNoIvDec({CKMC_ALGO_AES_CTR, 128});
1281 testNoIvDec({CKMC_ALGO_AES_CTR, 192});
1282 testNoIvDec({CKMC_ALGO_AES_CTR, 256});
1283 testNoIvDec({CKMC_ALGO_AES_CBC, 128});
1284 testNoIvDec({CKMC_ALGO_AES_CBC, 192});
1285 testNoIvDec({CKMC_ALGO_AES_CBC, 256});
1286 testNoIvDec({CKMC_ALGO_AES_CFB, 128});
1287 testNoIvDec({CKMC_ALGO_AES_CFB, 192});
1288 testNoIvDec({CKMC_ALGO_AES_CFB, 256});
1289 testNoIvDec({CKMC_ALGO_AES_GCM, 128});
1290 testNoIvDec({CKMC_ALGO_AES_GCM, 192});
1291 testNoIvDec({CKMC_ALGO_AES_GCM, 256});
1294 RUNNER_TEST_MULTIPLE(TED_1020_invalid_iv_dec, SyncEnv, AsyncEnv)
1296 testInvalidIvDec({CKMC_ALGO_AES_CTR, 128});
1297 testInvalidIvDec({CKMC_ALGO_AES_CTR, 192});
1298 testInvalidIvDec({CKMC_ALGO_AES_CTR, 256});
1299 testInvalidIvDec({CKMC_ALGO_AES_CBC, 128});
1300 testInvalidIvDec({CKMC_ALGO_AES_CBC, 192});
1301 testInvalidIvDec({CKMC_ALGO_AES_CBC, 256});
1302 testInvalidIvDec({CKMC_ALGO_AES_CFB, 128});
1303 testInvalidIvDec({CKMC_ALGO_AES_CFB, 192});
1304 testInvalidIvDec({CKMC_ALGO_AES_CFB, 256});
1307 RUNNER_TEST_MULTIPLE(TED_1050_data_integrity, SyncEnv, AsyncEnv)
1309 testIntegrity({CKMC_ALGO_AES_CTR, 128});
1310 testIntegrity({CKMC_ALGO_AES_CTR, 192});
1311 testIntegrity({CKMC_ALGO_AES_CTR, 256});
1312 testIntegrity({CKMC_ALGO_AES_CBC, 128});
1313 testIntegrity({CKMC_ALGO_AES_CBC, 192});
1314 testIntegrity({CKMC_ALGO_AES_CBC, 256});
1315 testIntegrity({CKMC_ALGO_AES_CFB, 128});
1316 testIntegrity({CKMC_ALGO_AES_CFB, 192});
1317 testIntegrity({CKMC_ALGO_AES_CFB, 256});
1320 RUNNER_TEST_MULTIPLE(TED_1100_ctr_encryption_invalid_length, SyncEnv, AsyncEnv)
1322 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1323 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1324 testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1327 RUNNER_TEST_MULTIPLE(TED_1105_ctr_encryption_valid_length, SyncEnv, AsyncEnv)
1329 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1330 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1331 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1332 testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1335 RUNNER_TEST_MULTIPLE(TED_1110_ctr_decryption_invalid_length, SyncEnv, AsyncEnv)
1337 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1338 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1339 testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1342 RUNNER_TEST_MULTIPLE(TED_1115_ctr_decryption_valid_length, SyncEnv, AsyncEnv)
1344 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1345 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1346 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1347 testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1350 RUNNER_TEST_MULTIPLE(TED_1200_gcm_encryption_tag_len, SyncEnv, AsyncEnv)
1352 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1353 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1354 testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1357 RUNNER_TEST_MULTIPLE(TED_1210_gcm_decryption_tag_len, SyncEnv, AsyncEnv)
1359 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1360 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1361 testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1364 RUNNER_TEST_MULTIPLE(TED_1230_gcm_wrong_tag, SyncEnv, AsyncEnv)
1366 testGcmWrongTag({CKMC_ALGO_AES_GCM, 128});
1367 testGcmWrongTag({CKMC_ALGO_AES_GCM, 192});
1368 testGcmWrongTag({CKMC_ALGO_AES_GCM, 256});
1371 RUNNER_TEST_MULTIPLE(TED_1240_gcm_different_iv_sizes, SyncEnv, AsyncEnv)
1373 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 128});
1374 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 192});
1375 testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 256});
1378 RUNNER_TEST_MULTIPLE(TED_1250_gcm_aad, SyncEnv, AsyncEnv)
1380 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 128}, CKMC_PARAM_ED_AAD);
1381 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 192}, CKMC_PARAM_ED_AAD);
1382 encryptionWithCustomData({CKMC_ALGO_AES_GCM, 256}, CKMC_PARAM_ED_AAD);
1385 RUNNER_TEST_MULTIPLE(TED_1300_rsa_label, SyncEnv, AsyncEnv)
1387 RUNNER_IGNORED_MSG("RSA-OAEP labels are not supported in openssl");
1388 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 1024}, CKMC_PARAM_ED_LABEL);
1389 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 2048}, CKMC_PARAM_ED_LABEL);
1390 encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 4096}, CKMC_PARAM_ED_LABEL);
1393 RUNNER_TEST_MULTIPLE(TED_1330_rsa_longest_data, SyncEnv, AsyncEnv)
1395 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 1024}, 86);
1396 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 2048}, 214);
1397 testRsaLongestData({CKMC_ALGO_RSA_OAEP, 4096}, 470);
1400 RUNNER_TEST_MULTIPLE(TED_1350_rsa_data_too_long, SyncEnv, AsyncEnv)
1402 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 1024}, 87);
1403 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 2048}, 215);
1404 testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 4096}, 471);
1407 /////////////////////////////////////////
1408 // Asynchronous only tests
1409 /////////////////////////////////////////
1410 RUNNER_TEST(TED_2000_enc_no_observer_async, AsyncEnv)
1412 testAllAlgorithms([](const Algo& algo){
1414 RawBuffer plain = createRandomBuffer(BUF_LEN);
1417 KeyAliasPair aliases = getKey(algo, PRIMARY);
1420 ckmc_param_list_h handle = NULL;
1421 assert_positive(ckmc_generate_new_params, algo.type, &handle);
1422 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1423 setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1426 test_no_observer(&ManagerAsync::encrypt,
1427 *reinterpret_cast<CryptoAlgorithm*>(params.get()),
1434 RUNNER_TEST(TED_2010_dec_no_observer_async, AsyncEnv)
1436 testAllAlgorithms([](const Algo& algo){
1438 auto ret = encrypt(algo, PLAIN_DATA);
1439 RawBuffer encrypted(ret.encrypted->data, ret.encrypted->data + ret.encrypted->size);
1442 test_no_observer(&ManagerAsync::decrypt,
1443 *reinterpret_cast<CryptoAlgorithm*>(ret.params.get()),
1450 /////////////////////////////////////////
1451 // Mulithreaded test for synchronous API
1452 /////////////////////////////////////////
1453 RUNNER_TEST(TED_3000_muliple_threads, SyncEnv)
1455 std::vector<std::thread> threads;
1456 threads.reserve(10);
1457 for(unsigned i = 0; i < 10;++i)
1458 threads.emplace_back([]{ testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 256}); });
1459 for (auto& thread : threads)