diff options
author | Damien Bergamini <damien@cvs.openbsd.org> | 2008-08-12 15:49:09 +0000 |
---|---|---|
committer | Damien Bergamini <damien@cvs.openbsd.org> | 2008-08-12 15:49:09 +0000 |
commit | 31b5a37fa02f6bd597b9975d407c1228ca26a35c (patch) | |
tree | 40ccbc188fc3d839e7a6d49e61c633e94b840771 /regress/sys | |
parent | 2fcb64222ee15913fadca59e342e93b3d41857ad (diff) |
test vectors for HMAC-MD5, HMAC-SHA1, HMAC-SHA256, AES-128-CMAC,
AES Key Wrap.
ok djm@
Diffstat (limited to 'regress/sys')
-rw-r--r-- | regress/sys/crypto/Makefile | 5 | ||||
-rw-r--r-- | regress/sys/crypto/cmac/Makefile | 27 | ||||
-rw-r--r-- | regress/sys/crypto/cmac/cmac_test.c | 109 | ||||
-rw-r--r-- | regress/sys/crypto/cmac/cmac_test.txt | 38 | ||||
-rw-r--r-- | regress/sys/crypto/hmac/Makefile | 27 | ||||
-rw-r--r-- | regress/sys/crypto/hmac/hmac_test.c | 76 | ||||
-rw-r--r-- | regress/sys/crypto/hmac/hmac_test.txt | 9 | ||||
-rw-r--r-- | regress/sys/crypto/key_wrap/Makefile | 27 | ||||
-rw-r--r-- | regress/sys/crypto/key_wrap/key_wrap_test.c | 63 | ||||
-rw-r--r-- | regress/sys/crypto/key_wrap/key_wrap_test.txt | 66 |
10 files changed, 446 insertions, 1 deletions
diff --git a/regress/sys/crypto/Makefile b/regress/sys/crypto/Makefile index c32525c4274..5977c1b3532 100644 --- a/regress/sys/crypto/Makefile +++ b/regress/sys/crypto/Makefile @@ -1,9 +1,12 @@ -# $OpenBSD: Makefile,v 1.6 2008/06/12 19:44:39 djm Exp $ +# $OpenBSD: Makefile,v 1.7 2008/08/12 15:49:07 damien Exp $ SUBDIR=enc SUBDIR+=aesctr SUBDIR+=aesxts SUBDIR+=aes +SUBDIR+=cmac +SUBDIR+=hmac +SUBDIR+=key_wrap install: diff --git a/regress/sys/crypto/cmac/Makefile b/regress/sys/crypto/cmac/Makefile new file mode 100644 index 00000000000..03e87879a9c --- /dev/null +++ b/regress/sys/crypto/cmac/Makefile @@ -0,0 +1,27 @@ +# $OpenBSD: Makefile,v 1.1 2008/08/12 15:49:07 damien Exp $ + +DIR=${.CURDIR}/../../../../sys + +PROG= cmac_test +SRCS+= rijndael.c cmac.c cmac_test.c +CDIAGFLAGS= -Wall +CDIAGFLAGS+= -Werror +CDIAGFLAGS+= -Wpointer-arith +CDIAGFLAGS+= -Wno-uninitialized +CDIAGFLAGS+= -Wstrict-prototypes +CDIAGFLAGS+= -Wmissing-prototypes +CDIAGFLAGS+= -Wunused +CDIAGFLAGS+= -Wsign-compare +CDIAGFLAGS+= -Wbounded +CDIAGFLAGS+= -Wshadow + +REGRESS_TARGETS= run-regress-${PROG} + +CFLAGS+= -I${DIR} + +.PATH: ${DIR}/crypto/ + +run-regress-${PROG}: ${PROG} + ./${PROG} | diff - ${PROG}.txt + +.include <bsd.regress.mk> diff --git a/regress/sys/crypto/cmac/cmac_test.c b/regress/sys/crypto/cmac/cmac_test.c new file mode 100644 index 00000000000..5241e780903 --- /dev/null +++ b/regress/sys/crypto/cmac/cmac_test.c @@ -0,0 +1,109 @@ +#include <sys/param.h> +#include <crypto/rijndael.h> +#include <crypto/cmac.h> + +void print_hex(char *str, unsigned char *buf, int len) +{ + int i; + + for ( i=0; i<len; i++ ) { + if ( (i % 16) == 0 && i != 0 ) printf(str); + printf("%02x", buf[i]); + if ( (i % 4) == 3 ) printf(" "); + if ( (i % 16) == 15 ) printf("\n"); + } + if ( (i % 16) != 0 ) printf("\n"); +} + +void print128(unsigned char *bytes) +{ + int j; + for (j=0; j<16;j++) { + printf("%02x",bytes[j]); + if ( (j%4) == 3 ) printf(" "); + } +} + +int +main(void) +{ + unsigned char L[16], K1[16], K2[16], T[16], TT[12]; + unsigned char M[64] = { + 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, + 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, + 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, + 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, + 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, + 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, + 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 + }; + unsigned char key[16] = { + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c + }; + AES_CMAC_CTX ctx; + + printf("--------------------------------------------------\n"); + printf("K "); print128(key); printf("\n"); + + printf("\nExample 1: len = 0\n"); + printf("M "); printf("<empty string>\n"); + + AES_CMAC_SetKey(&ctx, key); + AES_CMAC_Init(&ctx); + AES_CMAC_Update(&ctx, M, 0); + AES_CMAC_Final(T, &ctx); + printf("AES_CMAC "); print128(T); printf("\n"); + + printf("\nExample 2: len = 16\n"); + printf("M "); print_hex(" ",M,16); + + AES_CMAC_SetKey(&ctx, key); + AES_CMAC_Init(&ctx); + AES_CMAC_Update(&ctx, M, 16); + AES_CMAC_Final(T, &ctx); + printf("AES_CMAC "); print128(T); printf("\n"); + printf("\nExample 3: len = 40\n"); + printf("M "); print_hex(" ",M,40); + + AES_CMAC_SetKey(&ctx, key); + AES_CMAC_Init(&ctx); + AES_CMAC_Update(&ctx, M, 40); + AES_CMAC_Final(T, &ctx); + printf("AES_CMAC "); print128(T); printf("\n"); + + printf("\nExample 4: len = 64\n"); + printf("M "); print_hex(" ",M,64); + AES_CMAC_SetKey(&ctx, key); + AES_CMAC_Init(&ctx); + AES_CMAC_Update(&ctx, M, 64); + AES_CMAC_Final(T, &ctx); + printf("AES_CMAC "); print128(T); printf("\n"); + + printf("\nExample 4bis: len = 64\n"); + printf("M "); print_hex(" ",M,64); + AES_CMAC_SetKey(&ctx, key); + AES_CMAC_Init(&ctx); + AES_CMAC_Update(&ctx, M, 40); + AES_CMAC_Update(&ctx, M + 40, 24); + AES_CMAC_Final(T, &ctx); + printf("AES_CMAC "); print128(T); printf("\n"); + + printf("\nExample 4ter: len = 64\n"); + printf("M "); print_hex(" ",M,64); + AES_CMAC_SetKey(&ctx, key); + AES_CMAC_Init(&ctx); + AES_CMAC_Update(&ctx, M, 16); + AES_CMAC_Update(&ctx, M + 16, 16); + AES_CMAC_Update(&ctx, M + 32, 10); + AES_CMAC_Update(&ctx, M + 42, 0); + AES_CMAC_Update(&ctx, M + 42, 14); + AES_CMAC_Update(&ctx, M + 56, 8); + AES_CMAC_Final(T, &ctx); + printf("AES_CMAC "); print128(T); printf("\n"); + + printf("--------------------------------------------------\n"); + + return 0; +} diff --git a/regress/sys/crypto/cmac/cmac_test.txt b/regress/sys/crypto/cmac/cmac_test.txt new file mode 100644 index 00000000000..6f4140f54e6 --- /dev/null +++ b/regress/sys/crypto/cmac/cmac_test.txt @@ -0,0 +1,38 @@ +-------------------------------------------------- +K 2b7e1516 28aed2a6 abf71588 09cf4f3c + +Example 1: len = 0 +M <empty string> +AES_CMAC bb1d6929 e9593728 7fa37d12 9b756746 + +Example 2: len = 16 +M 6bc1bee2 2e409f96 e93d7e11 7393172a +AES_CMAC 070a16b4 6b4d4144 f79bdd9d d04a287c + +Example 3: len = 40 +M 6bc1bee2 2e409f96 e93d7e11 7393172a + ae2d8a57 1e03ac9c 9eb76fac 45af8e51 + 30c81c46 a35ce411 +AES_CMAC dfa66747 de9ae630 30ca3261 1497c827 + +Example 4: len = 64 +M 6bc1bee2 2e409f96 e93d7e11 7393172a + ae2d8a57 1e03ac9c 9eb76fac 45af8e51 + 30c81c46 a35ce411 e5fbc119 1a0a52ef + f69f2445 df4f9b17 ad2b417b e66c3710 +AES_CMAC 51f0bebf 7e3b9d92 fc497417 79363cfe + +Example 4bis: len = 64 +M 6bc1bee2 2e409f96 e93d7e11 7393172a + ae2d8a57 1e03ac9c 9eb76fac 45af8e51 + 30c81c46 a35ce411 e5fbc119 1a0a52ef + f69f2445 df4f9b17 ad2b417b e66c3710 +AES_CMAC 51f0bebf 7e3b9d92 fc497417 79363cfe + +Example 4ter: len = 64 +M 6bc1bee2 2e409f96 e93d7e11 7393172a + ae2d8a57 1e03ac9c 9eb76fac 45af8e51 + 30c81c46 a35ce411 e5fbc119 1a0a52ef + f69f2445 df4f9b17 ad2b417b e66c3710 +AES_CMAC 51f0bebf 7e3b9d92 fc497417 79363cfe +-------------------------------------------------- diff --git a/regress/sys/crypto/hmac/Makefile b/regress/sys/crypto/hmac/Makefile new file mode 100644 index 00000000000..f31af2246e3 --- /dev/null +++ b/regress/sys/crypto/hmac/Makefile @@ -0,0 +1,27 @@ +# $OpenBSD: Makefile,v 1.1 2008/08/12 15:49:08 damien Exp $ + +DIR=${.CURDIR}/../../../../sys + +PROG= hmac_test +SRCS+= md5.c sha1.c sha2.c hmac.c hmac_test.c +CDIAGFLAGS= -Wall +CDIAGFLAGS+= -Werror +CDIAGFLAGS+= -Wpointer-arith +CDIAGFLAGS+= -Wno-uninitialized +CDIAGFLAGS+= -Wstrict-prototypes +CDIAGFLAGS+= -Wmissing-prototypes +CDIAGFLAGS+= -Wunused +CDIAGFLAGS+= -Wsign-compare +CDIAGFLAGS+= -Wbounded +CDIAGFLAGS+= -Wshadow + +REGRESS_TARGETS= run-regress-${PROG} + +CFLAGS+= -I${DIR} + +.PATH: ${DIR}/crypto/ + +run-regress-${PROG}: ${PROG} + ./${PROG} | diff - ${PROG}.txt + +.include <bsd.regress.mk> diff --git a/regress/sys/crypto/hmac/hmac_test.c b/regress/sys/crypto/hmac/hmac_test.c new file mode 100644 index 00000000000..83495aa8e83 --- /dev/null +++ b/regress/sys/crypto/hmac/hmac_test.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <crypto/md5.h> +#include <crypto/sha1.h> +#include <crypto/sha2.h> +#include <crypto/hmac.h> + +void +print_hex(unsigned char *buf, int len) +{ + int i; + + printf("digest = 0x"); + for (i = 0; i < len; i++) + printf("%02x", buf[i]); + printf("\n"); +} + +int +main(void) +{ + HMAC_MD5_CTX md5; + HMAC_SHA1_CTX sha1; + HMAC_SHA256_CTX sha256; + u_int8_t data[50], output[32]; + int i; + + HMAC_MD5_Init(&md5, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16); + HMAC_MD5_Update(&md5, "Hi There", 8); + HMAC_MD5_Final(output, &md5); + print_hex(output, MD5_DIGEST_LENGTH); + + HMAC_MD5_Init(&md5, "Jefe", 4); + HMAC_MD5_Update(&md5, "what do ya want for nothing?", 28); + HMAC_MD5_Final(output, &md5); + print_hex(output, MD5_DIGEST_LENGTH); + + HMAC_MD5_Init(&md5, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16); + memset(data, 0xDD, sizeof data); + HMAC_MD5_Update(&md5, data, sizeof data); + HMAC_MD5_Final(output, &md5); + print_hex(output, MD5_DIGEST_LENGTH); + + HMAC_SHA1_Init(&sha1, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16); + HMAC_SHA1_Update(&sha1, "Hi There", 8); + HMAC_SHA1_Final(output, &sha1); + print_hex(output, SHA1_DIGEST_LENGTH); + + HMAC_SHA1_Init(&sha1, "Jefe", 4); + HMAC_SHA1_Update(&sha1, "what do ya want for nothing?", 28); + HMAC_SHA1_Final(output, &sha1); + print_hex(output, SHA1_DIGEST_LENGTH); + + HMAC_SHA1_Init(&sha1, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16); + memset(data, 0xDD, sizeof data); + HMAC_SHA1_Update(&sha1, data, sizeof data); + HMAC_SHA1_Final(output, &sha1); + print_hex(output, SHA1_DIGEST_LENGTH); + + HMAC_SHA256_Init(&sha256, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16); + HMAC_SHA256_Update(&sha256, "Hi There", 8); + HMAC_SHA256_Final(output, &sha256); + print_hex(output, SHA256_DIGEST_LENGTH); + + HMAC_SHA256_Init(&sha256, "Jefe", 4); + HMAC_SHA256_Update(&sha256, "what do ya want for nothing?", 28); + HMAC_SHA256_Final(output, &sha256); + print_hex(output, SHA256_DIGEST_LENGTH); + + HMAC_SHA256_Init(&sha256, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16); + memset(data, 0xDD, sizeof data); + HMAC_SHA256_Update(&sha256, data, sizeof data); + HMAC_SHA256_Final(output, &sha256); + print_hex(output, SHA256_DIGEST_LENGTH); + + return 0; +} diff --git a/regress/sys/crypto/hmac/hmac_test.txt b/regress/sys/crypto/hmac/hmac_test.txt new file mode 100644 index 00000000000..d8375b615de --- /dev/null +++ b/regress/sys/crypto/hmac/hmac_test.txt @@ -0,0 +1,9 @@ +digest = 0x9294727a3638bb1c13f48ef8158bfc9d +digest = 0x750c783e6ab0b503eaa86e310a5db738 +digest = 0x56be34521d144c88dbb8c733f0e8b3f6 +digest = 0x675b0b3a1b4ddf4e124872da6c2f632bfed957e9 +digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79 +digest = 0xd730594d167e35d5956fd8003d0db3d3f46dc7bb +digest = 0x492ce020fe2534a5789dc3848806c78f4f6711397f08e7e7a12ca5a4483c8aa6 +digest = 0x5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 +digest = 0x7dda3cc169743a6484649f94f0eda0f9f2ff496a9733fb796ed5adb40a44c3c1 diff --git a/regress/sys/crypto/key_wrap/Makefile b/regress/sys/crypto/key_wrap/Makefile new file mode 100644 index 00000000000..2be26cc4c47 --- /dev/null +++ b/regress/sys/crypto/key_wrap/Makefile @@ -0,0 +1,27 @@ +# $OpenBSD: Makefile,v 1.1 2008/08/12 15:49:08 damien Exp $ + +DIR=${.CURDIR}/../../../../sys + +PROG= key_wrap_test +SRCS+= rijndael.c key_wrap.c key_wrap_test.c +CDIAGFLAGS= -Wall +CDIAGFLAGS+= -Werror +CDIAGFLAGS+= -Wpointer-arith +CDIAGFLAGS+= -Wno-uninitialized +CDIAGFLAGS+= -Wstrict-prototypes +CDIAGFLAGS+= -Wmissing-prototypes +CDIAGFLAGS+= -Wunused +CDIAGFLAGS+= -Wsign-compare +CDIAGFLAGS+= -Wbounded +CDIAGFLAGS+= -Wshadow + +REGRESS_TARGETS= run-regress-${PROG} + +CFLAGS+= -I${DIR} + +.PATH: ${DIR}/crypto/ + +run-regress-${PROG}: ${PROG} + ./${PROG} | diff - ${PROG}.txt + +.include <bsd.regress.mk> diff --git a/regress/sys/crypto/key_wrap/key_wrap_test.c b/regress/sys/crypto/key_wrap/key_wrap_test.c new file mode 100644 index 00000000000..dfd990fdb68 --- /dev/null +++ b/regress/sys/crypto/key_wrap/key_wrap_test.c @@ -0,0 +1,63 @@ +#include <stdio.h> +#include <crypto/rijndael.h> +#include <crypto/key_wrap.h> + +void +print_hex(const char *str, unsigned char *buf, int len) +{ + int i; + + printf("%s", str); + for (i = 0; i < len; i++) { + if ((i % 8) == 0) + printf(" "); + printf("%02X", buf[i]); + } + printf("\n"); +} + +void +ovbcopy(const void *src, void *dst, size_t len) +{ + /* userspace does not have ovbcopy: fake it */ + memmove(dst, src, len); +} + +void +do_test(u_int kek_len, u_int data_len) +{ + aes_key_wrap_ctx ctx; + u_int8_t kek[32], data[32]; + u_int8_t output[64]; + int i; + + for (i = 0; i < kek_len; i++) + kek[i] = i; + printf("Input:\n"); + print_hex("KEK:\n ", kek, kek_len); + for (i = 0; i < 16; i++) + data[i] = i * 16 + i; + for (; i < data_len; i++) + data[i] = i - 16; + print_hex("Key Data:\n ", data, data_len); + aes_key_wrap_set_key(&ctx, kek, kek_len); + aes_key_wrap(&ctx, data, data_len / 8, output); + print_hex("Ciphertext:\n ", output, data_len + 8); + aes_key_unwrap(&ctx, output, output, data_len / 8); + printf("Output:\n"); + print_hex("Key Data:\n ", output, data_len); + printf("====\n"); +} + +int +main(void) +{ + do_test(16, 16); + do_test(24, 16); + do_test(32, 16); + do_test(24, 24); + do_test(32, 24); + do_test(32, 32); + + return 0; +} diff --git a/regress/sys/crypto/key_wrap/key_wrap_test.txt b/regress/sys/crypto/key_wrap/key_wrap_test.txt new file mode 100644 index 00000000000..94c7a467359 --- /dev/null +++ b/regress/sys/crypto/key_wrap/key_wrap_test.txt @@ -0,0 +1,66 @@ +Input: +KEK: + 0001020304050607 08090A0B0C0D0E0F +Key Data: + 0011223344556677 8899AABBCCDDEEFF +Ciphertext: + 1FA68B0A8112B447 AEF34BD8FB5A7B82 9D3E862371D2CFE5 +Output: +Key Data: + 0011223344556677 8899AABBCCDDEEFF +==== +Input: +KEK: + 0001020304050607 08090A0B0C0D0E0F 1011121314151617 +Key Data: + 0011223344556677 8899AABBCCDDEEFF +Ciphertext: + 96778B25AE6CA435 F92B5B97C050AED2 468AB8A17AD84E5D +Output: +Key Data: + 0011223344556677 8899AABBCCDDEEFF +==== +Input: +KEK: + 0001020304050607 08090A0B0C0D0E0F 1011121314151617 18191A1B1C1D1E1F +Key Data: + 0011223344556677 8899AABBCCDDEEFF +Ciphertext: + 64E8C3F9CE0F5BA2 63E9777905818A2A 93C8191E7D6E8AE7 +Output: +Key Data: + 0011223344556677 8899AABBCCDDEEFF +==== +Input: +KEK: + 0001020304050607 08090A0B0C0D0E0F 1011121314151617 +Key Data: + 0011223344556677 8899AABBCCDDEEFF 0001020304050607 +Ciphertext: + 031D33264E15D332 68F24EC260743EDC E1C6C7DDEE725A93 6BA814915C6762D2 +Output: +Key Data: + 0011223344556677 8899AABBCCDDEEFF 0001020304050607 +==== +Input: +KEK: + 0001020304050607 08090A0B0C0D0E0F 1011121314151617 18191A1B1C1D1E1F +Key Data: + 0011223344556677 8899AABBCCDDEEFF 0001020304050607 +Ciphertext: + A8F9BC1612C68B3F F6E6F4FBE30E71E4 769C8B80A32CB895 8CD5D17D6B254DA1 +Output: +Key Data: + 0011223344556677 8899AABBCCDDEEFF 0001020304050607 +==== +Input: +KEK: + 0001020304050607 08090A0B0C0D0E0F 1011121314151617 18191A1B1C1D1E1F +Key Data: + 0011223344556677 8899AABBCCDDEEFF 0001020304050607 08090A0B0C0D0E0F +Ciphertext: + 28C9F404C4B810F4 CBCCB35CFB87F826 3F5786E2D80ED326 CBC7F0E71A99F43B FB988B9B7A02DD21 +Output: +Key Data: + 0011223344556677 8899AABBCCDDEEFF 0001020304050607 08090A0B0C0D0E0F +==== |