X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=test_digest.c;h=8500233a03d2dd4d486e6367157f45fa54ee3b8d;hb=949e0de6bf5c9c75f18924f062cf505eea796361;hp=a395f89994e85154af3655b70edabd41ea4f0264;hpb=c3eb4304e412235062c6cf45f407c80cddbaf944;p=openssl-gost%2Fengine.git diff --git a/test_digest.c b/test_digest.c index a395f89..8500233 100644 --- a/test_digest.c +++ b/test_digest.c @@ -8,10 +8,15 @@ */ #include +#include #include #include #include #include +# include +#if OPENSSL_VERSION_MAJOR >= 3 +# include +#endif #include #include #include @@ -41,20 +46,33 @@ #define cDGREEN "\033[0;32m" #define cBLUE "\033[1;34m" #define cDBLUE "\033[0;34m" +#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" /* - * Test key from both GOST R 34.12-2015 and GOST R 34.13-2015. + * Test keys from both GOST R 34.12-2015 and GOST R 34.13-2015, + * for 128-bit cipher (A.1). */ static const char K[32] = { 0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77, 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, }; +/* + * Key for 64-bit cipher (A.2). + */ +static const char Km[32] = { + 0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00, + 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff, +}; + /* * Plaintext from GOST R 34.13-2015 A.1. * First 16 bytes is vector (a) from GOST R 34.12-2015 A.1. @@ -66,11 +84,22 @@ static const char P[] = { 0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xee,0xff,0x0a,0x00,0x11, }; +/* Plaintext for 64-bit cipher (A.2) */ +static const char Pm[] = { + 0x92,0xde,0xf0,0x6b,0x3c,0x13,0x0a,0x59,0xdb,0x54,0xc7,0x04,0xf8,0x18,0x9d,0x20, + 0x4a,0x98,0xfb,0x2e,0x67,0xa8,0x02,0x4c,0x89,0x12,0x40,0x9b,0x17,0xb5,0x7e,0x41, +}; + /* - * OMAC1/CMAC test vector from GOST R 34.13-2015 А.1.6 + * Kuznyechik OMAC1/CMAC test vector from GOST R 34.13-2015 А.1.6 */ static const char MAC_omac[] = { 0x33,0x6f,0x4d,0x29,0x60,0x59,0xfb,0xe3 }; +/* + * Magma OMAC1/CMAC test vector from GOST R 34.13-2015 А.2.6 + */ +static const char MAC_magma_omac[] = { 0x15,0x4e,0x72,0x10 }; + /* * OMAC-ACPKM test vector from R 1323565.1.017-2018 A.4.1 */ @@ -134,11 +163,20 @@ 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. */ const char *plaintext; /* Input (of psize), NULL for synthetic test. */ const char *digest; /* Expected output (of EVP_MD_size or truncate). */ + const char *hmac; /* Expected output for HMAC tests. */ const char *key; /* MAC key.*/ int psize; /* Input (plaintext) size. */ int mdsize; /* Compare to EVP_MD_size() if non-zero. */ @@ -152,7 +190,7 @@ struct hash_testvec { }; static const struct hash_testvec testvecs[] = { - { + { /* Test vectors from standards. */ .nid = NID_id_GostR3411_2012_512, .name = "M1 from RFC 6986 (10.1.1) and GOST R 34.11-2012 (А.1.1)", .plaintext = @@ -220,6 +258,17 @@ static const struct hash_testvec testvecs[] = { .mdsize = 128 / 8, .truncate = sizeof(MAC_omac), }, + { + .nid = NID_magma_mac, + .name = "P from GOST R 34.13-2015 (А.2.6)", + .plaintext = Pm, + .psize = sizeof(Pm), + .key = Km, + .key_size = sizeof(Km), + .digest = MAC_magma_omac, + .mdsize = 64 / 8, + .truncate = sizeof(MAC_magma_omac), + }, { .nid = NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac, .name = "M from R 1323565.1.017-2018 (A.4.1)", @@ -244,6 +293,36 @@ static const struct hash_testvec testvecs[] = { .digest = MAC_omac_acpkm2, .mdsize = sizeof(MAC_omac_acpkm2), }, + { /* HMAC tests. */ + .nid = NID_id_GostR3411_2012_512, + .name = "HMAC_GOSTR3411_2012_512 from RFC 7836 (B) and R 50.1.113-2016 (A)", + .plaintext = + "\x01\x26\xbd\xb8\x78\x00\xaf\x21\x43\x41\x45\x65\x63\x78\x01\x00", + .psize = 16, + .key = + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", + .key_size = 32, + .hmac = + "\xa5\x9b\xab\x22\xec\xae\x19\xc6\x5f\xbd\xe6\xe5\xf4\xe9\xf5\xd8" + "\x54\x9d\x31\xf0\x37\xf9\xdf\x9b\x90\x55\x00\xe1\x71\x92\x3a\x77" + "\x3d\x5f\x15\x30\xf2\xed\x7e\x96\x4c\xb2\xee\xdc\x29\xe9\xad\x2f" + "\x3a\xfe\x93\xb2\x81\x4f\x79\xf5\x00\x0f\xfc\x03\x66\xc2\x51\xe6", + }, + { + .nid = NID_id_GostR3411_2012_256, + .name = "HMAC_GOSTR3411_2012_256 from RFC 7836 (B) and R 50.1.113-2016 (A)", + .plaintext = + "\x01\x26\xbd\xb8\x78\x00\xaf\x21\x43\x41\x45\x65\x63\x78\x01\x00", + .psize = 16, + .key = + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", + .key_size = 32, + .hmac = + "\xa1\xaa\x5f\x7d\xe4\x02\xd7\xb3\xd3\x23\xf2\x99\x1c\x8d\x45\x34" + "\x01\x31\x37\x01\x0a\x83\x75\x4f\xd0\xaf\x6d\x7c\xd4\x92\x2e\xd9", + }, /* Other KATs. */ { .nid = NID_id_GostR3411_2012_512, @@ -269,7 +348,7 @@ static const struct hash_testvec testvecs[] = { }, { /* M4 */ .nid = NID_id_GostR3411_2012_512, - .name = "64 bytes of zeros (M4)", + .name = "64 bytes of zero (M4)", .plaintext = etalon_M4, .psize = sizeof(etalon_M4), .digest = @@ -280,7 +359,7 @@ static const struct hash_testvec testvecs[] = { }, { .nid = NID_id_GostR3411_2012_256, - .name = "64 bytes of zeros (M4)", + .name = "64 bytes of zero (M4)", .plaintext = etalon_M4, .psize = sizeof(etalon_M4), .digest = @@ -327,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, @@ -347,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, @@ -356,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, @@ -382,34 +491,173 @@ static void hexdump(const void *ptr, size_t len) printf("\n"); } -static int do_digest(const EVP_MD *type, const char *plaintext, - unsigned int psize, const char *etalon, int mdsize, int truncate, - const char *key, unsigned int key_size, int acpkm, int acpkm_t, - int block_size) +static int do_hmac_old(int iter, const EVP_MD *type, const char *plaintext, + const struct hash_testvec *t) +{ + unsigned int len; + unsigned char md[EVP_MAX_MD_SIZE]; + if (!iter) + printf("[HMAC] "); + + HMAC_CTX *ctx; + T(ctx = HMAC_CTX_new()); + T(HMAC_Init_ex(ctx, t->key, t->key_size, type, NULL)); + T(HMAC_Update(ctx, (const unsigned char *)plaintext, t->psize)); + T(HMAC_Final(ctx, md, &len)); + HMAC_CTX_free(ctx); + + if (t->mdsize) + T(len == t->mdsize); + if (memcmp(md, t->hmac, len) != 0) { + printf(cRED "hmac mismatch" cNORM "\n"); + hexdump(t->hmac, len); + hexdump(md, len); + return 1; + } + return 0; +} + +#if OPENSSL_VERSION_MAJOR >= 3 +static int do_hmac_prov(int iter, const EVP_MD *type, const char *plaintext, + const struct hash_testvec *t) +{ + size_t len; + unsigned char md[EVP_MAX_MD_SIZE]; + if (!iter) + printf("[EVP_MAC] "); + + EVP_MAC *hmac; + T(hmac = EVP_MAC_fetch(NULL, "HMAC", NULL)); + EVP_MAC_CTX *ctx; + T(ctx = EVP_MAC_CTX_new(hmac)); + OSSL_PARAM params[] = { + OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, + (char *)EVP_MD_name(type), 0), + OSSL_PARAM_END + }; + 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); + EVP_MAC_free(hmac); + + if (t->mdsize) + T(len == t->mdsize); + if (memcmp(md, t->hmac, len) != 0) { + printf(cRED "hmac mismatch" cNORM "\n"); + hexdump(t->hmac, len); + hexdump(md, len); + return 1; + } + return 0; +} +#endif + +static int do_hmac(int iter, const EVP_MD *type, const char *plaintext, + const struct hash_testvec *t) +{ + int ret; + + /* Test old (deprecated) and (too) new APIs. */ + ret = do_hmac_old(iter, type, plaintext, t); +#if OPENSSL_VERSION_MAJOR >= 3 + ret |= do_hmac_prov(iter, type, plaintext, t); +#endif + + return ret; +} + +/* + * If we have OMAC1/CMAC test vector, + * use CMAC provider to test it. + */ +static int do_cmac_prov(int iter, const char *plaintext, + const struct hash_testvec *t) +{ +#if OPENSSL_VERSION_MAJOR >= 3 + char *mdname = NULL; + /* + * CMAC needs CBC. + * Convert 'mac' digest to the underlying CBC cipher. + */ + switch (t->nid) { + case NID_grasshopper_mac: + mdname = "kuznyechik-cbc"; + break; + case NID_magma_mac: + mdname = "magma-cbc"; + break; + default: + return 0; + } + + if (!iter) + printf("[CMAC(%s)] ", mdname); + + size_t len; + unsigned char md[EVP_MAX_MD_SIZE]; + size_t mdsize = t->mdsize; + if (t->truncate) + mdsize = t->truncate; + + EVP_MAC *cmac; + T(cmac = EVP_MAC_fetch(NULL, "CMAC", NULL)); + EVP_MAC_CTX *ctx; + T(ctx = EVP_MAC_CTX_new(cmac)); + OSSL_PARAM params[] = { + OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, mdname, 0), + OSSL_PARAM_END + }; + T(EVP_MAC_CTX_set_params(ctx, params)); + 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); + EVP_MAC_free(cmac); + + /* CMAC provider will not respect outsize, and will output full block. + * So, just compare until what we need. */ + T(mdsize <= len); + if (memcmp(md, t->digest, mdsize) != 0) { + printf(cRED "cmac mismatch" cNORM "\n"); + hexdump(t->digest, mdsize); + hexdump(md, len); + return 1; + } +#endif + return 0; +} + +static int do_digest(int iter, const EVP_MD *type, const char *plaintext, + const struct hash_testvec *t) { - if (mdsize) - T(EVP_MD_size(type) == mdsize); - if (truncate) - mdsize = truncate; + if (!iter) + printf("[MD] "); + if (t->mdsize) + T(EVP_MD_size(type) == t->mdsize); + size_t mdsize; + if (t->truncate) + mdsize = t->truncate; else mdsize = EVP_MD_size(type); - if (block_size) - T(EVP_MD_block_size(type) == block_size); + if (t->block_size) + T(EVP_MD_block_size(type) == t->block_size); EVP_MD_CTX *ctx; T(ctx = EVP_MD_CTX_new()); T(EVP_MD_CTX_init(ctx)); T(EVP_DigestInit_ex(ctx, type, NULL)); - if (key) - T(EVP_MD_CTX_ctrl(ctx, EVP_MD_CTRL_SET_KEY, key_size, (void *)key)); - if (acpkm) + if (t->key) + T(EVP_MD_CTX_ctrl(ctx, EVP_MD_CTRL_SET_KEY, t->key_size, + (void *)t->key)); + if (t->acpkm) T(EVP_MD_CTX_ctrl(ctx, - EVP_CTRL_KEY_MESH, acpkm, acpkm_t? &acpkm_t : NULL)); - T(EVP_DigestUpdate(ctx, plaintext, psize)); + EVP_CTRL_KEY_MESH, t->acpkm, + t->acpkm_t? (void *)&t->acpkm_t : NULL)); + T(EVP_DigestUpdate(ctx, plaintext, t->psize)); unsigned int len; unsigned char md[EVP_MAX_MD_SIZE]; - if (EVP_MD_flags(EVP_MD_CTX_md(ctx)) & EVP_MD_FLAG_XOF) { T(EVP_DigestFinalXOF(ctx, md, mdsize)); len = mdsize; @@ -420,8 +668,10 @@ static int do_digest(const EVP_MD *type, const char *plaintext, EVP_MD_CTX_free(ctx); T(len == mdsize); - if (memcmp(md, etalon, mdsize) != 0) { - printf(cRED "digest mismatch\n" cNORM); + if (memcmp(md, t->digest, mdsize) != 0) { + printf(cRED "digest mismatch" cNORM "\n"); + hexdump(t->digest, mdsize); + hexdump(md, mdsize); return 1; } @@ -430,35 +680,37 @@ static int do_digest(const EVP_MD *type, const char *plaintext, static int do_test(const struct hash_testvec *tv) { - int ret = 0; - - const EVP_MD *type; - T(type = EVP_get_digestbynid(tv->nid)); - const char *name = EVP_MD_name(type); - printf(cBLUE "Test %s: %s: " cNORM, name, tv->name); - fflush(stdout); - ret |= do_digest(type, tv->plaintext, tv->psize, tv->digest, - tv->mdsize, tv->truncate, tv->key, tv->key_size, tv->acpkm, - tv->acpkm_t, tv->block_size); - - /* Text alignment problems. */ - int shifts = 32; - int i; - char *buf; - T(buf = OPENSSL_malloc(tv->psize + shifts)); - for (i = 0; i < shifts; i++) { - memcpy(buf + i, tv->plaintext, tv->psize); - ret |= do_digest(type, buf + i, tv->psize, tv->digest, - tv->mdsize, tv->truncate, tv->key, tv->key_size, tv->acpkm, - tv->acpkm_t, tv->block_size); - } - OPENSSL_free(buf); - - if (!ret) - printf(cGREEN "success\n" cNORM); + int ret = 0; + + const EVP_MD *type; + T(type = EVP_get_digestbynid(tv->nid)); + const char *name = EVP_MD_name(type); + printf(cBLUE "Test %s: %s: " cNORM, name, tv->name); + + /* Test alignment problems. */ + int shifts = 32; + int i; + char *buf; + T(buf = OPENSSL_malloc(tv->psize + shifts)); + for (i = 0; i < shifts; i++) { + memcpy(buf + i, tv->plaintext, tv->psize); + if (tv->hmac) + ret |= do_hmac(i, type, buf + i, tv); else - printf(cRED "fail\n" cNORM); - return ret; + ret |= do_digest(i, type, buf + i, tv); + /* Test CMAC provider. */ + ret |= do_cmac_prov(i, buf + i, tv); + /* No need to continue loop on failure. */ + if (ret) + break; + } + OPENSSL_free(buf); + + if (!ret) + printf(cGREEN "success" cNORM "\n"); + else + printf(cRED "fail" cNORM "\n"); + return ret; } #define SUPER_SIZE 256 @@ -519,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: "); @@ -547,7 +799,7 @@ static int do_synthetic_test(const struct hash_testvec *tv) { int ret = 0; - printf(cBLUE "Synthetic test %s: " cNORM, tv->name); + printf(cBLUE "Test %s: " cNORM, tv->name); fflush(stdout); unsigned int shifts; @@ -555,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; } @@ -569,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++) { @@ -585,12 +831,25 @@ int main(int argc, char **argv) ret |= do_synthetic_test(tv); } - ENGINE_finish(eng); + 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; + n = fn_c(eng, NULL, &nids, 0); + for (k = 0; k < n; ++k) { + for (tv = testvecs; tv->nid; tv++) + if (tv->nid == nids[k]) + break; + if (!tv->nid) + printf(cMAGENT "Digest %s is untested!" cNORM "\n", OBJ_nid2sn(nids[k])); + } 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; }