1 /* $NetBSD: hmacsha.c,v 1.9 2015/07/08 17:28:59 christos Exp $ */
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.
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>
35 #include <isc/string.h>
36 #include <isc/types.h>
40 #include <pk11/internal.h>
41 #include <pk11/pk11.h>
44 #ifdef ISC_PLATFORM_OPENSSLHASH
46 isc_hmacsha1_init(isc_hmacsha1_t
*ctx
, const unsigned char *key
,
49 #ifdef HMAC_RETURN_INT
50 RUNTIME_CHECK(HMAC_Init(ctx
, (const void *) key
,
51 (int) len
, EVP_sha1()) == 1);
53 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha1());
58 isc_hmacsha1_invalidate(isc_hmacsha1_t
*ctx
) {
59 HMAC_CTX_cleanup(ctx
);
63 isc_hmacsha1_update(isc_hmacsha1_t
*ctx
, const unsigned char *buf
,
66 #ifdef HMAC_RETURN_INT
67 RUNTIME_CHECK(HMAC_Update(ctx
, buf
, (int) len
) == 1);
69 HMAC_Update(ctx
, buf
, (int) len
);
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);
82 HMAC_Final(ctx
, newdigest
, NULL
);
84 HMAC_CTX_cleanup(ctx
);
85 memmove(digest
, newdigest
, len
);
86 memset(newdigest
, 0, sizeof(newdigest
));
90 isc_hmacsha224_init(isc_hmacsha224_t
*ctx
, const unsigned char *key
,
93 #ifdef HMAC_RETURN_INT
94 RUNTIME_CHECK(HMAC_Init(ctx
, (const void *) key
,
95 (int) len
, EVP_sha224()) == 1);
97 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha224());
102 isc_hmacsha224_invalidate(isc_hmacsha224_t
*ctx
) {
103 HMAC_CTX_cleanup(ctx
);
107 isc_hmacsha224_update(isc_hmacsha224_t
*ctx
, const unsigned char *buf
,
110 #ifdef HMAC_RETURN_INT
111 RUNTIME_CHECK(HMAC_Update(ctx
, buf
, (int) len
) == 1);
113 HMAC_Update(ctx
, buf
, (int) len
);
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);
126 HMAC_Final(ctx
, newdigest
, NULL
);
128 HMAC_CTX_cleanup(ctx
);
129 memmove(digest
, newdigest
, len
);
130 memset(newdigest
, 0, sizeof(newdigest
));
134 isc_hmacsha256_init(isc_hmacsha256_t
*ctx
, const unsigned char *key
,
137 #ifdef HMAC_RETURN_INT
138 RUNTIME_CHECK(HMAC_Init(ctx
, (const void *) key
,
139 (int) len
, EVP_sha256()) == 1);
141 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha256());
146 isc_hmacsha256_invalidate(isc_hmacsha256_t
*ctx
) {
147 HMAC_CTX_cleanup(ctx
);
151 isc_hmacsha256_update(isc_hmacsha256_t
*ctx
, const unsigned char *buf
,
154 #ifdef HMAC_RETURN_INT
155 RUNTIME_CHECK(HMAC_Update(ctx
, buf
, (int) len
) == 1);
157 HMAC_Update(ctx
, buf
, (int) len
);
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);
170 HMAC_Final(ctx
, newdigest
, NULL
);
172 HMAC_CTX_cleanup(ctx
);
173 memmove(digest
, newdigest
, len
);
174 memset(newdigest
, 0, sizeof(newdigest
));
178 isc_hmacsha384_init(isc_hmacsha384_t
*ctx
, const unsigned char *key
,
181 #ifdef HMAC_RETURN_INT
182 RUNTIME_CHECK(HMAC_Init(ctx
, (const void *) key
,
183 (int) len
, EVP_sha384()) == 1);
185 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha384());
190 isc_hmacsha384_invalidate(isc_hmacsha384_t
*ctx
) {
191 HMAC_CTX_cleanup(ctx
);
195 isc_hmacsha384_update(isc_hmacsha384_t
*ctx
, const unsigned char *buf
,
198 #ifdef HMAC_RETURN_INT
199 RUNTIME_CHECK(HMAC_Update(ctx
, buf
, (int) len
) == 1);
201 HMAC_Update(ctx
, buf
, (int) len
);
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);
214 HMAC_Final(ctx
, newdigest
, NULL
);
216 HMAC_CTX_cleanup(ctx
);
217 memmove(digest
, newdigest
, len
);
218 memset(newdigest
, 0, sizeof(newdigest
));
222 isc_hmacsha512_init(isc_hmacsha512_t
*ctx
, const unsigned char *key
,
225 #ifdef HMAC_RETURN_INT
226 RUNTIME_CHECK(HMAC_Init(ctx
, (const void *) key
,
227 (int) len
, EVP_sha512()) == 1);
229 HMAC_Init(ctx
, (const void *) key
, (int) len
, EVP_sha512());
234 isc_hmacsha512_invalidate(isc_hmacsha512_t
*ctx
) {
235 HMAC_CTX_cleanup(ctx
);
239 isc_hmacsha512_update(isc_hmacsha512_t
*ctx
, const unsigned char *buf
,
242 #ifdef HMAC_RETURN_INT
243 RUNTIME_CHECK(HMAC_Update(ctx
, buf
, (int) len
) == 1);
245 HMAC_Update(ctx
, buf
, (int) len
);
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);
258 HMAC_Final(ctx
, newdigest
, NULL
);
260 HMAC_CTX_cleanup(ctx
);
261 memmove(digest
, newdigest
, len
);
262 memset(newdigest
, 0, sizeof(newdigest
));
267 static CK_BBOOL truevalue
= TRUE
;
268 static CK_BBOOL falsevalue
= FALSE
;
271 isc_hmacsha1_init(isc_hmacsha1_t
*ctx
, const unsigned char *key
,
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
));
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
)
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
);
315 isc_hmacsha1_update(isc_hmacsha1_t
*ctx
, const unsigned char *buf
,
321 DE_CONST(buf
, pPart
);
322 PK11_FATALCHECK(pkcs_C_SignUpdate
,
323 (ctx
->session
, pPart
, (CK_ULONG
) len
));
327 isc_hmacsha1_sign(isc_hmacsha1_t
*ctx
, unsigned char *digest
, size_t len
) {
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
));
344 isc_hmacsha224_init(isc_hmacsha224_t
*ctx
, const unsigned char *key
,
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
));
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
)
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
);
388 isc_hmacsha224_update(isc_hmacsha224_t
*ctx
, const unsigned char *buf
,
394 DE_CONST(buf
, pPart
);
395 PK11_FATALCHECK(pkcs_C_SignUpdate
,
396 (ctx
->session
, pPart
, (CK_ULONG
) len
));
400 isc_hmacsha224_sign(isc_hmacsha224_t
*ctx
, unsigned char *digest
, size_t len
) {
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
));
417 isc_hmacsha256_init(isc_hmacsha256_t
*ctx
, const unsigned char *key
,
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
));
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
)
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
);
461 isc_hmacsha256_update(isc_hmacsha256_t
*ctx
, const unsigned char *buf
,
467 DE_CONST(buf
, pPart
);
468 PK11_FATALCHECK(pkcs_C_SignUpdate
,
469 (ctx
->session
, pPart
, (CK_ULONG
) len
));
473 isc_hmacsha256_sign(isc_hmacsha256_t
*ctx
, unsigned char *digest
, size_t len
) {
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
));
490 isc_hmacsha384_init(isc_hmacsha384_t
*ctx
, const unsigned char *key
,
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
));
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
)
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
);
534 isc_hmacsha384_update(isc_hmacsha384_t
*ctx
, const unsigned char *buf
,
540 DE_CONST(buf
, pPart
);
541 PK11_FATALCHECK(pkcs_C_SignUpdate
,
542 (ctx
->session
, pPart
, (CK_ULONG
) len
));
546 isc_hmacsha384_sign(isc_hmacsha384_t
*ctx
, unsigned char *digest
, size_t len
) {
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
));
563 isc_hmacsha512_init(isc_hmacsha512_t
*ctx
, const unsigned char *key
,
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
));
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
)
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
);
607 isc_hmacsha512_update(isc_hmacsha512_t
*ctx
, const unsigned char *buf
,
613 DE_CONST(buf
, pPart
);
614 PK11_FATALCHECK(pkcs_C_SignUpdate
,
615 (ctx
->session
, pPart
, (CK_ULONG
) len
));
619 isc_hmacsha512_sign(isc_hmacsha512_t
*ctx
, unsigned char *digest
, size_t len
) {
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
));
641 * Start HMAC-SHA1 process. Initialize an sha1 context and digest the key.
644 isc_hmacsha1_init(isc_hmacsha1_t
*ctx
, const unsigned char *key
,
647 unsigned char ipad
[ISC_SHA1_BLOCK_LENGTH
];
650 memset(ctx
->key
, 0, sizeof(ctx
->key
));
651 if (len
> sizeof(ctx
->key
)) {
653 isc_sha1_init(&sha1ctx
);
654 isc_sha1_update(&sha1ctx
, key
, len
);
655 isc_sha1_final(&sha1ctx
, ctx
->key
);
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
));
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
678 isc_hmacsha1_update(isc_hmacsha1_t
*ctx
, const unsigned char *buf
,
681 isc_sha1_update(&ctx
->sha1ctx
, buf
, len
);
685 * Compute signature - finalize SHA1 operation and reapply SHA1.
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
];
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.
713 isc_hmacsha224_init(isc_hmacsha224_t
*ctx
, const unsigned char *key
,
716 unsigned char ipad
[ISC_SHA224_BLOCK_LENGTH
];
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
);
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
));
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
746 isc_hmacsha224_update(isc_hmacsha224_t
*ctx
, const unsigned char *buf
,
749 isc_sha224_update(&ctx
->sha224ctx
, buf
, len
);
753 * Compute signature - finalize SHA224 operation and reapply SHA224.
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
];
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.
780 isc_hmacsha256_init(isc_hmacsha256_t
*ctx
, const unsigned char *key
,
783 unsigned char ipad
[ISC_SHA256_BLOCK_LENGTH
];
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
);
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
));
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
813 isc_hmacsha256_update(isc_hmacsha256_t
*ctx
, const unsigned char *buf
,
816 isc_sha256_update(&ctx
->sha256ctx
, buf
, len
);
820 * Compute signature - finalize SHA256 operation and reapply SHA256.
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
];
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.
847 isc_hmacsha384_init(isc_hmacsha384_t
*ctx
, const unsigned char *key
,
850 unsigned char ipad
[ISC_SHA384_BLOCK_LENGTH
];
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
);
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
));
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
880 isc_hmacsha384_update(isc_hmacsha384_t
*ctx
, const unsigned char *buf
,
883 isc_sha384_update(&ctx
->sha384ctx
, buf
, len
);
887 * Compute signature - finalize SHA384 operation and reapply SHA384.
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
];
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.
914 isc_hmacsha512_init(isc_hmacsha512_t
*ctx
, const unsigned char *key
,
917 unsigned char ipad
[ISC_SHA512_BLOCK_LENGTH
];
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
);
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
));
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
947 isc_hmacsha512_update(isc_hmacsha512_t
*ctx
, const unsigned char *buf
,
950 isc_sha512_update(&ctx
->sha512ctx
, buf
, len
);
954 * Compute signature - finalize SHA512 operation and reapply SHA512.
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
];
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.
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.
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.
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.
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.
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
));