diff --git a/tests/crypto/opencrypto/h_md5hmac.c b/tests/crypto/opencrypto/h_md5hmac.c index 9fa1ce455473..fdcaed86ef86 100644 --- a/tests/crypto/opencrypto/h_md5hmac.c +++ b/tests/crypto/opencrypto/h_md5hmac.c @@ -1,4 +1,4 @@ -/* $NetBSD: h_md5hmac.c,v 1.2 2014/01/17 14:16:08 pgoyette Exp $ */ +/* $NetBSD: h_md5hmac.c,v 1.3 2014/01/17 22:31:25 pgoyette Exp $ */ /*- * Copyright (c) 2014 The NetBSD Foundation, Inc. @@ -36,19 +36,109 @@ #include -unsigned char key[16] = { - 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, - 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b -}; -char plaintx[8] = "Hi There"; -const unsigned char ciphertx[16] = { - 0x21, 0xc6, 0x0d, 0xa5, 0x34, 0x24, 0x8b, 0xce, - 0x95, 0x86, 0x64, 0xb3, 0x66, 0x77, 0x9b, 0x4c +#define MD5_HMAC_KEYLEN 16 /* Fixed key length supported */ + +/* Test data from RFC2202 */ +const struct { + int num; + size_t key_len; + size_t len; + unsigned char key[80]; + unsigned char data[80]; + unsigned char mac[16]; +} tests[] = { + /* Test #1 */ + { 1, 16, 8, + { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }, + "Hi There", + { 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, + 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d } + }, + /* Test #2 */ + { 2, 4, 28, + "Jefe", + "what do ya want for nothing?", + { 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, + 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 } + }, + /* Test #3 */ + { 3, 16, 50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0x3a, 0x75, 0x16, 0x47, 0x46, 0xff, 0xaa, 0x79 } + }, + /* Test #5 */ + { 5, 16, 20, + { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }, + "Test With Truncation", + { 0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, + 0xf9, 0xba, 0xb9, 0x95, 0x69, 0x0e, 0xfd, 0x4c } + }, + /* Test #6 */ + { 6, 80, 54, + { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }, + "Test Using Larger Than Block-Size Key - Hash Key First", + { 0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, + 0x0b, 0x62, 0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd } + }, + /* Test #7 */ + { 7, 80, 73, + { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }, + "Test Using Larger Than Block-Size Key and Larger " + "Than One Block-Size Data", + { 0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, + 0x1f, 0xb1, 0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e } + }, }; int main(void) { + size_t i; int fd, res; struct session_op cs; struct crypt_op co; @@ -57,27 +147,35 @@ main(void) fd = open("/dev/crypto", O_RDWR, 0); if (fd < 0) err(1, "open"); - memset(&cs, 0, sizeof(cs)); - cs.mac = CRYPTO_MD5_HMAC; - cs.mackeylen = sizeof(key); - cs.mackey = key; - res = ioctl(fd, CIOCGSESSION, &cs); - if (res < 0) - err(1, "CIOCGSESSION"); + for (i = 0; i < __arraycount(tests); i++) { + if (tests[i].key_len != MD5_HMAC_KEYLEN) + continue; - memset(&co, 0, sizeof(co)); - memset(buf, 0, sizeof(buf)); - co.ses = cs.ses; - co.op = COP_ENCRYPT; - co.len = sizeof(plaintx); - co.src = plaintx; - co.mac = buf; - res = ioctl(fd, CIOCCRYPT, &co); - if (res < 0) - err(1, "CIOCCRYPT"); + memset(&cs, 0, sizeof(cs)); + cs.mac = CRYPTO_MD5_HMAC; + cs.mackeylen = tests[i].key_len; + cs.mackey = &tests[i].key; + res = ioctl(fd, CIOCGSESSION, &cs); + if (res < 0) + err(1, "CIOCGSESSION test %d", tests[i].num); - if (memcmp(co.dst, ciphertx, sizeof(ciphertx))) - errx(1, "verification failed"); + memset(&co, 0, sizeof(co)); + memset(buf, 0, sizeof(buf)); + co.ses = cs.ses; + co.op = COP_ENCRYPT; + co.len = tests[i].len; + co.src = &tests[i].data; + co.mac = buf; + res = ioctl(fd, CIOCCRYPT, &co); + if (res < 0) + err(1, "CIOCCRYPT test %d", tests[i].num); + if (memcmp(co.mac, tests[i].mac, sizeof(tests[i].mac))) + errx(1, "verification failed test %d", tests[i].num); + + res = ioctl(fd, CIOCFSESSION, &cs.ses); + if (res < 0) + err(1, "CIOCFSESSION test %d", tests[i].num); + } return 0; }