File: hmactest.c

package info (click to toggle)
libcanlock 0.0.980924-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 228 kB
  • ctags: 102
  • sloc: ansic: 1,338; makefile: 63; sh: 34
file content (154 lines) | stat: -rw-r--r-- 4,337 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * hmac test program
 */

#include <stdio.h>
#include "sha1.h"
#include "hmac_sha1.h"
#include "md5.h"
#include "hmac_md5.h"

main()
{
    unsigned char *hmachash, *md5hash;
    unsigned char key1[] = "Jefe";
    unsigned char message1[] = "what do ya want for nothing?";
    unsigned char key2[20];
    unsigned char message2[] = "Hi There";
    unsigned char key3[80];
    unsigned char message3[] =
    "Test Using Larger Than Block-Size Key - Hash Key First";
    unsigned char key4[80];
    unsigned char message4[] =
    "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
    int i;
    unsigned char key5[] =
    {
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
	0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
	0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00
    };
    unsigned char message5[50];

    unsigned char j;


    for (i = 0; i < 20; i++)
	key2[i] = 0x0b;

    for (i = 0; i < 80; i++)
	key3[i] = 0xaa;

    for (i = 0; i < 80; i++)
	key4[i] = 0xaa;

    for (i = 0; i < 50; i++)
	message5[i] = (unsigned char) 0xcd;

    printf("Key: %s\n", key1);
    printf("Msg: %s\n", message1);
    hmachash = hmac_sha1(key1, strlen(key1), message1, strlen(message1));
    printf("Expected SHA Digest: %s\n",
	   "0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79");
    printf("  Actual SHA Digest: 0x");
    for (i = 0; i < SHA1_LENGTH; i++)
	printf("%02x", hmachash[i]);
    putchar('\n');


    md5hash = hmac_md5(key1, strlen(key1), message1, strlen(message1));
    printf("Expected MD5 Digest: %s\n",
	   "0x750c783e6ab0b503eaa86e310a5db738");
    printf("  Actual MD5 Digest: 0x");
    for (i = 0; i < MD5_LENGTH; i++)
	printf("%02x", md5hash[i]);
    putchar('\n');


/********/
    printf("\nKey: 0x0b, len 20 for SHA, 16 for MD5\n");
    printf("Msg: %s\n", message2);


    hmachash = hmac_sha1(key2, 20, message2, strlen(message2));
    printf("Expected SHA Digest: %s\n",
	   "0xb617318655057264e28bc0b6fb378c8ef146be00");
    printf("  Actual SHA Digest: 0x");
    for (i = 0; i < SHA1_LENGTH; i++)
	printf("%02x", hmachash[i]);
    putchar('\n');


    md5hash = hmac_md5(key2, 16, message2, strlen(message2));
    printf("Expected MD5 Digest: %s\n",
	   "0x9294727a3638bb1c13f48ef8158bfc9d");
    printf("  Actual MD5 Digest: 0x");
    for (i = 0; i < MD5_LENGTH; i++)
	printf("%02x", md5hash[i]);
    putchar('\n');

/********/
    printf("\nKey: 0xaa repeated 80 times\n");
    printf("Msg: %s\n", message3);

    hmachash = hmac_sha1(key3, 80, message3, strlen(message3));
    printf("Expected SHA Digest: %s\n",
	   "0xaa4ae5e15272d00e95705637ce8a3b55ed402112");
    printf("  Actual SHA Digest: 0x");
    for (i = 0; i < SHA1_LENGTH; i++)
	printf("%02x", hmachash[i]);
    putchar('\n');

    md5hash = hmac_md5(key3, 80, message3, strlen(message3));
    printf("Expected MD5 Digest: %s\n",
	   "0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd");
    printf("  Actual MD5 Digest: 0x");
    for (i = 0; i < MD5_LENGTH; i++)
	printf("%02x", md5hash[i]);
    putchar('\n');

/********/
    printf("\nKey: 0xaa repeated 80 times\n");
    printf("Msg: %s\n", message4);

    hmachash = hmac_sha1(key4, 80, message4, strlen(message4));
    printf("Expected SHA Digest: %s\n",
	   "0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91");
    printf("  Actual SHA Digest: 0x");
    for (i = 0; i < SHA1_LENGTH; i++)
	printf("%02x", hmachash[i]);
    putchar('\n');

    md5hash = hmac_md5(key4, 80, message4, strlen(message4));
    printf("Expected MD5 Digest: %s\n",
	   "0x6f630fad67cda0ee1fb1f562db3aa53e");
    printf("  Actual MD5 Digest: 0x");
    for (i = 0; i < MD5_LENGTH; i++)
	printf("%02x", md5hash[i]);
    putchar('\n');

/********/
    printf("\nKey: 0x");
    for (i = 0; i < 25; i++)
	printf("%02x", key5[i]);

    printf("\nMsg: 0xcd repeated 50 times\n");

    hmachash = hmac_sha1(key5, 25, message5, 50);
    printf("Expected SHA Digest: %s\n",
	   "0x4c9007f4026250c6bc8414f9bf50c86c2d7235da");
    printf("  Actual SHA Digest: 0x");
    for (i = 0; i < SHA1_LENGTH; i++)
	printf("%02x", hmachash[i]);
    putchar('\n');

    md5hash = hmac_md5(key5, 25, message5, 50);
    printf("Expected MD5 Digest: %s\n",
	   "0x697eaf0aca3a3aea3a75164746ffaa79");
    printf("  Actual MD5 Digest: 0x");
    for (i = 0; i < MD5_LENGTH; i++)
	printf("%02x", md5hash[i]);
    putchar('\n');


}