/**
* @file
* @author Dariusz Michaluk (d.michaluk@samsumg.com)
+ * @author Mateusz Forc (m.forc@samsung.com)
*/
#include "dpl/test/test_runner.h"
-
+#include "yaca-test-common.h"
#include <yaca_encrypt.h>
+#include <yaca_key.h>
+
+namespace {
+
+const Buffer DATA = random_buffer(1024);
+const size_t INVALID_IV_BIT_LENGTH = 512;
+
+struct KeyIvPair {
+public:
+ KeyPtr key;
+ KeyPtr iv;
+ KeyIvPair() : key(generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT)),
+ iv(generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_128BIT))
+ {}
+};
+
+ChrPtr out_buf_alloc(const CtxPtr& ctx_ptr, size_t input_len, size_t &out_len)
+{
+ out_len = get_output_length(ctx_ptr, input_len);
+ return create_yaca_buffer(out_len);
+}
+
+}//namespace anonymous
RUNNER_TEST_GROUP_INIT(T3000_YACA_ENCRYPT);
-RUNNER_TEST(T3001_yaca_encrypt_init)
+RUNNER_TEST(T3010_yaca_encrypt_init_invalid_param, YacaTest)
{
+ yaca_context_h ctx;
+ KeyIvPair key_iv_pair;
+ auto asym_key_ptr = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT);
+ auto bad_iv_ptr = generate_key(YACA_KEY_TYPE_IV, INVALID_IV_BIT_LENGTH);
+ auto des_key_ptr = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_64BIT);
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(nullptr, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ key_iv_pair.key.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, static_cast<yaca_encrypt_algorithm_e>(-1),
+ YACA_BCM_CBC, key_iv_pair.key.get(),
+ key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES,
+ static_cast<yaca_block_cipher_mode_e>(-1),
+ key_iv_pair.key.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
+ key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ asym_key_ptr.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_ECB,
+ des_key_ptr.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ key_iv_pair.key.get(), YACA_KEY_NULL));
+
+ YACA_INVALID_PARAM(yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ key_iv_pair.key.get(), bad_iv_ptr.get()));
}
-RUNNER_TEST(T3002_yaca_encrypt_update)
+
+RUNNER_TEST(T3020_yaca_encrypt_update_invalid_param, YacaTest)
{
+ size_t ciphertext_len = 0;
+ KeyIvPair key_iv_pair;
+ auto en_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+ auto inv_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+ auto ciphertext_ptr = out_buf_alloc(en_ctx_ptr, DATA.size(), ciphertext_len);
+
+ YACA_INVALID_PARAM(yaca_encrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
+ ciphertext_ptr.get(), &ciphertext_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), nullptr, DATA.size(),
+ ciphertext_ptr.get(), &ciphertext_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), 0,
+ ciphertext_ptr.get(), &ciphertext_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
+ nullptr, &ciphertext_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_update(en_ctx_ptr.get(), DATA.data(), DATA.size(),
+ ciphertext_ptr.get(), nullptr));
+
+ YACA_INVALID_PARAM(yaca_encrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
+ ciphertext_ptr.get(), &ciphertext_len));
}
-RUNNER_TEST(T3003_yaca_encrypt_final)
+RUNNER_TEST(T3030_yaca_encrypt_final_invalid_param, YacaTest)
{
+ size_t ciphertext_len = 0;
+ KeyIvPair key_iv_pair;
+ auto en_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+ auto ciphertext_ptr = out_buf_alloc(en_ctx_ptr, DATA.size(), ciphertext_len);
+ auto inv_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+
+ YACA_INVALID_PARAM(yaca_encrypt_finalize(YACA_CONTEXT_NULL, ciphertext_ptr.get(),
+ &ciphertext_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), nullptr, &ciphertext_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_finalize(en_ctx_ptr.get(), ciphertext_ptr.get(), nullptr));
+
+ YACA_INVALID_PARAM(yaca_encrypt_finalize(inv_ctx_ptr.get(), ciphertext_ptr.get(),
+ &ciphertext_len));
}
-RUNNER_TEST(T3004_yaca_decrypt_init)
+RUNNER_TEST(T3040_yaca_decrypt_init_invalid_param, YacaTest)
{
+ yaca_context_h ctx;
+ KeyIvPair key_iv_pair;
+ auto asym_key_ptr = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT);
+ auto bad_iv_ptr = generate_key(YACA_KEY_TYPE_IV, INVALID_IV_BIT_LENGTH);
+ auto des_key_ptr = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_64BIT);
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(nullptr, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ key_iv_pair.key.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, static_cast<yaca_encrypt_algorithm_e>(-1),
+ YACA_BCM_CBC, key_iv_pair.key.get(),
+ key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES,
+ static_cast<yaca_block_cipher_mode_e>(-1),
+ key_iv_pair.key.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_NULL,
+ key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ asym_key_ptr.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_ECB,
+ des_key_ptr.get(), key_iv_pair.iv.get()));
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ key_iv_pair.key.get(), YACA_KEY_NULL));
+
+ YACA_INVALID_PARAM(yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ key_iv_pair.key.get(), bad_iv_ptr.get()));
}
-RUNNER_TEST(T3005_yaca_decrypt_update)
+RUNNER_TEST(T3050_yaca_decrypt_update_invalid_param, YacaTest)
{
+ size_t plaintext_len = 0;
+ KeyIvPair key_iv_pair;
+ auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+ auto plaintext_ptr = out_buf_alloc(dec_ctx_ptr, DATA.size(), plaintext_len);
+ auto inv_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+
+ YACA_INVALID_PARAM(yaca_decrypt_update(YACA_CONTEXT_NULL, DATA.data(), DATA.size(),
+ nullptr, &plaintext_len));
+
+ YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), nullptr, DATA.size(),
+ plaintext_ptr.get(), &plaintext_len));
+
+ YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), 0,
+ plaintext_ptr.get(), &plaintext_len));
+
+ YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
+ nullptr, &plaintext_len));
+
+ YACA_INVALID_PARAM(yaca_decrypt_update(dec_ctx_ptr.get(), DATA.data(), DATA.size(),
+ plaintext_ptr.get(), nullptr));
+
+ YACA_INVALID_PARAM(yaca_decrypt_update(inv_ctx_ptr.get(), DATA.data(), DATA.size(),
+ plaintext_ptr.get(), &plaintext_len));
}
-RUNNER_TEST(T3006_yaca_decrypt_final)
+RUNNER_TEST(T3060_yaca_decrypt_final_invalid_param, YacaTest)
{
+ size_t plaintext_len = 0;
+ KeyIvPair key_iv_pair;
+ auto dec_ctx_ptr = decrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+ auto plaintext_ptr = out_buf_alloc(dec_ctx_ptr, DATA.size(), plaintext_len);
+ auto inv_ctx_ptr = encrypt_init(YACA_ENCRYPT_AES, YACA_BCM_CBC, key_iv_pair.key, key_iv_pair.iv);
+
+ YACA_INVALID_PARAM(yaca_decrypt_finalize(YACA_CONTEXT_NULL, plaintext_ptr.get(),
+ &plaintext_len));
+
+ YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), nullptr, &plaintext_len));
+
+ YACA_INVALID_PARAM(yaca_decrypt_finalize(dec_ctx_ptr.get(), plaintext_ptr.get(), nullptr));
+
+ YACA_INVALID_PARAM(yaca_decrypt_finalize(inv_ctx_ptr.get(), plaintext_ptr.get(),
+ &plaintext_len));
}
-RUNNER_TEST(T3007_yaca_get_iv_bits)
+RUNNER_TEST(T3070_yaca_get_iv_bits_invalid_param, YacaTest)
{
+ const size_t KEY_LEN = 256;
+ size_t iv_bit_len;
+ const size_t INVALID_KEY_BIT_LENGTH = 512;
+
+ YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(static_cast<yaca_encrypt_algorithm_e>(-1),
+ YACA_BCM_CBC, KEY_LEN, &iv_bit_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES,
+ static_cast<yaca_block_cipher_mode_e>(-1),
+ KEY_LEN, &iv_bit_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ INVALID_KEY_BIT_LENGTH, &iv_bit_len));
+
+ YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, YACA_BCM_CBC,
+ KEY_LEN, nullptr));
}