Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / lib / isc / hmacsha.c
blobb735e26fff3161772a1b2a563dcc6b9fadbfd359
1 /* $NetBSD: hmacsha.c,v 1.9 2015/07/08 17:28:59 christos Exp $ */
3 /*
4 * Copyright (C) 2005-2007, 2009, 2011-2014 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 */
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/safe.h>
33 #include <isc/sha1.h>
34 #include <isc/sha2.h>
35 #include <isc/string.h>
36 #include <isc/types.h>
37 #include <isc/util.h>
39 #if PKCS11CRYPTO
40 #include <pk11/internal.h>
41 #include <pk11/pk11.h>
42 #endif
44 #ifdef ISC_PLATFORM_OPENSSLHASH
45 void
46 isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
47 unsigned int len)
49 #ifdef HMAC_RETURN_INT
50 RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
51 (int) len, EVP_sha1()) == 1);
52 #else
53 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha1());
54 #endif
57 void
58 isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
59 HMAC_CTX_cleanup(ctx);
62 void
63 isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
64 unsigned int len)
66 #ifdef HMAC_RETURN_INT
67 RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
68 #else
69 HMAC_Update(ctx, buf, (int) len);
70 #endif
73 void
74 isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
75 unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
77 REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
79 #ifdef HMAC_RETURN_INT
80 RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
81 #else
82 HMAC_Final(ctx, newdigest, NULL);
83 #endif
84 HMAC_CTX_cleanup(ctx);
85 memmove(digest, newdigest, len);
86 memset(newdigest, 0, sizeof(newdigest));
89 void
90 isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
91 unsigned int len)
93 #ifdef HMAC_RETURN_INT
94 RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
95 (int) len, EVP_sha224()) == 1);
96 #else
97 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha224());
98 #endif
101 void
102 isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
103 HMAC_CTX_cleanup(ctx);
106 void
107 isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
108 unsigned int len)
110 #ifdef HMAC_RETURN_INT
111 RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
112 #else
113 HMAC_Update(ctx, buf, (int) len);
114 #endif
117 void
118 isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
119 unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
121 REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
123 #ifdef HMAC_RETURN_INT
124 RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
125 #else
126 HMAC_Final(ctx, newdigest, NULL);
127 #endif
128 HMAC_CTX_cleanup(ctx);
129 memmove(digest, newdigest, len);
130 memset(newdigest, 0, sizeof(newdigest));
133 void
134 isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
135 unsigned int len)
137 #ifdef HMAC_RETURN_INT
138 RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
139 (int) len, EVP_sha256()) == 1);
140 #else
141 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha256());
142 #endif
145 void
146 isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
147 HMAC_CTX_cleanup(ctx);
150 void
151 isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
152 unsigned int len)
154 #ifdef HMAC_RETURN_INT
155 RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
156 #else
157 HMAC_Update(ctx, buf, (int) len);
158 #endif
161 void
162 isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
163 unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
165 REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
167 #ifdef HMAC_RETURN_INT
168 RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
169 #else
170 HMAC_Final(ctx, newdigest, NULL);
171 #endif
172 HMAC_CTX_cleanup(ctx);
173 memmove(digest, newdigest, len);
174 memset(newdigest, 0, sizeof(newdigest));
177 void
178 isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
179 unsigned int len)
181 #ifdef HMAC_RETURN_INT
182 RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
183 (int) len, EVP_sha384()) == 1);
184 #else
185 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha384());
186 #endif
189 void
190 isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
191 HMAC_CTX_cleanup(ctx);
194 void
195 isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
196 unsigned int len)
198 #ifdef HMAC_RETURN_INT
199 RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
200 #else
201 HMAC_Update(ctx, buf, (int) len);
202 #endif
205 void
206 isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
207 unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
209 REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
211 #ifdef HMAC_RETURN_INT
212 RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
213 #else
214 HMAC_Final(ctx, newdigest, NULL);
215 #endif
216 HMAC_CTX_cleanup(ctx);
217 memmove(digest, newdigest, len);
218 memset(newdigest, 0, sizeof(newdigest));
221 void
222 isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
223 unsigned int len)
225 #ifdef HMAC_RETURN_INT
226 RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
227 (int) len, EVP_sha512()) == 1);
228 #else
229 HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha512());
230 #endif
233 void
234 isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
235 HMAC_CTX_cleanup(ctx);
238 void
239 isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
240 unsigned int len)
242 #ifdef HMAC_RETURN_INT
243 RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
244 #else
245 HMAC_Update(ctx, buf, (int) len);
246 #endif
249 void
250 isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
251 unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
253 REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
255 #ifdef HMAC_RETURN_INT
256 RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
257 #else
258 HMAC_Final(ctx, newdigest, NULL);
259 #endif
260 HMAC_CTX_cleanup(ctx);
261 memmove(digest, newdigest, len);
262 memset(newdigest, 0, sizeof(newdigest));
265 #elif PKCS11CRYPTO
267 static CK_BBOOL truevalue = TRUE;
268 static CK_BBOOL falsevalue = FALSE;
270 void
271 isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
272 unsigned int len)
274 CK_RV rv;
275 CK_MECHANISM mech = { CKM_SHA_1_HMAC, NULL, 0 };
276 CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
277 CK_KEY_TYPE keyType = CKK_SHA_1_HMAC;
278 CK_ATTRIBUTE keyTemplate[] =
280 { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
281 { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
282 { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
283 { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
284 { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
285 { CKA_VALUE, NULL, (CK_ULONG) len }
288 DE_CONST(key, keyTemplate[5].pValue);
289 RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
290 ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
291 ctx->object = CK_INVALID_HANDLE;
292 PK11_FATALCHECK(pkcs_C_CreateObject,
293 (ctx->session, keyTemplate,
294 (CK_ULONG) 6, &ctx->object));
295 INSIST(ctx->object != CK_INVALID_HANDLE);
296 PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
299 void
300 isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
301 CK_BYTE garbage[ISC_SHA1_DIGESTLENGTH];
302 CK_ULONG len = ISC_SHA1_DIGESTLENGTH;
304 if (ctx->handle == NULL)
305 return;
306 (void) pkcs_C_SignFinal(ctx->session, garbage, &len);
307 memset(garbage, 0, sizeof(garbage));
308 if (ctx->object != CK_INVALID_HANDLE)
309 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
310 ctx->object = CK_INVALID_HANDLE;
311 pk11_return_session(ctx);
314 void
315 isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
316 unsigned int len)
318 CK_RV rv;
319 CK_BYTE_PTR pPart;
321 DE_CONST(buf, pPart);
322 PK11_FATALCHECK(pkcs_C_SignUpdate,
323 (ctx->session, pPart, (CK_ULONG) len));
326 void
327 isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
328 CK_RV rv;
329 CK_BYTE newdigest[ISC_SHA1_DIGESTLENGTH];
330 CK_ULONG psl = ISC_SHA1_DIGESTLENGTH;
332 REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
334 PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
335 if (ctx->object != CK_INVALID_HANDLE)
336 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
337 ctx->object = CK_INVALID_HANDLE;
338 pk11_return_session(ctx);
339 memmove(digest, newdigest, len);
340 memset(newdigest, 0, sizeof(newdigest));
343 void
344 isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
345 unsigned int len)
347 CK_RV rv;
348 CK_MECHANISM mech = { CKM_SHA224_HMAC, NULL, 0 };
349 CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
350 CK_KEY_TYPE keyType = CKK_SHA224_HMAC;
351 CK_ATTRIBUTE keyTemplate[] =
353 { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
354 { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
355 { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
356 { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
357 { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
358 { CKA_VALUE, NULL, (CK_ULONG) len }
361 DE_CONST(key, keyTemplate[5].pValue);
362 RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
363 ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
364 ctx->object = CK_INVALID_HANDLE;
365 PK11_FATALCHECK(pkcs_C_CreateObject,
366 (ctx->session, keyTemplate,
367 (CK_ULONG) 6, &ctx->object));
368 INSIST(ctx->object != CK_INVALID_HANDLE);
369 PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
372 void
373 isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
374 CK_BYTE garbage[ISC_SHA224_DIGESTLENGTH];
375 CK_ULONG len = ISC_SHA224_DIGESTLENGTH;
377 if (ctx->handle == NULL)
378 return;
379 (void) pkcs_C_SignFinal(ctx->session, garbage, &len);
380 memset(garbage, 0, sizeof(garbage));
381 if (ctx->object != CK_INVALID_HANDLE)
382 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
383 ctx->object = CK_INVALID_HANDLE;
384 pk11_return_session(ctx);
387 void
388 isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
389 unsigned int len)
391 CK_RV rv;
392 CK_BYTE_PTR pPart;
394 DE_CONST(buf, pPart);
395 PK11_FATALCHECK(pkcs_C_SignUpdate,
396 (ctx->session, pPart, (CK_ULONG) len));
399 void
400 isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
401 CK_RV rv;
402 CK_BYTE newdigest[ISC_SHA224_DIGESTLENGTH];
403 CK_ULONG psl = ISC_SHA224_DIGESTLENGTH;
405 REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
407 PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
408 if (ctx->object != CK_INVALID_HANDLE)
409 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
410 ctx->object = CK_INVALID_HANDLE;
411 pk11_return_session(ctx);
412 memmove(digest, newdigest, len);
413 memset(newdigest, 0, sizeof(newdigest));
416 void
417 isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
418 unsigned int len)
420 CK_RV rv;
421 CK_MECHANISM mech = { CKM_SHA256_HMAC, NULL, 0 };
422 CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
423 CK_KEY_TYPE keyType = CKK_SHA256_HMAC;
424 CK_ATTRIBUTE keyTemplate[] =
426 { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
427 { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
428 { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
429 { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
430 { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
431 { CKA_VALUE, NULL, (CK_ULONG) len }
434 DE_CONST(key, keyTemplate[5].pValue);
435 RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
436 ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
437 ctx->object = CK_INVALID_HANDLE;
438 PK11_FATALCHECK(pkcs_C_CreateObject,
439 (ctx->session, keyTemplate,
440 (CK_ULONG) 6, &ctx->object));
441 INSIST(ctx->object != CK_INVALID_HANDLE);
442 PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
445 void
446 isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
447 CK_BYTE garbage[ISC_SHA256_DIGESTLENGTH];
448 CK_ULONG len = ISC_SHA256_DIGESTLENGTH;
450 if (ctx->handle == NULL)
451 return;
452 (void) pkcs_C_SignFinal(ctx->session, garbage, &len);
453 memset(garbage, 0, sizeof(garbage));
454 if (ctx->object != CK_INVALID_HANDLE)
455 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
456 ctx->object = CK_INVALID_HANDLE;
457 pk11_return_session(ctx);
460 void
461 isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
462 unsigned int len)
464 CK_RV rv;
465 CK_BYTE_PTR pPart;
467 DE_CONST(buf, pPart);
468 PK11_FATALCHECK(pkcs_C_SignUpdate,
469 (ctx->session, pPart, (CK_ULONG) len));
472 void
473 isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
474 CK_RV rv;
475 CK_BYTE newdigest[ISC_SHA256_DIGESTLENGTH];
476 CK_ULONG psl = ISC_SHA256_DIGESTLENGTH;
478 REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
480 PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
481 if (ctx->object != CK_INVALID_HANDLE)
482 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
483 ctx->object = CK_INVALID_HANDLE;
484 pk11_return_session(ctx);
485 memmove(digest, newdigest, len);
486 memset(newdigest, 0, sizeof(newdigest));
489 void
490 isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
491 unsigned int len)
493 CK_RV rv;
494 CK_MECHANISM mech = { CKM_SHA384_HMAC, NULL, 0 };
495 CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
496 CK_KEY_TYPE keyType = CKK_SHA384_HMAC;
497 CK_ATTRIBUTE keyTemplate[] =
499 { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
500 { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
501 { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
502 { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
503 { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
504 { CKA_VALUE, NULL, (CK_ULONG) len }
507 DE_CONST(key, keyTemplate[5].pValue);
508 RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
509 ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
510 ctx->object = CK_INVALID_HANDLE;
511 PK11_FATALCHECK(pkcs_C_CreateObject,
512 (ctx->session, keyTemplate,
513 (CK_ULONG) 6, &ctx->object));
514 INSIST(ctx->object != CK_INVALID_HANDLE);
515 PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
518 void
519 isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
520 CK_BYTE garbage[ISC_SHA384_DIGESTLENGTH];
521 CK_ULONG len = ISC_SHA384_DIGESTLENGTH;
523 if (ctx->handle == NULL)
524 return;
525 (void) pkcs_C_SignFinal(ctx->session, garbage, &len);
526 memset(garbage, 0, sizeof(garbage));
527 if (ctx->object != CK_INVALID_HANDLE)
528 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
529 ctx->object = CK_INVALID_HANDLE;
530 pk11_return_session(ctx);
533 void
534 isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
535 unsigned int len)
537 CK_RV rv;
538 CK_BYTE_PTR pPart;
540 DE_CONST(buf, pPart);
541 PK11_FATALCHECK(pkcs_C_SignUpdate,
542 (ctx->session, pPart, (CK_ULONG) len));
545 void
546 isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
547 CK_RV rv;
548 CK_BYTE newdigest[ISC_SHA384_DIGESTLENGTH];
549 CK_ULONG psl = ISC_SHA384_DIGESTLENGTH;
551 REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
553 PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
554 if (ctx->object != CK_INVALID_HANDLE)
555 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
556 ctx->object = CK_INVALID_HANDLE;
557 pk11_return_session(ctx);
558 memmove(digest, newdigest, len);
559 memset(newdigest, 0, sizeof(newdigest));
562 void
563 isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
564 unsigned int len)
566 CK_RV rv;
567 CK_MECHANISM mech = { CKM_SHA512_HMAC, NULL, 0 };
568 CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
569 CK_KEY_TYPE keyType = CKK_SHA512_HMAC;
570 CK_ATTRIBUTE keyTemplate[] =
572 { CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
573 { CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
574 { CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
575 { CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
576 { CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
577 { CKA_VALUE, NULL, (CK_ULONG) len }
580 DE_CONST(key, keyTemplate[5].pValue);
581 RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
582 ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
583 ctx->object = CK_INVALID_HANDLE;
584 PK11_FATALCHECK(pkcs_C_CreateObject,
585 (ctx->session, keyTemplate,
586 (CK_ULONG) 6, &ctx->object));
587 INSIST(ctx->object != CK_INVALID_HANDLE);
588 PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
591 void
592 isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
593 CK_BYTE garbage[ISC_SHA512_DIGESTLENGTH];
594 CK_ULONG len = ISC_SHA512_DIGESTLENGTH;
596 if (ctx->handle == NULL)
597 return;
598 (void) pkcs_C_SignFinal(ctx->session, garbage, &len);
599 memset(garbage, 0, sizeof(garbage));
600 if (ctx->object != CK_INVALID_HANDLE)
601 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
602 ctx->object = CK_INVALID_HANDLE;
603 pk11_return_session(ctx);
606 void
607 isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
608 unsigned int len)
610 CK_RV rv;
611 CK_BYTE_PTR pPart;
613 DE_CONST(buf, pPart);
614 PK11_FATALCHECK(pkcs_C_SignUpdate,
615 (ctx->session, pPart, (CK_ULONG) len));
618 void
619 isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
620 CK_RV rv;
621 CK_BYTE newdigest[ISC_SHA512_DIGESTLENGTH];
622 CK_ULONG psl = ISC_SHA512_DIGESTLENGTH;
624 REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
626 PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
627 if (ctx->object != CK_INVALID_HANDLE)
628 (void) pkcs_C_DestroyObject(ctx->session, ctx->object);
629 ctx->object = CK_INVALID_HANDLE;
630 pk11_return_session(ctx);
631 memmove(digest, newdigest, len);
632 memset(newdigest, 0, sizeof(newdigest));
635 #else
637 #define IPAD 0x36
638 #define OPAD 0x5C
641 * Start HMAC-SHA1 process. Initialize an sha1 context and digest the key.
643 void
644 isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
645 unsigned int len)
647 unsigned char ipad[ISC_SHA1_BLOCK_LENGTH];
648 unsigned int i;
650 memset(ctx->key, 0, sizeof(ctx->key));
651 if (len > sizeof(ctx->key)) {
652 isc_sha1_t sha1ctx;
653 isc_sha1_init(&sha1ctx);
654 isc_sha1_update(&sha1ctx, key, len);
655 isc_sha1_final(&sha1ctx, ctx->key);
656 } else
657 memmove(ctx->key, key, len);
659 isc_sha1_init(&ctx->sha1ctx);
660 memset(ipad, IPAD, sizeof(ipad));
661 for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
662 ipad[i] ^= ctx->key[i];
663 isc_sha1_update(&ctx->sha1ctx, ipad, sizeof(ipad));
666 void
667 isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
668 isc_sha1_invalidate(&ctx->sha1ctx);
669 memset(ctx->key, 0, sizeof(ctx->key));
670 memset(ctx, 0, sizeof(*ctx));
674 * Update context to reflect the concatenation of another buffer full
675 * of bytes.
677 void
678 isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
679 unsigned int len)
681 isc_sha1_update(&ctx->sha1ctx, buf, len);
685 * Compute signature - finalize SHA1 operation and reapply SHA1.
687 void
688 isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
689 unsigned char opad[ISC_SHA1_BLOCK_LENGTH];
690 unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
691 unsigned int i;
693 REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
694 isc_sha1_final(&ctx->sha1ctx, newdigest);
696 memset(opad, OPAD, sizeof(opad));
697 for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
698 opad[i] ^= ctx->key[i];
700 isc_sha1_init(&ctx->sha1ctx);
701 isc_sha1_update(&ctx->sha1ctx, opad, sizeof(opad));
702 isc_sha1_update(&ctx->sha1ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
703 isc_sha1_final(&ctx->sha1ctx, newdigest);
704 isc_hmacsha1_invalidate(ctx);
705 memmove(digest, newdigest, len);
706 memset(newdigest, 0, sizeof(newdigest));
710 * Start HMAC-SHA224 process. Initialize an sha224 context and digest the key.
712 void
713 isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
714 unsigned int len)
716 unsigned char ipad[ISC_SHA224_BLOCK_LENGTH];
717 unsigned int i;
719 memset(ctx->key, 0, sizeof(ctx->key));
720 if (len > sizeof(ctx->key)) {
721 isc_sha224_t sha224ctx;
722 isc_sha224_init(&sha224ctx);
723 isc_sha224_update(&sha224ctx, key, len);
724 isc_sha224_final(ctx->key, &sha224ctx);
725 } else
726 memmove(ctx->key, key, len);
728 isc_sha224_init(&ctx->sha224ctx);
729 memset(ipad, IPAD, sizeof(ipad));
730 for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
731 ipad[i] ^= ctx->key[i];
732 isc_sha224_update(&ctx->sha224ctx, ipad, sizeof(ipad));
735 void
736 isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
737 memset(ctx->key, 0, sizeof(ctx->key));
738 memset(ctx, 0, sizeof(*ctx));
742 * Update context to reflect the concatenation of another buffer full
743 * of bytes.
745 void
746 isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
747 unsigned int len)
749 isc_sha224_update(&ctx->sha224ctx, buf, len);
753 * Compute signature - finalize SHA224 operation and reapply SHA224.
755 void
756 isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
757 unsigned char opad[ISC_SHA224_BLOCK_LENGTH];
758 unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
759 unsigned int i;
761 REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
762 isc_sha224_final(newdigest, &ctx->sha224ctx);
764 memset(opad, OPAD, sizeof(opad));
765 for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
766 opad[i] ^= ctx->key[i];
768 isc_sha224_init(&ctx->sha224ctx);
769 isc_sha224_update(&ctx->sha224ctx, opad, sizeof(opad));
770 isc_sha224_update(&ctx->sha224ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
771 isc_sha224_final(newdigest, &ctx->sha224ctx);
772 memmove(digest, newdigest, len);
773 memset(newdigest, 0, sizeof(newdigest));
777 * Start HMAC-SHA256 process. Initialize an sha256 context and digest the key.
779 void
780 isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
781 unsigned int len)
783 unsigned char ipad[ISC_SHA256_BLOCK_LENGTH];
784 unsigned int i;
786 memset(ctx->key, 0, sizeof(ctx->key));
787 if (len > sizeof(ctx->key)) {
788 isc_sha256_t sha256ctx;
789 isc_sha256_init(&sha256ctx);
790 isc_sha256_update(&sha256ctx, key, len);
791 isc_sha256_final(ctx->key, &sha256ctx);
792 } else
793 memmove(ctx->key, key, len);
795 isc_sha256_init(&ctx->sha256ctx);
796 memset(ipad, IPAD, sizeof(ipad));
797 for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
798 ipad[i] ^= ctx->key[i];
799 isc_sha256_update(&ctx->sha256ctx, ipad, sizeof(ipad));
802 void
803 isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
804 memset(ctx->key, 0, sizeof(ctx->key));
805 memset(ctx, 0, sizeof(*ctx));
809 * Update context to reflect the concatenation of another buffer full
810 * of bytes.
812 void
813 isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
814 unsigned int len)
816 isc_sha256_update(&ctx->sha256ctx, buf, len);
820 * Compute signature - finalize SHA256 operation and reapply SHA256.
822 void
823 isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
824 unsigned char opad[ISC_SHA256_BLOCK_LENGTH];
825 unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
826 unsigned int i;
828 REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
829 isc_sha256_final(newdigest, &ctx->sha256ctx);
831 memset(opad, OPAD, sizeof(opad));
832 for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
833 opad[i] ^= ctx->key[i];
835 isc_sha256_init(&ctx->sha256ctx);
836 isc_sha256_update(&ctx->sha256ctx, opad, sizeof(opad));
837 isc_sha256_update(&ctx->sha256ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
838 isc_sha256_final(newdigest, &ctx->sha256ctx);
839 memmove(digest, newdigest, len);
840 memset(newdigest, 0, sizeof(newdigest));
844 * Start HMAC-SHA384 process. Initialize an sha384 context and digest the key.
846 void
847 isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
848 unsigned int len)
850 unsigned char ipad[ISC_SHA384_BLOCK_LENGTH];
851 unsigned int i;
853 memset(ctx->key, 0, sizeof(ctx->key));
854 if (len > sizeof(ctx->key)) {
855 isc_sha384_t sha384ctx;
856 isc_sha384_init(&sha384ctx);
857 isc_sha384_update(&sha384ctx, key, len);
858 isc_sha384_final(ctx->key, &sha384ctx);
859 } else
860 memmove(ctx->key, key, len);
862 isc_sha384_init(&ctx->sha384ctx);
863 memset(ipad, IPAD, sizeof(ipad));
864 for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
865 ipad[i] ^= ctx->key[i];
866 isc_sha384_update(&ctx->sha384ctx, ipad, sizeof(ipad));
869 void
870 isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
871 memset(ctx->key, 0, sizeof(ctx->key));
872 memset(ctx, 0, sizeof(*ctx));
876 * Update context to reflect the concatenation of another buffer full
877 * of bytes.
879 void
880 isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
881 unsigned int len)
883 isc_sha384_update(&ctx->sha384ctx, buf, len);
887 * Compute signature - finalize SHA384 operation and reapply SHA384.
889 void
890 isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
891 unsigned char opad[ISC_SHA384_BLOCK_LENGTH];
892 unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
893 unsigned int i;
895 REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
896 isc_sha384_final(newdigest, &ctx->sha384ctx);
898 memset(opad, OPAD, sizeof(opad));
899 for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
900 opad[i] ^= ctx->key[i];
902 isc_sha384_init(&ctx->sha384ctx);
903 isc_sha384_update(&ctx->sha384ctx, opad, sizeof(opad));
904 isc_sha384_update(&ctx->sha384ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
905 isc_sha384_final(newdigest, &ctx->sha384ctx);
906 memmove(digest, newdigest, len);
907 memset(newdigest, 0, sizeof(newdigest));
911 * Start HMAC-SHA512 process. Initialize an sha512 context and digest the key.
913 void
914 isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
915 unsigned int len)
917 unsigned char ipad[ISC_SHA512_BLOCK_LENGTH];
918 unsigned int i;
920 memset(ctx->key, 0, sizeof(ctx->key));
921 if (len > sizeof(ctx->key)) {
922 isc_sha512_t sha512ctx;
923 isc_sha512_init(&sha512ctx);
924 isc_sha512_update(&sha512ctx, key, len);
925 isc_sha512_final(ctx->key, &sha512ctx);
926 } else
927 memmove(ctx->key, key, len);
929 isc_sha512_init(&ctx->sha512ctx);
930 memset(ipad, IPAD, sizeof(ipad));
931 for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
932 ipad[i] ^= ctx->key[i];
933 isc_sha512_update(&ctx->sha512ctx, ipad, sizeof(ipad));
936 void
937 isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
938 memset(ctx->key, 0, sizeof(ctx->key));
939 memset(ctx, 0, sizeof(*ctx));
943 * Update context to reflect the concatenation of another buffer full
944 * of bytes.
946 void
947 isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
948 unsigned int len)
950 isc_sha512_update(&ctx->sha512ctx, buf, len);
954 * Compute signature - finalize SHA512 operation and reapply SHA512.
956 void
957 isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
958 unsigned char opad[ISC_SHA512_BLOCK_LENGTH];
959 unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
960 unsigned int i;
962 REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
963 isc_sha512_final(newdigest, &ctx->sha512ctx);
965 memset(opad, OPAD, sizeof(opad));
966 for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
967 opad[i] ^= ctx->key[i];
969 isc_sha512_init(&ctx->sha512ctx);
970 isc_sha512_update(&ctx->sha512ctx, opad, sizeof(opad));
971 isc_sha512_update(&ctx->sha512ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
972 isc_sha512_final(newdigest, &ctx->sha512ctx);
973 memmove(digest, newdigest, len);
974 memset(newdigest, 0, sizeof(newdigest));
976 #endif /* !ISC_PLATFORM_OPENSSLHASH */
979 * Verify signature - finalize SHA1 operation and reapply SHA1, then
980 * compare to the supplied digest.
982 isc_boolean_t
983 isc_hmacsha1_verify(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
984 unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
986 REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
987 isc_hmacsha1_sign(ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
988 return (isc_safe_memcmp(digest, newdigest, len));
992 * Verify signature - finalize SHA224 operation and reapply SHA224, then
993 * compare to the supplied digest.
995 isc_boolean_t
996 isc_hmacsha224_verify(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
997 unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
999 REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
1000 isc_hmacsha224_sign(ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
1001 return (isc_safe_memcmp(digest, newdigest, len));
1005 * Verify signature - finalize SHA256 operation and reapply SHA256, then
1006 * compare to the supplied digest.
1008 isc_boolean_t
1009 isc_hmacsha256_verify(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
1010 unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
1012 REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
1013 isc_hmacsha256_sign(ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
1014 return (isc_safe_memcmp(digest, newdigest, len));
1018 * Verify signature - finalize SHA384 operation and reapply SHA384, then
1019 * compare to the supplied digest.
1021 isc_boolean_t
1022 isc_hmacsha384_verify(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
1023 unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
1025 REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
1026 isc_hmacsha384_sign(ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
1027 return (isc_safe_memcmp(digest, newdigest, len));
1031 * Verify signature - finalize SHA512 operation and reapply SHA512, then
1032 * compare to the supplied digest.
1034 isc_boolean_t
1035 isc_hmacsha512_verify(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
1036 unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
1038 REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
1039 isc_hmacsha512_sign(ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
1040 return (isc_safe_memcmp(digest, newdigest, len));