masqmail
diff src/md5/hmactest.c @ 212:38f86ae6171b
improved hmactest
added the test vectors that are included in the RFC
plus improved the output
author | meillo@marmaro.de |
---|---|
date | Sun, 18 Jul 2010 22:25:21 +0200 |
parents | 10da50168dab |
children | 41958685480d |
line diff
1.1 --- a/src/md5/hmactest.c Sun Jul 18 22:20:36 2010 +0200 1.2 +++ b/src/md5/hmactest.c Sun Jul 18 22:25:21 2010 +0200 1.3 @@ -5,6 +5,11 @@ 1.4 #include "md5.h" 1.5 #include "hmac_md5.h" 1.6 1.7 +/* 1.8 +instead of pad0_copy(d, s, sz) use: 1.9 + memset(d, 0, sz); 1.10 + memcpy(d, s, strlen(s)); 1.11 + 1.12 static void 1.13 pad0_copy(char *d, char *s, int sz) 1.14 { 1.15 @@ -18,31 +23,82 @@ 1.16 i++; 1.17 } 1.18 } 1.19 +*/ 1.20 1.21 int 1.22 main() 1.23 { 1.24 int i; 1.25 - /* unsigned char digest[16]; */ 1.26 char digest[16]; 1.27 char *msgid = "<1896.697170952@postoffice.reston.mci.net>"; 1.28 char secret[65]; 1.29 1.30 + 1.31 hmac_md5("<48157.953508124@mail.class-c.net>", 34, "no!SpamAtAll", 12, digest); 1.32 for (i = 0; i < 16; i++) 1.33 - printf("%x", (unsigned int) digest[i]); 1.34 - printf("\n"); 1.35 + printf("%.2x", 0xFF & (unsigned int) digest[i]); 1.36 + printf("\n\n"); 1.37 + 1.38 + 1.39 + puts("---- The next two should be equal"); 1.40 + 1.41 1.42 hmac_md5(msgid, strlen(msgid), "tanstaaftanstaaf", 16, digest); 1.43 for (i = 0; i < 16; i++) 1.44 - printf("%x", (unsigned int) digest[i]); 1.45 - printf("\n"); 1.46 + printf("%.2x", 0xFF & (unsigned int) digest[i]); 1.47 + printf("\n\n"); 1.48 1.49 - pad0_copy(secret, "tanstaaftanstaaf", 64); 1.50 + 1.51 + /* pad0_copy(secret, "tanstaaftanstaaf", 64); */ 1.52 + /* let's do it easier ... */ 1.53 + memset(secret, 0, sizeof(secret)); 1.54 + memcpy(secret, "tanstaaftanstaaf", 16); 1.55 hmac_md5(msgid, strlen(msgid), secret, 64, digest); 1.56 for (i = 0; i < 16; i++) 1.57 - printf("%x", (unsigned int) digest[i]); 1.58 - printf("\n"); 1.59 + printf("%.2x", 0xFF & (unsigned int) digest[i]); 1.60 + printf("\n\n"); 1.61 + 1.62 + 1.63 + puts("---- Following are the test vectors from RFC 2104"); 1.64 + 1.65 + 1.66 + char* d01 = "Hi There"; 1.67 + char k01[16]; 1.68 + for (i=0; i<16; i++) { 1.69 + k01[i] = 0x0b; 1.70 + } 1.71 + printf("9294727a3638bb1c13f48ef8158bfc9d (should be)\n"); 1.72 + hmac_md5(d01, strlen(d01), k01, sizeof(k01), digest); 1.73 + for (i = 0; i < 16; i++) { 1.74 + printf("%.2x", 0xFF & (unsigned int) digest[i]); 1.75 + } 1.76 + printf(" (was computed)\n\n"); 1.77 + 1.78 + 1.79 + char* d02 = "what do ya want for nothing?"; 1.80 + char* k02 = "Jefe"; 1.81 + printf("750c783e6ab0b503eaa86e310a5db738 (should be)\n"); 1.82 + hmac_md5(d02, strlen(d02), k02, strlen(k02), digest); 1.83 + for (i = 0; i < 16; i++) { 1.84 + printf("%.2x", 0xFF & (unsigned int) digest[i]); 1.85 + } 1.86 + printf(" (was computed)\n\n"); 1.87 + 1.88 + 1.89 + char d03[50]; 1.90 + for (i=0; i<sizeof(d03); i++) { 1.91 + d03[i] = 0xdd; 1.92 + } 1.93 + char k03[16]; 1.94 + for (i=0; i<sizeof(k03); i++) { 1.95 + k03[i] = 0xaa; 1.96 + } 1.97 + printf("56be34521d144c88dbb8c733f0e8b3f6 (should be)\n"); 1.98 + hmac_md5(d03, sizeof(d03), k03, sizeof(k03), digest); 1.99 + for (i = 0; i < 16; i++) { 1.100 + printf("%.2x", 0xFF & (unsigned int) digest[i]); 1.101 + } 1.102 + printf(" (was computed)\n\n"); 1.103 1.104 exit(0); 1.105 }