From 6c0db0b6f7d5f8edcffdff55f76a6f0ad2bf7996 Mon Sep 17 00:00:00 2001 From: Dariusz Michaluk Date: Fri, 12 Aug 2016 12:26:43 +0200 Subject: [PATCH] YACA: Add simple encrypt/decrypt invalid param tests. Change-Id: I1ff53992cf048eeca8ec43dd22f5d4c0bdeeb80d --- src/yaca/yaca-test-simple.cpp | 144 ++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/src/yaca/yaca-test-simple.cpp b/src/yaca/yaca-test-simple.cpp index 190a55c6..e7d58831 100644 --- a/src/yaca/yaca-test-simple.cpp +++ b/src/yaca/yaca-test-simple.cpp @@ -597,3 +597,147 @@ RUNNER_TEST(T7110_yaca_simple_calculate_cmac_vectors, YacaTest) << encrypt2str(algo) << " is different than expected"); } } + +RUNNER_TEST(T7120_yaca_simple_encrypt_invalid_param, YacaTest) +{ + Buffer plaintext = random_buffer(1024); + char* ciphertext; + size_t ciphertext_len; + + yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES; + yaca_block_cipher_mode_e bcm = YACA_BCM_CBC; + + auto sym_key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT); + auto iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_128BIT); + + auto rsa_priv = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT); + auto rsa_pub = extract_public_key(rsa_priv); + auto des_key = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_192BIT); + auto dh_params = generate_key(YACA_KEY_TYPE_DH_PARAMS, YACA_KEY_LENGTH_DH_RFC_1024_160); + auto dh_priv = generate_key_from_parameters(dh_params); + auto bad_iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_UNSAFE_8BIT); + + YACA_INVALID_PARAM(yaca_simple_encrypt(static_cast(-1), bcm, + sym_key.get(), iv.get(), plaintext.data(), plaintext.size(), + &ciphertext, &ciphertext_len)); + + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, static_cast(-1), + sym_key.get(), iv.get(), plaintext.data(), plaintext.size(), + &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, YACA_BCM_GCM, sym_key.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, YACA_BCM_CCM, sym_key.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, YACA_KEY_NULL, iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, iv.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, rsa_priv.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, rsa_pub.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, dh_params.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, dh_priv.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), YACA_KEY_NULL, + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), bad_iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), sym_key.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), rsa_priv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), rsa_pub.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), dh_params.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), dh_priv.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), des_key.get(), + plaintext.data(), plaintext.size(), &ciphertext, &ciphertext_len)); + + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), iv.get(), + nullptr, plaintext.size(), &ciphertext, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), iv.get(), + plaintext.data(), 0, &ciphertext, &ciphertext_len)); + + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), iv.get(), + plaintext.data(), plaintext.size(), nullptr, &ciphertext_len)); + YACA_INVALID_PARAM(yaca_simple_encrypt(algo, bcm, sym_key.get(), iv.get(), + plaintext.data(), plaintext.size(), &ciphertext, nullptr)); +} + +RUNNER_TEST(T7130_yaca_simple_decrypt_invalid_param, YacaTest) +{ + Buffer ciphertext = random_buffer(1024); + char* plaintext; + size_t plaintext_len; + + yaca_encrypt_algorithm_e algo = YACA_ENCRYPT_AES; + yaca_block_cipher_mode_e bcm = YACA_BCM_CBC; + + auto sym_key = generate_key(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT); + auto iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_128BIT); + + auto rsa_priv = generate_key(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_1024BIT); + auto rsa_pub = extract_public_key(rsa_priv); + auto des_key = generate_key(YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_192BIT); + auto dh_params = generate_key(YACA_KEY_TYPE_DH_PARAMS, YACA_KEY_LENGTH_DH_RFC_1024_160); + auto dh_priv = generate_key_from_parameters(dh_params); + auto bad_iv = generate_key(YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_UNSAFE_8BIT); + + YACA_INVALID_PARAM(yaca_simple_decrypt(static_cast(-1), bcm, + sym_key.get(), iv.get(), ciphertext.data(), ciphertext.size(), + &plaintext, &plaintext_len)); + + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, static_cast(-1), + sym_key.get(), iv.get(), ciphertext.data(), ciphertext.size(), + &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, YACA_BCM_GCM, sym_key.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, YACA_BCM_CCM, sym_key.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, YACA_KEY_NULL, iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, iv.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, rsa_priv.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, rsa_pub.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, dh_params.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, dh_priv.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), YACA_KEY_NULL, + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), bad_iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), sym_key.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), rsa_priv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), rsa_pub.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), dh_params.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), dh_priv.get(), + ciphertext.data(), ciphertext.size(),&plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), des_key.get(), + ciphertext.data(), ciphertext.size(), &plaintext, &plaintext_len)); + + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), iv.get(), + nullptr, ciphertext.size(), &plaintext, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), iv.get(), + ciphertext.data(), 0, &plaintext, &plaintext_len)); + + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), iv.get(), + ciphertext.data(), ciphertext.size(), nullptr, &plaintext_len)); + YACA_INVALID_PARAM(yaca_simple_decrypt(algo, bcm, sym_key.get(), iv.get(), + ciphertext.data(), ciphertext.size(), &plaintext, nullptr)); +} -- 2.34.1