From 6913160bb8ad64572057dbc7ec96a72a0111b1f5 Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Mon, 4 May 2020 17:23:09 +0200 Subject: [PATCH 01/16] OpenSSL and libc mockup infrastructure implementation Most of the OpenSSL functions used in YACA now have their mockups that the tests can control. In every YACA src file 3 lines has been added (conditional include of a redefine header). In the tests one can fail a specific or nth OpenSSL function by setting special global variables. Functions excluded from mockup are functions returning void and few specific functions use in initialization. Change-Id: I815f5fecd3a3fc7427e9f5b2d443fdba18d3d2ee --- src/crypto.c | 5 + src/digest.c | 5 + src/encrypt.c | 5 + src/key.c | 5 + src/rsa.c | 5 + src/seal.c | 5 + src/sign.c | 5 + tests/CMakeLists.txt | 3 +- tests/openssl_mock_functions.h | 207 +++++++ tests/openssl_mock_impl.c | 1198 ++++++++++++++++++++++++++++++++++++++++ tests/openssl_mock_impl.h | 203 +++++++ tests/openssl_mock_redefine.h | 235 ++++++++ 12 files changed, 1880 insertions(+), 1 deletion(-) create mode 100644 tests/openssl_mock_functions.h create mode 100644 tests/openssl_mock_impl.c create mode 100644 tests/openssl_mock_impl.h create mode 100644 tests/openssl_mock_redefine.h diff --git a/src/crypto.c b/src/crypto.c index b798196..ab94112 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -44,6 +44,11 @@ #include "internal.h" +#ifdef OPENSSL_MOCKUP_TESTS +#include "../tests/openssl_mock_redefine.h" +#endif + + static __thread bool current_thread_initialized = false; static size_t threads_cnt = 0; static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER; diff --git a/src/digest.c b/src/digest.c index 9b88442..07f7de8 100644 --- a/src/digest.c +++ b/src/digest.c @@ -31,6 +31,11 @@ #include "internal.h" +#ifdef OPENSSL_MOCKUP_TESTS +#include "../tests/openssl_mock_redefine.h" +#endif + + static const struct { yaca_digest_algorithm_e algo; const EVP_MD *(*digest)(void); diff --git a/src/encrypt.c b/src/encrypt.c index 49a4a4d..f0ef959 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -36,6 +36,11 @@ #include "internal.h" +#ifdef OPENSSL_MOCKUP_TESTS +#include "../tests/openssl_mock_redefine.h" +#endif + + static int set_encrypt_property(yaca_context_h ctx, yaca_property_e property, const void *value, size_t value_len); diff --git a/src/key.c b/src/key.c index 68def62..d94e533 100644 --- a/src/key.c +++ b/src/key.c @@ -41,6 +41,11 @@ #include "internal.h" +#ifdef OPENSSL_MOCKUP_TESTS +#include "../tests/openssl_mock_redefine.h" +#endif + + struct openssl_password_data { bool password_requested; const char *password; diff --git a/src/rsa.c b/src/rsa.c index 21077b9..cbd951b 100644 --- a/src/rsa.c +++ b/src/rsa.c @@ -34,6 +34,11 @@ #include "internal.h" +#ifdef OPENSSL_MOCKUP_TESTS +#include "../tests/openssl_mock_redefine.h" +#endif + + int rsa_padding2openssl(yaca_padding_e padding) { switch (padding) { diff --git a/src/seal.c b/src/seal.c index 27a1c21..73e7abb 100644 --- a/src/seal.c +++ b/src/seal.c @@ -34,6 +34,11 @@ #include "internal.h" +#ifdef OPENSSL_MOCKUP_TESTS +#include "../tests/openssl_mock_redefine.h" +#endif + + static int seal_generate_sym_key(yaca_encrypt_algorithm_e algo, size_t sym_key_bit_len, yaca_key_h *sym_key) diff --git a/src/sign.c b/src/sign.c index e276af6..3b4f6f6 100644 --- a/src/sign.c +++ b/src/sign.c @@ -35,6 +35,11 @@ #include "internal.h" +#ifdef OPENSSL_MOCKUP_TESTS +#include "../tests/openssl_mock_redefine.h" +#endif + + /* Operation type saved in context to recognize what * type of operation is performed and how to perform it. */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4ebee1a..ad3eb0d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,10 +33,11 @@ SET(TESTS_SOURCES test_encrypt.cpp test_seal.cpp test_sign.cpp + openssl_mock_impl.c ) FIND_PACKAGE(Boost REQUIRED unit_test_framework) -ADD_DEFINITIONS("-DBOOST_TEST_DYN_LINK") +ADD_DEFINITIONS("-DBOOST_TEST_DYN_LINK -DOPENSSL_MOCKUP_TESTS") INCLUDE_DIRECTORIES(${API_FOLDER} ${SRC_FOLDER}) INCLUDE_DIRECTORIES(SYSTEM ${YACA_DEPS_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}) diff --git a/tests/openssl_mock_functions.h b/tests/openssl_mock_functions.h new file mode 100644 index 0000000..d11b692 --- /dev/null +++ b/tests/openssl_mock_functions.h @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file openssl_mock_functions.h + * @brief + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Mockup declarations */ +int MOCK_open(const char *pathname, int flags); +ssize_t MOCK_read(int fd, void *buf, size_t count); +int MOCK_BIO_flush(BIO *b); +long MOCK_BIO_get_mem_data(BIO *b, char **pp); +BIO *MOCK_BIO_new(const BIO_METHOD *type); +BIO *MOCK_BIO_new_mem_buf(const void *buf, int len); +int MOCK_BIO_read(BIO *b, void *data, int dlen); +int MOCK_BIO_reset(BIO *b); +int MOCK_BIO_write(BIO *b, const void *data, int dlen); +CMAC_CTX *MOCK_CMAC_CTX_new(void); +int MOCK_CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, const EVP_CIPHER *cipher, ENGINE *impl); +int MOCK_DES_random_key(DES_cblock *ret); +int MOCK_DH_KDF_X9_42(unsigned char *out, size_t outlen, const unsigned char *Z, size_t Zlen, ASN1_OBJECT *key_oid, const unsigned char *ukm, size_t ukmlen, const EVP_MD *md); +int MOCK_ECDH_KDF_X9_62(unsigned char *out, size_t outlen, const unsigned char *Z, size_t Zlen, const unsigned char *sinfo, size_t sinfolen, const EVP_MD *md); +int MOCK_EC_GROUP_get_asn1_flag(const EC_GROUP *group); +int MOCK_EC_GROUP_get_curve_name(const EC_GROUP *group); +EC_KEY *MOCK_EC_KEY_new(void); +int MOCK_EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group); +int MOCK_EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx); +int MOCK_EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c); +int MOCK_EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr); +EVP_CIPHER_CTX *MOCK_EVP_CIPHER_CTX_new(void); +int MOCK_EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen); +int MOCK_EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad); +int MOCK_EVP_CIPHER_iv_length(const EVP_CIPHER *cipher); +int MOCK_EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl); +int MOCK_EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc); +int MOCK_EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl); +int MOCK_EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s); +int MOCK_EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type); +int MOCK_EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen); +int MOCK_EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey); +int MOCK_EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt); +int MOCK_EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt); +int MOCK_EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen); +int MOCK_EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey); +int MOCK_EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt); +EVP_MD_CTX *MOCK_EVP_MD_CTX_create(void); +EVP_PKEY_CTX *MOCK_EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx); +int MOCK_EVP_MD_CTX_size(const EVP_MD_CTX *ctx); +int MOCK_EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, int p1, void *p2); +EVP_PKEY *MOCK_EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx); +EVP_PKEY_CTX *MOCK_EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e); +EVP_PKEY_CTX *MOCK_EVP_PKEY_CTX_new_id(int id, ENGINE *e); +int MOCK_EVP_PKEY_CTX_set_dh_paramgen_generator(EVP_PKEY_CTX *ctx, int gen); +int MOCK_EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int len); +int MOCK_EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits); +int MOCK_EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int param_enc); +int MOCK_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid); +int MOCK_EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int mbits); +int MOCK_EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad); +int MOCK_EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key); +int MOCK_EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key); +int MOCK_EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key); +int MOCK_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); +int MOCK_EVP_PKEY_bits(const EVP_PKEY *pkey); +int MOCK_EVP_PKEY_decrypt_old(unsigned char *dec_key, const unsigned char *enc_key, int enc_key_len, EVP_PKEY *private_key); +int MOCK_EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); +int MOCK_EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); +int MOCK_EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); +int MOCK_EVP_PKEY_encrypt_old(unsigned char *enc_key, const unsigned char *key, int key_len, EVP_PKEY *pub_key); +int MOCK_EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); +int MOCK_EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx); +EVP_PKEY *MOCK_EVP_PKEY_new(void); +EVP_PKEY *MOCK_EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key, int keylen); +int MOCK_EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); +int MOCK_EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx); +int MOCK_EVP_PKEY_size(EVP_PKEY *pkey); +int MOCK_EVP_PKEY_up_ref(EVP_PKEY *pkey); +const EVP_CIPHER *MOCK_EVP_aes_128_cbc(void); +const EVP_CIPHER *MOCK_EVP_aes_128_ccm(void); +const EVP_CIPHER *MOCK_EVP_aes_128_cfb(void); +const EVP_CIPHER *MOCK_EVP_aes_128_cfb1(void); +const EVP_CIPHER *MOCK_EVP_aes_128_cfb8(void); +const EVP_CIPHER *MOCK_EVP_aes_128_ctr(void); +const EVP_CIPHER *MOCK_EVP_aes_128_ecb(void); +const EVP_CIPHER *MOCK_EVP_aes_128_gcm(void); +const EVP_CIPHER *MOCK_EVP_aes_128_ofb(void); +const EVP_CIPHER *MOCK_EVP_aes_128_wrap(void); +const EVP_CIPHER *MOCK_EVP_aes_192_cbc(void); +const EVP_CIPHER *MOCK_EVP_aes_192_ccm(void); +const EVP_CIPHER *MOCK_EVP_aes_192_cfb(void); +const EVP_CIPHER *MOCK_EVP_aes_192_cfb1(void); +const EVP_CIPHER *MOCK_EVP_aes_192_cfb8(void); +const EVP_CIPHER *MOCK_EVP_aes_192_ctr(void); +const EVP_CIPHER *MOCK_EVP_aes_192_ecb(void); +const EVP_CIPHER *MOCK_EVP_aes_192_gcm(void); +const EVP_CIPHER *MOCK_EVP_aes_192_ofb(void); +const EVP_CIPHER *MOCK_EVP_aes_192_wrap(void); +const EVP_CIPHER *MOCK_EVP_aes_256_cbc(void); +const EVP_CIPHER *MOCK_EVP_aes_256_ccm(void); +const EVP_CIPHER *MOCK_EVP_aes_256_cfb(void); +const EVP_CIPHER *MOCK_EVP_aes_256_cfb1(void); +const EVP_CIPHER *MOCK_EVP_aes_256_cfb8(void); +const EVP_CIPHER *MOCK_EVP_aes_256_ctr(void); +const EVP_CIPHER *MOCK_EVP_aes_256_ecb(void); +const EVP_CIPHER *MOCK_EVP_aes_256_gcm(void); +const EVP_CIPHER *MOCK_EVP_aes_256_ofb(void); +const EVP_CIPHER *MOCK_EVP_aes_256_wrap(void); +const EVP_CIPHER *MOCK_EVP_cast5_cbc(void); +const EVP_CIPHER *MOCK_EVP_cast5_cfb(void); +const EVP_CIPHER *MOCK_EVP_cast5_ecb(void); +const EVP_CIPHER *MOCK_EVP_cast5_ofb(void); +const EVP_CIPHER *MOCK_EVP_des_cbc(void); +const EVP_CIPHER *MOCK_EVP_des_cfb(void); +const EVP_CIPHER *MOCK_EVP_des_cfb1(void); +const EVP_CIPHER *MOCK_EVP_des_cfb8(void); +const EVP_CIPHER *MOCK_EVP_des_ecb(void); +const EVP_CIPHER *MOCK_EVP_des_ede3_cbc(void); +const EVP_CIPHER *MOCK_EVP_des_ede3_cfb(void); +const EVP_CIPHER *MOCK_EVP_des_ede3_cfb1(void); +const EVP_CIPHER *MOCK_EVP_des_ede3_cfb8(void); +const EVP_CIPHER *MOCK_EVP_des_ede3_ecb(void); +const EVP_CIPHER *MOCK_EVP_des_ede3_ofb(void); +const EVP_CIPHER *MOCK_EVP_des_ede3_wrap(void); +const EVP_CIPHER *MOCK_EVP_des_ede_cbc(void); +const EVP_CIPHER *MOCK_EVP_des_ede_cfb(void); +const EVP_CIPHER *MOCK_EVP_des_ede_ecb(void); +const EVP_CIPHER *MOCK_EVP_des_ede_ofb(void); +const EVP_CIPHER *MOCK_EVP_des_ofb(void); +const EVP_MD *MOCK_EVP_md5(void); +const EVP_CIPHER *MOCK_EVP_rc2_cbc(void); +const EVP_CIPHER *MOCK_EVP_rc2_cfb(void); +const EVP_CIPHER *MOCK_EVP_rc2_ecb(void); +const EVP_CIPHER *MOCK_EVP_rc2_ofb(void); +const EVP_CIPHER *MOCK_EVP_rc4(void); +const EVP_MD *MOCK_EVP_sha1(void); +const EVP_MD *MOCK_EVP_sha224(void); +const EVP_MD *MOCK_EVP_sha256(void); +const EVP_MD *MOCK_EVP_sha384(void); +const EVP_MD *MOCK_EVP_sha512(void); +void *MOCK_OPENSSL_malloc(size_t num); +void *MOCK_OPENSSL_realloc(void *addr, size_t num); +EVP_PKEY *MOCK_PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u); +EVP_PKEY *MOCK_PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x); +EVP_PKEY *MOCK_PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u); +X509 *MOCK_PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u); +int MOCK_PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u); +int MOCK_PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x); +int MOCK_PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x); +int MOCK_PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u); +int MOCK_PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, const EVP_MD *digest, int keylen, unsigned char *out); +int MOCK_RAND_bytes(unsigned char *buf, int num); +int MOCK_RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); +int MOCK_RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); +int MOCK_RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); +int MOCK_RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); +EVP_PKEY *MOCK_X509_get_pubkey(X509 *x); +DH *MOCK_d2i_DHparams_bio(BIO *bp, DH **x); +DSA *MOCK_d2i_DSAparams_bio(BIO *bp, DSA **x); +EC_GROUP *MOCK_d2i_ECPKParameters_bio(BIO *bp, EC_GROUP **x); +EVP_PKEY *MOCK_d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u); +EVP_PKEY *MOCK_d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a); +EVP_PKEY *MOCK_d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a); +X509 *MOCK_d2i_X509_bio(BIO *bp, X509 **x509); +int MOCK_i2d_DHparams_bio(BIO *bp, const DH *x); +int MOCK_i2d_DSAparams_bio(BIO *bp, const DSA *x); +int MOCK_i2d_ECPKParameters_bio(BIO *bp, const EC_GROUP *x); +int MOCK_i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u); +int MOCK_i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey); +int MOCK_i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey); + +#ifdef __cplusplus +} +#endif diff --git a/tests/openssl_mock_impl.c b/tests/openssl_mock_impl.c new file mode 100644 index 0000000..7c10355 --- /dev/null +++ b/tests/openssl_mock_impl.c @@ -0,0 +1,1198 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file openssl_mock_impl.c + * @brief + */ + +#include +#include +#include +#include + +#include + +#include "openssl_mock_impl.h" + + +unsigned MOCK_fail_nth = 0; + +static int BIO_reset_just_called = 0; +static void reset_conditions() +{ + BIO_reset_just_called = 0; +} + +#define HANDLE_FUNCTION(FNAME, VALUE, COND) \ + do { \ + if (GET_BOOL_NAME(FNAME)) { \ + GET_BOOL_NAME(FNAME) = 0; \ + return VALUE; \ + } \ + if (COND) { \ + reset_conditions(); \ + break; \ + } \ + reset_conditions(); \ + if (MOCK_fail_nth == 0) { \ + break; \ + } \ + --MOCK_fail_nth; \ + if (MOCK_fail_nth == 0) { \ + return VALUE; \ + } \ + } while(0) + + +int GET_BOOL_NAME(open) = 0; +int MOCK_open(const char *pathname, int flags) +{ + HANDLE_FUNCTION(open, -1, 1); + return open(pathname, flags); +} + +int GET_BOOL_NAME(read) = 0; +ssize_t MOCK_read(int fd, void *buf, size_t count) +{ + HANDLE_FUNCTION(read, -1, 1); + return read(fd, buf, count); +} + +int GET_BOOL_NAME(BIO_flush) = 0; +int MOCK_BIO_flush(BIO *b) +{ + HANDLE_FUNCTION(BIO_flush, 0, 0); + return BIO_flush(b); +} + +int GET_BOOL_NAME(BIO_get_mem_data) = 0; +long MOCK_BIO_get_mem_data(BIO *b, char **pp) +{ + HANDLE_FUNCTION(BIO_get_mem_data, -1, 0); + return BIO_get_mem_data(b, pp); +} + +int GET_BOOL_NAME(BIO_new) = 0; +BIO *MOCK_BIO_new(const BIO_METHOD *type) +{ + HANDLE_FUNCTION(BIO_new, NULL, 0); + return BIO_new(type); +} + +int GET_BOOL_NAME(BIO_new_mem_buf) = 0; +BIO *MOCK_BIO_new_mem_buf(const void *buf, int len) +{ + HANDLE_FUNCTION(BIO_new_mem_buf, NULL, 0); + return BIO_new_mem_buf(buf, len); +} + +int GET_BOOL_NAME(BIO_read) = 0; +int MOCK_BIO_read(BIO *b, void *data, int dlen) +{ + HANDLE_FUNCTION(BIO_read, -1, 0); + return BIO_read(b, data, dlen); +} + +int GET_BOOL_NAME(BIO_reset) = 0; +int MOCK_BIO_reset(BIO *b) +{ + HANDLE_FUNCTION(BIO_reset, 0, 0); + BIO_reset_just_called = 1; + return BIO_reset(b); +} + +int GET_BOOL_NAME(BIO_write) = 0; +int MOCK_BIO_write(BIO *b, const void *data, int dlen) +{ + HANDLE_FUNCTION(BIO_write, -1, 0); + return BIO_write(b, data, dlen); +} + +int GET_BOOL_NAME(CMAC_CTX_new) = 0; +CMAC_CTX *MOCK_CMAC_CTX_new(void) +{ + HANDLE_FUNCTION(CMAC_CTX_new, NULL, 0); + return CMAC_CTX_new(); +} + +int GET_BOOL_NAME(CMAC_Init) = 0; +int MOCK_CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, const EVP_CIPHER *cipher, ENGINE *impl) +{ + HANDLE_FUNCTION(CMAC_Init, 0, 0); + return CMAC_Init(ctx, key, keylen, cipher, impl); +} + +int GET_BOOL_NAME(DES_random_key) = 0; +int MOCK_DES_random_key(DES_cblock *ret) +{ + HANDLE_FUNCTION(DES_random_key, 0, 0); + return DES_random_key(ret); +} + +int GET_BOOL_NAME(DH_KDF_X9_42) = 0; +int MOCK_DH_KDF_X9_42(unsigned char *out, size_t outlen, const unsigned char *Z, size_t Zlen, ASN1_OBJECT *key_oid, const unsigned char *ukm, size_t ukmlen, const EVP_MD *md) +{ + HANDLE_FUNCTION(DH_KDF_X9_42, 0, 0); + return DH_KDF_X9_42(out, outlen, Z, Zlen, key_oid, ukm, ukmlen, md); +} + +int GET_BOOL_NAME(ECDH_KDF_X9_62) = 0; +int MOCK_ECDH_KDF_X9_62(unsigned char *out, size_t outlen, const unsigned char *Z, size_t Zlen, const unsigned char *sinfo, size_t sinfolen, const EVP_MD *md) +{ + HANDLE_FUNCTION(ECDH_KDF_X9_62, 0, 0); + return ECDH_KDF_X9_62(out, outlen, Z, Zlen, sinfo, sinfolen, md); +} + +int GET_BOOL_NAME(EC_GROUP_get_asn1_flag) = 0; +int MOCK_EC_GROUP_get_asn1_flag(const EC_GROUP *group) +{ + HANDLE_FUNCTION(EC_GROUP_get_asn1_flag, 0, 0); + return EC_GROUP_get_asn1_flag(group); +} + +int GET_BOOL_NAME(EC_GROUP_get_curve_name) = 0; +int MOCK_EC_GROUP_get_curve_name(const EC_GROUP *group) +{ + HANDLE_FUNCTION(EC_GROUP_get_curve_name, 0, 0); + return EC_GROUP_get_curve_name(group); +} + +int GET_BOOL_NAME(EC_KEY_new) = 0; +EC_KEY *MOCK_EC_KEY_new() +{ + HANDLE_FUNCTION(EC_KEY_new, NULL, 0); + return EC_KEY_new(); +} + +int GET_BOOL_NAME(EC_KEY_set_group) = 0; +int MOCK_EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group) +{ + HANDLE_FUNCTION(EC_KEY_set_group, 0, 0); + return EC_KEY_set_group(key, group); +} + +int GET_BOOL_NAME(EVP_CIPHER_CTX_block_size) = 0; +int MOCK_EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) +{ + HANDLE_FUNCTION(EVP_CIPHER_CTX_block_size, 0, 0); + return EVP_CIPHER_CTX_block_size(ctx); +} + +int GET_BOOL_NAME(EVP_CIPHER_CTX_cleanup) = 0; +int MOCK_EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) +{ + HANDLE_FUNCTION(EVP_CIPHER_CTX_cleanup, 0, 0); + return EVP_CIPHER_CTX_cleanup(c); +} + +int GET_BOOL_NAME(EVP_CIPHER_CTX_ctrl) = 0; +int MOCK_EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) +{ + HANDLE_FUNCTION(EVP_CIPHER_CTX_ctrl, 0, 0); + return EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr); +} + +int GET_BOOL_NAME(EVP_CIPHER_CTX_new) = 0; +EVP_CIPHER_CTX *MOCK_EVP_CIPHER_CTX_new(void) +{ + HANDLE_FUNCTION(EVP_CIPHER_CTX_new, NULL, 0); + return EVP_CIPHER_CTX_new(); +} + +int GET_BOOL_NAME(EVP_CIPHER_CTX_set_key_length) = 0; +int MOCK_EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen) +{ + HANDLE_FUNCTION(EVP_CIPHER_CTX_set_key_length, 0, 0); + return EVP_CIPHER_CTX_set_key_length(x, keylen); +} + +int GET_BOOL_NAME(EVP_CIPHER_CTX_set_padding) = 0; +int MOCK_EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad) +{ + HANDLE_FUNCTION(EVP_CIPHER_CTX_set_padding, 0, 0); + return EVP_CIPHER_CTX_set_padding(c, pad); +} + +int GET_BOOL_NAME(EVP_CIPHER_iv_length) = 0; +int MOCK_EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) +{ + HANDLE_FUNCTION(EVP_CIPHER_iv_length, -1, 0); + return EVP_CIPHER_iv_length(cipher); +} + +int GET_BOOL_NAME(EVP_CipherFinal) = 0; +int MOCK_EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl) +{ + HANDLE_FUNCTION(EVP_CipherFinal, 0, 0); + return EVP_CipherFinal(ctx, outm, outl); +} + +int GET_BOOL_NAME(EVP_CipherInit_ex) = 0; +int MOCK_EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc) +{ + HANDLE_FUNCTION(EVP_CipherInit_ex, 0, 0); + return EVP_CipherInit_ex(ctx, type, impl, key, iv, enc); +} + +int GET_BOOL_NAME(EVP_CipherUpdate) = 0; +int MOCK_EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, const unsigned char *in, int inl) +{ + HANDLE_FUNCTION(EVP_CipherUpdate, 0, 0); + return EVP_CipherUpdate(ctx, out, outl, in, inl); +} + +int GET_BOOL_NAME(EVP_DigestFinal_ex) = 0; +int MOCK_EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s) +{ + HANDLE_FUNCTION(EVP_DigestFinal_ex, 0, 0); + return EVP_DigestFinal_ex(ctx, md, s); +} + +int GET_BOOL_NAME(EVP_DigestInit) = 0; +int MOCK_EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) +{ + HANDLE_FUNCTION(EVP_DigestInit, 0, 0); + return EVP_DigestInit(ctx, type); +} + +int GET_BOOL_NAME(EVP_DigestSignFinal) = 0; +int MOCK_EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen) +{ + HANDLE_FUNCTION(EVP_DigestSignFinal, 0, 0); + return EVP_DigestSignFinal(ctx, sigret, siglen); +} + +int GET_BOOL_NAME(EVP_DigestSignInit) = 0; +int MOCK_EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) +{ + HANDLE_FUNCTION(EVP_DigestSignInit, 0, 0); + return EVP_DigestSignInit(ctx, pctx, type, e, pkey); +} + +int GET_BOOL_NAME(EVP_DigestSignUpdate) = 0; +int MOCK_EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt) +{ + HANDLE_FUNCTION(EVP_DigestSignUpdate, 0, 0); + return EVP_DigestSignUpdate(ctx, d, cnt); +} + +int GET_BOOL_NAME(EVP_DigestUpdate) = 0; +int MOCK_EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt) +{ + HANDLE_FUNCTION(EVP_DigestUpdate, 0, 0); + return EVP_DigestUpdate(ctx, d, cnt); +} + +int GET_BOOL_NAME(EVP_DigestVerifyFinal) = 0; +int MOCK_EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen) +{ + HANDLE_FUNCTION(EVP_DigestVerifyFinal, -1, 0); + return EVP_DigestVerifyFinal(ctx, sig, siglen); +} + +int GET_BOOL_NAME(EVP_DigestVerifyInit) = 0; +int MOCK_EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) +{ + HANDLE_FUNCTION(EVP_DigestVerifyInit, 0, 0); + return EVP_DigestVerifyInit(ctx, pctx, type, e, pkey); +} + +int GET_BOOL_NAME(EVP_DigestVerifyUpdate) = 0; +int MOCK_EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt) +{ + HANDLE_FUNCTION(EVP_DigestVerifyUpdate, 0, 0); + return EVP_DigestVerifyUpdate(ctx, d, cnt); +} + +int GET_BOOL_NAME(EVP_MD_CTX_create) = 0; +EVP_MD_CTX *MOCK_EVP_MD_CTX_create() +{ + HANDLE_FUNCTION(EVP_MD_CTX_create, NULL, 0); + return EVP_MD_CTX_create(); +} + +int GET_BOOL_NAME(EVP_MD_CTX_pkey_ctx) = 0; +EVP_PKEY_CTX *MOCK_EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx) +{ + HANDLE_FUNCTION(EVP_MD_CTX_pkey_ctx, NULL, 0); + return EVP_MD_CTX_pkey_ctx(ctx); +} + +int GET_BOOL_NAME(EVP_MD_CTX_size) = 0; +int MOCK_EVP_MD_CTX_size(const EVP_MD_CTX *ctx) +{ + HANDLE_FUNCTION(EVP_MD_CTX_size, 0, 0); + return EVP_MD_CTX_size(ctx); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_ctrl) = 0; +int MOCK_EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, int p1, void *p2) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_ctrl, 0, 0); + return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, p1, p2); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_get0_pkey) = 0; +EVP_PKEY *MOCK_EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_get0_pkey, NULL, 0); + return EVP_PKEY_CTX_get0_pkey(ctx); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_new) = 0; +EVP_PKEY_CTX *MOCK_EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_new, NULL, 0); + return EVP_PKEY_CTX_new(pkey, e); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_new_id) = 0; +EVP_PKEY_CTX *MOCK_EVP_PKEY_CTX_new_id(int id, ENGINE *e) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_new_id, NULL, 0); + return EVP_PKEY_CTX_new_id(id, e); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_set_dh_paramgen_generator) = 0; +int MOCK_EVP_PKEY_CTX_set_dh_paramgen_generator(EVP_PKEY_CTX *ctx, int gen) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_set_dh_paramgen_generator, 0, 0); + return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_set_dh_paramgen_prime_len) = 0; +int MOCK_EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int len) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_set_dh_paramgen_prime_len, 0, 0); + return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_set_dsa_paramgen_bits) = 0; +int MOCK_EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_set_dsa_paramgen_bits, 0, 0); + return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_set_ec_param_enc) = 0; +int MOCK_EVP_PKEY_CTX_set_ec_param_enc(EVP_PKEY_CTX *ctx, int param_enc) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_set_ec_param_enc, 0, 0); + return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_set_ec_paramgen_curve_nid) = 0; +int MOCK_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX *ctx, int nid) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_set_ec_paramgen_curve_nid, 0, 0); + return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_set_rsa_keygen_bits) = 0; +int MOCK_EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int mbits) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_set_rsa_keygen_bits, 0, 0); + return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, mbits); +} + +int GET_BOOL_NAME(EVP_PKEY_CTX_set_rsa_padding) = 0; +int MOCK_EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad) +{ + HANDLE_FUNCTION(EVP_PKEY_CTX_set_rsa_padding, 0, 0); + return EVP_PKEY_CTX_set_rsa_padding(ctx, pad); +} + +int GET_BOOL_NAME(EVP_PKEY_assign) = 0; +int MOCK_EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) +{ + HANDLE_FUNCTION(EVP_PKEY_assign, 0, 0); + return EVP_PKEY_assign(pkey, type, key); +} + +int GET_BOOL_NAME(EVP_PKEY_assign_DH) = 0; +int MOCK_EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) +{ + HANDLE_FUNCTION(EVP_PKEY_assign_DH, 0, 0); + return EVP_PKEY_assign_DH(pkey, key); +} + +int GET_BOOL_NAME(EVP_PKEY_assign_DSA) = 0; +int MOCK_EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) +{ + HANDLE_FUNCTION(EVP_PKEY_assign_DSA, 0, 0); + return EVP_PKEY_assign_DSA(pkey, key); +} + +int GET_BOOL_NAME(EVP_PKEY_assign_EC_KEY) = 0; +int MOCK_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) +{ + HANDLE_FUNCTION(EVP_PKEY_assign_EC_KEY, 0, 0); + return EVP_PKEY_assign_EC_KEY(pkey, key); +} + +int GET_BOOL_NAME(EVP_PKEY_bits) = 0; +int MOCK_EVP_PKEY_bits(const EVP_PKEY *pkey) +{ + HANDLE_FUNCTION(EVP_PKEY_bits, 0, 0); + return EVP_PKEY_bits(pkey); +} + +int GET_BOOL_NAME(EVP_PKEY_decrypt_old) = 0; +int MOCK_EVP_PKEY_decrypt_old(unsigned char *dec_key, const unsigned char *enc_key, int enc_key_len, EVP_PKEY *private_key) +{ + HANDLE_FUNCTION(EVP_PKEY_decrypt_old, 0, 0); + return EVP_PKEY_decrypt_old(dec_key, enc_key, enc_key_len, private_key); +} + +int GET_BOOL_NAME(EVP_PKEY_derive) = 0; +int MOCK_EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen) +{ + HANDLE_FUNCTION(EVP_PKEY_derive, 0, 0); + return EVP_PKEY_derive(ctx, key, keylen); +} + +int GET_BOOL_NAME(EVP_PKEY_derive_init) = 0; +int MOCK_EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) +{ + HANDLE_FUNCTION(EVP_PKEY_derive_init, 0, 0); + return EVP_PKEY_derive_init(ctx); +} + +int GET_BOOL_NAME(EVP_PKEY_derive_set_peer) = 0; +int MOCK_EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) +{ + HANDLE_FUNCTION(EVP_PKEY_derive_set_peer, 0, 0); + return EVP_PKEY_derive_set_peer(ctx, peer); +} + +int GET_BOOL_NAME(EVP_PKEY_encrypt_old) = 0; +int MOCK_EVP_PKEY_encrypt_old(unsigned char *enc_key, const unsigned char *key, int key_len, EVP_PKEY *pub_key) +{ + HANDLE_FUNCTION(EVP_PKEY_encrypt_old, 0, 0); + return EVP_PKEY_encrypt_old(enc_key, key, key_len, pub_key); +} + +int GET_BOOL_NAME(EVP_PKEY_keygen) = 0; +int MOCK_EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) +{ + HANDLE_FUNCTION(EVP_PKEY_keygen, 0, 0); + return EVP_PKEY_keygen(ctx, ppkey); +} + +int GET_BOOL_NAME(EVP_PKEY_keygen_init) = 0; +int MOCK_EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) +{ + HANDLE_FUNCTION(EVP_PKEY_keygen_init, 0, 0); + return EVP_PKEY_keygen_init(ctx); +} + +int GET_BOOL_NAME(EVP_PKEY_new) = 0; +EVP_PKEY *MOCK_EVP_PKEY_new() +{ + HANDLE_FUNCTION(EVP_PKEY_new, NULL, 0); + return EVP_PKEY_new(); +} + +int GET_BOOL_NAME(EVP_PKEY_new_mac_key) = 0; +EVP_PKEY *MOCK_EVP_PKEY_new_mac_key(int type, ENGINE *e, const unsigned char *key, int keylen) +{ + HANDLE_FUNCTION(EVP_PKEY_new_mac_key, NULL, 0); + return EVP_PKEY_new_mac_key(type, e, key, keylen); +} + +int GET_BOOL_NAME(EVP_PKEY_paramgen) = 0; +int MOCK_EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) +{ + HANDLE_FUNCTION(EVP_PKEY_paramgen, 0, 0); + return EVP_PKEY_paramgen(ctx, ppkey); +} + +int GET_BOOL_NAME(EVP_PKEY_paramgen_init) = 0; +int MOCK_EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) +{ + HANDLE_FUNCTION(EVP_PKEY_paramgen_init, 0, 0); + return EVP_PKEY_paramgen_init(ctx); +} + +int GET_BOOL_NAME(EVP_PKEY_size) = 0; +int MOCK_EVP_PKEY_size(EVP_PKEY *pkey) +{ + /* Cannot fail? */ + HANDLE_FUNCTION(EVP_PKEY_size, 0, 0); + return EVP_PKEY_size(pkey); +} + +int GET_BOOL_NAME(EVP_PKEY_up_ref) = 0; +int MOCK_EVP_PKEY_up_ref(EVP_PKEY *pkey) +{ + HANDLE_FUNCTION(EVP_PKEY_up_ref, 0, 0); + return EVP_PKEY_up_ref(pkey); +} + +int GET_BOOL_NAME(EVP_aes_128_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_cbc(void) +{ + HANDLE_FUNCTION(EVP_aes_128_cbc, NULL, 0); + return EVP_aes_128_cbc(); +} + +int GET_BOOL_NAME(EVP_aes_128_ccm) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_ccm(void) +{ + HANDLE_FUNCTION(EVP_aes_128_ccm, NULL, 0); + return EVP_aes_128_ccm(); +} + +#undef EVP_aes_128_cfb +int GET_BOOL_NAME(EVP_aes_128_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_cfb(void) +{ + HANDLE_FUNCTION(EVP_aes_128_cfb, NULL, 0); + return EVP_aes_128_cfb128(); +} + +int GET_BOOL_NAME(EVP_aes_128_cfb1) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_cfb1(void) +{ + HANDLE_FUNCTION(EVP_aes_128_cfb1, NULL, 0); + return EVP_aes_128_cfb1(); +} + +int GET_BOOL_NAME(EVP_aes_128_cfb8) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_cfb8(void) +{ + HANDLE_FUNCTION(EVP_aes_128_cfb8, NULL, 0); + return EVP_aes_128_cfb8(); +} + +int GET_BOOL_NAME(EVP_aes_128_ctr) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_ctr(void) +{ + HANDLE_FUNCTION(EVP_aes_128_ctr, NULL, 0); + return EVP_aes_128_ctr(); +} + +int GET_BOOL_NAME(EVP_aes_128_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_ecb(void) +{ + HANDLE_FUNCTION(EVP_aes_128_ecb, NULL, 0); + return EVP_aes_128_ecb(); +} + +int GET_BOOL_NAME(EVP_aes_128_gcm) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_gcm(void) +{ + HANDLE_FUNCTION(EVP_aes_128_gcm, NULL, 0); + return EVP_aes_128_gcm(); +} + +int GET_BOOL_NAME(EVP_aes_128_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_ofb(void) +{ + HANDLE_FUNCTION(EVP_aes_128_ofb, NULL, 0); + return EVP_aes_128_ofb(); +} + +int GET_BOOL_NAME(EVP_aes_128_wrap) = 0; +const EVP_CIPHER *MOCK_EVP_aes_128_wrap(void) +{ + HANDLE_FUNCTION(EVP_aes_128_wrap, NULL, 0); + return EVP_aes_128_wrap(); +} + +int GET_BOOL_NAME(EVP_aes_192_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_cbc(void) +{ + HANDLE_FUNCTION(EVP_aes_192_cbc, NULL, 0); + return EVP_aes_192_cbc(); +} + +int GET_BOOL_NAME(EVP_aes_192_ccm) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_ccm(void) +{ + HANDLE_FUNCTION(EVP_aes_192_ccm, NULL, 0); + return EVP_aes_192_ccm(); +} + +#undef EVP_aes_192_cfb +int GET_BOOL_NAME(EVP_aes_192_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_cfb(void) +{ + HANDLE_FUNCTION(EVP_aes_192_cfb, NULL, 0); + return EVP_aes_192_cfb128(); +} + +int GET_BOOL_NAME(EVP_aes_192_cfb1) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_cfb1(void) +{ + HANDLE_FUNCTION(EVP_aes_192_cfb1, NULL, 0); + return EVP_aes_192_cfb1(); +} + +int GET_BOOL_NAME(EVP_aes_192_cfb8) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_cfb8(void) +{ + HANDLE_FUNCTION(EVP_aes_192_cfb8, NULL, 0); + return EVP_aes_192_cfb8(); +} + +int GET_BOOL_NAME(EVP_aes_192_ctr) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_ctr(void) +{ + HANDLE_FUNCTION(EVP_aes_192_ctr, NULL, 0); + return EVP_aes_192_ctr(); +} + +int GET_BOOL_NAME(EVP_aes_192_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_ecb(void) +{ + HANDLE_FUNCTION(EVP_aes_192_ecb, NULL, 0); + return EVP_aes_192_ecb(); +} + +int GET_BOOL_NAME(EVP_aes_192_gcm) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_gcm(void) +{ + HANDLE_FUNCTION(EVP_aes_192_gcm, NULL, 0); + return EVP_aes_192_gcm(); +} + +int GET_BOOL_NAME(EVP_aes_192_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_ofb(void) +{ + HANDLE_FUNCTION(EVP_aes_192_ofb, NULL, 0); + return EVP_aes_192_ofb(); +} + +int GET_BOOL_NAME(EVP_aes_192_wrap) = 0; +const EVP_CIPHER *MOCK_EVP_aes_192_wrap(void) +{ + HANDLE_FUNCTION(EVP_aes_192_wrap, NULL, 0); + return EVP_aes_192_wrap(); +} + +int GET_BOOL_NAME(EVP_aes_256_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_cbc() +{ + HANDLE_FUNCTION(EVP_aes_256_cbc, NULL, 0); + return EVP_aes_256_cbc(); +} + +int GET_BOOL_NAME(EVP_aes_256_ccm) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_ccm(void) +{ + HANDLE_FUNCTION(EVP_aes_256_ccm, NULL, 0); + return EVP_aes_256_ccm(); +} + +#undef EVP_aes_256_cfb +int GET_BOOL_NAME(EVP_aes_256_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_cfb(void) +{ + HANDLE_FUNCTION(EVP_aes_256_cfb, NULL, 0); + return EVP_aes_256_cfb128(); +} + +int GET_BOOL_NAME(EVP_aes_256_cfb1) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_cfb1(void) +{ + HANDLE_FUNCTION(EVP_aes_256_cfb1, NULL, 0); + return EVP_aes_256_cfb1(); +} + +int GET_BOOL_NAME(EVP_aes_256_cfb8) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_cfb8(void) +{ + HANDLE_FUNCTION(EVP_aes_256_cfb8, NULL, 0); + return EVP_aes_256_cfb8(); +} + +int GET_BOOL_NAME(EVP_aes_256_ctr) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_ctr(void) +{ + HANDLE_FUNCTION(EVP_aes_256_ctr, NULL, 0); + return EVP_aes_256_ctr(); +} + +int GET_BOOL_NAME(EVP_aes_256_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_ecb(void) +{ + HANDLE_FUNCTION(EVP_aes_256_ecb, NULL, 0); + return EVP_aes_256_ecb(); +} + +int GET_BOOL_NAME(EVP_aes_256_gcm) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_gcm(void) +{ + HANDLE_FUNCTION(EVP_aes_256_gcm, NULL, 0); + return EVP_aes_256_gcm(); +} + +int GET_BOOL_NAME(EVP_aes_256_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_ofb(void) +{ + HANDLE_FUNCTION(EVP_aes_256_ofb, NULL, 0); + return EVP_aes_256_ofb(); +} + +int GET_BOOL_NAME(EVP_aes_256_wrap) = 0; +const EVP_CIPHER *MOCK_EVP_aes_256_wrap(void) +{ + HANDLE_FUNCTION(EVP_aes_256_wrap, NULL, 0); + return EVP_aes_256_wrap(); +} + +int GET_BOOL_NAME(EVP_cast5_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_cast5_cbc(void) +{ + HANDLE_FUNCTION(EVP_cast5_cbc, NULL, 0); + return EVP_cast5_cbc(); +} + +#undef EVP_cast5_cfb +int GET_BOOL_NAME(EVP_cast5_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_cast5_cfb(void) +{ + HANDLE_FUNCTION(EVP_cast5_cfb, NULL, 0); + return EVP_cast5_cfb64(); +} + +int GET_BOOL_NAME(EVP_cast5_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_cast5_ecb(void) +{ + HANDLE_FUNCTION(EVP_cast5_ecb, NULL, 0); + return EVP_cast5_ecb(); +} + +int GET_BOOL_NAME(EVP_cast5_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_cast5_ofb(void) +{ + HANDLE_FUNCTION(EVP_cast5_ofb, NULL, 0); + return EVP_cast5_ofb(); +} + +int GET_BOOL_NAME(EVP_des_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_des_cbc(void) +{ + HANDLE_FUNCTION(EVP_des_cbc, NULL, 0); + return EVP_des_cbc(); +} + +#undef EVP_des_cfb +int GET_BOOL_NAME(EVP_des_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_des_cfb(void) +{ + HANDLE_FUNCTION(EVP_des_cfb, NULL, 0); + return EVP_des_cfb64(); +} + +int GET_BOOL_NAME(EVP_des_cfb1) = 0; +const EVP_CIPHER *MOCK_EVP_des_cfb1(void) +{ + HANDLE_FUNCTION(EVP_des_cfb1, NULL, 0); + return EVP_des_cfb1(); +} + +int GET_BOOL_NAME(EVP_des_cfb8) = 0; +const EVP_CIPHER *MOCK_EVP_des_cfb8(void) +{ + HANDLE_FUNCTION(EVP_des_cfb8, NULL, 0); + return EVP_des_cfb8(); +} + +int GET_BOOL_NAME(EVP_des_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ecb(void) +{ + HANDLE_FUNCTION(EVP_des_ecb, NULL, 0); + return EVP_des_ecb(); +} + +int GET_BOOL_NAME(EVP_des_ede3_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede3_cbc(void) +{ + HANDLE_FUNCTION(EVP_des_ede3_cbc, NULL, 0); + return EVP_des_ede3_cbc(); +} + +#undef EVP_des_ede3_cfb +int GET_BOOL_NAME(EVP_des_ede3_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede3_cfb(void) +{ + HANDLE_FUNCTION(EVP_des_ede3_cfb, NULL, 0); + return EVP_des_ede3_cfb64(); +} + +int GET_BOOL_NAME(EVP_des_ede3_cfb1) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede3_cfb1(void) +{ + HANDLE_FUNCTION(EVP_des_ede3_cfb1, NULL, 0); + return EVP_des_ede3_cfb1(); +} + +int GET_BOOL_NAME(EVP_des_ede3_cfb8) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede3_cfb8(void) +{ + HANDLE_FUNCTION(EVP_des_ede3_cfb8, NULL, 0); + return EVP_des_ede3_cfb8(); +} + +int GET_BOOL_NAME(EVP_des_ede3_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede3_ecb(void) +{ + HANDLE_FUNCTION(EVP_des_ede3_ecb, NULL, 0); + return EVP_des_ede3_ecb(); +} + +int GET_BOOL_NAME(EVP_des_ede3_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede3_ofb(void) +{ + HANDLE_FUNCTION(EVP_des_ede3_ofb, NULL, 0); + return EVP_des_ede3_ofb(); +} + +int GET_BOOL_NAME(EVP_des_ede3_wrap) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede3_wrap(void) +{ + HANDLE_FUNCTION(EVP_des_ede3_wrap, NULL, 0); + return EVP_des_ede3_wrap(); +} + +int GET_BOOL_NAME(EVP_des_ede_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede_cbc(void) +{ + HANDLE_FUNCTION(EVP_des_ede_cbc, NULL, 0); + return EVP_des_ede_cbc(); +} + +#undef EVP_des_ede_cfb +int GET_BOOL_NAME(EVP_des_ede_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede_cfb(void) +{ + HANDLE_FUNCTION(EVP_des_ede_cfb, NULL, 0); + return EVP_des_ede_cfb64(); +} + +int GET_BOOL_NAME(EVP_des_ede_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede_ecb(void) +{ + HANDLE_FUNCTION(EVP_des_ede_ecb, NULL, 0); + return EVP_des_ede_ecb(); +} + +int GET_BOOL_NAME(EVP_des_ede_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ede_ofb(void) +{ + HANDLE_FUNCTION(EVP_des_ede_ofb, NULL, 0); + return EVP_des_ede_ofb(); +} + +int GET_BOOL_NAME(EVP_des_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_des_ofb(void) +{ + HANDLE_FUNCTION(EVP_des_ofb, NULL, 0); + return EVP_des_ofb(); +} + +int GET_BOOL_NAME(EVP_md5) = 0; +const EVP_MD *MOCK_EVP_md5(void) +{ + HANDLE_FUNCTION(EVP_md5, NULL, 0); + return EVP_md5(); +} + +int GET_BOOL_NAME(EVP_rc2_cbc) = 0; +const EVP_CIPHER *MOCK_EVP_rc2_cbc(void) +{ + HANDLE_FUNCTION(EVP_rc2_cbc, NULL, 0); + return EVP_rc2_cbc(); +} + +#undef EVP_rc2_cfb +int GET_BOOL_NAME(EVP_rc2_cfb) = 0; +const EVP_CIPHER *MOCK_EVP_rc2_cfb(void) +{ + HANDLE_FUNCTION(EVP_rc2_cfb, NULL, 0); + return EVP_rc2_cfb64(); +} + +int GET_BOOL_NAME(EVP_rc2_ecb) = 0; +const EVP_CIPHER *MOCK_EVP_rc2_ecb(void) +{ + HANDLE_FUNCTION(EVP_rc2_ecb, NULL, 0); + return EVP_rc2_ecb(); +} + +int GET_BOOL_NAME(EVP_rc2_ofb) = 0; +const EVP_CIPHER *MOCK_EVP_rc2_ofb(void) +{ + HANDLE_FUNCTION(EVP_rc2_ofb, NULL, 0); + return EVP_rc2_ofb(); +} + +int GET_BOOL_NAME(EVP_rc4) = 0; +const EVP_CIPHER *MOCK_EVP_rc4(void) +{ + HANDLE_FUNCTION(EVP_rc4, NULL, 0); + return EVP_rc4(); +} + +int GET_BOOL_NAME(EVP_sha1) = 0; +const EVP_MD *MOCK_EVP_sha1(void) +{ + HANDLE_FUNCTION(EVP_sha1, NULL, 0); + return EVP_sha1(); +} + +int GET_BOOL_NAME(EVP_sha224) = 0; +const EVP_MD *MOCK_EVP_sha224(void) +{ + HANDLE_FUNCTION(EVP_sha224, NULL, 0); + return EVP_sha224(); +} + +int GET_BOOL_NAME(EVP_sha256) = 0; +const EVP_MD *MOCK_EVP_sha256(void) +{ + HANDLE_FUNCTION(EVP_sha256, NULL, 0); + return EVP_sha256(); +} + +int GET_BOOL_NAME(EVP_sha384) = 0; +const EVP_MD *MOCK_EVP_sha384(void) +{ + HANDLE_FUNCTION(EVP_sha384, NULL, 0); + return EVP_sha384(); +} + +int GET_BOOL_NAME(EVP_sha512) = 0; +const EVP_MD *MOCK_EVP_sha512(void) +{ + HANDLE_FUNCTION(EVP_sha512, NULL, 0); + return EVP_sha512(); +} + +int GET_BOOL_NAME(OPENSSL_malloc) = 0; +void *MOCK_OPENSSL_malloc(size_t num) +{ + HANDLE_FUNCTION(OPENSSL_malloc, NULL, 0); + return OPENSSL_malloc(num); +} + +int GET_BOOL_NAME(OPENSSL_realloc) = 0; +void *MOCK_OPENSSL_realloc(void *addr, size_t num) +{ + HANDLE_FUNCTION(OPENSSL_realloc, NULL, 0); + return OPENSSL_realloc(addr, num); +} + +int GET_BOOL_NAME(PEM_read_bio_PUBKEY) = 0; +EVP_PKEY *MOCK_PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u) +{ + HANDLE_FUNCTION(PEM_read_bio_PUBKEY, NULL, BIO_reset_just_called); + return PEM_read_bio_PUBKEY(bp, x, cb, u); +} + +int GET_BOOL_NAME(PEM_read_bio_Parameters) = 0; +EVP_PKEY *MOCK_PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x) +{ + HANDLE_FUNCTION(PEM_read_bio_Parameters, NULL, BIO_reset_just_called); + return PEM_read_bio_Parameters(bp, x); +} + +int GET_BOOL_NAME(PEM_read_bio_PrivateKey) = 0; +EVP_PKEY *MOCK_PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u) +{ + HANDLE_FUNCTION(PEM_read_bio_PrivateKey, NULL, BIO_reset_just_called); + return PEM_read_bio_PrivateKey(bp, x, cb, u); +} + +int GET_BOOL_NAME(PEM_read_bio_X509) = 0; +X509 *MOCK_PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u) +{ + HANDLE_FUNCTION(PEM_read_bio_X509, NULL, BIO_reset_just_called); + return PEM_read_bio_X509(bp, x, cb, u); +} + +int GET_BOOL_NAME(PEM_write_bio_PKCS8PrivateKey) = 0; +int MOCK_PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u) +{ + HANDLE_FUNCTION(PEM_write_bio_PKCS8PrivateKey, 0, 0); + return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, kstr, klen, cb, u); +} + +int GET_BOOL_NAME(PEM_write_bio_PUBKEY) = 0; +int MOCK_PEM_write_bio_PUBKEY(BIO *bp, EVP_PKEY *x) +{ + HANDLE_FUNCTION(PEM_write_bio_PUBKEY, 0, 0); + return PEM_write_bio_PUBKEY(bp, x); +} + +int GET_BOOL_NAME(PEM_write_bio_Parameters) = 0; +int MOCK_PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x) +{ + HANDLE_FUNCTION(PEM_write_bio_Parameters, 0, 0); + return PEM_write_bio_Parameters(bp, x); +} + +int GET_BOOL_NAME(PEM_write_bio_PrivateKey) = 0; +int MOCK_PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u) +{ + HANDLE_FUNCTION(PEM_write_bio_PrivateKey, 0, 0); + return PEM_write_bio_PrivateKey(bp, x, enc, kstr, klen, cb, u); +} + +int GET_BOOL_NAME(PKCS5_PBKDF2_HMAC); +int MOCK_PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, const EVP_MD *digest, int keylen, unsigned char *out) +{ + HANDLE_FUNCTION(PKCS5_PBKDF2_HMAC, 0, 0); + return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, digest, keylen, out); +} + +int GET_BOOL_NAME(RAND_bytes) = 0; +int MOCK_RAND_bytes(unsigned char *buf, int num) +{ + HANDLE_FUNCTION(RAND_bytes, 0, 0); + return RAND_bytes(buf, num); +} + +int GET_BOOL_NAME(RSA_private_decrypt) = 0; +int MOCK_RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) +{ + HANDLE_FUNCTION(RSA_private_decrypt, -1, 0); + return RSA_private_decrypt(flen, from, to, rsa, padding); +} + +int GET_BOOL_NAME(RSA_private_encrypt) = 0; +int MOCK_RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) +{ + HANDLE_FUNCTION(RSA_private_encrypt, -1, 0); + return RSA_private_encrypt(flen, from, to, rsa, padding); +} + +int GET_BOOL_NAME(RSA_public_decrypt) = 0; +int MOCK_RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) +{ + HANDLE_FUNCTION(RSA_public_decrypt, -1, 0); + return RSA_public_decrypt(flen, from, to, rsa, padding); +} + +int GET_BOOL_NAME(RSA_public_encrypt) = 0; +int MOCK_RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) +{ + HANDLE_FUNCTION(RSA_public_encrypt, -1, 0); + return RSA_public_encrypt(flen, from, to, rsa, padding); +} + +int GET_BOOL_NAME(X509_get_pubkey) = 0; +EVP_PKEY *MOCK_X509_get_pubkey(X509 *x) +{ + HANDLE_FUNCTION(X509_get_pubkey, NULL, 0); + return X509_get_pubkey(x); +} + +int GET_BOOL_NAME(d2i_DHparams_bio) = 0; +DH *MOCK_d2i_DHparams_bio(BIO *bp, DH **x) +{ + HANDLE_FUNCTION(d2i_DHparams_bio, NULL, BIO_reset_just_called); + return d2i_DHparams_bio(bp, x); +} + +int GET_BOOL_NAME(d2i_DSAparams_bio) = 0; +DSA *MOCK_d2i_DSAparams_bio(BIO *bp, DSA **x) +{ + HANDLE_FUNCTION(d2i_DSAparams_bio, NULL, BIO_reset_just_called); + return d2i_DSAparams_bio(bp, x); +} + +int GET_BOOL_NAME(d2i_ECPKParameters_bio) = 0; +EC_GROUP *MOCK_d2i_ECPKParameters_bio(BIO *bp, EC_GROUP **x) +{ + HANDLE_FUNCTION(d2i_ECPKParameters_bio, NULL, BIO_reset_just_called); + return d2i_ECPKParameters_bio(bp, x); +} + +int GET_BOOL_NAME(d2i_PKCS8PrivateKey_bio) = 0; +EVP_PKEY *MOCK_d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u) +{ + HANDLE_FUNCTION(d2i_PKCS8PrivateKey_bio, NULL, BIO_reset_just_called); + return d2i_PKCS8PrivateKey_bio(bp, x, cb, u); +} + +int GET_BOOL_NAME(d2i_PUBKEY_bio) = 0; +EVP_PKEY *MOCK_d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a) +{ + HANDLE_FUNCTION(d2i_PUBKEY_bio, NULL, BIO_reset_just_called); + return d2i_PUBKEY_bio(bp, a); +} + +int GET_BOOL_NAME(d2i_PrivateKey_bio) = 0; +EVP_PKEY *MOCK_d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a) +{ + HANDLE_FUNCTION(d2i_PrivateKey_bio, NULL, BIO_reset_just_called); + return d2i_PrivateKey_bio(bp, a); +} + +int GET_BOOL_NAME(d2i_X509_bio) = 0; +X509 *MOCK_d2i_X509_bio(BIO *bp, X509 **x509) +{ + HANDLE_FUNCTION(d2i_X509_bio, NULL, BIO_reset_just_called); + return d2i_X509_bio(bp, x509); +} + +int GET_BOOL_NAME(i2d_DHparams_bio) = 0; +int MOCK_i2d_DHparams_bio(BIO *bp, const DH *x) +{ + HANDLE_FUNCTION(i2d_DHparams_bio, 0, 0); + return i2d_DHparams_bio(bp, x); +} + +int GET_BOOL_NAME(i2d_DSAparams_bio) = 0; +int MOCK_i2d_DSAparams_bio(BIO *bp, const DSA *x) +{ + HANDLE_FUNCTION(i2d_DSAparams_bio, 0, 0); + return i2d_DSAparams_bio(bp, x); +} + +int GET_BOOL_NAME(i2d_ECPKParameters_bio) = 0; +int MOCK_i2d_ECPKParameters_bio(BIO *bp, const EC_GROUP *x) +{ + HANDLE_FUNCTION(i2d_ECPKParameters_bio, 0, 0); + return i2d_ECPKParameters_bio(bp, x); +} + +int GET_BOOL_NAME(i2d_PKCS8PrivateKey_bio) = 0; +int MOCK_i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, char *kstr, int klen, pem_password_cb *cb, void *u) +{ + HANDLE_FUNCTION(i2d_PKCS8PrivateKey_bio, 0, 0); + return i2d_PKCS8PrivateKey_bio(bp, x, enc, kstr, klen, cb, u); +} + +int GET_BOOL_NAME(i2d_PUBKEY_bio) = 0; +int MOCK_i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey) +{ + HANDLE_FUNCTION(i2d_PUBKEY_bio, 0, 0); + return i2d_PUBKEY_bio(bp, pkey); +} + +int GET_BOOL_NAME(i2d_PrivateKey_bio) = 0; +int MOCK_i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey) +{ + HANDLE_FUNCTION(i2d_PrivateKey_bio, 0, 0); + return i2d_PrivateKey_bio(bp, pkey); +} diff --git a/tests/openssl_mock_impl.h b/tests/openssl_mock_impl.h new file mode 100644 index 0000000..c0c089c --- /dev/null +++ b/tests/openssl_mock_impl.h @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file openssl_mock_impl.h + * @brief + */ + +#ifndef OPENSSL_MOCK_IMPL_H +#define OPENSSL_MOCK_IMPL_H + +#include "openssl_mock_functions.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define GET_BOOL_NAME(FNAME) MOCK_fail_##FNAME + +extern unsigned MOCK_fail_nth; + +extern int GET_BOOL_NAME(open); +extern int GET_BOOL_NAME(read); +extern int GET_BOOL_NAME(BIO_flush); +extern int GET_BOOL_NAME(BIO_flush); +extern int GET_BOOL_NAME(BIO_get_mem_data); +extern int GET_BOOL_NAME(BIO_new); +extern int GET_BOOL_NAME(BIO_new_mem_buf); +extern int GET_BOOL_NAME(BIO_read); +extern int GET_BOOL_NAME(BIO_reset); +extern int GET_BOOL_NAME(BIO_write); +extern int GET_BOOL_NAME(CMAC_CTX_new); +extern int GET_BOOL_NAME(CMAC_Init); +extern int GET_BOOL_NAME(DES_random_key); +extern int GET_BOOL_NAME(DH_KDF_X9_42); +extern int GET_BOOL_NAME(ECDH_KDF_X9_62); +extern int GET_BOOL_NAME(EC_GROUP_get_asn1_flag); +extern int GET_BOOL_NAME(EC_GROUP_get_curve_name); +extern int GET_BOOL_NAME(EC_KEY_new); +extern int GET_BOOL_NAME(EC_KEY_set_group); +extern int GET_BOOL_NAME(EVP_CIPHER_CTX_block_size); +extern int GET_BOOL_NAME(EVP_CIPHER_CTX_cleanup); +extern int GET_BOOL_NAME(EVP_CIPHER_CTX_ctrl); +extern int GET_BOOL_NAME(EVP_CIPHER_CTX_new); +extern int GET_BOOL_NAME(EVP_CIPHER_CTX_set_key_length); +extern int GET_BOOL_NAME(EVP_CIPHER_CTX_set_padding); +extern int GET_BOOL_NAME(EVP_CIPHER_iv_length); +extern int GET_BOOL_NAME(EVP_CipherFinal); +extern int GET_BOOL_NAME(EVP_CipherUpdate); +extern int GET_BOOL_NAME(EVP_DigestFinal_ex); +extern int GET_BOOL_NAME(EVP_DigestInit); +extern int GET_BOOL_NAME(EVP_DigestSignFinal); +extern int GET_BOOL_NAME(EVP_DigestSignInit); +extern int GET_BOOL_NAME(EVP_DigestSignUpdate); +extern int GET_BOOL_NAME(EVP_DigestUpdate); +extern int GET_BOOL_NAME(EVP_DigestVerifyFinal); +extern int GET_BOOL_NAME(EVP_DigestVerifyInit); +extern int GET_BOOL_NAME(EVP_DigestVerifyUpdate); +extern int GET_BOOL_NAME(EVP_MD_CTX_create); +extern int GET_BOOL_NAME(EVP_MD_CTX_pkey_ctx); +extern int GET_BOOL_NAME(EVP_MD_CTX_size); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_ctrl); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_get0_pkey); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_new); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_new_id); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_set_dh_paramgen_generator); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_set_dh_paramgen_prime_len); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_set_dsa_paramgen_bits); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_set_ec_param_enc); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_set_ec_paramgen_curve_nid); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_set_rsa_keygen_bits); +extern int GET_BOOL_NAME(EVP_PKEY_CTX_set_rsa_padding); +extern int GET_BOOL_NAME(EVP_PKEY_assign); +extern int GET_BOOL_NAME(EVP_PKEY_assign_DH); +extern int GET_BOOL_NAME(EVP_PKEY_assign_DSA); +extern int GET_BOOL_NAME(EVP_PKEY_assign_EC_KEY); +extern int GET_BOOL_NAME(EVP_PKEY_bits); +extern int GET_BOOL_NAME(EVP_PKEY_decrypt_old); +extern int GET_BOOL_NAME(EVP_PKEY_derive); +extern int GET_BOOL_NAME(EVP_PKEY_derive_init); +extern int GET_BOOL_NAME(EVP_PKEY_derive_set_peer); +extern int GET_BOOL_NAME(EVP_PKEY_encrypt_old); +extern int GET_BOOL_NAME(EVP_PKEY_keygen); +extern int GET_BOOL_NAME(EVP_PKEY_keygen_init); +extern int GET_BOOL_NAME(EVP_PKEY_new); +extern int GET_BOOL_NAME(EVP_PKEY_new_mac_key); +extern int GET_BOOL_NAME(EVP_PKEY_paramgen); +extern int GET_BOOL_NAME(EVP_PKEY_paramgen_init); +extern int GET_BOOL_NAME(EVP_PKEY_size); +extern int GET_BOOL_NAME(EVP_PKEY_up_ref); +extern int GET_BOOL_NAME(EVP_aes_128_cbc); +extern int GET_BOOL_NAME(EVP_aes_128_ccm); +extern int GET_BOOL_NAME(EVP_aes_128_cfb); +extern int GET_BOOL_NAME(EVP_aes_128_cfb1); +extern int GET_BOOL_NAME(EVP_aes_128_cfb8); +extern int GET_BOOL_NAME(EVP_aes_128_ctr); +extern int GET_BOOL_NAME(EVP_aes_128_ecb); +extern int GET_BOOL_NAME(EVP_aes_128_gcm); +extern int GET_BOOL_NAME(EVP_aes_128_ofb); +extern int GET_BOOL_NAME(EVP_aes_128_wrap); +extern int GET_BOOL_NAME(EVP_aes_192_cbc); +extern int GET_BOOL_NAME(EVP_aes_192_ccm); +extern int GET_BOOL_NAME(EVP_aes_192_cfb); +extern int GET_BOOL_NAME(EVP_aes_192_cfb1); +extern int GET_BOOL_NAME(EVP_aes_192_cfb8); +extern int GET_BOOL_NAME(EVP_aes_192_ctr); +extern int GET_BOOL_NAME(EVP_aes_192_ecb); +extern int GET_BOOL_NAME(EVP_aes_192_gcm); +extern int GET_BOOL_NAME(EVP_aes_192_ofb); +extern int GET_BOOL_NAME(EVP_aes_192_wrap); +extern int GET_BOOL_NAME(EVP_aes_256_cbc); +extern int GET_BOOL_NAME(EVP_aes_256_ccm); +extern int GET_BOOL_NAME(EVP_aes_256_cfb); +extern int GET_BOOL_NAME(EVP_aes_256_cfb1); +extern int GET_BOOL_NAME(EVP_aes_256_cfb8); +extern int GET_BOOL_NAME(EVP_aes_256_ctr); +extern int GET_BOOL_NAME(EVP_aes_256_ecb); +extern int GET_BOOL_NAME(EVP_aes_256_gcm); +extern int GET_BOOL_NAME(EVP_aes_256_ofb); +extern int GET_BOOL_NAME(EVP_aes_256_wrap); +extern int GET_BOOL_NAME(EVP_cast5_cbc); +extern int GET_BOOL_NAME(EVP_cast5_cfb); +extern int GET_BOOL_NAME(EVP_cast5_ecb); +extern int GET_BOOL_NAME(EVP_cast5_ofb); +extern int GET_BOOL_NAME(EVP_des_cbc); +extern int GET_BOOL_NAME(EVP_des_cfb); +extern int GET_BOOL_NAME(EVP_des_cfb1); +extern int GET_BOOL_NAME(EVP_des_cfb8); +extern int GET_BOOL_NAME(EVP_des_ecb); +extern int GET_BOOL_NAME(EVP_des_ede3_cbc); +extern int GET_BOOL_NAME(EVP_des_ede3_cfb); +extern int GET_BOOL_NAME(EVP_des_ede3_cfb1); +extern int GET_BOOL_NAME(EVP_des_ede3_cfb8); +extern int GET_BOOL_NAME(EVP_des_ede3_ecb); +extern int GET_BOOL_NAME(EVP_des_ede3_ofb); +extern int GET_BOOL_NAME(EVP_des_ede3_wrap); +extern int GET_BOOL_NAME(EVP_des_ede_cbc); +extern int GET_BOOL_NAME(EVP_des_ede_cfb); +extern int GET_BOOL_NAME(EVP_des_ede_ecb); +extern int GET_BOOL_NAME(EVP_des_ede_ofb); +extern int GET_BOOL_NAME(EVP_des_ofb); +extern int GET_BOOL_NAME(EVP_md5); +extern int GET_BOOL_NAME(EVP_rc2_cbc); +extern int GET_BOOL_NAME(EVP_rc2_cfb); +extern int GET_BOOL_NAME(EVP_rc2_ecb); +extern int GET_BOOL_NAME(EVP_rc2_ofb); +extern int GET_BOOL_NAME(EVP_rc4); +extern int GET_BOOL_NAME(EVP_sha1); +extern int GET_BOOL_NAME(EVP_sha224); +extern int GET_BOOL_NAME(EVP_sha256); +extern int GET_BOOL_NAME(EVP_sha384); +extern int GET_BOOL_NAME(EVP_sha512); +extern int GET_BOOL_NAME(OPENSSL_malloc); +extern int GET_BOOL_NAME(OPENSSL_realloc); +extern int GET_BOOL_NAME(PEM_read_bio_PUBKEY); +extern int GET_BOOL_NAME(PEM_read_bio_Parameters); +extern int GET_BOOL_NAME(PEM_read_bio_PrivateKey); +extern int GET_BOOL_NAME(PEM_read_bio_X509); +extern int GET_BOOL_NAME(PEM_write_bio_PKCS8PrivateKey); +extern int GET_BOOL_NAME(PEM_write_bio_PUBKEY); +extern int GET_BOOL_NAME(PEM_write_bio_Parameters); +extern int GET_BOOL_NAME(PEM_write_bio_PrivateKey); +extern int GET_BOOL_NAME(PKCS5_PBKDF2_HMAC); +extern int GET_BOOL_NAME(RAND_bytes); +extern int GET_BOOL_NAME(RSA_private_decrypt); +extern int GET_BOOL_NAME(RSA_private_encrypt); +extern int GET_BOOL_NAME(RSA_public_decrypt); +extern int GET_BOOL_NAME(RSA_public_encrypt); +extern int GET_BOOL_NAME(X509_get_pubkey); +extern int GET_BOOL_NAME(d2i_DHparams_bio); +extern int GET_BOOL_NAME(d2i_DSAparams_bio); +extern int GET_BOOL_NAME(d2i_ECPKParameters_bio); +extern int GET_BOOL_NAME(d2i_PKCS8PrivateKey_bio); +extern int GET_BOOL_NAME(d2i_PUBKEY_bio); +extern int GET_BOOL_NAME(d2i_PrivateKey_bio); +extern int GET_BOOL_NAME(d2i_X509_bio); +extern int GET_BOOL_NAME(i2d_DHparams_bio); +extern int GET_BOOL_NAME(i2d_DSAparams_bio); +extern int GET_BOOL_NAME(i2d_ECPKParameters_bio); +extern int GET_BOOL_NAME(i2d_PKCS8PrivateKey_bio); +extern int GET_BOOL_NAME(i2d_PUBKEY_bio); +extern int GET_BOOL_NAME(i2d_PrivateKey_bio); + +#ifdef __cplusplus +} +#endif + +#endif // OPENSSL_MOCK_IMPL_H diff --git a/tests/openssl_mock_redefine.h b/tests/openssl_mock_redefine.h new file mode 100644 index 0000000..06b5adc --- /dev/null +++ b/tests/openssl_mock_redefine.h @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file openssl_mock_redefine.h + * @brief + */ + +/* This file is to be included in the source files that want to mockup + * OpenSSL and libc. After OpenSSL and libc headers. E.g. + +#include +#include + +#ifdef OPENSSL_MOCKUP +#include "../tests/openssl_mock_redefine.h" +#endif + +*/ + +#include "openssl_mock_functions.h" + +#define open(a, b) MOCK_open(a, b) +#define read(a, b, c) MOCK_read(a, b, c) +#undef BIO_flush +#define BIO_flush(a) MOCK_BIO_flush(a) +#undef BIO_get_mem_data +#define BIO_get_mem_data(a, b) MOCK_BIO_get_mem_data(a, (char **)b) +#define BIO_new(a) MOCK_BIO_new(a) +#define BIO_new_mem_buf(a, b) MOCK_BIO_new_mem_buf(a, b) +#define BIO_read(a, b, c) MOCK_BIO_read(a, b, c) +#undef BIO_reset +#define BIO_reset(a) MOCK_BIO_reset(a) +#define BIO_write(a, b, c) MOCK_BIO_write(a, b, c) +#define CMAC_CTX_new() MOCK_CMAC_CTX_new() +#define CMAC_Init(a, b, c, d, e) MOCK_CMAC_Init(a, b, c, d, e) +#define DES_random_key(a) MOCK_DES_random_key(a) +#define DH_KDF_X9_42(a, b, c, d, e, f, g, h) MOCK_DH_KDF_X9_42(a, b, c, d, e, f, g, h) +#define ECDH_KDF_X9_62(a, b, c, d, e, f, g) MOCK_ECDH_KDF_X9_62(a, b, c, d, e, f, g) +#define EC_GROUP_get_asn1_flag(a) MOCK_EC_GROUP_get_asn1_flag(a) +#define EC_GROUP_get_curve_name(a) MOCK_EC_GROUP_get_curve_name(a) +#define EC_KEY_new() MOCK_EC_KEY_new() +#define EC_KEY_set_group(a, b) MOCK_EC_KEY_set_group(a, b) +#define EVP_CIPHER_CTX_block_size(a) MOCK_EVP_CIPHER_CTX_block_size(a) +#undef EVP_CIPHER_CTX_cleanup +#define EVP_CIPHER_CTX_cleanup(a) MOCK_EVP_CIPHER_CTX_cleanup(a) +#define EVP_CIPHER_CTX_ctrl(a, b, c, d) MOCK_EVP_CIPHER_CTX_ctrl(a, b, c, d) +#define EVP_CIPHER_CTX_new() MOCK_EVP_CIPHER_CTX_new() +#define EVP_CIPHER_CTX_set_key_length(a, b) MOCK_EVP_CIPHER_CTX_set_key_length(a, b) +#define EVP_CIPHER_CTX_set_padding(a, b) MOCK_EVP_CIPHER_CTX_set_padding(a, b) +#define EVP_CIPHER_iv_length(a) MOCK_EVP_CIPHER_iv_length(a) +#define EVP_CipherFinal(a, b, c) MOCK_EVP_CipherFinal(a, b, c) +#define EVP_CipherInit_ex(a, b, c, d, e, f) MOCK_EVP_CipherInit_ex(a, b, c, d, e, f) +#define EVP_CipherUpdate(a, b, c, d, e) MOCK_EVP_CipherUpdate(a, b, c, d, e) +#define EVP_DigestFinal_ex(a, b, c) MOCK_EVP_DigestFinal_ex(a, b, c) +#define EVP_DigestInit(a, b) MOCK_EVP_DigestInit(a, b) +#define EVP_DigestSignFinal(a, b, c) MOCK_EVP_DigestSignFinal(a, b, c) +#define EVP_DigestSignInit(a, b, c, d, e) MOCK_EVP_DigestSignInit(a, b, c, d, e) +#undef EVP_DigestSignUpdate +#define EVP_DigestSignUpdate(a, b, c) MOCK_EVP_DigestSignUpdate(a, b, c) +#undef EVP_DigestUpdate +#define EVP_DigestUpdate(a, b, c) MOCK_EVP_DigestUpdate(a, b, c) +#define EVP_DigestVerifyFinal(a, b, c) MOCK_EVP_DigestVerifyFinal(a, b, c) +#define EVP_DigestVerifyInit(a, b, c, d, e) MOCK_EVP_DigestVerifyInit(a, b, c, d, e) +#undef EVP_DigestVerifyUpdate +#define EVP_DigestVerifyUpdate(a, b, c) MOCK_EVP_DigestVerifyUpdate(a, b, c) +#undef EVP_MD_CTX_create +#define EVP_MD_CTX_create() MOCK_EVP_MD_CTX_create() +#define EVP_MD_CTX_pkey_ctx(a) MOCK_EVP_MD_CTX_pkey_ctx(a) +#undef EVP_MD_CTX_size +#define EVP_MD_CTX_size(a) MOCK_EVP_MD_CTX_size(a) +#define EVP_PKEY_CTX_ctrl(a, b, c, d, e, f) MOCK_EVP_PKEY_CTX_ctrl(a, b, c, d, e, f) +#define EVP_PKEY_CTX_get0_pkey(a) MOCK_EVP_PKEY_CTX_get0_pkey(a) +#define EVP_PKEY_CTX_new(a, b) MOCK_EVP_PKEY_CTX_new(a, b) +#define EVP_PKEY_CTX_new_id(a, b) MOCK_EVP_PKEY_CTX_new_id(a, b) +#undef EVP_PKEY_CTX_set_dh_paramgen_generator +#define EVP_PKEY_CTX_set_dh_paramgen_generator(a, b) MOCK_EVP_PKEY_CTX_set_dh_paramgen_generator(a, b) +#undef EVP_PKEY_CTX_set_dh_paramgen_prime_len +#define EVP_PKEY_CTX_set_dh_paramgen_prime_len(a, b) MOCK_EVP_PKEY_CTX_set_dh_paramgen_prime_len(a, b) +#undef EVP_PKEY_CTX_set_dsa_paramgen_bits +#define EVP_PKEY_CTX_set_dsa_paramgen_bits(a, b) MOCK_EVP_PKEY_CTX_set_dsa_paramgen_bits(a, b) +#undef EVP_PKEY_CTX_set_ec_param_enc +#define EVP_PKEY_CTX_set_ec_param_enc(a, b) MOCK_EVP_PKEY_CTX_set_ec_param_enc(a, b) +#undef EVP_PKEY_CTX_set_ec_paramgen_curve_nid +#define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(a, b) MOCK_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(a, b) +#undef EVP_PKEY_CTX_set_rsa_keygen_bits +#define EVP_PKEY_CTX_set_rsa_keygen_bits(a, b) MOCK_EVP_PKEY_CTX_set_rsa_keygen_bits(a, b) +#undef EVP_PKEY_CTX_set_rsa_padding +#define EVP_PKEY_CTX_set_rsa_padding(a, b) MOCK_EVP_PKEY_CTX_set_rsa_padding(a, b) +#define EVP_PKEY_assign(a, b, c) MOCK_EVP_PKEY_assign(a, b, c) +#undef EVP_PKEY_assign_DH +#define EVP_PKEY_assign_DH(a, b) MOCK_EVP_PKEY_assign_DH(a, b) +#undef EVP_PKEY_assign_DSA +#define EVP_PKEY_assign_DSA(a, b) MOCK_EVP_PKEY_assign_DSA(a, b) +#undef EVP_PKEY_assign_EC_KEY +#define EVP_PKEY_assign_EC_KEY(a, b) MOCK_EVP_PKEY_assign_EC_KEY(a, b) +#define EVP_PKEY_bits(a) MOCK_EVP_PKEY_bits(a) +#define EVP_PKEY_decrypt_old(a, b, c, d) MOCK_EVP_PKEY_decrypt_old(a, b, c, d) +#define EVP_PKEY_derive(a, b, c) MOCK_EVP_PKEY_derive(a, b, c) +#define EVP_PKEY_derive_init(a) MOCK_EVP_PKEY_derive_init(a) +#define EVP_PKEY_derive_set_peer(a, b) MOCK_EVP_PKEY_derive_set_peer(a, b) +#define EVP_PKEY_encrypt_old(a, b, c, d) MOCK_EVP_PKEY_encrypt_old(a, b, c, d) +#define EVP_PKEY_keygen(a, b) MOCK_EVP_PKEY_keygen(a, b) +#define EVP_PKEY_keygen_init(a) MOCK_EVP_PKEY_keygen_init(a) +#define EVP_PKEY_new() MOCK_EVP_PKEY_new() +#define EVP_PKEY_new_mac_key(a, b, c, d) MOCK_EVP_PKEY_new_mac_key(a, b, c, d) +#define EVP_PKEY_paramgen(a, b) MOCK_EVP_PKEY_paramgen(a, b) +#define EVP_PKEY_paramgen_init(a) MOCK_EVP_PKEY_paramgen_init(a) +#define EVP_PKEY_size(a) MOCK_EVP_PKEY_size(a) +#define EVP_PKEY_up_ref(a) MOCK_EVP_PKEY_up_ref(a) +/* Special cases for algorithms, used as function pointers in YACA */ +#define EVP_aes_128_cbc MOCK_EVP_aes_128_cbc +#define EVP_aes_128_ccm MOCK_EVP_aes_128_ccm +#undef EVP_aes_128_cfb +#define EVP_aes_128_cfb MOCK_EVP_aes_128_cfb +#define EVP_aes_128_cfb1 MOCK_EVP_aes_128_cfb1 +#define EVP_aes_128_cfb8 MOCK_EVP_aes_128_cfb8 +#define EVP_aes_128_ctr MOCK_EVP_aes_128_ctr +#define EVP_aes_128_ecb MOCK_EVP_aes_128_ecb +#define EVP_aes_128_gcm MOCK_EVP_aes_128_gcm +#define EVP_aes_128_ofb MOCK_EVP_aes_128_ofb +#define EVP_aes_128_wrap MOCK_EVP_aes_128_wrap +#define EVP_aes_192_cbc MOCK_EVP_aes_192_cbc +#define EVP_aes_192_ccm MOCK_EVP_aes_192_ccm +#undef EVP_aes_192_cfb +#define EVP_aes_192_cfb MOCK_EVP_aes_192_cfb +#define EVP_aes_192_cfb1 MOCK_EVP_aes_192_cfb1 +#define EVP_aes_192_cfb8 MOCK_EVP_aes_192_cfb8 +#define EVP_aes_192_ctr MOCK_EVP_aes_192_ctr +#define EVP_aes_192_ecb MOCK_EVP_aes_192_ecb +#define EVP_aes_192_gcm MOCK_EVP_aes_192_gcm +#define EVP_aes_192_ofb MOCK_EVP_aes_192_ofb +#define EVP_aes_192_wrap MOCK_EVP_aes_192_wrap +#define EVP_aes_256_cbc MOCK_EVP_aes_256_cbc +#define EVP_aes_256_ccm MOCK_EVP_aes_256_ccm +#undef EVP_aes_256_cfb +#define EVP_aes_256_cfb MOCK_EVP_aes_256_cfb +#define EVP_aes_256_cfb1 MOCK_EVP_aes_256_cfb1 +#define EVP_aes_256_cfb8 MOCK_EVP_aes_256_cfb8 +#define EVP_aes_256_ctr MOCK_EVP_aes_256_ctr +#define EVP_aes_256_ecb MOCK_EVP_aes_256_ecb +#define EVP_aes_256_gcm MOCK_EVP_aes_256_gcm +#define EVP_aes_256_ofb MOCK_EVP_aes_256_ofb +#define EVP_aes_256_wrap MOCK_EVP_aes_256_wrap +#define EVP_cast5_cbc MOCK_EVP_cast5_cbc +#undef EVP_cast5_cfb +#define EVP_cast5_cfb MOCK_EVP_cast5_cfb +#define EVP_cast5_ecb MOCK_EVP_cast5_ecb +#define EVP_cast5_ofb MOCK_EVP_cast5_ofb +#define EVP_des_cbc MOCK_EVP_des_cbc +#undef EVP_des_cfb +#define EVP_des_cfb MOCK_EVP_des_cfb +#define EVP_des_cfb1 MOCK_EVP_des_cfb1 +#define EVP_des_cfb8 MOCK_EVP_des_cfb8 +#define EVP_des_ecb MOCK_EVP_des_ecb +#define EVP_des_ede3_cbc MOCK_EVP_des_ede3_cbc +#undef EVP_des_ede3_cfb +#define EVP_des_ede3_cfb MOCK_EVP_des_ede3_cfb +#define EVP_des_ede3_cfb1 MOCK_EVP_des_ede3_cfb1 +#define EVP_des_ede3_cfb8 MOCK_EVP_des_ede3_cfb8 +#define EVP_des_ede3_ecb MOCK_EVP_des_ede3_ecb +#define EVP_des_ede3_ofb MOCK_EVP_des_ede3_ofb +#define EVP_des_ede3_wrap MOCK_EVP_des_ede3_wrap +#define EVP_des_ede_cbc MOCK_EVP_des_ede_cbc +#undef EVP_des_ede_cfb +#define EVP_des_ede_cfb MOCK_EVP_des_ede_cfb +#define EVP_des_ede_ecb MOCK_EVP_des_ede_ecb +#define EVP_des_ede_ofb MOCK_EVP_des_ede_ofb +#define EVP_des_ofb MOCK_EVP_des_ofb +#define EVP_md5 MOCK_EVP_md5 +#define EVP_rc2_cbc MOCK_EVP_rc2_cbc +#undef EVP_rc2_cfb +#define EVP_rc2_cfb MOCK_EVP_rc2_cfb +#define EVP_rc2_ecb MOCK_EVP_rc2_ecb +#define EVP_rc2_ofb MOCK_EVP_rc2_ofb +#define EVP_rc4 MOCK_EVP_rc4 +#define EVP_sha1 MOCK_EVP_sha1 +#define EVP_sha224 MOCK_EVP_sha224 +#define EVP_sha256 MOCK_EVP_sha256 +#define EVP_sha384 MOCK_EVP_sha384 +#define EVP_sha512 MOCK_EVP_sha512 +#undef OPENSSL_malloc +#define OPENSSL_malloc(a) MOCK_OPENSSL_malloc(a) +#undef OPENSSL_realloc +#define OPENSSL_realloc(a, b) MOCK_OPENSSL_realloc(a, b) +#define PEM_read_bio_PUBKEY(a, b, c, d) MOCK_PEM_read_bio_PUBKEY(a, b, c, d) +#define PEM_read_bio_Parameters(a, b) MOCK_PEM_read_bio_Parameters(a, b) +#define PEM_read_bio_PrivateKey(a, b, c, d) MOCK_PEM_read_bio_PrivateKey(a, b, c, d) +#define PEM_read_bio_X509(a, b, c, d) MOCK_PEM_read_bio_X509(a, b, c, d) +#define PEM_write_bio_PKCS8PrivateKey(a, b, c, d, e, f, g) MOCK_PEM_write_bio_PKCS8PrivateKey(a, b, c, d, e, f, g) +#define PEM_write_bio_PUBKEY(a, b) MOCK_PEM_write_bio_PUBKEY(a, b) +#define PEM_write_bio_Parameters(a, b) MOCK_PEM_write_bio_Parameters(a, b) +#define PEM_write_bio_PrivateKey(a, b, c, d, e, f, g) MOCK_PEM_write_bio_PrivateKey(a, b, c, d, e, f, g) +#define PKCS5_PBKDF2_HMAC(a, b, c, d, e, f, g, h) MOCK_PKCS5_PBKDF2_HMAC(a, b, c, d, e, f, g, h) +#define RAND_bytes(a, b) MOCK_RAND_bytes(a, b) +/* 4 Special cases, used as function pointers in YACA */ +#define RSA_private_decrypt MOCK_RSA_private_decrypt +#define RSA_private_encrypt MOCK_RSA_private_encrypt +#define RSA_public_decrypt MOCK_RSA_public_decrypt +#define RSA_public_encrypt MOCK_RSA_public_encrypt +#define X509_get_pubkey(a) MOCK_X509_get_pubkey(a) +#undef d2i_DHparams_bio +#define d2i_DHparams_bio(a, b) MOCK_d2i_DHparams_bio(a, b) +#undef d2i_DSAparams_bio +#define d2i_DSAparams_bio(a, b) MOCK_d2i_DSAparams_bio(a, b) +#undef d2i_ECPKParameters_bio +#define d2i_ECPKParameters_bio(a, b) MOCK_d2i_ECPKParameters_bio(a, b) +#define d2i_PKCS8PrivateKey_bio(a, b, c, d) MOCK_d2i_PKCS8PrivateKey_bio(a, b, c, d) +#define d2i_PUBKEY_bio(a, b) MOCK_d2i_PUBKEY_bio(a, b) +#define d2i_PrivateKey_bio(a, b) MOCK_d2i_PrivateKey_bio(a, b) +#define d2i_X509_bio(a, b) MOCK_d2i_X509_bio(a, b) +#undef i2d_DHparams_bio +#define i2d_DHparams_bio(a, b) MOCK_i2d_DHparams_bio(a, b) +#undef i2d_DSAparams_bio +#define i2d_DSAparams_bio(a, b) MOCK_i2d_DSAparams_bio(a, b) +#undef i2d_ECPKParameters_bio +#define i2d_ECPKParameters_bio(a, b) MOCK_i2d_ECPKParameters_bio(a, b) +#define i2d_PKCS8PrivateKey_bio(a, b, c, d, e, f, g) MOCK_i2d_PKCS8PrivateKey_bio(a, b, c, d, e, f, g) +#define i2d_PUBKEY_bio(a, b) MOCK_i2d_PUBKEY_bio(a, b) +#define i2d_PrivateKey_bio(a, b) MOCK_i2d_PrivateKey_bio(a, b) -- 2.7.4 From cd5ade8ce65240e9016e0d427f0ee2d6299cc6c3 Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Wed, 6 May 2020 14:35:53 +0200 Subject: [PATCH 02/16] Unit and system tests for YACA using OpenSSL mockup Those tests use OpenSSL mockup to test behaviour of YACA when OpenSSL fails. This code covers remaining ~16% of YACA's code. Change-Id: I1c22097155dc37a56c397b024abbc04a1c60faba --- tests/CMakeLists.txt | 8 + tests/common.h | 48 ++++ tests/mock_test_crypto.cpp | 113 ++++++++ tests/mock_test_digest.cpp | 83 ++++++ tests/mock_test_encrypt.cpp | 684 ++++++++++++++++++++++++++++++++++++++++++++ tests/mock_test_key.cpp | 573 +++++++++++++++++++++++++++++++++++++ tests/mock_test_rsa.cpp | 133 +++++++++ tests/mock_test_seal.cpp | 558 ++++++++++++++++++++++++++++++++++++ tests/mock_test_sign.cpp | 242 ++++++++++++++++ tests/mock_test_simple.cpp | 251 ++++++++++++++++ 10 files changed, 2693 insertions(+) create mode 100644 tests/mock_test_crypto.cpp create mode 100644 tests/mock_test_digest.cpp create mode 100644 tests/mock_test_encrypt.cpp create mode 100644 tests/mock_test_key.cpp create mode 100644 tests/mock_test_rsa.cpp create mode 100644 tests/mock_test_seal.cpp create mode 100644 tests/mock_test_sign.cpp create mode 100644 tests/mock_test_simple.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ad3eb0d..8e81541 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -34,6 +34,14 @@ SET(TESTS_SOURCES test_seal.cpp test_sign.cpp openssl_mock_impl.c + mock_test_crypto.cpp + mock_test_key.cpp + mock_test_simple.cpp + mock_test_rsa.cpp + mock_test_digest.cpp + mock_test_encrypt.cpp + mock_test_seal.cpp + mock_test_sign.cpp ) FIND_PACKAGE(Boost REQUIRED unit_test_framework) diff --git a/tests/common.h b/tests/common.h index 6c661e3..38d5122 100644 --- a/tests/common.h +++ b/tests/common.h @@ -31,6 +31,12 @@ #include #include "../src/debug.h" +#include "openssl_mock_impl.h" + +#include +#include +#include "../src/debug.h" + constexpr size_t INPUT_DATA_SIZE = 4096; constexpr char INPUT_DATA[INPUT_DATA_SIZE] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus congue semper ipsum, ac convallis magna rhoncus sit amet. Donec pellentesque maximus convallis. Mauris ut egestas sem. Maecenas efficitur suscipit auctor. Nunc malesuada laoreet porttitor. Donec gravida tortor nisi, in mattis lectus porta ut. Integer vehicula eros et tellus placerat, nec fermentum justo aliquet.\ @@ -89,4 +95,46 @@ void call_update_loop(yaca_context_h ctx, const char *input, size_t input_len, char *output, size_t &output_len, size_t split, update_fun_5_t *fun); +/* This function automates using mockup infrastructure with the + * designated test (passed in the argument). The test is called in a + * loop starting with MOCK_fail_nth == 1 increasing it by 1 on every + * call. The idea is to fail all mocked up OpenSSL functions that the + * test uses one by one. In such cases the test is expected to fail. + * + * When the value of MOCK_fail_nth surpasses the number of used + * OpenSSL functions that are mocked up the test is expected to + * succeed and the function quits with a success. + * + * Every other outcome is considered a failure. + * + * See HANDLE_FUNCTION() in openssl_mock_impl.c + */ +template +void call_mock_test(func F) +{ + for (int n = 1; ; ++n) { + MOCK_fail_nth = n; + + int ret = F(); + + if (MOCK_fail_nth == 0) { + /* The nth OpenSSL function failed. The test is expected + * to fail. Increase the value and carry on. + */ + BOOST_REQUIRE_MESSAGE(ret != YACA_ERROR_NONE, + "The code should've failed\n"); + } else /* if (MOCK_fail_nth > 0) */ { + /* The n seems to be higher than the number of mocked up + * OpenSSL calls. The test is expected to succeed as no + * OpenSSL functions failed in the end. Quit the loop. + */ + BOOST_REQUIRE_MESSAGE(ret == YACA_ERROR_NONE, + "The code should've succeeded\n"); + MOCK_fail_nth = 0; + break; + } + } +} + + #endif /* COMMON_H */ diff --git a/tests/mock_test_crypto.cpp b/tests/mock_test_crypto.cpp new file mode 100644 index 0000000..93deb81 --- /dev/null +++ b/tests/mock_test_crypto.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file mock_test_crypto.cpp + * @author Lukasz Pawelczyk + * @brief Base crypto API unit tests using mockup. + */ + +#include + +#include +#include + +#include +#include + +#include "common.h" +#include "openssl_mock_impl.h" + + +namespace { + +const size_t DATA_SIZE = 10; + +} + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_CRYPTO) + +BOOST_FIXTURE_TEST_CASE(T1101__mock__negative__malloc, InitFixture) +{ + int ret; + char *alloc; + + MOCK_fail_OPENSSL_malloc = 1; + ret = yaca_malloc(DATA_SIZE, (void**)&alloc); + BOOST_REQUIRE(ret == YACA_ERROR_OUT_OF_MEMORY); +} + + +BOOST_FIXTURE_TEST_CASE(T1102__mock__negative__zalloc, InitFixture) +{ + int ret; + char *alloc; + + MOCK_fail_OPENSSL_malloc = 1; + ret = yaca_zalloc(DATA_SIZE, (void**)&alloc); + BOOST_REQUIRE(ret == YACA_ERROR_OUT_OF_MEMORY); +} + + +BOOST_FIXTURE_TEST_CASE(T1103__mock__negative__realloc, InitFixture) +{ + int ret; + char *alloc; + + ret = yaca_zalloc(DATA_SIZE, (void**)&alloc); + BOOST_REQUIRE(ret == YACA_ERROR_NONE); + + MOCK_fail_OPENSSL_realloc = 1; + ret = yaca_realloc(DATA_SIZE * 2, (void**)&alloc); + BOOST_REQUIRE(ret == YACA_ERROR_OUT_OF_MEMORY); + + yaca_free(alloc); +} + +BOOST_FIXTURE_TEST_CASE(T1104__mock__negative__randomize_bytes, InitFixture) +{ + int ret; + char buf[DATA_SIZE] = {}; + + MOCK_fail_RAND_bytes = 1; + ret = yaca_randomize_bytes(buf, DATA_SIZE); + BOOST_REQUIRE(ret == YACA_ERROR_INTERNAL); +} + +#ifndef SYS_getrandom +BOOST_AUTO_TEST_CASE(T1105__mock__negative__sys_init) +{ + int ret; + + MOCK_fail_open = 1; + ret = yaca_initialize(); + BOOST_REQUIRE(ret == YACA_ERROR_INTERNAL); +} + +BOOST_FIXTURE_TEST_CASE(T1106__mock__negative__sys_randomize_bytes, InitFixture) +{ + int ret; + char data[DATA_SIZE]; + + MOCK_fail_read = 1; + ret = yaca_randomize_bytes(data, DATA_SIZE); + BOOST_REQUIRE(ret == YACA_ERROR_INTERNAL); +} +#endif + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/mock_test_digest.cpp b/tests/mock_test_digest.cpp new file mode 100644 index 0000000..24ba81f --- /dev/null +++ b/tests/mock_test_digest.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file test_digest.cpp + * @author Lukasz Pawelczyk + * @brief Digest API unit tests. + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include "common.h" + + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_DIGEST) + +BOOST_FIXTURE_TEST_CASE(T1501__mock__negative__yaca_digest, InitFixture) +{ + struct digest_args { + yaca_digest_algorithm_e algo = YACA_DIGEST_SHA256; + }; + + const std::vector dargs = { + {yaca_digest_algorithm_e::YACA_DIGEST_MD5}, + {yaca_digest_algorithm_e::YACA_DIGEST_SHA224} + }; + + for (const auto &da: dargs) { + auto test_code = [&da]() -> int + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + char *digest = NULL; + size_t digest_len; + + ret = yaca_digest_initialize(&ctx, da.algo); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_digest_update(ctx, INPUT_DATA, INPUT_DATA_SIZE); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, 0, &digest_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(digest_len, (void**)&digest); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_digest_finalize(ctx, digest, &digest_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_context_destroy(ctx); + yaca_free(digest); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/mock_test_encrypt.cpp b/tests/mock_test_encrypt.cpp new file mode 100644 index 0000000..e2657b5 --- /dev/null +++ b/tests/mock_test_encrypt.cpp @@ -0,0 +1,684 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file test_encrypt.cpp + * @author Lukasz Pawelczyk + * @brief Encrypt API unit tests. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "common.h" + + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_ENCRYPT) + +BOOST_FIXTURE_TEST_CASE(T1601__mock__negative__encrypt_decrypt, InitFixture) +{ + struct encrypt_args { + yaca_encrypt_algorithm_e algo; + yaca_block_cipher_mode_e bcm; + size_t key_bit_len; + yaca_padding_e padding; + }; + + const std::vector eargs = { + {YACA_ENCRYPT_AES, YACA_BCM_CBC, 128, YACA_PADDING_NONE }, + {YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_OFB, 64, YACA_INVALID_PADDING}, + {YACA_ENCRYPT_UNSAFE_3DES_2TDEA, YACA_BCM_CBC, 128, YACA_PADDING_PKCS7 }, + {YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_CFB1, 192, YACA_INVALID_PADDING}, + {YACA_ENCRYPT_UNSAFE_RC4, YACA_BCM_NONE, 256, YACA_INVALID_PADDING}, + {YACA_ENCRYPT_CAST5, YACA_BCM_ECB, 128, YACA_PADDING_NONE } + }; + + for (const auto &ea: eargs) { + auto test_code = [&ea]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t iv_bit_len, len1 = 0, len2 = 0; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len = 0, decrypted_len = 0; + + ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, ea.key_bit_len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_get_iv_bit_length(ea.algo, ea.bcm, ea.key_bit_len, &iv_bit_len); + if (ret != YACA_ERROR_NONE) goto exit; + + if (iv_bit_len > 0) { + ret = yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + } + + /* ENCRYPT */ + { + ret = yaca_encrypt_initialize(&ctx, ea.algo, ea.bcm, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + if (ea.padding != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, &ea.padding, + sizeof(yaca_padding_e)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_encrypt_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* DECRYPT */ + { + ret = yaca_decrypt_initialize(&ctx, ea.algo, ea.bcm, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + if (ea.padding != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, &ea.padding, + sizeof(yaca_padding_e)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_decrypt_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_decrypt_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1602__mock__negative__encrypt_decrypt_wrap, InitFixture) +{ + struct encrypt_args { + yaca_encrypt_algorithm_e algo; + size_t key_bit_len; + size_t key_material_len; + }; + + const std::vector eargs = { + {YACA_ENCRYPT_AES, 128, 192 / 8}, + {YACA_ENCRYPT_3DES_3TDEA, 192, 128 / 8}, + }; + + for (const auto &ea: eargs) { + auto test_code = [&ea]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key = YACA_KEY_NULL, iv = YACA_KEY_NULL; + char *key_material = NULL; + + size_t iv_bit_len, len1, len2; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len, decrypted_len; + + ret = yaca_zalloc(ea.key_material_len, (void**)&key_material); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_randomize_bytes(key_material, ea.key_material_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, ea.key_bit_len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_get_iv_bit_length(ea.algo, YACA_BCM_WRAP, ea.key_bit_len, &iv_bit_len); + if (ret != YACA_ERROR_NONE) goto exit; + + if (iv_bit_len > 0) { + ret = yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + } + + /* ENCRYPT */ + { + ret = yaca_encrypt_initialize(&ctx, ea.algo, YACA_BCM_WRAP, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, ea.key_material_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_update(ctx, key_material, ea.key_material_len, + encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + ret = yaca_encrypt_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* DECRYPT */ + { + ret = yaca_decrypt_initialize(&ctx, ea.algo, YACA_BCM_WRAP, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_decrypt_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_decrypt_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key); + yaca_key_destroy(iv); + yaca_free(key_material); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1603__mock__negative__encrypt_decrypt_rc2, InitFixture) +{ + struct encrypt_args { + yaca_block_cipher_mode_e bcm; + size_t key_bit_len; + yaca_padding_e padding; + size_t effective_key_bits; + }; + + const std::vector eargs = { + {YACA_BCM_CBC, 224, YACA_INVALID_PADDING, 1}, + {YACA_BCM_CBC, 192, YACA_PADDING_NONE, 64}, + {YACA_BCM_CBC, 272, YACA_PADDING_PKCS7, 8}, + }; + + for (const auto &ea: eargs) { + auto test_code = [&ea]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t iv_bit_len, len1 = 0, len2 = 0; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len = 0, decrypted_len = 0; + + ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, ea.key_bit_len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_get_iv_bit_length(YACA_ENCRYPT_UNSAFE_RC2, ea.bcm, ea.key_bit_len, &iv_bit_len); + if (ret != YACA_ERROR_NONE) goto exit; + + if (iv_bit_len > 0) { + ret = yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + } + + /* ENCRYPT */ + { + ret = yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_RC2, ea.bcm, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + if (ea.padding != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, + &ea.padding, sizeof(yaca_padding_e)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS, + &ea.effective_key_bits, sizeof(size_t)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + ret = yaca_encrypt_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* DECRYPT */ + { + ret = yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_UNSAFE_RC2, ea.bcm, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + if (ea.padding != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, + &ea.padding, sizeof(yaca_padding_e)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS, + &ea.effective_key_bits, sizeof(size_t)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_decrypt_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_decrypt_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1604__mock__negative__encrypt_decrypt_ccm, InitFixture) +{ + struct encrypt_args { + size_t key_bit_len; + size_t ccm_tag_len; + size_t aad_len; + size_t iv_bit_len; + }; + + const std::vector eargs = { + {128, 12, 19, 64}, + {192, 10, 34, 96} + }; + + for (const auto &ea: eargs) { + auto test_code = [&ea]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t len1, len2; + + char *tag = NULL, *aad = NULL; + size_t tag_len; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len, decrypted_len; + + ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, ea.key_bit_len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_generate(YACA_KEY_TYPE_IV, ea.iv_bit_len, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + + /* ENCRYPT */ + { + ret = yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + tag_len = ea.ccm_tag_len; + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_TAG_LEN, + &tag_len, sizeof(tag_len)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(ea.aad_len, (void**)&aad); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_randomize_bytes(aad, ea.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_update(ctx, NULL, INPUT_DATA_SIZE, + NULL, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, + aad, ea.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + ret = yaca_encrypt_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_property(ctx, YACA_PROPERTY_CCM_TAG, (void**)&tag, &tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* DECRYPT */ + { + ret = yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_CCM, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_TAG, tag, tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_decrypt_update(ctx, NULL, encrypted_len, + NULL, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, + aad, ea.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_decrypt_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_decrypt_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + yaca_free(tag); + yaca_free(aad); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1605__mock__negative__encrypt_decrypt_gcm, InitFixture) +{ + struct encrypt_args { + size_t key_bit_len; + size_t gcm_tag_len; + size_t aad_len; + size_t iv_bit_len; + }; + + const std::vector eargs = { + {128, 13, 22, 64}, + {256, 14, 12, 128} + }; + + for (const auto &ea: eargs) { + auto test_code = [&ea]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t len1 = 0, len2 = 0; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len = 0, decrypted_len = 0; + + char *tag = NULL, *aad = NULL; + size_t tag_len; + + ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, ea.key_bit_len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_generate(YACA_KEY_TYPE_IV, ea.iv_bit_len, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + + /* ENCRYPT */ + { + ret = yaca_encrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_GCM, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(ea.aad_len, (void**)&aad); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_randomize_bytes(aad, ea.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, + aad, ea.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_encrypt_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + ret = yaca_encrypt_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + tag_len = ea.gcm_tag_len; + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG_LEN, + &tag_len, sizeof(tag_len)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_property(ctx, YACA_PROPERTY_GCM_TAG, + (void**)&tag, &tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* DECRYPT */ + { + ret = yaca_decrypt_initialize(&ctx, YACA_ENCRYPT_AES, YACA_BCM_GCM, key, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, + aad, ea.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_decrypt_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG, + tag, tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_decrypt_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + yaca_free(tag); + yaca_free(aad); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/mock_test_key.cpp b/tests/mock_test_key.cpp new file mode 100644 index 0000000..2c94c4d --- /dev/null +++ b/tests/mock_test_key.cpp @@ -0,0 +1,573 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file test_key.cpp + * @author Lukasz Pawelczyk + * @brief Key API unit tests. + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include "common.h" + + +namespace { + +int import_export(yaca_key_h key, yaca_key_type_e type, const char *password, + yaca_key_format_e format, yaca_key_file_format_e file_format) +{ + int ret; + yaca_key_h imported = YACA_KEY_NULL; + + char *data = NULL; + size_t data_len = 0; + yaca_key_type_e key_type; + size_t key_length; + + ret = yaca_key_export(key, format, file_format, + password, &data, &data_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_import(type, password, data, data_len, &imported); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_get_type(imported, &key_type); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_get_bit_length(imported, &key_length); + if (ret != YACA_ERROR_NONE) goto exit; + +exit: + yaca_key_destroy(imported); + yaca_free(data); + return ret; +} + +} // namespace + + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_KEY) + +BOOST_FIXTURE_TEST_CASE(T1201__mock__negative__key_symmetric_all, InitFixture) +{ + struct key_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + }; + + const std::vector kargs = { + {YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_256BIT}, + {YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_192BIT}, + {YACA_KEY_TYPE_IV, YACA_KEY_LENGTH_IV_64BIT} + }; + + for (const auto &ka: kargs) { + auto test_code = [&ka]() + { + int ret; + yaca_key_h key = YACA_KEY_NULL; + + ret = yaca_key_generate(ka.type, ka.len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key, ka.type, "", + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_RAW); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key, ka.type, "", + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_BASE64); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(key); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1202__mock__negative__key_asymmetric_generate_all, InitFixture) +{ + struct key_args { + yaca_key_type_e type_priv; + yaca_key_type_e type_pub; + yaca_key_type_e type_params; + yaca_key_bit_length_e len; + }; + + const std::vector kargs = { + {YACA_KEY_TYPE_RSA_PRIV, + YACA_KEY_TYPE_RSA_PUB, + YACA_INVALID_KEY_TYPE, + YACA_KEY_LENGTH_512BIT}, + {YACA_KEY_TYPE_DSA_PRIV, + YACA_KEY_TYPE_DSA_PUB, + YACA_KEY_TYPE_DSA_PARAMS, + YACA_KEY_LENGTH_512BIT}, + {YACA_KEY_TYPE_DH_PRIV, + YACA_KEY_TYPE_DH_PUB, + YACA_KEY_TYPE_DH_PARAMS, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_DH_RFC_1024_160}, + {YACA_KEY_TYPE_EC_PRIV, + YACA_KEY_TYPE_EC_PUB, + YACA_KEY_TYPE_EC_PARAMS, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_EC_PRIME192V1} + }; + + for (const auto &ka: kargs) { + auto test_code = [&ka]() + { + int ret; + yaca_key_h priv = YACA_KEY_NULL; + yaca_key_h priv2 = YACA_KEY_NULL; + yaca_key_h pub = YACA_KEY_NULL; + yaca_key_h params = YACA_KEY_NULL; + yaca_key_h params2 = YACA_KEY_NULL; + + ret = yaca_key_generate(ka.type_priv, ka.len, &priv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_extract_public(priv, &pub); + if (ret != YACA_ERROR_NONE) goto exit; + + if (ka.type_params != YACA_INVALID_KEY_TYPE) { + ret = yaca_key_generate(ka.type_params, ka.len, ¶ms); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_generate_from_parameters(params, &priv2); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_extract_parameters(pub, ¶ms2); + if (ret != YACA_ERROR_NONE) goto exit; + } + + exit: + yaca_key_destroy(params); + yaca_key_destroy(params2); + yaca_key_destroy(priv); + yaca_key_destroy(priv2); + yaca_key_destroy(pub); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1204__mock__negative__key_asymmetric_import_export, InitFixture) +{ + struct key_args { + yaca_key_type_e type_priv; + yaca_key_type_e type_pub; + yaca_key_type_e type_params; + yaca_key_bit_length_e len; + }; + + const std::vector kargs = { + {YACA_KEY_TYPE_RSA_PRIV, + YACA_KEY_TYPE_RSA_PUB, + YACA_INVALID_KEY_TYPE, + YACA_KEY_LENGTH_512BIT}, + {YACA_KEY_TYPE_DSA_PRIV, + YACA_KEY_TYPE_DSA_PUB, + YACA_KEY_TYPE_DSA_PARAMS, + YACA_KEY_LENGTH_512BIT}, + {YACA_KEY_TYPE_EC_PRIV, + YACA_KEY_TYPE_EC_PUB, + YACA_KEY_TYPE_EC_PARAMS, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_EC_PRIME192V1}, + {YACA_KEY_TYPE_DH_PRIV, + YACA_KEY_TYPE_DH_PUB, + YACA_KEY_TYPE_DH_PARAMS, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_DH_RFC_1024_160}, + }; + + for (const auto &ka: kargs) { + auto test_code = [&ka]() + { + int ret; + yaca_key_h key_priv = YACA_KEY_NULL; + yaca_key_h key_pub = YACA_KEY_NULL; + yaca_key_h key_params = YACA_KEY_NULL; + + ret = yaca_key_generate(ka.type_priv, ka.len, &key_priv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key_priv, ka.type_priv, NULL, + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_DER); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key_priv, ka.type_priv, NULL, + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_PEM); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_extract_public(key_priv, &key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key_pub, ka.type_pub, "", + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_DER); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key_pub, ka.type_pub, "", + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_PEM); + if (ret != YACA_ERROR_NONE) goto exit; + + if (ka.type_params != YACA_INVALID_KEY_TYPE) { + ret = yaca_key_extract_parameters(key_priv, &key_params); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key_params, ka.type_params, NULL, + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_DER); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key_params, ka.type_params, NULL, + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_PEM); + if (ret != YACA_ERROR_NONE) goto exit; + } + + exit: + yaca_key_destroy(key_params); + yaca_key_destroy(key_priv); + yaca_key_destroy(key_pub); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1205__mock__negative__key_encrypted_import_export, InitFixture) +{ + static const char *PASSWORD = "ExamplE_PassworD"; + + struct default_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + }; + + const std::vector dargs = { + {YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_512BIT}, + {YACA_KEY_TYPE_DSA_PRIV, YACA_KEY_LENGTH_512BIT} + }; + + for (const auto &da: dargs) { + auto test_code = [&da]() + { + int ret; + yaca_key_h key = YACA_KEY_NULL; + + ret = yaca_key_generate(da.type, da.len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key, da.type, PASSWORD, + YACA_KEY_FORMAT_DEFAULT, + YACA_KEY_FILE_FORMAT_PEM); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(key); + return ret; + }; + + call_mock_test(test_code); + } + + struct pkcs8_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + }; + + const std::vector pargs { + {YACA_KEY_TYPE_RSA_PRIV, + YACA_KEY_LENGTH_512BIT}, + {YACA_KEY_TYPE_DSA_PRIV, + YACA_KEY_LENGTH_512BIT}, + {YACA_KEY_TYPE_EC_PRIV, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_EC_PRIME256V1}, + {YACA_KEY_TYPE_DH_PRIV, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_DH_RFC_1024_160}, + }; + + for (const auto &pa: pargs) { + auto test_code2 = [&pa]() + { + int ret; + yaca_key_h key = YACA_KEY_NULL; + + ret = yaca_key_generate(pa.type, pa.len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key, pa.type, PASSWORD, + YACA_KEY_FORMAT_PKCS8, + YACA_KEY_FILE_FORMAT_DER); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = import_export(key, pa.type, PASSWORD, + YACA_KEY_FORMAT_PKCS8, + YACA_KEY_FILE_FORMAT_PEM); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(key); + return ret; + }; + + call_mock_test(test_code2); + } +} + +BOOST_FIXTURE_TEST_CASE(T1206__mock__negative__key_derive_dh, InitFixture) +{ + struct key_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + }; + + const std::vector kargs = { + {YACA_KEY_TYPE_DH_PRIV, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_DH_RFC_1024_160}, + {YACA_KEY_TYPE_EC_PRIV, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_EC_SECP256K1} + }; + + for (const auto &ka: kargs) { + auto test_code = [&ka]() + { + int ret; + yaca_key_h priv1 = YACA_KEY_NULL, pub1 = YACA_KEY_NULL; + yaca_key_h priv2 = YACA_KEY_NULL, pub2 = YACA_KEY_NULL; + char *secret1 = NULL, *secret2 = NULL; + size_t secret1_len, secret2_len; + + ret = yaca_key_generate(ka.type, ka.len, &priv1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_generate(ka.type, ka.len, &priv2); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(priv1, &pub1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(priv2, &pub2); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_derive_dh(priv1, pub2, &secret1, &secret1_len); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_derive_dh(priv2, pub1, &secret2, &secret2_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(priv1); + yaca_key_destroy(priv2); + yaca_key_destroy(pub1); + yaca_key_destroy(pub2); + yaca_free(secret1); + yaca_free(secret2); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1207__mock__negative__key_derive_kdf, InitFixture) +{ + static const size_t SECRET_LEN = 128; + static const size_t MATERIAL_LEN = 256; + + struct kdf_args { + yaca_kdf_e kdf; + yaca_digest_algorithm_e digest; + }; + + const std::vector kargs = { + {YACA_KDF_X942, YACA_DIGEST_SHA1}, + {YACA_KDF_X962, YACA_DIGEST_MD5} + }; + + int ret; + char secret[SECRET_LEN]; + + ret = yaca_randomize_bytes(secret, SECRET_LEN); + BOOST_REQUIRE(ret == YACA_ERROR_NONE); + + for (const auto &ka: kargs) { + auto test_code = [&ka, &secret]() + { + int ret; + char *key_material = NULL;; + + ret = yaca_key_derive_kdf(ka.kdf, ka.digest, secret, SECRET_LEN, + NULL, 0, MATERIAL_LEN, &key_material); + + yaca_free(key_material); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1208__mock__negative__key_derive_pbkdf2, InitFixture) +{ + static const char *PASSWORD = "Password_ExamplE"; + static const size_t SALT_LEN = 64; + + struct pbkdf2_args { + yaca_digest_algorithm_e digest; + size_t iter; + size_t bit_len; + }; + + const std::vector pargs = { + {YACA_DIGEST_MD5, 1, 256}, + {YACA_DIGEST_SHA512, 50, 512} + }; + + int ret; + char salt[SALT_LEN]; + + ret = yaca_randomize_bytes(salt, SALT_LEN); + BOOST_REQUIRE(ret == YACA_ERROR_NONE); + + for (const auto &pa: pargs) { + auto test_code = [&pa, &salt]() + { + int ret; + yaca_key_h key = YACA_KEY_NULL; + + ret = yaca_key_derive_pbkdf2(PASSWORD, salt, SALT_LEN, pa.iter, + pa.digest, pa.bit_len, &key); + + yaca_key_destroy(key); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1209__mock__negative__import_x509_cert, InitFixture) +{ + static const char data_pem[] = "-----BEGIN CERTIFICATE-----\n\ +MIIC9jCCAl+gAwIBAgIUaWM7DVy/evvsrKz8gkz3qWZKw7EwDQYJKoZIhvcNAQEL\n\ +BQAwgYwxCzAJBgNVBAYTAlBMMRQwEgYDVQQIDAtNYXpvd2llY2tpZTERMA8GA1UE\n\ +BwwIV2Fyc3phd2ExEDAOBgNVBAoMB1NhbXN1bmcxCzAJBgNVBAsMAklUMRQwEgYD\n\ +VQQDDAtzYW1zdW5nLmNvbTEfMB0GCSqGSIb3DQEJARYQbm9uZUBzYW1zdW5nLmNv\n\ +bTAeFw0yMDA0MDkxNzUzMDlaFw0yNTA0MDgxNzUzMDlaMIGMMQswCQYDVQQGEwJQ\n\ +TDEUMBIGA1UECAwLTWF6b3dpZWNraWUxETAPBgNVBAcMCFdhcnN6YXdhMRAwDgYD\n\ +VQQKDAdTYW1zdW5nMQswCQYDVQQLDAJJVDEUMBIGA1UEAwwLc2Ftc3VuZy5jb20x\n\ +HzAdBgkqhkiG9w0BCQEWEG5vbmVAc2Ftc3VuZy5jb20wgZ8wDQYJKoZIhvcNAQEB\n\ +BQADgY0AMIGJAoGBAMrx4VdcBEWSXdOa7nJr6Vh53TDfnqhgOGRUC8c+kGUu45Cp\n\ +hcGU7q44zfqvEdgkVBK+Y6GBMrbB0TALo2zK4RVDIgTc8UskbiBjiP4cHB+Zl460\n\ +kU/0vKZPWt7yWq9g87lppEr/f0RTGrKkkcVadCxmKILr4ZtS9563xXH+kKAlAgMB\n\ +AAGjUzBRMB0GA1UdDgQWBBQBroKxSi+l6RqOD5jQGRYyoM0I1jAfBgNVHSMEGDAW\n\ +gBQBroKxSi+l6RqOD5jQGRYyoM0I1jAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3\n\ +DQEBCwUAA4GBAC1f+n4ly876nTXMjdINH8qmxrHOH55vt7v1KYWqCVFSJbqtQMlT\n\ +E9+bqRGN2LpzMBkDdNkGSrCesI1l/FUStjqdpBGMi1fqFDNDyBXkLJDH5HAMR3ei\n\ +hajHIasdGWcAfj+Cyuk1KcTIEkBfdYR6a8C4g04Vbg6M0qEjFl5UTMwm\n\ +-----END CERTIFICATE-----"; + + /* THIS CHUNK OF BYTES IS AUTOMATICALLY GENERATED */ + static const unsigned char data_der[] = { + 0x30, 0x82, 0x02, 0xf2, 0x30, 0x82, 0x02, 0x5b, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x3e, + 0x03, 0xd9, 0x79, 0x86, 0xae, 0xa4, 0x85, 0x59, 0xd6, 0x2b, 0x53, 0x29, 0xee, 0xfd, 0x2c, 0x26, + 0xe8, 0x72, 0x57, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, + 0x05, 0x00, 0x30, 0x81, 0x8a, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, + 0x50, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0b, 0x4d, 0x61, 0x7a, + 0x6f, 0x77, 0x69, 0x65, 0x63, 0x6b, 0x69, 0x65, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, + 0x07, 0x0c, 0x06, 0x57, 0x61, 0x72, 0x73, 0x61, 0x77, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, + 0x04, 0x0a, 0x0c, 0x07, 0x53, 0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67, 0x31, 0x0b, 0x30, 0x09, 0x06, + 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x02, 0x49, 0x54, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, + 0x03, 0x0c, 0x0b, 0x73, 0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, + 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x6e, + 0x6f, 0x6e, 0x65, 0x40, 0x73, 0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x30, + 0x1e, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x34, 0x31, 0x34, 0x31, 0x35, 0x32, 0x33, 0x30, 0x37, 0x5a, + 0x17, 0x0d, 0x32, 0x31, 0x30, 0x34, 0x31, 0x34, 0x31, 0x35, 0x32, 0x33, 0x30, 0x37, 0x5a, 0x30, + 0x81, 0x8a, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x50, 0x4c, 0x31, + 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0b, 0x4d, 0x61, 0x7a, 0x6f, 0x77, 0x69, + 0x65, 0x63, 0x6b, 0x69, 0x65, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x06, + 0x57, 0x61, 0x72, 0x73, 0x61, 0x77, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, + 0x07, 0x53, 0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, + 0x0b, 0x0c, 0x02, 0x49, 0x54, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0b, + 0x73, 0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, + 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x6e, 0x6f, 0x6e, 0x65, + 0x40, 0x73, 0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x81, 0x9f, 0x30, + 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, + 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xc9, 0x73, 0x11, 0x8f, 0x63, 0x4d, 0xaa, + 0x8e, 0xc5, 0xb5, 0x6d, 0x9c, 0xea, 0x30, 0x43, 0xb5, 0x5d, 0xd3, 0xb2, 0x9c, 0x59, 0x23, 0xdf, + 0xa8, 0x69, 0xe6, 0x0d, 0xfe, 0x0a, 0xdb, 0xce, 0x22, 0x64, 0x15, 0x02, 0xf6, 0xa4, 0xe9, 0x22, + 0x04, 0xce, 0x73, 0x9e, 0x89, 0x1e, 0x87, 0x93, 0x31, 0x07, 0x91, 0x0e, 0xbd, 0x98, 0x45, 0x3d, + 0x66, 0xe9, 0x59, 0x02, 0xfc, 0x2f, 0xd9, 0x11, 0x71, 0xc4, 0x11, 0x3f, 0x20, 0xf3, 0x49, 0xb6, + 0x59, 0x26, 0xb2, 0x8c, 0x9f, 0x74, 0xe0, 0x09, 0x3b, 0x4f, 0xdd, 0xf4, 0x13, 0x8b, 0x91, 0x48, + 0x1e, 0x1b, 0xf5, 0x86, 0xca, 0xe6, 0xd6, 0x1d, 0x29, 0x74, 0x1d, 0xb6, 0x84, 0x0b, 0x48, 0xe7, + 0x40, 0x14, 0x60, 0x65, 0xb2, 0x35, 0xf0, 0x48, 0xe9, 0x93, 0xea, 0x77, 0x63, 0x77, 0x53, 0x77, + 0xaf, 0xbb, 0xac, 0xc2, 0x86, 0x40, 0xc7, 0xb0, 0x0b, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x53, + 0x30, 0x51, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xd0, 0xb1, 0x78, + 0xed, 0xee, 0xb4, 0x57, 0x7b, 0x4f, 0xed, 0x45, 0xba, 0x0b, 0x5a, 0x32, 0xe5, 0xe1, 0x32, 0xee, + 0x83, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xd0, 0xb1, + 0x78, 0xed, 0xee, 0xb4, 0x57, 0x7b, 0x4f, 0xed, 0x45, 0xba, 0x0b, 0x5a, 0x32, 0xe5, 0xe1, 0x32, + 0xee, 0x83, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, + 0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, + 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x2e, 0xf4, 0x42, 0x8b, 0xde, 0xfe, 0x36, 0x79, 0x6d, 0xaa, + 0x51, 0x85, 0x65, 0xe3, 0x0f, 0x89, 0x1f, 0x84, 0xce, 0x5c, 0x34, 0x03, 0x0d, 0x59, 0x2a, 0xad, + 0xfb, 0x09, 0xd2, 0xcd, 0xbc, 0xac, 0x51, 0x4a, 0xe3, 0xcb, 0x9e, 0xe5, 0x75, 0x26, 0x36, 0x5e, + 0xe7, 0xc6, 0x86, 0x6b, 0xf8, 0xc4, 0x96, 0x99, 0x43, 0xb6, 0x53, 0xcc, 0x6a, 0x14, 0x57, 0xcd, + 0x08, 0xad, 0x53, 0x11, 0x5f, 0x17, 0x97, 0xb3, 0x2f, 0x36, 0xbe, 0xd6, 0x5c, 0x03, 0x32, 0xe3, + 0x2a, 0x4f, 0x69, 0x85, 0xf6, 0xf0, 0x14, 0x13, 0x2b, 0xfc, 0xa6, 0x64, 0x67, 0x4d, 0x7b, 0xab, + 0xb9, 0xd0, 0x06, 0x00, 0xce, 0xc6, 0x85, 0x08, 0x45, 0xfb, 0xca, 0x70, 0x1b, 0xb4, 0x8f, 0x4e, + 0x49, 0x2e, 0xfe, 0x94, 0xd7, 0x7b, 0xf1, 0xc6, 0x60, 0x24, 0xa6, 0x79, 0x5a, 0xeb, 0x92, 0xed, + 0xd7, 0x07, 0x42, 0x65, 0xd3, 0x31 + }; + + auto test_code = []() + { + int ret; + yaca_key_h key_pem = YACA_KEY_NULL, key_der = YACA_KEY_NULL; + + ret = yaca_key_import(YACA_KEY_TYPE_RSA_PUB, NULL, data_pem, + sizeof(data_pem), &key_pem); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_import(YACA_KEY_TYPE_RSA_PUB, NULL, (char*)data_der, + sizeof(data_der), &key_der); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(key_pem); + yaca_key_destroy(key_der); + return ret; + }; + + call_mock_test(test_code); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/mock_test_rsa.cpp b/tests/mock_test_rsa.cpp new file mode 100644 index 0000000..0fd2afb --- /dev/null +++ b/tests/mock_test_rsa.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file test_rsa.c + * @author Lukasz Pawelczyk + * @brief RSA API unit tests. + */ + +#include +#include + +#include +#include +#include +#include + +#include "common.h" + + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_RSA) + +BOOST_FIXTURE_TEST_CASE(T1401__mock__negative__private_encrypt, InitFixture) +{ + struct rsa_args { + yaca_padding_e pad; + yaca_key_bit_length_e bit_len; + size_t shorter; + }; + + const std::vector rargs = { + {YACA_PADDING_NONE, YACA_KEY_LENGTH_512BIT, 0}, + {YACA_PADDING_PKCS1, YACA_KEY_LENGTH_512BIT, 11}, + }; + + for (const auto &ra: rargs) { + auto test_code = [&ra]() + { + int ret; + yaca_key_h rsa_prv = YACA_KEY_NULL, rsa_pub = YACA_KEY_NULL; + size_t input_len = ra.bit_len / 8 - ra.shorter; + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len, decrypted_len; + + ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, ra.bit_len, &rsa_prv); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(rsa_prv, &rsa_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_rsa_private_encrypt(ra.pad, rsa_prv, INPUT_DATA, input_len, + &encrypted, &encrypted_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_rsa_public_decrypt(ra.pad, rsa_pub, encrypted, encrypted_len, + &decrypted, &decrypted_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(rsa_prv); + yaca_key_destroy(rsa_pub); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1402__mock__negative__public_encrypt, InitFixture) +{ + struct rsa_args { + yaca_padding_e pad; + yaca_key_bit_length_e bit_len; + size_t shorter; + }; + + const std::vector rargs = { + {YACA_PADDING_NONE, YACA_KEY_LENGTH_512BIT, 0}, + {YACA_PADDING_PKCS1, YACA_KEY_LENGTH_1024BIT, 11}, + {YACA_PADDING_PKCS1_OAEP, YACA_KEY_LENGTH_2048BIT, 42}, + }; + + for (const auto &ra: rargs) { + auto test_code = [&ra]() + { + int ret; + yaca_key_h rsa_prv = YACA_KEY_NULL, rsa_pub = YACA_KEY_NULL; + size_t input_len = ra.bit_len / 8 - ra.shorter; + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len, decrypted_len; + + ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, ra.bit_len, &rsa_prv); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(rsa_prv, &rsa_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_rsa_public_encrypt(ra.pad, rsa_pub, INPUT_DATA, input_len, + &encrypted, &encrypted_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_rsa_private_decrypt(ra.pad, rsa_prv, encrypted, encrypted_len, + &decrypted, &decrypted_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(rsa_prv); + yaca_key_destroy(rsa_pub); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/mock_test_seal.cpp b/tests/mock_test_seal.cpp new file mode 100644 index 0000000..cccc7e1 --- /dev/null +++ b/tests/mock_test_seal.cpp @@ -0,0 +1,558 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file test_seal.cpp + * @author Lukasz Pawelczyk + * @brief Seal API unit tests. + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include "common.h" + + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_SEAL) + +BOOST_FIXTURE_TEST_CASE(T1701__mock__negative__seal_open, InitFixture) +{ + struct seal_args { + yaca_encrypt_algorithm_e algo; + yaca_block_cipher_mode_e bcm; + size_t key_bit_len; + yaca_padding_e padding; + }; + + const std::vector sargs = { + {YACA_ENCRYPT_AES, YACA_BCM_CBC, 128, YACA_PADDING_NONE }, + {YACA_ENCRYPT_UNSAFE_DES, YACA_BCM_ECB, 64, YACA_PADDING_PKCS7 }, + {YACA_ENCRYPT_UNSAFE_3DES_2TDEA, YACA_BCM_CFB, 128, YACA_INVALID_PADDING}, + {YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_CBC, 192, YACA_PADDING_PKCS7 }, + {YACA_ENCRYPT_UNSAFE_RC4, YACA_BCM_NONE, 256, YACA_INVALID_PADDING}, + {YACA_ENCRYPT_CAST5, YACA_BCM_CBC, 128, YACA_PADDING_NONE }, + }; + + for (const auto &sa: sargs) { + auto test_code = [&sa]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key_prv = YACA_KEY_NULL, key_pub = YACA_KEY_NULL; + yaca_key_h key_sym = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t len1 = 0, len2 = 0; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len = 0, decrypted_len = 0; + + ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_512BIT, &key_prv); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(key_prv, &key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + /* SEAL */ + { + ret = yaca_seal_initialize(&ctx, key_pub, sa.algo, sa.bcm, + sa.key_bit_len, &key_sym, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_seal_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + if (sa.padding != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, &sa.padding, + sizeof(yaca_padding_e)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_seal_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* OPEN */ + { + ret = yaca_open_initialize(&ctx, key_prv, sa.algo, sa.bcm, + sa.key_bit_len, key_sym, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + if (sa.padding != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, &sa.padding, + sizeof(yaca_padding_e)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_open_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_open_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key_prv); + yaca_key_destroy(key_pub); + yaca_key_destroy(key_sym); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1702__mock__negative__seal_open_rc2, InitFixture) +{ + struct seal_args { + yaca_block_cipher_mode_e bcm; + size_t key_bit_len; + size_t effective_key_bits; + }; + + const std::vector sargs = { + {YACA_BCM_CBC, 192, 1024}, + {YACA_BCM_CFB, 192, 333} + }; + + for (const auto &sa: sargs) { + auto test_code = [&sa]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key_prv = YACA_KEY_NULL, key_pub = YACA_KEY_NULL; + yaca_key_h key_sym = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t len1 = 0, len2 = 0; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len = 0, decrypted_len = 0; + + ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_512BIT, &key_prv); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(key_prv, &key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + /* SEAL */ + { + ret = yaca_seal_initialize(&ctx, key_pub, YACA_ENCRYPT_UNSAFE_RC2, sa.bcm, + sa.key_bit_len, &key_sym, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS, + &sa.effective_key_bits, sizeof(size_t)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_seal_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + ret = yaca_seal_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* OPEN */ + { + ret = yaca_open_initialize(&ctx, key_prv, YACA_ENCRYPT_UNSAFE_RC2, sa.bcm, + sa.key_bit_len, key_sym, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_RC2_EFFECTIVE_KEY_BITS, + &sa.effective_key_bits, sizeof(size_t)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_open_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_open_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key_prv); + yaca_key_destroy(key_pub); + yaca_key_destroy(key_sym); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1703__mock__negative__open_seal_ccm, InitFixture) +{ + struct seal_args { + size_t key_bit_len; + size_t ccm_tag_len; + size_t aad_len; + }; + + const std::vector sargs = { + {128, 6, 23}, + {256, 12, 33} + }; + + for (const auto &sa: sargs) { + auto test_code = [&sa]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key_prv = YACA_KEY_NULL, key_pub = YACA_KEY_NULL; + yaca_key_h key_sym = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t len1, len2; + + char *tag = NULL, *aad = NULL; + size_t tag_len; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len, decrypted_len; + + ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_512BIT, &key_prv); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(key_prv, &key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + /* SEAL */ + { + ret = yaca_seal_initialize(&ctx, key_pub, YACA_ENCRYPT_AES, YACA_BCM_CCM, + sa.key_bit_len, &key_sym, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + tag_len = sa.ccm_tag_len; + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_TAG_LEN, + &tag_len, sizeof(tag_len)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(sa.aad_len, (void**)&aad); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_randomize_bytes(aad, sa.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_seal_update(ctx, NULL, INPUT_DATA_SIZE, + NULL, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, + aad, sa.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_seal_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + ret = yaca_seal_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_property(ctx, YACA_PROPERTY_CCM_TAG, (void**)&tag, &tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* OPEN */ + { + ret = yaca_open_initialize(&ctx, key_prv, YACA_ENCRYPT_AES, YACA_BCM_CCM, + sa.key_bit_len, key_sym, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_TAG, tag, tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_open_update(ctx, NULL, encrypted_len, + NULL, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, + aad, sa.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_open_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_open_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key_prv); + yaca_key_destroy(key_pub); + yaca_key_destroy(key_sym); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + yaca_free(tag); + yaca_free(aad); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1704__mock__negative__seal_open_gcm, InitFixture) +{ + struct seal_args { + size_t key_bit_len; + size_t gcm_tag_len; + size_t aad_len; + }; + + const std::vector sargs = { + {128, 13, 22}, + {192, 15, 33} + }; + + for (const auto &sa: sargs) { + auto test_code = [&sa]() + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key_prv = YACA_KEY_NULL, key_pub = YACA_KEY_NULL; + yaca_key_h key_sym = YACA_KEY_NULL, iv = YACA_KEY_NULL; + + size_t len1 = 0, len2 = 0; + + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len = 0, decrypted_len = 0; + + char *tag = NULL, *aad = NULL; + size_t tag_len; + + ret = yaca_key_generate(YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_512BIT, &key_prv); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(key_prv, &key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + /* SEAL */ + { + ret = yaca_seal_initialize(&ctx, key_pub, YACA_ENCRYPT_AES, YACA_BCM_GCM, + sa.key_bit_len, &key_sym, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, INPUT_DATA_SIZE, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(sa.aad_len, (void**)&aad); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_randomize_bytes(aad, sa.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, + aad, sa.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_seal_update(ctx, INPUT_DATA, INPUT_DATA_SIZE, encrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len = written; + + ret = yaca_seal_finalize(ctx, encrypted + encrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + encrypted_len += written; + + ret = yaca_realloc(encrypted_len, (void **)&encrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + tag_len = sa.gcm_tag_len; + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG_LEN, + &tag_len, sizeof(tag_len)); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_property(ctx, YACA_PROPERTY_GCM_TAG, + (void**)&tag, &tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* OPEN */ + { + ret = yaca_open_initialize(&ctx, key_prv, YACA_ENCRYPT_AES, YACA_BCM_GCM, + sa.key_bit_len, key_sym, iv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, encrypted_len, &len1); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_context_get_output_length(ctx, 0, &len2); + if (ret != YACA_ERROR_NONE) goto exit; + + size_t total = len1 + len2; + size_t written; + + ret = yaca_zalloc(total, (void**)&decrypted); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_AAD, + aad, sa.aad_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_open_update(ctx, encrypted, encrypted_len, decrypted, &written); + if (ret != YACA_ERROR_NONE) goto exit; + decrypted_len = written; + + ret = yaca_context_set_property(ctx, YACA_PROPERTY_GCM_TAG, + tag, tag_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_open_finalize(ctx, decrypted + decrypted_len, &written); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key_prv); + yaca_key_destroy(key_pub); + yaca_key_destroy(key_sym); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + yaca_free(tag); + yaca_free(aad); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/mock_test_sign.cpp b/tests/mock_test_sign.cpp new file mode 100644 index 0000000..59aad1e --- /dev/null +++ b/tests/mock_test_sign.cpp @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file test_sign.cpp + * @author Lukasz Pawelczyk + * @brief Signature API unit tests. + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include "common.h" + + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_SIGN) + +BOOST_FIXTURE_TEST_CASE(T1801__mock__negative__sign_verify, InitFixture) +{ + struct sign_args { + yaca_key_type_e type_prv; + yaca_key_type_e type_pub; + yaca_key_bit_length_e len; + yaca_digest_algorithm_e digest; + yaca_padding_e pad; + }; + + const std::vector sargs = { + {YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_TYPE_RSA_PUB, YACA_KEY_LENGTH_512BIT, + YACA_DIGEST_SHA1, YACA_PADDING_X931}, + {YACA_KEY_TYPE_DSA_PRIV, YACA_KEY_TYPE_DSA_PUB, YACA_KEY_LENGTH_512BIT, + YACA_DIGEST_SHA224, YACA_INVALID_PADDING}, + {YACA_KEY_TYPE_EC_PRIV, YACA_KEY_TYPE_EC_PUB, + (yaca_key_bit_length_e)YACA_KEY_LENGTH_EC_PRIME192V1, + YACA_DIGEST_SHA384, YACA_INVALID_PADDING} + }; + + for (const auto &sa: sargs) { + auto test_code = [&sa]() -> int + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key_prv = YACA_KEY_NULL, key_pub = YACA_KEY_NULL; + + char *signature = NULL; + size_t signature_len; + + ret = yaca_key_generate(sa.type_prv, sa.len, &key_prv); + if (ret != YACA_ERROR_NONE) goto exit; + ret = yaca_key_extract_public(key_prv, &key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + /* SIGN */ + { + ret = yaca_sign_initialize(&ctx, sa.digest, key_prv); + if (ret != YACA_ERROR_NONE) goto exit; + + if (sa.pad != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, + &sa.pad, sizeof(sa.pad)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_sign_update(ctx, INPUT_DATA, INPUT_DATA_SIZE); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, 0, &signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(signature_len, (void **)&signature); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_sign_finalize(ctx, signature, &signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + /* VERIFY */ + { + ret = yaca_verify_initialize(&ctx, sa.digest, key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + if (sa.pad != YACA_INVALID_PADDING) { + ret = yaca_context_set_property(ctx, YACA_PROPERTY_PADDING, + &sa.pad, sizeof(sa.pad)); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_verify_update(ctx, INPUT_DATA, INPUT_DATA_SIZE); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_verify_finalize(ctx, signature, signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + yaca_context_destroy(ctx); + ctx = YACA_CONTEXT_NULL; + } + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key_prv); + yaca_key_destroy(key_pub); + yaca_free(signature); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1802__mock__negative__sign_cmac, InitFixture) +{ + struct cmac_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + yaca_encrypt_algorithm_e algo; + }; + + const std::vector cargs = { + {YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_UNSAFE_64BIT, + YACA_ENCRYPT_UNSAFE_DES}, + {YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_192BIT, + YACA_ENCRYPT_3DES_3TDEA} + }; + + for (const auto &ca: cargs) { + auto test_code = [&ca]() -> int + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key = YACA_KEY_NULL; + + char *signature = NULL; + size_t signature_len; + + ret = yaca_key_generate(ca.type, ca.len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_sign_initialize_cmac(&ctx, ca.algo, key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_sign_update(ctx, INPUT_DATA, INPUT_DATA_SIZE); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, 0, &signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(signature_len, (void **)&signature); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_sign_finalize(ctx, signature, &signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key); + yaca_free(signature); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1803__mock__negative__sign_hmac, InitFixture) +{ + struct hmac_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + yaca_digest_algorithm_e digest; + }; + + const std::vector hargs = { + {YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_UNSAFE_128BIT, + YACA_DIGEST_SHA1}, + {YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_128BIT, + YACA_DIGEST_SHA224} + }; + + for (const auto &ha: hargs) { + auto test_code = [&ha]() -> int + { + int ret; + yaca_context_h ctx = YACA_CONTEXT_NULL; + yaca_key_h key = YACA_KEY_NULL; + + char *signature = NULL; + size_t signature_len; + + ret = yaca_key_generate(ha.type, ha.len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_sign_initialize_hmac(&ctx, ha.digest, key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_sign_update(ctx, INPUT_DATA, INPUT_DATA_SIZE); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_context_get_output_length(ctx, 0, &signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_malloc(signature_len, (void **)&signature); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_sign_finalize(ctx, signature, &signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_context_destroy(ctx); + yaca_key_destroy(key); + yaca_free(signature); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/mock_test_simple.cpp b/tests/mock_test_simple.cpp new file mode 100644 index 0000000..8dc2bc3 --- /dev/null +++ b/tests/mock_test_simple.cpp @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Lukasz Pawelczyk + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file test_simple.cpp + * @author Lukasz Pawelczyk + * @brief Simple API unit tests. + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include "common.h" + + +BOOST_AUTO_TEST_SUITE(MOCK_TESTS_SIMPLE) + +BOOST_FIXTURE_TEST_CASE(T1301__mock__negative__positive__simple_encrypt_decrypt, InitFixture) +{ + struct encrypt_args { + yaca_encrypt_algorithm_e algo; + yaca_block_cipher_mode_e bcm; + size_t key_bit_len; + }; + + const std::vector eargs = { + {YACA_ENCRYPT_AES, YACA_BCM_CBC, YACA_KEY_LENGTH_256BIT }, + {YACA_ENCRYPT_3DES_3TDEA, YACA_BCM_ECB, YACA_KEY_LENGTH_192BIT }, + {YACA_ENCRYPT_CAST5, YACA_BCM_OFB, YACA_KEY_LENGTH_UNSAFE_128BIT} + }; + + for (const auto &ea: eargs) { + auto test_case = [&ea]() -> int + { + int ret; + yaca_key_h sym = YACA_KEY_NULL, iv = YACA_KEY_NULL; + size_t iv_bit_len; + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len, decrypted_len; + + ret = yaca_encrypt_get_iv_bit_length(ea.algo, ea.bcm, ea.key_bit_len, &iv_bit_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, ea.key_bit_len, &sym); + if (ret != YACA_ERROR_NONE) goto exit; + + if (iv_bit_len > 0) { + ret = yaca_key_generate(YACA_KEY_TYPE_IV, iv_bit_len, &iv); + if (ret != YACA_ERROR_NONE) goto exit; + } + + ret = yaca_simple_encrypt(ea.algo, ea.bcm, sym, iv, INPUT_DATA, INPUT_DATA_SIZE, + &encrypted, &encrypted_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_simple_decrypt(ea.algo, ea.bcm, sym, iv, encrypted, encrypted_len, + &decrypted, &decrypted_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(sym); + yaca_key_destroy(iv); + yaca_free(encrypted); + yaca_free(decrypted); + return ret; + }; + + call_mock_test(test_case); + } +} + +BOOST_FIXTURE_TEST_CASE(T1302__mock__negative__simple_calculate_digest, InitFixture) +{ + struct digest_args { + yaca_digest_algorithm_e algo = YACA_DIGEST_SHA256; + }; + + const std::vector dargs = { + {YACA_DIGEST_MD5}, + {YACA_DIGEST_SHA256} + }; + + for (const auto &da: dargs) { + auto test_code = [&da]() -> int + { + int ret; + char *digest = NULL; + size_t digest_len; + + ret = yaca_simple_calculate_digest(da.algo, INPUT_DATA, INPUT_DATA_SIZE, + &digest, &digest_len); + yaca_free(digest); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1303__mock__negative__simple_calculate_verify_signature, InitFixture) +{ + struct signature_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + yaca_digest_algorithm_e algo; + }; + + const std::vector sargs = { + {YACA_KEY_TYPE_RSA_PRIV, YACA_KEY_LENGTH_512BIT, YACA_DIGEST_MD5}, + {YACA_KEY_TYPE_DSA_PRIV, YACA_KEY_LENGTH_512BIT, YACA_DIGEST_SHA256}, + {YACA_KEY_TYPE_EC_PRIV, (yaca_key_bit_length_e)YACA_KEY_LENGTH_EC_PRIME192V1, + YACA_DIGEST_SHA224} + }; + + for (const auto &sa: sargs) { + auto test_code = [&sa]() -> int + { + int ret; + yaca_key_h key_priv = YACA_KEY_NULL; + yaca_key_h key_pub = YACA_KEY_NULL; + + char *signature = NULL; + size_t signature_len; + + ret = yaca_key_generate(sa.type, sa.len, &key_priv); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_key_extract_public(key_priv, &key_pub); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_simple_calculate_signature(sa.algo, key_priv, + INPUT_DATA, INPUT_DATA_SIZE, + &signature, &signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_simple_verify_signature(sa.algo, key_pub, + INPUT_DATA, INPUT_DATA_SIZE, + signature, signature_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(key_priv); + yaca_key_destroy(key_pub); + yaca_free(signature); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1304__mock__negative__simple_calculate_hmac, InitFixture) +{ + struct hmac_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + yaca_digest_algorithm_e algo; + }; + + const std::vector hargs = { + {YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT, YACA_DIGEST_MD5}, + {YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_128BIT, YACA_DIGEST_SHA1} + }; + + for (const auto &ha: hargs) { + auto test_code = [&ha]() -> int + { + int ret; + yaca_key_h key = YACA_KEY_NULL; + char *mac = NULL; + size_t mac_len; + + ret = yaca_key_generate(ha.type, ha.len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_simple_calculate_hmac(ha.algo, key, + INPUT_DATA, INPUT_DATA_SIZE, + &mac, &mac_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(key); + yaca_free(mac); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_FIXTURE_TEST_CASE(T1305__mock__negative__simple_calculate_cmac, InitFixture) +{ + struct cmac_args { + yaca_key_type_e type; + yaca_key_bit_length_e len; + yaca_encrypt_algorithm_e algo; + }; + + const std::vector cargs = { + {YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_192BIT, YACA_ENCRYPT_AES}, + {YACA_KEY_TYPE_DES, YACA_KEY_LENGTH_UNSAFE_64BIT, YACA_ENCRYPT_UNSAFE_DES} + }; + + for (const auto &ca: cargs) { + auto test_code = [&ca]() -> int + { + int ret; + yaca_key_h key = YACA_KEY_NULL; + char *mac = NULL; + size_t mac_len; + + ret = yaca_key_generate(ca.type, ca.len, &key); + if (ret != YACA_ERROR_NONE) goto exit; + + ret = yaca_simple_calculate_cmac(ca.algo, key, + INPUT_DATA, INPUT_DATA_SIZE, + &mac, &mac_len); + if (ret != YACA_ERROR_NONE) goto exit; + + exit: + yaca_key_destroy(key); + yaca_free(mac); + return ret; + }; + + call_mock_test(test_code); + } +} + +BOOST_AUTO_TEST_SUITE_END() -- 2.7.4 From 662d5009f51115f078998dcec2c1252fc5ebace6 Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Fri, 17 Jul 2020 11:35:10 +0200 Subject: [PATCH 03/16] Fix static analysis issue src/key.c: openssl_password_cb according to openssl examples, password is considered a null-terminated string (https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_set_default_passwd_cb.html) Change-Id: I3b2fc13043e4adb7f5885d4140453297311e74f3 --- src/key.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/key.c b/src/key.c index d94e533..1eea8d9 100644 --- a/src/key.c +++ b/src/key.c @@ -62,10 +62,11 @@ static int openssl_password_cb(char *buf, int size, UNUSED int rwflag, void *u) size_t pass_len = strlen(cb_data->password); - if (pass_len > INT_MAX || (int)pass_len > size) + if (pass_len + 1 > INT_MAX || (int)pass_len + 1 > size) return 0; memcpy(buf, cb_data->password, pass_len); + buf[pass_len] = 0; cb_data->password_requested = true; return pass_len; -- 2.7.4 From b871e78243e5fff1ebe1106c5fa5af5a19c5e77b Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Fri, 17 Jul 2020 11:56:58 +0200 Subject: [PATCH 04/16] Release 0.0.6 * many small fixes uncovered during work on unit tests * new, great unit tests that cover almost entire code of the library Change-Id: I05c0b6c37d55942f1ab7d10dc58abc673f8097a3 --- CMakeLists.txt | 2 +- packaging/yaca.spec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c1a1f4..89f74a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ CMAKE_MINIMUM_REQUIRED (VERSION 2.6.2) PROJECT(yaca) -SET(VERSION "0.0.5") +SET(VERSION "0.0.6") ## pkgconfig ################################################################### INCLUDE(FindPkgConfig) diff --git a/packaging/yaca.spec b/packaging/yaca.spec index 7802567..22adaec 100644 --- a/packaging/yaca.spec +++ b/packaging/yaca.spec @@ -1,7 +1,7 @@ %{!?build_type:%define build_type "RELEASE"} Name: yaca -Version: 0.0.5 +Version: 0.0.6 Release: 0 Source0: %{name}-%{version}.tar.gz License: Apache-2.0 -- 2.7.4 From c18d380289dc5a4e0dda9af08dfce949c6861b01 Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Tue, 1 Sep 2020 21:05:28 +0200 Subject: [PATCH 05/16] Reindent recently added files and fix file modes Change-Id: Ie66b299be62061dea2f204f897ba03fafd94c4cf --- python/yaca/__init__.py | 0 tests/common.h | 2 +- tests/openssl_mock_impl.c | 36 ++++++++++++++++++------------------ tests/openssl_mock_redefine.h | 10 +++++----- 4 files changed, 24 insertions(+), 24 deletions(-) mode change 100755 => 100644 python/yaca/__init__.py diff --git a/python/yaca/__init__.py b/python/yaca/__init__.py old mode 100755 new mode 100644 diff --git a/tests/common.h b/tests/common.h index 38d5122..eafeb79 100644 --- a/tests/common.h +++ b/tests/common.h @@ -49,7 +49,7 @@ Nunc ac purus vel sem laoreet interdum quis eget ligula. Aenean id nisl ut quam constexpr size_t IGNORE = static_cast(-1); -#define DEFINE_INVALID(type, name) \ +#define DEFINE_INVALID(type, name) \ constexpr type YACA_INVALID_##name = static_cast(-1) DEFINE_INVALID(yaca_error_e, ERROR); diff --git a/tests/openssl_mock_impl.c b/tests/openssl_mock_impl.c index 7c10355..3af1917 100644 --- a/tests/openssl_mock_impl.c +++ b/tests/openssl_mock_impl.c @@ -39,24 +39,24 @@ static void reset_conditions() BIO_reset_just_called = 0; } -#define HANDLE_FUNCTION(FNAME, VALUE, COND) \ - do { \ - if (GET_BOOL_NAME(FNAME)) { \ - GET_BOOL_NAME(FNAME) = 0; \ - return VALUE; \ - } \ - if (COND) { \ - reset_conditions(); \ - break; \ - } \ - reset_conditions(); \ - if (MOCK_fail_nth == 0) { \ - break; \ - } \ - --MOCK_fail_nth; \ - if (MOCK_fail_nth == 0) { \ - return VALUE; \ - } \ +#define HANDLE_FUNCTION(FNAME, VALUE, COND) \ + do { \ + if (GET_BOOL_NAME(FNAME)) { \ + GET_BOOL_NAME(FNAME) = 0; \ + return VALUE; \ + } \ + if (COND) { \ + reset_conditions(); \ + break; \ + } \ + reset_conditions(); \ + if (MOCK_fail_nth == 0) { \ + break; \ + } \ + --MOCK_fail_nth; \ + if (MOCK_fail_nth == 0) { \ + return VALUE; \ + } \ } while(0) diff --git a/tests/openssl_mock_redefine.h b/tests/openssl_mock_redefine.h index 06b5adc..2b7750b 100644 --- a/tests/openssl_mock_redefine.h +++ b/tests/openssl_mock_redefine.h @@ -24,12 +24,12 @@ /* This file is to be included in the source files that want to mockup * OpenSSL and libc. After OpenSSL and libc headers. E.g. -#include -#include + #include + #include -#ifdef OPENSSL_MOCKUP -#include "../tests/openssl_mock_redefine.h" -#endif + #ifdef OPENSSL_MOCKUP + #include "../tests/openssl_mock_redefine.h" + #endif */ -- 2.7.4 From de0cfbed194dfede9cf031594174b774a9359e94 Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Wed, 2 Sep 2020 08:29:42 +0200 Subject: [PATCH 06/16] Test for empty plain and cipher texts in simple API Change-Id: I71a702b8f70ba862f06e0a49f8b5868f084a7bf9 --- tests/test_simple.cpp | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/tests/test_simple.cpp b/tests/test_simple.cpp index 59f25eb..6c64788 100644 --- a/tests/test_simple.cpp +++ b/tests/test_simple.cpp @@ -105,7 +105,34 @@ BOOST_FIXTURE_TEST_CASE(T301__positive__simple_encrypt_decrypt, InitDebugFixture } } -BOOST_FIXTURE_TEST_CASE(T302__negative__simple_encrypt_decrypt, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T302__positive__simple_encrypt_decrypt_empty, InitDebugFixture) +{ + int ret; + yaca_key_h sym = YACA_KEY_NULL; + char *encrypted = NULL, *decrypted = NULL; + size_t encrypted_len, decrypted_len; + + ret = yaca_key_generate(YACA_KEY_TYPE_SYMMETRIC, YACA_KEY_LENGTH_256BIT, &sym); + BOOST_REQUIRE(ret == YACA_ERROR_NONE); + + ret = yaca_simple_encrypt(YACA_ENCRYPT_UNSAFE_RC4, YACA_BCM_NONE, sym, NULL, + NULL, 0, &encrypted, &encrypted_len); + BOOST_REQUIRE(ret == YACA_ERROR_NONE); + + BOOST_REQUIRE(encrypted_len == 0); + BOOST_REQUIRE(encrypted == NULL); + + ret = yaca_simple_decrypt(YACA_ENCRYPT_UNSAFE_RC4, YACA_BCM_NONE, sym, NULL, + NULL, 0, &decrypted, &decrypted_len); + BOOST_REQUIRE(ret == YACA_ERROR_NONE); + + BOOST_REQUIRE(decrypted_len == 0); + BOOST_REQUIRE(decrypted == NULL); + + yaca_key_destroy(sym); +} + +BOOST_FIXTURE_TEST_CASE(T303__negative__simple_encrypt_decrypt, InitDebugFixture) { int ret; yaca_key_h sym = YACA_KEY_NULL, iv = YACA_KEY_NULL; @@ -256,7 +283,7 @@ BOOST_FIXTURE_TEST_CASE(T302__negative__simple_encrypt_decrypt, InitDebugFixture yaca_free(decrypted); } -BOOST_FIXTURE_TEST_CASE(T303__positive__simple_calculate_digest, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T304__positive__simple_calculate_digest, InitDebugFixture) { struct digest_args { yaca_digest_algorithm_e algo = YACA_DIGEST_SHA256; @@ -287,7 +314,7 @@ BOOST_FIXTURE_TEST_CASE(T303__positive__simple_calculate_digest, InitDebugFixtur } } -BOOST_FIXTURE_TEST_CASE(T304__negative__simple_calculate_digest, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T305__negative__simple_calculate_digest, InitDebugFixture) { int ret; char *digest = NULL; @@ -314,7 +341,7 @@ BOOST_FIXTURE_TEST_CASE(T304__negative__simple_calculate_digest, InitDebugFixtur BOOST_REQUIRE(ret == YACA_ERROR_INVALID_PARAMETER); } -BOOST_FIXTURE_TEST_CASE(T305__positive__simple_calculate_verify_signature, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T306__positive__simple_calculate_verify_signature, InitDebugFixture) { struct signature_args { yaca_key_type_e type; @@ -374,7 +401,7 @@ BOOST_FIXTURE_TEST_CASE(T305__positive__simple_calculate_verify_signature, InitD } } -BOOST_FIXTURE_TEST_CASE(T306__negative__simple_calculate_verify_signature, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T307__negative__simple_calculate_verify_signature, InitDebugFixture) { int ret; yaca_key_h key_priv = YACA_KEY_NULL, key_pub = YACA_KEY_NULL; @@ -541,7 +568,7 @@ BOOST_FIXTURE_TEST_CASE(T306__negative__simple_calculate_verify_signature, InitD yaca_free(signature); } -BOOST_FIXTURE_TEST_CASE(T307__positive__simple_calculate_hmac, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T308__positive__simple_calculate_hmac, InitDebugFixture) { struct hmac_args { yaca_key_type_e type; @@ -596,7 +623,7 @@ BOOST_FIXTURE_TEST_CASE(T307__positive__simple_calculate_hmac, InitDebugFixture) } } -BOOST_FIXTURE_TEST_CASE(T308__negative__simple_calculate_hmac, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T309__negative__simple_calculate_hmac, InitDebugFixture) { int ret; yaca_key_h key = YACA_KEY_NULL, key_prv = YACA_KEY_NULL; @@ -647,7 +674,7 @@ BOOST_FIXTURE_TEST_CASE(T308__negative__simple_calculate_hmac, InitDebugFixture) yaca_key_destroy(key_prv); } -BOOST_FIXTURE_TEST_CASE(T309__positive__simple_calculate_cmac, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T310__positive__simple_calculate_cmac, InitDebugFixture) { struct cmac_args { yaca_key_type_e type; @@ -701,7 +728,7 @@ BOOST_FIXTURE_TEST_CASE(T309__positive__simple_calculate_cmac, InitDebugFixture) } } -BOOST_FIXTURE_TEST_CASE(T3010__negative__simple_calculate_cmac, InitDebugFixture) +BOOST_FIXTURE_TEST_CASE(T3011__negative__simple_calculate_cmac, InitDebugFixture) { int ret; yaca_key_h key = YACA_KEY_NULL, key_prv = YACA_KEY_NULL; -- 2.7.4 From aab621df68682a90c9b562f146b5cfec7e4583d7 Mon Sep 17 00:00:00 2001 From: Dongsun Lee Date: Thu, 3 Sep 2020 12:56:32 +0900 Subject: [PATCH 07/16] Fix errors in public api headers Change-Id: Icd6bdf804d3df8c0e3dae5aab7b878dde5589c5f Signed-off-by: Dongsun Lee --- api/yaca/yaca_key.h | 2 +- api/yaca/yaca_types.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/api/yaca/yaca_key.h b/api/yaca/yaca_key.h index 8abf4d1..e0bf47a 100644 --- a/api/yaca/yaca_key.h +++ b/api/yaca/yaca_key.h @@ -222,7 +222,7 @@ int yaca_key_generate(yaca_key_type_e key_type, size_t key_bit_len, yaca_key_h * * based on pre-generated parameters. * @remarks This function does not support RSA keys, as it's not possible * to extract parameters from them. - * @remarks The @a key should be released using yaca_key_destroy(). + * @remarks The @a prv_key should be released using yaca_key_destroy(). * @param[in] params Pre-generated parameters * @param[out] prv_key Newly generated private key * @return #YACA_ERROR_NONE on success, diff --git a/api/yaca/yaca_types.h b/api/yaca/yaca_types.h index ec32158..8973422 100644 --- a/api/yaca/yaca_types.h +++ b/api/yaca/yaca_types.h @@ -283,12 +283,14 @@ typedef enum { * @brief Definition for the value indicating generator equal 2 for DH parameters. * To be or'ed with safe prime length in bits. Prime length is recommended * to be 2048 bits or higher. + * @since_tizen 3.0 */ #define YACA_KEY_LENGTH_DH_GENERATOR_2 (YACA_KEYLEN_COMPONENT_TYPE_DH | YACA_KEYLEN_COMPONENT_DH_GEN_2) /** * @brief Definition for the value indicating generator equal 5 for DH parameters. * To be or'ed with safe prime length in bits. Prime length is recommended * to be 2048 bits or higher. + * @since_tizen 3.0 */ #define YACA_KEY_LENGTH_DH_GENERATOR_5 (YACA_KEYLEN_COMPONENT_TYPE_DH | YACA_KEYLEN_COMPONENT_DH_GEN_5) -- 2.7.4 From 1a6ed356c15132b9affacd8fb5bb08fdbcfd898f Mon Sep 17 00:00:00 2001 From: Dariusz Michaluk Date: Wed, 2 Sep 2020 13:37:14 +0200 Subject: [PATCH 08/16] Automate code coverage measurement To gather unit tests coverage report: - use COVERAGE build_type, - instal yaca-coverage rpm, - run yaca-coverage.sh script. Change-Id: Ia3dd921d12e86cf0541252d9b1d224ce52a2d428 --- CMakeLists.txt | 4 ++++ packaging/yaca.spec | 47 +++++++++++++++++++++++------------------------ tests/CMakeLists.txt | 24 ++++++++++++++++++++++++ tests/yaca-coverage.sh.in | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 24 deletions(-) create mode 100644 tests/yaca-coverage.sh.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 89f74a5..8629504 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,10 @@ IF(NOT DEFINED EXAMPLES_DIR) SET(EXAMPLES_DIR "${SHARE_INSTALL_PREFIX}/${PROJECT_NAME}/examples") ENDIF(NOT DEFINED EXAMPLES_DIR) +IF(NOT DEFINED COVERAGE_DIR) + SET(COVERAGE_DIR "${SHARE_INSTALL_PREFIX}/${PROJECT_NAME}-coverage") +ENDIF(NOT DEFINED COVERAGE_DIR) + CONFIGURE_FILE(packaging/yaca.manifest.in yaca.manifest @ONLY) ADD_SUBDIRECTORY(${SRC_FOLDER}) diff --git a/packaging/yaca.spec b/packaging/yaca.spec index 22adaec..86d02ad 100644 --- a/packaging/yaca.spec +++ b/packaging/yaca.spec @@ -34,30 +34,23 @@ The package provides Yet Another Crypto API. %prep %setup -q +%global coverage_dir %{_datadir}/yaca-coverage + %build -%cmake . -DCMAKE_BUILD_TYPE=%{build_type} +%cmake . \ + -DCMAKE_BUILD_TYPE=%{build_type} \ + -DCOVERAGE_DIR=%{coverage_dir} make -k %{?jobs:-j%jobs} -%if %{build_type} == "COVERAGE" -mkdir -p gcov-obj -find . \( -name '*.gcno' ! -name 'tc_*' \) -exec cp '{}' gcov-obj ';' -%endif - %install %make_install %py3_compile %{buildroot}/%{python3_sitearch} -%if "%{build_type}" == "COVERAGE" -mkdir -p %{buildroot}%{_datadir}/gcov/obj -install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj -%endif - - %clean rm -rf %{buildroot} -## Devel Package ############################################################### +## Devel Package ############################################################## %package devel Summary: Yet Another Crypto API development files Group: Security/Other @@ -71,7 +64,7 @@ The package provides Yet Another Crypto API development files. %{_includedir}/yaca %{_libdir}/pkgconfig/yaca.pc -## Examples Package ############################################################ +## Examples Package ########################################################### %package examples Summary: Yet Another Crypto API example files Group: Security/Other @@ -84,7 +77,7 @@ The package provides Yet Another Crypto API example files. %{_bindir}/yaca-example* %{_datadir}/%{name}/examples -## Tests Package ############################################################ +## Tests Package ############################################################## %package tests Summary: Yet Another Crypto API tests Group: Security/Other @@ -108,14 +101,20 @@ The package provides Yet Another Crypto API bindings for Python3. %files -n python3-yaca %{python3_sitearch}/%{name} -## gcov Package ############################################################ +## Coverage Package ########################################################### %if "%{build_type}" == "COVERAGE" -%package gcov -Summary: yaca gcov for measuring test coverage -Group: Secureity/Testing -%description gcov -New yaca gcov objects - -%files gcov -%{_datadir}/gcov/obj/* +%package coverage +Summary: Yet Another Crypto API code coverage data +Group: Security/Other +Requires: yaca-tests = %{version}-%{release} +Requires: yaca-debugsource = %{version}-%{release} +Requires: lcov +Requires: gcc + +%description coverage +Yet Another Crypto API code coverage data + +%files coverage +%{_bindir}/yaca-coverage.sh +%coverage_dir %endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8e81541..efcbd0e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,6 +21,30 @@ # SET(TESTS_NAME yaca-unit-tests) + +IF (CMAKE_BUILD_TYPE MATCHES "COVERAGE") + + # coverage data + SET(COVERAGE_BUILD_DIR + ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${TESTS_NAME}.dir/ + ) + + # install gcno files + INSTALL( + DIRECTORY ${COVERAGE_BUILD_DIR}/ + DESTINATION ${COVERAGE_DIR} + FILES_MATCHING PATTERN "*.gcno" + ) + + # install code coverage automation script + CONFIGURE_FILE(yaca-coverage.sh.in yaca-coverage.sh @ONLY) + INSTALL( + PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/yaca-coverage.sh + DESTINATION ${CMAKE_INSTALL_BINDIR} + ) + +ENDIF (CMAKE_BUILD_TYPE MATCHES "COVERAGE") + FILE(GLOB YACA_SOURCES ${SRC_FOLDER}/*.c) SET(TESTS_SOURCES common.cpp diff --git a/tests/yaca-coverage.sh.in b/tests/yaca-coverage.sh.in new file mode 100644 index 0000000..6cd4584 --- /dev/null +++ b/tests/yaca-coverage.sh.in @@ -0,0 +1,39 @@ +#!/bin/bash + +set -exuo pipefail + +REPORT="@PROJECT_NAME@-coverage.info" +STDERR="@PROJECT_NAME@-coverage.stderr" +HTML_DIR="@PROJECT_NAME@-coverage" + +SRCS_DIR="/usr/src/debug/@PROJECT_NAME@-@VERSION@" + +# create dir for the report +mkdir $HTML_DIR + +# remove old gcda files +find / -iname "*.gcda" -exec rm {} \; + +# launch unit tests +yaca-unit-tests + +# copy source files +cp -rp $SRCS_DIR/* "@CMAKE_BINARY_DIR@" + +# copy gcda files +cp -r "@COVERAGE_BUILD_DIR@"/* "@COVERAGE_DIR@" + +# prepare report +rm -f $STDERR +lcov --no-external -c -d "@COVERAGE_DIR@" -b "@CMAKE_BINARY_DIR@" -o $REPORT 2>$STDERR +lcov -r $REPORT "@CMAKE_BINARY_DIR@/tests/*" -o $REPORT + +# check warnings +if [ -s $STDERR ] +then + echo "Warnings detected (see $STDERR). Aborting." + exit 1 +fi + +# html +genhtml $REPORT --output-directory $HTML_DIR/ -- 2.7.4 From 02f6dd11f549fb6ddb03a9b8036a8a7d76be4f80 Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Tue, 10 Nov 2020 14:28:57 +0100 Subject: [PATCH 09/16] Fix typo in python Change-Id: Ia15e71dbd32e62ad74eca315dd321eb5a4f23410 --- python/yaca/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) mode change 100644 => 100755 python/yaca/__init__.py diff --git a/python/yaca/__init__.py b/python/yaca/__init__.py old mode 100644 new mode 100755 index 3efd0bd..2002f08 --- a/python/yaca/__init__.py +++ b/python/yaca/__init__.py @@ -188,8 +188,8 @@ class DIGEST_ALGORITHM(_enum.Enum): class ENCRYPT_ALGORITHM(_enum.Enum): AES = 0 UNSAFE_DES = 1 - UNSAFE_TRIPPLE_DES_2TDEA = 2 - TRIPPLE_DES_3TDEA = 3 + UNSAFE_TRIPLE_DES_2TDEA = 2 + TRIPLE_DES_3TDEA = 3 UNSAFE_RC2 = 4 UNSAFE_RC4 = 5 CAST5 = 6 -- 2.7.4 From 9459272ec1d0bc653eee10da54da6c8c76dd5b4c Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Fri, 13 Nov 2020 15:36:38 +0100 Subject: [PATCH 10/16] Add a cmake option to not build tests Change-Id: Ifb5e887c2086df3e7e53920334e14d409ca98198 --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8629504..365bd0a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,7 +107,9 @@ CONFIGURE_FILE(packaging/yaca.manifest.in yaca.manifest @ONLY) ADD_SUBDIRECTORY(${SRC_FOLDER}) ADD_SUBDIRECTORY(${EXAMPLES_FOLDER}) -ADD_SUBDIRECTORY(${TESTS_FOLDER}) +IF(NOT WITHOUT_TESTS) + ADD_SUBDIRECTORY(${TESTS_FOLDER}) +ENDIF(NOT WITHOUT_TESTS) IF(NOT WITHOUT_PYTHON) ADD_SUBDIRECTORY(${PYTHON_FOLDER}) ENDIF(NOT WITHOUT_PYTHON) -- 2.7.4 From 105cd12fc5291c7d6b5f062ce7f0c57e46650ffe Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Fri, 13 Nov 2020 15:36:58 +0100 Subject: [PATCH 11/16] Add a static library Change-Id: Ic8ff0e863787bbb49dbdf59a05258e1f4bc79e70 --- src/CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 57c4488..7cf4872 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,6 +43,9 @@ ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS} ${HEADERS}) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${_LIB_SOVERSION_} VERSION ${_LIB_VERSION_}) +ADD_LIBRARY(${PROJECT_NAME}-static STATIC ${SRCS} ${HEADERS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME}-static PROPERTIES + OUTPUT_NAME ${PROJECT_NAME}) ## Link libraries ############################################################## PKG_CHECK_MODULES(YACA_DEPS REQUIRED openssl1.1 capi-base-common) @@ -65,6 +68,9 @@ INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PC_FILE} INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) +INSTALL(TARGETS ${PROJECT_NAME}-static + DESTINATION ${LIB_INSTALL_DIR} + COMPONENT DevelopmentLibraries) INSTALL(FILES ${HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/yaca) -- 2.7.4 From 32604b251b3b4e5e562867b780ff68c712b1dced Mon Sep 17 00:00:00 2001 From: Dariusz Michaluk Date: Thu, 26 Nov 2020 12:18:10 +0100 Subject: [PATCH 12/16] Add colour log formatter Change-Id: I2f5febb66b26107e092c72cfadccf6a91ab8a976 --- tests/CMakeLists.txt | 1 + tests/colour_log_formatter.cpp | 355 +++++++++++++++++++++++++++++++++++++++++ tests/colour_log_formatter.h | 78 +++++++++ tests/common.cpp | 2 + 4 files changed, 436 insertions(+) create mode 100644 tests/colour_log_formatter.cpp create mode 100644 tests/colour_log_formatter.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index efcbd0e..5bb357a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,6 +48,7 @@ ENDIF (CMAKE_BUILD_TYPE MATCHES "COVERAGE") FILE(GLOB YACA_SOURCES ${SRC_FOLDER}/*.c) SET(TESTS_SOURCES common.cpp + colour_log_formatter.cpp test_debug.cpp test_crypto.cpp test_key.cpp diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp new file mode 100644 index 0000000..5033e2f --- /dev/null +++ b/tests/colour_log_formatter.cpp @@ -0,0 +1,355 @@ +/* + * (C) Copyright Gennadiy Rozental 2005-2008. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * See http://www.boost.org/libs/test for the library home page. + */ +/* + * @file colour_log_formatter.cpp + * @author Zofia Abramowska (z.abramowska@samsung.com) + * @version + * @brief + */ +// Boost.Test +#include "colour_log_formatter.h" +#include +#if BOOST_VERSION >= 105900 +#include +#else +#include +#endif +#include +#include +#include + +// Boost +#include + +// STL +#include +#include + +const char* GREEN_BEGIN = "\033[0;32m"; +const char* RED_BEGIN = "\033[0;31m"; +const char* CYAN_BEGIN = "\033[0;36m"; +const char* BOLD_YELLOW_BEGIN = "\033[1;33m"; +const char* COLOR_END = "\033[m"; + +// ************************************************************************** // +// ************** colour_log_formatter ************** // +// ************************************************************************** // + +using namespace boost::unit_test; +namespace Yaca { + +namespace { + +const_string +test_unit_type_name(const test_unit &tu) +{ +#if BOOST_VERSION >= 105900 + return const_string(tu.p_type_name); +#else + return tu.p_type_name.get(); +#endif +} + +const_string +test_unit_name(const test_unit &tu) +{ +#if BOOST_VERSION >= 105900 + return const_string(tu.p_name); +#else + return tu.p_name.get(); +#endif +} + +const_string +test_phase_identifier() +{ + return test_unit_name(framework::current_test_case()); +} + +const_string +get_basename(const const_string &file_name) +{ + return basename(file_name.begin()); +} + +std::string +get_basename(const std::string &file_name) +{ + return basename(file_name.c_str()); +} + +bool +test_unit_type_name_contains(const test_unit &tu, const std::string &substr) +{ + return test_unit_type_name(tu).find(const_string(substr)) == 0; +} + +} // local namespace + +//____________________________________________________________________________// + +void +colour_log_formatter::log_start( + std::ostream &output, + counter_t test_cases_amount) +{ + if (test_cases_amount > 0) + output << "Running " << test_cases_amount << " test " + << (test_cases_amount > 1 ? "cases" : "case") << "...\n"; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::log_finish(std::ostream &ostr) +{ + ostr.flush(); +} + +//____________________________________________________________________________// + +void +colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info) +{ + if (log_build_info) + output << "Platform: " << BOOST_PLATFORM << '\n' + << "Compiler: " << BOOST_COMPILER << '\n' + << "STL : " << BOOST_STDLIB << '\n'; + output << "Boost : " << BOOST_VERSION / 100000 << '.' + << BOOST_VERSION / 100 % 1000 << '.' + << BOOST_VERSION % 100 << std::endl; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::test_unit_start( + std::ostream &output, + test_unit const &tu) +{ + if (test_unit_type_name_contains(tu, "suite")) { + output << "Starting test "; + } else { + output << "Running test "; + } + output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) + << "\"" << std::endl; + +} + +//____________________________________________________________________________// + +void +colour_log_formatter::test_unit_finish( + std::ostream &output, + test_unit const &tu, + unsigned long elapsed) +{ + if (test_unit_type_name_contains(tu, "suite")) { + output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" << + std::endl; + return; + } + + std::string color = GREEN_BEGIN; + std::string status = "OK"; + + if (m_isTestCaseFailed) { + color = RED_BEGIN; + status = "FAIL"; + } + + output << "\t" << "[ " << color << status << COLOR_END << + " ]"; + + + output << ", " << CYAN_BEGIN << "time: "; + + if (elapsed > 0) { + if (elapsed % 1000 == 0) + output << elapsed / 1000 << "ms"; + else + output << elapsed << "mks"; + } else { + output << "N/A"; + } + + output << COLOR_END << std::endl; + m_isTestCaseFailed = false; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::test_unit_skipped( + std::ostream &output, + test_unit const &tu) +{ + output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" << + "is skipped" << std::endl; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::log_exception( + std::ostream &output, + log_checkpoint_data const &checkpoint_data, + boost::execution_exception const &ex) +{ + boost::execution_exception::location const &loc = ex.where(); + output << '\t' << BOLD_YELLOW_BEGIN << get_basename( + loc.m_file_name) + << '(' << loc.m_line_num << "), "; + + output << "fatal error in \"" + << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) << + "\": "; + + output << COLOR_END << ex.what(); + + if (!checkpoint_data.m_file_name.is_empty()) { + output << '\n'; + output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name) + << '(' << checkpoint_data.m_line_num << ")"; + + if (!checkpoint_data.m_message.empty()) + output << ": " << checkpoint_data.m_message; + } + + output << std::endl; + m_isTestCaseFailed = true; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::log_entry_start( + std::ostream &output, + log_entry_data const &entry_data, + log_entry_types let) +{ + switch (let) { + case BOOST_UTL_ET_INFO: + output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num << + "), "; + output << "info: "; + break; + + case BOOST_UTL_ET_MESSAGE: + break; + + case BOOST_UTL_ET_WARNING: + output << '\t' << get_basename(entry_data.m_file_name) << '(' << + entry_data.m_line_num << "), "; + output << "warning in \"" << test_phase_identifier() << "\": "; + break; + + case BOOST_UTL_ET_ERROR: + output << '\t' << BOLD_YELLOW_BEGIN << get_basename( + entry_data.m_file_name) + << '(' << entry_data.m_line_num << "), "; + output << "error in \"" << test_phase_identifier() << "\": "; + m_isTestCaseFailed = true; + break; + + case BOOST_UTL_ET_FATAL_ERROR: + output << '\t' << BOLD_YELLOW_BEGIN << get_basename( + entry_data.m_file_name) + << '(' << entry_data.m_line_num << "), "; + output << " fatal error in \"" << test_phase_identifier() << "\": "; + m_isTestCaseFailed = true; + break; + } + + output << COLOR_END; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::log_entry_value( + std::ostream &output, + const_string value) +{ + output << value; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::log_entry_value( + std::ostream &output, + lazy_ostream const &value) +{ + output << value; +} + +//____________________________________________________________________________// + +void +colour_log_formatter::log_entry_finish( + std::ostream &output) +{ + output << std::endl; +} + +//____________________________________________________________________________// + +#if BOOST_VERSION >= 106501 +void +colour_log_formatter::log_exception_start( + std::ostream& os, + boost::unit_test::log_checkpoint_data const& lcd, + boost::execution_exception const& ex) +{ + log_exception(os, lcd, ex); +} + +void +colour_log_formatter::log_exception_finish(std::ostream& os) +{ + (void)os; +} + +void +colour_log_formatter::entry_context_start( + std::ostream& os, + boost::unit_test::log_level l) +{ + (void)os; + (void)l; +} + +void +colour_log_formatter::log_entry_context( + std::ostream& os, + boost::unit_test::log_level l, + boost::unit_test::const_string value) +{ + (void)os; + (void)l; + (void)value; +} + +void +colour_log_formatter::entry_context_finish( + std::ostream& os, + boost::unit_test::log_level l) +{ + (void)os; + (void)l; +} +#endif + +//____________________________________________________________________________// + +} // namespace Yaca + +//____________________________________________________________________________// diff --git a/tests/colour_log_formatter.h b/tests/colour_log_formatter.h new file mode 100644 index 0000000..c971263 --- /dev/null +++ b/tests/colour_log_formatter.h @@ -0,0 +1,78 @@ +/* + * (C) Copyright Gennadiy Rozental 2005-2008. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * See http://www.boost.org/libs/test for the library home page. + */ +/* + * @file colour_log_formatter.h + * @author Zofia Abramowska (z.abramowska@samsung.com) + * @version + * @brief + */ +#pragma once +#include + +namespace Yaca { +class colour_log_formatter : public boost::unit_test::unit_test_log_formatter { +public: + // Formatter interface + colour_log_formatter() : m_isTestCaseFailed(false) {} + void log_start( + std::ostream &, + boost::unit_test::counter_t test_cases_amount); + void log_finish(std::ostream &); + void log_build_info(std::ostream &output, bool log_build_info = true); + + void test_unit_start( + std::ostream &, + boost::unit_test::test_unit const &tu); + void test_unit_finish( + std::ostream &, + boost::unit_test::test_unit const &tu, + unsigned long elapsed); + void test_unit_skipped( + std::ostream &, + boost::unit_test::test_unit const &tu); + + void log_exception( + std::ostream &, + boost::unit_test::log_checkpoint_data const &, + boost::execution_exception const &ex); + + void log_entry_start( + std::ostream &, + boost::unit_test::log_entry_data const &, + log_entry_types let); + void log_entry_value( + std::ostream &, + boost::unit_test::const_string value); + void log_entry_value( + std::ostream &, + boost::unit_test::lazy_ostream const &value); + void log_entry_finish(std::ostream &); + +#if BOOST_VERSION >= 106501 + void log_exception_start( + std::ostream& os, + boost::unit_test::log_checkpoint_data const& lcd, + boost::execution_exception const& ex); + void log_exception_finish(std::ostream& os); + void entry_context_start( + std::ostream& os, + boost::unit_test::log_level l); + void log_entry_context( + std::ostream& os, + boost::unit_test::log_level l, + boost::unit_test::const_string value); + void entry_context_finish( + std::ostream& os, + boost::unit_test::log_level l); +#endif + +private: + bool m_isTestCaseFailed; +}; +} // namespace Yaca diff --git a/tests/common.cpp b/tests/common.cpp index e7b5db3..04c2779 100644 --- a/tests/common.cpp +++ b/tests/common.cpp @@ -36,6 +36,7 @@ #include "../src/debug.h" #include "common.h" +#include "colour_log_formatter.h" namespace { @@ -84,6 +85,7 @@ struct TestConfig { boost::unit_test::unit_test_log.set_threshold_level( boost::unit_test::log_test_units); boost::unit_test::results_reporter::set_level(boost::unit_test::SHORT_REPORT); + boost::unit_test::unit_test_log.set_formatter(new Yaca::colour_log_formatter); } ~TestConfig() { -- 2.7.4 From d8a2c64d6f1f6c586abdea37fb8790f6489f8a1e Mon Sep 17 00:00:00 2001 From: Dariusz Michaluk Date: Mon, 30 Nov 2020 11:16:59 +0100 Subject: [PATCH 13/16] Categorize neutral tests as positive Change-Id: I5fdd9344850e781a6625539cde9a350b8c932713 --- tests/test_debug.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_debug.cpp b/tests/test_debug.cpp index 3786abd..8d366ab 100644 --- a/tests/test_debug.cpp +++ b/tests/test_debug.cpp @@ -73,7 +73,7 @@ struct CallbackCleanup BOOST_AUTO_TEST_SUITE(TESTS_DEBUG) -BOOST_AUTO_TEST_CASE(T001__neutral__translate_error) +BOOST_AUTO_TEST_CASE(T001__positive__translate_error) { struct error_args { yaca_error_e err; @@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(T001__neutral__translate_error) } } -BOOST_FIXTURE_TEST_CASE(T002__neutral__debug_set_error_cb, CallbackCleanup) +BOOST_FIXTURE_TEST_CASE(T002__positive__debug_set_error_cb, CallbackCleanup) { ERROR_DUMP(YACA_ERROR_INTERNAL); BOOST_REQUIRE(error_cb_called == 0); @@ -114,7 +114,7 @@ BOOST_FIXTURE_TEST_CASE(T002__neutral__debug_set_error_cb, CallbackCleanup) BOOST_REQUIRE(error_cb_called == 2); } -BOOST_FIXTURE_TEST_CASE(T003__neutral__error_dump, CallbackCleanup) +BOOST_FIXTURE_TEST_CASE(T003__positive__error_dump, CallbackCleanup) { yaca_debug_set_error_cb(&debug_error_cb); @@ -146,7 +146,7 @@ BOOST_FIXTURE_TEST_CASE(T003__neutral__error_dump, CallbackCleanup) BOOST_REQUIRE(ellipsis == ELLIPSIS); } -BOOST_FIXTURE_TEST_CASE(T004__neutral__error_handle, CallbackCleanup) +BOOST_FIXTURE_TEST_CASE(T004__positive__error_handle, CallbackCleanup) { struct error_args { long err1; -- 2.7.4 From 997a1147b5b53e591a60c93c1f73917a5474c945 Mon Sep 17 00:00:00 2001 From: Dariusz Michaluk Date: Tue, 1 Dec 2020 11:38:19 +0100 Subject: [PATCH 14/16] Fix colour log formatter compatibility with boost 1.65 Change-Id: I855eed7f331163e0edc3dee9644e7c673b063377 --- tests/colour_log_formatter.cpp | 13 ++++++++----- tests/colour_log_formatter.h | 6 +++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp index 5033e2f..bd1e323 100644 --- a/tests/colour_log_formatter.cpp +++ b/tests/colour_log_formatter.cpp @@ -115,12 +115,15 @@ colour_log_formatter::log_finish(std::ostream &ostr) //____________________________________________________________________________// void -colour_log_formatter::log_build_info(std::ostream &output, bool log_build_info) +#if BOOST_VERSION >= 107000 +colour_log_formatter::log_build_info(std::ostream &output, bool) +#else +colour_log_formatter::log_build_info(std::ostream &output) +#endif { - if (log_build_info) - output << "Platform: " << BOOST_PLATFORM << '\n' - << "Compiler: " << BOOST_COMPILER << '\n' - << "STL : " << BOOST_STDLIB << '\n'; + output << "Platform: " << BOOST_PLATFORM << '\n' + << "Compiler: " << BOOST_COMPILER << '\n' + << "STL : " << BOOST_STDLIB << '\n'; output << "Boost : " << BOOST_VERSION / 100000 << '.' << BOOST_VERSION / 100 % 1000 << '.' << BOOST_VERSION % 100 << std::endl; diff --git a/tests/colour_log_formatter.h b/tests/colour_log_formatter.h index c971263..10c4b76 100644 --- a/tests/colour_log_formatter.h +++ b/tests/colour_log_formatter.h @@ -24,7 +24,11 @@ public: std::ostream &, boost::unit_test::counter_t test_cases_amount); void log_finish(std::ostream &); - void log_build_info(std::ostream &output, bool log_build_info = true); +#if BOOST_VERSION >= 107000 + void log_build_info(std::ostream &, bool); +#else + void log_build_info(std::ostream &); +#endif void test_unit_start( std::ostream &, -- 2.7.4 From bbd1d8e575c8f1df9397b2f87588363d2a60bddb Mon Sep 17 00:00:00 2001 From: Lukasz Pawelczyk Date: Tue, 1 Dec 2020 17:52:09 +0100 Subject: [PATCH 15/16] Fix style, coding rules and overall esthetics of log formatter code Change-Id: I96da85bbe6b24f37a79d76365a69707afd430752 --- tests/colour_log_formatter.cpp | 178 +++++++++++++++-------------------------- tests/colour_log_formatter.h | 38 +++++---- 2 files changed, 84 insertions(+), 132 deletions(-) diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp index bd1e323..81ffd06 100644 --- a/tests/colour_log_formatter.cpp +++ b/tests/colour_log_formatter.cpp @@ -12,6 +12,7 @@ * @version * @brief */ + // Boost.Test #include "colour_log_formatter.h" #include @@ -37,17 +38,14 @@ const char* CYAN_BEGIN = "\033[0;36m"; const char* BOLD_YELLOW_BEGIN = "\033[1;33m"; const char* COLOR_END = "\033[m"; -// ************************************************************************** // -// ************** colour_log_formatter ************** // -// ************************************************************************** // - using namespace boost::unit_test; + + namespace Yaca { namespace { -const_string -test_unit_type_name(const test_unit &tu) +const_string test_unit_type_name(const test_unit &tu) { #if BOOST_VERSION >= 105900 return const_string(tu.p_type_name); @@ -56,8 +54,7 @@ test_unit_type_name(const test_unit &tu) #endif } -const_string -test_unit_name(const test_unit &tu) +const_string test_unit_name(const test_unit &tu) { #if BOOST_VERSION >= 105900 return const_string(tu.p_name); @@ -66,73 +63,59 @@ test_unit_name(const test_unit &tu) #endif } -const_string -test_phase_identifier() +const_string test_phase_identifier() { return test_unit_name(framework::current_test_case()); } -const_string -get_basename(const const_string &file_name) +const_string get_basename(const const_string &file_name) { return basename(file_name.begin()); } -std::string -get_basename(const std::string &file_name) +std::string get_basename(const std::string &file_name) { return basename(file_name.c_str()); } -bool -test_unit_type_name_contains(const test_unit &tu, const std::string &substr) +bool test_unit_type_name_contains(const test_unit &tu, const std::string &substr) { return test_unit_type_name(tu).find(const_string(substr)) == 0; } } // local namespace -//____________________________________________________________________________// -void -colour_log_formatter::log_start( +void colour_log_formatter::log_start( std::ostream &output, counter_t test_cases_amount) { - if (test_cases_amount > 0) - output << "Running " << test_cases_amount << " test " - << (test_cases_amount > 1 ? "cases" : "case") << "...\n"; + if (test_cases_amount > 0) { + output << "Running " << test_cases_amount << " test " + << (test_cases_amount > 1 ? "cases" : "case") << "...\n"; + } } -//____________________________________________________________________________// - -void -colour_log_formatter::log_finish(std::ostream &ostr) +void colour_log_formatter::log_finish(std::ostream &ostr) { ostr.flush(); } -//____________________________________________________________________________// - -void #if BOOST_VERSION >= 107000 -colour_log_formatter::log_build_info(std::ostream &output, bool) +void colour_log_formatter::log_build_info(std::ostream &output, bool) #else -colour_log_formatter::log_build_info(std::ostream &output) +void colour_log_formatter::log_build_info(std::ostream &output) #endif { - output << "Platform: " << BOOST_PLATFORM << '\n' - << "Compiler: " << BOOST_COMPILER << '\n' - << "STL : " << BOOST_STDLIB << '\n'; - output << "Boost : " << BOOST_VERSION / 100000 << '.' - << BOOST_VERSION / 100 % 1000 << '.' - << BOOST_VERSION % 100 << std::endl; + output << "Platform: " << BOOST_PLATFORM << '\n' + << "Compiler: " << BOOST_COMPILER << '\n' + << "STL : " << BOOST_STDLIB << '\n'; + output << "Boost : " << BOOST_VERSION / 100000 << '.' + << BOOST_VERSION / 100 % 1000 << '.' + << BOOST_VERSION % 100 << '\n'; } -//____________________________________________________________________________// - -void -colour_log_formatter::test_unit_start( +void colour_log_formatter::test_unit_start( std::ostream &output, test_unit const &tu) { @@ -141,22 +124,17 @@ colour_log_formatter::test_unit_start( } else { output << "Running test "; } - output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) - << "\"" << std::endl; - + output << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"\n"; } -//____________________________________________________________________________// - -void -colour_log_formatter::test_unit_finish( +void colour_log_formatter::test_unit_finish( std::ostream &output, test_unit const &tu, unsigned long elapsed) { if (test_unit_type_name_contains(tu, "suite")) { - output << "Finished test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" << - std::endl; + output << "Finished test " << test_unit_type_name(tu) + << " \"" << test_unit_name(tu) << "\"\n"; return; } @@ -168,52 +146,44 @@ colour_log_formatter::test_unit_finish( status = "FAIL"; } - output << "\t" << "[ " << color << status << COLOR_END << - " ]"; - - - output << ", " << CYAN_BEGIN << "time: "; + output << "\t" << "[ " << color << status << COLOR_END << " ], " + << ", " << CYAN_BEGIN << "time: "; if (elapsed > 0) { - if (elapsed % 1000 == 0) + if (elapsed % 1000 == 0) { output << elapsed / 1000 << "ms"; - else + } else { output << elapsed << "mks"; + } } else { output << "N/A"; } - output << COLOR_END << std::endl; + output << COLOR_END << '\n'; m_isTestCaseFailed = false; } -//____________________________________________________________________________// - -void -colour_log_formatter::test_unit_skipped( +void colour_log_formatter::test_unit_skipped( std::ostream &output, test_unit const &tu) { - output << "Test " << test_unit_type_name(tu) << " \"" << test_unit_name(tu) << "\"" << - "is skipped" << std::endl; + output << "Test " << test_unit_type_name(tu) + << " \"" << test_unit_name(tu) << "\" is skipped\n"; } -//____________________________________________________________________________// - -void -colour_log_formatter::log_exception( +void colour_log_formatter::log_exception( std::ostream &output, log_checkpoint_data const &checkpoint_data, boost::execution_exception const &ex) { boost::execution_exception::location const &loc = ex.where(); - output << '\t' << BOLD_YELLOW_BEGIN << get_basename( - loc.m_file_name) + output << '\t' << BOLD_YELLOW_BEGIN + << get_basename(loc.m_file_name) << '(' << loc.m_line_num << "), "; output << "fatal error in \"" - << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) << - "\": "; + << (loc.m_function.is_empty() ? test_phase_identifier() : loc.m_function) + << "\": "; output << COLOR_END << ex.what(); @@ -222,26 +192,24 @@ colour_log_formatter::log_exception( output << "\tlast checkpoint : " << get_basename(checkpoint_data.m_file_name) << '(' << checkpoint_data.m_line_num << ")"; - if (!checkpoint_data.m_message.empty()) + if (!checkpoint_data.m_message.empty()) { output << ": " << checkpoint_data.m_message; + } } - output << std::endl; + output << '\n'; m_isTestCaseFailed = true; } -//____________________________________________________________________________// - -void -colour_log_formatter::log_entry_start( +void colour_log_formatter::log_entry_start( std::ostream &output, log_entry_data const &entry_data, log_entry_types let) { switch (let) { case BOOST_UTL_ET_INFO: - output << '\t' << entry_data.m_file_name << '(' << entry_data.m_line_num << - "), "; + output << '\t' << entry_data.m_file_name + << '(' << entry_data.m_line_num << "), "; output << "info: "; break; @@ -249,22 +217,22 @@ colour_log_formatter::log_entry_start( break; case BOOST_UTL_ET_WARNING: - output << '\t' << get_basename(entry_data.m_file_name) << '(' << - entry_data.m_line_num << "), "; + output << '\t' << get_basename(entry_data.m_file_name) + << '(' << entry_data.m_line_num << "), "; output << "warning in \"" << test_phase_identifier() << "\": "; break; case BOOST_UTL_ET_ERROR: - output << '\t' << BOLD_YELLOW_BEGIN << get_basename( - entry_data.m_file_name) + output << '\t' << BOLD_YELLOW_BEGIN + << get_basename(entry_data.m_file_name) << '(' << entry_data.m_line_num << "), "; output << "error in \"" << test_phase_identifier() << "\": "; m_isTestCaseFailed = true; break; case BOOST_UTL_ET_FATAL_ERROR: - output << '\t' << BOLD_YELLOW_BEGIN << get_basename( - entry_data.m_file_name) + output << '\t' << BOLD_YELLOW_BEGIN + << get_basename(entry_data.m_file_name) << '(' << entry_data.m_line_num << "), "; output << " fatal error in \"" << test_phase_identifier() << "\": "; m_isTestCaseFailed = true; @@ -274,40 +242,28 @@ colour_log_formatter::log_entry_start( output << COLOR_END; } -//____________________________________________________________________________// - -void -colour_log_formatter::log_entry_value( +void colour_log_formatter::log_entry_value( std::ostream &output, const_string value) { output << value; } -//____________________________________________________________________________// - -void -colour_log_formatter::log_entry_value( +void colour_log_formatter::log_entry_value( std::ostream &output, lazy_ostream const &value) { output << value; } -//____________________________________________________________________________// - -void -colour_log_formatter::log_entry_finish( +void colour_log_formatter::log_entry_finish( std::ostream &output) { - output << std::endl; + output << '\n'; } -//____________________________________________________________________________// - #if BOOST_VERSION >= 106501 -void -colour_log_formatter::log_exception_start( +void colour_log_formatter::log_exception_start( std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex) @@ -315,14 +271,12 @@ colour_log_formatter::log_exception_start( log_exception(os, lcd, ex); } -void -colour_log_formatter::log_exception_finish(std::ostream& os) +void colour_log_formatter::log_exception_finish(std::ostream& os) { (void)os; } -void -colour_log_formatter::entry_context_start( +void colour_log_formatter::entry_context_start( std::ostream& os, boost::unit_test::log_level l) { @@ -330,8 +284,7 @@ colour_log_formatter::entry_context_start( (void)l; } -void -colour_log_formatter::log_entry_context( +void colour_log_formatter::log_entry_context( std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value) @@ -341,8 +294,7 @@ colour_log_formatter::log_entry_context( (void)value; } -void -colour_log_formatter::entry_context_finish( +void colour_log_formatter::entry_context_finish( std::ostream& os, boost::unit_test::log_level l) { @@ -351,8 +303,4 @@ colour_log_formatter::entry_context_finish( } #endif -//____________________________________________________________________________// - } // namespace Yaca - -//____________________________________________________________________________// diff --git a/tests/colour_log_formatter.h b/tests/colour_log_formatter.h index 10c4b76..8760a74 100644 --- a/tests/colour_log_formatter.h +++ b/tests/colour_log_formatter.h @@ -12,66 +12,69 @@ * @version * @brief */ + #pragma once #include + namespace Yaca { + class colour_log_formatter : public boost::unit_test::unit_test_log_formatter { public: // Formatter interface colour_log_formatter() : m_isTestCaseFailed(false) {} - void log_start( + void log_start( std::ostream &, boost::unit_test::counter_t test_cases_amount); - void log_finish(std::ostream &); + void log_finish(std::ostream &); #if BOOST_VERSION >= 107000 - void log_build_info(std::ostream &, bool); + void log_build_info(std::ostream &, bool); #else - void log_build_info(std::ostream &); + void log_build_info(std::ostream &); #endif - void test_unit_start( + void test_unit_start( std::ostream &, boost::unit_test::test_unit const &tu); - void test_unit_finish( + void test_unit_finish( std::ostream &, boost::unit_test::test_unit const &tu, unsigned long elapsed); - void test_unit_skipped( + void test_unit_skipped( std::ostream &, boost::unit_test::test_unit const &tu); - void log_exception( + void log_exception( std::ostream &, boost::unit_test::log_checkpoint_data const &, boost::execution_exception const &ex); - void log_entry_start( + void log_entry_start( std::ostream &, boost::unit_test::log_entry_data const &, log_entry_types let); - void log_entry_value( + void log_entry_value( std::ostream &, boost::unit_test::const_string value); - void log_entry_value( + void log_entry_value( std::ostream &, boost::unit_test::lazy_ostream const &value); - void log_entry_finish(std::ostream &); + void log_entry_finish(std::ostream &); #if BOOST_VERSION >= 106501 - void log_exception_start( + void log_exception_start( std::ostream& os, boost::unit_test::log_checkpoint_data const& lcd, boost::execution_exception const& ex); - void log_exception_finish(std::ostream& os); - void entry_context_start( + void log_exception_finish(std::ostream& os); + void entry_context_start( std::ostream& os, boost::unit_test::log_level l); - void log_entry_context( + void log_entry_context( std::ostream& os, boost::unit_test::log_level l, boost::unit_test::const_string value); - void entry_context_finish( + void entry_context_finish( std::ostream& os, boost::unit_test::log_level l); #endif @@ -79,4 +82,5 @@ public: private: bool m_isTestCaseFailed; }; + } // namespace Yaca -- 2.7.4 From 3492dc19d22e2b83149a6fd36c34d9f03bb20941 Mon Sep 17 00:00:00 2001 From: Krzysztof Jackiewicz Date: Wed, 2 Dec 2020 14:55:23 +0100 Subject: [PATCH 16/16] Fix wrong key decryption test The test T604 fails from time to time at test_encrypt.cpp:738. The reason is that the wrong key used for decryption can generate a properly padded buffer with quite high probability (more than 1/256). Fix by adding a length check in above case. Also remove an outdated comment related to invalid decryption. Change-Id: I22f7c837dc30c605dadd5c7ba8fa6b025f3396e0 --- tests/test_encrypt.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/test_encrypt.cpp b/tests/test_encrypt.cpp index 92565a6..810c837 100644 --- a/tests/test_encrypt.cpp +++ b/tests/test_encrypt.cpp @@ -735,7 +735,16 @@ BOOST_FIXTURE_TEST_CASE(T604__negative__encrypt_decrypt, InitDebugFixture) decrypted_len = written; ret = yaca_decrypt_finalize(ctx, decrypted + decrypted_len, &written); - BOOST_REQUIRE(ret == YACA_ERROR_INVALID_PARAMETER); + if (ret != YACA_ERROR_INVALID_PARAMETER) { + /* + * There's a quite high (over 1/256) chance that the decryption with key2 will create + * a correctly padded buffer (e.g. last byte equal to 0x01). In such case we expect that + * the length of the decrypted buffer will not match the original one. + */ + + BOOST_REQUIRE(ret == YACA_ERROR_NONE); + BOOST_REQUIRE(decrypted_len + written != INPUT_DATA_SIZE); + } yaca_context_destroy(ctx); ctx = YACA_CONTEXT_NULL; @@ -1776,9 +1785,6 @@ BOOST_FIXTURE_TEST_CASE(T610__negative__encrypt_decrypt_ccm, InitDebugFixture) ret = yaca_context_set_property(ctx, YACA_PROPERTY_CCM_AAD, aad, aad_len); BOOST_REQUIRE(ret == YACA_ERROR_NONE); - /* In case of AES/CBC wrong key returned INVALID_PASS - * Why this inconsistency? - */ ret = yaca_decrypt_update(ctx, encrypted, encrypted_len, decrypted, &written); BOOST_REQUIRE(ret == YACA_ERROR_INVALID_PARAMETER); -- 2.7.4