hmac-test.c 5.83 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "testutils.h"
#include "hmac.h"

int
test_main(void)
{
  struct hmac_md5_ctx md5;
  struct hmac_sha1_ctx sha1;
  struct hmac_sha256_ctx sha256;

  /* sha256's digests are longest */
  uint8_t digest[SHA256_DIGEST_SIZE];

  memset(digest, 0, sizeof(digest));
  
  /* Test vectors for md5, from RFC-2202 */
17
18

  /* md5 - 1 */
Niels Möller's avatar
Niels Möller committed
19
20
21
22
23
24
25
26
  hmac_md5_set_key(&md5, HL("0b0b0b0b0b0b0b0b 0b0b0b0b0b0b0b0b"));
  hmac_md5_update(&md5, LDATA("Hi There"));
  hmac_md5_digest(&md5, MD5_DIGEST_SIZE, digest);

  if (!MEMEQ(MD5_DIGEST_SIZE, digest,
	     H("9294727a3638bb1c 13f48ef8158bfc9d")))
    FAIL();

27
  /* md5 - 2 */
Niels Möller's avatar
Niels Möller committed
28
29
30
31
32
33
34
35
  hmac_md5_set_key(&md5, LDATA("Jefe"));
  hmac_md5_update(&md5, LDATA("what do ya want for nothing?"));
  hmac_md5_digest(&md5, MD5_DIGEST_SIZE, digest);

  if (!MEMEQ(MD5_DIGEST_SIZE, digest,
	     H("750c783e6ab0b503 eaa86e310a5db738")))
    FAIL();

36
  /* md5 - 3 */
Niels Möller's avatar
Niels Möller committed
37
38
39
40
41
42
43
44
  hmac_md5_set_key(&md5, HL("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"));
  hmac_md5_update(&md5, HL("dddddddddddddddd dddddddddddddddd"
			   "dddddddddddddddd dddddddddddddddd"
			   "dddddddddddddddd dddddddddddddddd"
			   "dddd"));
  hmac_md5_digest(&md5, MD5_DIGEST_SIZE, digest);

  if (!MEMEQ(MD5_DIGEST_SIZE, digest,
45
	     H("56be34521d144c88 dbb8c733f0e8b3f6")))
Niels Möller's avatar
Niels Möller committed
46
47
    FAIL();
  
48
  /* md5 - 4 */
Niels Möller's avatar
Niels Möller committed
49
50
51
52
53
54
55
56
57
  hmac_md5_set_key(&md5, HL("0102030405060708 090a0b0c0d0e0f10" 
			    "1112131415161718 19"));
  hmac_md5_update(&md5, HL("cdcdcdcdcdcdcdcd cdcdcdcdcdcdcdcd"
			   "cdcdcdcdcdcdcdcd cdcdcdcdcdcdcdcd"
			   "cdcdcdcdcdcdcdcd cdcdcdcdcdcdcdcd"
			   "cdcd"));
  hmac_md5_digest(&md5, MD5_DIGEST_SIZE, digest);

  if (!MEMEQ(MD5_DIGEST_SIZE, digest,
58
	     H("697eaf0aca3a3aea 3a75164746ffaa79")))
Niels Möller's avatar
Niels Möller committed
59
60
    FAIL();

61
  /* md5 - 5 */
Niels Möller's avatar
Niels Möller committed
62
63
64
65
66
67
  memset(digest, 0, MD5_DIGEST_SIZE);
  hmac_md5_set_key(&md5, HL("0c0c0c0c0c0c0c0c 0c0c0c0c0c0c0c0c" ));
  hmac_md5_update(&md5, LDATA("Test With Truncation"));
  hmac_md5_digest(&md5, 12, digest);

  if (!MEMEQ(MD5_DIGEST_SIZE, digest,
68
	     H("56461ef2342edc00 f9bab99500000000")))
Niels Möller's avatar
Niels Möller committed
69
70
    FAIL();

71
  /* md5 - 6 */
Niels Möller's avatar
Niels Möller committed
72
73
74
75
76
77
78
79
80
81
  hmac_md5_set_key(&md5, HL("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"));
  hmac_md5_update(&md5,
		  LDATA("Test Using Larger Than Block-Size Key - Hash Key First"));
  hmac_md5_digest(&md5, MD5_DIGEST_SIZE, digest);

  if (!MEMEQ(MD5_DIGEST_SIZE, digest,
82
	     H("6b1ab7fe4bd7bf8f 0b62e6ce61b9d0cd")))
Niels Möller's avatar
Niels Möller committed
83
84
    FAIL();

85
  /* md5 - 7 */
Niels Möller's avatar
Niels Möller committed
86
87
88
89
90
91
92
93
94
95
96
  hmac_md5_set_key(&md5, HL("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			    "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"));
  hmac_md5_update(&md5,
		  LDATA("Test Using Larger Than Block-Size Key and Larger "
			"Than One Block-Size Data"));
  hmac_md5_digest(&md5, MD5_DIGEST_SIZE, digest);

  if (!MEMEQ(MD5_DIGEST_SIZE, digest,
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
	     H("6f630fad67cda0ee 1fb1f562db3aa53e")))
    FAIL();

 
  /* Test vectors for sha1, from RFC-2202 */

  /* sha1 - 1 */
  hmac_sha1_set_key(&sha1, HL("0b0b0b0b0b0b0b0b 0b0b0b0b0b0b0b0b 0b0b0b0b"));
  hmac_sha1_update(&sha1, LDATA("Hi There"));
  hmac_sha1_digest(&sha1, SHA1_DIGEST_SIZE, digest);

  if (!MEMEQ(SHA1_DIGEST_SIZE, digest,
	     H("b617318655057264 e28bc0b6fb378c8e f146be00")))
    FAIL();

  /* sha1 - 2 */
  hmac_sha1_set_key(&sha1, LDATA("Jefe"));
  hmac_sha1_update(&sha1, LDATA("what do ya want for nothing?"));
  hmac_sha1_digest(&sha1, SHA1_DIGEST_SIZE, digest);

  if (!MEMEQ(SHA1_DIGEST_SIZE, digest,
	     H("effcdf6ae5eb2fa2 d27416d5f184df9c 259a7c79")))
    FAIL();

  /* sha1 - 3 */
  hmac_sha1_set_key(&sha1, HL("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaa"));
  hmac_sha1_update(&sha1, HL("dddddddddddddddd dddddddddddddddd"
			     "dddddddddddddddd dddddddddddddddd"
			     "dddddddddddddddd dddddddddddddddd"
			     "dddd"));
  hmac_sha1_digest(&sha1, SHA1_DIGEST_SIZE, digest);

  if (!MEMEQ(SHA1_DIGEST_SIZE, digest,
	     H("125d7342b9ac11cd 91a39af48aa17b4f 63f175d3")))
    FAIL();
  
  /* sha1 - 4 */
  hmac_sha1_set_key(&sha1, HL("0102030405060708 090a0b0c0d0e0f10" 
			      "1112131415161718 19"));
  hmac_sha1_update(&sha1, HL("cdcdcdcdcdcdcdcd cdcdcdcdcdcdcdcd"
			     "cdcdcdcdcdcdcdcd cdcdcdcdcdcdcdcd"
			     "cdcdcdcdcdcdcdcd cdcdcdcdcdcdcdcd"
			     "cdcd"));
  hmac_sha1_digest(&sha1, SHA1_DIGEST_SIZE, digest);

  if (!MEMEQ(SHA1_DIGEST_SIZE, digest,
	     H("4c9007f4026250c6 bc8414f9bf50c86c 2d7235da")))
    FAIL();

  /* sha1 - 5 */
  memset(digest, 0, SHA1_DIGEST_SIZE);
  hmac_sha1_set_key(&sha1, HL("0c0c0c0c0c0c0c0c 0c0c0c0c0c0c0c0c 0c0c0c0c"));
  hmac_sha1_update(&sha1, LDATA("Test With Truncation"));
  hmac_sha1_digest(&sha1, 12, digest);

  if (!MEMEQ(SHA1_DIGEST_SIZE, digest,
	     H("4c1a03424b55e07f e7f27be100000000 00000000")))
    FAIL();

  /* sha1 - 6 */
  hmac_sha1_set_key(&sha1, HL("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"));
  hmac_sha1_update(&sha1,
		   LDATA("Test Using Larger Than Block-Size Key - Hash Key First"));
  hmac_sha1_digest(&sha1, SHA1_DIGEST_SIZE, digest);

  if (!MEMEQ(SHA1_DIGEST_SIZE, digest,
	     H("aa4ae5e15272d00e 95705637ce8a3b55 ed402112")))
    FAIL();

  /* sha1 - 7 */
  hmac_sha1_set_key(&sha1, HL("aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"
			      "aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa"));
  hmac_sha1_update(&sha1,
		  LDATA("Test Using Larger Than Block-Size Key and Larger "
			"Than One Block-Size Data"));
  hmac_sha1_digest(&sha1, SHA1_DIGEST_SIZE, digest);

  if (!MEMEQ(SHA1_DIGEST_SIZE, digest,
	     H("e8e99d0f45237d78 6d6bbaa7965c7808 bbff1a91")))
Niels Möller's avatar
Niels Möller committed
183
184
185
186
    FAIL();

  SUCCESS();
}