comparison src/md5/hmactest.c @ 165:ca99aa7f052a

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:24:51 +0200
parents 52c82d755215
children
comparison
equal deleted inserted replaced
164:b8c358b2e242 165:ca99aa7f052a
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <sys/time.h> 3 #include <sys/time.h>
4 #include <string.h> 4 #include <string.h>
5 #include "md5.h" 5 #include "md5.h"
6 #include "hmac_md5.h" 6 #include "hmac_md5.h"
7
8 /*
9 instead of pad0_copy(d, s, sz) use:
10 memset(d, 0, sz);
11 memcpy(d, s, strlen(s));
7 12
8 static void 13 static void
9 pad0_copy(char *d, char *s, int sz) 14 pad0_copy(char *d, char *s, int sz)
10 { 15 {
11 int i = 0; 16 int i = 0;
16 while (i <= sz) { 21 while (i <= sz) {
17 *(d++) = 0; 22 *(d++) = 0;
18 i++; 23 i++;
19 } 24 }
20 } 25 }
26 */
21 27
22 int 28 int
23 main() 29 main()
24 { 30 {
25 int i; 31 int i;
26 // unsigned char digest[16];
27 char digest[16]; 32 char digest[16];
28 char *msgid = "<1896.697170952@postoffice.reston.mci.net>"; 33 char *msgid = "<1896.697170952@postoffice.reston.mci.net>";
29 char secret[65]; 34 char secret[65];
30 35
36
31 hmac_md5("<48157.953508124@mail.class-c.net>", 34, "no!SpamAtAll", 12, digest); 37 hmac_md5("<48157.953508124@mail.class-c.net>", 34, "no!SpamAtAll", 12, digest);
32 for (i = 0; i < 16; i++) 38 for (i = 0; i < 16; i++)
33 printf("%x", (unsigned int) digest[i]); 39 printf("%.2x", 0xFF & (unsigned int) digest[i]);
34 printf("\n"); 40 printf("\n\n");
41
42
43 puts("---- The next two should be equal");
44
35 45
36 hmac_md5(msgid, strlen(msgid), "tanstaaftanstaaf", 16, digest); 46 hmac_md5(msgid, strlen(msgid), "tanstaaftanstaaf", 16, digest);
37 for (i = 0; i < 16; i++) 47 for (i = 0; i < 16; i++)
38 printf("%x", (unsigned int) digest[i]); 48 printf("%.2x", 0xFF & (unsigned int) digest[i]);
39 printf("\n"); 49 printf("\n\n");
40 50
41 pad0_copy(secret, "tanstaaftanstaaf", 64); 51
52 /* pad0_copy(secret, "tanstaaftanstaaf", 64); */
53 /* let's do it easier ... */
54 memset(secret, 0, sizeof(secret));
55 memcpy(secret, "tanstaaftanstaaf", 16);
42 hmac_md5(msgid, strlen(msgid), secret, 64, digest); 56 hmac_md5(msgid, strlen(msgid), secret, 64, digest);
43 for (i = 0; i < 16; i++) 57 for (i = 0; i < 16; i++)
44 printf("%x", (unsigned int) digest[i]); 58 printf("%.2x", 0xFF & (unsigned int) digest[i]);
45 printf("\n"); 59 printf("\n\n");
60
61
62 puts("---- Following are the test vectors from RFC 2104");
63
64
65 char* d01 = "Hi There";
66 char k01[16];
67 for (i=0; i<16; i++) {
68 k01[i] = 0x0b;
69 }
70 printf("9294727a3638bb1c13f48ef8158bfc9d (should be)\n");
71 hmac_md5(d01, strlen(d01), k01, sizeof(k01), digest);
72 for (i = 0; i < 16; i++) {
73 printf("%.2x", 0xFF & (unsigned int) digest[i]);
74 }
75 printf(" (was computed)\n\n");
76
77
78 char* d02 = "what do ya want for nothing?";
79 char* k02 = "Jefe";
80 printf("750c783e6ab0b503eaa86e310a5db738 (should be)\n");
81 hmac_md5(d02, strlen(d02), k02, strlen(k02), digest);
82 for (i = 0; i < 16; i++) {
83 printf("%.2x", 0xFF & (unsigned int) digest[i]);
84 }
85 printf(" (was computed)\n\n");
86
87
88 char d03[50];
89 for (i=0; i<sizeof(d03); i++) {
90 d03[i] = 0xdd;
91 }
92 char k03[16];
93 for (i=0; i<sizeof(k03); i++) {
94 k03[i] = 0xaa;
95 }
96 printf("56be34521d144c88dbb8c733f0e8b3f6 (should be)\n");
97 hmac_md5(d03, sizeof(d03), k03, sizeof(k03), digest);
98 for (i = 0; i < 16; i++) {
99 printf("%.2x", 0xFF & (unsigned int) digest[i]);
100 }
101 printf(" (was computed)\n\n");
46 102
47 exit(0); 103 exit(0);
48 } 104 }