X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=test_digest.c;h=8500233a03d2dd4d486e6367157f45fa54ee3b8d;hb=d003ede538d8bdb5fc18949da6ed8c3eebc49791;hp=cc1efe1ae43a266910d4429eb70fbfccaf597654;hpb=cf1e315c325d7717161066df3f2c43b38d537a6f;p=openssl-gost%2Fengine.git diff --git a/test_digest.c b/test_digest.c index cc1efe1..8500233 100644 --- a/test_digest.c +++ b/test_digest.c @@ -49,9 +49,9 @@ #define cMAGENT "\033[1;35m" #define cNORM "\033[m" #define TEST_ASSERT(e) {if ((test = (e))) \ - printf(cRED " Test FAILED\n" cNORM); \ + printf(cRED " Test FAILED" cNORM "\n"); \ else \ - printf(cGREEN " Test passed\n" cNORM);} + printf(cGREEN " Test passed" cNORM "\n");} /* To test older APIs. */ #pragma GCC diagnostic ignored "-Wdeprecated-declarations" @@ -163,6 +163,14 @@ static const char etalon_carry[] = { 0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x16, }; +/* This is another carry test. */ +static const char ff[] = { + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff +}; + struct hash_testvec { int nid; /* OpenSSL algorithm numeric id. */ const char *name; /* Test name and source. */ @@ -398,7 +406,7 @@ static const struct hash_testvec testvecs[] = { "\x2f\x4f\x65\x1f\xe8\x8f\xea\x46\xec\x6f\x22\x23\x72\x8d\x8d\xff" "\x39\x68\x89\x35\x58\xef\x00\xa3\x10\xc2\x3e\x7d\x19\x23\xba\x0c" }, - { /* carry */ + { /* Carry */ .nid = NID_id_GostR3411_2012_512, .name = "(carry)", .plaintext = etalon_carry, @@ -418,6 +426,35 @@ static const struct hash_testvec testvecs[] = { "\x81\xbb\x63\x2f\xa3\x1f\xcc\x38\xb4\xc3\x79\xa6\x62\xdb\xc5\x8b" "\x9b\xed\x83\xf5\x0d\x3a\x1b\x2c\xe7\x27\x1a\xb0\x2d\x25\xba\xbb" }, + { /* ff (Better carry test). */ + .nid = NID_id_GostR3411_2012_512, + .name = "64 bytes of FF", + .plaintext = ff, + .psize = sizeof(ff), + .digest = + "\x41\x62\x9d\xe6\x77\xd7\xe8\x09\x0c\x3c\xd7\x0a\xff\xe3\x30\x0d" + "\x1e\x1c\xfb\xa2\xdb\x97\x94\x5e\xc3\x7f\xeb\x4e\x13\x75\xbc\x02" + "\xa5\x3f\x00\x37\x0b\x7d\x71\x5b\x07\xf3\x7f\x93\xca\xc8\x44\xef" + "\xad\xbf\xd1\xb8\x5f\x9d\xda\xe3\xde\x96\x56\xc0\xe9\x5a\xff\xc7" + }, + { + .nid = NID_id_GostR3411_2012_256, + .name = "64 bytes of FF", + .plaintext = ff, + .psize = sizeof(ff), + .digest = + "\x96\x4a\x5a\xb6\x02\x86\xf1\x06\x28\x87\x43\xe2\xfe\x1a\x42\x2d" + "\x16\x08\x98\xca\x1b\xd5\x35\xe8\x31\xaa\x50\x0c\xfe\x34\xd7\xe8" + }, + { + .nid = NID_id_GostR3411_94, + .name = "64 bytes of FF", + .plaintext = ff, + .psize = sizeof(ff), + .digest = + "\x58\x50\x4d\x26\xb3\x67\x7e\x75\x6b\xa3\xf4\xa9\xfd\x2f\x14\xb3" + "\xba\x54\x57\x06\x6a\x4a\xa1\xd7\x00\x65\x9b\x90\xdc\xdd\xd3\xc6" + }, /* Synthetic tests. */ { .nid = NID_id_GostR3411_2012_256, @@ -427,7 +464,8 @@ static const struct hash_testvec testvecs[] = { .digest = "\xa2\xf3\x6d\x9c\x42\xa1\x1e\xad\xe3\xc1\xfe\x99\xf9\x99\xc3\x84" "\xe7\x98\xae\x24\x50\x75\x73\xd7\xfc\x99\x81\xa0\x45\x85\x41\xf6" - }, { + }, + { .nid = NID_id_GostR3411_2012_512, .name = "streebog512 synthetic test", .mdsize = 64, @@ -471,7 +509,7 @@ static int do_hmac_old(int iter, const EVP_MD *type, const char *plaintext, if (t->mdsize) T(len == t->mdsize); if (memcmp(md, t->hmac, len) != 0) { - printf(cRED "hmac mismatch\n" cNORM); + printf(cRED "hmac mismatch" cNORM "\n"); hexdump(t->hmac, len); hexdump(md, len); return 1; @@ -495,12 +533,9 @@ static int do_hmac_prov(int iter, const EVP_MD *type, const char *plaintext, OSSL_PARAM params[] = { OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, (char *)EVP_MD_name(type), 0), - OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, - (char *)t->key, t->key_size), OSSL_PARAM_END }; - T(EVP_MAC_CTX_set_params(ctx, params)); - T(EVP_MAC_init(ctx)); + T(EVP_MAC_init(ctx, (const unsigned char *)t->key, t->key_size, params)); T(EVP_MAC_update(ctx, (unsigned char *)plaintext, t->psize)); T(EVP_MAC_final(ctx, md, &len, EVP_MAX_MD_SIZE)); EVP_MAC_CTX_free(ctx); @@ -509,7 +544,7 @@ static int do_hmac_prov(int iter, const EVP_MD *type, const char *plaintext, if (t->mdsize) T(len == t->mdsize); if (memcmp(md, t->hmac, len) != 0) { - printf(cRED "hmac mismatch\n" cNORM); + printf(cRED "hmac mismatch" cNORM "\n"); hexdump(t->hmac, len); hexdump(md, len); return 1; @@ -571,11 +606,10 @@ static int do_cmac_prov(int iter, const char *plaintext, T(ctx = EVP_MAC_CTX_new(cmac)); OSSL_PARAM params[] = { OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, mdname, 0), - OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, (char *)t->key, t->key_size), OSSL_PARAM_END }; T(EVP_MAC_CTX_set_params(ctx, params)); - T(EVP_MAC_init(ctx)); + T(EVP_MAC_init(ctx, (const unsigned char *)t->key, t->key_size, params)); T(EVP_MAC_update(ctx, (unsigned char *)plaintext, t->psize)); T(EVP_MAC_final(ctx, md, &len, EVP_MAX_MD_SIZE)); EVP_MAC_CTX_free(ctx); @@ -585,7 +619,7 @@ static int do_cmac_prov(int iter, const char *plaintext, * So, just compare until what we need. */ T(mdsize <= len); if (memcmp(md, t->digest, mdsize) != 0) { - printf(cRED "cmac mismatch\n" cNORM); + printf(cRED "cmac mismatch" cNORM "\n"); hexdump(t->digest, mdsize); hexdump(md, len); return 1; @@ -635,7 +669,7 @@ static int do_digest(int iter, const EVP_MD *type, const char *plaintext, EVP_MD_CTX_free(ctx); T(len == mdsize); if (memcmp(md, t->digest, mdsize) != 0) { - printf(cRED "digest mismatch\n" cNORM); + printf(cRED "digest mismatch" cNORM "\n"); hexdump(t->digest, mdsize); hexdump(md, mdsize); return 1; @@ -673,9 +707,9 @@ static int do_test(const struct hash_testvec *tv) OPENSSL_free(buf); if (!ret) - printf(cGREEN "success\n" cNORM); + printf(cGREEN "success" cNORM "\n"); else - printf(cRED "fail\n" cNORM); + printf(cRED "fail" cNORM "\n"); return ret; } @@ -737,13 +771,13 @@ static int do_synthetic_once(const struct hash_testvec *tv, unsigned int shifts) EVP_MD_CTX_free(ctx2); if (len != mdlen) { - printf(cRED "digest output len mismatch %u != %u (expected)\n" cNORM, + printf(cRED "digest output len mismatch %u != %u (expected)" cNORM "\n", len, mdlen); goto err; } if (memcmp(md, tv->digest, mdlen) != 0) { - printf(cRED "digest mismatch\n" cNORM); + printf(cRED "digest mismatch" cNORM "\n"); unsigned int i; printf(" Expected value is: "); @@ -773,9 +807,9 @@ static int do_synthetic_test(const struct hash_testvec *tv) ret |= do_synthetic_once(tv, shifts); if (!ret) - printf(cGREEN "success\n" cNORM); + printf(cGREEN "success" cNORM "\n"); else - printf(cRED "fail\n" cNORM); + printf(cRED "fail" cNORM "\n"); return 0; } @@ -787,13 +821,7 @@ int main(int argc, char **argv) /* Trigger SIGBUS for unaligned access. */ sysmips(MIPS_FIXADE, 0); #endif - 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 hash_testvec *tv; for (tv = testvecs; tv->nid; tv++) { @@ -803,7 +831,9 @@ int main(int argc, char **argv) ret |= do_synthetic_test(tv); } + ENGINE *eng; ENGINE_DIGESTS_PTR fn_c; + T(eng = ENGINE_by_id("gost")); T(fn_c = ENGINE_get_digests(eng)); const int *nids; int n, k; @@ -813,15 +843,13 @@ int main(int argc, char **argv) if (tv->nid == nids[k]) break; if (!tv->nid) - printf(cMAGENT "Digest %s is untested!\n" cNORM, OBJ_nid2sn(nids[k])); + printf(cMAGENT "Digest %s is untested!" cNORM "\n", OBJ_nid2sn(nids[k])); } - - ENGINE_finish(eng); ENGINE_free(eng); if (ret) - printf(cDRED "= Some tests FAILED!\n" cNORM); + printf(cDRED "= Some tests FAILED!" cNORM "\n"); else - printf(cDGREEN "= All tests passed!\n" cNORM); + printf(cDGREEN "= All tests passed!" cNORM "\n"); return ret; }