From: Vitaly Chikunov Date: Fri, 8 May 2020 14:49:15 +0000 (+0300) Subject: test_context: Instantiate ciphers using EVP API instead of direct calls X-Git-Tag: v3.0.0~142 X-Git-Url: http://wagner.pp.ru/gitweb/?a=commitdiff_plain;h=38c2c3ffc045d0a8f6efcb229ee2cf33f640353a;p=openssl-gost%2Fengine.git test_context: Instantiate ciphers using EVP API instead of direct calls Also, use testcases table and add more ciphers. --- diff --git a/test_context.c b/test_context.c index 72e1057..a9358f3 100644 --- a/test_context.c +++ b/test_context.c @@ -5,12 +5,7 @@ * See https://www.openssl.org/source/license.html for details */ -#include "gost_grasshopper_cipher.h" -#include "gost_grasshopper_defines.h" -#include "gost_grasshopper_math.h" -#include "gost_grasshopper_core.h" -#include "e_gost_err.h" -#include "gost_lcl.h" +#include #include #include #include @@ -49,19 +44,22 @@ static void hexdump(const void *ptr, size_t len) #define TEST_SIZE 256 #define STEP_SIZE 16 -static int test_contexts(const EVP_CIPHER *type, const int enc, const char *msg, - int acpkm) +static int test_contexts(int nid, const int enc, int acpkm) { EVP_CIPHER_CTX *ctx, *save; unsigned char pt[TEST_SIZE] = {1}; - unsigned char b[TEST_SIZE]; - unsigned char c[TEST_SIZE]; + unsigned char b[TEST_SIZE]; /* base output */ + unsigned char c[TEST_SIZE]; /* cloned output */ unsigned char K[32] = {1}; unsigned char iv[16] = {1}; int outlen, tmplen; int ret = 0, test = 0; - printf(cBLUE "%s test for %s\n" cNORM, enc ? "Encryption" : "Decryption", msg); + const EVP_CIPHER *type = EVP_get_cipherbynid(nid); + const char *name = EVP_CIPHER_name(type); + + printf(cBLUE "%s test for %s (nid %d)\n" cNORM, + enc ? "Encryption" : "Decryption", name, nid); /* produce base encryption */ ctx = EVP_CIPHER_CTX_new(); @@ -74,15 +72,15 @@ static int test_contexts(const EVP_CIPHER *type, const int enc, const char *msg, T(EVP_CipherFinal_ex(ctx, b + outlen, &tmplen)); /* and now tests */ - printf(" cloned contexts\n"); EVP_CIPHER_CTX_reset(ctx); EVP_CIPHER_CTX_reset(ctx); /* double call is intentional */ T(EVP_CipherInit_ex(ctx, type, NULL, K, iv, enc)); T(EVP_CIPHER_CTX_set_padding(ctx, 0)); if (acpkm) T(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_KEY_MESH, acpkm, NULL)); - save = ctx; + + printf(" cloned contexts: "); int i; memset(c, 0, sizeof(c)); for (i = 0; i < TEST_SIZE / STEP_SIZE; i++) { @@ -97,7 +95,7 @@ static int test_contexts(const EVP_CIPHER *type, const int enc, const char *msg, pt + STEP_SIZE * i, STEP_SIZE)); } - outlen = i * GRASSHOPPER_BLOCK_SIZE; + outlen = i * STEP_SIZE; T(EVP_CipherFinal_ex(ctx, c + outlen, &tmplen)); TEST_ASSERT(outlen != TEST_SIZE || memcmp(c, b, TEST_SIZE)); EVP_CIPHER_CTX_free(ctx); @@ -110,7 +108,7 @@ static int test_contexts(const EVP_CIPHER *type, const int enc, const char *msg, ret |= test; /* resume original context */ - printf(" base context\n"); + printf(" base context: "); memset(c, 0, sizeof(c)); T(EVP_CipherUpdate(save, c, &outlen, pt, sizeof(c))); T(EVP_CipherFinal_ex(save, c + outlen, &tmplen)); @@ -129,21 +127,45 @@ static int test_contexts(const EVP_CIPHER *type, const int enc, const char *msg, return ret; } +static struct testcase { + int nid; + int acpkm; +} testcases[] = { + { NID_id_Gost28147_89, }, + { NID_gost89_cnt, }, + { NID_gost89_cnt_12, }, + { NID_gost89_cbc, }, + { NID_grasshopper_ecb, }, + { NID_grasshopper_cbc, }, + { NID_grasshopper_cfb, }, + { NID_grasshopper_ofb, }, + { NID_grasshopper_ctr, }, + { NID_magma_cbc, }, + { NID_magma_ctr, }, + { NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm, 256 / 8 }, + { 0 }, +}; int main(int argc, char **argv) { int ret = 0; - ret |= test_contexts(cipher_gost_grasshopper_ecb(), 1, "grasshopper ecb", 0); - ret |= test_contexts(cipher_gost_grasshopper_ecb(), 0, "grasshopper ecb", 0); - ret |= test_contexts(cipher_gost_grasshopper_cbc(), 1, "grasshopper cbc", 0); - ret |= test_contexts(cipher_gost_grasshopper_cbc(), 0, "grasshopper cbc", 0); - ret |= test_contexts(cipher_gost_grasshopper_ctr(), 1, "grasshopper ctr", 0); - ret |= test_contexts(cipher_gost_grasshopper_ctr(), 0, "grasshopper ctr", 0); - ret |= test_contexts(cipher_gost_grasshopper_ofb(), 1, "grasshopper ofb", 0); - ret |= test_contexts(cipher_gost_grasshopper_ofb(), 0, "grasshopper ofb", 0); - ret |= test_contexts(cipher_gost_grasshopper_ctracpkm(), 1, "grasshopper ctracpkm", 256 / 8); - ret |= test_contexts(cipher_gost_grasshopper_ctracpkm(), 0, "grasshopper ctracpkm", 256 / 8); + setenv("OPENSSL_ENGINES", ENGINE_DIR, 0); + OPENSSL_add_all_algorithms_conf(); + ERR_load_crypto_strings(); + ENGINE *eng; + T(eng = ENGINE_by_id("gost")); + T(ENGINE_init(eng)); + T(ENGINE_set_default(eng, ENGINE_METHOD_ALL)); + + const struct testcase *t; + for (t = testcases; t->nid; t++) { + ret |= test_contexts(t->nid, 1, t->acpkm); + ret |= test_contexts(t->nid, 0, t->acpkm); + } + + ENGINE_finish(eng); + ENGINE_free(eng); if (ret) printf(cDRED "= Some tests FAILED!\n" cNORM);