From: Mateusz Forc Date: Tue, 5 Jul 2016 16:20:16 +0000 (+0200) Subject: YACA: Add invalid param tests for encrypt/decrypt functions X-Git-Tag: security-manager_5.5_testing~49 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3fbb4de1c8b7ba0b4741e6e40d4bba3626a1f58b;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git YACA: Add invalid param tests for encrypt/decrypt functions Change-Id: I4081eb9ee3586ace34887d62bf1f5e672901b273 --- diff --git a/src/yaca/yaca-test-common.cpp b/src/yaca/yaca-test-common.cpp index fff27c02..29f66930 100644 --- a/src/yaca/yaca-test-common.cpp +++ b/src/yaca/yaca-test-common.cpp @@ -16,6 +16,7 @@ /* * @file yaca-test-common.cpp * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com) + * @author Mateusz Forc (m.forc@samsung.com) * @version 1.0 */ @@ -28,6 +29,7 @@ #include #include #include +#include #include "yaca-test-common.h" @@ -163,6 +165,29 @@ ChrPtr simple_sign(yaca_digest_algorithm_e algo, return wrap_ptr(signature); } +CtxPtr encrypt_init(yaca_encrypt_algorithm_e algo, + yaca_block_cipher_mode_e bcm, + const KeyPtr &sym_key, + const KeyPtr &iv) +{ + yaca_context_h ctx = YACA_CONTEXT_NULL; + + YACA_SUCCESS(yaca_encrypt_initialize(&ctx, algo, bcm, sym_key.get(), iv.get())); + + return wrap_ptr(ctx); +} + +CtxPtr decrypt_init(yaca_encrypt_algorithm_e algo, + yaca_block_cipher_mode_e bcm, + const KeyPtr& sym_key, + const KeyPtr& iv) +{ + yaca_context_h ctx = YACA_CONTEXT_NULL; + + YACA_SUCCESS(yaca_decrypt_initialize(&ctx, algo, bcm, sym_key.get(), iv.get())); + + return wrap_ptr(ctx); +} CtxPtr seal_init(const KeyPtr &key_pub, yaca_encrypt_algorithm_e algo, diff --git a/src/yaca/yaca-test-common.h b/src/yaca/yaca-test-common.h index 77c82547..9303d9dc 100644 --- a/src/yaca/yaca-test-common.h +++ b/src/yaca/yaca-test-common.h @@ -159,6 +159,16 @@ CtxPtr seal_init(const KeyPtr &key_pub, KeyPtr &sym_key_ptr, KeyPtr &iv_ptr); +CtxPtr encrypt_init(yaca_encrypt_algorithm_e algo, + yaca_block_cipher_mode_e bcm, + const KeyPtr &sym_key, + const KeyPtr &iv); + +CtxPtr decrypt_init(yaca_encrypt_algorithm_e algo, + yaca_block_cipher_mode_e bcm, + const KeyPtr &sym_key, + const KeyPtr &iv); + CtxPtr sign_init(yaca_digest_algorithm_e algo, const KeyPtr &key_prv); CtxPtr verify_init(yaca_digest_algorithm_e algo, const KeyPtr &key_prv); diff --git a/src/yaca/yaca-test-encrypt.cpp b/src/yaca/yaca-test-encrypt.cpp index 648897c9..df83d8ae 100644 --- a/src/yaca/yaca-test-encrypt.cpp +++ b/src/yaca/yaca-test-encrypt.cpp @@ -19,39 +19,218 @@ /** * @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 +#include + +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(-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(-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(-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(-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(-1), + YACA_BCM_CBC, KEY_LEN, &iv_bit_len)); + + YACA_INVALID_PARAM(yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_AES, + static_cast(-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)); }