masqmail

annotate src/md5/hmactest.c @ 380:35c5239ebcc1

Updated version number.
author markus schnalke <meillo@marmaro.de>
date Wed, 18 Jan 2012 09:31:45 +0100
parents 41958685480d
children
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@367 9 ** instead of pad0_copy(d, s, sz) use:
meillo@367 10 ** memset(d, 0, sz);
meillo@367 11 ** memcpy(d, s, strlen(s));
meillo@367 12 **
meillo@367 13 ** static void
meillo@367 14 ** pad0_copy(char *d, char *s, int sz)
meillo@367 15 ** {
meillo@367 16 ** int i = 0;
meillo@367 17 ** while (*s && (i < sz)) {
meillo@367 18 ** *(d++) = *(s++);
meillo@367 19 ** i++;
meillo@367 20 ** }
meillo@367 21 ** while (i <= sz) {
meillo@367 22 ** *(d++) = 0;
meillo@367 23 ** i++;
meillo@367 24 ** }
meillo@367 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@366 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@366 78 char *d02 = "what do ya want for nothing?";
meillo@366 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 }