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.
29 #include <isc/assertions.h>
30 #include <isc/hmacsha.h>
31 #include <isc/platform.h>
34 #include <isc/string.h>
35 #include <isc/types.h>
38 #ifdef ISC_PLATFORM_OPENSSLHASH
41 isc_hmacsha1_init(isc_hmacsha1_t
*ctx
, const unsigned char *key
,
44 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha1());
48 isc_hmacsha1_invalidate(isc_hmacsha1_t
*ctx
) {
49 HMAC_CTX_cleanup(ctx
);
53 isc_hmacsha1_update(isc_hmacsha1_t
*ctx
, const unsigned char *buf
,
56 HMAC_Update(ctx
, buf
, (int) len
);
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
));
72 isc_hmacsha224_init(isc_hmacsha224_t
*ctx
, const unsigned char *key
,
75 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha224());
79 isc_hmacsha224_invalidate(isc_hmacsha224_t
*ctx
) {
80 HMAC_CTX_cleanup(ctx
);
84 isc_hmacsha224_update(isc_hmacsha224_t
*ctx
, const unsigned char *buf
,
87 HMAC_Update(ctx
, buf
, (int) len
);
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
));
103 isc_hmacsha256_init(isc_hmacsha256_t
*ctx
, const unsigned char *key
,
106 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha256());
110 isc_hmacsha256_invalidate(isc_hmacsha256_t
*ctx
) {
111 HMAC_CTX_cleanup(ctx
);
115 isc_hmacsha256_update(isc_hmacsha256_t
*ctx
, const unsigned char *buf
,
118 HMAC_Update(ctx
, buf
, (int) len
);
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
));
134 isc_hmacsha384_init(isc_hmacsha384_t
*ctx
, const unsigned char *key
,
137 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha384());
141 isc_hmacsha384_invalidate(isc_hmacsha384_t
*ctx
) {
142 HMAC_CTX_cleanup(ctx
);
146 isc_hmacsha384_update(isc_hmacsha384_t
*ctx
, const unsigned char *buf
,
149 HMAC_Update(ctx
, buf
, (int) len
);
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
));
165 isc_hmacsha512_init(isc_hmacsha512_t
*ctx
, const unsigned char *key
,
168 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha512());
172 isc_hmacsha512_invalidate(isc_hmacsha512_t
*ctx
) {
173 HMAC_CTX_cleanup(ctx
);
177 isc_hmacsha512_update(isc_hmacsha512_t
*ctx
, const unsigned char *buf
,
180 HMAC_Update(ctx
, buf
, (int) len
);
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
));
201 * Start HMAC-SHA1 process. Initialize an sha1 context and digest the key.
204 isc_hmacsha1_init(isc_hmacsha1_t
*ctx
, const unsigned char *key
,
207 unsigned char ipad
[ISC_SHA1_BLOCK_LENGTH
];
210 memset(ctx
->key
, 0, sizeof(ctx
->key
));
211 if (len
> sizeof(ctx
->key
)) {
213 isc_sha1_init(&sha1ctx
);
214 isc_sha1_update(&sha1ctx
, key
, len
);
215 isc_sha1_final(&sha1ctx
, ctx
->key
);
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
));
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
238 isc_hmacsha1_update(isc_hmacsha1_t
*ctx
, const unsigned char *buf
,
241 isc_sha1_update(&ctx
->sha1ctx
, buf
, len
);
245 * Compute signature - finalize SHA1 operation and reapply SHA1.
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
];
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.
273 isc_hmacsha224_init(isc_hmacsha224_t
*ctx
, const unsigned char *key
,
276 unsigned char ipad
[ISC_SHA224_BLOCK_LENGTH
];
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
);
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
));
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
306 isc_hmacsha224_update(isc_hmacsha224_t
*ctx
, const unsigned char *buf
,
309 isc_sha224_update(&ctx
->sha224ctx
, buf
, len
);
313 * Compute signature - finalize SHA224 operation and reapply SHA224.
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
];
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.
340 isc_hmacsha256_init(isc_hmacsha256_t
*ctx
, const unsigned char *key
,
343 unsigned char ipad
[ISC_SHA256_BLOCK_LENGTH
];
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
);
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
));
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
373 isc_hmacsha256_update(isc_hmacsha256_t
*ctx
, const unsigned char *buf
,
376 isc_sha256_update(&ctx
->sha256ctx
, buf
, len
);
380 * Compute signature - finalize SHA256 operation and reapply SHA256.
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
];
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.
407 isc_hmacsha384_init(isc_hmacsha384_t
*ctx
, const unsigned char *key
,
410 unsigned char ipad
[ISC_SHA384_BLOCK_LENGTH
];
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
);
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
));
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
440 isc_hmacsha384_update(isc_hmacsha384_t
*ctx
, const unsigned char *buf
,
443 isc_sha384_update(&ctx
->sha384ctx
, buf
, len
);
447 * Compute signature - finalize SHA384 operation and reapply SHA384.
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
];
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.
474 isc_hmacsha512_init(isc_hmacsha512_t
*ctx
, const unsigned char *key
,
477 unsigned char ipad
[ISC_SHA512_BLOCK_LENGTH
];
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
);
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
));
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
507 isc_hmacsha512_update(isc_hmacsha512_t
*ctx
, const unsigned char *buf
,
510 isc_sha512_update(&ctx
->sha512ctx
, buf
, len
);
514 * Compute signature - finalize SHA512 operation and reapply SHA512.
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
];
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.
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.
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.
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.
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.
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));