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 <access_provider2.h>
36 const char* PASSWORD = "test-password";
37 const uid_t UID = 5555;
38 const gid_t GID = 5555;
39 const size_t CTR_DEFAULT_LEN = 16*8;
40 const size_t DEFAULT_IV_LEN = 16;
41 const size_t BUF_LEN = 86; // must be less than 1024/8-41 to support RSA OAEP 1024
54 class EncEnv : public RemoveDataEnv<UID> {
56 EncEnv() : m_dbu(NULL), m_sap(NULL) {}
57 ~EncEnv() { delete m_sap; delete m_dbu; }
59 void init(const std::string& str) {
60 RemoveDataEnv<UID>::init(str);
61 m_dbu = new ScopedDBUnlock(UID, "db-pass"); // unlock user's database
62 m_sap = new ScopedAccessProvider("my-label"); // setup label
64 // setup smack rules and switch user
65 m_sap->allowAPI("key-manager::api-storage", "rw");
66 m_sap->allowAPI("key-manager::api-encryption", "rw");
67 m_sap->applyAndSwithToUser(UID, GID);
75 RemoveDataEnv<UID>::finish();
78 ScopedDBUnlock* m_dbu;
79 ScopedAccessProvider* m_sap;
83 ckmc_algo_type_e m_type;
86 AlgoBase(ckmc_algo_type_e type, size_t keyLen) : m_type(type), m_keyLen(keyLen) {}
88 virtual KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr) = 0;
91 typedef std::shared_ptr<AlgoBase> AlgoBasePtr;
94 AlgoBasePtr createAlgo(ckmc_algo_type_e type, size_t keyLen) {
95 return AlgoBasePtr(new T(type, keyLen));
98 struct AlgoAes : public AlgoBase {
99 AlgoAes(ckmc_algo_type_e type, size_t keyLen) : AlgoBase(type, keyLen) {}
100 KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr);
103 KeyAliasPair AlgoAes::keyGen(const char* pass, const char* suffix)
105 KeyAliasPair aliases;
106 std::ostringstream oss;
107 CharPtr my_label = get_label();
108 CharPtr passPtr(nullptr, free);
110 passPtr.reset(strdup(pass));
112 oss << "aes_" << static_cast<int>(m_type) << "_" << m_keyLen << "_key_alias";
115 aliases.prv = aliasWithLabel(my_label.get(),oss.str().c_str());
116 aliases.pub = aliasWithLabel(my_label.get(), oss.str().c_str());
118 ckmc_policy_s policy;
119 policy.extractable = false;
120 policy.password = passPtr.get();
122 assert_positive(ckmc_create_key_aes, m_keyLen, aliases.prv.c_str(), policy);
126 struct AlgoRsa : public AlgoBase {
127 AlgoRsa(ckmc_algo_type_e type, size_t keyLen) : AlgoBase(type, keyLen) {}
128 KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr);
131 KeyAliasPair AlgoRsa::keyGen(const char* pass, const char* suffix)
133 std::ostringstream oss_prv, oss_pub;
134 oss_prv << "rsa_oaep_prv_alias_" << m_keyLen;
135 oss_pub << "rsa_oaep_pub_alias_" << m_keyLen;
140 KeyAliasPair aliases = {
141 aliasWithLabel(get_label().get(), oss_prv.str().c_str()),
142 aliasWithLabel(get_label().get(), oss_pub.str().c_str())
144 CharPtr passPtr(nullptr, free);
146 passPtr.reset(strdup(pass));
148 ckmc_policy_s policyPrv;
149 policyPrv.password = passPtr.get();
150 policyPrv.extractable = 0;
152 ckmc_policy_s policyPub;
153 policyPub.password = passPtr.get();
154 policyPub.extractable = 0;
156 assert_positive(ckmc_create_key_pair_rsa,
183 std::map<Algorithm, AlgoBasePtr> g_algorithms = {
184 { AES_CBC_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 128) },
185 { AES_CBC_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 192) },
186 { AES_CBC_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 256) },
187 { AES_GCM_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 128) },
188 { AES_GCM_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 192) },
189 { AES_GCM_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 256) },
190 { AES_CTR_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 128) },
191 { AES_CTR_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 192) },
192 { AES_CTR_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 256) },
193 { AES_CFB_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 128) },
194 { AES_CFB_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 192) },
195 { AES_CFB_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 256) },
196 { RSA_OAEP_1024, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 1024) },
197 { RSA_OAEP_2048, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 2048) },
198 { RSA_OAEP_4096, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 4096) },
201 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
203 int ret = ckmc_param_list_add_buffer(params.get(), name, buffer);
204 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
205 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
208 void setParam(ParamListPtr& params, ckmc_param_name_e name, int integer)
210 int ret = ckmc_param_list_add_integer(params.get(), name, integer);
211 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
212 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
215 struct EncryptionResult
217 RawBufferPtr encrypted;
223 EncryptionResult encrypt(const AlgoBasePtr& algo,
224 const RawBufferPtr& plain,
225 const char* pass = nullptr)
227 EncryptionResult ret;
228 ckmc_raw_buffer_s* encrypted = nullptr;
229 KeyAliasPair aliases = algo->keyGen(pass);
231 ret.params = createParamListPtr();
232 assert_positive(ckmc_generate_params, algo->m_type, ret.params.get());
233 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
235 assert_positive(ckmc_encrypt_data,
242 ret.encrypted = create_raw_buffer(encrypted);
243 ret.prvKey = aliases.prv;
244 ret.pubKey = aliases.pub;
248 void testAllAlgorithms(
249 const std::function<void(const AlgoBasePtr& algo)>& test)
251 for(const auto& it : g_algorithms)
255 void testNoIvEnc(Algorithm type)
257 const AlgoBasePtr& algo = g_algorithms.at(type);
260 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
261 ckmc_raw_buffer_s* encrypted = nullptr;
264 KeyAliasPair aliases = algo->keyGen();
266 // param list with algo type only
267 ParamListPtr params = createParamListPtr();
268 setParam(params, CKMC_PARAM_ALGO_TYPE, algo->m_type);
269 assert_invalid_param(ckmc_encrypt_data,
277 void testNoIvDec(Algorithm type)
279 const AlgoBasePtr& algo = g_algorithms.at(type);
282 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
283 ckmc_raw_buffer_s* decrypted = nullptr;
286 auto ret = encrypt(algo, plain);
288 // param list with algo type only
289 ParamListPtr params = createParamListPtr();
290 setParam(params, CKMC_PARAM_ALGO_TYPE, algo->m_type);
291 assert_invalid_param(ckmc_decrypt_data,
295 *ret.encrypted.get(),
299 void testInvalidIvEnc(Algorithm type)
301 const AlgoBasePtr& algo = g_algorithms.at(type);
304 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
305 ckmc_raw_buffer_s* encryptedTmp = nullptr;
308 KeyAliasPair aliases = algo->keyGen();
311 ParamListPtr params = createParamListPtr();
312 assert_positive(ckmc_generate_params, algo->m_type, params.get());
314 // invalid encryption
316 assert_invalid_param(ckmc_encrypt_data,
322 ckmc_buffer_free(encryptedTmp);
323 encryptedTmp = nullptr;
326 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN-1));
328 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN+1));
332 void testInvalidIvDec(Algorithm type)
334 const AlgoBasePtr& algo = g_algorithms.at(type);
337 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
338 ckmc_raw_buffer_s* decrypted = nullptr;
341 auto ret = encrypt(algo, plain);
345 assert_invalid_param(ckmc_decrypt_data,
349 *ret.encrypted.get(),
351 ckmc_buffer_free(decrypted);
356 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN-1));
358 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN+1));
362 void encryptionWithCustomData(Algorithm type, ckmc_param_name_e name)
364 const AlgoBasePtr& algo = g_algorithms.at(type);
367 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
368 ckmc_raw_buffer_s* encrypted = nullptr;
369 ckmc_raw_buffer_s* decrypted = nullptr;
372 KeyAliasPair aliases = algo->keyGen();
375 ParamListPtr params = createParamListPtr();
376 assert_positive(ckmc_generate_params, algo->m_type, params.get());
377 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
380 setParam(params, name, createRandomBufferCAPI(64));
383 assert_positive(ckmc_encrypt_data,
389 RawBufferPtr tmpEnc = create_raw_buffer(encrypted);
392 assert_positive(ckmc_decrypt_data,
398 RawBufferPtr tmpDec = create_raw_buffer(decrypted);
401 assert_buffers_equal(*plain.get(), *tmpDec.get());
406 setParam(params, name, createRandomBufferCAPI(32));
409 assert_result(CKMC_ERROR_SERVER_ERROR,
418 void testGcmIvSize(size_t size, const KeyAliasPair& aliases, int error = CKMC_ERROR_NONE)
421 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
422 RawBufferPtr encrypted;
423 RawBufferPtr decrypted;
424 ckmc_raw_buffer_s* encryptedTmp = nullptr;
425 ckmc_raw_buffer_s* decryptedTmp = nullptr;
428 ParamListPtr params = createParamListPtr();
429 assert_positive(ckmc_generate_params, CKMC_ALGO_AES_GCM, params.get());
430 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
431 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(size));
442 if(error != CKMC_ERROR_NONE)
444 encrypted = create_raw_buffer(encryptedTmp);
447 assert_positive(ckmc_decrypt_data,
453 decrypted = create_raw_buffer(decryptedTmp);
455 assert_buffers_equal(*plain.get(), *decrypted.get());
458 void testIntegrity(Algorithm type)
460 const AlgoBasePtr& algo = g_algorithms.at(type);
463 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
464 ckmc_raw_buffer_s* decrypted = nullptr;
467 auto ret = encrypt(algo, plain);
469 // break the encrypted data
470 ret.encrypted->data[BUF_LEN/2]++;
472 // no data integrity check
473 assert_positive(ckmc_decrypt_data,
477 *ret.encrypted.get(),
480 RawBufferPtr tmp = create_raw_buffer(decrypted);
481 assert_buffers_equal(*plain.get(), *decrypted, false);
484 void testCtrEncryptionInvalidLength(Algorithm type)
486 const AlgoBasePtr& algo = g_algorithms.at(type);
489 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
490 ckmc_raw_buffer_s* encryptedTmp = nullptr;
493 KeyAliasPair aliases = algo->keyGen();
496 ParamListPtr params = createParamListPtr();
497 assert_positive(ckmc_generate_params, algo->m_type, params.get());
498 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
502 assert_invalid_param(ckmc_encrypt_data,
508 ckmc_buffer_free(encryptedTmp);
509 encryptedTmp = nullptr;
511 // invalid counter size
512 setParam(params, CKMC_PARAM_ED_CTR_LEN, -1);
514 setParam(params, CKMC_PARAM_ED_CTR_LEN, 0);
516 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
520 void testCtrEncryptionValidLength(Algorithm type)
522 const AlgoBasePtr& algo = g_algorithms.at(type);
525 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
526 ckmc_raw_buffer_s* encryptedTmp = nullptr;
529 KeyAliasPair aliases = algo->keyGen();
532 ParamListPtr params = createParamListPtr();
533 assert_positive(ckmc_generate_params, algo->m_type, params.get());
534 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
538 assert_positive(ckmc_encrypt_data,
544 ckmc_buffer_free(encryptedTmp);
545 encryptedTmp = nullptr;
547 // valid counter sizez
548 setParam(params, CKMC_PARAM_ED_CTR_LEN, 1);
550 setParam(params, CKMC_PARAM_ED_CTR_LEN, 4);
552 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
554 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
558 void testCtrDecryptionInvalidLength(Algorithm type)
560 const AlgoBasePtr& algo = g_algorithms.at(type);
563 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
564 ckmc_raw_buffer_s* decrypted = nullptr;
566 // add AES CTR key & encrypt
567 auto ret = encrypt(algo, plain);
571 assert_invalid_param(ckmc_decrypt_data,
575 *ret.encrypted.get(),
577 ckmc_buffer_free(decrypted);
580 // invalid counter size
581 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, -1);
583 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0);
585 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
589 void testCtrDecryptionValidLength(Algorithm type)
591 const AlgoBasePtr& algo = g_algorithms.at(type);
594 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
595 ckmc_raw_buffer_s* decrypted = nullptr;
597 // add AES CTR key & encrypt
598 auto ret = encrypt(algo, plain);
602 assert_positive(ckmc_decrypt_data,
606 *ret.encrypted.get(),
608 ckmc_buffer_free(decrypted);
609 RawBufferPtr tmp = create_raw_buffer(decrypted);
610 assert_buffers_equal(*plain.get(), *decrypted);
612 // invalid counter size
613 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
615 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 4);
617 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
619 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
623 void testGcmEncryptionTagLen(Algorithm type)
625 const AlgoBasePtr& algo = g_algorithms.at(type);
628 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
629 ckmc_raw_buffer_s* encryptedTmp = nullptr;
632 KeyAliasPair aliases = algo->keyGen();
635 ParamListPtr params = createParamListPtr();
636 assert_positive(ckmc_generate_params, algo->m_type, params.get());
637 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
639 std::vector<TagTest> testData = {
640 // illegal tag lengths
641 { -1, CKMC_ERROR_INVALID_PARAMETER },
642 { 0, CKMC_ERROR_INVALID_PARAMETER },
643 { 16, CKMC_ERROR_INVALID_PARAMETER },
644 { 48, CKMC_ERROR_INVALID_PARAMETER },
645 { 72, CKMC_ERROR_INVALID_PARAMETER },
646 { 100, CKMC_ERROR_INVALID_PARAMETER },
647 { 108, CKMC_ERROR_INVALID_PARAMETER },
648 { 116, CKMC_ERROR_INVALID_PARAMETER },
649 { 124, CKMC_ERROR_INVALID_PARAMETER },
650 { 256, CKMC_ERROR_INVALID_PARAMETER },
652 { 32, CKMC_ERROR_NONE },
653 { 64, CKMC_ERROR_NONE },
654 { 96, CKMC_ERROR_NONE },
655 { 104, CKMC_ERROR_NONE },
656 { 112, CKMC_ERROR_NONE },
657 { 120, CKMC_ERROR_NONE },
658 { 128, CKMC_ERROR_NONE },
662 for(const auto& it : testData)
664 setParam(params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
665 assert_result(it.expected,
672 ckmc_buffer_free(encryptedTmp);
673 encryptedTmp = nullptr;
677 void testGcmDecryptionTagLen(Algorithm type)
679 const AlgoBasePtr& algo = g_algorithms.at(type);
682 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
683 ckmc_raw_buffer_s* decrypted = nullptr;
685 // add AES GCM key & encrypt
686 auto ret = encrypt(algo, plain);
688 std::vector<TagTest> testData = {
689 // illegal tag lengths
690 { -1, CKMC_ERROR_INVALID_PARAMETER },
691 { 0, CKMC_ERROR_INVALID_PARAMETER },
692 { 16, CKMC_ERROR_INVALID_PARAMETER },
693 { 48, CKMC_ERROR_INVALID_PARAMETER },
694 { 72, CKMC_ERROR_INVALID_PARAMETER },
695 { 100, CKMC_ERROR_INVALID_PARAMETER },
696 { 108, CKMC_ERROR_INVALID_PARAMETER },
697 { 116, CKMC_ERROR_INVALID_PARAMETER },
698 { 124, CKMC_ERROR_INVALID_PARAMETER },
699 { 256, CKMC_ERROR_INVALID_PARAMETER },
700 // legal tag lengths (EVP_CipherFinal fails but we can't get the error code)
701 { 32, CKMC_ERROR_SERVER_ERROR },
702 { 64, CKMC_ERROR_SERVER_ERROR },
703 { 96, CKMC_ERROR_SERVER_ERROR },
704 { 104, CKMC_ERROR_SERVER_ERROR },
705 { 112, CKMC_ERROR_SERVER_ERROR },
706 { 120, CKMC_ERROR_SERVER_ERROR },
707 // legal tag length that was actually used for encryption (default)
708 { 128, CKMC_ERROR_NONE },
712 for(const auto& it : testData)
714 setParam(ret.params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
715 assert_result(it.expected,
720 *ret.encrypted.get(),
722 ckmc_buffer_free(decrypted);
727 void testGcmWrongTag(Algorithm type)
729 const AlgoBasePtr& algo = g_algorithms.at(type);
732 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
733 ckmc_raw_buffer_s* decrypted = nullptr;
735 // encrypt with AES GCM
736 auto ret = encrypt(algo, plain);
738 // modify tag (last 16B of encrypted message)
739 ret.encrypted->data[ret.encrypted->size-1]++;
741 // EVP_CipherFinal fails but we can't get error code
742 assert_result(CKMC_ERROR_SERVER_ERROR,
747 *ret.encrypted.get(),
751 void testGcmDifferentIvSizes(Algorithm type)
753 const AlgoBasePtr& algo = g_algorithms.at(type);
756 KeyAliasPair aliases = algo->keyGen();
758 testGcmIvSize(11, aliases, CKMC_ERROR_SERVER_ERROR); // 12B is the smallest
759 testGcmIvSize(12, aliases);
760 testGcmIvSize(17, aliases);
761 testGcmIvSize(128, aliases);
764 void testEncryptDecryptBigData(Algorithm type)
766 const AlgoBasePtr& algo = g_algorithms.at(type);
769 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(5000000));
770 ckmc_raw_buffer_s* decrypted = nullptr;
773 auto ret = encrypt(algo, plain);
775 assert_positive(ckmc_decrypt_data,
779 *ret.encrypted.get(),
781 RawBufferPtr tmp = create_raw_buffer(decrypted);
783 assert_buffers_equal(*plain.get(), *decrypted);
786 void testEncryptDecryptDifferentKeys(Algorithm type, bool success)
788 const AlgoBasePtr& algo = g_algorithms.at(type);
790 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
791 ckmc_raw_buffer_s* decrypted = nullptr;
794 auto ret = encrypt(algo, plain);
797 KeyAliasPair differentKeys = algo->keyGen(nullptr, "_wrong");
801 // some algorithms don't verify key validity
802 assert_positive(ckmc_decrypt_data,
804 differentKeys.prv.c_str(),
806 *ret.encrypted.get(),
808 RawBufferPtr tmp = create_raw_buffer(decrypted);
810 assert_buffers_equal(*plain.get(), *decrypted, false);
812 // different key should not be accepted
813 assert_result(CKMC_ERROR_SERVER_ERROR,
816 differentKeys.prv.c_str(),
818 *ret.encrypted.get(),
822 // Cleanup before testing next algorithm. Ignore results because not all keys are present
823 ckmc_remove_alias(ret.prvKey.c_str());
824 ckmc_remove_alias(ret.pubKey.c_str());
825 ckmc_remove_alias(differentKeys.prv.c_str());
826 ckmc_remove_alias(differentKeys.pub.c_str());
829 void testRsaLongestData(Algorithm type, size_t dataSize)
831 const AlgoBasePtr& algo = g_algorithms.at(type);
833 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
834 ckmc_raw_buffer_s* decrypted = nullptr;
837 auto ret = encrypt(algo, plain);
839 assert_positive(ckmc_decrypt_data,
843 *ret.encrypted.get(),
845 RawBufferPtr tmp = create_raw_buffer(decrypted);
847 assert_buffers_equal(*plain.get(), *decrypted);
850 void testRsaDataTooLong(Algorithm type, size_t dataSize)
852 const AlgoBasePtr& algo = g_algorithms.at(type);
854 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
857 EncryptionResult ret;
858 ckmc_raw_buffer_s* encrypted = nullptr;
859 KeyAliasPair aliases = algo->keyGen();
861 ret.params = createParamListPtr();
862 assert_positive(ckmc_generate_params, algo->m_type, ret.params.get());
863 assert_result(CKMC_ERROR_SERVER_ERROR,
872 } // namespace anonymous
875 RUNNER_TEST_GROUP_INIT(CKM_ENCRYPTION_DECRYPTION);
877 /////////////////////////////////////////
878 // Generic encryption decryption tests
879 /////////////////////////////////////////
881 RUNNER_TEST(TED_0010_encrypt_invalid_param_list, EncEnv)
883 testAllAlgorithms([](const AlgoBasePtr& algo){
885 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
886 ckmc_raw_buffer_s* encrypted = nullptr;
889 KeyAliasPair aliases = algo->keyGen();
892 assert_invalid_param(ckmc_encrypt_data,
900 ParamListPtr params = createParamListPtr();
901 assert_invalid_param(ckmc_encrypt_data,
910 RUNNER_TEST(TED_0020_encrypt_missing_key, EncEnv)
912 testAllAlgorithms([](const AlgoBasePtr& algo){
914 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
915 ckmc_raw_buffer_s* encrypted = nullptr;
918 ParamListPtr params = createParamListPtr();
919 assert_positive(ckmc_generate_params, algo->m_type, params.get());
920 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
922 assert_result(CKMC_ERROR_DB_ALIAS_UNKNOWN,
925 "non-existing-key-alias",
932 RUNNER_TEST(TED_0030_encrypt_no_plain_text, EncEnv)
934 testAllAlgorithms([](const AlgoBasePtr& algo){
936 ckmc_raw_buffer_s plain = { nullptr, 0 };
937 ckmc_raw_buffer_s* encrypted = nullptr;
940 KeyAliasPair aliases = algo->keyGen();
943 ParamListPtr params = createParamListPtr();
944 assert_positive(ckmc_generate_params, algo->m_type, params.get());
945 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
947 assert_invalid_param(ckmc_encrypt_data,
956 RUNNER_TEST(TED_0040_encrypt_no_output_buffer, EncEnv)
958 testAllAlgorithms([](const AlgoBasePtr& algo){
960 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
961 ckmc_raw_buffer_s** encrypted = nullptr;
964 KeyAliasPair aliases = algo->keyGen();
967 ParamListPtr params = createParamListPtr();
968 assert_positive(ckmc_generate_params, algo->m_type, params.get());
969 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
971 assert_invalid_param(ckmc_encrypt_data,
980 RUNNER_TEST(TED_0110_decrypt_invalid_param_list, EncEnv)
982 testAllAlgorithms([](const AlgoBasePtr& algo){
984 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
985 ckmc_raw_buffer_s* decrypted = nullptr;
988 auto ret = encrypt(algo, plain);
991 assert_invalid_param(ckmc_decrypt_data,
995 *ret.encrypted.get(),
999 ParamListPtr params = createParamListPtr();
1000 assert_invalid_param(ckmc_decrypt_data,
1004 *ret.encrypted.get(),
1009 RUNNER_TEST(TED_0120_decrypt_missing_key, EncEnv)
1011 testAllAlgorithms([](const AlgoBasePtr& algo){
1013 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1014 ckmc_raw_buffer_s* decrypted = nullptr;
1017 auto ret = encrypt(algo, plain);
1020 assert_positive(ckmc_remove_alias, ret.prvKey.c_str());
1023 assert_result(CKMC_ERROR_DB_ALIAS_UNKNOWN,
1028 *ret.encrypted.get(),
1033 RUNNER_TEST(TED_0130_decrypt_no_encrypted_text, EncEnv)
1035 testAllAlgorithms([](const AlgoBasePtr& algo){
1037 ckmc_raw_buffer_s encrypted = { nullptr, 0 };
1038 ckmc_raw_buffer_s* decrypted = nullptr;
1041 KeyAliasPair aliases = algo->keyGen();
1044 ParamListPtr params = createParamListPtr();
1045 assert_positive(ckmc_generate_params, algo->m_type, params.get());
1046 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1048 assert_invalid_param(ckmc_decrypt_data,
1050 aliases.prv.c_str(),
1057 RUNNER_TEST(TED_0140_decrypt_no_output_buffer, EncEnv)
1059 testAllAlgorithms([](const AlgoBasePtr& algo){
1061 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1062 ckmc_raw_buffer_s** decrypted = nullptr;
1065 auto ret = encrypt(algo, plain);
1067 assert_invalid_param(ckmc_decrypt_data,
1071 *ret.encrypted.get(),
1076 RUNNER_TEST(TED_0200_encrypt_decrypt_different_keys, EncEnv)
1078 testEncryptDecryptDifferentKeys(AES_CBC_128, false);
1079 testEncryptDecryptDifferentKeys(AES_CBC_192, false);
1080 testEncryptDecryptDifferentKeys(AES_CBC_256, false);
1081 testEncryptDecryptDifferentKeys(AES_GCM_128, false);
1082 testEncryptDecryptDifferentKeys(AES_GCM_192, false);
1083 testEncryptDecryptDifferentKeys(AES_GCM_256, false);
1084 testEncryptDecryptDifferentKeys(AES_CTR_128, true);
1085 testEncryptDecryptDifferentKeys(AES_CTR_192, true);
1086 testEncryptDecryptDifferentKeys(AES_CTR_256, true);
1087 testEncryptDecryptDifferentKeys(AES_CFB_128, true);
1088 testEncryptDecryptDifferentKeys(AES_CFB_192, true);
1089 testEncryptDecryptDifferentKeys(AES_CFB_256, true);
1090 testEncryptDecryptDifferentKeys(RSA_OAEP_1024, false);
1091 testEncryptDecryptDifferentKeys(RSA_OAEP_2048, false);
1092 testEncryptDecryptDifferentKeys(RSA_OAEP_4096, false);
1095 RUNNER_TEST(TED_0210_encrypt_decrypt_different_params, EncEnv)
1097 testAllAlgorithms([](const AlgoBasePtr& algo){
1099 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1100 ckmc_raw_buffer_s* decrypted = nullptr;
1103 auto ret = encrypt(algo, plain);
1105 // setup different params
1106 ParamListPtr params = createParamListPtr();
1107 assert_positive(ckmc_generate_params, CKMC_ALGO_RSA_GEN, params.get());
1108 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1110 assert_invalid_param(ckmc_decrypt_data,
1114 *ret.encrypted.get(),
1119 RUNNER_TEST(TED_0300_encrypt_decrypt, EncEnv)
1121 testAllAlgorithms([](const AlgoBasePtr& algo){
1123 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1124 ckmc_raw_buffer_s* decrypted = nullptr;
1127 auto ret = encrypt(algo, plain);
1129 assert_positive(ckmc_decrypt_data,
1133 *ret.encrypted.get(),
1135 RawBufferPtr tmp = create_raw_buffer(decrypted);
1137 assert_buffers_equal(*plain.get(), *decrypted);
1141 RUNNER_TEST(TED_0310_encrypt_decrypt_password, EncEnv)
1143 testAllAlgorithms([](const AlgoBasePtr& algo){
1145 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1146 ckmc_raw_buffer_s* decrypted = nullptr;
1149 auto ret = encrypt(algo, plain, PASSWORD);
1152 assert_result(CKMC_ERROR_AUTHENTICATION_FAILED,
1157 *ret.encrypted.get(),
1161 assert_positive(ckmc_decrypt_data,
1165 *ret.encrypted.get(),
1167 RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
1169 assert_buffers_equal(*plain.get(), *decrypted);
1173 // long test split into smaller ones
1174 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_cbc_128, EncEnv)
1176 testEncryptDecryptBigData(AES_CBC_128);
1179 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_cbc_192, EncEnv)
1181 testEncryptDecryptBigData(AES_CBC_192);
1184 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_cbc_256, EncEnv)
1186 testEncryptDecryptBigData(AES_CBC_256);
1189 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_gcm_128, EncEnv)
1191 testEncryptDecryptBigData(AES_GCM_128);
1194 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_gcm_192, EncEnv)
1196 testEncryptDecryptBigData(AES_GCM_192);
1199 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_gcm_256, EncEnv)
1201 testEncryptDecryptBigData(AES_GCM_256);
1204 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_ctr_128, EncEnv)
1206 testEncryptDecryptBigData(AES_CTR_128);
1209 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_ctr_192, EncEnv)
1211 testEncryptDecryptBigData(AES_CTR_192);
1214 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_ctr_256, EncEnv)
1216 testEncryptDecryptBigData(AES_CTR_256);
1219 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_cfb_128, EncEnv)
1221 testEncryptDecryptBigData(AES_CFB_128);
1224 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_cfb_192, EncEnv)
1226 testEncryptDecryptBigData(AES_CFB_192);
1229 RUNNER_TEST(TED_0400_encrypt_decrypt_big_data_aes_cfb_256, EncEnv)
1231 testEncryptDecryptBigData(AES_CFB_256);
1234 /////////////////////////////////////////
1235 // Algorithm specific tests
1236 /////////////////////////////////////////
1238 RUNNER_TEST(TED_1005_no_iv_enc, EncEnv)
1240 testNoIvEnc(AES_CTR_128);
1241 testNoIvEnc(AES_CTR_192);
1242 testNoIvEnc(AES_CTR_256);
1243 testNoIvEnc(AES_CBC_128);
1244 testNoIvEnc(AES_CBC_192);
1245 testNoIvEnc(AES_CBC_256);
1246 testNoIvEnc(AES_CFB_128);
1247 testNoIvEnc(AES_CFB_192);
1248 testNoIvEnc(AES_CFB_256);
1249 testNoIvEnc(AES_GCM_128);
1250 testNoIvEnc(AES_GCM_192);
1251 testNoIvEnc(AES_GCM_256);
1254 RUNNER_TEST(TED_1010_invalid_iv_enc, EncEnv)
1256 testInvalidIvEnc(AES_CTR_128);
1257 testInvalidIvEnc(AES_CTR_192);
1258 testInvalidIvEnc(AES_CTR_256);
1259 testInvalidIvEnc(AES_CBC_128);
1260 testInvalidIvEnc(AES_CBC_192);
1261 testInvalidIvEnc(AES_CBC_256);
1262 testInvalidIvEnc(AES_CFB_128);
1263 testInvalidIvEnc(AES_CFB_192);
1264 testInvalidIvEnc(AES_CFB_256);
1267 RUNNER_TEST(TED_1015_no_iv_dec, EncEnv)
1269 testNoIvDec(AES_CTR_128);
1270 testNoIvDec(AES_CTR_192);
1271 testNoIvDec(AES_CTR_256);
1272 testNoIvDec(AES_CBC_128);
1273 testNoIvDec(AES_CBC_192);
1274 testNoIvDec(AES_CBC_256);
1275 testNoIvDec(AES_CFB_128);
1276 testNoIvDec(AES_CFB_192);
1277 testNoIvDec(AES_CFB_256);
1278 testNoIvDec(AES_GCM_128);
1279 testNoIvDec(AES_GCM_192);
1280 testNoIvDec(AES_GCM_256);
1283 RUNNER_TEST(TED_1020_invalid_iv_dec, EncEnv)
1285 testInvalidIvDec(AES_CTR_128);
1286 testInvalidIvDec(AES_CTR_192);
1287 testInvalidIvDec(AES_CTR_256);
1288 testInvalidIvDec(AES_CBC_128);
1289 testInvalidIvDec(AES_CBC_192);
1290 testInvalidIvDec(AES_CBC_256);
1291 testInvalidIvDec(AES_CFB_128);
1292 testInvalidIvDec(AES_CFB_192);
1293 testInvalidIvDec(AES_CFB_256);
1296 RUNNER_TEST(TED_1050_data_integrity, EncEnv)
1298 testIntegrity(AES_CTR_128);
1299 testIntegrity(AES_CTR_192);
1300 testIntegrity(AES_CTR_256);
1301 testIntegrity(AES_CBC_128);
1302 testIntegrity(AES_CBC_192);
1303 testIntegrity(AES_CBC_256);
1304 testIntegrity(AES_CFB_128);
1305 testIntegrity(AES_CFB_192);
1306 testIntegrity(AES_CFB_256);
1309 RUNNER_TEST(TED_1100_ctr_encryption_invalid_length, EncEnv)
1311 testCtrEncryptionInvalidLength(AES_CTR_128);
1312 testCtrEncryptionInvalidLength(AES_CTR_192);
1313 testCtrEncryptionInvalidLength(AES_CTR_256);
1316 RUNNER_TEST(TED_1105_ctr_encryption_valid_length, EncEnv)
1318 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1319 testCtrEncryptionValidLength(AES_CTR_128);
1320 testCtrEncryptionValidLength(AES_CTR_192);
1321 testCtrEncryptionValidLength(AES_CTR_256);
1324 RUNNER_TEST(TED_1110_ctr_decryption_invalid_length, EncEnv)
1326 testCtrDecryptionInvalidLength(AES_CTR_128);
1327 testCtrDecryptionInvalidLength(AES_CTR_192);
1328 testCtrDecryptionInvalidLength(AES_CTR_256);
1331 RUNNER_TEST(TED_1115_ctr_decryption_valid_length, EncEnv)
1333 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1334 testCtrDecryptionValidLength(AES_CTR_128);
1335 testCtrDecryptionValidLength(AES_CTR_192);
1336 testCtrDecryptionValidLength(AES_CTR_256);
1339 RUNNER_TEST(TED_1200_gcm_encryption_tag_len, EncEnv)
1341 testGcmEncryptionTagLen(AES_GCM_128);
1342 testGcmEncryptionTagLen(AES_GCM_192);
1343 testGcmEncryptionTagLen(AES_GCM_256);
1346 RUNNER_TEST(TED_1210_gcm_decryption_tag_len, EncEnv)
1348 testGcmDecryptionTagLen(AES_GCM_128);
1349 testGcmDecryptionTagLen(AES_GCM_192);
1350 testGcmDecryptionTagLen(AES_GCM_256);
1353 RUNNER_TEST(TED_1230_gcm_wrong_tag, EncEnv)
1355 testGcmWrongTag(AES_GCM_128);
1356 testGcmWrongTag(AES_GCM_192);
1357 testGcmWrongTag(AES_GCM_256);
1360 RUNNER_TEST(TED_1240_gcm_different_iv_sizes, EncEnv)
1362 testGcmDifferentIvSizes(AES_GCM_128);
1363 testGcmDifferentIvSizes(AES_GCM_192);
1364 testGcmDifferentIvSizes(AES_GCM_256);
1367 RUNNER_TEST(TED_1250_gcm_aad, EncEnv)
1369 encryptionWithCustomData(AES_GCM_128, CKMC_PARAM_ED_AAD);
1370 encryptionWithCustomData(AES_GCM_192, CKMC_PARAM_ED_AAD);
1371 encryptionWithCustomData(AES_GCM_256, CKMC_PARAM_ED_AAD);
1374 RUNNER_TEST(TED_1300_rsa_label, EncEnv)
1376 RUNNER_IGNORED_MSG("RSA-OAEP labels are not supported in openssl");
1377 encryptionWithCustomData(RSA_OAEP_1024, CKMC_PARAM_ED_LABEL);
1378 encryptionWithCustomData(RSA_OAEP_2048, CKMC_PARAM_ED_LABEL);
1379 encryptionWithCustomData(RSA_OAEP_4096, CKMC_PARAM_ED_LABEL);
1382 RUNNER_TEST(TED_1330_rsa_longest_data, EncEnv)
1384 testRsaLongestData(RSA_OAEP_1024, 86);
1385 testRsaLongestData(RSA_OAEP_2048, 214);
1386 testRsaLongestData(RSA_OAEP_4096, 470);
1389 RUNNER_TEST(TED_1350_rsa_data_too_long, EncEnv)
1391 testRsaDataTooLong(RSA_OAEP_1024, 87);
1392 testRsaDataTooLong(RSA_OAEP_2048, 215);
1393 testRsaDataTooLong(RSA_OAEP_4096, 471);