]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/isc/hmacsha.c
Update BIND to 9.9.8
[FreeBSD/stable/9.git] / contrib / bind9 / lib / isc / hmacsha.c
1 /*
2  * Copyright (C) 2005-2007, 2009, 2011-2015  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id$ */
18
19 /*
20  * This code implements the HMAC-SHA1, HMAC-SHA224, HMAC-SHA256, HMAC-SHA384
21  * and HMAC-SHA512 keyed hash algorithm described in RFC 2104 and
22  * draft-ietf-dnsext-tsig-sha-01.txt.
23  */
24
25 #include "config.h"
26
27 #include <isc/assertions.h>
28 #include <isc/hmacsha.h>
29 #include <isc/platform.h>
30 #include <isc/safe.h>
31 #include <isc/sha1.h>
32 #include <isc/sha2.h>
33 #include <isc/string.h>
34 #include <isc/types.h>
35 #include <isc/util.h>
36
37 #ifdef ISC_PLATFORM_OPENSSLHASH
38
39 void
40 isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
41                   unsigned int len)
42 {
43 #ifdef HMAC_RETURN_INT
44         RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
45                                 (int) len, EVP_sha1()) == 1);
46 #else
47         HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha1());
48 #endif
49 }
50
51 void
52 isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
53         HMAC_CTX_cleanup(ctx);
54 }
55
56 void
57 isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
58                    unsigned int len)
59 {
60 #ifdef HMAC_RETURN_INT
61         RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
62 #else
63         HMAC_Update(ctx, buf, (int) len);
64 #endif
65 }
66
67 void
68 isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
69         unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
70
71         REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
72
73 #ifdef HMAC_RETURN_INT
74         RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
75 #else
76         HMAC_Final(ctx, newdigest, NULL);
77 #endif
78         HMAC_CTX_cleanup(ctx);
79         memmove(digest, newdigest, len);
80         memset(newdigest, 0, sizeof(newdigest));
81 }
82
83 void
84 isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
85                     unsigned int len)
86 {
87 #ifdef HMAC_RETURN_INT
88         RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
89                                 (int) len, EVP_sha224()) == 1);
90 #else
91         HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha224());
92 #endif
93 }
94
95 void
96 isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
97         HMAC_CTX_cleanup(ctx);
98 }
99
100 void
101 isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
102                    unsigned int len)
103 {
104 #ifdef HMAC_RETURN_INT
105         RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
106 #else
107         HMAC_Update(ctx, buf, (int) len);
108 #endif
109 }
110
111 void
112 isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
113         unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
114
115         REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
116
117 #ifdef HMAC_RETURN_INT
118         RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
119 #else
120         HMAC_Final(ctx, newdigest, NULL);
121 #endif
122         HMAC_CTX_cleanup(ctx);
123         memmove(digest, newdigest, len);
124         memset(newdigest, 0, sizeof(newdigest));
125 }
126
127 void
128 isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
129                     unsigned int len)
130 {
131 #ifdef HMAC_RETURN_INT
132         RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
133                                 (int) len, EVP_sha256()) == 1);
134 #else
135         HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha256());
136 #endif
137 }
138
139 void
140 isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
141         HMAC_CTX_cleanup(ctx);
142 }
143
144 void
145 isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
146                    unsigned int len)
147 {
148 #ifdef HMAC_RETURN_INT
149         RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
150 #else
151         HMAC_Update(ctx, buf, (int) len);
152 #endif
153 }
154
155 void
156 isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
157         unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
158
159         REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
160
161 #ifdef HMAC_RETURN_INT
162         RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
163 #else
164         HMAC_Final(ctx, newdigest, NULL);
165 #endif
166         HMAC_CTX_cleanup(ctx);
167         memmove(digest, newdigest, len);
168         memset(newdigest, 0, sizeof(newdigest));
169 }
170
171 void
172 isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
173                     unsigned int len)
174 {
175 #ifdef HMAC_RETURN_INT
176         RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
177                                 (int) len, EVP_sha384()) == 1);
178 #else
179         HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha384());
180 #endif
181 }
182
183 void
184 isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
185         HMAC_CTX_cleanup(ctx);
186 }
187
188 void
189 isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
190                    unsigned int len)
191 {
192 #ifdef HMAC_RETURN_INT
193         RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
194 #else
195         HMAC_Update(ctx, buf, (int) len);
196 #endif
197 }
198
199 void
200 isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
201         unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
202
203         REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
204
205 #ifdef HMAC_RETURN_INT
206         RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
207 #else
208         HMAC_Final(ctx, newdigest, NULL);
209 #endif
210         HMAC_CTX_cleanup(ctx);
211         memmove(digest, newdigest, len);
212         memset(newdigest, 0, sizeof(newdigest));
213 }
214
215 void
216 isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
217                     unsigned int len)
218 {
219 #ifdef HMAC_RETURN_INT
220         RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
221                                 (int) len, EVP_sha512()) == 1);
222 #else
223         HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha512());
224 #endif
225 }
226
227 void
228 isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
229         HMAC_CTX_cleanup(ctx);
230 }
231
232 void
233 isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
234                    unsigned int len)
235 {
236 #ifdef HMAC_RETURN_INT
237         RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
238 #else
239         HMAC_Update(ctx, buf, (int) len);
240 #endif
241 }
242
243 void
244 isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
245         unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
246
247         REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
248
249 #ifdef HMAC_RETURN_INT
250         RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
251 #else
252         HMAC_Final(ctx, newdigest, NULL);
253 #endif
254         HMAC_CTX_cleanup(ctx);
255         memmove(digest, newdigest, len);
256         memset(newdigest, 0, sizeof(newdigest));
257 }
258
259 #else
260
261 #define IPAD 0x36
262 #define OPAD 0x5C
263
264 /*
265  * Start HMAC-SHA1 process.  Initialize an sha1 context and digest the key.
266  */
267 void
268 isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
269                   unsigned int len)
270 {
271         unsigned char ipad[ISC_SHA1_BLOCK_LENGTH];
272         unsigned int i;
273
274         memset(ctx->key, 0, sizeof(ctx->key));
275         if (len > sizeof(ctx->key)) {
276                 isc_sha1_t sha1ctx;
277                 isc_sha1_init(&sha1ctx);
278                 isc_sha1_update(&sha1ctx, key, len);
279                 isc_sha1_final(&sha1ctx, ctx->key);
280         } else
281                 memmove(ctx->key, key, len);
282
283         isc_sha1_init(&ctx->sha1ctx);
284         memset(ipad, IPAD, sizeof(ipad));
285         for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
286                 ipad[i] ^= ctx->key[i];
287         isc_sha1_update(&ctx->sha1ctx, ipad, sizeof(ipad));
288 }
289
290 void
291 isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
292         isc_sha1_invalidate(&ctx->sha1ctx);
293         memset(ctx, 0, sizeof(*ctx));
294 }
295
296 /*
297  * Update context to reflect the concatenation of another buffer full
298  * of bytes.
299  */
300 void
301 isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
302                    unsigned int len)
303 {
304         isc_sha1_update(&ctx->sha1ctx, buf, len);
305 }
306
307 /*
308  * Compute signature - finalize SHA1 operation and reapply SHA1.
309  */
310 void
311 isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
312         unsigned char opad[ISC_SHA1_BLOCK_LENGTH];
313         unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
314         unsigned int i;
315
316         REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
317         isc_sha1_final(&ctx->sha1ctx, newdigest);
318
319         memset(opad, OPAD, sizeof(opad));
320         for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
321                 opad[i] ^= ctx->key[i];
322
323         isc_sha1_init(&ctx->sha1ctx);
324         isc_sha1_update(&ctx->sha1ctx, opad, sizeof(opad));
325         isc_sha1_update(&ctx->sha1ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
326         isc_sha1_final(&ctx->sha1ctx, newdigest);
327         isc_hmacsha1_invalidate(ctx);
328         memmove(digest, newdigest, len);
329         memset(newdigest, 0, sizeof(newdigest));
330 }
331
332 /*
333  * Start HMAC-SHA224 process.  Initialize an sha224 context and digest the key.
334  */
335 void
336 isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
337                     unsigned int len)
338 {
339         unsigned char ipad[ISC_SHA224_BLOCK_LENGTH];
340         unsigned int i;
341
342         memset(ctx->key, 0, sizeof(ctx->key));
343         if (len > sizeof(ctx->key)) {
344                 isc_sha224_t sha224ctx;
345                 isc_sha224_init(&sha224ctx);
346                 isc_sha224_update(&sha224ctx, key, len);
347                 isc_sha224_final(ctx->key, &sha224ctx);
348         } else
349                 memmove(ctx->key, key, len);
350
351         isc_sha224_init(&ctx->sha224ctx);
352         memset(ipad, IPAD, sizeof(ipad));
353         for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
354                 ipad[i] ^= ctx->key[i];
355         isc_sha224_update(&ctx->sha224ctx, ipad, sizeof(ipad));
356 }
357
358 void
359 isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
360         memset(ctx, 0, sizeof(*ctx));
361 }
362
363 /*
364  * Update context to reflect the concatenation of another buffer full
365  * of bytes.
366  */
367 void
368 isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
369                    unsigned int len)
370 {
371         isc_sha224_update(&ctx->sha224ctx, buf, len);
372 }
373
374 /*
375  * Compute signature - finalize SHA224 operation and reapply SHA224.
376  */
377 void
378 isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
379         unsigned char opad[ISC_SHA224_BLOCK_LENGTH];
380         unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
381         unsigned int i;
382
383         REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
384         isc_sha224_final(newdigest, &ctx->sha224ctx);
385
386         memset(opad, OPAD, sizeof(opad));
387         for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
388                 opad[i] ^= ctx->key[i];
389
390         isc_sha224_init(&ctx->sha224ctx);
391         isc_sha224_update(&ctx->sha224ctx, opad, sizeof(opad));
392         isc_sha224_update(&ctx->sha224ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
393         isc_sha224_final(newdigest, &ctx->sha224ctx);
394         memmove(digest, newdigest, len);
395         memset(newdigest, 0, sizeof(newdigest));
396 }
397
398 /*
399  * Start HMAC-SHA256 process.  Initialize an sha256 context and digest the key.
400  */
401 void
402 isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
403                     unsigned int len)
404 {
405         unsigned char ipad[ISC_SHA256_BLOCK_LENGTH];
406         unsigned int i;
407
408         memset(ctx->key, 0, sizeof(ctx->key));
409         if (len > sizeof(ctx->key)) {
410                 isc_sha256_t sha256ctx;
411                 isc_sha256_init(&sha256ctx);
412                 isc_sha256_update(&sha256ctx, key, len);
413                 isc_sha256_final(ctx->key, &sha256ctx);
414         } else
415                 memmove(ctx->key, key, len);
416
417         isc_sha256_init(&ctx->sha256ctx);
418         memset(ipad, IPAD, sizeof(ipad));
419         for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
420                 ipad[i] ^= ctx->key[i];
421         isc_sha256_update(&ctx->sha256ctx, ipad, sizeof(ipad));
422 }
423
424 void
425 isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
426         memset(ctx, 0, sizeof(*ctx));
427 }
428
429 /*
430  * Update context to reflect the concatenation of another buffer full
431  * of bytes.
432  */
433 void
434 isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
435                    unsigned int len)
436 {
437         isc_sha256_update(&ctx->sha256ctx, buf, len);
438 }
439
440 /*
441  * Compute signature - finalize SHA256 operation and reapply SHA256.
442  */
443 void
444 isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
445         unsigned char opad[ISC_SHA256_BLOCK_LENGTH];
446         unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
447         unsigned int i;
448
449         REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
450         isc_sha256_final(newdigest, &ctx->sha256ctx);
451
452         memset(opad, OPAD, sizeof(opad));
453         for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
454                 opad[i] ^= ctx->key[i];
455
456         isc_sha256_init(&ctx->sha256ctx);
457         isc_sha256_update(&ctx->sha256ctx, opad, sizeof(opad));
458         isc_sha256_update(&ctx->sha256ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
459         isc_sha256_final(newdigest, &ctx->sha256ctx);
460         memmove(digest, newdigest, len);
461         memset(newdigest, 0, sizeof(newdigest));
462 }
463
464 /*
465  * Start HMAC-SHA384 process.  Initialize an sha384 context and digest the key.
466  */
467 void
468 isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
469                     unsigned int len)
470 {
471         unsigned char ipad[ISC_SHA384_BLOCK_LENGTH];
472         unsigned int i;
473
474         memset(ctx->key, 0, sizeof(ctx->key));
475         if (len > sizeof(ctx->key)) {
476                 isc_sha384_t sha384ctx;
477                 isc_sha384_init(&sha384ctx);
478                 isc_sha384_update(&sha384ctx, key, len);
479                 isc_sha384_final(ctx->key, &sha384ctx);
480         } else
481                 memmove(ctx->key, key, len);
482
483         isc_sha384_init(&ctx->sha384ctx);
484         memset(ipad, IPAD, sizeof(ipad));
485         for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
486                 ipad[i] ^= ctx->key[i];
487         isc_sha384_update(&ctx->sha384ctx, ipad, sizeof(ipad));
488 }
489
490 void
491 isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
492         memset(ctx, 0, sizeof(*ctx));
493 }
494
495 /*
496  * Update context to reflect the concatenation of another buffer full
497  * of bytes.
498  */
499 void
500 isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
501                    unsigned int len)
502 {
503         isc_sha384_update(&ctx->sha384ctx, buf, len);
504 }
505
506 /*
507  * Compute signature - finalize SHA384 operation and reapply SHA384.
508  */
509 void
510 isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
511         unsigned char opad[ISC_SHA384_BLOCK_LENGTH];
512         unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
513         unsigned int i;
514
515         REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
516         isc_sha384_final(newdigest, &ctx->sha384ctx);
517
518         memset(opad, OPAD, sizeof(opad));
519         for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
520                 opad[i] ^= ctx->key[i];
521
522         isc_sha384_init(&ctx->sha384ctx);
523         isc_sha384_update(&ctx->sha384ctx, opad, sizeof(opad));
524         isc_sha384_update(&ctx->sha384ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
525         isc_sha384_final(newdigest, &ctx->sha384ctx);
526         memmove(digest, newdigest, len);
527         memset(newdigest, 0, sizeof(newdigest));
528 }
529
530 /*
531  * Start HMAC-SHA512 process.  Initialize an sha512 context and digest the key.
532  */
533 void
534 isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
535                     unsigned int len)
536 {
537         unsigned char ipad[ISC_SHA512_BLOCK_LENGTH];
538         unsigned int i;
539
540         memset(ctx->key, 0, sizeof(ctx->key));
541         if (len > sizeof(ctx->key)) {
542                 isc_sha512_t sha512ctx;
543                 isc_sha512_init(&sha512ctx);
544                 isc_sha512_update(&sha512ctx, key, len);
545                 isc_sha512_final(ctx->key, &sha512ctx);
546         } else
547                 memmove(ctx->key, key, len);
548
549         isc_sha512_init(&ctx->sha512ctx);
550         memset(ipad, IPAD, sizeof(ipad));
551         for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
552                 ipad[i] ^= ctx->key[i];
553         isc_sha512_update(&ctx->sha512ctx, ipad, sizeof(ipad));
554 }
555
556 void
557 isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
558         memset(ctx, 0, sizeof(*ctx));
559 }
560
561 /*
562  * Update context to reflect the concatenation of another buffer full
563  * of bytes.
564  */
565 void
566 isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
567                    unsigned int len)
568 {
569         isc_sha512_update(&ctx->sha512ctx, buf, len);
570 }
571
572 /*
573  * Compute signature - finalize SHA512 operation and reapply SHA512.
574  */
575 void
576 isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
577         unsigned char opad[ISC_SHA512_BLOCK_LENGTH];
578         unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
579         unsigned int i;
580
581         REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
582         isc_sha512_final(newdigest, &ctx->sha512ctx);
583
584         memset(opad, OPAD, sizeof(opad));
585         for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
586                 opad[i] ^= ctx->key[i];
587
588         isc_sha512_init(&ctx->sha512ctx);
589         isc_sha512_update(&ctx->sha512ctx, opad, sizeof(opad));
590         isc_sha512_update(&ctx->sha512ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
591         isc_sha512_final(newdigest, &ctx->sha512ctx);
592         memmove(digest, newdigest, len);
593         memset(newdigest, 0, sizeof(newdigest));
594 }
595 #endif /* !ISC_PLATFORM_OPENSSLHASH */
596
597 /*
598  * Verify signature - finalize SHA1 operation and reapply SHA1, then
599  * compare to the supplied digest.
600  */
601 isc_boolean_t
602 isc_hmacsha1_verify(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
603         unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
604
605         REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
606         isc_hmacsha1_sign(ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
607         return (isc_safe_memequal(digest, newdigest, len));
608 }
609
610 /*
611  * Verify signature - finalize SHA224 operation and reapply SHA224, then
612  * compare to the supplied digest.
613  */
614 isc_boolean_t
615 isc_hmacsha224_verify(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
616         unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
617
618         REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
619         isc_hmacsha224_sign(ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
620         return (isc_safe_memequal(digest, newdigest, len));
621 }
622
623 /*
624  * Verify signature - finalize SHA256 operation and reapply SHA256, then
625  * compare to the supplied digest.
626  */
627 isc_boolean_t
628 isc_hmacsha256_verify(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
629         unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
630
631         REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
632         isc_hmacsha256_sign(ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
633         return (isc_safe_memequal(digest, newdigest, len));
634 }
635
636 /*
637  * Verify signature - finalize SHA384 operation and reapply SHA384, then
638  * compare to the supplied digest.
639  */
640 isc_boolean_t
641 isc_hmacsha384_verify(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
642         unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
643
644         REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
645         isc_hmacsha384_sign(ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
646         return (isc_safe_memequal(digest, newdigest, len));
647 }
648
649 /*
650  * Verify signature - finalize SHA512 operation and reapply SHA512, then
651  * compare to the supplied digest.
652  */
653 isc_boolean_t
654 isc_hmacsha512_verify(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
655         unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
656
657         REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
658         isc_hmacsha512_sign(ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
659         return (isc_safe_memequal(digest, newdigest, len));
660 }