Merge branch 'nether' into tizen
[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                                                (void*)(&padding), sizeof(padding)));
94         YACA_SUCCESS(yaca_context_set_property(m_decCtxPtr.get(), YACA_PROPERTY_PADDING,
95                                                (void*)(&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                                                (void*)(&key_bits), sizeof(key_bits)));
102         YACA_SUCCESS(yaca_context_set_property(m_decCtxPtr.get(), YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
103                                                (void*)(&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     char *tag_output;
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, &tag_len));
397     ChrPtr tag_ptr = wrap_ptr(tag_output);
398
399     YACA_ASSERT_MSG(output.size() == encrypt_output.size(), "Ciphertext size after encrypt differs\n");
400     YACA_ASSERT_MSG(output == encrypt_output, "Ciphertext after encrypt differs\n");
401     YACA_ASSERT_MSG(tag.size() == tag_len, "Tag size after encrypt differs\n");
402     YACA_ASSERT_MSG(yaca_memcmp(tag.data(), tag_output, tag_len) == YACA_ERROR_NONE,
403                     "Tag after encrypt differs\n");
404
405     // decryption
406     Buffer decrypt_output;
407     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key_ptr, iv_ptr);
408
409     if (!aad.empty())
410         YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
411                                                aad.data(), aad.size()));
412     if (!output.empty()) {
413         auto update_ptr = out_buf_alloc(dec_ctx_ptr, output.size(), update_len);
414         YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), output.data(), output.size(),
415                                          update_ptr.get(), &update_len));
416         decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
417     }
418
419     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
420                                            tag.data(), tag.size()));
421
422     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
423     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
424     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
425
426     YACA_ASSERT_MSG(input.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
427     YACA_ASSERT_MSG(input == decrypt_output, "Text after encrypt-decrypt has changed\n");
428 }
429
430 void aes_ccm_test_output(const Buffer &input,
431                          yaca_encrypt_algorithm_e algo,
432                          yaca_block_cipher_mode_e bcm,
433                          const Buffer &key,
434                          const Buffer &iv,
435                          const Buffer &aad,
436                          const Buffer &tag,
437                          const Buffer &output)
438 {
439     auto key_ptr = import_key(algo_to_key_type(algo), nullptr, key.data(), key.size());
440     auto iv_ptr = (iv.size() > 0) ? import_key(YACA_KEY_TYPE_IV, nullptr, iv.data(), iv.size())
441                                   : null_key();
442
443     size_t tag_len = tag.size();
444     char *tag_output;
445
446     // encryption
447     Buffer encrypt_output;
448     auto enc_ctx_ptr = encrypt_init(algo, bcm, key_ptr, iv_ptr);
449     size_t update_len = 0;
450     size_t final_len = 0;
451
452     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
453                                            (void*)&tag_len, sizeof(tag_len)));
454
455     if (!aad.empty()) {
456         YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), NULL, input.size(), NULL, &update_len));
457         YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
458                                                aad.data(), aad.size()));
459     }
460
461     if (!input.empty()) {
462         auto update_ptr = out_buf_alloc(enc_ctx_ptr, input.size(), update_len);
463         YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), input.data(), input.size(),
464                                          update_ptr.get(), &update_len));
465         encrypt_output.insert(encrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
466     }
467
468     auto final_ptr = out_buf_alloc(enc_ctx_ptr, 0, final_len);
469     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx_ptr.get(), final_ptr.get(), &final_len));
470     encrypt_output.insert(encrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
471
472     YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
473                                            (void**)&tag_output, &tag_len));
474     ChrPtr tag_ptr = wrap_ptr(tag_output);
475
476     YACA_ASSERT_MSG(output.size() == encrypt_output.size(), "Ciphertext size after encrypt differs \n");
477     YACA_ASSERT_MSG(output == encrypt_output, "ciphertext after encrypt differs\n");
478     YACA_ASSERT_MSG(tag.size() == tag_len, "Tag size after encrypt differs\n");
479     YACA_ASSERT_MSG(yaca_memcmp(tag.data(), tag_output, tag_len) == YACA_ERROR_NONE,
480                     "Tag after encrypt differs\n");
481
482     // decryption
483     Buffer decrypt_output;
484     auto dec_ctx_ptr = decrypt_init(algo, bcm, key_ptr, iv_ptr);
485
486     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
487                                            tag.data(), tag.size()));
488
489     if (!aad.empty()) {
490         YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), NULL, output.size(), NULL, &update_len));
491         YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
492                                                aad.data(), aad.size()));
493     }
494
495     if (!output.empty()) {
496         auto update_ptr = out_buf_alloc(dec_ctx_ptr, output.size(), update_len);
497         YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), output.data(), output.size(),
498                                          update_ptr.get(), &update_len));
499         decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
500     }
501
502     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
503     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
504     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
505
506     YACA_ASSERT_MSG(input.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
507     YACA_ASSERT_MSG(input == decrypt_output, "Text after encrypt-decrypt has changed\n");
508 }
509
510 }//namespace anonymous
511
512 RUNNER_TEST_GROUP_INIT(T3000_YACA_ENCRYPT);
513
514 RUNNER_TEST(T3010_yaca_encrypt_init_invalid_param, YacaTest)
515 {
516     yaca_context_h ctx;
517     KeyIvPair key_iv_pair;
518     auto asym_key_ptr = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT);
519     auto bad_iv_ptr = generate_key(YACA_KEY_TYPE_IV, INVALID_IV_BIT_LENGTH);
520     auto des_key_ptr = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_64BIT);
521
522     YACA_INVALID_PARAM(yaca_encrypt_initialize(nullptr, YACA_ENCRYPT_AES, YACA_BCM_CBC,
523                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
524
525     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, static_cast<yaca_encrypt_algorithm_e>(-1),
526                                                YACA_BCM_CBC, key_iv_pair.key.get(),
527                                                key_iv_pair.iv.get()));
528
529     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES,
530                                                static_cast<yaca_block_cipher_mode_e>(-1),
531                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
532
533     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
534                                                key_iv_pair.iv.get()));
535
536     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
537                                                asym_key_ptr.get(), key_iv_pair.iv.get()));
538
539     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_ECB,
540                                                des_key_ptr.get(), key_iv_pair.iv.get()));
541
542     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
543                                                key_iv_pair.key.get(), YACA_KEY_NULL));
544
545     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
546                                                key_iv_pair.key.get(), bad_iv_ptr.get()));
547
548     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
549                                                key_iv_pair.iv.get(), key_iv_pair.iv.get()));
550
551     YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
552                                                des_key_ptr.get(), key_iv_pair.iv.get()));
553 }
554
555 RUNNER_TEST(T3020_yaca_encrypt_update_invalid_param, YacaTest)
556 {
557     size_t ciphertext_len = 0;
558     KeyIvPair key_iv_pair;
559     auto en_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
560     auto inv_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
561     auto ciphertext_ptr = out_buf_alloc(en_ctx_ptr, DATA.size(), ciphertext_len);
562
563     YACA_INVALID_PARAM(yaca_encrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
564                                            ciphertext_ptr.get(), &ciphertext_len));
565
566     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, DATA.size(),
567                                            ciphertext_ptr.get(), &ciphertext_len));
568
569     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, 0,
570                                            ciphertext_ptr.get(), &ciphertext_len));
571
572     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), 0,
573                                            ciphertext_ptr.get(), &ciphertext_len));
574
575     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
576                                            nullptr, &ciphertext_len));
577
578     YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
579                                            ciphertext_ptr.get(), nullptr));
580
581     YACA_INVALID_PARAM(yaca_encrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
582                                            ciphertext_ptr.get(), &ciphertext_len));
583 }
584
585 RUNNER_TEST(T3030_yaca_encrypt_final_invalid_param, YacaTest)
586 {
587     size_t ciphertext_len = 0;
588     KeyIvPair key_iv_pair;
589     auto en_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
590     auto ciphertext_ptr = out_buf_alloc(en_ctx_ptr, DATA.size(), ciphertext_len);
591     auto inv_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
592
593     YACA_INVALID_PARAM(yaca_encrypt_finalize(YACA_CONTEXT_NULL, ciphertext_ptr.get(),
594                                              &ciphertext_len));
595
596     YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), nullptr, &ciphertext_len));
597
598     YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), ciphertext_ptr.get(), nullptr));
599
600     YACA_INVALID_PARAM(yaca_encrypt_finalize(inv_ctx_ptr.get(), ciphertext_ptr.get(),
601                                              &ciphertext_len));
602 }
603
604 RUNNER_TEST(T3040_yaca_decrypt_init_invalid_param, YacaTest)
605 {
606     yaca_context_h ctx;
607     KeyIvPair key_iv_pair;
608     auto asym_key_ptr = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT);
609     auto bad_iv_ptr = generate_key(YACA_KEY_TYPE_IV, INVALID_IV_BIT_LENGTH);
610     auto des_key_ptr = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_64BIT);
611
612     YACA_INVALID_PARAM(yaca_decrypt_initialize(nullptr, YACA_ENCRYPT_AES, YACA_BCM_CBC,
613                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
614
615     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, static_cast<yaca_encrypt_algorithm_e>(-1),
616                                                YACA_BCM_CBC, key_iv_pair.key.get(),
617                                                key_iv_pair.iv.get()));
618
619     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES,
620                                                static_cast<yaca_block_cipher_mode_e>(-1),
621                                                key_iv_pair.key.get(), key_iv_pair.iv.get()));
622
623     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
624                                                key_iv_pair.iv.get()));
625
626     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
627                                                asym_key_ptr.get(), key_iv_pair.iv.get()));
628
629     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_ECB,
630                                                des_key_ptr.get(), key_iv_pair.iv.get()));
631
632     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
633                                                key_iv_pair.key.get(), YACA_KEY_NULL));
634
635     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
636                                                key_iv_pair.key.get(), bad_iv_ptr.get()));
637
638     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
639                                                key_iv_pair.iv.get(), key_iv_pair.iv.get()));
640
641     YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
642                                                des_key_ptr.get(), key_iv_pair.iv.get()));
643 }
644
645 RUNNER_TEST(T3050_yaca_decrypt_update_invalid_param, YacaTest)
646 {
647     size_t plaintext_len = 0;
648     KeyIvPair key_iv_pair;
649     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
650     auto plaintext_ptr = out_buf_alloc(dec_ctx_ptr, DATA.size(), plaintext_len);
651     auto inv_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
652
653     YACA_INVALID_PARAM(yaca_decrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
654                                            nullptr, &plaintext_len));
655
656     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, DATA.size(),
657                                            plaintext_ptr.get(), &plaintext_len));
658
659     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), 0,
660                                            plaintext_ptr.get(), &plaintext_len));
661
662     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, 0,
663                                            plaintext_ptr.get(), &plaintext_len));
664
665     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
666                                            nullptr, &plaintext_len));
667
668     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
669                                            plaintext_ptr.get(), nullptr));
670
671     YACA_INVALID_PARAM(yaca_decrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
672                                            plaintext_ptr.get(), &plaintext_len));
673 }
674
675 RUNNER_TEST(T3060_yaca_decrypt_final_invalid_param, YacaTest)
676 {
677     size_t plaintext_len = 0;
678     KeyIvPair key_iv_pair;
679     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
680     auto plaintext_ptr = out_buf_alloc(dec_ctx_ptr, DATA.size(), plaintext_len);
681     auto inv_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
682
683     YACA_INVALID_PARAM(yaca_decrypt_finalize(YACA_CONTEXT_NULL, plaintext_ptr.get(),
684                                              &plaintext_len));
685
686     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), nullptr, &plaintext_len));
687
688     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), plaintext_ptr.get(), nullptr));
689
690     YACA_INVALID_PARAM(yaca_decrypt_finalize(inv_ctx_ptr.get(), plaintext_ptr.get(),
691                                              &plaintext_len));
692 }
693
694 RUNNER_TEST(T3070_yaca_get_iv_bits_invalid_param, YacaTest)
695 {
696     const size_t KEY_LEN = 256;
697     size_t iv_bit_len;
698     const size_t INVALID_KEY_BIT_LENGTH = 512;
699
700     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(static_cast<yaca_encrypt_algorithm_e>(-1),
701                                                       YACA_BCM_CBC, KEY_LEN, &iv_bit_len));
702
703     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES,
704                                                       static_cast<yaca_block_cipher_mode_e>(-1),
705                                                       KEY_LEN, &iv_bit_len));
706
707     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
708                                                       INVALID_KEY_BIT_LENGTH, &iv_bit_len));
709
710     YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
711                                                       KEY_LEN, nullptr));
712 }
713
714 RUNNER_TEST(T3075_yaca_key_wrap_unwrap_invalid_param, YacaTest)
715 {
716     size_t wrapped_len = 0;
717     size_t unwrapped_len = 0;
718
719     KeyPtr key = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_192BIT);
720     KeyPtr iv = null_key();
721     Buffer key_data = random_buffer(YACA_KEY_LENGTH_192BIT / 8);
722
723     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_WRAP, key, iv);
724     CtxPtr dec_ctx = decrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_WRAP, key, iv);
725
726     ChrPtr wrapped = out_buf_alloc(enc_ctx, key_data.size(), wrapped_len);
727     ChrPtr unwrapped = out_buf_alloc(dec_ctx, wrapped_len, unwrapped_len);
728
729     YACA_INVALID_PARAM(yaca_encrypt_finalize(enc_ctx.get(), wrapped.get(), &wrapped_len));
730     YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), YACA_KEY_LENGTH_UNSAFE_64BIT / 8,
731                                            wrapped.get(), &wrapped_len));
732     YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), YACA_KEY_LENGTH_512BIT / 8,
733                                            wrapped.get(), &wrapped_len));
734     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), key_data.data(), key_data.size(),
735                                      wrapped.get(), &wrapped_len));
736
737     // only a single update is allowed
738     YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), key_data.data(), key_data.size(),
739                                            wrapped.get(), &wrapped_len));
740
741     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx.get(), wrapped.get(), &wrapped_len));
742     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len - 1,
743                                            unwrapped.get(),&unwrapped_len));
744     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len,
745                                      unwrapped.get(), &unwrapped_len));
746
747     // only a single update is allowed
748     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), wrapped.get(), wrapped_len,
749                                            unwrapped.get(), &unwrapped_len));
750 }
751
752 RUNNER_TEST(T3077_yaca_get_iv_bit_length, YacaTest)
753 {
754     size_t iv_bit_len;
755     YACA_SUCCESS(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_LENGTH_256BIT,
756                                                 &iv_bit_len));
757     YACA_ASSERT_MSG(iv_bit_len == YACA_KEY_LENGTH_IV_128BIT, "Invalid IV bit length.");
758
759     YACA_SUCCESS(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_ECB, YACA_KEY_LENGTH_256BIT,
760                                                 &iv_bit_len));
761     YACA_ASSERT_MSG(iv_bit_len == 0, "Invalid IV bit length.");
762
763     YACA_SUCCESS(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_CBC, YACA_KEY_LENGTH_192BIT,
764                                                 &iv_bit_len));
765     YACA_ASSERT_MSG(iv_bit_len == YACA_KEY_LENGTH_IV_64BIT, "Invalid IV bit length.");
766 }
767
768 RUNNER_TEST(T3080_yaca_encrypt_decrypt_init_param_comb, YacaTest)
769 {
770     auto tvv = loadTestVector("encrypt_param_comb.txt");
771
772     for (const auto& tv : tvv) {
773         yaca_encrypt_algorithm_e algo;
774         yaca_block_cipher_mode_e bcm;
775         size_t key_len;
776         size_t iv_len;
777         bool valid;
778
779         tv.get("algo", algo);
780         tv.get("bcm", bcm);
781         tv.get("key_len", key_len);
782         tv.get("iv_len", iv_len);
783         tv.get("valid", valid);
784
785         check_test_vector_init(algo, bcm, key_len, iv_len, valid);
786     }
787 }
788
789 RUNNER_TEST(T3090_yaca_encrypt_update_finalize_output_comp, YacaTest)
790 {
791     test_encryption_output("encrypt_output_comparison.txt");
792     test_encryption_output("encrypt_output_comparison_rc2_cast5_nopad.txt", false);
793     test_encryption_output("encrypt_output_comparison_rc4.txt");
794     test_encryption_output("encrypt_output_comparison_wrap.txt");
795 }
796
797 RUNNER_TEST(T3100_yaca_encrypt_decrypt_comparison, YacaTest)
798 {
799     test_encryption_decryption("encrypt_valid_param.txt");
800     test_encryption_decryption("encrypt_valid_param_wrap.txt");
801 }
802
803 RUNNER_TEST(T3110_yaca_aes_gcm_call_order_invalid_param, YacaTest)
804 {
805     KeyIvPair key_iv_pair;
806     size_t tag_len = 14;
807     char *tag;
808     auto aad = random_buffer(16);
809     Buffer encrypt_output;
810     Buffer decrypt_output;
811
812     // encryption
813     auto enc_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM,
814                                     key_iv_pair.key, key_iv_pair.iv);
815     size_t update_len = 0;
816     auto update_ptr = out_buf_alloc(enc_ctx_ptr, DATA.size(), update_len);
817     size_t final_len = 0;
818     auto final_ptr = out_buf_alloc(enc_ctx_ptr, 0, final_len);
819
820     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
821                                                  tag, tag_len));
822     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
823                                                  (void**)&tag, &tag_len));
824     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
825                                                  (void*)&tag_len, sizeof(tag_len)));
826
827     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
828                                            aad.data(), aad.size()));
829
830     YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), DATA.data(), DATA.size(),
831                                      update_ptr.get(), &update_len));
832     encrypt_output.insert(encrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
833
834     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
835                                                  aad.data(), aad.size()));
836     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
837                                                  tag, tag_len));
838     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
839                                                  (void**)&tag, &tag_len));
840     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
841                                                  (void*)&tag_len, sizeof(tag_len)));
842
843     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx_ptr.get(), final_ptr.get(), &final_len));
844     encrypt_output.insert(encrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
845
846     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
847                                                  aad.data(), aad.size()));
848     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
849                                                  tag, tag_len));
850     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
851                                            (void*)&tag_len, sizeof(tag_len)));
852     YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
853                                            (void**)&tag, &tag_len));
854     ChrPtr tag_ptr = wrap_ptr(tag);
855
856     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
857                                                  (void*)&tag_len, sizeof(tag_len)));
858
859     // decryption
860     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM,
861                                     key_iv_pair.key, key_iv_pair.iv);
862     update_ptr = out_buf_alloc(dec_ctx_ptr, encrypt_output.size(), update_len);
863     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
864
865     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
866                                                  (void**)&tag, &tag_len));
867     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
868                                                  (void*)&tag_len, sizeof(tag_len)));
869
870     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
871                                            aad.data(), aad.size()));
872
873     YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), encrypt_output.data(), encrypt_output.size(),
874                                      update_ptr.get(), &update_len));
875     decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
876
877     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
878                                                  aad.data(), aad.size()));
879     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
880                                                  (void*)&tag_len, sizeof(tag_len)));
881     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
882                                                  (void**)&tag, &tag_len));
883     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
884                                                  tag, tag_len + 42));
885
886     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG, tag, tag_len));
887
888     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
889     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
890
891     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
892                                                  aad.data(), aad.size()));
893     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
894                                                  tag, tag_len));
895     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG_LEN,
896                                                  (void*)&tag_len, sizeof(tag_len)));
897     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
898                                                  (void**)&tag, &tag_len));
899
900     YACA_ASSERT_MSG(DATA.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
901     YACA_ASSERT_MSG(DATA == decrypt_output, "Text after encrypt-decrypt has changed\n");
902 }
903
904 RUNNER_TEST(T3120_yaca_aes_ccm_call_order_invalid_param, YacaTest)
905 {
906     KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_256BIT);
907     KeyPtr iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_64BIT);
908     size_t tag_len = 16;
909     char *tag;
910     auto aad = random_buffer(16);
911     Buffer encrypt_output;
912     Buffer decrypt_output;
913
914     // encryption
915     auto enc_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
916     size_t update_len = 0;
917     auto update_ptr = out_buf_alloc(enc_ctx_ptr, DATA.size(), update_len);
918     size_t final_len = 0;
919     auto final_ptr = out_buf_alloc(enc_ctx_ptr, 0, final_len);
920
921     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
922                                                  tag, tag_len));
923     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
924                                                  (void**)&tag, &tag_len));
925     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
926                                            (void*)&tag_len, sizeof(tag_len)));
927
928     YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), NULL, DATA.size(), NULL, &update_len));
929     YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
930                                            aad.data(), aad.size()));
931     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
932                                                  (void*)&tag_len, sizeof(tag_len)));
933
934     YACA_SUCCESS(yaca_encrypt_update(enc_ctx_ptr.get(), DATA.data(), DATA.size(),
935                                      update_ptr.get(), &update_len));
936     encrypt_output.insert(encrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
937
938     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
939                                                  aad.data(), aad.size()));
940     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
941                                                  tag, tag_len));
942     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
943                                                  (void*)&tag_len, sizeof(tag_len)));
944     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
945                                                  (void**)&tag, &tag_len));
946
947     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx_ptr.get(), final_ptr.get(), &final_len));
948     encrypt_output.insert(encrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
949
950     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
951                                                  aad.data(), aad.size()));
952     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
953                                                  tag, tag_len));
954     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
955                                                  (void*)&tag_len, sizeof(tag_len)));
956     YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
957                                            (void**)&tag, &tag_len));
958     ChrPtr tag_ptr = wrap_ptr(tag);
959
960     // decryption
961     auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
962     update_ptr = out_buf_alloc(dec_ctx_ptr, encrypt_output.size(), update_len);
963     final_ptr = out_buf_alloc(dec_ctx_ptr, 0, final_len);
964
965     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
966                                                  (void*)&tag_len, sizeof(tag_len)));
967     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
968                                                  (void**)&tag, &tag_len));
969     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
970                                                  tag, tag_len + 42));
971
972     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG, tag, tag_len));
973
974     YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), NULL, encrypt_output.size(),
975                                      NULL, &update_len));
976     YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
977                                            aad.data(), aad.size()));
978     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
979                                                  tag, tag_len));
980
981     YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), encrypt_output.data(), encrypt_output.size(),
982                                      update_ptr.get(), &update_len));
983     decrypt_output.insert(decrypt_output.end(), update_ptr.get(), update_ptr.get() + update_len);
984
985     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
986                                                  aad.data(), aad.size()));
987     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
988                                                  tag, tag_len));
989     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
990                                                  (void*)&tag_len, sizeof(tag_len)));
991     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
992                                                  (void**)&tag, &tag_len));
993
994     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx_ptr.get(), final_ptr.get(), &final_len));
995     decrypt_output.insert(decrypt_output.end(), final_ptr.get(), final_ptr.get() + final_len);
996
997     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_AAD,
998                                                  aad.data(), aad.size()));
999     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
1000                                                  tag, tag_len));
1001     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
1002                                                  (void*)&tag_len, sizeof(tag_len)));
1003     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
1004                                                  (void**)&tag, &tag_len));
1005
1006     YACA_ASSERT_MSG(DATA.size() == decrypt_output.size(), "Size after encrypt-decrypt differs\n");
1007     YACA_ASSERT_MSG(DATA == decrypt_output, "Text after encrypt-decrypt has changed\n");
1008 }
1009
1010 RUNNER_TEST(T3130_yaca_aes_gcm_tag_len_combs_invalid_param, YacaTest)
1011 {
1012     auto tvv = loadTestVector("encrypt_aes_gcm_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_gcm_tag_len(key_len, iv_len, tag_len, valid);
1026     }
1027 }
1028
1029 RUNNER_TEST(T3140_yaca_aes_ccm_tag_len_combs_invalid_param, YacaTest)
1030 {
1031     auto tvv = loadTestVector("encrypt_aes_ccm_tag_len.txt");
1032
1033     for (const auto& tv : tvv) {
1034         size_t key_len;
1035         size_t iv_len;
1036         size_t tag_len;
1037         bool valid;
1038
1039         tv.get("key_len", key_len);
1040         tv.get("iv_len", iv_len);
1041         tv.get("tag_len", tag_len);
1042         tv.get("valid", valid);
1043
1044         test_vector_ccm_tag_len(key_len, iv_len, tag_len, valid);
1045     }
1046 }
1047
1048 RUNNER_TEST(T3150_yaca_aes_gcm_ccm_output_comparison, YacaTest)
1049 {
1050     auto tvv = loadTestVector("encrypt_output_comparison_aes_gcm_ccm.txt");
1051
1052     for (const auto& tv : tvv) {
1053         Buffer input;
1054         yaca_encrypt_algorithm_e algo;
1055         yaca_block_cipher_mode_e bcm;
1056         Buffer key;
1057         Buffer iv;
1058         Buffer aad;
1059         Buffer tag;
1060         Buffer output;
1061
1062         tv.get("input", input);
1063         tv.get("algo", algo);
1064         tv.get("bcm", bcm);
1065         tv.get("key", key);
1066         tv.get("iv", iv);
1067         tv.get("aad", aad);
1068         tv.get("tag", tag);
1069         tv.get("output", output);
1070
1071         bcm == YACA_BCM_GCM ? aes_gcm_test_output(input, algo, bcm, key, iv, aad, tag, output)
1072                             : aes_ccm_test_output(input, algo, bcm, key, iv, aad, tag, output);
1073     }
1074 }
1075
1076 RUNNER_TEST(T3160_yaca_set_get_padding_invalid_param, YacaTest)
1077 {
1078     KeyIvPair key_iv_pair;
1079     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC,
1080                                   key_iv_pair.key, key_iv_pair.iv);
1081
1082     yaca_padding_e padding = YACA_PADDING_NONE;
1083     size_t padding_len;
1084
1085     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), static_cast<yaca_property_e>(-1),
1086                                                  (void*)(&padding), sizeof(padding)));
1087     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_PADDING,
1088                                                  nullptr, sizeof(padding)));
1089     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_PADDING,
1090                                                  (void*)(&padding), 0));
1091     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_PADDING,
1092                                                  (void*)(&padding), sizeof(char)));
1093
1094     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_PADDING,
1095                                                  (void**)(&padding), &padding_len));
1096
1097     size_t output_len = get_output_length(enc_ctx);
1098     Buffer output(output_len);
1099     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx.get(), output.data(), &output_len));
1100     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_PADDING,
1101                                                  (void*)(&padding), sizeof(padding)));
1102
1103     std::vector<yaca_block_cipher_mode_e> bcms = {YACA_BCM_OFB, YACA_BCM_CFB, YACA_BCM_GCM,
1104                                                   YACA_BCM_CTR, YACA_BCM_CBC};
1105     std::vector<yaca_padding_e> paddings = {YACA_PADDING_PKCS1, YACA_PADDING_PKCS1_PSS,
1106                                             YACA_PADDING_X931, YACA_PADDING_PKCS1_SSLV23,
1107                                             YACA_PADDING_PKCS1_OAEP};
1108
1109     for (yaca_block_cipher_mode_e bcm : bcms) {
1110         enc_ctx = encrypt_init(YACA_ENCRYPT_AES, bcm, key_iv_pair.key, key_iv_pair.iv);
1111
1112         for (yaca_padding_e padding : paddings) {
1113             YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_PADDING,
1114                                                          (void*)(&padding), sizeof(padding)));
1115         }
1116     }
1117 }
1118
1119 RUNNER_TEST(T3170_yaca_set_get_rc2_key_bits_invalid_param, YacaTest)
1120 {
1121     size_t effective_key_bits;
1122     size_t effective_key_bits_len;
1123     KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_UNSAFE_128BIT);
1124     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_UNSAFE_RC2, YACA_BCM_ECB, key, null_key());
1125
1126     effective_key_bits = 0;
1127     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(),
1128                                                  YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
1129                                                  (void*)(&effective_key_bits),
1130                                                  sizeof(effective_key_bits)));
1131     effective_key_bits = 1025;
1132     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(),
1133                                                  YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
1134                                                  (void*)(&effective_key_bits),
1135                                                  sizeof(effective_key_bits)));
1136
1137     effective_key_bits = 128;
1138     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(),
1139                                                  YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
1140                                                  (void*)(&effective_key_bits), sizeof(char)));
1141
1142     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(),
1143                                                  YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
1144                                                  (void**)(&effective_key_bits),
1145                                                  &effective_key_bits_len));
1146
1147     size_t output_len = get_output_length(enc_ctx, DATA.size());
1148     Buffer output(output_len);
1149     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), DATA.data(), DATA.size(),
1150                                      output.data(), &output_len));
1151     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(),
1152                                                  YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
1153                                                  (void*)(&effective_key_bits),
1154                                                  sizeof(effective_key_bits)));
1155
1156     std::vector<yaca_encrypt_algorithm_e> algos = {YACA_ENCRYPT_AES, YACA_ENCRYPT_UNSAFE_3DES_2TDEA,
1157                                                    YACA_ENCRYPT_CAST5};
1158
1159     for (yaca_encrypt_algorithm_e algo : algos) {
1160         enc_ctx = encrypt_init(algo, YACA_BCM_ECB, key, null_key());
1161
1162         YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(),
1163                                                      YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS,
1164                                                      (void*)(&effective_key_bits),
1165                                                      sizeof(effective_key_bits)));
1166     }
1167 }
1168
1169 RUNNER_TEST(T3180_yaca_set_get_gcm_properties_invalid_param, YacaTest)
1170 {
1171     KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
1172     KeyPtr iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_128BIT);
1173     KeyPtr gcm_iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_64BIT);
1174
1175     size_t aad_len = 16;
1176     Buffer aad = random_buffer(aad_len);
1177     size_t tag_len = 16;
1178     char *tag;
1179
1180     size_t update_len, final_len;
1181
1182     // CTR
1183     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CTR, key, iv);
1184     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_GCM_AAD,
1185                                                  aad.data(), aad.size()));
1186
1187     Buffer encrypt_output;
1188     ChrPtr update = out_buf_alloc(enc_ctx, DATA.size(), update_len);
1189     ChrPtr final = out_buf_alloc(enc_ctx, 0, final_len);
1190     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), DATA.data(), DATA.size(),
1191                                      update.get(), &update_len));
1192     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx.get(), final.get(), &final_len));
1193     encrypt_output.insert(encrypt_output.end(), update.get(), update.get() + update_len);
1194     encrypt_output.insert(encrypt_output.end(), final.get(), final.get() + final_len);
1195
1196     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG_LEN,
1197                                                  (void*)(&tag_len), sizeof(tag_len)));
1198     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG,
1199                                                  (void**)&tag, &tag_len));
1200
1201     CtxPtr dec_ctx = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CTR, key, iv);
1202     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_AAD,
1203                                                  aad.data(), aad.size()));
1204
1205     update = out_buf_alloc(dec_ctx, encrypt_output.size(), update_len);
1206     final = out_buf_alloc(dec_ctx, 0, final_len);
1207
1208     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1209                                      update.get(), &update_len));
1210
1211     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_TAG,
1212                                                  tag, tag_len));
1213
1214     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx.get(), final.get(), &final_len));
1215
1216     // GCM
1217     enc_ctx = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key, gcm_iv);
1218     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_CCM_AAD,
1219                                                  aad.data(), aad.size()));
1220     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_GCM_AAD,
1221                                                  (void**)aad.data(), &aad_len));
1222
1223     encrypt_output.clear();
1224     update = out_buf_alloc(enc_ctx, DATA.size(), update_len);
1225     final = out_buf_alloc(enc_ctx, 0, final_len);
1226     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), DATA.data(), DATA.size(),
1227                                      update.get(), &update_len));
1228     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx.get(), final.get(), &final_len));
1229     encrypt_output.insert(encrypt_output.end(), update.get(), update.get() + update_len);
1230     encrypt_output.insert(encrypt_output.end(), final.get(), final.get() + final_len);
1231
1232     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG_LEN,
1233                                                  (void*)(&tag_len), sizeof(tag_len)));
1234     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG_LEN,
1235                                                  (void*)(&tag_len), sizeof(char)));
1236
1237     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG,
1238                                                  (void**)&tag, &tag_len));
1239     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG,
1240                                                  nullptr, &tag_len));
1241     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG,
1242                                                  (void**)&tag, nullptr));
1243
1244     YACA_SUCCESS(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG,
1245                                            (void**)&tag, &tag_len));
1246     ChrPtr tag_ptr = wrap_ptr(tag);
1247
1248     dec_ctx = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key, gcm_iv);
1249     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_AAD,
1250                                                  aad.data(), aad.size()));
1251     YACA_INVALID_PARAM(yaca_context_get_property(dec_ctx.get(), YACA_PROPERTY_GCM_AAD,
1252                                                  (void**)aad.data(), &aad_len));
1253
1254     update = out_buf_alloc(dec_ctx, encrypt_output.size(), update_len);
1255     final = out_buf_alloc(dec_ctx, 0, final_len);
1256
1257     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1258                                      update.get(), &update_len));
1259
1260     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_TAG,
1261                                                  tag, tag_len));
1262
1263     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_TAG, tag, tag_len));
1264
1265     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx.get(), final.get(), &final_len));
1266 }
1267
1268 RUNNER_TEST(T3190_yaca_set_get_ccm_properties_invalid_param, YacaTest)
1269 {
1270     KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
1271     KeyPtr iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_64BIT);
1272
1273     size_t aad_len = 16;
1274     Buffer aad = random_buffer(aad_len);
1275     size_t tag_len = 12;
1276     char *tag;
1277
1278     size_t update_len, final_len;
1279
1280     // CFB
1281     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_CFB, key, iv);
1282     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG_LEN,
1283                                                  (void*)(&tag_len), sizeof(tag_len)));
1284
1285     YACA_INVALID_PARAM(yaca_encrypt_update(enc_ctx.get(), NULL, DATA.size(), NULL, &update_len));
1286     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_CCM_AAD,
1287                                                  aad.data(), aad.size()));
1288
1289     Buffer encrypt_output;
1290     ChrPtr update = out_buf_alloc(enc_ctx, DATA.size(), update_len);
1291     ChrPtr final = out_buf_alloc(enc_ctx, 0, final_len);
1292     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), DATA.data(), DATA.size(),
1293                                      update.get(), &update_len));
1294     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx.get(), final.get(), &final_len));
1295     encrypt_output.insert(encrypt_output.end(), update.get(), update.get() + update_len);
1296     encrypt_output.insert(encrypt_output.end(), final.get(), final.get() + final_len);
1297
1298     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG,
1299                                                  (void**)&tag, &tag_len));
1300
1301     CtxPtr dec_ctx = decrypt_init(YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_CFB, key, iv);
1302     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_TAG,
1303                                                  tag, tag_len));
1304     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), NULL, encrypt_output.size(), NULL,
1305                                            &update_len));
1306     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_AAD,
1307                                                  aad.data(), aad.size()));
1308
1309     update = out_buf_alloc(dec_ctx, encrypt_output.size(), update_len);
1310     final = out_buf_alloc(dec_ctx, 0, final_len);
1311
1312     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1313                                      update.get(), &update_len));
1314     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx.get(), final.get(), &final_len));
1315
1316     // CCM
1317     enc_ctx = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
1318     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG_LEN,
1319                                                  (void*)(&tag_len), sizeof(tag_len)));
1320     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG_LEN,
1321                                                  (void*)(&tag_len), sizeof(char)));
1322
1323     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), NULL, DATA.size(), NULL, &update_len));
1324     YACA_INVALID_PARAM(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_GCM_AAD,
1325                                                  aad.data(), aad.size()));
1326     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_CCM_AAD,
1327                                                  (void**)aad.data(), &aad_len));
1328
1329     YACA_SUCCESS(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_CCM_AAD,
1330                                            aad.data(), aad.size()));
1331
1332     encrypt_output.clear();
1333     update = out_buf_alloc(enc_ctx, DATA.size(), update_len);
1334     final = out_buf_alloc(enc_ctx, 0, final_len);
1335     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), DATA.data(), DATA.size(),
1336                                      update.get(), &update_len));
1337     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx.get(), final.get(), &final_len));
1338     encrypt_output.insert(encrypt_output.end(), update.get(), update.get() + update_len);
1339     encrypt_output.insert(encrypt_output.end(), final.get(), final.get() + final_len);
1340
1341     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG,
1342                                                  (void**)&tag, &tag_len));
1343     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG,
1344                                                  nullptr, &tag_len));
1345     YACA_INVALID_PARAM(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG,
1346                                                  (void**)&tag, nullptr));
1347
1348     YACA_SUCCESS(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG,
1349                                            (void**)&tag, &tag_len));
1350     ChrPtr tag_ptr = wrap_ptr(tag);
1351
1352     dec_ctx = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
1353     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_TAG,
1354                                                  tag, tag_len));
1355
1356     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_TAG, tag, tag_len));
1357
1358     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), NULL, encrypt_output.size(), NULL,
1359                                      &update_len));
1360     YACA_INVALID_PARAM(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_AAD,
1361                                                  aad.data(), aad.size()));
1362
1363     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_AAD,
1364                                            aad.data(), aad.size()));
1365
1366     update = out_buf_alloc(dec_ctx, encrypt_output.size(), update_len);
1367     final = out_buf_alloc(dec_ctx, 0, final_len);
1368     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1369                                      update.get(), &update_len));
1370     YACA_SUCCESS(yaca_decrypt_finalize(dec_ctx.get(), final.get(), &final_len));
1371 }
1372
1373 RUNNER_TEST(T3200_yaca_set_invalid_gcm_tag_aad, YacaTest)
1374 {
1375     KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
1376     KeyPtr iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_64BIT);
1377
1378     size_t aad_len = 16;
1379     Buffer aad = random_buffer(aad_len);
1380     size_t tag_len = 16;
1381     char *tag;
1382
1383     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key, iv);
1384
1385     Buffer encrypt_output;
1386     size_t update_len, final_len;
1387     ChrPtr update = out_buf_alloc(enc_ctx, DATA.size(), update_len);
1388     ChrPtr final = out_buf_alloc(enc_ctx, 0, final_len);
1389
1390     YACA_SUCCESS(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_GCM_AAD,
1391                                            aad.data(), aad.size()));
1392     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), DATA.data(), DATA.size(),
1393                                      update.get(), &update_len));
1394     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx.get(), final.get(), &final_len));
1395     YACA_SUCCESS(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_GCM_TAG,
1396                                            (void**)&tag, &tag_len));
1397     ChrPtr tag_ptr = wrap_ptr(tag);
1398
1399     encrypt_output.insert(encrypt_output.end(), update.get(), update.get() + update_len);
1400     encrypt_output.insert(encrypt_output.end(), final.get(), final.get() + final_len);
1401
1402     CtxPtr dec_ctx = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key, iv);
1403     update = out_buf_alloc(dec_ctx, encrypt_output.size(), update_len);
1404     final = out_buf_alloc(dec_ctx, 0, final_len);
1405
1406     // Invalid AAD
1407     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_AAD, tag, aad.size()));
1408
1409     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1410                                      update.get(), &update_len));
1411
1412     // Valid TAG
1413     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_TAG, tag, tag_len));
1414
1415     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx.get(), final.get(), &final_len));
1416
1417     dec_ctx = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key, iv);
1418
1419     // Valid AAD
1420     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_AAD,
1421                                            aad.data(), aad.size()));
1422
1423     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1424                                      update.get(), &update_len));
1425
1426     // Invalid TAG
1427     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_GCM_TAG,
1428                                            aad.data(), tag_len));
1429
1430     YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx.get(), final.get(), &final_len));
1431 }
1432
1433 RUNNER_TEST(T3210_yaca_set_invalid_ccm_tag_aad, YacaTest)
1434 {
1435     KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT);
1436     KeyPtr iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_64BIT);
1437
1438     size_t aad_len = 16;
1439     Buffer aad = random_buffer(aad_len);
1440     size_t tag_len = 12;
1441     char *tag;
1442
1443     CtxPtr enc_ctx = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
1444
1445     Buffer encrypt_output;
1446     size_t update_len, final_len;
1447     ChrPtr update = out_buf_alloc(enc_ctx, DATA.size(), update_len);
1448     ChrPtr final = out_buf_alloc(enc_ctx, 0, final_len);
1449
1450     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), NULL, DATA.size(), NULL, &update_len));
1451     YACA_SUCCESS(yaca_context_set_property(enc_ctx.get(), YACA_PROPERTY_CCM_AAD,
1452                                            aad.data(), aad.size()));
1453     YACA_SUCCESS(yaca_encrypt_update(enc_ctx.get(), DATA.data(), DATA.size(),
1454                                      update.get(), &update_len));
1455     YACA_SUCCESS(yaca_encrypt_finalize(enc_ctx.get(), final.get(), &final_len));
1456     YACA_SUCCESS(yaca_context_get_property(enc_ctx.get(), YACA_PROPERTY_CCM_TAG,
1457                                            (void**)&tag, &tag_len));
1458     ChrPtr tag_ptr = wrap_ptr(tag);
1459
1460     encrypt_output.insert(encrypt_output.end(), update.get(), update.get() + update_len);
1461     encrypt_output.insert(encrypt_output.end(), final.get(), final.get() + final_len);
1462
1463     CtxPtr dec_ctx = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
1464     update = out_buf_alloc(dec_ctx, encrypt_output.size(), update_len);
1465     final = out_buf_alloc(dec_ctx, 0, final_len);
1466
1467     // Invalid TAG
1468     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_TAG,
1469                                            aad.data(), tag_len));
1470
1471     // Valid AAD
1472     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), NULL, encrypt_output.size(), NULL,
1473                                      &update_len));
1474     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_AAD,
1475                                            aad.data(), aad.size()));
1476
1477     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1478                                            update.get(), &update_len));
1479
1480     dec_ctx = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv);
1481
1482     // Valid TAG
1483     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_TAG, tag, tag_len));
1484
1485     // Invalid AAD
1486     YACA_SUCCESS(yaca_decrypt_update(dec_ctx.get(), NULL, encrypt_output.size(), NULL,
1487                                      &update_len));
1488     YACA_SUCCESS(yaca_context_set_property(dec_ctx.get(), YACA_PROPERTY_CCM_AAD,
1489                                            tag, aad.size()));
1490
1491     YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx.get(), encrypt_output.data(), encrypt_output.size(),
1492                                            update.get(), &update_len));
1493 }