2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Dariusz Michaluk (d.michaluk@samsung.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * @author Dariusz Michaluk (d.michaluk@samsung.com)
22 * @author Mateusz Forc (m.forc@samsung.com)
25 #include "dpl/test/test_runner.h"
26 #include "yaca-test-common.h"
27 #include "yaca-test-vector.h"
29 #include <yaca_encrypt.h>
34 const Buffer DATA = random_buffer(1024);
35 const size_t INVALID_IV_BIT_LENGTH = 512;
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))
47 yaca_key_type_e algo_to_key_type(yaca_encrypt_algorithm_e algo)
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;
54 return YACA_KEY_TYPE_SYMMETRIC;
59 Encryptor(yaca_encrypt_algorithm_e algo,
60 yaca_block_cipher_mode_e bcm,
61 const KeyPtr& key_ptr,
65 m_enCtxPtr(encrypt_init(algo, bcm, key_ptr, iv_ptr)),
66 m_decCtxPtr(decrypt_init(algo, bcm, key_ptr, iv_ptr))
69 set_padding(YACA_PADDING_NONE);
71 set_effective_key_bits(key_bits);
74 virtual Buffer encrypt(const Buffer& data, size_t repeats = 1)
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());
82 virtual Buffer decrypt(const Buffer& data, size_t repeats = 1)
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());
90 void set_padding(yaca_padding_e padding)
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)));
98 void set_effective_key_bits(size_t key_bits)
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)));
106 int set_tag_len(yaca_block_cipher_mode_e bcm, size_t tag_len)
110 return yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_GCM_TAG_LEN,
111 (void*)&tag_len, sizeof(tag_len));
113 return yaca_context_set_property(m_enCtxPtr.get(), YACA_PROPERTY_CCM_TAG_LEN,
114 (void*)&tag_len, sizeof(tag_len));
116 return YACA_ERROR_INVALID_PARAMETER;
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;
127 Buffer run_update(const Buffer& data, size_t repeats, CtxPtr* ctx_ptr, Update update)
132 auto text_ptr = out_buf_alloc(*ctx_ptr, data.size(), len);
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);
143 Buffer run_finalize(CtxPtr* ctx_ptr, Final finalize)
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);
156 std::string bin2hex(const Buffer &bin_str)
158 std::stringstream data;
160 for (const auto& i : bin_str)
161 data << std::hex << static_cast<int>(static_cast<unsigned char>(i));
166 void check_test_vector_init(yaca_encrypt_algorithm_e algo,
167 yaca_block_cipher_mode_e bcm,
172 yaca_context_h ctx = YACA_CONTEXT_NULL;
173 int expected = YACA_ERROR_NONE;
174 yaca_key_type_e key_type;
177 expected = YACA_ERROR_INVALID_PARAMETER;
179 auto iv_ptr = (iv_len > 0) ? generate_key(YACA_KEY_TYPE_IV, iv_len)
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
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;
192 key_type = YACA_KEY_TYPE_SYMMETRIC;
194 auto key_ptr = generate_key(key_type, key_len);
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);
203 void check_test_vector_output(const Buffer &input,
204 yaca_encrypt_algorithm_e algo,
205 yaca_block_cipher_mode_e bcm,
210 const Buffer &expected,
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())
217 Encryptor encryptor(algo, bcm, key_ptr, iv_ptr, key_bits, padded);
218 Buffer output_vector = encryptor.encrypt(input, repeats);
220 auto keyHex = bin2hex(key);
221 auto ivHex = bin2hex(iv);
222 std::stringstream message;
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;
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());
236 void test_encryption_output(std::string filename, bool padded=true)
238 auto tvv = loadTestVector(filename);
240 for (const auto& tv : tvv) {
242 yaca_encrypt_algorithm_e algo;
243 yaca_block_cipher_mode_e bcm;
250 tv.get("input", input);
251 tv.get("algo", algo);
254 if (algo == YACA_ENCRYPT_UNSAFE_RC2)
255 tv.get("effective_key_bits", key_bits);
257 tv.get("repeats", repeats);
258 tv.get("output", expected);
260 check_test_vector_output(input, algo, bcm, key, key_bits, iv, repeats, expected, padded);
264 void test_vector_encrypt_decrypt(const Buffer &input,
265 yaca_encrypt_algorithm_e algo,
266 yaca_block_cipher_mode_e bcm,
270 std::stringstream message;
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)
275 Encryptor encryptor(algo, bcm, key_ptr, iv_ptr);
277 auto encrypt_output = encryptor.encrypt(input);
278 auto decrypt_output = encryptor.decrypt(encrypt_output);
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;
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"
291 void test_encryption_decryption(std::string filename)
293 std::string s = "abcdefghijklmnoprstuvwxyz0123456789";
295 auto tvv = loadTestVector(filename);
297 for (const auto& tv : tvv) {
298 yaca_encrypt_algorithm_e algo;
299 yaca_block_cipher_mode_e bcm;
302 size_t key_data_len = 0;
305 tv.get("algo", algo);
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);
312 if (key_data_len > 0)
313 input = random_buffer(key_data_len / 8);
315 std::copy(s.begin(), s.end(), std::back_inserter(input));
317 test_vector_encrypt_decrypt(input, algo, bcm, key_len, iv_len);
321 void test_vector_gcm_tag_len(size_t key_len,
326 auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
327 auto iv_ptr = generate_key(YACA_KEY_TYPE_IV, iv_len);
329 int expected = YACA_ERROR_NONE;
331 expected = YACA_ERROR_INVALID_PARAMETER;
333 Encryptor encryptor(YACA_ENCRYPT_AES, YACA_BCM_GCM, key_ptr, iv_ptr);
334 encryptor.encrypt(DATA);
336 YACA_RESULT(expected, encryptor.set_tag_len(YACA_BCM_GCM, tag_len));
339 void test_vector_ccm_tag_len(size_t key_len,
344 auto key_ptr = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
345 auto iv_ptr = generate_key(YACA_KEY_TYPE_IV, iv_len);
347 int expected = YACA_ERROR_NONE;
349 expected = YACA_ERROR_INVALID_PARAMETER;
351 Encryptor encryptor(YACA_ENCRYPT_AES, YACA_BCM_CCM, key_ptr, iv_ptr);
353 YACA_RESULT(expected, encryptor.set_tag_len(YACA_BCM_CCM, tag_len));
356 void aes_gcm_test_output(const Buffer &input,
357 yaca_encrypt_algorithm_e algo,
358 yaca_block_cipher_mode_e bcm,
363 const Buffer &output)
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())
369 size_t tag_len = tag.size();
370 auto tag_output = create_yaca_buffer(tag_len);
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;
379 YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
380 aad.data(), aad.size()));
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);
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);
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));
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");
405 Buffer decrypt_output;
406 auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_GCM, key_ptr, iv_ptr);
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);
418 YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
419 tag.data(), tag.size()));
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);
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");
429 void aes_ccm_test_output(const Buffer &input,
430 yaca_encrypt_algorithm_e algo,
431 yaca_block_cipher_mode_e bcm,
436 const Buffer &output)
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())
442 size_t tag_len = tag.size();
443 auto tag_output = create_yaca_buffer(tag_len);
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;
451 YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG_LEN,
452 (void*)&tag_len, sizeof(tag_len)));
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()));
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);
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);
471 YACA_SUCCESS(yaca_context_get_property(enc_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
472 (void**)tag_output.get(), &tag_len));
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");
481 Buffer decrypt_output;
482 auto dec_ctx_ptr = decrypt_init(algo, bcm, key_ptr, iv_ptr);
484 YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
485 tag.data(), tag.size()));
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()));
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);
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);
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");
508 }//namespace anonymous
510 RUNNER_TEST_GROUP_INIT(T3000_YACA_ENCRYPT);
512 RUNNER_TEST(T3010_yaca_encrypt_init_invalid_param, YacaTest)
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);
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()));
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()));
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()));
531 YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
532 key_iv_pair.iv.get()));
534 YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
535 asym_key_ptr.get(), key_iv_pair.iv.get()));
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()));
540 YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
541 key_iv_pair.key.get(), YACA_KEY_NULL));
543 YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
544 key_iv_pair.key.get(), bad_iv_ptr.get()));
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()));
549 YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
550 des_key_ptr.get(), key_iv_pair.iv.get()));
553 RUNNER_TEST(T3020_yaca_encrypt_update_invalid_param, YacaTest)
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);
561 YACA_INVALID_PARAM(yaca_encrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
562 ciphertext_ptr.get(), &ciphertext_len));
564 YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, DATA.size(),
565 ciphertext_ptr.get(), &ciphertext_len));
567 YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, 0,
568 ciphertext_ptr.get(), &ciphertext_len));
570 YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), 0,
571 ciphertext_ptr.get(), &ciphertext_len));
573 YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
574 nullptr, &ciphertext_len));
576 YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
577 ciphertext_ptr.get(), nullptr));
579 YACA_INVALID_PARAM(yaca_encrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
580 ciphertext_ptr.get(), &ciphertext_len));
583 RUNNER_TEST(T3030_yaca_encrypt_final_invalid_param, YacaTest)
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);
591 YACA_INVALID_PARAM(yaca_encrypt_finalize(YACA_CONTEXT_NULL, ciphertext_ptr.get(),
594 YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), nullptr, &ciphertext_len));
596 YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), ciphertext_ptr.get(), nullptr));
598 YACA_INVALID_PARAM(yaca_encrypt_finalize(inv_ctx_ptr.get(), ciphertext_ptr.get(),
602 RUNNER_TEST(T3040_yaca_decrypt_init_invalid_param, YacaTest)
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);
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()));
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()));
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()));
621 YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
622 key_iv_pair.iv.get()));
624 YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
625 asym_key_ptr.get(), key_iv_pair.iv.get()));
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()));
630 YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
631 key_iv_pair.key.get(), YACA_KEY_NULL));
633 YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
634 key_iv_pair.key.get(), bad_iv_ptr.get()));
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()));
639 YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
640 des_key_ptr.get(), key_iv_pair.iv.get()));
643 RUNNER_TEST(T3050_yaca_decrypt_update_invalid_param, YacaTest)
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);
651 YACA_INVALID_PARAM(yaca_decrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
652 nullptr, &plaintext_len));
654 YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, DATA.size(),
655 plaintext_ptr.get(), &plaintext_len));
657 YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), 0,
658 plaintext_ptr.get(), &plaintext_len));
660 YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, 0,
661 plaintext_ptr.get(), &plaintext_len));
663 YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
664 nullptr, &plaintext_len));
666 YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
667 plaintext_ptr.get(), nullptr));
669 YACA_INVALID_PARAM(yaca_decrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
670 plaintext_ptr.get(), &plaintext_len));
673 RUNNER_TEST(T3060_yaca_decrypt_final_invalid_param, YacaTest)
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);
681 YACA_INVALID_PARAM(yaca_decrypt_finalize(YACA_CONTEXT_NULL, plaintext_ptr.get(),
684 YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), nullptr, &plaintext_len));
686 YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), plaintext_ptr.get(), nullptr));
688 YACA_INVALID_PARAM(yaca_decrypt_finalize(inv_ctx_ptr.get(), plaintext_ptr.get(),
692 RUNNER_TEST(T3070_yaca_get_iv_bits_invalid_param, YacaTest)
694 const size_t KEY_LEN = 256;
696 const size_t INVALID_KEY_BIT_LENGTH = 512;
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));
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));
705 YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
706 INVALID_KEY_BIT_LENGTH, &iv_bit_len));
708 YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
712 RUNNER_TEST(T3075_yaca_key_wrap_unwrap_invalid_param, YacaTest)
714 size_t wrapped_len = 0;
715 size_t unwrapped_len = 0;
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);
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);
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);
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));
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));
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));
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));
750 RUNNER_TEST(T3080_yaca_encrypt_decrypt_init_param_comb, YacaTest)
752 auto tvv = loadTestVector("encrypt_param_comb.txt");
754 for (const auto& tv : tvv) {
755 yaca_encrypt_algorithm_e algo;
756 yaca_block_cipher_mode_e bcm;
761 tv.get("algo", algo);
763 tv.get("key_len", key_len);
764 tv.get("iv_len", iv_len);
765 tv.get("valid", valid);
767 check_test_vector_init(algo, bcm, key_len, iv_len, valid);
771 RUNNER_TEST(T3090_yaca_encrypt_update_finalize_output_comp, YacaTest)
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");
779 RUNNER_TEST(T3100_yaca_encrypt_decrypt_comparison, YacaTest)
781 test_encryption_decryption("encrypt_valid_param.txt");
782 test_encryption_decryption("encrypt_valid_param_wrap.txt");
785 RUNNER_TEST(T3110_yaca_aes_gcm_call_order_invalid_param, YacaTest)
787 KeyIvPair key_iv_pair;
789 auto tag = create_yaca_buffer(tag_len);
790 auto aad = random_buffer(16);
791 Buffer encrypt_output;
792 Buffer decrypt_output;
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);
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)));
809 YACA_SUCCESS(yaca_context_set_property(enc_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
810 aad.data(), aad.size()));
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);
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)));
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);
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)));
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);
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)));
850 YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_AAD,
851 aad.data(), aad.size()));
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);
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));
866 YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_GCM_TAG,
867 tag.get(), tag_len));
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);
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));
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");
885 RUNNER_TEST(T3120_yaca_aes_ccm_call_order_invalid_param, YacaTest)
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);
890 auto tag = create_yaca_buffer(tag_len);
891 auto aad = random_buffer(16);
892 Buffer encrypt_output;
893 Buffer decrypt_output;
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);
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)));
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)));
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);
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));
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);
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));
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);
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));
952 YACA_SUCCESS(yaca_context_set_property(dec_ctx_ptr.get(), YACA_PROPERTY_CCM_TAG,
953 tag.get(), tag_len));
955 YACA_SUCCESS(yaca_decrypt_update(dec_ctx_ptr.get(), NULL, encrypt_output.size(),
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));
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);
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));
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);
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));
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");
991 RUNNER_TEST(T3130_yaca_aes_gcm_tag_len_combs_invalid_param, YacaTest)
993 auto tvv = loadTestVector("encrypt_aes_gcm_tag_len.txt");
995 for (const auto& tv : tvv) {
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);
1006 test_vector_gcm_tag_len(key_len, iv_len, tag_len, valid);
1010 RUNNER_TEST(T3140_yaca_aes_ccm_tag_len_combs_invalid_param, YacaTest)
1012 auto tvv = loadTestVector("encrypt_aes_ccm_tag_len.txt");
1014 for (const auto& tv : tvv) {
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);
1025 test_vector_ccm_tag_len(key_len, iv_len, tag_len, valid);
1029 RUNNER_TEST(T3150_yaca_aes_gcm_ccm_output_comparison, YacaTest)
1031 auto tvv = loadTestVector("encrypt_output_comparison_aes_gcm_ccm.txt");
1033 for (const auto& tv : tvv) {
1035 yaca_encrypt_algorithm_e algo;
1036 yaca_block_cipher_mode_e bcm;
1043 tv.get("input", input);
1044 tv.get("algo", algo);
1050 tv.get("output", output);
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);