Mercurial > masqmail-0.2
annotate src/md5/hmactest.c @ 179:ec3fe72a3e99
Fixed an important bug with folded headers!
g_strconcat() returns a *copy* of the string, but hdr->value still
pointed to the old header (which probably was a memory leak, too).
If the folded part had been quite small it was likely that the new
string was at the same position as the old one, thus making everything
go well. But if pretty long headers were folded several times it was
likely that the new string was allocated somewhere else in memory,
thus breaking things. In result mails to lots of recipients (folded
header) were frequently only sent to the ones in the first line. Sorry
for the inconvenience.
author | meillo@marmaro.de |
---|---|
date | Fri, 03 Jun 2011 09:52:17 +0200 |
parents | ca99aa7f052a |
children |
rev | line source |
---|---|
0 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <sys/time.h> | |
4 #include <string.h> | |
5 #include "md5.h" | |
6 #include "hmac_md5.h" | |
7 | |
165 | 8 /* |
9 instead of pad0_copy(d, s, sz) use: | |
10 memset(d, 0, sz); | |
11 memcpy(d, s, strlen(s)); | |
12 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
13 static void |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
14 pad0_copy(char *d, char *s, int sz) |
0 | 15 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
16 int i = 0; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
17 while (*s && (i < sz)) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
18 *(d++) = *(s++); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
19 i++; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
20 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
21 while (i <= sz) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
22 *(d++) = 0; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
23 i++; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
24 } |
0 | 25 } |
165 | 26 */ |
0 | 27 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
28 int |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
29 main() |
0 | 30 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
31 int i; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
32 char digest[16]; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
33 char *msgid = "<1896.697170952@postoffice.reston.mci.net>"; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
34 char secret[65]; |
0 | 35 |
165 | 36 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
37 hmac_md5("<48157.953508124@mail.class-c.net>", 34, "no!SpamAtAll", 12, digest); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
38 for (i = 0; i < 16; i++) |
165 | 39 printf("%.2x", 0xFF & (unsigned int) digest[i]); |
40 printf("\n\n"); | |
41 | |
42 | |
43 puts("---- The next two should be equal"); | |
44 | |
0 | 45 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 hmac_md5(msgid, strlen(msgid), "tanstaaftanstaaf", 16, digest); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
47 for (i = 0; i < 16; i++) |
165 | 48 printf("%.2x", 0xFF & (unsigned int) digest[i]); |
49 printf("\n\n"); | |
50 | |
0 | 51 |
165 | 52 /* pad0_copy(secret, "tanstaaftanstaaf", 64); */ |
53 /* let's do it easier ... */ | |
54 memset(secret, 0, sizeof(secret)); | |
55 memcpy(secret, "tanstaaftanstaaf", 16); | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
56 hmac_md5(msgid, strlen(msgid), secret, 64, digest); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
57 for (i = 0; i < 16; i++) |
165 | 58 printf("%.2x", 0xFF & (unsigned int) digest[i]); |
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"); | |
0 | 102 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
103 exit(0); |
0 | 104 } |