masqmail

annotate src/md5/hmactest.c @ 323:29de6a1c4538

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