hash_test.c 8.25 KB
Newer Older
Michael Graff's avatar
Michael Graff committed
1
/*
2
 * Copyright (C) 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/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Michael Graff's avatar
Michael Graff committed
10 11
 */

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

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

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

24 25
#include <pk11/site.h>

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

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

int
main(int argc, char **argv) {
43
	isc_hmacmd5_t hmacmd5;
44 45 46 47 48
	isc_hmacsha1_t hmacsha1;
	isc_hmacsha224_t hmacsha224;
	isc_hmacsha256_t hmacsha256;
	isc_hmacsha384_t hmacsha384;
	isc_hmacsha512_t hmacsha512;
49
	unsigned char digest[ISC_MAX_MD_SIZE];
Michael Graff's avatar
Michael Graff committed
50
	unsigned char buffer[1024];
51
	const char *s;
52
	unsigned char key[20];
Michael Graff's avatar
Michael Graff committed
53 54 55 56

	UNUSED(argc);
	UNUSED(argv);

57
	/*
58
	 * The 3 HMAC-MD5 examples from RFC2104
59 60 61 62
	 */
	s = "Hi There";
	memset(key, 0x0b, 16);
	isc_hmacmd5_init(&hmacmd5, key, 16);
63
	memmove(buffer, s, strlen(s));
64 65
	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
	isc_hmacmd5_sign(&hmacmd5, digest);
66
	print_digest(s, "hmacmd5", digest, 4);
67 68

	s = "what do ya want for nothing?";
Evan Hunt's avatar
Evan Hunt committed
69
	strlcpy((char *)key, "Jefe", sizeof(key));
70
	isc_hmacmd5_init(&hmacmd5, key, 4);
71
	memmove(buffer, s, strlen(s));
72 73
	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
	isc_hmacmd5_sign(&hmacmd5, digest);
74
	print_digest(s, "hmacmd5", digest, 4);
75 76 77 78 79 80 81 82

	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);
83
	memmove(buffer, s, strlen(s));
84 85
	isc_hmacmd5_update(&hmacmd5, buffer, strlen(s));
	isc_hmacmd5_sign(&hmacmd5, digest);
86 87 88 89 90 91 92 93
	print_digest(s, "hmacmd5", digest, 4);

	/*
	 * The 3 HMAC-SHA1 examples from RFC4634.
	 */
	s = "Hi There";
	memset(key, 0x0b, 20);
	isc_hmacsha1_init(&hmacsha1, key, 20);
94
	memmove(buffer, s, strlen(s));
95 96 97 98 99
	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?";
Evan Hunt's avatar
Evan Hunt committed
100
	strlcpy((char *)key, "Jefe", sizeof(key));
101
	isc_hmacsha1_init(&hmacsha1, key, 4);
102
	memmove(buffer, s, strlen(s));
103 104 105 106 107 108 109 110 111 112 113
	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);
114
	memmove(buffer, s, strlen(s));
115 116 117 118 119 120 121 122 123 124
	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);
125
	memmove(buffer, s, strlen(s));
126 127 128 129 130
	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?";
Evan Hunt's avatar
Evan Hunt committed
131
	strlcpy((char *)key, "Jefe", sizeof(key));
132
	isc_hmacsha224_init(&hmacsha224, key, 4);
133
	memmove(buffer, s, strlen(s));
134 135 136 137 138 139 140 141 142 143 144
	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);
145
	memmove(buffer, s, strlen(s));
146 147 148 149 150 151 152 153 154 155
	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);
156
	memmove(buffer, s, strlen(s));
157 158 159 160 161
	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?";
Evan Hunt's avatar
Evan Hunt committed
162
	strlcpy((char *)key, "Jefe", sizeof(key));
163
	isc_hmacsha256_init(&hmacsha256, key, 4);
164
	memmove(buffer, s, strlen(s));
165 166 167 168 169 170 171 172 173 174 175
	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);
176
	memmove(buffer, s, strlen(s));
177 178 179 180 181 182 183 184 185 186
	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);
187
	memmove(buffer, s, strlen(s));
188 189 190 191 192
	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?";
Evan Hunt's avatar
Evan Hunt committed
193
	strlcpy((char *)key, "Jefe", sizeof(key));
194
	isc_hmacsha384_init(&hmacsha384, key, 4);
195
	memmove(buffer, s, strlen(s));
196 197 198 199 200 201 202 203 204 205 206
	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);
207
	memmove(buffer, s, strlen(s));
208 209 210 211 212 213 214 215 216 217
	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);
218
	memmove(buffer, s, strlen(s));
219 220 221 222 223
	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?";
Evan Hunt's avatar
Evan Hunt committed
224
	strlcpy((char *)key, "Jefe", sizeof(key));
225
	isc_hmacsha512_init(&hmacsha512, key, 4);
226
	memmove(buffer, s, strlen(s));
227 228 229 230 231 232 233 234 235 236 237
	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);
238
	memmove(buffer, s, strlen(s));
239 240 241
	isc_hmacsha512_update(&hmacsha512, buffer, strlen(s));
	isc_hmacsha512_sign(&hmacsha512, digest, ISC_SHA512_DIGESTLENGTH);
	print_digest(s, "hmacsha512", digest, ISC_SHA512_DIGESTLENGTH/4);
242

Michael Graff's avatar
Michael Graff committed
243 244
	return (0);
}