hmacsha.c 40.7 KB
Newer Older
1
/*
2
 * Copyright (C) 2005-2007, 2009, 2011-2016  Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
 */

Mark Andrews's avatar
Mark Andrews committed
9
/* $Id$ */
10
11
12
13
14
15
16
17
18
19
20

/*
 * This code implements the HMAC-SHA1, HMAC-SHA224, HMAC-SHA256, HMAC-SHA384
 * and HMAC-SHA512 keyed hash algorithm described in RFC 2104 and
 * draft-ietf-dnsext-tsig-sha-01.txt.
 */

#include "config.h"

#include <isc/assertions.h>
#include <isc/hmacsha.h>
Francis Dupont's avatar
Francis Dupont committed
21
#include <isc/platform.h>
Evan Hunt's avatar
Evan Hunt committed
22
#include <isc/safe.h>
23
24
25
26
27
28
#include <isc/sha1.h>
#include <isc/sha2.h>
#include <isc/string.h>
#include <isc/types.h>
#include <isc/util.h>

Evan Hunt's avatar
Evan Hunt committed
29
#if PKCS11CRYPTO
30
31
#include <pk11/internal.h>
#include <pk11/pk11.h>
Evan Hunt's avatar
Evan Hunt committed
32
#endif
Francis Dupont's avatar
Francis Dupont committed
33

Evan Hunt's avatar
Evan Hunt committed
34
#ifdef ISC_PLATFORM_OPENSSLHASH
Francis Dupont's avatar
Francis Dupont committed
35
36
37
38
void
isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
		  unsigned int len)
{
39
40
41
42
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
				(int) len, EVP_sha1()) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
43
	HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha1());
44
#endif
Francis Dupont's avatar
Francis Dupont committed
45
46
47
48
49
50
51
52
53
54
55
}

void
isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
	HMAC_CTX_cleanup(ctx);
}

void
isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
56
57
58
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
59
	HMAC_Update(ctx, buf, (int) len);
60
#endif
Francis Dupont's avatar
Francis Dupont committed
61
62
63
64
65
66
67
68
}

void
isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);

69
70
71
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
72
	HMAC_Final(ctx, newdigest, NULL);
73
#endif
Francis Dupont's avatar
Francis Dupont committed
74
	HMAC_CTX_cleanup(ctx);
75
	memmove(digest, newdigest, len);
Francis Dupont's avatar
Francis Dupont committed
76
77
78
79
80
81
82
	memset(newdigest, 0, sizeof(newdigest));
}

void
isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
		    unsigned int len)
{
83
84
85
86
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
				(int) len, EVP_sha224()) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
87
	HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha224());
88
#endif
Francis Dupont's avatar
Francis Dupont committed
89
90
91
92
93
94
95
96
97
98
99
}

void
isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
	HMAC_CTX_cleanup(ctx);
}

void
isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
100
101
102
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
103
	HMAC_Update(ctx, buf, (int) len);
104
#endif
Francis Dupont's avatar
Francis Dupont committed
105
106
107
108
109
110
111
112
}

void
isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);

113
114
115
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
116
	HMAC_Final(ctx, newdigest, NULL);
117
#endif
Francis Dupont's avatar
Francis Dupont committed
118
	HMAC_CTX_cleanup(ctx);
119
	memmove(digest, newdigest, len);
Francis Dupont's avatar
Francis Dupont committed
120
121
122
123
124
125
126
	memset(newdigest, 0, sizeof(newdigest));
}

void
isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
		    unsigned int len)
{
127
128
129
130
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
				(int) len, EVP_sha256()) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
131
	HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha256());
132
#endif
Francis Dupont's avatar
Francis Dupont committed
133
134
135
136
137
138
139
140
141
142
143
}

void
isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
	HMAC_CTX_cleanup(ctx);
}

void
isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
144
145
146
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
147
	HMAC_Update(ctx, buf, (int) len);
148
#endif
Francis Dupont's avatar
Francis Dupont committed
149
150
151
152
153
154
155
156
}

void
isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);

157
158
159
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
160
	HMAC_Final(ctx, newdigest, NULL);
161
#endif
Francis Dupont's avatar
Francis Dupont committed
162
	HMAC_CTX_cleanup(ctx);
163
	memmove(digest, newdigest, len);
Francis Dupont's avatar
Francis Dupont committed
164
165
166
167
168
169
170
	memset(newdigest, 0, sizeof(newdigest));
}

void
isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
		    unsigned int len)
{
171
172
173
174
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
				(int) len, EVP_sha384()) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
175
	HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha384());
176
#endif
Francis Dupont's avatar
Francis Dupont committed
177
178
179
180
181
182
183
184
185
186
187
}

void
isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
	HMAC_CTX_cleanup(ctx);
}

void
isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
188
189
190
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
191
	HMAC_Update(ctx, buf, (int) len);
192
#endif
Francis Dupont's avatar
Francis Dupont committed
193
194
195
196
197
198
199
200
}

void
isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);

201
202
203
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
204
	HMAC_Final(ctx, newdigest, NULL);
205
#endif
Francis Dupont's avatar
Francis Dupont committed
206
	HMAC_CTX_cleanup(ctx);
207
	memmove(digest, newdigest, len);
Francis Dupont's avatar
Francis Dupont committed
208
209
210
211
212
213
214
	memset(newdigest, 0, sizeof(newdigest));
}

void
isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
		    unsigned int len)
{
215
216
217
218
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
				(int) len, EVP_sha512()) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
219
	HMAC_Init(ctx, (const void *) key, (int) len, EVP_sha512());
220
#endif
Francis Dupont's avatar
Francis Dupont committed
221
222
223
224
225
226
227
228
229
230
231
}

void
isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
	HMAC_CTX_cleanup(ctx);
}

void
isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
232
233
234
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Update(ctx, buf, (int) len) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
235
	HMAC_Update(ctx, buf, (int) len);
236
#endif
Francis Dupont's avatar
Francis Dupont committed
237
238
239
240
241
242
243
244
}

void
isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);

245
246
247
#ifdef HMAC_RETURN_INT
	RUNTIME_CHECK(HMAC_Final(ctx, newdigest, NULL) == 1);
#else
Francis Dupont's avatar
Francis Dupont committed
248
	HMAC_Final(ctx, newdigest, NULL);
249
#endif
Francis Dupont's avatar
Francis Dupont committed
250
	HMAC_CTX_cleanup(ctx);
251
	memmove(digest, newdigest, len);
Francis Dupont's avatar
Francis Dupont committed
252
253
254
	memset(newdigest, 0, sizeof(newdigest));
}

Evan Hunt's avatar
Evan Hunt committed
255
256
#elif PKCS11CRYPTO

257
258
259
260
261
262
263
264
265
266
267
268
269
270
#if defined(PK11_SHA_1_HMAC_REPLACE) || \
    defined(PK11_SHA224_HMAC_REPLACE) || \
    defined(PK11_SHA256_HMAC_REPLACE) || \
    defined(PK11_SHA384_HMAC_REPLACE) || \
    defined(PK11_SHA512_HMAC_REPLACE)
#define IPAD 0x36
#define OPAD 0x5C
#endif

#if !defined(PK11_SHA_1_HMAC_REPLACE) && \
    !defined(PK11_SHA224_HMAC_REPLACE) && \
    !defined(PK11_SHA256_HMAC_REPLACE) && \
    !defined(PK11_SHA384_HMAC_REPLACE) && \
    !defined(PK11_SHA512_HMAC_REPLACE)
Evan Hunt's avatar
Evan Hunt committed
271
272
static CK_BBOOL truevalue = TRUE;
static CK_BBOOL falsevalue = FALSE;
273
#endif
Evan Hunt's avatar
Evan Hunt committed
274

275
#ifndef PK11_SHA_1_HMAC_REPLACE
Evan Hunt's avatar
Evan Hunt committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
void
isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
		 unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA_1_HMAC, NULL, 0 };
	CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
	CK_KEY_TYPE keyType = CKK_SHA_1_HMAC;
	CK_ATTRIBUTE keyTemplate[] =
	{
		{ CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
		{ CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
		{ CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
		{ CKA_VALUE, NULL, (CK_ULONG) len }
	};

	DE_CONST(key, keyTemplate[5].pValue);
295
296
	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
Evan Hunt's avatar
Evan Hunt committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
	ctx->object = CK_INVALID_HANDLE;
	PK11_FATALCHECK(pkcs_C_CreateObject,
			(ctx->session, keyTemplate,
			 (CK_ULONG) 6, &ctx->object));
	INSIST(ctx->object != CK_INVALID_HANDLE);
	PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
}

void
isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
	CK_BYTE garbage[ISC_SHA1_DIGESTLENGTH];
	CK_ULONG len = ISC_SHA1_DIGESTLENGTH;

	if (ctx->handle == NULL)
		return;
	(void) pkcs_C_SignFinal(ctx->session, garbage, &len);
	memset(garbage, 0, sizeof(garbage));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
}

void
isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_SignUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA1_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA1_DIGESTLENGTH;

	REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
345
	memmove(digest, newdigest, len);
Evan Hunt's avatar
Evan Hunt committed
346
347
	memset(newdigest, 0, sizeof(newdigest));
}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#else
void
isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
		  unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA_1, NULL, 0 };
	unsigned char ipad[ISC_SHA1_BLOCK_LENGTH];
	unsigned int i;

	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
	RUNTIME_CHECK((ctx->key = pk11_mem_get(ISC_SHA1_BLOCK_LENGTH))
		      != NULL);
	if (len > ISC_SHA1_BLOCK_LENGTH) {
		CK_BYTE_PTR kPart;
		CK_ULONG kl;

		PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
		DE_CONST(key, kPart);
		PK11_FATALCHECK(pkcs_C_DigestUpdate,
				(ctx->session, kPart, (CK_ULONG) len));
		kl = ISC_SHA1_DIGESTLENGTH;
		PK11_FATALCHECK(pkcs_C_DigestFinal,
				(ctx->session, (CK_BYTE_PTR) ctx->key, &kl));
	} else
		memmove(ctx->key, key, len);
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	memset(ipad, IPAD, ISC_SHA1_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
		ipad[i] ^= ctx->key[i];
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, ipad,
			 (CK_ULONG) ISC_SHA1_BLOCK_LENGTH));
}

void
isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
	if (ctx->key != NULL)
		pk11_mem_put(ctx->key, ISC_SHA1_BLOCK_LENGTH);
	ctx->key = NULL;
	isc_sha1_invalidate(ctx);
}
Evan Hunt's avatar
Evan Hunt committed
391

392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
void
isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
		    unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA1_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA1_DIGESTLENGTH;
	CK_MECHANISM mech = { CKM_SHA_1, NULL, 0 };
	CK_BYTE opad[ISC_SHA1_BLOCK_LENGTH];
	unsigned int i;

	REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	memset(opad, OPAD, ISC_SHA1_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
		opad[i] ^= ctx->key[i];
	pk11_mem_put(ctx->key, ISC_SHA1_BLOCK_LENGTH);
	ctx->key = NULL;
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, opad,
			 (CK_ULONG) ISC_SHA1_BLOCK_LENGTH));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, (CK_BYTE_PTR) newdigest, psl));
	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	pk11_return_session(ctx);
	memmove(digest, newdigest, len);
	memset(newdigest, 0, sizeof(newdigest));
}
#endif

#ifndef PK11_SHA224_HMAC_REPLACE
Evan Hunt's avatar
Evan Hunt committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
void
isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA224_HMAC, NULL, 0 };
	CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
	CK_KEY_TYPE keyType = CKK_SHA224_HMAC;
	CK_ATTRIBUTE keyTemplate[] =
	{
		{ CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
		{ CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
		{ CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
		{ CKA_VALUE, NULL, (CK_ULONG) len }
	};

	DE_CONST(key, keyTemplate[5].pValue);
454
455
	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
Evan Hunt's avatar
Evan Hunt committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
	ctx->object = CK_INVALID_HANDLE;
	PK11_FATALCHECK(pkcs_C_CreateObject,
			(ctx->session, keyTemplate,
			 (CK_ULONG) 6, &ctx->object));
	INSIST(ctx->object != CK_INVALID_HANDLE);
	PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
}

void
isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
	CK_BYTE garbage[ISC_SHA224_DIGESTLENGTH];
	CK_ULONG len = ISC_SHA224_DIGESTLENGTH;

	if (ctx->handle == NULL)
		return;
	(void) pkcs_C_SignFinal(ctx->session, garbage, &len);
	memset(garbage, 0, sizeof(garbage));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
}

void
isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_SignUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA224_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA224_DIGESTLENGTH;

	REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
504
	memmove(digest, newdigest, len);
Evan Hunt's avatar
Evan Hunt committed
505
506
	memset(newdigest, 0, sizeof(newdigest));
}
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#else
void
isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA224, NULL, 0 };
	unsigned char ipad[ISC_SHA224_BLOCK_LENGTH];
	unsigned int i;

	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
	RUNTIME_CHECK((ctx->key = pk11_mem_get(ISC_SHA224_BLOCK_LENGTH))
		      != NULL);
	if (len > ISC_SHA224_BLOCK_LENGTH) {
		CK_BYTE_PTR kPart;
		CK_ULONG kl;

		PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
		DE_CONST(key, kPart);
		PK11_FATALCHECK(pkcs_C_DigestUpdate,
				(ctx->session, kPart, (CK_ULONG) len));
		kl = ISC_SHA224_DIGESTLENGTH;
		PK11_FATALCHECK(pkcs_C_DigestFinal,
				(ctx->session, (CK_BYTE_PTR) ctx->key, &kl));
	} else
		memmove(ctx->key, key, len);
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	memset(ipad, IPAD, ISC_SHA224_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
		ipad[i] ^= ctx->key[i];
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, ipad,
			 (CK_ULONG) ISC_SHA224_BLOCK_LENGTH));
}

void
isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
	if (ctx->key != NULL)
		pk11_mem_put(ctx->key, ISC_SHA224_BLOCK_LENGTH);
	ctx->key = NULL;
	isc_sha224_invalidate(ctx);
}

void
isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA224_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA224_DIGESTLENGTH;
	CK_MECHANISM mech = { CKM_SHA224, NULL, 0 };
	CK_BYTE opad[ISC_SHA224_BLOCK_LENGTH];
	unsigned int i;

	REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	memset(opad, OPAD, ISC_SHA224_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
		opad[i] ^= ctx->key[i];
	pk11_mem_put(ctx->key, ISC_SHA224_BLOCK_LENGTH);
	ctx->key = NULL;
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, opad,
			 (CK_ULONG) ISC_SHA224_BLOCK_LENGTH));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, (CK_BYTE_PTR) newdigest, psl));
	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	pk11_return_session(ctx);
	memmove(digest, newdigest, len);
	memset(newdigest, 0, sizeof(newdigest));
}
#endif
Evan Hunt's avatar
Evan Hunt committed
592

593
#ifndef PK11_SHA256_HMAC_REPLACE
Evan Hunt's avatar
Evan Hunt committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
void
isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA256_HMAC, NULL, 0 };
	CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
	CK_KEY_TYPE keyType = CKK_SHA256_HMAC;
	CK_ATTRIBUTE keyTemplate[] =
	{
		{ CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
		{ CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
		{ CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
		{ CKA_VALUE, NULL, (CK_ULONG) len }
	};

	DE_CONST(key, keyTemplate[5].pValue);
613
614
	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
Evan Hunt's avatar
Evan Hunt committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
	ctx->object = CK_INVALID_HANDLE;
	PK11_FATALCHECK(pkcs_C_CreateObject,
			(ctx->session, keyTemplate,
			 (CK_ULONG) 6, &ctx->object));
	INSIST(ctx->object != CK_INVALID_HANDLE);
	PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
}

void
isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
	CK_BYTE garbage[ISC_SHA256_DIGESTLENGTH];
	CK_ULONG len = ISC_SHA256_DIGESTLENGTH;

	if (ctx->handle == NULL)
		return;
	(void) pkcs_C_SignFinal(ctx->session, garbage, &len);
	memset(garbage, 0, sizeof(garbage));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
}

void
isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_SignUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA256_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA256_DIGESTLENGTH;

	REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
663
	memmove(digest, newdigest, len);
Evan Hunt's avatar
Evan Hunt committed
664
665
	memset(newdigest, 0, sizeof(newdigest));
}
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#else
void
isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA256, NULL, 0 };
	unsigned char ipad[ISC_SHA256_BLOCK_LENGTH];
	unsigned int i;

	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
	RUNTIME_CHECK((ctx->key = pk11_mem_get(ISC_SHA256_BLOCK_LENGTH))
		      != NULL);
	if (len > ISC_SHA256_BLOCK_LENGTH) {
		CK_BYTE_PTR kPart;
		CK_ULONG kl;

		PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
		DE_CONST(key, kPart);
		PK11_FATALCHECK(pkcs_C_DigestUpdate,
				(ctx->session, kPart, (CK_ULONG) len));
		kl = ISC_SHA256_DIGESTLENGTH;
		PK11_FATALCHECK(pkcs_C_DigestFinal,
				(ctx->session, (CK_BYTE_PTR) ctx->key, &kl));
	} else
		memmove(ctx->key, key, len);
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	memset(ipad, IPAD, ISC_SHA256_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
		ipad[i] ^= ctx->key[i];
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, ipad,
			 (CK_ULONG) ISC_SHA256_BLOCK_LENGTH));
}

void
isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
	if (ctx->key != NULL)
		pk11_mem_put(ctx->key, ISC_SHA256_BLOCK_LENGTH);
	ctx->key = NULL;
	isc_sha256_invalidate(ctx);
}

void
isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA256_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA256_DIGESTLENGTH;
	CK_MECHANISM mech = { CKM_SHA256, NULL, 0 };
	CK_BYTE opad[ISC_SHA256_BLOCK_LENGTH];
	unsigned int i;

	REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	memset(opad, OPAD, ISC_SHA256_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
		opad[i] ^= ctx->key[i];
	pk11_mem_put(ctx->key, ISC_SHA256_BLOCK_LENGTH);
	ctx->key = NULL;
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, opad,
			 (CK_ULONG) ISC_SHA256_BLOCK_LENGTH));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, (CK_BYTE_PTR) newdigest, psl));
	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	pk11_return_session(ctx);
	memmove(digest, newdigest, len);
	memset(newdigest, 0, sizeof(newdigest));
}
#endif
Evan Hunt's avatar
Evan Hunt committed
751

752
#ifndef PK11_SHA384_HMAC_REPLACE
Evan Hunt's avatar
Evan Hunt committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
void
isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA384_HMAC, NULL, 0 };
	CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
	CK_KEY_TYPE keyType = CKK_SHA384_HMAC;
	CK_ATTRIBUTE keyTemplate[] =
	{
		{ CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
		{ CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
		{ CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
		{ CKA_VALUE, NULL, (CK_ULONG) len }
	};

	DE_CONST(key, keyTemplate[5].pValue);
772
773
	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
Evan Hunt's avatar
Evan Hunt committed
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
	ctx->object = CK_INVALID_HANDLE;
	PK11_FATALCHECK(pkcs_C_CreateObject,
			(ctx->session, keyTemplate,
			 (CK_ULONG) 6, &ctx->object));
	INSIST(ctx->object != CK_INVALID_HANDLE);
	PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
}

void
isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
	CK_BYTE garbage[ISC_SHA384_DIGESTLENGTH];
	CK_ULONG len = ISC_SHA384_DIGESTLENGTH;

	if (ctx->handle == NULL)
		return;
	(void) pkcs_C_SignFinal(ctx->session, garbage, &len);
	memset(garbage, 0, sizeof(garbage));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
}

void
isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_SignUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA384_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA384_DIGESTLENGTH;

	REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
822
	memmove(digest, newdigest, len);
Evan Hunt's avatar
Evan Hunt committed
823
824
	memset(newdigest, 0, sizeof(newdigest));
}
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#else
void
isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA384, NULL, 0 };
	unsigned char ipad[ISC_SHA384_BLOCK_LENGTH];
	unsigned int i;

	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
	RUNTIME_CHECK((ctx->key = pk11_mem_get(ISC_SHA384_BLOCK_LENGTH))
		      != NULL);
	if (len > ISC_SHA384_BLOCK_LENGTH) {
		CK_BYTE_PTR kPart;
		CK_ULONG kl;

		PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
		DE_CONST(key, kPart);
		PK11_FATALCHECK(pkcs_C_DigestUpdate,
				(ctx->session, kPart, (CK_ULONG) len));
		kl = ISC_SHA384_DIGESTLENGTH;
		PK11_FATALCHECK(pkcs_C_DigestFinal,
				(ctx->session, (CK_BYTE_PTR) ctx->key, &kl));
	} else
		memmove(ctx->key, key, len);
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	memset(ipad, IPAD, ISC_SHA384_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
		ipad[i] ^= ctx->key[i];
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, ipad,
			 (CK_ULONG) ISC_SHA384_BLOCK_LENGTH));
}

void
isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
	if (ctx->key != NULL)
		pk11_mem_put(ctx->key, ISC_SHA384_BLOCK_LENGTH);
	ctx->key = NULL;
	isc_sha384_invalidate(ctx);
}

void
isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA384_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA384_DIGESTLENGTH;
	CK_MECHANISM mech = { CKM_SHA384, NULL, 0 };
	CK_BYTE opad[ISC_SHA384_BLOCK_LENGTH];
	unsigned int i;

	REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	memset(opad, OPAD, ISC_SHA384_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
		opad[i] ^= ctx->key[i];
	pk11_mem_put(ctx->key, ISC_SHA384_BLOCK_LENGTH);
	ctx->key = NULL;
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, opad,
			 (CK_ULONG) ISC_SHA384_BLOCK_LENGTH));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, (CK_BYTE_PTR) newdigest, psl));
	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	pk11_return_session(ctx);
	memmove(digest, newdigest, len);
	memset(newdigest, 0, sizeof(newdigest));
}
#endif
Evan Hunt's avatar
Evan Hunt committed
910

911
#ifndef PK11_SHA512_HMAC_REPLACE
Evan Hunt's avatar
Evan Hunt committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
void
isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA512_HMAC, NULL, 0 };
	CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY;
	CK_KEY_TYPE keyType = CKK_SHA512_HMAC;
	CK_ATTRIBUTE keyTemplate[] =
	{
		{ CKA_CLASS, &keyClass, (CK_ULONG) sizeof(keyClass) },
		{ CKA_KEY_TYPE, &keyType, (CK_ULONG) sizeof(keyType) },
		{ CKA_TOKEN, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_PRIVATE, &falsevalue, (CK_ULONG) sizeof(falsevalue) },
		{ CKA_SIGN, &truevalue, (CK_ULONG) sizeof(truevalue) },
		{ CKA_VALUE, NULL, (CK_ULONG) len }
	};

	DE_CONST(key, keyTemplate[5].pValue);
931
932
	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
Evan Hunt's avatar
Evan Hunt committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
	ctx->object = CK_INVALID_HANDLE;
	PK11_FATALCHECK(pkcs_C_CreateObject,
			(ctx->session, keyTemplate,
			 (CK_ULONG) 6, &ctx->object));
	INSIST(ctx->object != CK_INVALID_HANDLE);
	PK11_FATALCHECK(pkcs_C_SignInit, (ctx->session, &mech, ctx->object));
}

void
isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
	CK_BYTE garbage[ISC_SHA512_DIGESTLENGTH];
	CK_ULONG len = ISC_SHA512_DIGESTLENGTH;

	if (ctx->handle == NULL)
		return;
	(void) pkcs_C_SignFinal(ctx->session, garbage, &len);
	memset(garbage, 0, sizeof(garbage));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
}

void
isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_SignUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA512_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA512_DIGESTLENGTH;

	REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_SignFinal, (ctx->session, newdigest, &psl));
	if (ctx->object != CK_INVALID_HANDLE)
		(void) pkcs_C_DestroyObject(ctx->session, ctx->object);
	ctx->object = CK_INVALID_HANDLE;
	pk11_return_session(ctx);
981
	memmove(digest, newdigest, len);
Evan Hunt's avatar
Evan Hunt committed
982
983
	memset(newdigest, 0, sizeof(newdigest));
}
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
#else
void
isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
		    unsigned int len)
{
	CK_RV rv;
	CK_MECHANISM mech = { CKM_SHA512, NULL, 0 };
	unsigned char ipad[ISC_SHA512_BLOCK_LENGTH];
	unsigned int i;

	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
	RUNTIME_CHECK((ctx->key = pk11_mem_get(ISC_SHA512_BLOCK_LENGTH))
		      != NULL);
	if (len > ISC_SHA512_BLOCK_LENGTH) {
		CK_BYTE_PTR kPart;
		CK_ULONG kl;

		PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
		DE_CONST(key, kPart);
		PK11_FATALCHECK(pkcs_C_DigestUpdate,
				(ctx->session, kPart, (CK_ULONG) len));
		kl = ISC_SHA512_DIGESTLENGTH;
		PK11_FATALCHECK(pkcs_C_DigestFinal,
				(ctx->session, (CK_BYTE_PTR) ctx->key, &kl));
	} else
		memmove(ctx->key, key, len);
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	memset(ipad, IPAD, ISC_SHA512_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
		ipad[i] ^= ctx->key[i];
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, ipad,
			 (CK_ULONG) ISC_SHA512_BLOCK_LENGTH));
}

void
isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
	if (ctx->key != NULL)
		pk11_mem_put(ctx->key, ISC_SHA512_BLOCK_LENGTH);
	ctx->key = NULL;
	isc_sha512_invalidate(ctx);
}

void
isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
		      unsigned int len)
{
	CK_RV rv;
	CK_BYTE_PTR pPart;

	DE_CONST(buf, pPart);
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, pPart, (CK_ULONG) len));
}

void
isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
	CK_RV rv;
	CK_BYTE newdigest[ISC_SHA512_DIGESTLENGTH];
	CK_ULONG psl = ISC_SHA512_DIGESTLENGTH;
	CK_MECHANISM mech = { CKM_SHA512, NULL, 0 };
	CK_BYTE opad[ISC_SHA512_BLOCK_LENGTH];
	unsigned int i;

	REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);

	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	memset(opad, OPAD, ISC_SHA512_BLOCK_LENGTH);
	for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
		opad[i] ^= ctx->key[i];
	pk11_mem_put(ctx->key, ISC_SHA512_BLOCK_LENGTH);
	ctx->key = NULL;
	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, opad,
			 (CK_ULONG) ISC_SHA512_BLOCK_LENGTH));
	PK11_FATALCHECK(pkcs_C_DigestUpdate,
			(ctx->session, (CK_BYTE_PTR) newdigest, psl));
	PK11_FATALCHECK(pkcs_C_DigestFinal, (ctx->session, newdigest, &psl));
	pk11_return_session(ctx);
	memmove(digest, newdigest, len);
	memset(newdigest, 0, sizeof(newdigest));
}
#endif
Evan Hunt's avatar
Evan Hunt committed
1069

Francis Dupont's avatar
Francis Dupont committed
1070
1071
#else

1072
1073
1074
1075
1076
1077
1078
1079
#define IPAD 0x36
#define OPAD 0x5C

/*
 * Start HMAC-SHA1 process.  Initialize an sha1 context and digest the key.
 */
void
isc_hmacsha1_init(isc_hmacsha1_t *ctx, const unsigned char *key,
Mark Andrews's avatar
Mark Andrews committed
1080
		  unsigned int len)
1081
{
1082
	unsigned char ipad[ISC_SHA1_BLOCK_LENGTH];
Mark Andrews's avatar
Mark Andrews committed
1083
	unsigned int i;
1084
1085
1086
1087
1088
1089
1090
1091

	memset(ctx->key, 0, sizeof(ctx->key));
	if (len > sizeof(ctx->key)) {
		isc_sha1_t sha1ctx;
		isc_sha1_init(&sha1ctx);
		isc_sha1_update(&sha1ctx, key, len);
		isc_sha1_final(&sha1ctx, ctx->key);
	} else
1092
		memmove(ctx->key, key, len);
1093
1094
1095

	isc_sha1_init(&ctx->sha1ctx);
	memset(ipad, IPAD, sizeof(ipad));
1096
	for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
1097
1098
1099
1100
1101
1102
1103
		ipad[i] ^= ctx->key[i];
	isc_sha1_update(&ctx->sha1ctx, ipad, sizeof(ipad));
}

void
isc_hmacsha1_invalidate(isc_hmacsha1_t *ctx) {
	isc_sha1_invalidate(&ctx->sha1ctx);
1104
	memset(ctx, 0, sizeof(*ctx));
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
}

/*
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
 */
void
isc_hmacsha1_update(isc_hmacsha1_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
	isc_sha1_update(&ctx->sha1ctx, buf, len);
}

/*
 * Compute signature - finalize SHA1 operation and reapply SHA1.
 */
void
isc_hmacsha1_sign(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
1123
	unsigned char opad[ISC_SHA1_BLOCK_LENGTH];
1124
	unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];
Mark Andrews's avatar
Mark Andrews committed
1125
	unsigned int i;
1126
1127
1128
1129
1130

	REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
	isc_sha1_final(&ctx->sha1ctx, newdigest);

	memset(opad, OPAD, sizeof(opad));
1131
	for (i = 0; i < ISC_SHA1_BLOCK_LENGTH; i++)
1132
1133
1134
1135
1136
1137
1138
		opad[i] ^= ctx->key[i];

	isc_sha1_init(&ctx->sha1ctx);
	isc_sha1_update(&ctx->sha1ctx, opad, sizeof(opad));
	isc_sha1_update(&ctx->sha1ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
	isc_sha1_final(&ctx->sha1ctx, newdigest);
	isc_hmacsha1_invalidate(ctx);
1139
	memmove(digest, newdigest, len);
1140
	memset(newdigest, 0, sizeof(newdigest));
1141
1142
1143
1144
1145
1146
1147
}

/*
 * Start HMAC-SHA224 process.  Initialize an sha224 context and digest the key.
 */
void
isc_hmacsha224_init(isc_hmacsha224_t *ctx, const unsigned char *key,
Mark Andrews's avatar
Mark Andrews committed
1148
		    unsigned int len)
1149
{
1150
	unsigned char ipad[ISC_SHA224_BLOCK_LENGTH];
Mark Andrews's avatar
Mark Andrews committed
1151
	unsigned int i;
1152
1153
1154
1155
1156
1157
1158
1159

	memset(ctx->key, 0, sizeof(ctx->key));
	if (len > sizeof(ctx->key)) {
		isc_sha224_t sha224ctx;
		isc_sha224_init(&sha224ctx);
		isc_sha224_update(&sha224ctx, key, len);
		isc_sha224_final(ctx->key, &sha224ctx);
	} else
1160
		memmove(ctx->key, key, len);
1161
1162
1163

	isc_sha224_init(&ctx->sha224ctx);
	memset(ipad, IPAD, sizeof(ipad));
1164
	for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
1165
1166
1167
1168
1169
1170
		ipad[i] ^= ctx->key[i];
	isc_sha224_update(&ctx->sha224ctx, ipad, sizeof(ipad));
}

void
isc_hmacsha224_invalidate(isc_hmacsha224_t *ctx) {
1171
	memset(ctx, 0, sizeof(*ctx));
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
}

/*
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
 */
void
isc_hmacsha224_update(isc_hmacsha224_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
	isc_sha224_update(&ctx->sha224ctx, buf, len);
}

/*
 * Compute signature - finalize SHA224 operation and reapply SHA224.
 */
void
isc_hmacsha224_sign(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
1190
	unsigned char opad[ISC_SHA224_BLOCK_LENGTH];
1191
	unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];
Mark Andrews's avatar
Mark Andrews committed
1192
	unsigned int i;
1193
1194
1195
1196
1197

	REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
	isc_sha224_final(newdigest, &ctx->sha224ctx);

	memset(opad, OPAD, sizeof(opad));
1198
	for (i = 0; i < ISC_SHA224_BLOCK_LENGTH; i++)
1199
1200
1201
1202
1203
1204
		opad[i] ^= ctx->key[i];

	isc_sha224_init(&ctx->sha224ctx);
	isc_sha224_update(&ctx->sha224ctx, opad, sizeof(opad));
	isc_sha224_update(&ctx->sha224ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
	isc_sha224_final(newdigest, &ctx->sha224ctx);
1205
	memmove(digest, newdigest, len);
1206
	memset(newdigest, 0, sizeof(newdigest));
1207
1208
1209
1210
1211
1212
1213
}

/*
 * Start HMAC-SHA256 process.  Initialize an sha256 context and digest the key.
 */
void
isc_hmacsha256_init(isc_hmacsha256_t *ctx, const unsigned char *key,
Mark Andrews's avatar
Mark Andrews committed
1214
		    unsigned int len)
1215
{
1216
	unsigned char ipad[ISC_SHA256_BLOCK_LENGTH];
Mark Andrews's avatar
Mark Andrews committed
1217
	unsigned int i;
1218
1219
1220
1221
1222
1223
1224
1225

	memset(ctx->key, 0, sizeof(ctx->key));
	if (len > sizeof(ctx->key)) {
		isc_sha256_t sha256ctx;
		isc_sha256_init(&sha256ctx);
		isc_sha256_update(&sha256ctx, key, len);
		isc_sha256_final(ctx->key, &sha256ctx);
	} else
1226
		memmove(ctx->key, key, len);
1227
1228
1229

	isc_sha256_init(&ctx->sha256ctx);
	memset(ipad, IPAD, sizeof(ipad));
1230
	for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
1231
1232
1233
1234
1235
1236
		ipad[i] ^= ctx->key[i];
	isc_sha256_update(&ctx->sha256ctx, ipad, sizeof(ipad));
}

void
isc_hmacsha256_invalidate(isc_hmacsha256_t *ctx) {
1237
	memset(ctx, 0, sizeof(*ctx));
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
}

/*
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
 */
void
isc_hmacsha256_update(isc_hmacsha256_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
	isc_sha256_update(&ctx->sha256ctx, buf, len);
}

/*
 * Compute signature - finalize SHA256 operation and reapply SHA256.
 */
void
isc_hmacsha256_sign(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
1256
	unsigned char opad[ISC_SHA256_BLOCK_LENGTH];
1257
	unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];
Mark Andrews's avatar
Mark Andrews committed
1258
	unsigned int i;
1259
1260
1261
1262
1263

	REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
	isc_sha256_final(newdigest, &ctx->sha256ctx);

	memset(opad, OPAD, sizeof(opad));
1264
	for (i = 0; i < ISC_SHA256_BLOCK_LENGTH; i++)
1265
1266
1267
1268
1269
1270
		opad[i] ^= ctx->key[i];

	isc_sha256_init(&ctx->sha256ctx);
	isc_sha256_update(&ctx->sha256ctx, opad, sizeof(opad));
	isc_sha256_update(&ctx->sha256ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
	isc_sha256_final(newdigest, &ctx->sha256ctx);
1271
	memmove(digest, newdigest, len);
1272
	memset(newdigest, 0, sizeof(newdigest));
1273
1274
1275
1276
1277
1278
1279
}

/*
 * Start HMAC-SHA384 process.  Initialize an sha384 context and digest the key.
 */
void
isc_hmacsha384_init(isc_hmacsha384_t *ctx, const unsigned char *key,
Mark Andrews's avatar
Mark Andrews committed
1280
		    unsigned int len)
1281
{
1282
	unsigned char ipad[ISC_SHA384_BLOCK_LENGTH];
Mark Andrews's avatar
Mark Andrews committed
1283
	unsigned int i;
1284
1285
1286
1287
1288
1289
1290
1291

	memset(ctx->key, 0, sizeof(ctx->key));
	if (len > sizeof(ctx->key)) {
		isc_sha384_t sha384ctx;
		isc_sha384_init(&sha384ctx);
		isc_sha384_update(&sha384ctx, key, len);
		isc_sha384_final(ctx->key, &sha384ctx);
	} else
1292
		memmove(ctx->key, key, len);
1293
1294
1295

	isc_sha384_init(&ctx->sha384ctx);
	memset(ipad, IPAD, sizeof(ipad));
1296
	for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
1297
1298
1299
1300
1301
1302
		ipad[i] ^= ctx->key[i];
	isc_sha384_update(&ctx->sha384ctx, ipad, sizeof(ipad));
}

void
isc_hmacsha384_invalidate(isc_hmacsha384_t *ctx) {
1303
	memset(ctx, 0, sizeof(*ctx));
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
}

/*
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
 */
void
isc_hmacsha384_update(isc_hmacsha384_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
	isc_sha384_update(&ctx->sha384ctx, buf, len);
}

/*
 * Compute signature - finalize SHA384 operation and reapply SHA384.
 */
void
isc_hmacsha384_sign(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
1322
	unsigned char opad[ISC_SHA384_BLOCK_LENGTH];
1323
	unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];
Mark Andrews's avatar
Mark Andrews committed
1324
	unsigned int i;
1325
1326
1327
1328
1329

	REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
	isc_sha384_final(newdigest, &ctx->sha384ctx);

	memset(opad, OPAD, sizeof(opad));
1330
	for (i = 0; i < ISC_SHA384_BLOCK_LENGTH; i++)
1331
1332
1333
1334
1335
1336
		opad[i] ^= ctx->key[i];

	isc_sha384_init(&ctx->sha384ctx);
	isc_sha384_update(&ctx->sha384ctx, opad, sizeof(opad));
	isc_sha384_update(&ctx->sha384ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
	isc_sha384_final(newdigest, &ctx->sha384ctx);
1337
	memmove(digest, newdigest, len);
1338
	memset(newdigest, 0, sizeof(newdigest));
1339
1340
1341
1342
1343
1344
1345
}

/*
 * Start HMAC-SHA512 process.  Initialize an sha512 context and digest the key.
 */
void
isc_hmacsha512_init(isc_hmacsha512_t *ctx, const unsigned char *key,
Mark Andrews's avatar
Mark Andrews committed
1346
		    unsigned int len)
1347
{
1348
	unsigned char ipad[ISC_SHA512_BLOCK_LENGTH];
Mark Andrews's avatar
Mark Andrews committed
1349
	unsigned int i;
1350
1351
1352
1353
1354
1355
1356
1357

	memset(ctx->key, 0, sizeof(ctx->key));
	if (len > sizeof(ctx->key)) {
		isc_sha512_t sha512ctx;
		isc_sha512_init(&sha512ctx);
		isc_sha512_update(&sha512ctx, key, len);
		isc_sha512_final(ctx->key, &sha512ctx);
	} else
1358
		memmove(ctx->key, key, len);
1359
1360
1361

	isc_sha512_init(&ctx->sha512ctx);
	memset(ipad, IPAD, sizeof(ipad));
1362
	for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
1363
1364
1365
1366
1367
1368
		ipad[i] ^= ctx->key[i];
	isc_sha512_update(&ctx->sha512ctx, ipad, sizeof(ipad));
}

void
isc_hmacsha512_invalidate(isc_hmacsha512_t *ctx) {
1369
	memset(ctx, 0, sizeof(*ctx));
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
}

/*
 * Update context to reflect the concatenation of another buffer full
 * of bytes.
 */
void
isc_hmacsha512_update(isc_hmacsha512_t *ctx, const unsigned char *buf,
		   unsigned int len)
{
	isc_sha512_update(&ctx->sha512ctx, buf, len);
}

/*
 * Compute signature - finalize SHA512 operation and reapply SHA512.
 */
void
isc_hmacsha512_sign(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
1388
	unsigned char opad[ISC_SHA512_BLOCK_LENGTH];
1389
	unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];
Mark Andrews's avatar
Mark Andrews committed
1390
	unsigned int i;
1391
1392
1393
1394
1395

	REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
	isc_sha512_final(newdigest, &ctx->sha512ctx);

	memset(opad, OPAD, sizeof(opad));
1396
	for (i = 0; i < ISC_SHA512_BLOCK_LENGTH; i++)
1397
1398
1399
1400
1401
1402
		opad[i] ^= ctx->key[i];

	isc_sha512_init(&ctx->sha512ctx);
	isc_sha512_update(&ctx->sha512ctx, opad, sizeof(opad));
	isc_sha512_update(&ctx->sha512ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
	isc_sha512_final(newdigest, &ctx->sha512ctx);
1403
	memmove(digest, newdigest, len);
1404
	memset(newdigest, 0, sizeof(newdigest));
1405
}
Francis Dupont's avatar
Francis Dupont committed
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
#endif /* !ISC_PLATFORM_OPENSSLHASH */

/*
 * Verify signature - finalize SHA1 operation and reapply SHA1, then
 * compare to the supplied digest.
 */
isc_boolean_t
isc_hmacsha1_verify(isc_hmacsha1_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA1_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA1_DIGESTLENGTH);
	isc_hmacsha1_sign(ctx, newdigest, ISC_SHA1_DIGESTLENGTH);
1418
	return (isc_safe_memequal(digest, newdigest, len));
Francis Dupont's avatar
Francis Dupont committed
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
}

/*
 * Verify signature - finalize SHA224 operation and reapply SHA224, then
 * compare to the supplied digest.
 */
isc_boolean_t
isc_hmacsha224_verify(isc_hmacsha224_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA224_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA224_DIGESTLENGTH);
	isc_hmacsha224_sign(ctx, newdigest, ISC_SHA224_DIGESTLENGTH);
1431
	return (isc_safe_memequal(digest, newdigest, len));
Francis Dupont's avatar
Francis Dupont committed
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
}

/*
 * Verify signature - finalize SHA256 operation and reapply SHA256, then
 * compare to the supplied digest.
 */
isc_boolean_t
isc_hmacsha256_verify(isc_hmacsha256_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA256_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA256_DIGESTLENGTH);
	isc_hmacsha256_sign(ctx, newdigest, ISC_SHA256_DIGESTLENGTH);
1444
	return (isc_safe_memequal(digest, newdigest, len));
Francis Dupont's avatar
Francis Dupont committed
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
}

/*
 * Verify signature - finalize SHA384 operation and reapply SHA384, then
 * compare to the supplied digest.
 */
isc_boolean_t
isc_hmacsha384_verify(isc_hmacsha384_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA384_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA384_DIGESTLENGTH);
	isc_hmacsha384_sign(ctx, newdigest, ISC_SHA384_DIGESTLENGTH);
1457
	return (isc_safe_memequal(digest, newdigest, len));
Francis Dupont's avatar
Francis Dupont committed
1458
}
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469

/*
 * Verify signature - finalize SHA512 operation and reapply SHA512, then
 * compare to the supplied digest.
 */
isc_boolean_t
isc_hmacsha512_verify(isc_hmacsha512_t *ctx, unsigned char *digest, size_t len) {
	unsigned char newdigest[ISC_SHA512_DIGESTLENGTH];

	REQUIRE(len <= ISC_SHA512_DIGESTLENGTH);
	isc_hmacsha512_sign(ctx, newdigest, ISC_SHA512_DIGESTLENGTH);
1470
	return (isc_safe_memequal(digest, newdigest, len));
1471
}