1 /* $NetBSD: hash_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004-2007, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /* Id: hash_test.c,v 1.19 2007/06/19 23:46:59 tbox Exp */
21
22 /*! \file */
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <isc/hmacmd5.h>
29 #include <isc/hmacsha.h>
30 #include <isc/md5.h>
31 #include <isc/sha1.h>
32 #include <isc/util.h>
33 #include <isc/string.h>
34
35 static void
print_digest(const char * s,const char * hash,unsigned char * d,unsigned int words)36 print_digest(const char *s, const char *hash, unsigned char *d,
37 unsigned int words)
38 {
39 unsigned int i, j;
40
41 printf("hash (%s) %s:\n\t", hash, s);
42 for (i = 0; i < words; i++) {
43 printf(" ");
44 for (j = 0; j < 4; j++)
45 printf("%02x", d[i * 4 + j]);
46 }
47 printf("\n");
48 }
49
50 int
main(int argc,char ** argv)51 main(int argc, char **argv) {
52 isc_sha1_t sha1;
53 isc_sha224_t sha224;
54 isc_md5_t md5;
55 isc_hmacmd5_t hmacmd5;
56 isc_hmacsha1_t hmacsha1;
57 isc_hmacsha224_t hmacsha224;
58 isc_hmacsha256_t hmacsha256;
59 isc_hmacsha384_t hmacsha384;
60 isc_hmacsha512_t hmacsha512;
61 unsigned char digest[ISC_SHA512_DIGESTLENGTH];
62 unsigned char buffer[1024];
63 const char *s;
64 unsigned char key[20];
65
66 UNUSED(argc);
67 UNUSED(argv);
68
69 s = "abc";
70 isc_sha1_init(&sha1);
71 memmove(buffer, s, strlen(s));
72 isc_sha1_update(&sha1, buffer, strlen(s));
73 isc_sha1_final(&sha1, digest);
74 print_digest(s, "sha1", digest, ISC_SHA1_DIGESTLENGTH/4);
75
76 s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
77 isc_sha1_init(&sha1);
78 memmove(buffer, s, strlen(s));
79 isc_sha1_update(&sha1, buffer, strlen(s));
80 isc_sha1_final(&sha1, digest);
81 print_digest(s, "sha1", digest, ISC_SHA1_DIGESTLENGTH/4);
82
83 s = "abc";
84 isc_sha224_init(&sha224);
85 memmove(buffer, s, strlen(s));
86 isc_sha224_update(&sha224, buffer, strlen(s));
87 isc_sha224_final(digest, &sha224);
88 print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4);
89
90 s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
91 isc_sha224_init(&sha224);
92 memmove(buffer, s, strlen(s));
93 isc_sha224_update(&sha224, buffer, strlen(s));
94 isc_sha224_final(digest, &sha224);
95 print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4);
96
97 s = "abc";
98 isc_md5_init(&md5);
99 memmove(buffer, s, strlen(s));
100 isc_md5_update(&md5, buffer, strlen(s));
101 isc_md5_final(&md5, digest);
102 print_digest(s, "md5", digest, 4);
103
104 /*
105 * The 3 HMAC-MD5 examples from RFC2104
106 */
107 s = "Hi There";
108 memset(key, 0x0b, 16);
109 isc_hmacmd5_init(&hmacmd5, key, 16);
110 memmove(buffer, s, strlen(s));
111 isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
112 isc_hmacmd5_sign(&hmacmd5, digest);
113 print_digest(s, "hmacmd5", digest, 4);
114
115 s = "what do ya want for nothing?";
116 strcpy((char *)key, "Jefe");
117 isc_hmacmd5_init(&hmacmd5, key, 4);
118 memmove(buffer, s, strlen(s));
119 isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
120 isc_hmacmd5_sign(&hmacmd5, digest);
121 print_digest(s, "hmacmd5", digest, 4);
122
123 s = "\335\335\335\335\335\335\335\335\335\335"
124 "\335\335\335\335\335\335\335\335\335\335"
125 "\335\335\335\335\335\335\335\335\335\335"
126 "\335\335\335\335\335\335\335\335\335\335"
127 "\335\335\335\335\335\335\335\335\335\335";
128 memset(key, 0xaa, 16);
129 isc_hmacmd5_init(&hmacmd5, key, 16);
130 memmove(buffer, s, strlen(s));
131 isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
132 isc_hmacmd5_sign(&hmacmd5, digest);
133 print_digest(s, "hmacmd5", digest, 4);
134
135 /*
136 * The 3 HMAC-SHA1 examples from RFC4634.
137 */
138 s = "Hi There";
139 memset(key, 0x0b, 20);
140 isc_hmacsha1_init(&hmacsha1, key, 20);
141 memmove(buffer, s, strlen(s));
142 isc_hmacsha1_update(&hmacsha1, buffer, strlen(s));
143 isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH);
144 print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4);
145
146 s = "what do ya want for nothing?";
147 strcpy((char *)key, "Jefe");
148 isc_hmacsha1_init(&hmacsha1, key, 4);
149 memmove(buffer, s, strlen(s));
150 isc_hmacsha1_update(&hmacsha1, buffer, strlen(s));
151 isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH);
152 print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4);
153
154 s = "\335\335\335\335\335\335\335\335\335\335"
155 "\335\335\335\335\335\335\335\335\335\335"
156 "\335\335\335\335\335\335\335\335\335\335"
157 "\335\335\335\335\335\335\335\335\335\335"
158 "\335\335\335\335\335\335\335\335\335\335";
159 memset(key, 0xaa, 20);
160 isc_hmacsha1_init(&hmacsha1, key, 20);
161 memmove(buffer, s, strlen(s));
162 isc_hmacsha1_update(&hmacsha1, buffer, strlen(s));
163 isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH);
164 print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4);
165
166 /*
167 * The 3 HMAC-SHA224 examples from RFC4634.
168 */
169 s = "Hi There";
170 memset(key, 0x0b, 20);
171 isc_hmacsha224_init(&hmacsha224, key, 20);
172 memmove(buffer, s, strlen(s));
173 isc_hmacsha224_update(&hmacsha224, buffer, strlen(s));
174 isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH);
175 print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4);
176
177 s = "what do ya want for nothing?";
178 strcpy((char *)key, "Jefe");
179 isc_hmacsha224_init(&hmacsha224, key, 4);
180 memmove(buffer, s, strlen(s));
181 isc_hmacsha224_update(&hmacsha224, buffer, strlen(s));
182 isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH);
183 print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4);
184
185 s = "\335\335\335\335\335\335\335\335\335\335"
186 "\335\335\335\335\335\335\335\335\335\335"
187 "\335\335\335\335\335\335\335\335\335\335"
188 "\335\335\335\335\335\335\335\335\335\335"
189 "\335\335\335\335\335\335\335\335\335\335";
190 memset(key, 0xaa, 20);
191 isc_hmacsha224_init(&hmacsha224, key, 20);
192 memmove(buffer, s, strlen(s));
193 isc_hmacsha224_update(&hmacsha224, buffer, strlen(s));
194 isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH);
195 print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4);
196
197 /*
198 * The 3 HMAC-SHA256 examples from RFC4634.
199 */
200 s = "Hi There";
201 memset(key, 0x0b, 20);
202 isc_hmacsha256_init(&hmacsha256, key, 20);
203 memmove(buffer, s, strlen(s));
204 isc_hmacsha256_update(&hmacsha256, buffer, strlen(s));
205 isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH);
206 print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4);
207
208 s = "what do ya want for nothing?";
209 strcpy((char *)key, "Jefe");
210 isc_hmacsha256_init(&hmacsha256, key, 4);
211 memmove(buffer, s, strlen(s));
212 isc_hmacsha256_update(&hmacsha256, buffer, strlen(s));
213 isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH);
214 print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4);
215
216 s = "\335\335\335\335\335\335\335\335\335\335"
217 "\335\335\335\335\335\335\335\335\335\335"
218 "\335\335\335\335\335\335\335\335\335\335"
219 "\335\335\335\335\335\335\335\335\335\335"
220 "\335\335\335\335\335\335\335\335\335\335";
221 memset(key, 0xaa, 20);
222 isc_hmacsha256_init(&hmacsha256, key, 20);
223 memmove(buffer, s, strlen(s));
224 isc_hmacsha256_update(&hmacsha256, buffer, strlen(s));
225 isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH);
226 print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4);
227
228 /*
229 * The 3 HMAC-SHA384 examples from RFC4634.
230 */
231 s = "Hi There";
232 memset(key, 0x0b, 20);
233 isc_hmacsha384_init(&hmacsha384, key, 20);
234 memmove(buffer, s, strlen(s));
235 isc_hmacsha384_update(&hmacsha384, buffer, strlen(s));
236 isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH);
237 print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4);
238
239 s = "what do ya want for nothing?";
240 strcpy((char *)key, "Jefe");
241 isc_hmacsha384_init(&hmacsha384, key, 4);
242 memmove(buffer, s, strlen(s));
243 isc_hmacsha384_update(&hmacsha384, buffer, strlen(s));
244 isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH);
245 print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4);
246
247 s = "\335\335\335\335\335\335\335\335\335\335"
248 "\335\335\335\335\335\335\335\335\335\335"
249 "\335\335\335\335\335\335\335\335\335\335"
250 "\335\335\335\335\335\335\335\335\335\335"
251 "\335\335\335\335\335\335\335\335\335\335";
252 memset(key, 0xaa, 20);
253 isc_hmacsha384_init(&hmacsha384, key, 20);
254 memmove(buffer, s, strlen(s));
255 isc_hmacsha384_update(&hmacsha384, buffer, strlen(s));
256 isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH);
257 print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4);
258
259 /*
260 * The 3 HMAC-SHA512 examples from RFC4634.
261 */
262 s = "Hi There";
263 memset(key, 0x0b, 20);
264 isc_hmacsha512_init(&hmacsha512, key, 20);
265 memmove(buffer, s, strlen(s));
266 isc_hmacsha512_update(&hmacsha512, buffer, strlen(s));
267 isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH);
268 print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4);
269
270 s = "what do ya want for nothing?";
271 strcpy((char *)key, "Jefe");
272 isc_hmacsha512_init(&hmacsha512, key, 4);
273 memmove(buffer, s, strlen(s));
274 isc_hmacsha512_update(&hmacsha512, buffer, strlen(s));
275 isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH);
276 print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4);
277
278 s = "\335\335\335\335\335\335\335\335\335\335"
279 "\335\335\335\335\335\335\335\335\335\335"
280 "\335\335\335\335\335\335\335\335\335\335"
281 "\335\335\335\335\335\335\335\335\335\335"
282 "\335\335\335\335\335\335\335\335\335\335";
283 memset(key, 0xaa, 20);
284 isc_hmacsha512_init(&hmacsha512, key, 20);
285 memmove(buffer, s, strlen(s));
286 isc_hmacsha512_update(&hmacsha512, buffer, strlen(s));
287 isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH);
288 print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4);
289
290 return (0);
291 }
292