2 * Portions Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 1999-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
12 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
24 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
26 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 * Principal Author: Brian Wellington
34 * $Id: hmac_link.c,v 1.11 2008/04/01 23:47:10 tbox Exp $
39 #include <isc/buffer.h>
40 #include <isc/hmacmd5.h>
41 #include <isc/hmacsha.h>
45 #include <isc/string.h>
48 #include <dst/result.h>
50 #include "dst_internal.h"
51 #include "dst_parse.h"
54 #define HMAC_IPAD 0x36
55 #define HMAC_OPAD 0x5c
57 static isc_result_t hmacmd5_fromdns(dst_key_t *key, isc_buffer_t *data);
59 struct dst_hmacmd5_key {
60 unsigned char key[HMAC_LEN];
64 getkeybits(dst_key_t *key, struct dst_private_element *element) {
66 if (element->length != 2)
67 return (DST_R_INVALIDPRIVATEKEY);
69 key->key_bits = (element->data[0] << 8) + element->data[1];
71 return (ISC_R_SUCCESS);
75 hmacmd5_createctx(dst_key_t *key, dst_context_t *dctx) {
76 isc_hmacmd5_t *hmacmd5ctx;
77 dst_hmacmd5_key_t *hkey = key->keydata.hmacmd5;
79 hmacmd5ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacmd5_t));
80 if (hmacmd5ctx == NULL)
81 return (ISC_R_NOMEMORY);
82 isc_hmacmd5_init(hmacmd5ctx, hkey->key, HMAC_LEN);
83 dctx->ctxdata.hmacmd5ctx = hmacmd5ctx;
84 return (ISC_R_SUCCESS);
88 hmacmd5_destroyctx(dst_context_t *dctx) {
89 isc_hmacmd5_t *hmacmd5ctx = dctx->ctxdata.hmacmd5ctx;
91 if (hmacmd5ctx != NULL) {
92 isc_hmacmd5_invalidate(hmacmd5ctx);
93 isc_mem_put(dctx->mctx, hmacmd5ctx, sizeof(isc_hmacmd5_t));
94 dctx->ctxdata.hmacmd5ctx = NULL;
99 hmacmd5_adddata(dst_context_t *dctx, const isc_region_t *data) {
100 isc_hmacmd5_t *hmacmd5ctx = dctx->ctxdata.hmacmd5ctx;
102 isc_hmacmd5_update(hmacmd5ctx, data->base, data->length);
103 return (ISC_R_SUCCESS);
107 hmacmd5_sign(dst_context_t *dctx, isc_buffer_t *sig) {
108 isc_hmacmd5_t *hmacmd5ctx = dctx->ctxdata.hmacmd5ctx;
109 unsigned char *digest;
111 if (isc_buffer_availablelength(sig) < ISC_MD5_DIGESTLENGTH)
112 return (ISC_R_NOSPACE);
113 digest = isc_buffer_used(sig);
114 isc_hmacmd5_sign(hmacmd5ctx, digest);
115 isc_buffer_add(sig, ISC_MD5_DIGESTLENGTH);
117 return (ISC_R_SUCCESS);
121 hmacmd5_verify(dst_context_t *dctx, const isc_region_t *sig) {
122 isc_hmacmd5_t *hmacmd5ctx = dctx->ctxdata.hmacmd5ctx;
124 if (sig->length > ISC_MD5_DIGESTLENGTH)
125 return (DST_R_VERIFYFAILURE);
127 if (isc_hmacmd5_verify2(hmacmd5ctx, sig->base, sig->length))
128 return (ISC_R_SUCCESS);
130 return (DST_R_VERIFYFAILURE);
134 hmacmd5_compare(const dst_key_t *key1, const dst_key_t *key2) {
135 dst_hmacmd5_key_t *hkey1, *hkey2;
137 hkey1 = key1->keydata.hmacmd5;
138 hkey2 = key2->keydata.hmacmd5;
140 if (hkey1 == NULL && hkey2 == NULL)
142 else if (hkey1 == NULL || hkey2 == NULL)
145 if (memcmp(hkey1->key, hkey2->key, HMAC_LEN) == 0)
152 hmacmd5_generate(dst_key_t *key, int pseudorandom_ok) {
156 unsigned char data[HMAC_LEN];
158 bytes = (key->key_size + 7) / 8;
159 if (bytes > HMAC_LEN) {
161 key->key_size = HMAC_LEN * 8;
164 memset(data, 0, HMAC_LEN);
165 ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));
167 if (ret != ISC_R_SUCCESS)
170 isc_buffer_init(&b, data, bytes);
171 isc_buffer_add(&b, bytes);
172 ret = hmacmd5_fromdns(key, &b);
173 memset(data, 0, HMAC_LEN);
179 hmacmd5_isprivate(const dst_key_t *key) {
185 hmacmd5_destroy(dst_key_t *key) {
186 dst_hmacmd5_key_t *hkey = key->keydata.hmacmd5;
187 memset(hkey, 0, sizeof(dst_hmacmd5_key_t));
188 isc_mem_put(key->mctx, hkey, sizeof(dst_hmacmd5_key_t));
189 key->keydata.hmacmd5 = NULL;
193 hmacmd5_todns(const dst_key_t *key, isc_buffer_t *data) {
194 dst_hmacmd5_key_t *hkey;
197 REQUIRE(key->keydata.hmacmd5 != NULL);
199 hkey = key->keydata.hmacmd5;
201 bytes = (key->key_size + 7) / 8;
202 if (isc_buffer_availablelength(data) < bytes)
203 return (ISC_R_NOSPACE);
204 isc_buffer_putmem(data, hkey->key, bytes);
206 return (ISC_R_SUCCESS);
210 hmacmd5_fromdns(dst_key_t *key, isc_buffer_t *data) {
211 dst_hmacmd5_key_t *hkey;
216 isc_buffer_remainingregion(data, &r);
218 return (ISC_R_SUCCESS);
220 hkey = isc_mem_get(key->mctx, sizeof(dst_hmacmd5_key_t));
222 return (ISC_R_NOMEMORY);
224 memset(hkey->key, 0, sizeof(hkey->key));
226 if (r.length > HMAC_LEN) {
227 isc_md5_init(&md5ctx);
228 isc_md5_update(&md5ctx, r.base, r.length);
229 isc_md5_final(&md5ctx, hkey->key);
230 keylen = ISC_MD5_DIGESTLENGTH;
233 memcpy(hkey->key, r.base, r.length);
237 key->key_size = keylen * 8;
238 key->keydata.hmacmd5 = hkey;
240 return (ISC_R_SUCCESS);
244 hmacmd5_tofile(const dst_key_t *key, const char *directory) {
246 dst_hmacmd5_key_t *hkey;
248 int bytes = (key->key_size + 7) / 8;
249 unsigned char buf[2];
251 if (key->keydata.hmacmd5 == NULL)
252 return (DST_R_NULLKEY);
254 hkey = key->keydata.hmacmd5;
256 priv.elements[cnt].tag = TAG_HMACMD5_KEY;
257 priv.elements[cnt].length = bytes;
258 priv.elements[cnt++].data = hkey->key;
260 buf[0] = (key->key_bits >> 8) & 0xffU;
261 buf[1] = key->key_bits & 0xffU;
262 priv.elements[cnt].tag = TAG_HMACMD5_BITS;
263 priv.elements[cnt].data = buf;
264 priv.elements[cnt++].length = 2;
266 priv.nelements = cnt;
267 return (dst__privstruct_writefile(key, &priv, directory));
271 hmacmd5_parse(dst_key_t *key, isc_lex_t *lexer) {
273 isc_result_t result, tresult;
275 isc_mem_t *mctx = key->mctx;
278 /* read private key file */
279 result = dst__privstruct_parse(key, DST_ALG_HMACMD5, lexer, mctx, &priv);
280 if (result != ISC_R_SUCCESS)
284 for (i = 0; i < priv.nelements && result == ISC_R_SUCCESS; i++) {
285 switch (priv.elements[i].tag) {
286 case TAG_HMACMD5_KEY:
287 isc_buffer_init(&b, priv.elements[i].data,
288 priv.elements[i].length);
289 isc_buffer_add(&b, priv.elements[i].length);
290 tresult = hmacmd5_fromdns(key, &b);
291 if (tresult != ISC_R_SUCCESS)
294 case TAG_HMACMD5_BITS:
295 tresult = getkeybits(key, &priv.elements[i]);
296 if (tresult != ISC_R_SUCCESS)
300 result = DST_R_INVALIDPRIVATEKEY;
304 dst__privstruct_free(&priv, mctx);
305 memset(&priv, 0, sizeof(priv));
309 static dst_func_t hmacmd5_functions = {
315 NULL, /*%< computesecret */
317 NULL, /*%< paramcompare */
325 NULL, /*%< cleanup */
326 NULL, /*%< fromlabel */
330 dst__hmacmd5_init(dst_func_t **funcp) {
331 REQUIRE(funcp != NULL);
333 *funcp = &hmacmd5_functions;
334 return (ISC_R_SUCCESS);
337 static isc_result_t hmacsha1_fromdns(dst_key_t *key, isc_buffer_t *data);
339 struct dst_hmacsha1_key {
340 unsigned char key[ISC_SHA1_DIGESTLENGTH];
344 hmacsha1_createctx(dst_key_t *key, dst_context_t *dctx) {
345 isc_hmacsha1_t *hmacsha1ctx;
346 dst_hmacsha1_key_t *hkey = key->keydata.hmacsha1;
348 hmacsha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacsha1_t));
349 if (hmacsha1ctx == NULL)
350 return (ISC_R_NOMEMORY);
351 isc_hmacsha1_init(hmacsha1ctx, hkey->key, ISC_SHA1_DIGESTLENGTH);
352 dctx->ctxdata.hmacsha1ctx = hmacsha1ctx;
353 return (ISC_R_SUCCESS);
357 hmacsha1_destroyctx(dst_context_t *dctx) {
358 isc_hmacsha1_t *hmacsha1ctx = dctx->ctxdata.hmacsha1ctx;
360 if (hmacsha1ctx != NULL) {
361 isc_hmacsha1_invalidate(hmacsha1ctx);
362 isc_mem_put(dctx->mctx, hmacsha1ctx, sizeof(isc_hmacsha1_t));
363 dctx->ctxdata.hmacsha1ctx = NULL;
368 hmacsha1_adddata(dst_context_t *dctx, const isc_region_t *data) {
369 isc_hmacsha1_t *hmacsha1ctx = dctx->ctxdata.hmacsha1ctx;
371 isc_hmacsha1_update(hmacsha1ctx, data->base, data->length);
372 return (ISC_R_SUCCESS);
376 hmacsha1_sign(dst_context_t *dctx, isc_buffer_t *sig) {
377 isc_hmacsha1_t *hmacsha1ctx = dctx->ctxdata.hmacsha1ctx;
378 unsigned char *digest;
380 if (isc_buffer_availablelength(sig) < ISC_SHA1_DIGESTLENGTH)
381 return (ISC_R_NOSPACE);
382 digest = isc_buffer_used(sig);
383 isc_hmacsha1_sign(hmacsha1ctx, digest, ISC_SHA1_DIGESTLENGTH);
384 isc_buffer_add(sig, ISC_SHA1_DIGESTLENGTH);
386 return (ISC_R_SUCCESS);
390 hmacsha1_verify(dst_context_t *dctx, const isc_region_t *sig) {
391 isc_hmacsha1_t *hmacsha1ctx = dctx->ctxdata.hmacsha1ctx;
393 if (sig->length > ISC_SHA1_DIGESTLENGTH || sig->length == 0)
394 return (DST_R_VERIFYFAILURE);
396 if (isc_hmacsha1_verify(hmacsha1ctx, sig->base, sig->length))
397 return (ISC_R_SUCCESS);
399 return (DST_R_VERIFYFAILURE);
403 hmacsha1_compare(const dst_key_t *key1, const dst_key_t *key2) {
404 dst_hmacsha1_key_t *hkey1, *hkey2;
406 hkey1 = key1->keydata.hmacsha1;
407 hkey2 = key2->keydata.hmacsha1;
409 if (hkey1 == NULL && hkey2 == NULL)
411 else if (hkey1 == NULL || hkey2 == NULL)
414 if (memcmp(hkey1->key, hkey2->key, ISC_SHA1_DIGESTLENGTH) == 0)
421 hmacsha1_generate(dst_key_t *key, int pseudorandom_ok) {
425 unsigned char data[HMAC_LEN];
427 bytes = (key->key_size + 7) / 8;
428 if (bytes > HMAC_LEN) {
430 key->key_size = HMAC_LEN * 8;
433 memset(data, 0, HMAC_LEN);
434 ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));
436 if (ret != ISC_R_SUCCESS)
439 isc_buffer_init(&b, data, bytes);
440 isc_buffer_add(&b, bytes);
441 ret = hmacsha1_fromdns(key, &b);
442 memset(data, 0, ISC_SHA1_DIGESTLENGTH);
448 hmacsha1_isprivate(const dst_key_t *key) {
454 hmacsha1_destroy(dst_key_t *key) {
455 dst_hmacsha1_key_t *hkey = key->keydata.hmacsha1;
456 memset(hkey, 0, sizeof(dst_hmacsha1_key_t));
457 isc_mem_put(key->mctx, hkey, sizeof(dst_hmacsha1_key_t));
458 key->keydata.hmacsha1 = NULL;
462 hmacsha1_todns(const dst_key_t *key, isc_buffer_t *data) {
463 dst_hmacsha1_key_t *hkey;
466 REQUIRE(key->keydata.hmacsha1 != NULL);
468 hkey = key->keydata.hmacsha1;
470 bytes = (key->key_size + 7) / 8;
471 if (isc_buffer_availablelength(data) < bytes)
472 return (ISC_R_NOSPACE);
473 isc_buffer_putmem(data, hkey->key, bytes);
475 return (ISC_R_SUCCESS);
479 hmacsha1_fromdns(dst_key_t *key, isc_buffer_t *data) {
480 dst_hmacsha1_key_t *hkey;
485 isc_buffer_remainingregion(data, &r);
487 return (ISC_R_SUCCESS);
489 hkey = isc_mem_get(key->mctx, sizeof(dst_hmacsha1_key_t));
491 return (ISC_R_NOMEMORY);
493 memset(hkey->key, 0, sizeof(hkey->key));
495 if (r.length > ISC_SHA1_DIGESTLENGTH) {
496 isc_sha1_init(&sha1ctx);
497 isc_sha1_update(&sha1ctx, r.base, r.length);
498 isc_sha1_final(&sha1ctx, hkey->key);
499 keylen = ISC_SHA1_DIGESTLENGTH;
502 memcpy(hkey->key, r.base, r.length);
506 key->key_size = keylen * 8;
507 key->keydata.hmacsha1 = hkey;
509 return (ISC_R_SUCCESS);
513 hmacsha1_tofile(const dst_key_t *key, const char *directory) {
515 dst_hmacsha1_key_t *hkey;
517 int bytes = (key->key_size + 7) / 8;
518 unsigned char buf[2];
520 if (key->keydata.hmacsha1 == NULL)
521 return (DST_R_NULLKEY);
523 hkey = key->keydata.hmacsha1;
525 priv.elements[cnt].tag = TAG_HMACSHA1_KEY;
526 priv.elements[cnt].length = bytes;
527 priv.elements[cnt++].data = hkey->key;
529 buf[0] = (key->key_bits >> 8) & 0xffU;
530 buf[1] = key->key_bits & 0xffU;
531 priv.elements[cnt].tag = TAG_HMACSHA1_BITS;
532 priv.elements[cnt].data = buf;
533 priv.elements[cnt++].length = 2;
535 priv.nelements = cnt;
536 return (dst__privstruct_writefile(key, &priv, directory));
540 hmacsha1_parse(dst_key_t *key, isc_lex_t *lexer) {
542 isc_result_t result, tresult;
544 isc_mem_t *mctx = key->mctx;
547 /* read private key file */
548 result = dst__privstruct_parse(key, DST_ALG_HMACSHA1, lexer, mctx,
550 if (result != ISC_R_SUCCESS)
554 for (i = 0; i < priv.nelements; i++) {
555 switch (priv.elements[i].tag) {
556 case TAG_HMACSHA1_KEY:
557 isc_buffer_init(&b, priv.elements[i].data,
558 priv.elements[i].length);
559 isc_buffer_add(&b, priv.elements[i].length);
560 tresult = hmacsha1_fromdns(key, &b);
561 if (tresult != ISC_R_SUCCESS)
564 case TAG_HMACSHA1_BITS:
565 tresult = getkeybits(key, &priv.elements[i]);
566 if (tresult != ISC_R_SUCCESS)
570 result = DST_R_INVALIDPRIVATEKEY;
574 dst__privstruct_free(&priv, mctx);
575 memset(&priv, 0, sizeof(priv));
579 static dst_func_t hmacsha1_functions = {
585 NULL, /* computesecret */
587 NULL, /* paramcompare */
596 NULL, /* fromlabel */
600 dst__hmacsha1_init(dst_func_t **funcp) {
601 REQUIRE(funcp != NULL);
603 *funcp = &hmacsha1_functions;
604 return (ISC_R_SUCCESS);
607 static isc_result_t hmacsha224_fromdns(dst_key_t *key, isc_buffer_t *data);
609 struct dst_hmacsha224_key {
610 unsigned char key[ISC_SHA224_DIGESTLENGTH];
614 hmacsha224_createctx(dst_key_t *key, dst_context_t *dctx) {
615 isc_hmacsha224_t *hmacsha224ctx;
616 dst_hmacsha224_key_t *hkey = key->keydata.hmacsha224;
618 hmacsha224ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacsha224_t));
619 if (hmacsha224ctx == NULL)
620 return (ISC_R_NOMEMORY);
621 isc_hmacsha224_init(hmacsha224ctx, hkey->key, ISC_SHA224_DIGESTLENGTH);
622 dctx->ctxdata.hmacsha224ctx = hmacsha224ctx;
623 return (ISC_R_SUCCESS);
627 hmacsha224_destroyctx(dst_context_t *dctx) {
628 isc_hmacsha224_t *hmacsha224ctx = dctx->ctxdata.hmacsha224ctx;
630 if (hmacsha224ctx != NULL) {
631 isc_hmacsha224_invalidate(hmacsha224ctx);
632 isc_mem_put(dctx->mctx, hmacsha224ctx, sizeof(isc_hmacsha224_t));
633 dctx->ctxdata.hmacsha224ctx = NULL;
638 hmacsha224_adddata(dst_context_t *dctx, const isc_region_t *data) {
639 isc_hmacsha224_t *hmacsha224ctx = dctx->ctxdata.hmacsha224ctx;
641 isc_hmacsha224_update(hmacsha224ctx, data->base, data->length);
642 return (ISC_R_SUCCESS);
646 hmacsha224_sign(dst_context_t *dctx, isc_buffer_t *sig) {
647 isc_hmacsha224_t *hmacsha224ctx = dctx->ctxdata.hmacsha224ctx;
648 unsigned char *digest;
650 if (isc_buffer_availablelength(sig) < ISC_SHA224_DIGESTLENGTH)
651 return (ISC_R_NOSPACE);
652 digest = isc_buffer_used(sig);
653 isc_hmacsha224_sign(hmacsha224ctx, digest, ISC_SHA224_DIGESTLENGTH);
654 isc_buffer_add(sig, ISC_SHA224_DIGESTLENGTH);
656 return (ISC_R_SUCCESS);
660 hmacsha224_verify(dst_context_t *dctx, const isc_region_t *sig) {
661 isc_hmacsha224_t *hmacsha224ctx = dctx->ctxdata.hmacsha224ctx;
663 if (sig->length > ISC_SHA224_DIGESTLENGTH || sig->length == 0)
664 return (DST_R_VERIFYFAILURE);
666 if (isc_hmacsha224_verify(hmacsha224ctx, sig->base, sig->length))
667 return (ISC_R_SUCCESS);
669 return (DST_R_VERIFYFAILURE);
673 hmacsha224_compare(const dst_key_t *key1, const dst_key_t *key2) {
674 dst_hmacsha224_key_t *hkey1, *hkey2;
676 hkey1 = key1->keydata.hmacsha224;
677 hkey2 = key2->keydata.hmacsha224;
679 if (hkey1 == NULL && hkey2 == NULL)
681 else if (hkey1 == NULL || hkey2 == NULL)
684 if (memcmp(hkey1->key, hkey2->key, ISC_SHA224_DIGESTLENGTH) == 0)
691 hmacsha224_generate(dst_key_t *key, int pseudorandom_ok) {
695 unsigned char data[HMAC_LEN];
697 bytes = (key->key_size + 7) / 8;
698 if (bytes > HMAC_LEN) {
700 key->key_size = HMAC_LEN * 8;
703 memset(data, 0, HMAC_LEN);
704 ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));
706 if (ret != ISC_R_SUCCESS)
709 isc_buffer_init(&b, data, bytes);
710 isc_buffer_add(&b, bytes);
711 ret = hmacsha224_fromdns(key, &b);
712 memset(data, 0, ISC_SHA224_DIGESTLENGTH);
718 hmacsha224_isprivate(const dst_key_t *key) {
724 hmacsha224_destroy(dst_key_t *key) {
725 dst_hmacsha224_key_t *hkey = key->keydata.hmacsha224;
726 memset(hkey, 0, sizeof(dst_hmacsha224_key_t));
727 isc_mem_put(key->mctx, hkey, sizeof(dst_hmacsha224_key_t));
728 key->keydata.hmacsha224 = NULL;
732 hmacsha224_todns(const dst_key_t *key, isc_buffer_t *data) {
733 dst_hmacsha224_key_t *hkey;
736 REQUIRE(key->keydata.hmacsha224 != NULL);
738 hkey = key->keydata.hmacsha224;
740 bytes = (key->key_size + 7) / 8;
741 if (isc_buffer_availablelength(data) < bytes)
742 return (ISC_R_NOSPACE);
743 isc_buffer_putmem(data, hkey->key, bytes);
745 return (ISC_R_SUCCESS);
749 hmacsha224_fromdns(dst_key_t *key, isc_buffer_t *data) {
750 dst_hmacsha224_key_t *hkey;
753 isc_sha224_t sha224ctx;
755 isc_buffer_remainingregion(data, &r);
757 return (ISC_R_SUCCESS);
759 hkey = isc_mem_get(key->mctx, sizeof(dst_hmacsha224_key_t));
761 return (ISC_R_NOMEMORY);
763 memset(hkey->key, 0, sizeof(hkey->key));
765 if (r.length > ISC_SHA224_DIGESTLENGTH) {
766 isc_sha224_init(&sha224ctx);
767 isc_sha224_update(&sha224ctx, r.base, r.length);
768 isc_sha224_final(hkey->key, &sha224ctx);
769 keylen = ISC_SHA224_DIGESTLENGTH;
772 memcpy(hkey->key, r.base, r.length);
776 key->key_size = keylen * 8;
777 key->keydata.hmacsha224 = hkey;
779 return (ISC_R_SUCCESS);
783 hmacsha224_tofile(const dst_key_t *key, const char *directory) {
785 dst_hmacsha224_key_t *hkey;
787 int bytes = (key->key_size + 7) / 8;
788 unsigned char buf[2];
790 if (key->keydata.hmacsha224 == NULL)
791 return (DST_R_NULLKEY);
793 hkey = key->keydata.hmacsha224;
795 priv.elements[cnt].tag = TAG_HMACSHA224_KEY;
796 priv.elements[cnt].length = bytes;
797 priv.elements[cnt++].data = hkey->key;
799 buf[0] = (key->key_bits >> 8) & 0xffU;
800 buf[1] = key->key_bits & 0xffU;
801 priv.elements[cnt].tag = TAG_HMACSHA224_BITS;
802 priv.elements[cnt].data = buf;
803 priv.elements[cnt++].length = 2;
805 priv.nelements = cnt;
806 return (dst__privstruct_writefile(key, &priv, directory));
810 hmacsha224_parse(dst_key_t *key, isc_lex_t *lexer) {
812 isc_result_t result, tresult;
814 isc_mem_t *mctx = key->mctx;
817 /* read private key file */
818 result = dst__privstruct_parse(key, DST_ALG_HMACSHA224, lexer, mctx,
820 if (result != ISC_R_SUCCESS)
824 for (i = 0; i < priv.nelements; i++) {
825 switch (priv.elements[i].tag) {
826 case TAG_HMACSHA224_KEY:
827 isc_buffer_init(&b, priv.elements[i].data,
828 priv.elements[i].length);
829 isc_buffer_add(&b, priv.elements[i].length);
830 tresult = hmacsha224_fromdns(key, &b);
831 if (tresult != ISC_R_SUCCESS)
834 case TAG_HMACSHA224_BITS:
835 tresult = getkeybits(key, &priv.elements[i]);
836 if (tresult != ISC_R_SUCCESS)
840 result = DST_R_INVALIDPRIVATEKEY;
844 dst__privstruct_free(&priv, mctx);
845 memset(&priv, 0, sizeof(priv));
849 static dst_func_t hmacsha224_functions = {
850 hmacsha224_createctx,
851 hmacsha224_destroyctx,
855 NULL, /* computesecret */
857 NULL, /* paramcompare */
859 hmacsha224_isprivate,
866 NULL, /* fromlabel */
870 dst__hmacsha224_init(dst_func_t **funcp) {
871 REQUIRE(funcp != NULL);
873 *funcp = &hmacsha224_functions;
874 return (ISC_R_SUCCESS);
877 static isc_result_t hmacsha256_fromdns(dst_key_t *key, isc_buffer_t *data);
879 struct dst_hmacsha256_key {
880 unsigned char key[ISC_SHA256_DIGESTLENGTH];
884 hmacsha256_createctx(dst_key_t *key, dst_context_t *dctx) {
885 isc_hmacsha256_t *hmacsha256ctx;
886 dst_hmacsha256_key_t *hkey = key->keydata.hmacsha256;
888 hmacsha256ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacsha256_t));
889 if (hmacsha256ctx == NULL)
890 return (ISC_R_NOMEMORY);
891 isc_hmacsha256_init(hmacsha256ctx, hkey->key, ISC_SHA256_DIGESTLENGTH);
892 dctx->ctxdata.hmacsha256ctx = hmacsha256ctx;
893 return (ISC_R_SUCCESS);
897 hmacsha256_destroyctx(dst_context_t *dctx) {
898 isc_hmacsha256_t *hmacsha256ctx = dctx->ctxdata.hmacsha256ctx;
900 if (hmacsha256ctx != NULL) {
901 isc_hmacsha256_invalidate(hmacsha256ctx);
902 isc_mem_put(dctx->mctx, hmacsha256ctx, sizeof(isc_hmacsha256_t));
903 dctx->ctxdata.hmacsha256ctx = NULL;
908 hmacsha256_adddata(dst_context_t *dctx, const isc_region_t *data) {
909 isc_hmacsha256_t *hmacsha256ctx = dctx->ctxdata.hmacsha256ctx;
911 isc_hmacsha256_update(hmacsha256ctx, data->base, data->length);
912 return (ISC_R_SUCCESS);
916 hmacsha256_sign(dst_context_t *dctx, isc_buffer_t *sig) {
917 isc_hmacsha256_t *hmacsha256ctx = dctx->ctxdata.hmacsha256ctx;
918 unsigned char *digest;
920 if (isc_buffer_availablelength(sig) < ISC_SHA256_DIGESTLENGTH)
921 return (ISC_R_NOSPACE);
922 digest = isc_buffer_used(sig);
923 isc_hmacsha256_sign(hmacsha256ctx, digest, ISC_SHA256_DIGESTLENGTH);
924 isc_buffer_add(sig, ISC_SHA256_DIGESTLENGTH);
926 return (ISC_R_SUCCESS);
930 hmacsha256_verify(dst_context_t *dctx, const isc_region_t *sig) {
931 isc_hmacsha256_t *hmacsha256ctx = dctx->ctxdata.hmacsha256ctx;
933 if (sig->length > ISC_SHA256_DIGESTLENGTH || sig->length == 0)
934 return (DST_R_VERIFYFAILURE);
936 if (isc_hmacsha256_verify(hmacsha256ctx, sig->base, sig->length))
937 return (ISC_R_SUCCESS);
939 return (DST_R_VERIFYFAILURE);
943 hmacsha256_compare(const dst_key_t *key1, const dst_key_t *key2) {
944 dst_hmacsha256_key_t *hkey1, *hkey2;
946 hkey1 = key1->keydata.hmacsha256;
947 hkey2 = key2->keydata.hmacsha256;
949 if (hkey1 == NULL && hkey2 == NULL)
951 else if (hkey1 == NULL || hkey2 == NULL)
954 if (memcmp(hkey1->key, hkey2->key, ISC_SHA256_DIGESTLENGTH) == 0)
961 hmacsha256_generate(dst_key_t *key, int pseudorandom_ok) {
965 unsigned char data[HMAC_LEN];
967 bytes = (key->key_size + 7) / 8;
968 if (bytes > HMAC_LEN) {
970 key->key_size = HMAC_LEN * 8;
973 memset(data, 0, HMAC_LEN);
974 ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));
976 if (ret != ISC_R_SUCCESS)
979 isc_buffer_init(&b, data, bytes);
980 isc_buffer_add(&b, bytes);
981 ret = hmacsha256_fromdns(key, &b);
982 memset(data, 0, ISC_SHA256_DIGESTLENGTH);
988 hmacsha256_isprivate(const dst_key_t *key) {
994 hmacsha256_destroy(dst_key_t *key) {
995 dst_hmacsha256_key_t *hkey = key->keydata.hmacsha256;
996 memset(hkey, 0, sizeof(dst_hmacsha256_key_t));
997 isc_mem_put(key->mctx, hkey, sizeof(dst_hmacsha256_key_t));
998 key->keydata.hmacsha256 = NULL;
1002 hmacsha256_todns(const dst_key_t *key, isc_buffer_t *data) {
1003 dst_hmacsha256_key_t *hkey;
1006 REQUIRE(key->keydata.hmacsha256 != NULL);
1008 hkey = key->keydata.hmacsha256;
1010 bytes = (key->key_size + 7) / 8;
1011 if (isc_buffer_availablelength(data) < bytes)
1012 return (ISC_R_NOSPACE);
1013 isc_buffer_putmem(data, hkey->key, bytes);
1015 return (ISC_R_SUCCESS);
1019 hmacsha256_fromdns(dst_key_t *key, isc_buffer_t *data) {
1020 dst_hmacsha256_key_t *hkey;
1023 isc_sha256_t sha256ctx;
1025 isc_buffer_remainingregion(data, &r);
1027 return (ISC_R_SUCCESS);
1029 hkey = isc_mem_get(key->mctx, sizeof(dst_hmacsha256_key_t));
1031 return (ISC_R_NOMEMORY);
1033 memset(hkey->key, 0, sizeof(hkey->key));
1035 if (r.length > ISC_SHA256_DIGESTLENGTH) {
1036 isc_sha256_init(&sha256ctx);
1037 isc_sha256_update(&sha256ctx, r.base, r.length);
1038 isc_sha256_final(hkey->key, &sha256ctx);
1039 keylen = ISC_SHA256_DIGESTLENGTH;
1042 memcpy(hkey->key, r.base, r.length);
1046 key->key_size = keylen * 8;
1047 key->keydata.hmacsha256 = hkey;
1049 return (ISC_R_SUCCESS);
1053 hmacsha256_tofile(const dst_key_t *key, const char *directory) {
1055 dst_hmacsha256_key_t *hkey;
1057 int bytes = (key->key_size + 7) / 8;
1058 unsigned char buf[2];
1060 if (key->keydata.hmacsha256 == NULL)
1061 return (DST_R_NULLKEY);
1063 hkey = key->keydata.hmacsha256;
1065 priv.elements[cnt].tag = TAG_HMACSHA256_KEY;
1066 priv.elements[cnt].length = bytes;
1067 priv.elements[cnt++].data = hkey->key;
1069 buf[0] = (key->key_bits >> 8) & 0xffU;
1070 buf[1] = key->key_bits & 0xffU;
1071 priv.elements[cnt].tag = TAG_HMACSHA256_BITS;
1072 priv.elements[cnt].data = buf;
1073 priv.elements[cnt++].length = 2;
1075 priv.nelements = cnt;
1076 return (dst__privstruct_writefile(key, &priv, directory));
1080 hmacsha256_parse(dst_key_t *key, isc_lex_t *lexer) {
1082 isc_result_t result, tresult;
1084 isc_mem_t *mctx = key->mctx;
1087 /* read private key file */
1088 result = dst__privstruct_parse(key, DST_ALG_HMACSHA256, lexer, mctx,
1090 if (result != ISC_R_SUCCESS)
1094 for (i = 0; i < priv.nelements; i++) {
1095 switch (priv.elements[i].tag) {
1096 case TAG_HMACSHA256_KEY:
1097 isc_buffer_init(&b, priv.elements[i].data,
1098 priv.elements[i].length);
1099 isc_buffer_add(&b, priv.elements[i].length);
1100 tresult = hmacsha256_fromdns(key, &b);
1101 if (tresult != ISC_R_SUCCESS)
1104 case TAG_HMACSHA256_BITS:
1105 tresult = getkeybits(key, &priv.elements[i]);
1106 if (tresult != ISC_R_SUCCESS)
1110 result = DST_R_INVALIDPRIVATEKEY;
1114 dst__privstruct_free(&priv, mctx);
1115 memset(&priv, 0, sizeof(priv));
1119 static dst_func_t hmacsha256_functions = {
1120 hmacsha256_createctx,
1121 hmacsha256_destroyctx,
1125 NULL, /* computesecret */
1127 NULL, /* paramcompare */
1128 hmacsha256_generate,
1129 hmacsha256_isprivate,
1136 NULL, /* fromlabel */
1140 dst__hmacsha256_init(dst_func_t **funcp) {
1141 REQUIRE(funcp != NULL);
1143 *funcp = &hmacsha256_functions;
1144 return (ISC_R_SUCCESS);
1147 static isc_result_t hmacsha384_fromdns(dst_key_t *key, isc_buffer_t *data);
1149 struct dst_hmacsha384_key {
1150 unsigned char key[ISC_SHA384_DIGESTLENGTH];
1154 hmacsha384_createctx(dst_key_t *key, dst_context_t *dctx) {
1155 isc_hmacsha384_t *hmacsha384ctx;
1156 dst_hmacsha384_key_t *hkey = key->keydata.hmacsha384;
1158 hmacsha384ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacsha384_t));
1159 if (hmacsha384ctx == NULL)
1160 return (ISC_R_NOMEMORY);
1161 isc_hmacsha384_init(hmacsha384ctx, hkey->key, ISC_SHA384_DIGESTLENGTH);
1162 dctx->ctxdata.hmacsha384ctx = hmacsha384ctx;
1163 return (ISC_R_SUCCESS);
1167 hmacsha384_destroyctx(dst_context_t *dctx) {
1168 isc_hmacsha384_t *hmacsha384ctx = dctx->ctxdata.hmacsha384ctx;
1170 if (hmacsha384ctx != NULL) {
1171 isc_hmacsha384_invalidate(hmacsha384ctx);
1172 isc_mem_put(dctx->mctx, hmacsha384ctx, sizeof(isc_hmacsha384_t));
1173 dctx->ctxdata.hmacsha384ctx = NULL;
1178 hmacsha384_adddata(dst_context_t *dctx, const isc_region_t *data) {
1179 isc_hmacsha384_t *hmacsha384ctx = dctx->ctxdata.hmacsha384ctx;
1181 isc_hmacsha384_update(hmacsha384ctx, data->base, data->length);
1182 return (ISC_R_SUCCESS);
1186 hmacsha384_sign(dst_context_t *dctx, isc_buffer_t *sig) {
1187 isc_hmacsha384_t *hmacsha384ctx = dctx->ctxdata.hmacsha384ctx;
1188 unsigned char *digest;
1190 if (isc_buffer_availablelength(sig) < ISC_SHA384_DIGESTLENGTH)
1191 return (ISC_R_NOSPACE);
1192 digest = isc_buffer_used(sig);
1193 isc_hmacsha384_sign(hmacsha384ctx, digest, ISC_SHA384_DIGESTLENGTH);
1194 isc_buffer_add(sig, ISC_SHA384_DIGESTLENGTH);
1196 return (ISC_R_SUCCESS);
1200 hmacsha384_verify(dst_context_t *dctx, const isc_region_t *sig) {
1201 isc_hmacsha384_t *hmacsha384ctx = dctx->ctxdata.hmacsha384ctx;
1203 if (sig->length > ISC_SHA384_DIGESTLENGTH || sig->length == 0)
1204 return (DST_R_VERIFYFAILURE);
1206 if (isc_hmacsha384_verify(hmacsha384ctx, sig->base, sig->length))
1207 return (ISC_R_SUCCESS);
1209 return (DST_R_VERIFYFAILURE);
1212 static isc_boolean_t
1213 hmacsha384_compare(const dst_key_t *key1, const dst_key_t *key2) {
1214 dst_hmacsha384_key_t *hkey1, *hkey2;
1216 hkey1 = key1->keydata.hmacsha384;
1217 hkey2 = key2->keydata.hmacsha384;
1219 if (hkey1 == NULL && hkey2 == NULL)
1221 else if (hkey1 == NULL || hkey2 == NULL)
1224 if (memcmp(hkey1->key, hkey2->key, ISC_SHA384_DIGESTLENGTH) == 0)
1231 hmacsha384_generate(dst_key_t *key, int pseudorandom_ok) {
1235 unsigned char data[HMAC_LEN];
1237 bytes = (key->key_size + 7) / 8;
1238 if (bytes > HMAC_LEN) {
1240 key->key_size = HMAC_LEN * 8;
1243 memset(data, 0, HMAC_LEN);
1244 ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));
1246 if (ret != ISC_R_SUCCESS)
1249 isc_buffer_init(&b, data, bytes);
1250 isc_buffer_add(&b, bytes);
1251 ret = hmacsha384_fromdns(key, &b);
1252 memset(data, 0, ISC_SHA384_DIGESTLENGTH);
1257 static isc_boolean_t
1258 hmacsha384_isprivate(const dst_key_t *key) {
1264 hmacsha384_destroy(dst_key_t *key) {
1265 dst_hmacsha384_key_t *hkey = key->keydata.hmacsha384;
1266 memset(hkey, 0, sizeof(dst_hmacsha384_key_t));
1267 isc_mem_put(key->mctx, hkey, sizeof(dst_hmacsha384_key_t));
1268 key->keydata.hmacsha384 = NULL;
1272 hmacsha384_todns(const dst_key_t *key, isc_buffer_t *data) {
1273 dst_hmacsha384_key_t *hkey;
1276 REQUIRE(key->keydata.hmacsha384 != NULL);
1278 hkey = key->keydata.hmacsha384;
1280 bytes = (key->key_size + 7) / 8;
1281 if (isc_buffer_availablelength(data) < bytes)
1282 return (ISC_R_NOSPACE);
1283 isc_buffer_putmem(data, hkey->key, bytes);
1285 return (ISC_R_SUCCESS);
1289 hmacsha384_fromdns(dst_key_t *key, isc_buffer_t *data) {
1290 dst_hmacsha384_key_t *hkey;
1293 isc_sha384_t sha384ctx;
1295 isc_buffer_remainingregion(data, &r);
1297 return (ISC_R_SUCCESS);
1299 hkey = isc_mem_get(key->mctx, sizeof(dst_hmacsha384_key_t));
1301 return (ISC_R_NOMEMORY);
1303 memset(hkey->key, 0, sizeof(hkey->key));
1305 if (r.length > ISC_SHA384_DIGESTLENGTH) {
1306 isc_sha384_init(&sha384ctx);
1307 isc_sha384_update(&sha384ctx, r.base, r.length);
1308 isc_sha384_final(hkey->key, &sha384ctx);
1309 keylen = ISC_SHA384_DIGESTLENGTH;
1312 memcpy(hkey->key, r.base, r.length);
1316 key->key_size = keylen * 8;
1317 key->keydata.hmacsha384 = hkey;
1319 return (ISC_R_SUCCESS);
1323 hmacsha384_tofile(const dst_key_t *key, const char *directory) {
1325 dst_hmacsha384_key_t *hkey;
1327 int bytes = (key->key_size + 7) / 8;
1328 unsigned char buf[2];
1330 if (key->keydata.hmacsha384 == NULL)
1331 return (DST_R_NULLKEY);
1333 hkey = key->keydata.hmacsha384;
1335 priv.elements[cnt].tag = TAG_HMACSHA384_KEY;
1336 priv.elements[cnt].length = bytes;
1337 priv.elements[cnt++].data = hkey->key;
1339 buf[0] = (key->key_bits >> 8) & 0xffU;
1340 buf[1] = key->key_bits & 0xffU;
1341 priv.elements[cnt].tag = TAG_HMACSHA384_BITS;
1342 priv.elements[cnt].data = buf;
1343 priv.elements[cnt++].length = 2;
1345 priv.nelements = cnt;
1346 return (dst__privstruct_writefile(key, &priv, directory));
1350 hmacsha384_parse(dst_key_t *key, isc_lex_t *lexer) {
1352 isc_result_t result, tresult;
1354 isc_mem_t *mctx = key->mctx;
1357 /* read private key file */
1358 result = dst__privstruct_parse(key, DST_ALG_HMACSHA384, lexer, mctx,
1360 if (result != ISC_R_SUCCESS)
1364 for (i = 0; i < priv.nelements; i++) {
1365 switch (priv.elements[i].tag) {
1366 case TAG_HMACSHA384_KEY:
1367 isc_buffer_init(&b, priv.elements[i].data,
1368 priv.elements[i].length);
1369 isc_buffer_add(&b, priv.elements[i].length);
1370 tresult = hmacsha384_fromdns(key, &b);
1371 if (tresult != ISC_R_SUCCESS)
1374 case TAG_HMACSHA384_BITS:
1375 tresult = getkeybits(key, &priv.elements[i]);
1376 if (tresult != ISC_R_SUCCESS)
1380 result = DST_R_INVALIDPRIVATEKEY;
1384 dst__privstruct_free(&priv, mctx);
1385 memset(&priv, 0, sizeof(priv));
1389 static dst_func_t hmacsha384_functions = {
1390 hmacsha384_createctx,
1391 hmacsha384_destroyctx,
1395 NULL, /* computesecret */
1397 NULL, /* paramcompare */
1398 hmacsha384_generate,
1399 hmacsha384_isprivate,
1406 NULL, /* fromlabel */
1410 dst__hmacsha384_init(dst_func_t **funcp) {
1411 REQUIRE(funcp != NULL);
1413 *funcp = &hmacsha384_functions;
1414 return (ISC_R_SUCCESS);
1417 static isc_result_t hmacsha512_fromdns(dst_key_t *key, isc_buffer_t *data);
1419 struct dst_hmacsha512_key {
1420 unsigned char key[ISC_SHA512_DIGESTLENGTH];
1424 hmacsha512_createctx(dst_key_t *key, dst_context_t *dctx) {
1425 isc_hmacsha512_t *hmacsha512ctx;
1426 dst_hmacsha512_key_t *hkey = key->keydata.hmacsha512;
1428 hmacsha512ctx = isc_mem_get(dctx->mctx, sizeof(isc_hmacsha512_t));
1429 if (hmacsha512ctx == NULL)
1430 return (ISC_R_NOMEMORY);
1431 isc_hmacsha512_init(hmacsha512ctx, hkey->key, ISC_SHA512_DIGESTLENGTH);
1432 dctx->ctxdata.hmacsha512ctx = hmacsha512ctx;
1433 return (ISC_R_SUCCESS);
1437 hmacsha512_destroyctx(dst_context_t *dctx) {
1438 isc_hmacsha512_t *hmacsha512ctx = dctx->ctxdata.hmacsha512ctx;
1440 if (hmacsha512ctx != NULL) {
1441 isc_hmacsha512_invalidate(hmacsha512ctx);
1442 isc_mem_put(dctx->mctx, hmacsha512ctx, sizeof(isc_hmacsha512_t));
1443 dctx->ctxdata.hmacsha512ctx = NULL;
1448 hmacsha512_adddata(dst_context_t *dctx, const isc_region_t *data) {
1449 isc_hmacsha512_t *hmacsha512ctx = dctx->ctxdata.hmacsha512ctx;
1451 isc_hmacsha512_update(hmacsha512ctx, data->base, data->length);
1452 return (ISC_R_SUCCESS);
1456 hmacsha512_sign(dst_context_t *dctx, isc_buffer_t *sig) {
1457 isc_hmacsha512_t *hmacsha512ctx = dctx->ctxdata.hmacsha512ctx;
1458 unsigned char *digest;
1460 if (isc_buffer_availablelength(sig) < ISC_SHA512_DIGESTLENGTH)
1461 return (ISC_R_NOSPACE);
1462 digest = isc_buffer_used(sig);
1463 isc_hmacsha512_sign(hmacsha512ctx, digest, ISC_SHA512_DIGESTLENGTH);
1464 isc_buffer_add(sig, ISC_SHA512_DIGESTLENGTH);
1466 return (ISC_R_SUCCESS);
1470 hmacsha512_verify(dst_context_t *dctx, const isc_region_t *sig) {
1471 isc_hmacsha512_t *hmacsha512ctx = dctx->ctxdata.hmacsha512ctx;
1473 if (sig->length > ISC_SHA512_DIGESTLENGTH || sig->length == 0)
1474 return (DST_R_VERIFYFAILURE);
1476 if (isc_hmacsha512_verify(hmacsha512ctx, sig->base, sig->length))
1477 return (ISC_R_SUCCESS);
1479 return (DST_R_VERIFYFAILURE);
1482 static isc_boolean_t
1483 hmacsha512_compare(const dst_key_t *key1, const dst_key_t *key2) {
1484 dst_hmacsha512_key_t *hkey1, *hkey2;
1486 hkey1 = key1->keydata.hmacsha512;
1487 hkey2 = key2->keydata.hmacsha512;
1489 if (hkey1 == NULL && hkey2 == NULL)
1491 else if (hkey1 == NULL || hkey2 == NULL)
1494 if (memcmp(hkey1->key, hkey2->key, ISC_SHA512_DIGESTLENGTH) == 0)
1501 hmacsha512_generate(dst_key_t *key, int pseudorandom_ok) {
1505 unsigned char data[HMAC_LEN];
1507 bytes = (key->key_size + 7) / 8;
1508 if (bytes > HMAC_LEN) {
1510 key->key_size = HMAC_LEN * 8;
1513 memset(data, 0, HMAC_LEN);
1514 ret = dst__entropy_getdata(data, bytes, ISC_TF(pseudorandom_ok != 0));
1516 if (ret != ISC_R_SUCCESS)
1519 isc_buffer_init(&b, data, bytes);
1520 isc_buffer_add(&b, bytes);
1521 ret = hmacsha512_fromdns(key, &b);
1522 memset(data, 0, ISC_SHA512_DIGESTLENGTH);
1527 static isc_boolean_t
1528 hmacsha512_isprivate(const dst_key_t *key) {
1534 hmacsha512_destroy(dst_key_t *key) {
1535 dst_hmacsha512_key_t *hkey = key->keydata.hmacsha512;
1536 memset(hkey, 0, sizeof(dst_hmacsha512_key_t));
1537 isc_mem_put(key->mctx, hkey, sizeof(dst_hmacsha512_key_t));
1538 key->keydata.hmacsha512 = NULL;
1542 hmacsha512_todns(const dst_key_t *key, isc_buffer_t *data) {
1543 dst_hmacsha512_key_t *hkey;
1546 REQUIRE(key->keydata.hmacsha512 != NULL);
1548 hkey = key->keydata.hmacsha512;
1550 bytes = (key->key_size + 7) / 8;
1551 if (isc_buffer_availablelength(data) < bytes)
1552 return (ISC_R_NOSPACE);
1553 isc_buffer_putmem(data, hkey->key, bytes);
1555 return (ISC_R_SUCCESS);
1559 hmacsha512_fromdns(dst_key_t *key, isc_buffer_t *data) {
1560 dst_hmacsha512_key_t *hkey;
1563 isc_sha512_t sha512ctx;
1565 isc_buffer_remainingregion(data, &r);
1567 return (ISC_R_SUCCESS);
1569 hkey = isc_mem_get(key->mctx, sizeof(dst_hmacsha512_key_t));
1571 return (ISC_R_NOMEMORY);
1573 memset(hkey->key, 0, sizeof(hkey->key));
1575 if (r.length > ISC_SHA512_DIGESTLENGTH) {
1576 isc_sha512_init(&sha512ctx);
1577 isc_sha512_update(&sha512ctx, r.base, r.length);
1578 isc_sha512_final(hkey->key, &sha512ctx);
1579 keylen = ISC_SHA512_DIGESTLENGTH;
1582 memcpy(hkey->key, r.base, r.length);
1586 key->key_size = keylen * 8;
1587 key->keydata.hmacsha512 = hkey;
1589 return (ISC_R_SUCCESS);
1593 hmacsha512_tofile(const dst_key_t *key, const char *directory) {
1595 dst_hmacsha512_key_t *hkey;
1597 int bytes = (key->key_size + 7) / 8;
1598 unsigned char buf[2];
1600 if (key->keydata.hmacsha512 == NULL)
1601 return (DST_R_NULLKEY);
1603 hkey = key->keydata.hmacsha512;
1605 priv.elements[cnt].tag = TAG_HMACSHA512_KEY;
1606 priv.elements[cnt].length = bytes;
1607 priv.elements[cnt++].data = hkey->key;
1609 buf[0] = (key->key_bits >> 8) & 0xffU;
1610 buf[1] = key->key_bits & 0xffU;
1611 priv.elements[cnt].tag = TAG_HMACSHA512_BITS;
1612 priv.elements[cnt].data = buf;
1613 priv.elements[cnt++].length = 2;
1615 priv.nelements = cnt;
1616 return (dst__privstruct_writefile(key, &priv, directory));
1620 hmacsha512_parse(dst_key_t *key, isc_lex_t *lexer) {
1622 isc_result_t result, tresult;
1624 isc_mem_t *mctx = key->mctx;
1627 /* read private key file */
1628 result = dst__privstruct_parse(key, DST_ALG_HMACSHA512, lexer, mctx,
1630 if (result != ISC_R_SUCCESS)
1634 for (i = 0; i < priv.nelements; i++) {
1635 switch (priv.elements[i].tag) {
1636 case TAG_HMACSHA512_KEY:
1637 isc_buffer_init(&b, priv.elements[i].data,
1638 priv.elements[i].length);
1639 isc_buffer_add(&b, priv.elements[i].length);
1640 tresult = hmacsha512_fromdns(key, &b);
1641 if (tresult != ISC_R_SUCCESS)
1644 case TAG_HMACSHA512_BITS:
1645 tresult = getkeybits(key, &priv.elements[i]);
1646 if (tresult != ISC_R_SUCCESS)
1650 result = DST_R_INVALIDPRIVATEKEY;
1654 dst__privstruct_free(&priv, mctx);
1655 memset(&priv, 0, sizeof(priv));
1659 static dst_func_t hmacsha512_functions = {
1660 hmacsha512_createctx,
1661 hmacsha512_destroyctx,
1665 NULL, /* computesecret */
1667 NULL, /* paramcompare */
1668 hmacsha512_generate,
1669 hmacsha512_isprivate,
1676 NULL, /* fromlabel */
1680 dst__hmacsha512_init(dst_func_t **funcp) {
1681 REQUIRE(funcp != NULL);
1683 *funcp = &hmacsha512_functions;
1684 return (ISC_R_SUCCESS);