hash_test.c 9.42 KB
Newer Older
Michael Graff's avatar
Michael Graff committed
1
/*
2
 * Copyright (C) 2000, 2001, 2004-2007, 2014-2016  Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Michael Graff's avatar
Michael Graff committed
7 8
 */

Automatic Updater's avatar
Automatic Updater committed
9
/* $Id: hash_test.c,v 1.19 2007/06/19 23:46:59 tbox Exp $ */
David Lawrence's avatar
David Lawrence committed
10

11
/*! \file */
David Lawrence's avatar
David Lawrence committed
12 13 14
#include <config.h>

#include <stdio.h>
15
#include <string.h>
David Lawrence's avatar
David Lawrence committed
16

17
#include <isc/hmacmd5.h>
18
#include <isc/hmacsha.h>
19
#include <isc/md5.h>
Michael Graff's avatar
Michael Graff committed
20 21
#include <isc/sha1.h>
#include <isc/util.h>
22
#include <isc/print.h>
23
#include <isc/string.h>
Michael Graff's avatar
Michael Graff committed
24

25 26
#include <pk11/site.h>

Michael Graff's avatar
Michael Graff committed
27
static void
28
print_digest(const char *s, const char *hash, unsigned char *d,
29 30 31
	     unsigned int words)
{
	unsigned int i, j;
Michael Graff's avatar
Michael Graff committed
32

33
	printf("hash (%s) %s:\n\t", hash, s);
34
	for (i = 0; i < words; i++) {
Michael Graff's avatar
Michael Graff committed
35
		printf(" ");
36
		for (j = 0; j < 4; j++)
Michael Graff's avatar
Michael Graff committed
37 38 39 40 41 42 43 44
			printf("%02x", d[i * 4 + j]);
	}
	printf("\n");
}

int
main(int argc, char **argv) {
	isc_sha1_t sha1;
45
	isc_sha224_t sha224;
46
#ifndef PK11_MD5_DISABLE
47
	isc_md5_t md5;
48
	isc_hmacmd5_t hmacmd5;
49
#endif
50 51 52 53 54 55
	isc_hmacsha1_t hmacsha1;
	isc_hmacsha224_t hmacsha224;
	isc_hmacsha256_t hmacsha256;
	isc_hmacsha384_t hmacsha384;
	isc_hmacsha512_t hmacsha512;
	unsigned char digest[ISC_SHA512_DIGESTLENGTH];
Michael Graff's avatar
Michael Graff committed
56
	unsigned char buffer[1024];
57
	const char *s;
58
	unsigned char key[20];
Michael Graff's avatar
Michael Graff committed
59 60 61 62 63 64

	UNUSED(argc);
	UNUSED(argv);

	s = "abc";
	isc_sha1_init(&sha1);
65
	memmove(buffer, s, strlen(s));
Michael Graff's avatar
Michael Graff committed
66 67
	isc_sha1_update(&sha1, buffer, strlen(s));
	isc_sha1_final(&sha1, digest);
68
	print_digest(s, "sha1", digest, ISC_SHA1_DIGESTLENGTH/4);
Michael Graff's avatar
Michael Graff committed
69 70 71

	s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
	isc_sha1_init(&sha1);
72
	memmove(buffer, s, strlen(s));
Michael Graff's avatar
Michael Graff committed
73 74
	isc_sha1_update(&sha1, buffer, strlen(s));
	isc_sha1_final(&sha1, digest);
75 76 77 78
	print_digest(s, "sha1", digest, ISC_SHA1_DIGESTLENGTH/4);

	s = "abc";
	isc_sha224_init(&sha224);
79
	memmove(buffer, s, strlen(s));
80 81 82 83 84 85
	isc_sha224_update(&sha224, buffer, strlen(s));
	isc_sha224_final(digest, &sha224);
	print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4);

	s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
	isc_sha224_init(&sha224);
86
	memmove(buffer, s, strlen(s));
87 88 89
	isc_sha224_update(&sha224, buffer, strlen(s));
	isc_sha224_final(digest, &sha224);
	print_digest(s, "sha224", digest, ISC_SHA224_DIGESTLENGTH/4);
90

91
#ifndef PK11_MD5_DISABLE
92 93
	s = "abc";
	isc_md5_init(&md5);
94
	memmove(buffer, s, strlen(s));
95 96
	isc_md5_update(&md5, buffer, strlen(s));
	isc_md5_final(&md5, digest);
97
	print_digest(s, "md5", digest, 4);
Michael Graff's avatar
Michael Graff committed
98

99
	/*
100
	 * The 3 HMAC-MD5 examples from RFC2104
101 102 103 104
	 */
	s = "Hi There";
	memset(key, 0x0b, 16);
	isc_hmacmd5_init(&hmacmd5, key, 16);
105
	memmove(buffer, s, strlen(s));
106 107
	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
	isc_hmacmd5_sign(&hmacmd5, digest);
108
	print_digest(s, "hmacmd5", digest, 4);
109 110

	s = "what do ya want for nothing?";
111
	strcpy((char *)key, "Jefe");
112
	isc_hmacmd5_init(&hmacmd5, key, 4);
113
	memmove(buffer, s, strlen(s));
114 115
	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
	isc_hmacmd5_sign(&hmacmd5, digest);
116
	print_digest(s, "hmacmd5", digest, 4);
117 118 119 120 121 122 123 124

	s = "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335";
	memset(key, 0xaa, 16);
	isc_hmacmd5_init(&hmacmd5, key, 16);
125
	memmove(buffer, s, strlen(s));
126 127
	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
	isc_hmacmd5_sign(&hmacmd5, digest);
128
	print_digest(s, "hmacmd5", digest, 4);
129
#endif
130 131 132 133 134 135 136

	/*
	 * The 3 HMAC-SHA1 examples from RFC4634.
	 */
	s = "Hi There";
	memset(key, 0x0b, 20);
	isc_hmacsha1_init(&hmacsha1, key, 20);
137
	memmove(buffer, s, strlen(s));
138 139 140 141 142 143 144
	isc_hmacsha1_update(&hmacsha1, buffer, strlen(s));
	isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH);
	print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4);

	s = "what do ya want for nothing?";
	strcpy((char *)key, "Jefe");
	isc_hmacsha1_init(&hmacsha1, key, 4);
145
	memmove(buffer, s, strlen(s));
146 147 148 149 150 151 152 153 154 155 156
	isc_hmacsha1_update(&hmacsha1, buffer, strlen(s));
	isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH);
	print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4);

	s = "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335";
	memset(key, 0xaa, 20);
	isc_hmacsha1_init(&hmacsha1, key, 20);
157
	memmove(buffer, s, strlen(s));
158 159 160 161 162 163 164 165 166 167
	isc_hmacsha1_update(&hmacsha1, buffer, strlen(s));
	isc_hmacsha1_sign(&hmacsha1, digest, ISC_SHA1_DIGESTLENGTH);
	print_digest(s, "hmacsha1", digest, ISC_SHA1_DIGESTLENGTH/4);

	/*
	 * The 3 HMAC-SHA224 examples from RFC4634.
	 */
	s = "Hi There";
	memset(key, 0x0b, 20);
	isc_hmacsha224_init(&hmacsha224, key, 20);
168
	memmove(buffer, s, strlen(s));
169 170 171 172 173 174 175
	isc_hmacsha224_update(&hmacsha224, buffer, strlen(s));
	isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH);
	print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4);

	s = "what do ya want for nothing?";
	strcpy((char *)key, "Jefe");
	isc_hmacsha224_init(&hmacsha224, key, 4);
176
	memmove(buffer, s, strlen(s));
177 178 179 180 181 182 183 184 185 186 187
	isc_hmacsha224_update(&hmacsha224, buffer, strlen(s));
	isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH);
	print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4);

	s = "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335";
	memset(key, 0xaa, 20);
	isc_hmacsha224_init(&hmacsha224, key, 20);
188
	memmove(buffer, s, strlen(s));
189 190 191 192 193 194 195 196 197 198
	isc_hmacsha224_update(&hmacsha224, buffer, strlen(s));
	isc_hmacsha224_sign(&hmacsha224, digest, ISC_SHA224_DIGESTLENGTH);
	print_digest(s, "hmacsha224", digest, ISC_SHA224_DIGESTLENGTH/4);

	/*
	 * The 3 HMAC-SHA256 examples from RFC4634.
	 */
	s = "Hi There";
	memset(key, 0x0b, 20);
	isc_hmacsha256_init(&hmacsha256, key, 20);
199
	memmove(buffer, s, strlen(s));
200 201 202 203 204 205 206
	isc_hmacsha256_update(&hmacsha256, buffer, strlen(s));
	isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH);
	print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4);

	s = "what do ya want for nothing?";
	strcpy((char *)key, "Jefe");
	isc_hmacsha256_init(&hmacsha256, key, 4);
207
	memmove(buffer, s, strlen(s));
208 209 210 211 212 213 214 215 216 217 218
	isc_hmacsha256_update(&hmacsha256, buffer, strlen(s));
	isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH);
	print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4);

	s = "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335";
	memset(key, 0xaa, 20);
	isc_hmacsha256_init(&hmacsha256, key, 20);
219
	memmove(buffer, s, strlen(s));
220 221 222 223 224 225 226 227 228 229
	isc_hmacsha256_update(&hmacsha256, buffer, strlen(s));
	isc_hmacsha256_sign(&hmacsha256, digest, ISC_SHA256_DIGESTLENGTH);
	print_digest(s, "hmacsha256", digest, ISC_SHA256_DIGESTLENGTH/4);

	/*
	 * The 3 HMAC-SHA384 examples from RFC4634.
	 */
	s = "Hi There";
	memset(key, 0x0b, 20);
	isc_hmacsha384_init(&hmacsha384, key, 20);
230
	memmove(buffer, s, strlen(s));
231 232 233 234 235 236 237
	isc_hmacsha384_update(&hmacsha384, buffer, strlen(s));
	isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH);
	print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4);

	s = "what do ya want for nothing?";
	strcpy((char *)key, "Jefe");
	isc_hmacsha384_init(&hmacsha384, key, 4);
238
	memmove(buffer, s, strlen(s));
239 240 241 242 243 244 245 246 247 248 249
	isc_hmacsha384_update(&hmacsha384, buffer, strlen(s));
	isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH);
	print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4);

	s = "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335";
	memset(key, 0xaa, 20);
	isc_hmacsha384_init(&hmacsha384, key, 20);
250
	memmove(buffer, s, strlen(s));
251 252 253 254 255 256 257 258 259 260
	isc_hmacsha384_update(&hmacsha384, buffer, strlen(s));
	isc_hmacsha384_sign(&hmacsha384, digest, ISC_SHA384_DIGESTLENGTH);
	print_digest(s, "hmacsha384", digest, ISC_SHA384_DIGESTLENGTH/4);

	/*
	 * The 3 HMAC-SHA512 examples from RFC4634.
	 */
	s = "Hi There";
	memset(key, 0x0b, 20);
	isc_hmacsha512_init(&hmacsha512, key, 20);
261
	memmove(buffer, s, strlen(s));
262 263 264 265 266 267 268
	isc_hmacsha512_update(&hmacsha512, buffer, strlen(s));
	isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH);
	print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4);

	s = "what do ya want for nothing?";
	strcpy((char *)key, "Jefe");
	isc_hmacsha512_init(&hmacsha512, key, 4);
269
	memmove(buffer, s, strlen(s));
270 271 272 273 274 275 276 277 278 279 280
	isc_hmacsha512_update(&hmacsha512, buffer, strlen(s));
	isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH);
	print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4);

	s = "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335"
	    "\335\335\335\335\335\335\335\335\335\335";
	memset(key, 0xaa, 20);
	isc_hmacsha512_init(&hmacsha512, key, 20);
281
	memmove(buffer, s, strlen(s));
282 283 284
	isc_hmacsha512_update(&hmacsha512, buffer, strlen(s));
	isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH);
	print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4);
285

Michael Graff's avatar
Michael Graff committed
286 287
	return (0);
}