Fix for gcc 9 toochain upgrade
[platform/core/test/security-tests.git] / src / yaca / yaca-test-encrypt.cpp
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Dariusz Michaluk (d.michaluk@samsung.com)
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18
19 /**
20  * @file
21  * @author Dariusz Michaluk (d.michaluk@samsung.com)
22  * @author Mateusz Forc (m.forc@samsung.com)
23  */
24
25 #include "dpl/test/test_runner.h"
26 #include "yaca-test-common.h"
27 #include "yaca-test-vector.h"
28
29 #include <yaca_encrypt.h>
30 #include <yaca_key.h>
31
32 namespace {
33
34 const Buffer DATA = random_buffer(1024);
35 const size_t INVALID_IV_BIT_LENGTH = 512;
36
37 struct KeyIvPair {
38 public:
39     KeyPtr key;
40     KeyPtr iv;
41
42     KeyIvPair() : key(generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT)),
43                   iv(generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_128BIT))
44                   {}
45 };
46
47 yaca_key_type_e algo_to_key_type(yaca_encrypt_algorithm_e algo)
48 {
49     if (algo == YACA_ENCRYPT_3DES_3TDEA ||
50         algo == YACA_ENCRYPT_UNSAFE_3DES_2TDEA ||
51         algo == YACA_ENCRYPT_UNSAFE_DES)
52         return YACA_KEY_TYPE_DES;
53     else
54         return YACA_KEY_TYPE_SYMMETRIC;
55 }
56
57 class Encryptor {
58 public:
59     Encryptor(yaca_encrypt_algorithm_e algo,
60               yaca_block_cipher_mode_e bcm,
61               const KeyPtr& key_ptr,
62               const KeyPtr& iv_ptr,
63               size_t key_bits = 0,
64               bool padded = true) :
65         m_enCtxPtr(encrypt_init(algo, bcm, key_ptr, iv_ptr)),
66         m_decCtxPtr(decrypt_init(algo, bcm, key_ptr, iv_ptr))
67     {
68         if (!padded)
69             set_padding(YACA_PADDING_NONE);
70         if (key_bits > 0)
71             set_effective_key_bits(key_bits);
72     }
73
74     virtual Buffer encrypt(const Buffer& data, size_t repeats = 1)
75     {
76         Buffer update_out = run_update(data, repeats, &m_enCtxPtr, yaca_encrypt_update);
77         Buffer final_out  = run_finalize(&m_enCtxPtr, yaca_encrypt_finalize);
78         update_out.insert(update_out.end(), final_out.begin(), final_out.end());
79         return update_out;
80     }
81
82     virtual Buffer decrypt(const Buffer& data, size_t repeats = 1)
83     {
84         Buffer update_out = run_update(data, repeats, &m_decCtxPtr, yaca_decrypt_update);
85         Buffer final_out  = run_finalize(&m_decCtxPtr, yaca_decrypt_finalize);
86         update_out.insert(update_out.end(), final_out.begin(), final_out.end());
87         return update_out;
88     }
89
90     void set_padding(yaca_padding_e padding)
91     {
92         YACA_SUCCESS(yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_PADDING,
93                                                (char*)(&padding), sizeof(padding)));
94         YACA_SUCCESS(yaca_context_set_property(m_decCtxPtr.get(), YACA_PROPERTY_PADDING,
95                                                (char*)(&padding), sizeof(padding)));
96     }
97
98     void set_effective_key_bits(size_t key_bits)
99     {
100         YACA_SUCCESS(yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
101                                                (char*)(&key_bits), sizeof(key_bits)));
102         YACA_SUCCESS(yaca_context_set_property(m_decCtxPtr.get(), YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
103                                                (char*)(&key_bits), sizeof(key_bits)));
104     }
105
106     int set_tag_len(yaca_block_cipher_mode_e bcm, size_t tag_len)
107     {
108         switch (bcm) {
109         case YACA_BCM_GCM:
110             return yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_GCM_TAG_LEN,
111                                              (void*)&tag_len, sizeof(tag_len));
112         case YACA_BCM_CCM:
113             return yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_CCM_TAG_LEN,
114                                              (void*)&tag_len, sizeof(tag_len));
115         default:
116             return YACA_ERROR_INVALID_PARAMETER;
117         }
118     }
119
120 private:
121     CtxPtr m_enCtxPtr;
122     CtxPtr m_decCtxPtr;
123
124     typedef std::function<int(yaca_context_h, const char*, size_t, char*, size_t*)> Update;
125     typedef std::function<int(yaca_context_h, char*, size_t*)> Final;
126
127     Buffer run_update(const Buffer& data, size_t repeats, CtxPtr* ctx_ptr, Update update)
128     {
129         Buffer output;
130         if (!data.empty()) {
131             size_t len = 0;
132             auto text_ptr = out_buf_alloc(*ctx_ptr, data.size(), len);
133
134             for (size_t i = 0; i < repeats; ++i) {
135                 YACA_SUCCESS(update(ctx_ptr->get(), data.data(), data.size(), text_ptr.get(), &len));
136                 output.insert(output.end(), text_ptr.get(), text_ptr.get() + len);
137             }
138         }
139
140         return output;
141     }
142
143     Buffer run_finalize(CtxPtr* ctx_ptr, Final finalize)
144     {
145         Buffer output;
146         size_t len = 0;
147         auto final_ptr = out_buf_alloc(m_decCtxPtr, 0, len);
148         if (final_ptr != nullptr) {
149             YACA_SUCCESS(finalize(ctx_ptr->get(), final_ptr.get(), &len));
150             output.insert(output.end(), final_ptr.get(), final_ptr.get() + len);
151         }
152         return output;
153     }
154 };
155
156 std::string bin2hex(const Buffer &bin_str)
157 {
158     std::stringstream data;
159
160     for (const auto& i : bin_str)
161         data << std::hex << static_cast<int>(static_cast<unsigned char>(i));
162
163     return data.str();
164 }
165
166 void check_test_vector_init(yaca_encrypt_algorithm_e algo,
167                             yaca_block_cipher_mode_e bcm,
168                             size_t key_len,
169                             size_t iv_len,
170                             bool valid)
171 {
172     yaca_context_h ctx = YACA_CONTEXT_NULL;
173     int expected = YACA_ERROR_NONE;
174     yaca_key_type_e key_type;
175
176     if (!valid)
177        expected = YACA_ERROR_INVALID_PARAMETER;
178
179     auto iv_ptr = (iv_len > 0) ? generate_key(YACA_KEY_TYPE_IV, iv_len)
180                                : null_key();
181
182     /*
183      * Invalid DES key cannot be created using yaca_key_generate().
184      * If test case is incorrect, wrong key (symmetric) is generated
185      * in order to check how api behave with inappropriate key imported
186      */
187
188     if ((algo == YACA_ENCRYPT_UNSAFE_DES || algo == YACA_ENCRYPT_UNSAFE_3DES_2TDEA ||
189         algo == YACA_ENCRYPT_3DES_3TDEA) && valid)
190         key_type = YACA_KEY_TYPE_DES;
191     else
192         key_type = YACA_KEY_TYPE_SYMMETRIC;
193
194     auto key_ptr = generate_key(key_type, key_len);
195
196     YACA_RESULT(expected, yaca_encrypt_initialize(&ctx, algo, bcm, key_ptr.get(), iv_ptr.get()));
197     yaca_context_destroy(ctx);
198     ctx = YACA_CONTEXT_NULL;
199     YACA_RESULT(expected, yaca_decrypt_initialize(&ctx, algo, bcm, key_ptr.get(), iv_ptr.get()));
200     yaca_context_destroy(ctx);
201 }
202
203 void check_test_vector_output(const Buffer &input,
204                               yaca_encrypt_algorithm_e algo,
205                               yaca_block_cipher_mode_e bcm,
206                               const Buffer &key,
207                               size_t key_bits,
208                               const Buffer &iv,
209                               size_t repeats,
210                               const Buffer &expected,
211                               bool padded)
212 {
213     auto key_ptr = import_key(algo_to_key_type(algo), nullptr, key.data(), key.size());
214     auto iv_ptr = (iv.size() > 0) ? import_key(YACA_KEY_TYPE_IV, nullptr, iv.data(), iv.size())
215                                   : null_key();
216
217     Encryptor encryptor(algo, bcm, key_ptr, iv_ptr, key_bits, padded);
218     Buffer output_vector = encryptor.encrypt(input, repeats);
219
220     auto keyHex = bin2hex(key);
221     auto ivHex = bin2hex(iv);
222     std::stringstream message;
223
224     message << "Parameters:\n"
225             << encrypt2str(algo) << std::endl
226             << bcm2str(bcm)      << std::endl
227             << "key: "           << keyHex  << std::endl
228             << "iv:  "           << ivHex   << std::endl
229             << "repeats: "       << repeats << std::endl;
230
231     YACA_ASSERT_MSG(expected.size() == output_vector.size(), "size differs\n" << message.str());
232     YACA_ASSERT_MSG(expected == output_vector,
233                     "\nEncrypt string comparison failed\n" << message.str());
234 }
235
236 void test_encryption_output(std::string filename, bool padded=true)
237 {
238     auto tvv = loadTestVector(filename);
239
240     for (const auto& tv : tvv) {
241         Buffer input;
242         yaca_encrypt_algorithm_e algo;
243         yaca_block_cipher_mode_e bcm;
244         Buffer key;
245         size_t key_bits = 0;
246         Buffer iv;
247         size_t repeats;
248         Buffer expected;
249
250         tv.get("input", input);
251         tv.get("algo", algo);
252         tv.get("bcm", bcm);
253         tv.get("key", key);
254         if (algo == YACA_ENCRYPT_UNSAFE_RC2)
255             tv.get("effective_key_bits", key_bits);
256         tv.get("iv", iv);
257         tv.get("repeats", repeats);
258         tv.get("output", expected);
259
260         check_test_vector_output(input, algo, bcm, key, key_bits, iv, repeats, expected, padded);
261     }
262 }
263
264 void test_vector_encrypt_decrypt(const Buffer &input,
265                                  yaca_encrypt_algorithm_e algo,
266                                  yaca_block_cipher_mode_e bcm,
267                                  size_t key_len,
268                                  size_t iv_len)
269 {
270     std::stringstream message;
271
272     auto key_ptr = generate_key(algo_to_key_type(algo), key_len);
273     auto iv_ptr = (iv_len > 0) ? generate_key(YACA_KEY_TYPE_IV, iv_len)
274                                : null_key();
275     Encryptor encryptor(algo, bcm, key_ptr, iv_ptr);
276
277     auto encrypt_output = encryptor.encrypt(input);
278     auto decrypt_output = encryptor.decrypt(encrypt_output);
279
280     message << "Parameters:\n"
281             << encrypt2str(algo) << std::endl
282             << bcm2str(bcm)      << std::endl
283             << "key_len: "       << key_len << std::endl
284             << "iv_len:  "       << iv_len  << std::endl;
285
286     YACA_ASSERT_MSG(input.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
287     YACA_ASSERT_MSG(input == decrypt_output, "Text after encrypt-decrypt has changed\n"
288                                               << message.str());
289 }
290
291 void test_encryption_decryption(std::string filename)
292 {
293     std::string s = "abcdefghijklmnoprstuvwxyz0123456789";
294
295     auto tvv = loadTestVector(filename);
296
297     for (const auto& tv : tvv) {
298         yaca_encrypt_algorithm_e algo;
299         yaca_block_cipher_mode_e bcm;
300         size_t key_len;
301         size_t iv_len;
302         size_t key_data_len = 0;
303         Buffer input;
304
305         tv.get("algo", algo);
306         tv.get("bcm", bcm);
307         tv.get("key_len", key_len);
308         tv.get("iv_len", iv_len);
309         if (bcm == YACA_BCM_WRAP)
310             tv.get("key_data_len", key_data_len);
311
312         if (key_data_len > 0)
313             input = random_buffer(key_data_len / 8);
314         else
315             std::copy(s.begin(), s.end(), std::back_inserter(input));
316
317         test_vector_encrypt_decrypt(input, algo, bcm, key_len, iv_len);
318     }
319 }
320
321 void test_vector_gcm_tag_len(size_t key_len,
322                              size_t iv_len,
323                              size_t tag_len,
324                              bool valid)
325 {
326     auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
327     auto iv_ptr  = generate_key(YACA_KEY_TYPE_IV, iv_len);
328
329     int expected = YACA_ERROR_NONE;
330     if (!valid)
331         expected = YACA_ERROR_INVALID_PARAMETER;
332
333     Encryptor encryptor(YACA_ENCRYPT_AES, YACA_BCM_GCM, key_ptr, iv_ptr);
334     encryptor.encrypt(DATA);
335
336     YACA_RESULT(expected, encryptor.set_tag_len(YACA_BCM_GCM, tag_len));
337 }
338
339 void test_vector_ccm_tag_len(size_t key_len,
340                              size_t iv_len,
341                              size_t tag_len,
342                              bool valid)
343 {
344     auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
345     auto iv_ptr  = generate_key(YACA_KEY_TYPE_IV, iv_len);
346
347     int expected = YACA_ERROR_NONE;
348     if (!valid)
349         expected = YACA_ERROR_INVALID_PARAMETER;
350
351     Encryptor encryptor(YACA_ENCRYPT_AES, YACA_BCM_CCM, key_ptr, iv_ptr);
352
353     YACA_RESULT(expected, encryptor.set_tag_len(YACA_BCM_CCM, tag_len));
354 }
355
356 void aes_gcm_test_output(const Buffer &input,
357                          yaca_encrypt_algorithm_e algo,
358                          yaca_block_cipher_mode_e bcm,
359                          const Buffer &key,
360                          const Buffer &iv,
361                          const Buffer &aad,
362                          const Buffer &tag,
363                          const Buffer &output)
364 {
365     auto key_ptr = import_key(algo_to_key_type(algo), nullptr, key.data(), key.size());
366     auto iv_ptr = (iv.size() > 0) ? import_key(YACA_KEY_TYPE_IV, nullptr, iv.data(), iv.size())
367                                   : null_key();
368
369     size_t tag_len = tag.size();
370     auto tag_output = create_yaca_buffer(tag_len);
371
372     // encryption
373     Buffer encrypt_output;
374     auto enc_ctx_ptr = encrypt_init(algo, bcm, key_ptr, iv_ptr);
375     size_t update_len = 0;
376     size_t final_len = 0;
377
378     if (!aad.empty())
379         YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
380                                                aad.data(), aad.size()));
381
382     if (!input.empty()) {
383         auto update_ptr = out_buf_alloc(enc_ctx_ptr, input.size(), update_len);
384         YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), input.data(), input.size(),
385                                          update_ptr.get(), &update_len));
386         encrypt_output.insert(encrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
387     }
388
389     auto final_ptr = out_buf_alloc(enc_ctx_ptr, 0, final_len);
390     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx_ptr.get(), final_ptr.get(), &final_len));
391     encrypt_output.insert(encrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
392
393     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
394                                            (void*)&tag_len, sizeof(tag_len)));
395     YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
396                                            (void**)tag_output.get(), &tag_len));
397
398     YACA_ASSERT_MSG(output.size() == encrypt_output.size(), "Ciphertext size after encrypt differs\n");
399     YACA_ASSERT_MSG(output == encrypt_output, "Ciphertext after encrypt differs\n");
400     YACA_ASSERT_MSG(tag.size() == tag_len, "Tag size after encrypt differs\n");
401     YACA_ASSERT_MSG(yaca_memcmp(tag.data(), tag_output.get(), tag_len) == YACA_ERROR_NONE,
402                     "Tag after encrypt differs\n");
403
404     // decryption
405     Buffer decrypt_output;
406     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key_ptr, iv_ptr);
407
408     if (!aad.empty())
409         YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
410                                                aad.data(), aad.size()));
411     if (!output.empty()) {
412         auto update_ptr = out_buf_alloc(dec_ctx_ptr, output.size(), update_len);
413         YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), output.data(), output.size(),
414                                          update_ptr.get(), &update_len));
415         decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
416     }
417
418     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
419                                            tag.data(), tag.size()));
420
421     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
422     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
423     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
424
425     YACA_ASSERT_MSG(input.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
426     YACA_ASSERT_MSG(input == decrypt_output, "Text after encrypt-decrypt has changed\n");
427 }
428
429 void aes_ccm_test_output(const Buffer &input,
430                          yaca_encrypt_algorithm_e algo,
431                          yaca_block_cipher_mode_e bcm,
432                          const Buffer &key,
433                          const Buffer &iv,
434                          const Buffer &aad,
435                          const Buffer &tag,
436                          const Buffer &output)
437 {
438     auto key_ptr = import_key(algo_to_key_type(algo), nullptr, key.data(), key.size());
439     auto iv_ptr = (iv.size() > 0) ? import_key(YACA_KEY_TYPE_IV, nullptr, iv.data(), iv.size())
440                                   : null_key();
441
442     size_t tag_len = tag.size();
443     auto tag_output = create_yaca_buffer(tag_len);
444
445     // encryption
446     Buffer encrypt_output;
447     auto enc_ctx_ptr = encrypt_init(algo, bcm, key_ptr, iv_ptr);
448     size_t update_len = 0;
449     size_t final_len = 0;
450
451     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
452                                            (void*)&tag_len, sizeof(tag_len)));
453
454     if (!aad.empty()) {
455         YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), NULL, input.size(), NULL, &update_len));
456         YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
457                                                aad.data(), aad.size()));
458     }
459
460     if (!input.empty()) {
461         auto update_ptr = out_buf_alloc(enc_ctx_ptr, input.size(), update_len);
462         YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), input.data(), input.size(),
463                                          update_ptr.get(), &update_len));
464         encrypt_output.insert(encrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
465     }
466
467     auto final_ptr = out_buf_alloc(enc_ctx_ptr, 0, final_len);
468     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx_ptr.get(), final_ptr.get(), &final_len));
469     encrypt_output.insert(encrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
470
471     YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
472                                            (void**)tag_output.get(), &tag_len));
473
474     YACA_ASSERT_MSG(output.size() == encrypt_output.size(), "Ciphertext size after encrypt differs \n");
475     YACA_ASSERT_MSG(output == encrypt_output, "ciphertext after encrypt differs\n");
476     YACA_ASSERT_MSG(tag.size() == tag_len, "Tag size after encrypt differs\n");
477     YACA_ASSERT_MSG(yaca_memcmp(tag.data(), tag_output.get(), tag_len) == YACA_ERROR_NONE,
478                     "Tag after encrypt differs\n");
479
480     // decryption
481     Buffer decrypt_output;
482     auto dec_ctx_ptr = decrypt_init(algo, bcm, key_ptr, iv_ptr);
483
484     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
485                                            tag.data(), tag.size()));
486
487     if (!aad.empty()) {
488         YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), NULL, output.size(), NULL, &update_len));
489         YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
490                                                aad.data(), aad.size()));
491     }
492
493     if (!output.empty()) {
494         auto update_ptr = out_buf_alloc(dec_ctx_ptr, output.size(), update_len);
495         YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), output.data(), output.size(),
496                                          update_ptr.get(), &update_len));
497         decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
498     }
499
500     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
501     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
502     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
503
504     YACA_ASSERT_MSG(input.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
505     YACA_ASSERT_MSG(input == decrypt_output, "Text after encrypt-decrypt has changed\n");
506 }
507
508 }//namespace anonymous
509
510 RUNNER_TEST_GROUP_INIT(T3000_YACA_ENCRYPT);
511
512 RUNNER_TEST(T3010_yaca_encrypt_init_invalid_param, YacaTest)
513 {
514     yaca_context_h ctx;
515     KeyIvPair key_iv_pair;
516     auto asym_key_ptr = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT);
517     auto bad_iv_ptr = generate_key(YACA_KEY_TYPE_IV, INVALID_IV_BIT_LENGTH);
518     auto des_key_ptr = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_64BIT);
519
520     YACA_INVALID_PARAM(yaca_encrypt_initialize(nullptr, YACA_ENCRYPT_AES, YACA_BCM_CBC,
521                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
522
523     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, static_cast<yaca_encrypt_algorithm_e>(-1),
524                                                YACA_BCM_CBC, key_iv_pair.key.get(),
525                                                key_iv_pair.iv.get()));
526
527     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES,
528                                                static_cast<yaca_block_cipher_mode_e>(-1),
529                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
530
531     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
532                                                key_iv_pair.iv.get()));
533
534     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
535                                                asym_key_ptr.get(), key_iv_pair.iv.get()));
536
537     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_ECB,
538                                                des_key_ptr.get(), key_iv_pair.iv.get()));
539
540     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
541                                                key_iv_pair.key.get(), YACA_KEY_NULL));
542
543     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
544                                                key_iv_pair.key.get(), bad_iv_ptr.get()));
545
546     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
547                                                key_iv_pair.iv.get(), key_iv_pair.iv.get()));
548
549     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
550                                                des_key_ptr.get(), key_iv_pair.iv.get()));
551 }
552
553 RUNNER_TEST(T3020_yaca_encrypt_update_invalid_param, YacaTest)
554 {
555     size_t ciphertext_len = 0;
556     KeyIvPair key_iv_pair;
557     auto en_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
558     auto inv_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
559     auto ciphertext_ptr = out_buf_alloc(en_ctx_ptr, DATA.size(), ciphertext_len);
560
561     YACA_INVALID_PARAM(yaca_encrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
562                                            ciphertext_ptr.get(), &ciphertext_len));
563
564     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, DATA.size(),
565                                            ciphertext_ptr.get(), &ciphertext_len));
566
567     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, 0,
568                                            ciphertext_ptr.get(), &ciphertext_len));
569
570     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), 0,
571                                            ciphertext_ptr.get(), &ciphertext_len));
572
573     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
574                                            nullptr, &ciphertext_len));
575
576     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
577                                            ciphertext_ptr.get(), nullptr));
578
579     YACA_INVALID_PARAM(yaca_encrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
580                                            ciphertext_ptr.get(), &ciphertext_len));
581 }
582
583 RUNNER_TEST(T3030_yaca_encrypt_final_invalid_param, YacaTest)
584 {
585     size_t ciphertext_len = 0;
586     KeyIvPair key_iv_pair;
587     auto en_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
588     auto ciphertext_ptr = out_buf_alloc(en_ctx_ptr, DATA.size(), ciphertext_len);
589     auto inv_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
590
591     YACA_INVALID_PARAM(yaca_encrypt_finalize(YACA_CONTEXT_NULL, ciphertext_ptr.get(),
592                                              &ciphertext_len));
593
594     YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), nullptr, &ciphertext_len));
595
596     YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), ciphertext_ptr.get(), nullptr));
597
598     YACA_INVALID_PARAM(yaca_encrypt_finalize(inv_ctx_ptr.get(), ciphertext_ptr.get(),
599                                              &ciphertext_len));
600 }
601
602 RUNNER_TEST(T3040_yaca_decrypt_init_invalid_param, YacaTest)
603 {
604     yaca_context_h ctx;
605     KeyIvPair key_iv_pair;
606     auto asym_key_ptr = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT);
607     auto bad_iv_ptr = generate_key(YACA_KEY_TYPE_IV, INVALID_IV_BIT_LENGTH);
608     auto des_key_ptr = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_64BIT);
609
610     YACA_INVALID_PARAM(yaca_decrypt_initialize(nullptr, YACA_ENCRYPT_AES, YACA_BCM_CBC,
611                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
612
613     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, static_cast<yaca_encrypt_algorithm_e>(-1),
614                                                YACA_BCM_CBC, key_iv_pair.key.get(),
615                                                key_iv_pair.iv.get()));
616
617     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES,
618                                                static_cast<yaca_block_cipher_mode_e>(-1),
619                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
620
621     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
622                                                key_iv_pair.iv.get()));
623
624     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
625                                                asym_key_ptr.get(), key_iv_pair.iv.get()));
626
627     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_ECB,
628                                                des_key_ptr.get(), key_iv_pair.iv.get()));
629
630     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
631                                                key_iv_pair.key.get(), YACA_KEY_NULL));
632
633     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
634                                                key_iv_pair.key.get(), bad_iv_ptr.get()));
635
636     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
637                                                key_iv_pair.iv.get(), key_iv_pair.iv.get()));
638
639     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
640                                                des_key_ptr.get(), key_iv_pair.iv.get()));
641 }
642
643 RUNNER_TEST(T3050_yaca_decrypt_update_invalid_param, YacaTest)
644 {
645     size_t plaintext_len = 0;
646     KeyIvPair key_iv_pair;
647     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
648     auto plaintext_ptr = out_buf_alloc(dec_ctx_ptr, DATA.size(), plaintext_len);
649     auto inv_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
650
651     YACA_INVALID_PARAM(yaca_decrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
652                                            nullptr, &plaintext_len));
653
654     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, DATA.size(),
655                                            plaintext_ptr.get(), &plaintext_len));
656
657     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), 0,
658                                            plaintext_ptr.get(), &plaintext_len));
659
660     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, 0,
661                                            plaintext_ptr.get(), &plaintext_len));
662
663     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
664                                            nullptr, &plaintext_len));
665
666     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
667                                            plaintext_ptr.get(), nullptr));
668
669     YACA_INVALID_PARAM(yaca_decrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
670                                            plaintext_ptr.get(), &plaintext_len));
671 }
672
673 RUNNER_TEST(T3060_yaca_decrypt_final_invalid_param, YacaTest)
674 {
675     size_t plaintext_len = 0;
676     KeyIvPair key_iv_pair;
677     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
678     auto plaintext_ptr = out_buf_alloc(dec_ctx_ptr, DATA.size(), plaintext_len);
679     auto inv_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
680
681     YACA_INVALID_PARAM(yaca_decrypt_finalize(YACA_CONTEXT_NULL, plaintext_ptr.get(),
682                                              &plaintext_len));
683
684     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), nullptr, &plaintext_len));
685
686     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), plaintext_ptr.get(), nullptr));
687
688     YACA_INVALID_PARAM(yaca_decrypt_finalize(inv_ctx_ptr.get(), plaintext_ptr.get(),
689                                              &plaintext_len));
690 }
691
692 RUNNER_TEST(T3070_yaca_get_iv_bits_invalid_param, YacaTest)
693 {
694     const size_t KEY_LEN = 256;
695     size_t iv_bit_len;
696     const size_t INVALID_KEY_BIT_LENGTH = 512;
697
698     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(static_cast<yaca_encrypt_algorithm_e>(-1),
699                                                       YACA_BCM_CBC, KEY_LEN, &iv_bit_len));
700
701     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES,
702                                                       static_cast<yaca_block_cipher_mode_e>(-1),
703                                                       KEY_LEN, &iv_bit_len));
704
705     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
706                                                       INVALID_KEY_BIT_LENGTH, &iv_bit_len));
707
708     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
709                                                       KEY_LEN, nullptr));
710 }
711
712 RUNNER_TEST(T3075_yaca_key_wrap_unwrap_invalid_param, YacaTest)
713 {
714     size_t wrapped_len = 0;
715     size_t unwrapped_len = 0;
716
717     KeyPtr key = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_192BIT);
718     KeyPtr iv = null_key();
719     Buffer key_data = random_buffer(YACA_KEY_LENGTH_192BIT / 8);
720
721     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_WRAP, key, iv);
722     CtxPtr dec_ctx = decrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_WRAP, key, iv);
723
724     ChrPtr wrapped = out_buf_alloc(enc_ctx, key_data.size(), wrapped_len);
725     ChrPtr unwrapped = out_buf_alloc(dec_ctx, wrapped_len, unwrapped_len);
726
727     YACA_INVALID_PARAM(yaca_encrypt_finalize(enc_ctx.get(), wrapped.get(), &wrapped_len));
728     YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), YACA_KEY_LENGTH_UNSAFE_64BIT / 8,
729                                            wrapped.get(), &wrapped_len));
730     YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), YACA_KEY_LENGTH_512BIT / 8,
731                                            wrapped.get(), &wrapped_len));
732     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), key_data.data(), key_data.size(),
733                                      wrapped.get(), &wrapped_len));
734
735     // only a single update is allowed
736     YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), key_data.size(),
737                                            wrapped.get(), &wrapped_len));
738
739     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx.get(), wrapped.get(), &wrapped_len));
740     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len - 1,
741                                            unwrapped.get(),&unwrapped_len));
742     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len,
743                                      unwrapped.get(), &unwrapped_len));
744
745     // only a single update is allowed
746     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len,
747                                            unwrapped.get(), &unwrapped_len));
748 }
749
750 RUNNER_TEST(T3080_yaca_encrypt_decrypt_init_param_comb, YacaTest)
751 {
752     auto tvv = loadTestVector("encrypt_param_comb.txt");
753
754     for (const auto& tv : tvv) {
755         yaca_encrypt_algorithm_e algo;
756         yaca_block_cipher_mode_e bcm;
757         size_t key_len;
758         size_t iv_len;
759         bool valid;
760
761         tv.get("algo", algo);
762         tv.get("bcm", bcm);
763         tv.get("key_len", key_len);
764         tv.get("iv_len", iv_len);
765         tv.get("valid", valid);
766
767         check_test_vector_init(algo, bcm, key_len, iv_len, valid);
768     }
769 }
770
771 RUNNER_TEST(T3090_yaca_encrypt_update_finalize_output_comp, YacaTest)
772 {
773     test_encryption_output("encrypt_output_comparison.txt");
774     test_encryption_output("encrypt_output_comparison_rc2_cast5_nopad.txt", false);
775     test_encryption_output("encrypt_output_comparison_rc4.txt");
776     test_encryption_output("encrypt_output_comparison_wrap.txt");
777 }
778
779 RUNNER_TEST(T3100_yaca_encrypt_decrypt_comparison, YacaTest)
780 {
781     test_encryption_decryption("encrypt_valid_param.txt");
782     test_encryption_decryption("encrypt_valid_param_wrap.txt");
783 }
784
785 RUNNER_TEST(T3110_yaca_aes_gcm_call_order_invalid_param, YacaTest)
786 {
787     KeyIvPair key_iv_pair;
788     size_t tag_len = 14;
789     auto tag = create_yaca_buffer(tag_len);
790     auto aad = random_buffer(16);
791     Buffer encrypt_output;
792     Buffer decrypt_output;
793
794     // encryption
795     auto enc_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM,
796                                     key_iv_pair.key, key_iv_pair.iv);
797     size_t update_len = 0;
798     auto update_ptr = out_buf_alloc(enc_ctx_ptr, DATA.size(), update_len);
799     size_t final_len = 0;
800     auto final_ptr = out_buf_alloc(enc_ctx_ptr, 0, final_len);
801
802     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
803                                                  tag.get(), tag_len));
804     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
805                                                  (void**)tag.get(), &tag_len));
806     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
807                                                  (void*)&tag_len, sizeof(tag_len)));
808
809     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
810                                            aad.data(), aad.size()));
811
812     YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), DATA.data(), DATA.size(),
813                                      update_ptr.get(), &update_len));
814     encrypt_output.insert(encrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
815
816     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
817                                                  aad.data(), aad.size()));
818     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
819                                                  tag.get(), tag_len));
820     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
821                                                  (void**)tag.get(), &tag_len));
822     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
823                                                  (void*)&tag_len, sizeof(tag_len)));
824
825     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx_ptr.get(), final_ptr.get(), &final_len));
826     encrypt_output.insert(encrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
827
828     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
829                                                  aad.data(), aad.size()));
830     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
831                                                  tag.get(), tag_len));
832     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
833                                            (void*)&tag_len, sizeof(tag_len)));
834     YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
835                                            (void**)tag.get(), &tag_len));
836     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
837                                                  (void*)&tag_len, sizeof(tag_len)));
838
839     // decryption
840     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM,
841                                     key_iv_pair.key, key_iv_pair.iv);
842     update_ptr = out_buf_alloc(dec_ctx_ptr, encrypt_output.size(), update_len);
843     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
844
845     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
846                                                  (void**)tag.get(), &tag_len));
847     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
848                                                  (void*)&tag_len, sizeof(tag_len)));
849
850     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
851                                            aad.data(), aad.size()));
852
853     YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), encrypt_output.data(), encrypt_output.size(),
854                                      update_ptr.get(), &update_len));
855     decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
856
857     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
858                                                  aad.data(), aad.size()));
859     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
860                                                  (void*)&tag_len, sizeof(tag_len)));
861     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
862                                                  (void**)tag.get(), &tag_len));
863     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
864                                                  tag.get(), tag_len + 42));
865
866     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
867                                            tag.get(), tag_len));
868
869     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
870     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
871
872     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
873                                                  aad.data(), aad.size()));
874     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
875                                                  tag.get(), tag_len));
876     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
877                                                  (void*)&tag_len, sizeof(tag_len)));
878     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
879                                                  (void**)tag.get(), &tag_len));
880
881     YACA_ASSERT_MSG(DATA.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
882     YACA_ASSERT_MSG(DATA == decrypt_output, "Text after encrypt-decrypt has changed\n");
883 }
884
885 RUNNER_TEST(T3120_yaca_aes_ccm_call_order_invalid_param, YacaTest)
886 {
887     KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_256BIT);
888     KeyPtr iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_64BIT);
889     size_t tag_len = 16;
890     auto tag = create_yaca_buffer(tag_len);
891     auto aad = random_buffer(16);
892     Buffer encrypt_output;
893     Buffer decrypt_output;
894
895     // encryption
896     auto enc_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
897     size_t update_len = 0;
898     auto update_ptr = out_buf_alloc(enc_ctx_ptr, DATA.size(), update_len);
899     size_t final_len = 0;
900     auto final_ptr = out_buf_alloc(enc_ctx_ptr, 0, final_len);
901
902     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
903                                                  tag.get(), tag_len));
904     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
905                                                  (void**)tag.get(), &tag_len));
906     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
907                                            (void*)&tag_len, sizeof(tag_len)));
908
909     YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), NULL, DATA.size(), NULL, &update_len));
910     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
911                                            aad.data(), aad.size()));
912     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
913                                                  (void*)&tag_len, sizeof(tag_len)));
914
915     YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), DATA.data(), DATA.size(),
916                                      update_ptr.get(), &update_len));
917     encrypt_output.insert(encrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
918
919     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
920                                                  aad.data(), aad.size()));
921     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
922                                                  tag.get(), tag_len));
923     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
924                                                  (void*)&tag_len, sizeof(tag_len)));
925     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
926                                                  (void**)tag.get(), &tag_len));
927
928     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx_ptr.get(), final_ptr.get(), &final_len));
929     encrypt_output.insert(encrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
930
931     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
932                                                  aad.data(), aad.size()));
933     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
934                                                  tag.get(), tag_len));
935     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
936                                                  (void*)&tag_len, sizeof(tag_len)));
937     YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
938                                            (void**)tag.get(), &tag_len));
939
940     // decryption
941     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
942     update_ptr = out_buf_alloc(dec_ctx_ptr, encrypt_output.size(), update_len);
943     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
944
945     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
946                                                  (void*)&tag_len, sizeof(tag_len)));
947     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
948                                                  (void**)tag.get(), &tag_len));
949     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
950                                                  tag.get(), tag_len + 42));
951
952     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
953                                            tag.get(), tag_len));
954
955     YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), NULL, encrypt_output.size(),
956                                      NULL, &update_len));
957     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
958                                            aad.data(), aad.size()));
959     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
960                                                  tag.get(), tag_len));
961
962     YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), encrypt_output.data(), encrypt_output.size(),
963                                      update_ptr.get(), &update_len));
964     decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
965
966     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
967                                                  aad.data(), aad.size()));
968     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
969                                                  tag.get(), tag_len));
970     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
971                                                  (void*)&tag_len, sizeof(tag_len)));
972     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
973                                                  (void**)tag.get(), &tag_len));
974
975     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
976     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
977
978     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
979                                                  aad.data(), aad.size()));
980     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
981                                                  tag.get(), tag_len));
982     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
983                                                  (void*)&tag_len, sizeof(tag_len)));
984     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
985                                                  (void**)tag.get(), &tag_len));
986
987     YACA_ASSERT_MSG(DATA.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
988     YACA_ASSERT_MSG(DATA == decrypt_output, "Text after encrypt-decrypt has changed\n");
989 }
990
991 RUNNER_TEST(T3130_yaca_aes_gcm_tag_len_combs_invalid_param, YacaTest)
992 {
993     auto tvv = loadTestVector("encrypt_aes_gcm_tag_len.txt");
994
995     for (const auto& tv : tvv) {
996         size_t key_len;
997         size_t iv_len;
998         size_t tag_len;
999         bool valid;
1000
1001         tv.get("key_len", key_len);
1002         tv.get("iv_len", iv_len);
1003         tv.get("tag_len", tag_len);
1004         tv.get("valid", valid);
1005
1006         test_vector_gcm_tag_len(key_len, iv_len, tag_len, valid);
1007     }
1008 }
1009
1010 RUNNER_TEST(T3140_yaca_aes_ccm_tag_len_combs_invalid_param, YacaTest)
1011 {
1012     auto tvv = loadTestVector("encrypt_aes_ccm_tag_len.txt");
1013
1014     for (const auto& tv : tvv) {
1015         size_t key_len;
1016         size_t iv_len;
1017         size_t tag_len;
1018         bool valid;
1019
1020         tv.get("key_len", key_len);
1021         tv.get("iv_len", iv_len);
1022         tv.get("tag_len", tag_len);
1023         tv.get("valid", valid);
1024
1025         test_vector_ccm_tag_len(key_len, iv_len, tag_len, valid);
1026     }
1027 }
1028
1029 RUNNER_TEST(T3150_yaca_aes_gcm_ccm_output_comparison, YacaTest)
1030 {
1031     auto tvv = loadTestVector("encrypt_output_comparison_aes_gcm_ccm.txt");
1032
1033     for (const auto& tv : tvv) {
1034         Buffer input;
1035         yaca_encrypt_algorithm_e algo;
1036         yaca_block_cipher_mode_e bcm;
1037         Buffer key;
1038         Buffer iv;
1039         Buffer aad;
1040         Buffer tag;
1041         Buffer output;
1042
1043         tv.get("input", input);
1044         tv.get("algo", algo);
1045         tv.get("bcm", bcm);
1046         tv.get("key", key);
1047         tv.get("iv", iv);
1048         tv.get("aad", aad);
1049         tv.get("tag", tag);
1050         tv.get("output", output);
1051
1052         bcm == YACA_BCM_GCM ? aes_gcm_test_output(input, algo, bcm, key, iv, aad, tag, output)
1053                             : aes_ccm_test_output(input, algo, bcm, key, iv, aad, tag, output);
1054     }
1055 }