return wrap_ptr(signature);
}
+ChrPtr simple_encrypt(yaca_encrypt_algorithm_e algo,
+ yaca_block_cipher_mode_e bcm,
+ const KeyPtr &key,
+ const KeyPtr &iv,
+ const char *plaintext,
+ size_t plaintext_len,
+ size_t *ciphertext_len)
+{
+ char *ciphertext;
+
+ YACA_SUCCESS(yaca_simple_encrypt(algo, bcm, key.get(), iv.get(), plaintext, plaintext_len,
+ &ciphertext, ciphertext_len));
+
+ return wrap_ptr(ciphertext);
+}
+
+ChrPtr simple_decrypt(yaca_encrypt_algorithm_e algo,
+ yaca_block_cipher_mode_e bcm,
+ const KeyPtr &key,
+ const KeyPtr &iv,
+ const char *ciphertext,
+ size_t ciphertext_len,
+ size_t *plaintext_len)
+{
+ char *plaintext;
+
+ YACA_SUCCESS(yaca_simple_decrypt(algo, bcm, key.get(), iv.get(), ciphertext, ciphertext_len,
+ &plaintext, plaintext_len));
+
+ return wrap_ptr(plaintext);
+}
+
CtxPtr encrypt_init(yaca_encrypt_algorithm_e algo,
yaca_block_cipher_mode_e bcm,
const KeyPtr &sym_key,
" is different than expected");
}
+void test_vector_simple_encrypt_decrypt(const Buffer &input,
+ yaca_encrypt_algorithm_e algo,
+ yaca_block_cipher_mode_e bcm,
+ size_t key_len,
+ size_t iv_len)
+{
+ KeyPtr key = generate_key(YACA_KEY_TYPE_SYMMETRIC, key_len);
+ KeyPtr iv = (iv_len > 0) ? generate_key(YACA_KEY_TYPE_IV, iv_len)
+ : null_key();
+
+ size_t ciphertext_len = 0;
+ size_t plaintext_len = 0;
+ ChrPtr ciphertext = simple_encrypt(algo, bcm, key, iv, input.data(), input.size(), &ciphertext_len);
+ ChrPtr plaintext = simple_decrypt(algo, bcm, key, iv, ciphertext.get(), ciphertext_len, &plaintext_len);
+
+ YACA_ASSERT_MSG(input.size() == plaintext_len, "Size after encrypt-decrypt differs\n");
+ YACA_ASSERT_MSG(yaca_memcmp(plaintext.get(), input.data(), plaintext_len) == YACA_ERROR_NONE,
+ "Text after encrypt-decrypt has changed\n");
+}
+
+void test_vector_simple_encrypt_decrypt_output(const Buffer &input,
+ yaca_encrypt_algorithm_e algo,
+ yaca_block_cipher_mode_e bcm,
+ const Buffer &key,
+ const Buffer &iv,
+ size_t repeats,
+ const Buffer &expected)
+{
+ Buffer input_repeated;
+ for (size_t i = 0; i < repeats; ++i)
+ std::copy(input.begin(), input.end(), std::back_inserter(input_repeated));
+
+ KeyPtr key_ptr = import_key(YACA_KEY_TYPE_SYMMETRIC, nullptr, key.data(), key.size());
+ KeyPtr iv_ptr = (iv.size() > 0) ? import_key(YACA_KEY_TYPE_IV, nullptr, iv.data(), iv.size())
+ : null_key();
+
+ size_t ciphertext_len = 0;
+ ChrPtr ciphertext = simple_encrypt(algo, bcm, key_ptr, iv_ptr,
+ input_repeated.data(), input_repeated.size(), &ciphertext_len);
+
+ YACA_ASSERT_MSG(expected.size() == ciphertext_len, "Size after encrypt differs\n");
+ YACA_ASSERT_MSG(yaca_memcmp(ciphertext.get(), expected.data(), ciphertext_len) == YACA_ERROR_NONE,
+ "Text after encrypt differs\n");
+}
+
+void test_simple_encryption_output(std::string filename)
+{
+ auto tvv = loadTestVector(filename);
+
+ for (const auto& tv : tvv) {
+ Buffer input;
+ yaca_encrypt_algorithm_e algo;
+ yaca_block_cipher_mode_e bcm;
+ Buffer key;
+ Buffer iv;
+ size_t repeats;
+ Buffer expected;
+
+ tv.get("input", input);
+ tv.get("algo", algo);
+ tv.get("bcm", bcm);
+ tv.get("key", key);
+ tv.get("iv", iv);
+ tv.get("repeats", repeats);
+ tv.get("output", expected);
+
+ test_vector_simple_encrypt_decrypt_output(input, algo, bcm, key, iv, repeats, expected);
+ }
+}
+
+void test_simple_encryption_decryption(std::string filename)
+{
+ Buffer input;
+ std::string s = "abcdefghijklmnoprstuvwxyz0123456789";
+
+ auto tvv = loadTestVector(filename);
+
+ for (const auto& tv : tvv) {
+ yaca_encrypt_algorithm_e algo;
+ yaca_block_cipher_mode_e bcm;
+ size_t key_len;
+ size_t iv_len;
+ size_t key_data_len = 0;
+
+ tv.get("algo", algo);
+ tv.get("bcm", bcm);
+ tv.get("key_len", key_len);
+ tv.get("iv_len", iv_len);
+ if (bcm == YACA_BCM_WRAP)
+ tv.get("key_data_len", key_data_len);
+
+ if (key_data_len > 0)
+ input = random_buffer(key_data_len / 8);
+ else
+ std::copy(s.begin(), s.end(), std::back_inserter(input));
+
+ test_vector_simple_encrypt_decrypt(input, algo, bcm, key_len, iv_len);
+ }
+}
+
} // anonymous namespace
YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), iv.get(),
ciphertext.data(), ciphertext.size(), &plaintext, nullptr));
}
+
+RUNNER_TEST(T7150_yaca_simple_encrypt_decrypt_output, YacaTest)
+{
+ test_simple_encryption_output("encrypt_output_comparison.txt");
+ test_simple_encryption_output("encrypt_output_comparison_rc4.txt");
+ test_simple_encryption_output("encrypt_output_comparison_wrap.txt");
+}
+
+RUNNER_TEST(T7160_yaca_simple_encrypt_decrypt_comparison, YacaTest)
+{
+ test_simple_encryption_decryption("encrypt_valid_param.txt");
+ test_simple_encryption_decryption("encrypt_valid_param_wrap.txt");
+}