2 * Copyright (c) 2000 - 2015 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)
28 #include <dpl/test/test_runner.h>
29 #include <ckm-common.h>
30 #include <ckmc/ckmc-manager.h>
31 #include <ckm/ckm-type.h>
32 #include <encryption-decryption-env.h>
38 const char* PASSWORD = "test-password";
39 const uid_t UID = 5001;
40 const size_t CTR_DEFAULT_LEN = 16*8;
41 const size_t DEFAULT_IV_LEN = 16;
42 const size_t BUF_LEN = 86; // must be less than 1024/8-41 to support RSA OAEP 1024
48 EncryptionApi* g_api = &g_syncApi;
50 EncryptionError apiEncrypt(ckmc_param_list_h params,
51 const char *key_alias,
53 const ckmc_raw_buffer_s decrypted,
54 ckmc_raw_buffer_s **ppencrypted) {
55 RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
56 return g_api->encrypt(params, key_alias, password, decrypted, ppencrypted);
59 EncryptionError apiDecrypt(ckmc_param_list_h params,
60 const char *key_alias,
62 const ckmc_raw_buffer_s encrypted,
63 ckmc_raw_buffer_s **ppdecrypted) {
64 RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
65 return g_api->decrypt(params, key_alias, password, encrypted, ppdecrypted);
68 template <typename F, typename... Args>
69 void assert_crypto_result(EncryptionError expected, F&& func, Args... args)
71 EncryptionError ret = func(args...);
72 RUNNER_ASSERT_MSG(ret == expected,
73 "Expected: " << static_cast<int>(expected) <<
74 " got: " << static_cast<int>(ret));
77 template <typename F, typename... Args>
78 void assert_crypto_positive(F&& func, Args... args)
80 assert_crypto_result(EncryptionError::SUCCESS, std::move(func), args...);
83 template <typename F, typename... Args>
84 void assert_crypto_invalid_param(F&& func, Args... args)
86 assert_crypto_result(EncryptionError::INVALID_PARAM, std::move(func), args...);
91 EncryptionError expected;
100 class EncEnv : public RemoveDataEnv<UID> {
102 EncEnv() : m_dbu(NULL) {}
103 ~EncEnv() { delete m_dbu; }
105 void init(const std::string& str) {
106 RemoveDataEnv<UID>::init(str);
107 m_dbu = new ScopedDBUnlock(UID, "db-pass"); // unlock user's database
113 RemoveDataEnv<UID>::finish();
117 ScopedDBUnlock* m_dbu;
120 struct SyncEnv : public EncEnv {
121 void init(const std::string& str) {
126 static std::string suffix() { return "_sync"; }
129 struct AsyncEnv : public EncEnv {
130 void init(const std::string& str) {
135 static std::string suffix() { return "_async"; }
139 ckmc_algo_type_e m_type;
142 AlgoBase(ckmc_algo_type_e type, size_t keyLen) : m_type(type), m_keyLen(keyLen) {}
144 virtual KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr) = 0;
147 typedef std::shared_ptr<AlgoBase> AlgoBasePtr;
149 template <typename T>
150 AlgoBasePtr createAlgo(ckmc_algo_type_e type, size_t keyLen) {
151 return AlgoBasePtr(new T(type, keyLen));
154 struct AlgoAes : public AlgoBase {
155 AlgoAes(ckmc_algo_type_e type, size_t keyLen) : AlgoBase(type, keyLen) {}
156 KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr);
159 KeyAliasPair AlgoAes::keyGen(const char* pass, const char* suffix)
161 KeyAliasPair aliases;
162 std::ostringstream oss;
163 std::string ownerId = getOwnerIdFromSelf();
164 CharPtr passPtr(nullptr, free);
166 passPtr.reset(strdup(pass));
168 oss << "aes_" << static_cast<int>(m_type) << "_" << m_keyLen << "_key_alias";
171 aliases.prv = aliasWithLabel(ownerId.c_str(),oss.str().c_str());
172 aliases.pub = aliasWithLabel(ownerId.c_str(), oss.str().c_str());
174 ckmc_policy_s policy;
175 policy.extractable = false;
176 policy.password = passPtr.get();
178 assert_positive(ckmc_create_key_aes, m_keyLen, aliases.prv.c_str(), policy);
182 struct AlgoRsa : public AlgoBase {
183 AlgoRsa(ckmc_algo_type_e type, size_t keyLen) : AlgoBase(type, keyLen) {}
184 KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr);
187 KeyAliasPair AlgoRsa::keyGen(const char* pass, const char* suffix)
189 std::ostringstream oss_prv, oss_pub;
190 oss_prv << "rsa_oaep_prv_alias_" << m_keyLen;
191 oss_pub << "rsa_oaep_pub_alias_" << m_keyLen;
196 KeyAliasPair aliases = {
197 aliasWithLabel(getOwnerIdFromSelf().c_str(), oss_prv.str().c_str()),
198 aliasWithLabel(getOwnerIdFromSelf().c_str(), oss_pub.str().c_str())
200 CharPtr passPtr(nullptr, free);
202 passPtr.reset(strdup(pass));
204 ckmc_policy_s policyPrv;
205 policyPrv.password = passPtr.get();
206 policyPrv.extractable = 0;
208 ckmc_policy_s policyPub;
209 policyPub.password = passPtr.get();
210 policyPub.extractable = 0;
212 assert_positive(ckmc_create_key_pair_rsa,
239 std::map<Algorithm, AlgoBasePtr> g_algorithms = {
240 { AES_CBC_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 128) },
241 { AES_CBC_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 192) },
242 { AES_CBC_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 256) },
243 { AES_GCM_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 128) },
244 { AES_GCM_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 192) },
245 { AES_GCM_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 256) },
246 { AES_CTR_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 128) },
247 { AES_CTR_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 192) },
248 { AES_CTR_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 256) },
249 { AES_CFB_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 128) },
250 { AES_CFB_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 192) },
251 { AES_CFB_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 256) },
252 { RSA_OAEP_1024, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 1024) },
253 { RSA_OAEP_2048, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 2048) },
254 { RSA_OAEP_4096, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 4096) },
257 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
259 int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
260 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
261 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
264 void setParam(ParamListPtr& params, ckmc_param_name_e name, int integer)
266 int ret = ckmc_param_list_set_integer(params.get(), name, integer);
267 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
268 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
271 struct EncryptionResult
273 RawBufferPtr encrypted;
279 EncryptionResult encrypt(const AlgoBasePtr& algo,
280 const RawBufferPtr& plain,
281 const char* pass = nullptr)
283 EncryptionResult ret;
284 ckmc_raw_buffer_s* encrypted = nullptr;
285 KeyAliasPair aliases = algo->keyGen(pass);
287 ckmc_param_list_h handle = NULL;
288 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
289 ret.params = ParamListPtr(handle, ckmc_param_list_free);
290 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
292 assert_crypto_positive(apiEncrypt,
299 ret.encrypted = create_raw_buffer(encrypted);
300 ret.prvKey = aliases.prv;
301 ret.pubKey = aliases.pub;
305 void testAllAlgorithms(
306 const std::function<void(const AlgoBasePtr& algo)>& test)
308 for(const auto& it : g_algorithms)
312 void testNoIvEnc(Algorithm type)
314 const AlgoBasePtr& algo = g_algorithms.at(type);
317 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
318 ckmc_raw_buffer_s* encrypted = nullptr;
321 KeyAliasPair aliases = algo->keyGen();
323 // param list with algo type only
324 ParamListPtr params = createParamListPtr();
325 setParam(params, CKMC_PARAM_ALGO_TYPE, algo->m_type);
326 assert_crypto_invalid_param(apiEncrypt,
334 void testNoIvDec(Algorithm type)
336 const AlgoBasePtr& algo = g_algorithms.at(type);
339 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
340 ckmc_raw_buffer_s* decrypted = nullptr;
343 auto ret = encrypt(algo, plain);
345 // param list with algo type only
346 ParamListPtr params = createParamListPtr();
347 setParam(params, CKMC_PARAM_ALGO_TYPE, algo->m_type);
348 assert_crypto_invalid_param(apiDecrypt,
352 *ret.encrypted.get(),
356 void testInvalidIvEnc(Algorithm type)
358 const AlgoBasePtr& algo = g_algorithms.at(type);
361 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
362 ckmc_raw_buffer_s* encryptedTmp = nullptr;
365 KeyAliasPair aliases = algo->keyGen();
368 ckmc_param_list_h handle = NULL;
369 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
370 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
372 // invalid encryption
374 assert_crypto_invalid_param(apiEncrypt,
380 ckmc_buffer_free(encryptedTmp);
381 encryptedTmp = nullptr;
384 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN-1));
386 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN+1));
390 void testInvalidIvDec(Algorithm type)
392 const AlgoBasePtr& algo = g_algorithms.at(type);
395 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
396 ckmc_raw_buffer_s* decrypted = nullptr;
399 auto ret = encrypt(algo, plain);
403 assert_crypto_invalid_param(apiDecrypt,
407 *ret.encrypted.get(),
409 ckmc_buffer_free(decrypted);
414 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN-1));
416 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN+1));
420 void encryptionWithCustomData(Algorithm type, ckmc_param_name_e name)
422 const AlgoBasePtr& algo = g_algorithms.at(type);
425 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
426 ckmc_raw_buffer_s* encrypted = nullptr;
427 ckmc_raw_buffer_s* decrypted = nullptr;
430 KeyAliasPair aliases = algo->keyGen();
433 ckmc_param_list_h handle = NULL;
434 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
435 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
437 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
440 setParam(params, name, createRandomBufferCAPI(64));
443 assert_crypto_positive(apiEncrypt,
449 RawBufferPtr tmpEnc = create_raw_buffer(encrypted);
452 assert_crypto_positive(apiDecrypt,
458 RawBufferPtr tmpDec = create_raw_buffer(decrypted);
461 assert_buffers_equal(*plain.get(), *tmpDec.get());
466 setParam(params, name, createRandomBufferCAPI(32));
469 assert_crypto_result(EncryptionError::SERVER_ERROR,
478 void testGcmIvSize(size_t size,
479 const KeyAliasPair& aliases,
480 EncryptionError error = EncryptionError::SUCCESS)
483 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
484 RawBufferPtr encrypted;
485 RawBufferPtr decrypted;
486 ckmc_raw_buffer_s* encryptedTmp = nullptr;
487 ckmc_raw_buffer_s* decryptedTmp = nullptr;
490 ckmc_param_list_h handle = NULL;
491 assert_positive(ckmc_generate_new_params, CKMC_ALGO_AES_GCM, &handle);
492 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
493 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
494 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(size));
497 assert_crypto_result(error,
505 if(error != EncryptionError::SUCCESS)
507 encrypted = create_raw_buffer(encryptedTmp);
510 assert_crypto_positive(apiDecrypt,
516 decrypted = create_raw_buffer(decryptedTmp);
518 assert_buffers_equal(*plain.get(), *decrypted.get());
521 void testIntegrity(Algorithm type)
523 const AlgoBasePtr& algo = g_algorithms.at(type);
526 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
527 ckmc_raw_buffer_s* decrypted = nullptr;
530 auto ret = encrypt(algo, plain);
532 // break the encrypted data
533 ret.encrypted->data[BUF_LEN/2]++;
535 // no data integrity check
536 assert_crypto_positive(apiDecrypt,
540 *ret.encrypted.get(),
543 RawBufferPtr tmp = create_raw_buffer(decrypted);
544 assert_buffers_equal(*plain.get(), *decrypted, false);
547 void testCtrEncryptionInvalidLength(Algorithm type)
549 const AlgoBasePtr& algo = g_algorithms.at(type);
552 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
553 ckmc_raw_buffer_s* encryptedTmp = nullptr;
556 KeyAliasPair aliases = algo->keyGen();
559 ckmc_param_list_h handle = NULL;
560 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
561 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
562 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
566 assert_crypto_invalid_param(apiEncrypt,
572 ckmc_buffer_free(encryptedTmp);
573 encryptedTmp = nullptr;
575 // invalid counter size
576 setParam(params, CKMC_PARAM_ED_CTR_LEN, -1);
578 setParam(params, CKMC_PARAM_ED_CTR_LEN, 0);
580 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
584 void testCtrEncryptionValidLength(Algorithm type)
586 const AlgoBasePtr& algo = g_algorithms.at(type);
589 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
590 ckmc_raw_buffer_s* encryptedTmp = nullptr;
593 KeyAliasPair aliases = algo->keyGen();
596 ckmc_param_list_h handle = NULL;
597 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
598 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
599 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
603 assert_crypto_positive(apiEncrypt,
609 ckmc_buffer_free(encryptedTmp);
610 encryptedTmp = nullptr;
612 // valid counter sizez
613 setParam(params, CKMC_PARAM_ED_CTR_LEN, 1);
615 setParam(params, CKMC_PARAM_ED_CTR_LEN, 4);
617 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
619 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
623 void testCtrDecryptionInvalidLength(Algorithm type)
625 const AlgoBasePtr& algo = g_algorithms.at(type);
628 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
629 ckmc_raw_buffer_s* decrypted = nullptr;
631 // add AES CTR key & encrypt
632 auto ret = encrypt(algo, plain);
636 assert_crypto_invalid_param(apiDecrypt,
640 *ret.encrypted.get(),
642 ckmc_buffer_free(decrypted);
645 // invalid counter size
646 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, -1);
648 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0);
650 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
654 void testCtrDecryptionValidLength(Algorithm type)
656 const AlgoBasePtr& algo = g_algorithms.at(type);
659 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
660 ckmc_raw_buffer_s* decrypted = nullptr;
662 // add AES CTR key & encrypt
663 auto ret = encrypt(algo, plain);
667 assert_crypto_positive(apiDecrypt,
671 *ret.encrypted.get(),
673 ckmc_buffer_free(decrypted);
674 RawBufferPtr tmp = create_raw_buffer(decrypted);
675 assert_buffers_equal(*plain.get(), *decrypted);
677 // invalid counter size
678 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
680 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 4);
682 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
684 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
688 void testGcmEncryptionTagLen(Algorithm type)
690 const AlgoBasePtr& algo = g_algorithms.at(type);
693 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
694 ckmc_raw_buffer_s* encryptedTmp = nullptr;
697 KeyAliasPair aliases = algo->keyGen();
700 ckmc_param_list_h handle = NULL;
701 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
702 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
703 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
705 std::vector<TagTest> testData = {
706 // illegal tag lengths
707 { -1, EncryptionError::INVALID_PARAM },
708 { 0, EncryptionError::INVALID_PARAM },
709 { 16, EncryptionError::INVALID_PARAM },
710 { 48, EncryptionError::INVALID_PARAM },
711 { 72, EncryptionError::INVALID_PARAM },
712 { 100, EncryptionError::INVALID_PARAM },
713 { 108, EncryptionError::INVALID_PARAM },
714 { 116, EncryptionError::INVALID_PARAM },
715 { 124, EncryptionError::INVALID_PARAM },
716 { 256, EncryptionError::INVALID_PARAM },
718 { 32, EncryptionError::SUCCESS },
719 { 64, EncryptionError::SUCCESS },
720 { 96, EncryptionError::SUCCESS },
721 { 104, EncryptionError::SUCCESS },
722 { 112, EncryptionError::SUCCESS },
723 { 120, EncryptionError::SUCCESS },
724 { 128, EncryptionError::SUCCESS },
728 for(const auto& it : testData)
730 setParam(params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
731 assert_crypto_result(it.expected,
738 ckmc_buffer_free(encryptedTmp);
739 encryptedTmp = nullptr;
743 void testGcmDecryptionTagLen(Algorithm type)
745 const AlgoBasePtr& algo = g_algorithms.at(type);
748 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
749 ckmc_raw_buffer_s* decrypted = nullptr;
751 // add AES GCM key & encrypt
752 auto ret = encrypt(algo, plain);
754 std::vector<TagTest> testData = {
755 // illegal tag lengths
756 { -1, EncryptionError::INVALID_PARAM },
757 { 0, EncryptionError::INVALID_PARAM },
758 { 16, EncryptionError::INVALID_PARAM },
759 { 48, EncryptionError::INVALID_PARAM },
760 { 72, EncryptionError::INVALID_PARAM },
761 { 100, EncryptionError::INVALID_PARAM },
762 { 108, EncryptionError::INVALID_PARAM },
763 { 116, EncryptionError::INVALID_PARAM },
764 { 124, EncryptionError::INVALID_PARAM },
765 { 256, EncryptionError::INVALID_PARAM },
766 // legal tag lengths (EVP_CipherFinal fails but we can't get the error code)
767 { 32, EncryptionError::SERVER_ERROR },
768 { 64, EncryptionError::SERVER_ERROR },
769 { 96, EncryptionError::SERVER_ERROR },
770 { 104, EncryptionError::SERVER_ERROR },
771 { 112, EncryptionError::SERVER_ERROR },
772 { 120, EncryptionError::SERVER_ERROR },
773 // legal tag length that was actually used for encryption (default)
774 { 128, EncryptionError::SUCCESS },
778 for(const auto& it : testData)
780 setParam(ret.params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
781 assert_crypto_result(it.expected,
786 *ret.encrypted.get(),
788 ckmc_buffer_free(decrypted);
793 void testGcmWrongTag(Algorithm type)
795 const AlgoBasePtr& algo = g_algorithms.at(type);
798 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
799 ckmc_raw_buffer_s* decrypted = nullptr;
801 // encrypt with AES GCM
802 auto ret = encrypt(algo, plain);
804 // modify tag (last 16B of encrypted message)
805 ret.encrypted->data[ret.encrypted->size-1]++;
807 // EVP_CipherFinal fails but we can't get error code
808 assert_crypto_result(EncryptionError::SERVER_ERROR,
813 *ret.encrypted.get(),
817 void testGcmDifferentIvSizes(Algorithm type)
819 const AlgoBasePtr& algo = g_algorithms.at(type);
822 KeyAliasPair aliases = algo->keyGen();
824 testGcmIvSize(11, aliases, EncryptionError::SERVER_ERROR); // 12B is the smallest
825 testGcmIvSize(12, aliases);
826 testGcmIvSize(17, aliases);
827 testGcmIvSize(128, aliases);
830 void testEncryptDecryptBigData(Algorithm type)
832 const AlgoBasePtr& algo = g_algorithms.at(type);
835 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(5000000));
836 ckmc_raw_buffer_s* decrypted = nullptr;
839 auto ret = encrypt(algo, plain);
841 assert_positive(apiDecrypt,
845 *ret.encrypted.get(),
847 RawBufferPtr tmp = create_raw_buffer(decrypted);
849 assert_buffers_equal(*plain.get(), *decrypted);
852 void testEncryptDecryptDifferentKeys(Algorithm type, bool success)
854 const AlgoBasePtr& algo = g_algorithms.at(type);
856 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
857 ckmc_raw_buffer_s* decrypted = nullptr;
860 auto ret = encrypt(algo, plain);
863 KeyAliasPair differentKeys = algo->keyGen(nullptr, "_wrong");
867 // some algorithms don't verify key validity
868 assert_crypto_positive(apiDecrypt,
870 differentKeys.prv.c_str(),
872 *ret.encrypted.get(),
874 RawBufferPtr tmp = create_raw_buffer(decrypted);
876 assert_buffers_equal(*plain.get(), *decrypted, false);
878 // different key should not be accepted
879 assert_crypto_result(EncryptionError::SERVER_ERROR,
882 differentKeys.prv.c_str(),
884 *ret.encrypted.get(),
888 // Cleanup before testing next algorithm. Ignore results because not all keys are present
889 ckmc_remove_alias(ret.prvKey.c_str());
890 ckmc_remove_alias(ret.pubKey.c_str());
891 ckmc_remove_alias(differentKeys.prv.c_str());
892 ckmc_remove_alias(differentKeys.pub.c_str());
895 void testRsaLongestData(Algorithm type, size_t dataSize)
897 const AlgoBasePtr& algo = g_algorithms.at(type);
899 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
900 ckmc_raw_buffer_s* decrypted = nullptr;
903 auto ret = encrypt(algo, plain);
905 assert_crypto_positive(apiDecrypt,
909 *ret.encrypted.get(),
911 RawBufferPtr tmp = create_raw_buffer(decrypted);
913 assert_buffers_equal(*plain.get(), *decrypted);
916 void testRsaDataTooLong(Algorithm type, size_t dataSize)
918 const AlgoBasePtr& algo = g_algorithms.at(type);
920 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
923 EncryptionResult ret;
924 ckmc_raw_buffer_s* encrypted = nullptr;
925 KeyAliasPair aliases = algo->keyGen();
927 ckmc_param_list_h handle = NULL;
928 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
929 ret.params = ParamListPtr(handle, ckmc_param_list_free);
930 assert_crypto_result(EncryptionError::SERVER_ERROR,
939 } // namespace anonymous
942 RUNNER_TEST_GROUP_INIT(CKM_ENCRYPTION_DECRYPTION);
944 /////////////////////////////////////////
945 // Generic encryption decryption tests
946 /////////////////////////////////////////
948 RUNNER_TEST_MULTIPLE(TED_0010_encrypt_invalid_param_list, SyncEnv, AsyncEnv)
950 testAllAlgorithms([](const AlgoBasePtr& algo){
952 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
953 ckmc_raw_buffer_s* encrypted = nullptr;
956 KeyAliasPair aliases = algo->keyGen();
959 assert_crypto_invalid_param(apiEncrypt,
967 ParamListPtr params = createParamListPtr();
968 assert_crypto_invalid_param(apiEncrypt,
977 RUNNER_TEST_MULTIPLE(TED_0020_encrypt_missing_key, SyncEnv, AsyncEnv)
979 testAllAlgorithms([](const AlgoBasePtr& algo){
981 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
982 ckmc_raw_buffer_s* encrypted = nullptr;
985 ckmc_param_list_h handle = NULL;
986 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
987 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
988 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
990 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
993 "non-existing-key-alias",
1000 RUNNER_TEST_MULTIPLE(TED_0030_encrypt_no_plain_text, SyncEnv, AsyncEnv)
1002 testAllAlgorithms([](const AlgoBasePtr& algo){
1004 ckmc_raw_buffer_s plain = { nullptr, 0 };
1005 ckmc_raw_buffer_s* encrypted = nullptr;
1008 KeyAliasPair aliases = algo->keyGen();
1011 ckmc_param_list_h handle = NULL;
1012 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
1013 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1014 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1016 assert_crypto_invalid_param(apiEncrypt,
1018 aliases.pub.c_str(),
1025 RUNNER_TEST_MULTIPLE(TED_0040_encrypt_no_output_buffer, SyncEnv, AsyncEnv)
1027 testAllAlgorithms([](const AlgoBasePtr& algo){
1029 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1030 ckmc_raw_buffer_s** encrypted = nullptr;
1033 KeyAliasPair aliases = algo->keyGen();
1036 ckmc_param_list_h handle = NULL;
1037 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
1038 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1039 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1041 assert_crypto_invalid_param(apiEncrypt,
1043 aliases.pub.c_str(),
1050 RUNNER_TEST_MULTIPLE(TED_0110_decrypt_invalid_param_list, SyncEnv, AsyncEnv)
1052 testAllAlgorithms([](const AlgoBasePtr& algo){
1054 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1055 ckmc_raw_buffer_s* decrypted = nullptr;
1058 auto ret = encrypt(algo, plain);
1061 assert_crypto_invalid_param(apiDecrypt,
1065 *ret.encrypted.get(),
1069 ParamListPtr params = createParamListPtr();
1070 assert_crypto_invalid_param(apiDecrypt,
1074 *ret.encrypted.get(),
1079 RUNNER_TEST_MULTIPLE(TED_0120_decrypt_missing_key, SyncEnv, AsyncEnv)
1081 testAllAlgorithms([](const AlgoBasePtr& algo){
1083 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1084 ckmc_raw_buffer_s* decrypted = nullptr;
1087 auto ret = encrypt(algo, plain);
1090 assert_positive(ckmc_remove_alias, ret.prvKey.c_str());
1093 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
1098 *ret.encrypted.get(),
1103 RUNNER_TEST_MULTIPLE(TED_0130_decrypt_no_encrypted_text, SyncEnv, AsyncEnv)
1105 testAllAlgorithms([](const AlgoBasePtr& algo){
1107 ckmc_raw_buffer_s encrypted = { nullptr, 0 };
1108 ckmc_raw_buffer_s* decrypted = nullptr;
1111 KeyAliasPair aliases = algo->keyGen();
1114 ckmc_param_list_h handle = NULL;
1115 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
1116 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1117 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1119 assert_crypto_invalid_param(apiDecrypt,
1121 aliases.prv.c_str(),
1128 RUNNER_TEST_MULTIPLE(TED_0140_decrypt_no_output_buffer, SyncEnv, AsyncEnv)
1130 testAllAlgorithms([](const AlgoBasePtr& algo){
1132 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1133 ckmc_raw_buffer_s** decrypted = nullptr;
1136 auto ret = encrypt(algo, plain);
1138 assert_crypto_invalid_param(apiDecrypt,
1142 *ret.encrypted.get(),
1147 RUNNER_TEST_MULTIPLE(TED_0200_encrypt_decrypt_different_keys, SyncEnv, AsyncEnv)
1149 testEncryptDecryptDifferentKeys(AES_CBC_128, false);
1150 testEncryptDecryptDifferentKeys(AES_CBC_192, false);
1151 testEncryptDecryptDifferentKeys(AES_CBC_256, false);
1152 testEncryptDecryptDifferentKeys(AES_GCM_128, false);
1153 testEncryptDecryptDifferentKeys(AES_GCM_192, false);
1154 testEncryptDecryptDifferentKeys(AES_GCM_256, false);
1155 testEncryptDecryptDifferentKeys(AES_CTR_128, true);
1156 testEncryptDecryptDifferentKeys(AES_CTR_192, true);
1157 testEncryptDecryptDifferentKeys(AES_CTR_256, true);
1158 testEncryptDecryptDifferentKeys(AES_CFB_128, true);
1159 testEncryptDecryptDifferentKeys(AES_CFB_192, true);
1160 testEncryptDecryptDifferentKeys(AES_CFB_256, true);
1161 testEncryptDecryptDifferentKeys(RSA_OAEP_1024, false);
1162 testEncryptDecryptDifferentKeys(RSA_OAEP_2048, false);
1163 testEncryptDecryptDifferentKeys(RSA_OAEP_4096, false);
1166 RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, SyncEnv, AsyncEnv)
1168 testAllAlgorithms([](const AlgoBasePtr& algo){
1170 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1171 ckmc_raw_buffer_s* decrypted = nullptr;
1174 auto ret = encrypt(algo, plain);
1176 assert_crypto_positive(apiDecrypt,
1180 *ret.encrypted.get(),
1182 RawBufferPtr tmp = create_raw_buffer(decrypted);
1184 assert_buffers_equal(*plain.get(), *decrypted);
1188 RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
1190 testAllAlgorithms([](const AlgoBasePtr& algo){
1192 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1193 ckmc_raw_buffer_s* decrypted = nullptr;
1196 auto ret = encrypt(algo, plain, PASSWORD);
1199 assert_crypto_result(EncryptionError::AUTH_FAILED,
1204 *ret.encrypted.get(),
1208 assert_crypto_positive(apiDecrypt,
1212 *ret.encrypted.get(),
1214 RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
1216 assert_buffers_equal(*plain.get(), *decrypted);
1220 // long test split into smaller ones
1221 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cbc_128, SyncEnv, AsyncEnv)
1223 testEncryptDecryptBigData(AES_CBC_128);
1226 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cbc_192, SyncEnv, AsyncEnv)
1228 testEncryptDecryptBigData(AES_CBC_192);
1231 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cbc_256, SyncEnv, AsyncEnv)
1233 testEncryptDecryptBigData(AES_CBC_256);
1236 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_gcm_128, SyncEnv, AsyncEnv)
1238 testEncryptDecryptBigData(AES_GCM_128);
1241 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_gcm_192, SyncEnv, AsyncEnv)
1243 testEncryptDecryptBigData(AES_GCM_192);
1246 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_gcm_256, SyncEnv, AsyncEnv)
1248 testEncryptDecryptBigData(AES_GCM_256);
1251 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_ctr_128, SyncEnv, AsyncEnv)
1253 testEncryptDecryptBigData(AES_CTR_128);
1256 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_ctr_192, SyncEnv, AsyncEnv)
1258 testEncryptDecryptBigData(AES_CTR_192);
1261 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_ctr_256, SyncEnv, AsyncEnv)
1263 testEncryptDecryptBigData(AES_CTR_256);
1266 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cfb_128, SyncEnv, AsyncEnv)
1268 testEncryptDecryptBigData(AES_CFB_128);
1271 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cfb_192, SyncEnv, AsyncEnv)
1273 testEncryptDecryptBigData(AES_CFB_192);
1276 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cfb_256, SyncEnv, AsyncEnv)
1278 testEncryptDecryptBigData(AES_CFB_256);
1281 /////////////////////////////////////////
1282 // Algorithm specific tests
1283 /////////////////////////////////////////
1285 RUNNER_TEST_MULTIPLE(TED_1005_no_iv_enc, SyncEnv, AsyncEnv)
1287 testNoIvEnc(AES_CTR_128);
1288 testNoIvEnc(AES_CTR_192);
1289 testNoIvEnc(AES_CTR_256);
1290 testNoIvEnc(AES_CBC_128);
1291 testNoIvEnc(AES_CBC_192);
1292 testNoIvEnc(AES_CBC_256);
1293 testNoIvEnc(AES_CFB_128);
1294 testNoIvEnc(AES_CFB_192);
1295 testNoIvEnc(AES_CFB_256);
1296 testNoIvEnc(AES_GCM_128);
1297 testNoIvEnc(AES_GCM_192);
1298 testNoIvEnc(AES_GCM_256);
1301 RUNNER_TEST_MULTIPLE(TED_1010_invalid_iv_enc, SyncEnv, AsyncEnv)
1303 testInvalidIvEnc(AES_CTR_128);
1304 testInvalidIvEnc(AES_CTR_192);
1305 testInvalidIvEnc(AES_CTR_256);
1306 testInvalidIvEnc(AES_CBC_128);
1307 testInvalidIvEnc(AES_CBC_192);
1308 testInvalidIvEnc(AES_CBC_256);
1309 testInvalidIvEnc(AES_CFB_128);
1310 testInvalidIvEnc(AES_CFB_192);
1311 testInvalidIvEnc(AES_CFB_256);
1314 RUNNER_TEST_MULTIPLE(TED_1015_no_iv_dec, SyncEnv, AsyncEnv)
1316 testNoIvDec(AES_CTR_128);
1317 testNoIvDec(AES_CTR_192);
1318 testNoIvDec(AES_CTR_256);
1319 testNoIvDec(AES_CBC_128);
1320 testNoIvDec(AES_CBC_192);
1321 testNoIvDec(AES_CBC_256);
1322 testNoIvDec(AES_CFB_128);
1323 testNoIvDec(AES_CFB_192);
1324 testNoIvDec(AES_CFB_256);
1325 testNoIvDec(AES_GCM_128);
1326 testNoIvDec(AES_GCM_192);
1327 testNoIvDec(AES_GCM_256);
1330 RUNNER_TEST_MULTIPLE(TED_1020_invalid_iv_dec, SyncEnv, AsyncEnv)
1332 testInvalidIvDec(AES_CTR_128);
1333 testInvalidIvDec(AES_CTR_192);
1334 testInvalidIvDec(AES_CTR_256);
1335 testInvalidIvDec(AES_CBC_128);
1336 testInvalidIvDec(AES_CBC_192);
1337 testInvalidIvDec(AES_CBC_256);
1338 testInvalidIvDec(AES_CFB_128);
1339 testInvalidIvDec(AES_CFB_192);
1340 testInvalidIvDec(AES_CFB_256);
1343 RUNNER_TEST_MULTIPLE(TED_1050_data_integrity, SyncEnv, AsyncEnv)
1345 testIntegrity(AES_CTR_128);
1346 testIntegrity(AES_CTR_192);
1347 testIntegrity(AES_CTR_256);
1348 testIntegrity(AES_CBC_128);
1349 testIntegrity(AES_CBC_192);
1350 testIntegrity(AES_CBC_256);
1351 testIntegrity(AES_CFB_128);
1352 testIntegrity(AES_CFB_192);
1353 testIntegrity(AES_CFB_256);
1356 RUNNER_TEST_MULTIPLE(TED_1100_ctr_encryption_invalid_length, SyncEnv, AsyncEnv)
1358 testCtrEncryptionInvalidLength(AES_CTR_128);
1359 testCtrEncryptionInvalidLength(AES_CTR_192);
1360 testCtrEncryptionInvalidLength(AES_CTR_256);
1363 RUNNER_TEST_MULTIPLE(TED_1105_ctr_encryption_valid_length, SyncEnv, AsyncEnv)
1365 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1366 testCtrEncryptionValidLength(AES_CTR_128);
1367 testCtrEncryptionValidLength(AES_CTR_192);
1368 testCtrEncryptionValidLength(AES_CTR_256);
1371 RUNNER_TEST_MULTIPLE(TED_1110_ctr_decryption_invalid_length, SyncEnv, AsyncEnv)
1373 testCtrDecryptionInvalidLength(AES_CTR_128);
1374 testCtrDecryptionInvalidLength(AES_CTR_192);
1375 testCtrDecryptionInvalidLength(AES_CTR_256);
1378 RUNNER_TEST_MULTIPLE(TED_1115_ctr_decryption_valid_length, SyncEnv, AsyncEnv)
1380 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1381 testCtrDecryptionValidLength(AES_CTR_128);
1382 testCtrDecryptionValidLength(AES_CTR_192);
1383 testCtrDecryptionValidLength(AES_CTR_256);
1386 RUNNER_TEST_MULTIPLE(TED_1200_gcm_encryption_tag_len, SyncEnv, AsyncEnv)
1388 testGcmEncryptionTagLen(AES_GCM_128);
1389 testGcmEncryptionTagLen(AES_GCM_192);
1390 testGcmEncryptionTagLen(AES_GCM_256);
1393 RUNNER_TEST_MULTIPLE(TED_1210_gcm_decryption_tag_len, SyncEnv, AsyncEnv)
1395 testGcmDecryptionTagLen(AES_GCM_128);
1396 testGcmDecryptionTagLen(AES_GCM_192);
1397 testGcmDecryptionTagLen(AES_GCM_256);
1400 RUNNER_TEST_MULTIPLE(TED_1230_gcm_wrong_tag, SyncEnv, AsyncEnv)
1402 testGcmWrongTag(AES_GCM_128);
1403 testGcmWrongTag(AES_GCM_192);
1404 testGcmWrongTag(AES_GCM_256);
1407 RUNNER_TEST_MULTIPLE(TED_1240_gcm_different_iv_sizes, SyncEnv, AsyncEnv)
1409 testGcmDifferentIvSizes(AES_GCM_128);
1410 testGcmDifferentIvSizes(AES_GCM_192);
1411 testGcmDifferentIvSizes(AES_GCM_256);
1414 RUNNER_TEST_MULTIPLE(TED_1250_gcm_aad, SyncEnv, AsyncEnv)
1416 encryptionWithCustomData(AES_GCM_128, CKMC_PARAM_ED_AAD);
1417 encryptionWithCustomData(AES_GCM_192, CKMC_PARAM_ED_AAD);
1418 encryptionWithCustomData(AES_GCM_256, CKMC_PARAM_ED_AAD);
1421 RUNNER_TEST_MULTIPLE(TED_1300_rsa_label, SyncEnv, AsyncEnv)
1423 RUNNER_IGNORED_MSG("RSA-OAEP labels are not supported in openssl");
1424 encryptionWithCustomData(RSA_OAEP_1024, CKMC_PARAM_ED_LABEL);
1425 encryptionWithCustomData(RSA_OAEP_2048, CKMC_PARAM_ED_LABEL);
1426 encryptionWithCustomData(RSA_OAEP_4096, CKMC_PARAM_ED_LABEL);
1429 RUNNER_TEST_MULTIPLE(TED_1330_rsa_longest_data, SyncEnv, AsyncEnv)
1431 testRsaLongestData(RSA_OAEP_1024, 86);
1432 testRsaLongestData(RSA_OAEP_2048, 214);
1433 testRsaLongestData(RSA_OAEP_4096, 470);
1436 RUNNER_TEST_MULTIPLE(TED_1350_rsa_data_too_long, SyncEnv, AsyncEnv)
1438 testRsaDataTooLong(RSA_OAEP_1024, 87);
1439 testRsaDataTooLong(RSA_OAEP_2048, 215);
1440 testRsaDataTooLong(RSA_OAEP_4096, 471);
1443 /////////////////////////////////////////
1444 // Asynchronous only tests
1445 /////////////////////////////////////////
1446 RUNNER_TEST(TED_2000_enc_no_observer_async, EncEnv)
1448 testAllAlgorithms([](const AlgoBasePtr& algo){
1450 RawBuffer plain = createRandomBuffer(BUF_LEN);
1453 KeyAliasPair aliases = algo->keyGen(nullptr);
1456 ckmc_param_list_h handle = NULL;
1457 assert_positive(ckmc_generate_new_params, algo->m_type, &handle);
1458 ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1459 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1462 test_no_observer(&ManagerAsync::encrypt,
1463 *reinterpret_cast<CryptoAlgorithm*>(params.get()),
1470 RUNNER_TEST(TED_2010_dec_no_observer_async, AsyncEnv)
1472 testAllAlgorithms([](const AlgoBasePtr& algo){
1474 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1477 auto ret = encrypt(algo, plain);
1478 RawBuffer encrypted(ret.encrypted->data, ret.encrypted->data + ret.encrypted->size);
1481 test_no_observer(&ManagerAsync::decrypt,
1482 *reinterpret_cast<CryptoAlgorithm*>(ret.params.get()),