Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / hmacsha.c
blob6e55ec18161a7181b3d26cd38af7eb4ab9b4a495
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2005-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: hmacsha.c,v 1.10 2009/02/06 23:47:42 tbox Exp */
22 * This code implements the HMAC-SHA1, HMAC-SHA224, HMAC-SHA256, HMAC-SHA384
23 * and HMAC-SHA512 keyed hash algorithm described in RFC 2104 and
24 * draft-ietf-dnsext-tsig-sha-01.txt.
27 #include "config.h"
29 #include <isc/assertions.h>
30 #include <isc/hmacsha.h>
31 #include <isc/platform.h>
32 #include <isc/sha1.h>
33 #include <isc/sha2.h>
34 #include <isc/string.h>
35 #include <isc/types.h>
36 #include <isc/util.h>
38 #ifdef ISC_PLATFORM_OPENSSLHASH
40 void
41 isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
42 unsigned int len)
44 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha1());
47 void
48 isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
49 HMAC_CTX_cleanup(ctx);
52 void
53 isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
54 unsigned int len)
56 HMAC_Update(ctx, buf, (int) len);
59 void
60 isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
61 unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
63 REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
65 HMAC_Final(ctx, newdigest, NULL);
66 HMAC_CTX_cleanup(ctx);
67 memcpy(digest, newdigest, len);
68 memset(newdigest, 0, sizeof(newdigest));
71 void
72 isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
73 unsigned int len)
75 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha224());
78 void
79 isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
80 HMAC_CTX_cleanup(ctx);
83 void
84 isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
85 unsigned int len)
87 HMAC_Update(ctx, buf, (int) len);
90 void
91 isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
92 unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
94 REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
96 HMAC_Final(ctx, newdigest, NULL);
97 HMAC_CTX_cleanup(ctx);
98 memcpy(digest, newdigest, len);
99 memset(newdigest, 0, sizeof(newdigest));
102 void
103 isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
104 unsigned int len)
106 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha256());
109 void
110 isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
111 HMAC_CTX_cleanup(ctx);
114 void
115 isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
116 unsigned int len)
118 HMAC_Update(ctx, buf, (int) len);
121 void
122 isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
123 unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
125 REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
127 HMAC_Final(ctx, newdigest, NULL);
128 HMAC_CTX_cleanup(ctx);
129 memcpy(digest, newdigest, len);
130 memset(newdigest, 0, sizeof(newdigest));
133 void
134 isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
135 unsigned int len)
137 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha384());
140 void
141 isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
142 HMAC_CTX_cleanup(ctx);
145 void
146 isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
147 unsigned int len)
149 HMAC_Update(ctx, buf, (int) len);
152 void
153 isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
154 unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
156 REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
158 HMAC_Final(ctx, newdigest, NULL);
159 HMAC_CTX_cleanup(ctx);
160 memcpy(digest, newdigest, len);
161 memset(newdigest, 0, sizeof(newdigest));
164 void
165 isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
166 unsigned int len)
168 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha512());
171 void
172 isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
173 HMAC_CTX_cleanup(ctx);
176 void
177 isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
178 unsigned int len)
180 HMAC_Update(ctx, buf, (int) len);
183 void
184 isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
185 unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
187 REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
189 HMAC_Final(ctx, newdigest, NULL);
190 HMAC_CTX_cleanup(ctx);
191 memcpy(digest, newdigest, len);
192 memset(newdigest, 0, sizeof(newdigest));
195 #else
197 #define IPAD 0x36
198 #define OPAD 0x5C
201 * Start HMAC-SHA1 process. Initialize an sha1 context and digest the key.
203 void
204 isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
205 unsigned int len)
207 unsigned char ipad[ISC_SHA1_BLOCK_LENGTH];
208 unsigned int i;
210 memset(ctx->key, 0, sizeof(ctx->key));
211 if (len > sizeof(ctx->key)) {
212 isc_sha1_t sha1ctx;
213 isc_sha1_init(&sha1ctx);
214 isc_sha1_update(&sha1ctx, key, len);
215 isc_sha1_final(&sha1ctx, ctx->key);
216 } else
217 memcpy(ctx->key, key, len);
219 isc_sha1_init(&ctx->sha1ctx);
220 memset(ipad, IPAD, sizeof(ipad));
221 for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
222 ipad[i] ^= ctx->key[i];
223 isc_sha1_update(&ctx->sha1ctx, ipad, sizeof(ipad));
226 void
227 isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
228 isc_sha1_invalidate(&ctx->sha1ctx);
229 memset(ctx->key, 0, sizeof(ctx->key));
230 memset(ctx, 0, sizeof(ctx));
234 * Update context to reflect the concatenation of another buffer full
235 * of bytes.
237 void
238 isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
239 unsigned int len)
241 isc_sha1_update(&ctx->sha1ctx, buf, len);
245 * Compute signature - finalize SHA1 operation and reapply SHA1.
247 void
248 isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
249 unsigned char opad[ISC_SHA1_BLOCK_LENGTH];
250 unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
251 unsigned int i;
253 REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
254 isc_sha1_final(&ctx->sha1ctx, newdigest);
256 memset(opad, OPAD, sizeof(opad));
257 for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
258 opad[i] ^= ctx->key[i];
260 isc_sha1_init(&ctx->sha1ctx);
261 isc_sha1_update(&ctx->sha1ctx, opad, sizeof(opad));
262 isc_sha1_update(&ctx->sha1ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
263 isc_sha1_final(&ctx->sha1ctx, newdigest);
264 isc_hmacsha1_invalidate(ctx);
265 memcpy(digest, newdigest, len);
266 memset(newdigest, 0, sizeof(newdigest));
270 * Start HMAC-SHA224 process. Initialize an sha224 context and digest the key.
272 void
273 isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
274 unsigned int len)
276 unsigned char ipad[ISC_SHA224_BLOCK_LENGTH];
277 unsigned int i;
279 memset(ctx->key, 0, sizeof(ctx->key));
280 if (len > sizeof(ctx->key)) {
281 isc_sha224_t sha224ctx;
282 isc_sha224_init(&sha224ctx);
283 isc_sha224_update(&sha224ctx, key, len);
284 isc_sha224_final(ctx->key, &sha224ctx);
285 } else
286 memcpy(ctx->key, key, len);
288 isc_sha224_init(&ctx->sha224ctx);
289 memset(ipad, IPAD, sizeof(ipad));
290 for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
291 ipad[i] ^= ctx->key[i];
292 isc_sha224_update(&ctx->sha224ctx, ipad, sizeof(ipad));
295 void
296 isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
297 memset(ctx->key, 0, sizeof(ctx->key));
298 memset(ctx, 0, sizeof(ctx));
302 * Update context to reflect the concatenation of another buffer full
303 * of bytes.
305 void
306 isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
307 unsigned int len)
309 isc_sha224_update(&ctx->sha224ctx, buf, len);
313 * Compute signature - finalize SHA224 operation and reapply SHA224.
315 void
316 isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
317 unsigned char opad[ISC_SHA224_BLOCK_LENGTH];
318 unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
319 unsigned int i;
321 REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
322 isc_sha224_final(newdigest, &ctx->sha224ctx);
324 memset(opad, OPAD, sizeof(opad));
325 for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
326 opad[i] ^= ctx->key[i];
328 isc_sha224_init(&ctx->sha224ctx);
329 isc_sha224_update(&ctx->sha224ctx, opad, sizeof(opad));
330 isc_sha224_update(&ctx->sha224ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
331 isc_sha224_final(newdigest, &ctx->sha224ctx);
332 memcpy(digest, newdigest, len);
333 memset(newdigest, 0, sizeof(newdigest));
337 * Start HMAC-SHA256 process. Initialize an sha256 context and digest the key.
339 void
340 isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
341 unsigned int len)
343 unsigned char ipad[ISC_SHA256_BLOCK_LENGTH];
344 unsigned int i;
346 memset(ctx->key, 0, sizeof(ctx->key));
347 if (len > sizeof(ctx->key)) {
348 isc_sha256_t sha256ctx;
349 isc_sha256_init(&sha256ctx);
350 isc_sha256_update(&sha256ctx, key, len);
351 isc_sha256_final(ctx->key, &sha256ctx);
352 } else
353 memcpy(ctx->key, key, len);
355 isc_sha256_init(&ctx->sha256ctx);
356 memset(ipad, IPAD, sizeof(ipad));
357 for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
358 ipad[i] ^= ctx->key[i];
359 isc_sha256_update(&ctx->sha256ctx, ipad, sizeof(ipad));
362 void
363 isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
364 memset(ctx->key, 0, sizeof(ctx->key));
365 memset(ctx, 0, sizeof(ctx));
369 * Update context to reflect the concatenation of another buffer full
370 * of bytes.
372 void
373 isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
374 unsigned int len)
376 isc_sha256_update(&ctx->sha256ctx, buf, len);
380 * Compute signature - finalize SHA256 operation and reapply SHA256.
382 void
383 isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
384 unsigned char opad[ISC_SHA256_BLOCK_LENGTH];
385 unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
386 unsigned int i;
388 REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
389 isc_sha256_final(newdigest, &ctx->sha256ctx);
391 memset(opad, OPAD, sizeof(opad));
392 for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
393 opad[i] ^= ctx->key[i];
395 isc_sha256_init(&ctx->sha256ctx);
396 isc_sha256_update(&ctx->sha256ctx, opad, sizeof(opad));
397 isc_sha256_update(&ctx->sha256ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
398 isc_sha256_final(newdigest, &ctx->sha256ctx);
399 memcpy(digest, newdigest, len);
400 memset(newdigest, 0, sizeof(newdigest));
404 * Start HMAC-SHA384 process. Initialize an sha384 context and digest the key.
406 void
407 isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
408 unsigned int len)
410 unsigned char ipad[ISC_SHA384_BLOCK_LENGTH];
411 unsigned int i;
413 memset(ctx->key, 0, sizeof(ctx->key));
414 if (len > sizeof(ctx->key)) {
415 isc_sha384_t sha384ctx;
416 isc_sha384_init(&sha384ctx);
417 isc_sha384_update(&sha384ctx, key, len);
418 isc_sha384_final(ctx->key, &sha384ctx);
419 } else
420 memcpy(ctx->key, key, len);
422 isc_sha384_init(&ctx->sha384ctx);
423 memset(ipad, IPAD, sizeof(ipad));
424 for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
425 ipad[i] ^= ctx->key[i];
426 isc_sha384_update(&ctx->sha384ctx, ipad, sizeof(ipad));
429 void
430 isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
431 memset(ctx->key, 0, sizeof(ctx->key));
432 memset(ctx, 0, sizeof(ctx));
436 * Update context to reflect the concatenation of another buffer full
437 * of bytes.
439 void
440 isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
441 unsigned int len)
443 isc_sha384_update(&ctx->sha384ctx, buf, len);
447 * Compute signature - finalize SHA384 operation and reapply SHA384.
449 void
450 isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
451 unsigned char opad[ISC_SHA384_BLOCK_LENGTH];
452 unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
453 unsigned int i;
455 REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
456 isc_sha384_final(newdigest, &ctx->sha384ctx);
458 memset(opad, OPAD, sizeof(opad));
459 for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
460 opad[i] ^= ctx->key[i];
462 isc_sha384_init(&ctx->sha384ctx);
463 isc_sha384_update(&ctx->sha384ctx, opad, sizeof(opad));
464 isc_sha384_update(&ctx->sha384ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
465 isc_sha384_final(newdigest, &ctx->sha384ctx);
466 memcpy(digest, newdigest, len);
467 memset(newdigest, 0, sizeof(newdigest));
471 * Start HMAC-SHA512 process. Initialize an sha512 context and digest the key.
473 void
474 isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
475 unsigned int len)
477 unsigned char ipad[ISC_SHA512_BLOCK_LENGTH];
478 unsigned int i;
480 memset(ctx->key, 0, sizeof(ctx->key));
481 if (len > sizeof(ctx->key)) {
482 isc_sha512_t sha512ctx;
483 isc_sha512_init(&sha512ctx);
484 isc_sha512_update(&sha512ctx, key, len);
485 isc_sha512_final(ctx->key, &sha512ctx);
486 } else
487 memcpy(ctx->key, key, len);
489 isc_sha512_init(&ctx->sha512ctx);
490 memset(ipad, IPAD, sizeof(ipad));
491 for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
492 ipad[i] ^= ctx->key[i];
493 isc_sha512_update(&ctx->sha512ctx, ipad, sizeof(ipad));
496 void
497 isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
498 memset(ctx->key, 0, sizeof(ctx->key));
499 memset(ctx, 0, sizeof(ctx));
503 * Update context to reflect the concatenation of another buffer full
504 * of bytes.
506 void
507 isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
508 unsigned int len)
510 isc_sha512_update(&ctx->sha512ctx, buf, len);
514 * Compute signature - finalize SHA512 operation and reapply SHA512.
516 void
517 isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
518 unsigned char opad[ISC_SHA512_BLOCK_LENGTH];
519 unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
520 unsigned int i;
522 REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
523 isc_sha512_final(newdigest, &ctx->sha512ctx);
525 memset(opad, OPAD, sizeof(opad));
526 for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
527 opad[i] ^= ctx->key[i];
529 isc_sha512_init(&ctx->sha512ctx);
530 isc_sha512_update(&ctx->sha512ctx, opad, sizeof(opad));
531 isc_sha512_update(&ctx->sha512ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
532 isc_sha512_final(newdigest, &ctx->sha512ctx);
533 memcpy(digest, newdigest, len);
534 memset(newdigest, 0, sizeof(newdigest));
536 #endif /* !ISC_PLATFORM_OPENSSLHASH */
539 * Verify signature - finalize SHA1 operation and reapply SHA1, then
540 * compare to the supplied digest.
542 isc_boolean_t
543 isc_hmacsha1_verify(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
544 unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
546 REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
547 isc_hmacsha1_sign(ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
548 return (ISC_TF(memcmp(digest, newdigest, len) == 0));
552 * Verify signature - finalize SHA224 operation and reapply SHA224, then
553 * compare to the supplied digest.
555 isc_boolean_t
556 isc_hmacsha224_verify(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
557 unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
559 REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
560 isc_hmacsha224_sign(ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
561 return (ISC_TF(memcmp(digest, newdigest, len) == 0));
565 * Verify signature - finalize SHA256 operation and reapply SHA256, then
566 * compare to the supplied digest.
568 isc_boolean_t
569 isc_hmacsha256_verify(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
570 unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
572 REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
573 isc_hmacsha256_sign(ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
574 return (ISC_TF(memcmp(digest, newdigest, len) == 0));
578 * Verify signature - finalize SHA384 operation and reapply SHA384, then
579 * compare to the supplied digest.
581 isc_boolean_t
582 isc_hmacsha384_verify(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
583 unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
585 REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
586 isc_hmacsha384_sign(ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
587 return (ISC_TF(memcmp(digest, newdigest, len) == 0));
591 * Verify signature - finalize SHA512 operation and reapply SHA512, then
592 * compare to the supplied digest.
594 isc_boolean_t
595 isc_hmacsha512_verify(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
596 unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
598 REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
599 isc_hmacsha512_sign(ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
600 return (ISC_TF(memcmp(digest, newdigest, len) == 0));