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>
33 #include <encryption-decryption-env.h>
39 const char* PASSWORD = "test-password";
40 const uid_t UID = 5555;
41 const gid_t GID = 5555;
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(const ckmc_param_list_s *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 EncryptionError apiDecrypt(const ckmc_param_list_s *params,
62 const char *key_alias,
64 const ckmc_raw_buffer_s encrypted,
65 ckmc_raw_buffer_s **ppdecrypted) {
66 RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
67 return g_api->decrypt(params, key_alias, password, encrypted, ppdecrypted);
70 template <typename F, typename... Args>
71 void assert_crypto_result(EncryptionError expected, F&& func, Args... args)
73 EncryptionError ret = func(args...);
74 RUNNER_ASSERT_MSG(ret == expected,
75 "Expected: " << static_cast<int>(expected) <<
76 " got: " << static_cast<int>(ret));
79 template <typename F, typename... Args>
80 void assert_crypto_positive(F&& func, Args... args)
82 assert_crypto_result(EncryptionError::SUCCESS, std::move(func), args...);
85 template <typename F, typename... Args>
86 void assert_crypto_invalid_param(F&& func, Args... args)
88 assert_crypto_result(EncryptionError::INVALID_PARAM, std::move(func), args...);
93 EncryptionError expected;
102 class EncEnv : public RemoveDataEnv<UID> {
104 EncEnv() : m_dbu(NULL), m_sap(NULL) {}
105 ~EncEnv() { delete m_sap; delete m_dbu; }
107 void init(const std::string& str) {
108 RemoveDataEnv<UID>::init(str);
109 m_dbu = new ScopedDBUnlock(UID, "db-pass"); // unlock user's database
110 m_sap = new ScopedAccessProvider("my-label"); // setup label
112 // setup smack rules and switch user
113 m_sap->allowAPI("key-manager::api-storage", "rw");
114 m_sap->allowAPI("key-manager::api-encryption", "rw");
115 m_sap->applyAndSwithToUser(UID, GID);
123 RemoveDataEnv<UID>::finish();
127 ScopedDBUnlock* m_dbu;
128 ScopedAccessProvider* m_sap;
131 struct SyncEnv : public EncEnv {
132 void init(const std::string& str) {
137 static std::string suffix() { return "_sync"; }
140 struct AsyncEnv : public EncEnv {
141 void init(const std::string& str) {
146 static std::string suffix() { return "_async"; }
150 ckmc_algo_type_e m_type;
153 AlgoBase(ckmc_algo_type_e type, size_t keyLen) : m_type(type), m_keyLen(keyLen) {}
155 virtual KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr) = 0;
158 typedef std::shared_ptr<AlgoBase> AlgoBasePtr;
160 template <typename T>
161 AlgoBasePtr createAlgo(ckmc_algo_type_e type, size_t keyLen) {
162 return AlgoBasePtr(new T(type, keyLen));
165 struct AlgoAes : public AlgoBase {
166 AlgoAes(ckmc_algo_type_e type, size_t keyLen) : AlgoBase(type, keyLen) {}
167 KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr);
170 KeyAliasPair AlgoAes::keyGen(const char* pass, const char* suffix)
172 KeyAliasPair aliases;
173 std::ostringstream oss;
174 std::string ownerId = getOwnerIdFromSelf();
175 CharPtr passPtr(nullptr, free);
177 passPtr.reset(strdup(pass));
179 oss << "aes_" << static_cast<int>(m_type) << "_" << m_keyLen << "_key_alias";
182 aliases.prv = aliasWithLabel(ownerId.c_str(),oss.str().c_str());
183 aliases.pub = aliasWithLabel(ownerId.c_str(), oss.str().c_str());
185 ckmc_policy_s policy;
186 policy.extractable = false;
187 policy.password = passPtr.get();
189 assert_positive(ckmc_create_key_aes, m_keyLen, aliases.prv.c_str(), policy);
193 struct AlgoRsa : public AlgoBase {
194 AlgoRsa(ckmc_algo_type_e type, size_t keyLen) : AlgoBase(type, keyLen) {}
195 KeyAliasPair keyGen(const char* pass = nullptr, const char* suffix = nullptr);
198 KeyAliasPair AlgoRsa::keyGen(const char* pass, const char* suffix)
200 std::ostringstream oss_prv, oss_pub;
201 oss_prv << "rsa_oaep_prv_alias_" << m_keyLen;
202 oss_pub << "rsa_oaep_pub_alias_" << m_keyLen;
207 KeyAliasPair aliases = {
208 aliasWithLabel(getOwnerIdFromSelf().c_str(), oss_prv.str().c_str()),
209 aliasWithLabel(getOwnerIdFromSelf().c_str(), oss_pub.str().c_str())
211 CharPtr passPtr(nullptr, free);
213 passPtr.reset(strdup(pass));
215 ckmc_policy_s policyPrv;
216 policyPrv.password = passPtr.get();
217 policyPrv.extractable = 0;
219 ckmc_policy_s policyPub;
220 policyPub.password = passPtr.get();
221 policyPub.extractable = 0;
223 assert_positive(ckmc_create_key_pair_rsa,
250 std::map<Algorithm, AlgoBasePtr> g_algorithms = {
251 { AES_CBC_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 128) },
252 { AES_CBC_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 192) },
253 { AES_CBC_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CBC, 256) },
254 { AES_GCM_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 128) },
255 { AES_GCM_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 192) },
256 { AES_GCM_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_GCM, 256) },
257 { AES_CTR_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 128) },
258 { AES_CTR_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 192) },
259 { AES_CTR_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CTR, 256) },
260 { AES_CFB_128, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 128) },
261 { AES_CFB_192, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 192) },
262 { AES_CFB_256, createAlgo<AlgoAes>(CKMC_ALGO_AES_CFB, 256) },
263 { RSA_OAEP_1024, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 1024) },
264 { RSA_OAEP_2048, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 2048) },
265 { RSA_OAEP_4096, createAlgo<AlgoRsa>(CKMC_ALGO_RSA_OAEP, 4096) },
268 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
270 int ret = ckmc_param_list_add_buffer(params.get(), name, buffer);
271 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
272 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
275 void setParam(ParamListPtr& params, ckmc_param_name_e name, int integer)
277 int ret = ckmc_param_list_add_integer(params.get(), name, integer);
278 RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
279 "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
282 struct EncryptionResult
284 RawBufferPtr encrypted;
290 EncryptionResult encrypt(const AlgoBasePtr& algo,
291 const RawBufferPtr& plain,
292 const char* pass = nullptr)
294 EncryptionResult ret;
295 ckmc_raw_buffer_s* encrypted = nullptr;
296 KeyAliasPair aliases = algo->keyGen(pass);
298 ret.params = createParamListPtr();
299 assert_positive(ckmc_generate_params, algo->m_type, ret.params.get());
300 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
302 assert_crypto_positive(apiEncrypt,
309 ret.encrypted = create_raw_buffer(encrypted);
310 ret.prvKey = aliases.prv;
311 ret.pubKey = aliases.pub;
315 void testAllAlgorithms(
316 const std::function<void(const AlgoBasePtr& algo)>& test)
318 for(const auto& it : g_algorithms)
322 void testNoIvEnc(Algorithm type)
324 const AlgoBasePtr& algo = g_algorithms.at(type);
327 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
328 ckmc_raw_buffer_s* encrypted = nullptr;
331 KeyAliasPair aliases = algo->keyGen();
333 // param list with algo type only
334 ParamListPtr params = createParamListPtr();
335 setParam(params, CKMC_PARAM_ALGO_TYPE, algo->m_type);
336 assert_crypto_invalid_param(apiEncrypt,
344 void testNoIvDec(Algorithm type)
346 const AlgoBasePtr& algo = g_algorithms.at(type);
349 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
350 ckmc_raw_buffer_s* decrypted = nullptr;
353 auto ret = encrypt(algo, plain);
355 // param list with algo type only
356 ParamListPtr params = createParamListPtr();
357 setParam(params, CKMC_PARAM_ALGO_TYPE, algo->m_type);
358 assert_crypto_invalid_param(apiDecrypt,
362 *ret.encrypted.get(),
366 void testInvalidIvEnc(Algorithm type)
368 const AlgoBasePtr& algo = g_algorithms.at(type);
371 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
372 ckmc_raw_buffer_s* encryptedTmp = nullptr;
375 KeyAliasPair aliases = algo->keyGen();
378 ParamListPtr params = createParamListPtr();
379 assert_positive(ckmc_generate_params, algo->m_type, params.get());
381 // invalid encryption
383 assert_crypto_invalid_param(apiEncrypt,
389 ckmc_buffer_free(encryptedTmp);
390 encryptedTmp = nullptr;
393 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN-1));
395 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN+1));
399 void testInvalidIvDec(Algorithm type)
401 const AlgoBasePtr& algo = g_algorithms.at(type);
404 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
405 ckmc_raw_buffer_s* decrypted = nullptr;
408 auto ret = encrypt(algo, plain);
412 assert_crypto_invalid_param(apiDecrypt,
416 *ret.encrypted.get(),
418 ckmc_buffer_free(decrypted);
423 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN-1));
425 setParam(ret.params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN+1));
429 void encryptionWithCustomData(Algorithm type, ckmc_param_name_e name)
431 const AlgoBasePtr& algo = g_algorithms.at(type);
434 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
435 ckmc_raw_buffer_s* encrypted = nullptr;
436 ckmc_raw_buffer_s* decrypted = nullptr;
439 KeyAliasPair aliases = algo->keyGen();
442 ParamListPtr params = createParamListPtr();
443 assert_positive(ckmc_generate_params, algo->m_type, params.get());
444 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
447 setParam(params, name, createRandomBufferCAPI(64));
450 assert_crypto_positive(apiEncrypt,
456 RawBufferPtr tmpEnc = create_raw_buffer(encrypted);
459 assert_crypto_positive(apiDecrypt,
465 RawBufferPtr tmpDec = create_raw_buffer(decrypted);
468 assert_buffers_equal(*plain.get(), *tmpDec.get());
473 setParam(params, name, createRandomBufferCAPI(32));
476 assert_crypto_result(EncryptionError::SERVER_ERROR,
485 void testGcmIvSize(size_t size,
486 const KeyAliasPair& aliases,
487 EncryptionError error = EncryptionError::SUCCESS)
490 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
491 RawBufferPtr encrypted;
492 RawBufferPtr decrypted;
493 ckmc_raw_buffer_s* encryptedTmp = nullptr;
494 ckmc_raw_buffer_s* decryptedTmp = nullptr;
497 ParamListPtr params = createParamListPtr();
498 assert_positive(ckmc_generate_params, CKMC_ALGO_AES_GCM, params.get());
499 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
500 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(size));
503 assert_crypto_result(error,
511 if(error != EncryptionError::SUCCESS)
513 encrypted = create_raw_buffer(encryptedTmp);
516 assert_crypto_positive(apiDecrypt,
522 decrypted = create_raw_buffer(decryptedTmp);
524 assert_buffers_equal(*plain.get(), *decrypted.get());
527 void testIntegrity(Algorithm type)
529 const AlgoBasePtr& algo = g_algorithms.at(type);
532 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
533 ckmc_raw_buffer_s* decrypted = nullptr;
536 auto ret = encrypt(algo, plain);
538 // break the encrypted data
539 ret.encrypted->data[BUF_LEN/2]++;
541 // no data integrity check
542 assert_crypto_positive(apiDecrypt,
546 *ret.encrypted.get(),
549 RawBufferPtr tmp = create_raw_buffer(decrypted);
550 assert_buffers_equal(*plain.get(), *decrypted, false);
553 void testCtrEncryptionInvalidLength(Algorithm type)
555 const AlgoBasePtr& algo = g_algorithms.at(type);
558 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
559 ckmc_raw_buffer_s* encryptedTmp = nullptr;
562 KeyAliasPair aliases = algo->keyGen();
565 ParamListPtr params = createParamListPtr();
566 assert_positive(ckmc_generate_params, algo->m_type, params.get());
567 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
571 assert_crypto_invalid_param(apiEncrypt,
577 ckmc_buffer_free(encryptedTmp);
578 encryptedTmp = nullptr;
580 // invalid counter size
581 setParam(params, CKMC_PARAM_ED_CTR_LEN, -1);
583 setParam(params, CKMC_PARAM_ED_CTR_LEN, 0);
585 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
589 void testCtrEncryptionValidLength(Algorithm type)
591 const AlgoBasePtr& algo = g_algorithms.at(type);
594 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
595 ckmc_raw_buffer_s* encryptedTmp = nullptr;
598 KeyAliasPair aliases = algo->keyGen();
601 ParamListPtr params = createParamListPtr();
602 assert_positive(ckmc_generate_params, algo->m_type, params.get());
603 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
607 assert_crypto_positive(apiEncrypt,
613 ckmc_buffer_free(encryptedTmp);
614 encryptedTmp = nullptr;
616 // valid counter sizez
617 setParam(params, CKMC_PARAM_ED_CTR_LEN, 1);
619 setParam(params, CKMC_PARAM_ED_CTR_LEN, 4);
621 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
623 setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
627 void testCtrDecryptionInvalidLength(Algorithm type)
629 const AlgoBasePtr& algo = g_algorithms.at(type);
632 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
633 ckmc_raw_buffer_s* decrypted = nullptr;
635 // add AES CTR key & encrypt
636 auto ret = encrypt(algo, plain);
640 assert_crypto_invalid_param(apiDecrypt,
644 *ret.encrypted.get(),
646 ckmc_buffer_free(decrypted);
649 // invalid counter size
650 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, -1);
652 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0);
654 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
658 void testCtrDecryptionValidLength(Algorithm type)
660 const AlgoBasePtr& algo = g_algorithms.at(type);
663 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
664 ckmc_raw_buffer_s* decrypted = nullptr;
666 // add AES CTR key & encrypt
667 auto ret = encrypt(algo, plain);
671 assert_crypto_positive(apiDecrypt,
675 *ret.encrypted.get(),
677 ckmc_buffer_free(decrypted);
678 RawBufferPtr tmp = create_raw_buffer(decrypted);
679 assert_buffers_equal(*plain.get(), *decrypted);
681 // invalid counter size
682 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
684 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 4);
686 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
688 setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
692 void testGcmEncryptionTagLen(Algorithm type)
694 const AlgoBasePtr& algo = g_algorithms.at(type);
697 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
698 ckmc_raw_buffer_s* encryptedTmp = nullptr;
701 KeyAliasPair aliases = algo->keyGen();
704 ParamListPtr params = createParamListPtr();
705 assert_positive(ckmc_generate_params, algo->m_type, params.get());
706 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
708 std::vector<TagTest> testData = {
709 // illegal tag lengths
710 { -1, EncryptionError::INVALID_PARAM },
711 { 0, EncryptionError::INVALID_PARAM },
712 { 16, EncryptionError::INVALID_PARAM },
713 { 48, EncryptionError::INVALID_PARAM },
714 { 72, EncryptionError::INVALID_PARAM },
715 { 100, EncryptionError::INVALID_PARAM },
716 { 108, EncryptionError::INVALID_PARAM },
717 { 116, EncryptionError::INVALID_PARAM },
718 { 124, EncryptionError::INVALID_PARAM },
719 { 256, EncryptionError::INVALID_PARAM },
721 { 32, EncryptionError::SUCCESS },
722 { 64, EncryptionError::SUCCESS },
723 { 96, EncryptionError::SUCCESS },
724 { 104, EncryptionError::SUCCESS },
725 { 112, EncryptionError::SUCCESS },
726 { 120, EncryptionError::SUCCESS },
727 { 128, EncryptionError::SUCCESS },
731 for(const auto& it : testData)
733 setParam(params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
734 assert_crypto_result(it.expected,
741 ckmc_buffer_free(encryptedTmp);
742 encryptedTmp = nullptr;
746 void testGcmDecryptionTagLen(Algorithm type)
748 const AlgoBasePtr& algo = g_algorithms.at(type);
751 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
752 ckmc_raw_buffer_s* decrypted = nullptr;
754 // add AES GCM key & encrypt
755 auto ret = encrypt(algo, plain);
757 std::vector<TagTest> testData = {
758 // illegal tag lengths
759 { -1, EncryptionError::INVALID_PARAM },
760 { 0, EncryptionError::INVALID_PARAM },
761 { 16, EncryptionError::INVALID_PARAM },
762 { 48, EncryptionError::INVALID_PARAM },
763 { 72, EncryptionError::INVALID_PARAM },
764 { 100, EncryptionError::INVALID_PARAM },
765 { 108, EncryptionError::INVALID_PARAM },
766 { 116, EncryptionError::INVALID_PARAM },
767 { 124, EncryptionError::INVALID_PARAM },
768 { 256, EncryptionError::INVALID_PARAM },
769 // legal tag lengths (EVP_CipherFinal fails but we can't get the error code)
770 { 32, EncryptionError::SERVER_ERROR },
771 { 64, EncryptionError::SERVER_ERROR },
772 { 96, EncryptionError::SERVER_ERROR },
773 { 104, EncryptionError::SERVER_ERROR },
774 { 112, EncryptionError::SERVER_ERROR },
775 { 120, EncryptionError::SERVER_ERROR },
776 // legal tag length that was actually used for encryption (default)
777 { 128, EncryptionError::SUCCESS },
781 for(const auto& it : testData)
783 setParam(ret.params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
784 assert_crypto_result(it.expected,
789 *ret.encrypted.get(),
791 ckmc_buffer_free(decrypted);
796 void testGcmWrongTag(Algorithm type)
798 const AlgoBasePtr& algo = g_algorithms.at(type);
801 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
802 ckmc_raw_buffer_s* decrypted = nullptr;
804 // encrypt with AES GCM
805 auto ret = encrypt(algo, plain);
807 // modify tag (last 16B of encrypted message)
808 ret.encrypted->data[ret.encrypted->size-1]++;
810 // EVP_CipherFinal fails but we can't get error code
811 assert_crypto_result(EncryptionError::SERVER_ERROR,
816 *ret.encrypted.get(),
820 void testGcmDifferentIvSizes(Algorithm type)
822 const AlgoBasePtr& algo = g_algorithms.at(type);
825 KeyAliasPair aliases = algo->keyGen();
827 testGcmIvSize(11, aliases, EncryptionError::SERVER_ERROR); // 12B is the smallest
828 testGcmIvSize(12, aliases);
829 testGcmIvSize(17, aliases);
830 testGcmIvSize(128, aliases);
833 void testEncryptDecryptBigData(Algorithm type)
835 const AlgoBasePtr& algo = g_algorithms.at(type);
838 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(5000000));
839 ckmc_raw_buffer_s* decrypted = nullptr;
842 auto ret = encrypt(algo, plain);
844 assert_positive(apiDecrypt,
848 *ret.encrypted.get(),
850 RawBufferPtr tmp = create_raw_buffer(decrypted);
852 assert_buffers_equal(*plain.get(), *decrypted);
855 void testEncryptDecryptDifferentKeys(Algorithm type, bool success)
857 const AlgoBasePtr& algo = g_algorithms.at(type);
859 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
860 ckmc_raw_buffer_s* decrypted = nullptr;
863 auto ret = encrypt(algo, plain);
866 KeyAliasPair differentKeys = algo->keyGen(nullptr, "_wrong");
870 // some algorithms don't verify key validity
871 assert_crypto_positive(apiDecrypt,
873 differentKeys.prv.c_str(),
875 *ret.encrypted.get(),
877 RawBufferPtr tmp = create_raw_buffer(decrypted);
879 assert_buffers_equal(*plain.get(), *decrypted, false);
881 // different key should not be accepted
882 assert_crypto_result(EncryptionError::SERVER_ERROR,
885 differentKeys.prv.c_str(),
887 *ret.encrypted.get(),
891 // Cleanup before testing next algorithm. Ignore results because not all keys are present
892 ckmc_remove_alias(ret.prvKey.c_str());
893 ckmc_remove_alias(ret.pubKey.c_str());
894 ckmc_remove_alias(differentKeys.prv.c_str());
895 ckmc_remove_alias(differentKeys.pub.c_str());
898 void testRsaLongestData(Algorithm type, size_t dataSize)
900 const AlgoBasePtr& algo = g_algorithms.at(type);
902 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
903 ckmc_raw_buffer_s* decrypted = nullptr;
906 auto ret = encrypt(algo, plain);
908 assert_crypto_positive(apiDecrypt,
912 *ret.encrypted.get(),
914 RawBufferPtr tmp = create_raw_buffer(decrypted);
916 assert_buffers_equal(*plain.get(), *decrypted);
919 void testRsaDataTooLong(Algorithm type, size_t dataSize)
921 const AlgoBasePtr& algo = g_algorithms.at(type);
923 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
926 EncryptionResult ret;
927 ckmc_raw_buffer_s* encrypted = nullptr;
928 KeyAliasPair aliases = algo->keyGen();
930 ret.params = createParamListPtr();
931 assert_positive(ckmc_generate_params, algo->m_type, ret.params.get());
932 assert_crypto_result(EncryptionError::SERVER_ERROR,
941 } // namespace anonymous
944 RUNNER_TEST_GROUP_INIT(CKM_ENCRYPTION_DECRYPTION);
946 /////////////////////////////////////////
947 // Generic encryption decryption tests
948 /////////////////////////////////////////
950 RUNNER_TEST_MULTIPLE(TED_0010_encrypt_invalid_param_list, SyncEnv, AsyncEnv)
952 testAllAlgorithms([](const AlgoBasePtr& algo){
954 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
955 ckmc_raw_buffer_s* encrypted = nullptr;
958 KeyAliasPair aliases = algo->keyGen();
961 assert_crypto_invalid_param(apiEncrypt,
969 ParamListPtr params = createParamListPtr();
970 assert_crypto_invalid_param(apiEncrypt,
979 RUNNER_TEST_MULTIPLE(TED_0020_encrypt_missing_key, SyncEnv, AsyncEnv)
981 testAllAlgorithms([](const AlgoBasePtr& algo){
983 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
984 ckmc_raw_buffer_s* encrypted = nullptr;
987 ParamListPtr params = createParamListPtr();
988 assert_positive(ckmc_generate_params, algo->m_type, params.get());
989 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
991 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
994 "non-existing-key-alias",
1001 RUNNER_TEST_MULTIPLE(TED_0030_encrypt_no_plain_text, SyncEnv, AsyncEnv)
1003 testAllAlgorithms([](const AlgoBasePtr& algo){
1005 ckmc_raw_buffer_s plain = { nullptr, 0 };
1006 ckmc_raw_buffer_s* encrypted = nullptr;
1009 KeyAliasPair aliases = algo->keyGen();
1012 ParamListPtr params = createParamListPtr();
1013 assert_positive(ckmc_generate_params, algo->m_type, params.get());
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 ParamListPtr params = createParamListPtr();
1037 assert_positive(ckmc_generate_params, algo->m_type, params.get());
1038 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1040 assert_crypto_invalid_param(apiEncrypt,
1042 aliases.pub.c_str(),
1049 RUNNER_TEST_MULTIPLE(TED_0110_decrypt_invalid_param_list, SyncEnv, AsyncEnv)
1051 testAllAlgorithms([](const AlgoBasePtr& algo){
1053 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1054 ckmc_raw_buffer_s* decrypted = nullptr;
1057 auto ret = encrypt(algo, plain);
1060 assert_crypto_invalid_param(apiDecrypt,
1064 *ret.encrypted.get(),
1068 ParamListPtr params = createParamListPtr();
1069 assert_crypto_invalid_param(apiDecrypt,
1073 *ret.encrypted.get(),
1078 RUNNER_TEST_MULTIPLE(TED_0120_decrypt_missing_key, SyncEnv, AsyncEnv)
1080 testAllAlgorithms([](const AlgoBasePtr& algo){
1082 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1083 ckmc_raw_buffer_s* decrypted = nullptr;
1086 auto ret = encrypt(algo, plain);
1089 assert_positive(ckmc_remove_alias, ret.prvKey.c_str());
1092 assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
1097 *ret.encrypted.get(),
1102 RUNNER_TEST_MULTIPLE(TED_0130_decrypt_no_encrypted_text, SyncEnv, AsyncEnv)
1104 testAllAlgorithms([](const AlgoBasePtr& algo){
1106 ckmc_raw_buffer_s encrypted = { nullptr, 0 };
1107 ckmc_raw_buffer_s* decrypted = nullptr;
1110 KeyAliasPair aliases = algo->keyGen();
1113 ParamListPtr params = createParamListPtr();
1114 assert_positive(ckmc_generate_params, algo->m_type, params.get());
1115 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1117 assert_crypto_invalid_param(apiDecrypt,
1119 aliases.prv.c_str(),
1126 RUNNER_TEST_MULTIPLE(TED_0140_decrypt_no_output_buffer, SyncEnv, AsyncEnv)
1128 testAllAlgorithms([](const AlgoBasePtr& algo){
1130 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1131 ckmc_raw_buffer_s** decrypted = nullptr;
1134 auto ret = encrypt(algo, plain);
1136 assert_crypto_invalid_param(apiDecrypt,
1140 *ret.encrypted.get(),
1145 RUNNER_TEST_MULTIPLE(TED_0200_encrypt_decrypt_different_keys, SyncEnv, AsyncEnv)
1147 testEncryptDecryptDifferentKeys(AES_CBC_128, false);
1148 testEncryptDecryptDifferentKeys(AES_CBC_192, false);
1149 testEncryptDecryptDifferentKeys(AES_CBC_256, false);
1150 testEncryptDecryptDifferentKeys(AES_GCM_128, false);
1151 testEncryptDecryptDifferentKeys(AES_GCM_192, false);
1152 testEncryptDecryptDifferentKeys(AES_GCM_256, false);
1153 testEncryptDecryptDifferentKeys(AES_CTR_128, true);
1154 testEncryptDecryptDifferentKeys(AES_CTR_192, true);
1155 testEncryptDecryptDifferentKeys(AES_CTR_256, true);
1156 testEncryptDecryptDifferentKeys(AES_CFB_128, true);
1157 testEncryptDecryptDifferentKeys(AES_CFB_192, true);
1158 testEncryptDecryptDifferentKeys(AES_CFB_256, true);
1159 testEncryptDecryptDifferentKeys(RSA_OAEP_1024, false);
1160 testEncryptDecryptDifferentKeys(RSA_OAEP_2048, false);
1161 testEncryptDecryptDifferentKeys(RSA_OAEP_4096, false);
1164 RUNNER_TEST_MULTIPLE(TED_0210_encrypt_decrypt_different_params, SyncEnv, AsyncEnv)
1166 testAllAlgorithms([](const AlgoBasePtr& algo){
1168 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1169 ckmc_raw_buffer_s* decrypted = nullptr;
1172 auto ret = encrypt(algo, plain);
1174 // setup different params
1175 ParamListPtr params = createParamListPtr();
1176 assert_positive(ckmc_generate_params, CKMC_ALGO_RSA_GEN, params.get());
1177 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1179 assert_crypto_invalid_param(apiDecrypt,
1183 *ret.encrypted.get(),
1188 RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, 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);
1198 assert_crypto_positive(apiDecrypt,
1202 *ret.encrypted.get(),
1204 RawBufferPtr tmp = create_raw_buffer(decrypted);
1206 assert_buffers_equal(*plain.get(), *decrypted);
1210 RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
1212 testAllAlgorithms([](const AlgoBasePtr& algo){
1214 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1215 ckmc_raw_buffer_s* decrypted = nullptr;
1218 auto ret = encrypt(algo, plain, PASSWORD);
1221 assert_crypto_result(EncryptionError::AUTH_FAILED,
1226 *ret.encrypted.get(),
1230 assert_crypto_positive(apiDecrypt,
1234 *ret.encrypted.get(),
1236 RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
1238 assert_buffers_equal(*plain.get(), *decrypted);
1242 // long test split into smaller ones
1243 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cbc_128, SyncEnv, AsyncEnv)
1245 testEncryptDecryptBigData(AES_CBC_128);
1248 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cbc_192, SyncEnv, AsyncEnv)
1250 testEncryptDecryptBigData(AES_CBC_192);
1253 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cbc_256, SyncEnv, AsyncEnv)
1255 testEncryptDecryptBigData(AES_CBC_256);
1258 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_gcm_128, SyncEnv, AsyncEnv)
1260 testEncryptDecryptBigData(AES_GCM_128);
1263 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_gcm_192, SyncEnv, AsyncEnv)
1265 testEncryptDecryptBigData(AES_GCM_192);
1268 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_gcm_256, SyncEnv, AsyncEnv)
1270 testEncryptDecryptBigData(AES_GCM_256);
1273 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_ctr_128, SyncEnv, AsyncEnv)
1275 testEncryptDecryptBigData(AES_CTR_128);
1278 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_ctr_192, SyncEnv, AsyncEnv)
1280 testEncryptDecryptBigData(AES_CTR_192);
1283 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_ctr_256, SyncEnv, AsyncEnv)
1285 testEncryptDecryptBigData(AES_CTR_256);
1288 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cfb_128, SyncEnv, AsyncEnv)
1290 testEncryptDecryptBigData(AES_CFB_128);
1293 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cfb_192, SyncEnv, AsyncEnv)
1295 testEncryptDecryptBigData(AES_CFB_192);
1298 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_aes_cfb_256, SyncEnv, AsyncEnv)
1300 testEncryptDecryptBigData(AES_CFB_256);
1303 /////////////////////////////////////////
1304 // Algorithm specific tests
1305 /////////////////////////////////////////
1307 RUNNER_TEST_MULTIPLE(TED_1005_no_iv_enc, SyncEnv, AsyncEnv)
1309 testNoIvEnc(AES_CTR_128);
1310 testNoIvEnc(AES_CTR_192);
1311 testNoIvEnc(AES_CTR_256);
1312 testNoIvEnc(AES_CBC_128);
1313 testNoIvEnc(AES_CBC_192);
1314 testNoIvEnc(AES_CBC_256);
1315 testNoIvEnc(AES_CFB_128);
1316 testNoIvEnc(AES_CFB_192);
1317 testNoIvEnc(AES_CFB_256);
1318 testNoIvEnc(AES_GCM_128);
1319 testNoIvEnc(AES_GCM_192);
1320 testNoIvEnc(AES_GCM_256);
1323 RUNNER_TEST_MULTIPLE(TED_1010_invalid_iv_enc, SyncEnv, AsyncEnv)
1325 testInvalidIvEnc(AES_CTR_128);
1326 testInvalidIvEnc(AES_CTR_192);
1327 testInvalidIvEnc(AES_CTR_256);
1328 testInvalidIvEnc(AES_CBC_128);
1329 testInvalidIvEnc(AES_CBC_192);
1330 testInvalidIvEnc(AES_CBC_256);
1331 testInvalidIvEnc(AES_CFB_128);
1332 testInvalidIvEnc(AES_CFB_192);
1333 testInvalidIvEnc(AES_CFB_256);
1336 RUNNER_TEST_MULTIPLE(TED_1015_no_iv_dec, SyncEnv, AsyncEnv)
1338 testNoIvDec(AES_CTR_128);
1339 testNoIvDec(AES_CTR_192);
1340 testNoIvDec(AES_CTR_256);
1341 testNoIvDec(AES_CBC_128);
1342 testNoIvDec(AES_CBC_192);
1343 testNoIvDec(AES_CBC_256);
1344 testNoIvDec(AES_CFB_128);
1345 testNoIvDec(AES_CFB_192);
1346 testNoIvDec(AES_CFB_256);
1347 testNoIvDec(AES_GCM_128);
1348 testNoIvDec(AES_GCM_192);
1349 testNoIvDec(AES_GCM_256);
1352 RUNNER_TEST_MULTIPLE(TED_1020_invalid_iv_dec, SyncEnv, AsyncEnv)
1354 testInvalidIvDec(AES_CTR_128);
1355 testInvalidIvDec(AES_CTR_192);
1356 testInvalidIvDec(AES_CTR_256);
1357 testInvalidIvDec(AES_CBC_128);
1358 testInvalidIvDec(AES_CBC_192);
1359 testInvalidIvDec(AES_CBC_256);
1360 testInvalidIvDec(AES_CFB_128);
1361 testInvalidIvDec(AES_CFB_192);
1362 testInvalidIvDec(AES_CFB_256);
1365 RUNNER_TEST_MULTIPLE(TED_1050_data_integrity, SyncEnv, AsyncEnv)
1367 testIntegrity(AES_CTR_128);
1368 testIntegrity(AES_CTR_192);
1369 testIntegrity(AES_CTR_256);
1370 testIntegrity(AES_CBC_128);
1371 testIntegrity(AES_CBC_192);
1372 testIntegrity(AES_CBC_256);
1373 testIntegrity(AES_CFB_128);
1374 testIntegrity(AES_CFB_192);
1375 testIntegrity(AES_CFB_256);
1378 RUNNER_TEST_MULTIPLE(TED_1100_ctr_encryption_invalid_length, SyncEnv, AsyncEnv)
1380 testCtrEncryptionInvalidLength(AES_CTR_128);
1381 testCtrEncryptionInvalidLength(AES_CTR_192);
1382 testCtrEncryptionInvalidLength(AES_CTR_256);
1385 RUNNER_TEST_MULTIPLE(TED_1105_ctr_encryption_valid_length, SyncEnv, AsyncEnv)
1387 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1388 testCtrEncryptionValidLength(AES_CTR_128);
1389 testCtrEncryptionValidLength(AES_CTR_192);
1390 testCtrEncryptionValidLength(AES_CTR_256);
1393 RUNNER_TEST_MULTIPLE(TED_1110_ctr_decryption_invalid_length, SyncEnv, AsyncEnv)
1395 testCtrDecryptionInvalidLength(AES_CTR_128);
1396 testCtrDecryptionInvalidLength(AES_CTR_192);
1397 testCtrDecryptionInvalidLength(AES_CTR_256);
1400 RUNNER_TEST_MULTIPLE(TED_1115_ctr_decryption_valid_length, SyncEnv, AsyncEnv)
1402 RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1403 testCtrDecryptionValidLength(AES_CTR_128);
1404 testCtrDecryptionValidLength(AES_CTR_192);
1405 testCtrDecryptionValidLength(AES_CTR_256);
1408 RUNNER_TEST_MULTIPLE(TED_1200_gcm_encryption_tag_len, SyncEnv, AsyncEnv)
1410 testGcmEncryptionTagLen(AES_GCM_128);
1411 testGcmEncryptionTagLen(AES_GCM_192);
1412 testGcmEncryptionTagLen(AES_GCM_256);
1415 RUNNER_TEST_MULTIPLE(TED_1210_gcm_decryption_tag_len, SyncEnv, AsyncEnv)
1417 testGcmDecryptionTagLen(AES_GCM_128);
1418 testGcmDecryptionTagLen(AES_GCM_192);
1419 testGcmDecryptionTagLen(AES_GCM_256);
1422 RUNNER_TEST_MULTIPLE(TED_1230_gcm_wrong_tag, SyncEnv, AsyncEnv)
1424 testGcmWrongTag(AES_GCM_128);
1425 testGcmWrongTag(AES_GCM_192);
1426 testGcmWrongTag(AES_GCM_256);
1429 RUNNER_TEST_MULTIPLE(TED_1240_gcm_different_iv_sizes, SyncEnv, AsyncEnv)
1431 testGcmDifferentIvSizes(AES_GCM_128);
1432 testGcmDifferentIvSizes(AES_GCM_192);
1433 testGcmDifferentIvSizes(AES_GCM_256);
1436 RUNNER_TEST_MULTIPLE(TED_1250_gcm_aad, SyncEnv, AsyncEnv)
1438 encryptionWithCustomData(AES_GCM_128, CKMC_PARAM_ED_AAD);
1439 encryptionWithCustomData(AES_GCM_192, CKMC_PARAM_ED_AAD);
1440 encryptionWithCustomData(AES_GCM_256, CKMC_PARAM_ED_AAD);
1443 RUNNER_TEST_MULTIPLE(TED_1300_rsa_label, SyncEnv, AsyncEnv)
1445 RUNNER_IGNORED_MSG("RSA-OAEP labels are not supported in openssl");
1446 encryptionWithCustomData(RSA_OAEP_1024, CKMC_PARAM_ED_LABEL);
1447 encryptionWithCustomData(RSA_OAEP_2048, CKMC_PARAM_ED_LABEL);
1448 encryptionWithCustomData(RSA_OAEP_4096, CKMC_PARAM_ED_LABEL);
1451 RUNNER_TEST_MULTIPLE(TED_1330_rsa_longest_data, SyncEnv, AsyncEnv)
1453 testRsaLongestData(RSA_OAEP_1024, 86);
1454 testRsaLongestData(RSA_OAEP_2048, 214);
1455 testRsaLongestData(RSA_OAEP_4096, 470);
1458 RUNNER_TEST_MULTIPLE(TED_1350_rsa_data_too_long, SyncEnv, AsyncEnv)
1460 testRsaDataTooLong(RSA_OAEP_1024, 87);
1461 testRsaDataTooLong(RSA_OAEP_2048, 215);
1462 testRsaDataTooLong(RSA_OAEP_4096, 471);
1465 /////////////////////////////////////////
1466 // Asynchronous only tests
1467 /////////////////////////////////////////
1468 RUNNER_TEST(TED_2000_enc_no_observer_async, EncEnv)
1470 testAllAlgorithms([](const AlgoBasePtr& algo){
1472 RawBuffer plain = createRandomBuffer(BUF_LEN);
1475 KeyAliasPair aliases = algo->keyGen(nullptr);
1478 ParamListPtr params = createParamListPtr();
1479 assert_positive(ckmc_generate_params, algo->m_type, params.get());
1480 setParam(params, CKMC_PARAM_ED_IV, createRandomBufferCAPI(DEFAULT_IV_LEN));
1483 test_no_observer(&ManagerAsync::encrypt,
1484 *reinterpret_cast<CryptoAlgorithm*>(params.get()),
1491 RUNNER_TEST(TED_2010_dec_no_observer_async, AsyncEnv)
1493 testAllAlgorithms([](const AlgoBasePtr& algo){
1495 RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
1498 auto ret = encrypt(algo, plain);
1499 RawBuffer encrypted(ret.encrypted->data, ret.encrypted->data + ret.encrypted->size);
1502 test_no_observer(&ManagerAsync::decrypt,
1503 *reinterpret_cast<CryptoAlgorithm*>(ret.params.get()),