CKM: Make encryption tests runnable on both backends
[platform/core/test/security-tests.git] / src / ckm / unprivileged / encryption-decryption.cpp
1 /*
2  *  Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       encryption-decryption.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22
23 #include <string>
24 #include <vector>
25 #include <unordered_map>
26
27 #include <dpl/test/test_runner.h>
28 #include <ckm-common.h>
29 #include <ckmc/ckmc-manager.h>
30 #include <ckmc/ckmc-control.h>
31 #include <ckm/ckm-type.h>
32 #include <ckm/ckm-manager.h>
33 #include <encryption-decryption-env.h>
34
35 using namespace CKM;
36
37 namespace {
38
39 const char* PASSWORD = "test-password";
40 const uid_t UID = 5001;
41 const size_t CTR_DEFAULT_LEN = 16*8;
42 const size_t DEFAULT_IV_LEN = 16;
43 const size_t BUF_LEN = 86; // must be less than 1024/8-41 to support RSA OAEP 1024
44
45 // Environment
46 SyncApi g_syncApi;
47 AsyncApi g_asyncApi;
48
49 EncryptionApi* g_api = &g_syncApi;
50
51 EncryptionError apiEncrypt(ckmc_param_list_h params,
52                         const char *key_alias,
53                         const char *password,
54                         const ckmc_raw_buffer_s decrypted,
55                         ckmc_raw_buffer_s **ppencrypted) {
56     RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
57     return g_api->encrypt(params, key_alias, password, decrypted, ppencrypted);
58 }
59
60 inline CKM::Password _tostring(const char *str)
61 {
62     return (str == nullptr) ? Password() : Password(str);
63 }
64
65 inline CKM::Policy _toCkmPolicy(const ckmc_policy_s &policy, PolicyBackend backend)
66 {
67     return CKM::Policy(_tostring(policy.password), policy.extractable, backend);
68 }
69
70 EncryptionError apiDecrypt(ckmc_param_list_h params,
71                         const char *key_alias,
72                         const char *password,
73                         const ckmc_raw_buffer_s encrypted,
74                         ckmc_raw_buffer_s **ppdecrypted) {
75     RUNNER_ASSERT_MSG(g_api, "No encryption API is connected");
76     return g_api->decrypt(params, key_alias, password, encrypted, ppdecrypted);
77 }
78
79
80 template <typename F, typename... Args>
81 void assert_crypto_result(EncryptionError expected, F&& func, Args... args)
82 {
83     EncryptionError ret = func(args...);
84     RUNNER_ASSERT_MSG(ret == expected,
85                       "Expected: " << static_cast<int>(expected) <<
86                       " got: " << static_cast<int>(ret));
87 }
88
89 template <typename F, typename... Args>
90 void assert_crypto_positive(F&& func, Args... args)
91 {
92     assert_crypto_result(EncryptionError::SUCCESS, std::move(func), args...);
93 }
94
95 template <typename F, typename... Args>
96 void assert_crypto_invalid_param(F&& func, Args... args)
97 {
98     assert_crypto_result(EncryptionError::INVALID_PARAM, std::move(func), args...);
99 }
100
101 struct TagTest {
102     int tagLen;
103     EncryptionError expected;
104 };
105
106 struct KeyAliasPair
107 {
108     Alias prv;
109     Alias pub;
110 };
111
112 struct SyncEnv {
113     void init(const std::string&) {
114         g_api = &g_syncApi;
115     }
116
117     void finish() {
118         g_api = nullptr;
119     }
120
121     static std::string suffix() { return "_sync"; }
122 };
123
124 struct AsyncEnv {
125     void init(const std::string&) {
126         g_api = &g_asyncApi;
127     }
128
129     void finish() {
130         g_api = nullptr;
131     }
132
133     static std::string suffix() { return "_async"; }
134 };
135
136 struct Algo {
137     ckmc_algo_type_e type;
138     size_t keyLen;
139 };
140
141 std::unordered_map<size_t, std::vector<Alias>> g_symKeys;
142 std::unordered_map<size_t, std::vector<KeyAliasPair>> g_asymKeys;
143
144 enum KeyIdx {
145     PRIMARY = 0,
146     PASSWORD_PROTECTED = 1,
147
148     KEY_IDX_MAX
149 };
150
151 RawBufferPtr PLAIN_DATA;
152 RawBufferPtr BIG_DATA;
153 ckmc_raw_buffer_s* DEFAULT_IV;
154 ckmc_raw_buffer_s* IV11;
155 ckmc_raw_buffer_s* IV12;
156 ckmc_raw_buffer_s* IV15;
157 ckmc_raw_buffer_s* IV17;
158 ckmc_raw_buffer_s* IV128;
159 ckmc_raw_buffer_s* AAD32;
160 ckmc_raw_buffer_s* AAD64;
161
162 void generateSymmetricKeys(ManagerShPtr& manager, PolicyBackend backend, size_t bitLen)
163 {
164     for (int i = 0; i < KEY_IDX_MAX; i++)
165     {
166         Policy p(Password(), false, backend);
167         if (i == PASSWORD_PROTECTED)
168             p.password.assign(PASSWORD);
169
170         std::string alias = std::string("skey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
171         int ret = manager->createKeyAES(bitLen, alias, p);
172         if (ret != CKM_API_SUCCESS)
173             RUNNER_ERROR_MSG("AES key creation failed");
174
175         g_symKeys[bitLen].push_back(alias);
176     }
177 }
178
179 void generateRsaKeys(ManagerShPtr& manager, PolicyBackend backend, size_t bitLen)
180 {
181     for (int i = 0; i < KEY_IDX_MAX; i++)
182     {
183         Policy prvPolicy(Password(), false, backend);
184         Policy pubPolicy(Password(), true, backend);
185         if (i == PASSWORD_PROTECTED) {
186             prvPolicy.password.assign(PASSWORD);
187             pubPolicy.password.assign(PASSWORD);
188         }
189
190         KeyAliasPair alias;
191         alias.prv = std::string("akey_") + std::to_string(bitLen) + std::string("_") + std::to_string(i);
192         alias.pub = std::string("pub") + alias.prv;
193         int ret = manager->createKeyPairRSA(bitLen, alias.prv, alias.pub, prvPolicy, pubPolicy);
194         if (ret != CKM_API_SUCCESS)
195             RUNNER_ERROR_MSG("RSA key creation failed");
196
197         g_asymKeys[bitLen].push_back(alias);
198     }
199 }
200
201 KeyAliasPair getKey(const Algo& algo, KeyIdx idx)
202 {
203     if (algo.type == CKMC_ALGO_RSA_OAEP)
204         return g_asymKeys[algo.keyLen][idx];
205
206     KeyAliasPair pair;
207     pair.prv = g_symKeys[algo.keyLen][idx];
208     pair.pub = pair.prv;
209     return pair;
210 }
211
212 class EncGroupFixture: public DPL::Test::TestGroup
213 {
214 public:
215     void Init() override
216     {
217         remove_user_data(UID);
218         int ret = ckmc_unlock_user_key(UID, "db-pass");
219         if (ret != CKMC_ERROR_NONE)
220             RUNNER_ERROR_MSG("DB unlock failed: " << CKMCErrorToString(ret));
221
222         // Policy backend to use in subsequent operations (global for each test case)
223 #ifdef TZ_BACKEND
224         PolicyBackend backend = PolicyBackend::FORCE_HARDWARE;
225 #else
226         PolicyBackend backend = PolicyBackend::FORCE_SOFTWARE;
227 #endif
228
229         // generate keys
230         auto manager = Manager::create();
231         generateSymmetricKeys(manager, 128);
232         generateSymmetricKeys(manager, 192);
233         generateSymmetricKeys(manager, 256);
234         generateRsaKeys(manager, 1024);
235         generateRsaKeys(manager, 2048);
236         generateRsaKeys(manager, 4096);
237
238         PLAIN_DATA = create_raw_buffer(createRandomBufferCAPI(BUF_LEN));
239 #ifdef TZ_BACKEND
240         BIG_DATA = create_raw_buffer(createRandomBufferCAPI(100000));
241 #else
242         BIG_DATA = create_raw_buffer(createRandomBufferCAPI(5000000));
243 #endif
244         DEFAULT_IV = createRandomBufferCAPI(DEFAULT_IV_LEN);
245         IV11 = createRandomBufferCAPI(11);
246         IV12 = createRandomBufferCAPI(12);
247         IV15 = createRandomBufferCAPI(15);
248         IV17 = createRandomBufferCAPI(17);
249         IV128 = createRandomBufferCAPI(128);
250         AAD32 = createRandomBufferCAPI(32);
251         AAD64 = createRandomBufferCAPI(64);
252     }
253
254     void Finish() override
255     {
256         BIG_DATA.reset();
257         PLAIN_DATA.reset();
258         ckmc_buffer_free(AAD64);
259         ckmc_buffer_free(AAD32);
260         ckmc_buffer_free(IV128);
261         ckmc_buffer_free(IV17);
262         ckmc_buffer_free(IV15);
263         ckmc_buffer_free(IV12);
264         ckmc_buffer_free(IV11);
265         ckmc_buffer_free(DEFAULT_IV);
266
267         int ret = ckmc_lock_user_key(UID);
268         if (ret != CKMC_ERROR_NONE)
269             RUNNER_ERROR_MSG("DB lock failed: " << CKMCErrorToString(ret));
270         remove_user_data(UID);
271     }
272 };
273
274
275 void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
276 {
277     int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
278     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
279                       "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
280 }
281
282 void setParam(ParamListPtr& params, ckmc_param_name_e name, int integer)
283 {
284     int ret = ckmc_param_list_set_integer(params.get(), name, integer);
285     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
286                       "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
287 }
288
289 struct EncryptionResult
290 {
291     RawBufferPtr encrypted;
292     ParamListPtr params;
293     Alias prvKey;
294     Alias pubKey;
295 };
296
297 EncryptionResult encrypt(const Algo& algo,
298                          const RawBufferPtr& plain,
299                          const char* pass = nullptr)
300 {
301     EncryptionResult ret;
302     ckmc_raw_buffer_s* encrypted = nullptr;
303     KeyAliasPair aliases = getKey(algo, pass == nullptr ? PRIMARY : PASSWORD_PROTECTED);
304
305     ckmc_param_list_h handle = NULL;
306     assert_positive(ckmc_generate_new_params, algo.type, &handle);
307     ret.params = ParamListPtr(handle, ckmc_param_list_free);
308     setParam(ret.params, CKMC_PARAM_ED_IV, DEFAULT_IV);
309
310     assert_crypto_positive(apiEncrypt,
311                            ret.params.get(),
312                            aliases.pub.c_str(),
313                            pass,
314                            *plain.get(),
315                            &encrypted);
316
317     ret.encrypted = create_raw_buffer(encrypted);
318     ret.prvKey = aliases.prv;
319     ret.pubKey = aliases.pub;
320     return ret;
321 }
322
323 void testAllAlgorithms(
324         const std::function<void(const Algo& algo)>& test)
325 {
326     test( { CKMC_ALGO_AES_CBC, 128 });
327     test( { CKMC_ALGO_AES_CBC, 192 });
328     test( { CKMC_ALGO_AES_CBC, 256 });
329     test( { CKMC_ALGO_AES_GCM, 128 });
330     test( { CKMC_ALGO_AES_GCM, 192 });
331     test( { CKMC_ALGO_AES_GCM, 256 });
332     test( { CKMC_ALGO_AES_CTR, 128 });
333     test( { CKMC_ALGO_AES_CTR, 192 });
334     test( { CKMC_ALGO_AES_CTR, 256 });
335     test( { CKMC_ALGO_AES_CFB, 128 });
336     test( { CKMC_ALGO_AES_CFB, 192 });
337     test( { CKMC_ALGO_AES_CFB, 256 });
338     test( { CKMC_ALGO_RSA_OAEP, 1024 });
339     test( { CKMC_ALGO_RSA_OAEP, 2048 });
340     test( { CKMC_ALGO_RSA_OAEP, 4096 });
341 }
342
343 void testNoIvEnc(const Algo& algo)
344 {
345     // prepare buffers
346     ckmc_raw_buffer_s* encrypted = nullptr;
347
348     // add key
349     KeyAliasPair aliases = getKey(algo, PRIMARY);
350
351     // param list with algo type only
352     ParamListPtr params = createParamListPtr();
353     setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
354     assert_crypto_invalid_param(apiEncrypt,
355                                 params.get(),
356                                 aliases.pub.c_str(),
357                                 nullptr,
358                                 *PLAIN_DATA.get(),
359                                 &encrypted);
360 }
361
362 void testNoIvDec(const Algo& algo)
363 {
364     // prepare buffers
365     ckmc_raw_buffer_s* decrypted = nullptr;
366
367     // encrypt;
368     auto ret = encrypt(algo, PLAIN_DATA);
369
370     // param list with algo type only
371     ParamListPtr params = createParamListPtr();
372     setParam(params, CKMC_PARAM_ALGO_TYPE, algo.type);
373     assert_crypto_invalid_param(apiDecrypt,
374                                 params.get(),
375                                 ret.prvKey.c_str(),
376                                 nullptr,
377                                 *ret.encrypted.get(),
378                                 &decrypted);
379 }
380
381 void testInvalidIvEnc(const Algo& algo)
382 {
383     // prepare buffers
384     ckmc_raw_buffer_s* encryptedTmp = nullptr;
385
386     // add key
387     KeyAliasPair aliases = getKey(algo, PRIMARY);
388
389     // setup params
390     ckmc_param_list_h handle = NULL;
391     assert_positive(ckmc_generate_new_params, algo.type, &handle);
392     ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
393
394     // invalid encryption
395     auto test = [&](){
396         assert_crypto_invalid_param(apiEncrypt,
397                                     params.get(),
398                                     aliases.pub.c_str(),
399                                     nullptr,
400                                     *PLAIN_DATA.get(),
401                                     &encryptedTmp);
402         ckmc_buffer_free(encryptedTmp);
403         encryptedTmp = nullptr;
404     };
405     // invalid iv size
406     setParam(params, CKMC_PARAM_ED_IV, IV15);
407     test();
408     setParam(params, CKMC_PARAM_ED_IV, IV17);
409     test();
410 };
411
412 void testInvalidIvDec(const Algo& algo)
413 {
414     // prepare buffers
415     ckmc_raw_buffer_s* decrypted = nullptr;
416
417     // valid encryption
418     auto ret = encrypt(algo, PLAIN_DATA);
419
420     // decryption
421     auto test2 = [&](){
422         assert_crypto_invalid_param(apiDecrypt,
423                                     ret.params.get(),
424                                     ret.prvKey.c_str(),
425                                     nullptr,
426                                     *ret.encrypted.get(),
427                                     &decrypted);
428         ckmc_buffer_free(decrypted);
429         decrypted = nullptr;
430     };
431
432     // invalid iv size
433     setParam(ret.params, CKMC_PARAM_ED_IV, IV15);
434     test2();
435     setParam(ret.params, CKMC_PARAM_ED_IV, IV17);
436     test2();
437 };
438
439 void encryptionWithCustomData(const Algo& algo, ckmc_param_name_e name)
440 {
441     // prepare buffers
442     ckmc_raw_buffer_s* encrypted = nullptr;
443     ckmc_raw_buffer_s* decrypted = nullptr;
444
445     // add key
446     KeyAliasPair aliases = getKey(algo, PRIMARY);
447
448     // setup params
449     ckmc_param_list_h handle = NULL;
450     assert_positive(ckmc_generate_new_params, algo.type, &handle);
451     ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
452
453     setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
454
455     // set AAD
456     setParam(params, name, AAD64);
457
458     // encrypt
459     assert_crypto_positive(apiEncrypt,
460                            params.get(),
461                            aliases.pub.c_str(),
462                            nullptr,
463                            *PLAIN_DATA.get(),
464                            &encrypted);
465     RawBufferPtr tmpEnc = create_raw_buffer(encrypted);
466
467     // decrypt
468     assert_crypto_positive(apiDecrypt,
469                            params.get(),
470                            aliases.prv.c_str(),
471                            nullptr,
472                            *tmpEnc.get(),
473                            &decrypted);
474     RawBufferPtr tmpDec = create_raw_buffer(decrypted);
475
476     // check
477     assert_buffers_equal(*PLAIN_DATA.get(), *tmpDec.get());
478     tmpDec.reset();
479     decrypted = nullptr;
480
481     // set wrong AAD
482     setParam(params, name, AAD32);
483
484     // decrypt
485     assert_crypto_result(EncryptionError::INVALID_PARAM,
486                          apiDecrypt,
487                          params.get(),
488                          aliases.prv.c_str(),
489                          nullptr,
490                          *tmpEnc.get(),
491                          &decrypted);
492 }
493
494 void testGcmIvSize(ckmc_raw_buffer_s* iv,
495                    const KeyAliasPair& aliases,
496                    EncryptionError error = EncryptionError::SUCCESS)
497 {
498     // prepare buffers
499     RawBufferPtr encrypted;
500     RawBufferPtr decrypted;
501     ckmc_raw_buffer_s* encryptedTmp = nullptr;
502     ckmc_raw_buffer_s* decryptedTmp = nullptr;
503
504     // setup params
505     ckmc_param_list_h handle = NULL;
506     assert_positive(ckmc_generate_new_params, CKMC_ALGO_AES_GCM, &handle);
507     ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
508     setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
509     setParam(params, CKMC_PARAM_ED_IV, iv);
510
511     // encryption
512     assert_crypto_result(error,
513                          apiEncrypt,
514                          params.get(),
515                          aliases.pub.c_str(),
516                          nullptr,
517                          *PLAIN_DATA.get(),
518                          &encryptedTmp);
519
520     if(error != EncryptionError::SUCCESS)
521         return;
522     encrypted = create_raw_buffer(encryptedTmp);
523
524     // decryption
525     assert_crypto_positive(apiDecrypt,
526                            params.get(),
527                            aliases.prv.c_str(),
528                            nullptr,
529                            *encrypted.get(),
530                            &decryptedTmp);
531     decrypted = create_raw_buffer(decryptedTmp);
532
533     assert_buffers_equal(*PLAIN_DATA.get(), *decrypted.get());
534 }
535
536 void testIntegrity(const Algo& algo)
537 {
538     // prepare buffers
539     ckmc_raw_buffer_s* decrypted = nullptr;
540
541     // encrypt
542     auto ret = encrypt(algo, PLAIN_DATA);
543
544     // break the encrypted data
545     ret.encrypted->data[BUF_LEN/2]++;
546
547     // no data integrity check
548     assert_crypto_positive(apiDecrypt,
549                            ret.params.get(),
550                            ret.prvKey.c_str(),
551                            nullptr,
552                            *ret.encrypted.get(),
553                            &decrypted);
554
555     RawBufferPtr tmp = create_raw_buffer(decrypted);
556     assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
557 }
558
559 void testCtrEncryptionInvalidLength(const Algo& algo)
560 {
561     // prepare buffers
562     ckmc_raw_buffer_s* encryptedTmp = nullptr;
563
564     // add AES CTR key
565     KeyAliasPair aliases = getKey(algo, PRIMARY);
566
567     // setup params
568     ckmc_param_list_h handle = NULL;
569     assert_positive(ckmc_generate_new_params, algo.type, &handle);
570     ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
571     setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
572
573     // encryption
574     auto test = [&](){
575         assert_crypto_invalid_param(apiEncrypt,
576                                     params.get(),
577                                     aliases.pub.c_str(),
578                                     nullptr,
579                                     *PLAIN_DATA.get(),
580                                     &encryptedTmp);
581         ckmc_buffer_free(encryptedTmp);
582         encryptedTmp = nullptr;
583     };
584     // invalid counter size
585     setParam(params, CKMC_PARAM_ED_CTR_LEN, -1);
586     test();
587     setParam(params, CKMC_PARAM_ED_CTR_LEN, 0);
588     test();
589     setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
590     test();
591 }
592
593 void testCtrEncryptionValidLength(const Algo& algo)
594 {
595     // prepare buffers
596     ckmc_raw_buffer_s* encryptedTmp = nullptr;
597
598     // add AES CTR key
599     KeyAliasPair aliases = getKey(algo, PRIMARY);
600
601     // setup params
602     ckmc_param_list_h handle = NULL;
603     assert_positive(ckmc_generate_new_params, algo.type, &handle);
604     ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
605     setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
606
607     // encryption
608     auto test = [&](){
609         assert_crypto_positive(apiEncrypt,
610                                params.get(),
611                                aliases.pub.c_str(),
612                                nullptr,
613                                *PLAIN_DATA.get(),
614                                &encryptedTmp);
615         ckmc_buffer_free(encryptedTmp);
616         encryptedTmp = nullptr;
617     };
618     // valid counter sizez
619     setParam(params, CKMC_PARAM_ED_CTR_LEN, 1);
620     test();
621     setParam(params, CKMC_PARAM_ED_CTR_LEN, 4);
622     test();
623     setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
624     test();
625     setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
626     test();
627 }
628
629 void testCtrDecryptionInvalidLength(const Algo& algo)
630 {
631     // prepare buffers
632     ckmc_raw_buffer_s* decrypted = nullptr;
633
634     // add AES CTR key & encrypt
635     auto ret = encrypt(algo, PLAIN_DATA);
636
637     // decryption
638     auto test = [&](){
639         assert_crypto_invalid_param(apiDecrypt,
640                                     ret.params.get(),
641                                     ret.prvKey.c_str(),
642                                     nullptr,
643                                     *ret.encrypted.get(),
644                                     &decrypted);
645         ckmc_buffer_free(decrypted);
646         decrypted = nullptr;
647     };
648     // invalid counter size
649     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, -1);
650     test();
651     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0);
652     test();
653     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
654     test();
655 }
656
657 void testCtrDecryptionValidLength(const Algo& algo)
658 {
659     // prepare buffers
660     ckmc_raw_buffer_s* decrypted = nullptr;
661
662     // add AES CTR key & encrypt
663     auto ret = encrypt(algo, PLAIN_DATA);
664
665     // decryption
666     auto test = [&](){
667         assert_crypto_positive(apiDecrypt,
668                                ret.params.get(),
669                                ret.prvKey.c_str(),
670                                nullptr,
671                                *ret.encrypted.get(),
672                                &decrypted);
673         ckmc_buffer_free(decrypted);
674         RawBufferPtr tmp = create_raw_buffer(decrypted);
675         assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
676     };
677     // invalid counter size
678     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
679     test();
680     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 4);
681     test();
682     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN-1);
683     test();
684     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN);
685     test();
686 }
687
688 void testGcmEncryptionTagLen(const Algo& algo)
689 {
690     // prepare buffers
691     ckmc_raw_buffer_s* encryptedTmp = nullptr;
692
693     // add AES GCM key
694     KeyAliasPair aliases = getKey(algo, PRIMARY);
695
696     // setup params
697     ckmc_param_list_h handle = NULL;
698     assert_positive(ckmc_generate_new_params, algo.type, &handle);
699     ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
700     setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
701
702     std::vector<TagTest> testData = {
703             // illegal tag lengths
704             { -1,   EncryptionError::INVALID_PARAM },
705             { 0,    EncryptionError::INVALID_PARAM },
706             { 16,   EncryptionError::INVALID_PARAM },
707             { 48,   EncryptionError::INVALID_PARAM },
708             { 72,   EncryptionError::INVALID_PARAM },
709             { 100,  EncryptionError::INVALID_PARAM },
710             { 108,  EncryptionError::INVALID_PARAM },
711             { 116,  EncryptionError::INVALID_PARAM },
712             { 124,  EncryptionError::INVALID_PARAM },
713             { 256,  EncryptionError::INVALID_PARAM },
714             // legal tag lengths
715             { 32,   EncryptionError::SUCCESS },
716             { 64,   EncryptionError::SUCCESS },
717             { 96,   EncryptionError::SUCCESS },
718             { 104,  EncryptionError::SUCCESS },
719             { 112,  EncryptionError::SUCCESS },
720             { 120,  EncryptionError::SUCCESS },
721             { 128,  EncryptionError::SUCCESS },
722     };
723
724     // encryption
725     for(const auto& it : testData)
726     {
727         setParam(params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
728         assert_crypto_result(it.expected,
729                              apiEncrypt,
730                              params.get(),
731                              aliases.pub.c_str(),
732                              nullptr,
733                              *PLAIN_DATA.get(),
734                              &encryptedTmp);
735         ckmc_buffer_free(encryptedTmp);
736         encryptedTmp = nullptr;
737     }
738 }
739
740 void testGcmDecryptionTagLen(const Algo& algo)
741 {
742     // prepare buffers
743     ckmc_raw_buffer_s* decrypted = nullptr;
744
745     // add AES GCM key & encrypt
746     auto ret = encrypt(algo, PLAIN_DATA);
747
748     std::vector<TagTest> testData = {
749             // illegal tag lengths
750             { -1,   EncryptionError::INVALID_PARAM },
751             { 0,    EncryptionError::INVALID_PARAM },
752             { 16,   EncryptionError::INVALID_PARAM },
753             { 48,   EncryptionError::INVALID_PARAM },
754             { 72,   EncryptionError::INVALID_PARAM },
755             { 100,  EncryptionError::INVALID_PARAM },
756             { 108,  EncryptionError::INVALID_PARAM },
757             { 116,  EncryptionError::INVALID_PARAM },
758             { 124,  EncryptionError::INVALID_PARAM },
759             { 256,  EncryptionError::INVALID_PARAM },
760             // legal tag lengths but different than the one used for encryption
761             { 32,   EncryptionError::INVALID_PARAM },
762             { 64,   EncryptionError::INVALID_PARAM },
763             { 96,   EncryptionError::INVALID_PARAM },
764             { 104,  EncryptionError::INVALID_PARAM },
765             { 112,  EncryptionError::INVALID_PARAM },
766             { 120,  EncryptionError::INVALID_PARAM },
767             // legal tag length that was actually used for encryption (default)
768             { 128,  EncryptionError::SUCCESS },
769     };
770
771     // decryption
772     for(const auto& it : testData)
773     {
774         setParam(ret.params, CKMC_PARAM_ED_TAG_LEN, it.tagLen);
775         assert_crypto_result(it.expected,
776                              apiDecrypt,
777                              ret.params.get(),
778                              ret.prvKey.c_str(),
779                              nullptr,
780                              *ret.encrypted.get(),
781                              &decrypted);
782         ckmc_buffer_free(decrypted);
783         decrypted = nullptr;
784     }
785 }
786
787 void testGcmWrongTag(const Algo& algo)
788 {
789     // prepare buffers
790     ckmc_raw_buffer_s* decrypted = nullptr;
791
792     // encrypt with AES GCM
793     auto ret = encrypt(algo, PLAIN_DATA);
794
795     // modify tag (last 16B of encrypted message)
796     ret.encrypted->data[ret.encrypted->size-1]++;
797
798     // EVP_CipherFinal fails because of an authentication error
799     assert_crypto_result(EncryptionError::INVALID_PARAM,
800                          apiDecrypt,
801                          ret.params.get(),
802                          ret.prvKey.c_str(),
803                          nullptr,
804                          *ret.encrypted.get(),
805                          &decrypted);
806 }
807
808 void testGcmDifferentIvSizes(const Algo& algo)
809 {
810     // add AES GCM key
811     KeyAliasPair aliases = getKey(algo, PRIMARY);
812
813     testGcmIvSize(IV11,  aliases, EncryptionError::SERVER_ERROR); // 12B is the smallest
814     testGcmIvSize(IV12,  aliases);
815     testGcmIvSize(IV17,  aliases);
816     testGcmIvSize(IV128, aliases);
817 }
818
819 void testEncryptDecryptBigData(const Algo& algo)
820 {
821     // prepare buffers
822     ckmc_raw_buffer_s* decrypted = nullptr;
823
824     // encrypt
825     auto ret = encrypt(algo, BIG_DATA);
826
827     assert_positive(apiDecrypt,
828                     ret.params.get(),
829                     ret.prvKey.c_str(),
830                     nullptr,
831                     *ret.encrypted.get(),
832                     &decrypted);
833     RawBufferPtr tmp = create_raw_buffer(decrypted);
834
835     assert_buffers_equal(*BIG_DATA.get(), *decrypted);
836 }
837
838 void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
839 {
840     // prepare buffers
841     ckmc_raw_buffer_s* decrypted = nullptr;
842
843     // encrypt
844     auto ret = encrypt(algo, PLAIN_DATA);
845
846     // get different keys
847     KeyAliasPair differentKeys = getKey(algo, PASSWORD_PROTECTED);
848
849     if (success) {
850         // some algorithms don't verify key validity
851         assert_crypto_positive(apiDecrypt,
852                                ret.params.get(),
853                                differentKeys.prv.c_str(),
854                                PASSWORD,
855                                *ret.encrypted.get(),
856                                &decrypted);
857         RawBufferPtr tmp = create_raw_buffer(decrypted);
858
859         assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
860     } else {
861         assert_crypto_result(EncryptionError::INVALID_PARAM,
862                              apiDecrypt,
863                              ret.params.get(),
864                              differentKeys.prv.c_str(),
865                              PASSWORD,
866                              *ret.encrypted.get(),
867                              &decrypted);
868     }
869 }
870
871 void testRsaLongestData(const Algo& algo, size_t dataSize)
872 {
873     // prepare buffers
874     RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
875     ckmc_raw_buffer_s* decrypted = nullptr;
876
877     // encrypt
878     auto ret = encrypt(algo, plain);
879
880     assert_crypto_positive(apiDecrypt,
881                            ret.params.get(),
882                            ret.prvKey.c_str(),
883                            nullptr,
884                            *ret.encrypted.get(),
885                            &decrypted);
886     RawBufferPtr tmp = create_raw_buffer(decrypted);
887
888     assert_buffers_equal(*plain.get(), *decrypted);
889 }
890
891 void testRsaDataTooLong(const Algo& algo, size_t dataSize)
892 {
893     // prepare buffers
894     RawBufferPtr plain = create_raw_buffer(createRandomBufferCAPI(dataSize));
895
896     // encrypt
897     EncryptionResult ret;
898     ckmc_raw_buffer_s* encrypted = nullptr;
899     KeyAliasPair aliases = getKey(algo, PRIMARY);
900
901     ckmc_param_list_h handle = NULL;
902     assert_positive(ckmc_generate_new_params, algo.type, &handle);
903     ret.params = ParamListPtr(handle, ckmc_param_list_free);
904     assert_crypto_result(EncryptionError::INVALID_PARAM,
905                          apiEncrypt,
906                          ret.params.get(),
907                          aliases.pub.c_str(),
908                          nullptr,
909                          *plain.get(),
910                          &encrypted);
911 }
912
913 } // namespace anonymous
914
915 RUNNER_TEST_GROUP_INIT_ENV(CKM_ENCRYPTION_DECRYPTION, EncGroupFixture);
916
917 /////////////////////////////////////////
918 // Generic encryption decryption tests
919 /////////////////////////////////////////
920
921 RUNNER_TEST_MULTIPLE(TED_0010_encrypt_invalid_param_list, SyncEnv, AsyncEnv)
922 {
923     testAllAlgorithms([](const Algo& algo){
924         // prepare buffers
925         ckmc_raw_buffer_s* encrypted = nullptr;
926
927         // add key
928         KeyAliasPair aliases = getKey(algo, PRIMARY);
929
930         // null param list
931         assert_crypto_invalid_param(apiEncrypt,
932                                     nullptr,
933                                     aliases.pub.c_str(),
934                                     nullptr,
935                                     *PLAIN_DATA.get(),
936                                     &encrypted);
937
938         // empty param list
939         ParamListPtr params = createParamListPtr();
940         assert_crypto_invalid_param(apiEncrypt,
941                                     params.get(),
942                                     aliases.pub.c_str(),
943                                     nullptr,
944                                     *PLAIN_DATA.get(),
945                                     &encrypted);
946     });
947 }
948
949 RUNNER_TEST_MULTIPLE(TED_0020_encrypt_missing_key, SyncEnv, AsyncEnv)
950 {
951     testAllAlgorithms([](const Algo& algo){
952         // prepare buffers
953         ckmc_raw_buffer_s* encrypted = nullptr;
954
955         // setup params
956         ckmc_param_list_h handle = NULL;
957         assert_positive(ckmc_generate_new_params, algo.type, &handle);
958         ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
959         setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
960
961         assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
962                              apiEncrypt,
963                              params.get(),
964                              "non-existing-key-alias",
965                              nullptr,
966                              *PLAIN_DATA.get(),
967                              &encrypted);
968     });
969 }
970
971 RUNNER_TEST_MULTIPLE(TED_0030_encrypt_no_plain_text, SyncEnv, AsyncEnv)
972 {
973     testAllAlgorithms([](const Algo& algo){
974         // prepare buffers
975         ckmc_raw_buffer_s plain = { nullptr, 0 };
976         ckmc_raw_buffer_s* encrypted = nullptr;
977
978         // add key
979         KeyAliasPair aliases = getKey(algo, PRIMARY);
980
981         // setup params
982         ckmc_param_list_h handle = NULL;
983         assert_positive(ckmc_generate_new_params, algo.type, &handle);
984         ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
985         setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
986
987         assert_crypto_invalid_param(apiEncrypt,
988                                     params.get(),
989                                     aliases.pub.c_str(),
990                                     nullptr,
991                                     plain,
992                                     &encrypted);
993     });
994 }
995
996 RUNNER_TEST_MULTIPLE(TED_0040_encrypt_no_output_buffer, SyncEnv, AsyncEnv)
997 {
998     testAllAlgorithms([](const Algo& algo){
999         // prepare buffers
1000         ckmc_raw_buffer_s** encrypted = nullptr;
1001
1002         // add key
1003         KeyAliasPair aliases = getKey(algo, PRIMARY);
1004
1005         // setup params
1006         ckmc_param_list_h handle = NULL;
1007         assert_positive(ckmc_generate_new_params, algo.type, &handle);
1008         ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1009         setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1010
1011         assert_crypto_invalid_param(apiEncrypt,
1012                                     params.get(),
1013                                     aliases.pub.c_str(),
1014                                     nullptr,
1015                                     *PLAIN_DATA.get(),
1016                                     encrypted);
1017     });
1018 }
1019
1020 RUNNER_TEST_MULTIPLE(TED_0110_decrypt_invalid_param_list, SyncEnv, AsyncEnv)
1021 {
1022     testAllAlgorithms([](const Algo& algo){
1023         // prepare buffers
1024         ckmc_raw_buffer_s* decrypted = nullptr;
1025
1026         // encrypt;
1027         auto ret = encrypt(algo, PLAIN_DATA);
1028
1029         // null param list
1030         assert_crypto_invalid_param(apiDecrypt,
1031                                     nullptr,
1032                                     ret.prvKey.c_str(),
1033                                     nullptr,
1034                                     *ret.encrypted.get(),
1035                                     &decrypted);
1036
1037         // empty param list
1038         ParamListPtr params = createParamListPtr();
1039         assert_crypto_invalid_param(apiDecrypt,
1040                                     params.get(),
1041                                     ret.prvKey.c_str(),
1042                                     nullptr,
1043                                     *ret.encrypted.get(),
1044                                     &decrypted);
1045     });
1046 }
1047
1048 RUNNER_TEST_MULTIPLE(TED_0120_decrypt_missing_key, SyncEnv, AsyncEnv)
1049 {
1050     testAllAlgorithms([](const Algo& algo){
1051         // prepare buffers
1052         ckmc_raw_buffer_s* decrypted = nullptr;
1053
1054         // encrypt
1055         auto ret = encrypt(algo, PLAIN_DATA);
1056
1057         // try to decrypt
1058         assert_crypto_result(EncryptionError::ALIAS_UNKNOWN,
1059                              apiDecrypt,
1060                              ret.params.get(),
1061                              "non-existent-key",
1062                              nullptr,
1063                              *ret.encrypted.get(),
1064                              &decrypted);
1065     });
1066 }
1067
1068 RUNNER_TEST_MULTIPLE(TED_0130_decrypt_no_encrypted_text, SyncEnv, AsyncEnv)
1069 {
1070     testAllAlgorithms([](const Algo& algo){
1071         // prepare buffers
1072         ckmc_raw_buffer_s encrypted = { nullptr, 0 };
1073         ckmc_raw_buffer_s* decrypted = nullptr;
1074
1075         // add key
1076         KeyAliasPair aliases = getKey(algo, PRIMARY);
1077
1078         // setup params
1079         ckmc_param_list_h handle = NULL;
1080         assert_positive(ckmc_generate_new_params, algo.type, &handle);
1081         ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1082         setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1083
1084         assert_crypto_invalid_param(apiDecrypt,
1085                                     params.get(),
1086                                     aliases.prv.c_str(),
1087                                     nullptr,
1088                                     encrypted,
1089                                     &decrypted);
1090     });
1091 }
1092
1093 RUNNER_TEST_MULTIPLE(TED_0140_decrypt_no_output_buffer, SyncEnv, AsyncEnv)
1094 {
1095     testAllAlgorithms([](const Algo& algo){
1096         // prepare buffers
1097         ckmc_raw_buffer_s** decrypted = nullptr;
1098
1099         // encrypt
1100         auto ret = encrypt(algo, PLAIN_DATA);
1101
1102         assert_crypto_invalid_param(apiDecrypt,
1103                                     ret.params.get(),
1104                                     ret.prvKey.c_str(),
1105                                     nullptr,
1106                                     *ret.encrypted.get(),
1107                                     decrypted);
1108     });
1109 }
1110
1111 RUNNER_TEST_MULTIPLE(TED_0200_encrypt_decrypt_different_keys, SyncEnv, AsyncEnv)
1112 {
1113     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 128}, false);
1114     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 192}, false);
1115     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_GCM, 256}, false);
1116     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 128}, true);
1117     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 192}, true);
1118     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CTR, 256}, true);
1119     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 128}, true);
1120     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 192}, true);
1121     testEncryptDecryptDifferentKeys({CKMC_ALGO_AES_CFB, 256}, true);
1122     testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 1024}, false);
1123     testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 2048}, false);
1124     testEncryptDecryptDifferentKeys({CKMC_ALGO_RSA_OAEP, 4096}, false);
1125 }
1126
1127 RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, SyncEnv, AsyncEnv)
1128 {
1129     testAllAlgorithms([](const Algo& algo){
1130         // prepare buffers
1131         ckmc_raw_buffer_s* decrypted = nullptr;
1132
1133         // encrypt
1134         auto ret = encrypt(algo, PLAIN_DATA);
1135
1136         assert_crypto_positive(apiDecrypt,
1137                                ret.params.get(),
1138                                ret.prvKey.c_str(),
1139                                nullptr,
1140                                *ret.encrypted.get(),
1141                                &decrypted);
1142         RawBufferPtr tmp = create_raw_buffer(decrypted);
1143
1144         assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
1145     });
1146 }
1147
1148 RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
1149 {
1150     testAllAlgorithms([](const Algo& algo){
1151         // prepare buffers
1152         ckmc_raw_buffer_s* decrypted = nullptr;
1153
1154         // encrypt
1155         auto ret = encrypt(algo, PLAIN_DATA, PASSWORD);
1156
1157         // wrong password
1158         assert_crypto_result(EncryptionError::AUTH_FAILED,
1159                              apiDecrypt,
1160                              ret.params.get(),
1161                              ret.prvKey.c_str(),
1162                              "wrong-password",
1163                              *ret.encrypted.get(),
1164                              &decrypted);
1165
1166         // correct password
1167         assert_crypto_positive(apiDecrypt,
1168                                ret.params.get(),
1169                                ret.prvKey.c_str(),
1170                                PASSWORD,
1171                                *ret.encrypted.get(),
1172                                &decrypted);
1173         RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
1174
1175         assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
1176     });
1177 }
1178
1179 // long test split into smaller ones
1180 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_128, SyncEnv, AsyncEnv)
1181 {
1182     testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 128});
1183 }
1184
1185 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_192, SyncEnv, AsyncEnv)
1186 {
1187     testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 192});
1188 }
1189
1190 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CBC_256, SyncEnv, AsyncEnv)
1191 {
1192     testEncryptDecryptBigData({CKMC_ALGO_AES_CBC, 256});
1193 }
1194
1195 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_128, SyncEnv, AsyncEnv)
1196 {
1197     testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 128});
1198 }
1199
1200 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_192, SyncEnv, AsyncEnv)
1201 {
1202     testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 192});
1203 }
1204
1205 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_GCM_256, SyncEnv, AsyncEnv)
1206 {
1207     testEncryptDecryptBigData({CKMC_ALGO_AES_GCM, 256});
1208 }
1209
1210 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_128, SyncEnv, AsyncEnv)
1211 {
1212     testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 128});
1213 }
1214
1215 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_192, SyncEnv, AsyncEnv)
1216 {
1217     testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 192});
1218 }
1219
1220 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CTR_256, SyncEnv, AsyncEnv)
1221 {
1222     testEncryptDecryptBigData({CKMC_ALGO_AES_CTR, 256});
1223 }
1224
1225 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_128, SyncEnv, AsyncEnv)
1226 {
1227     testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 128});
1228 }
1229
1230 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_192, SyncEnv, AsyncEnv)
1231 {
1232     testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 192});
1233 }
1234
1235 RUNNER_TEST_MULTIPLE(TED_0400_encrypt_decrypt_big_data_AES_CFB_256, SyncEnv, AsyncEnv)
1236 {
1237     testEncryptDecryptBigData({CKMC_ALGO_AES_CFB, 256});
1238 }
1239
1240 /////////////////////////////////////////
1241 // Algorithm specific tests
1242 /////////////////////////////////////////
1243
1244 RUNNER_TEST_MULTIPLE(TED_1005_no_iv_enc, SyncEnv, AsyncEnv)
1245 {
1246     testNoIvEnc({CKMC_ALGO_AES_CTR, 128});
1247     testNoIvEnc({CKMC_ALGO_AES_CTR, 192});
1248     testNoIvEnc({CKMC_ALGO_AES_CTR, 256});
1249     testNoIvEnc({CKMC_ALGO_AES_CBC, 128});
1250     testNoIvEnc({CKMC_ALGO_AES_CBC, 192});
1251     testNoIvEnc({CKMC_ALGO_AES_CBC, 256});
1252     testNoIvEnc({CKMC_ALGO_AES_CFB, 128});
1253     testNoIvEnc({CKMC_ALGO_AES_CFB, 192});
1254     testNoIvEnc({CKMC_ALGO_AES_CFB, 256});
1255     testNoIvEnc({CKMC_ALGO_AES_GCM, 128});
1256     testNoIvEnc({CKMC_ALGO_AES_GCM, 192});
1257     testNoIvEnc({CKMC_ALGO_AES_GCM, 256});
1258 }
1259
1260 RUNNER_TEST_MULTIPLE(TED_1010_invalid_iv_enc, SyncEnv, AsyncEnv)
1261 {
1262     testInvalidIvEnc({CKMC_ALGO_AES_CTR, 128});
1263     testInvalidIvEnc({CKMC_ALGO_AES_CTR, 192});
1264     testInvalidIvEnc({CKMC_ALGO_AES_CTR, 256});
1265     testInvalidIvEnc({CKMC_ALGO_AES_CBC, 128});
1266     testInvalidIvEnc({CKMC_ALGO_AES_CBC, 192});
1267     testInvalidIvEnc({CKMC_ALGO_AES_CBC, 256});
1268     testInvalidIvEnc({CKMC_ALGO_AES_CFB, 128});
1269     testInvalidIvEnc({CKMC_ALGO_AES_CFB, 192});
1270     testInvalidIvEnc({CKMC_ALGO_AES_CFB, 256});
1271 }
1272
1273 RUNNER_TEST_MULTIPLE(TED_1015_no_iv_dec, SyncEnv, AsyncEnv)
1274 {
1275     testNoIvDec({CKMC_ALGO_AES_CTR, 128});
1276     testNoIvDec({CKMC_ALGO_AES_CTR, 192});
1277     testNoIvDec({CKMC_ALGO_AES_CTR, 256});
1278     testNoIvDec({CKMC_ALGO_AES_CBC, 128});
1279     testNoIvDec({CKMC_ALGO_AES_CBC, 192});
1280     testNoIvDec({CKMC_ALGO_AES_CBC, 256});
1281     testNoIvDec({CKMC_ALGO_AES_CFB, 128});
1282     testNoIvDec({CKMC_ALGO_AES_CFB, 192});
1283     testNoIvDec({CKMC_ALGO_AES_CFB, 256});
1284     testNoIvDec({CKMC_ALGO_AES_GCM, 128});
1285     testNoIvDec({CKMC_ALGO_AES_GCM, 192});
1286     testNoIvDec({CKMC_ALGO_AES_GCM, 256});
1287 }
1288
1289 RUNNER_TEST_MULTIPLE(TED_1020_invalid_iv_dec, SyncEnv, AsyncEnv)
1290 {
1291     testInvalidIvDec({CKMC_ALGO_AES_CTR, 128});
1292     testInvalidIvDec({CKMC_ALGO_AES_CTR, 192});
1293     testInvalidIvDec({CKMC_ALGO_AES_CTR, 256});
1294     testInvalidIvDec({CKMC_ALGO_AES_CBC, 128});
1295     testInvalidIvDec({CKMC_ALGO_AES_CBC, 192});
1296     testInvalidIvDec({CKMC_ALGO_AES_CBC, 256});
1297     testInvalidIvDec({CKMC_ALGO_AES_CFB, 128});
1298     testInvalidIvDec({CKMC_ALGO_AES_CFB, 192});
1299     testInvalidIvDec({CKMC_ALGO_AES_CFB, 256});
1300 }
1301
1302 RUNNER_TEST_MULTIPLE(TED_1050_data_integrity, SyncEnv, AsyncEnv)
1303 {
1304     testIntegrity({CKMC_ALGO_AES_CTR, 128});
1305     testIntegrity({CKMC_ALGO_AES_CTR, 192});
1306     testIntegrity({CKMC_ALGO_AES_CTR, 256});
1307     testIntegrity({CKMC_ALGO_AES_CBC, 128});
1308     testIntegrity({CKMC_ALGO_AES_CBC, 192});
1309     testIntegrity({CKMC_ALGO_AES_CBC, 256});
1310     testIntegrity({CKMC_ALGO_AES_CFB, 128});
1311     testIntegrity({CKMC_ALGO_AES_CFB, 192});
1312     testIntegrity({CKMC_ALGO_AES_CFB, 256});
1313 }
1314
1315 RUNNER_TEST_MULTIPLE(TED_1100_ctr_encryption_invalid_length, SyncEnv, AsyncEnv)
1316 {
1317     testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1318     testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1319     testCtrEncryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1320 }
1321
1322 RUNNER_TEST_MULTIPLE(TED_1105_ctr_encryption_valid_length, SyncEnv, AsyncEnv)
1323 {
1324     RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1325     testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1326     testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1327     testCtrEncryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1328 }
1329
1330 RUNNER_TEST_MULTIPLE(TED_1110_ctr_decryption_invalid_length, SyncEnv, AsyncEnv)
1331 {
1332     testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 128});
1333     testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 192});
1334     testCtrDecryptionInvalidLength({CKMC_ALGO_AES_CTR, 256});
1335 }
1336
1337 RUNNER_TEST_MULTIPLE(TED_1115_ctr_decryption_valid_length, SyncEnv, AsyncEnv)
1338 {
1339     RUNNER_IGNORED_MSG("Openssl supports only 128-bit AES CTR length");
1340     testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 128});
1341     testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 192});
1342     testCtrDecryptionValidLength({CKMC_ALGO_AES_CTR, 256});
1343 }
1344
1345 RUNNER_TEST_MULTIPLE(TED_1200_gcm_encryption_tag_len, SyncEnv, AsyncEnv)
1346 {
1347     testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1348     testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1349     testGcmEncryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1350 }
1351
1352 RUNNER_TEST_MULTIPLE(TED_1210_gcm_decryption_tag_len, SyncEnv, AsyncEnv)
1353 {
1354     testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 128});
1355     testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 192});
1356     testGcmDecryptionTagLen({CKMC_ALGO_AES_GCM, 256});
1357 }
1358
1359 RUNNER_TEST_MULTIPLE(TED_1230_gcm_wrong_tag, SyncEnv, AsyncEnv)
1360 {
1361     testGcmWrongTag({CKMC_ALGO_AES_GCM, 128});
1362     testGcmWrongTag({CKMC_ALGO_AES_GCM, 192});
1363     testGcmWrongTag({CKMC_ALGO_AES_GCM, 256});
1364 }
1365
1366 RUNNER_TEST_MULTIPLE(TED_1240_gcm_different_iv_sizes, SyncEnv, AsyncEnv)
1367 {
1368     testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 128});
1369     testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 192});
1370     testGcmDifferentIvSizes({CKMC_ALGO_AES_GCM, 256});
1371 }
1372
1373 RUNNER_TEST_MULTIPLE(TED_1250_gcm_aad, SyncEnv, AsyncEnv)
1374 {
1375     encryptionWithCustomData({CKMC_ALGO_AES_GCM, 128}, CKMC_PARAM_ED_AAD);
1376     encryptionWithCustomData({CKMC_ALGO_AES_GCM, 192}, CKMC_PARAM_ED_AAD);
1377     encryptionWithCustomData({CKMC_ALGO_AES_GCM, 256}, CKMC_PARAM_ED_AAD);
1378 }
1379
1380 RUNNER_TEST_MULTIPLE(TED_1300_rsa_label, SyncEnv, AsyncEnv)
1381 {
1382     RUNNER_IGNORED_MSG("RSA-OAEP labels are not supported in openssl");
1383     encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 1024}, CKMC_PARAM_ED_LABEL);
1384     encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 2048}, CKMC_PARAM_ED_LABEL);
1385     encryptionWithCustomData({CKMC_ALGO_RSA_OAEP, 4096}, CKMC_PARAM_ED_LABEL);
1386 }
1387
1388 RUNNER_TEST_MULTIPLE(TED_1330_rsa_longest_data, SyncEnv, AsyncEnv)
1389 {
1390     testRsaLongestData({CKMC_ALGO_RSA_OAEP, 1024}, 86);
1391     testRsaLongestData({CKMC_ALGO_RSA_OAEP, 2048}, 214);
1392     testRsaLongestData({CKMC_ALGO_RSA_OAEP, 4096}, 470);
1393 }
1394
1395 RUNNER_TEST_MULTIPLE(TED_1350_rsa_data_too_long, SyncEnv, AsyncEnv)
1396 {
1397     testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 1024}, 87);
1398     testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 2048}, 215);
1399     testRsaDataTooLong({CKMC_ALGO_RSA_OAEP, 4096}, 471);
1400 }
1401
1402 /////////////////////////////////////////
1403 // Asynchronous only tests
1404 /////////////////////////////////////////
1405 RUNNER_TEST(TED_2000_enc_no_observer_async, AsyncEnv)
1406 {
1407     testAllAlgorithms([](const Algo& algo){
1408         // prepare buffers
1409         RawBuffer plain = createRandomBuffer(BUF_LEN);
1410
1411         // keys
1412         KeyAliasPair aliases = getKey(algo, PRIMARY);
1413
1414         // params
1415         ckmc_param_list_h handle = NULL;
1416         assert_positive(ckmc_generate_new_params, algo.type, &handle);
1417         ParamListPtr params = ParamListPtr(handle, ckmc_param_list_free);
1418         setParam(params, CKMC_PARAM_ED_IV, DEFAULT_IV);
1419
1420         // encrypt
1421         test_no_observer(&ManagerAsync::encrypt,
1422                          *reinterpret_cast<CryptoAlgorithm*>(params.get()),
1423                          aliases.pub,
1424                          Password(),
1425                          plain);
1426     });
1427 }
1428
1429 RUNNER_TEST(TED_2010_dec_no_observer_async, AsyncEnv)
1430 {
1431     testAllAlgorithms([](const Algo& algo){
1432         // encrypt
1433         auto ret = encrypt(algo, PLAIN_DATA);
1434         RawBuffer encrypted(ret.encrypted->data, ret.encrypted->data + ret.encrypted->size);
1435
1436         // decrypt
1437         test_no_observer(&ManagerAsync::decrypt,
1438                          *reinterpret_cast<CryptoAlgorithm*>(ret.params.get()),
1439                          ret.prvKey,
1440                          Password(),
1441                          encrypted);
1442     });
1443 }