Home | History | Annotate | Line # | Download | only in nist_hash_drbg
      1 /*	$NetBSD: nist_hash_drbg.c,v 1.4 2026/05/13 04:26:24 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * This file implements Hash_DRBG, a `deterministic random bit
     34  * generator' (more commonly known in lay terms and in the cryptography
     35  * literature as a pseudorandom bit generator or pseudorandom number
     36  * generator), described in
     37  *
     38  *	Elaine Barker and John Kelsey, `Recommendation for Random
     39  *	Number Generation Using Deterministic Random Bit Generators',
     40  *	NIST SP800-90A Rev. 1, June 2015.
     41  *	https://csrc.nist.gov/pubs/sp/800/90/a/r1/final
     42  *
     43  * This code is meant to work in userland or in kernel.  For a test
     44  * program, compile with -DNIST_HASH_DRBG_MAIN to define a `main'
     45  * function; for verbose debugging output, compile with
     46  * -DNIST_HASH_DRBG_DEBUG, mainly useful if you need to change
     47  * something and have to diagnose what's wrong with the known-answer
     48  * tests.
     49  *
     50  * Test vectors in Hash_DRBG.pdf published by NIST at:
     51  * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/Hash_DRBG.pdf
     52  * https://web.archive.org/web/20250313025236/https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/Hash_DRBG.pdf
     53  */
     54 
     55 #ifdef _KERNEL
     57 #include <sys/cdefs.h>
     58 __KERNEL_RCSID(0, "$NetBSD: nist_hash_drbg.c,v 1.4 2026/05/13 04:26:24 riastradh Exp $");
     59 #endif
     60 
     61 #include <sys/param.h>
     62 #include <sys/types.h>
     63 #include <sys/sha2.h>
     64 
     65 #ifdef _KERNEL
     66 #include <sys/systm.h>			/* memcpy */
     67 #include <lib/libkern/libkern.h>	/* KASSERT */
     68 #define	ASSERT		KASSERT
     69 #else
     70 #include <assert.h>
     71 #include <stdbool.h>
     72 #include <stdio.h>
     73 #include <string.h>
     74 #define	ASSERT		assert
     75 #define	CTASSERT	__CTASSERT
     76 #endif
     77 
     78 #include "nist_hash_drbg.h"
     79 
     80 #define	secret	/* must not use in variable-time operations; should zero */
     81 #define	arraycount(A)	(sizeof(A)/sizeof(A[0]))
     82 
     83 CTASSERT(0 < NIST_HASH_DRBG_RESEED_INTERVAL);
     84 CTASSERT(NIST_HASH_DRBG_RESEED_INTERVAL <= INT_MAX);
     85 CTASSERT(NIST_HASH_DRBG_RESEED_INTERVAL <= ~(~0ull << 48));
     86 
     87 /* Instantiation: SHA-256 */
     88 #define	HASH_LENGTH	SHA256_DIGEST_LENGTH
     89 #define	HASH_CTX	SHA256_CTX
     90 #define	hash_init	SHA256_Init
     91 #define	hash_update	SHA256_Update
     92 #define	hash_final	SHA256_Final
     93 
     94 #define	SEEDLEN_BYTES	NIST_HASH_DRBG_SEEDLEN_BYTES
     95 
     96 struct hvec {
     97 	const void	*hv_base;
     98 	size_t		hv_len;
     99 };
    100 
    101 static void	hashgen(secret uint8_t *, size_t,
    102 		    const secret uint8_t[SEEDLEN_BYTES]);
    103 static void	add8(secret uint8_t *, size_t, const secret uint8_t *, size_t);
    104 static void	hash_df(secret void *, size_t, const struct hvec *, size_t);
    105 static void	hash_df_block(secret void *, uint8_t, uint8_t[4],
    106 		    const struct hvec *, size_t);
    107 
    108 /* 10.1.1 Hash_DRBG */
    110 
    111 int
    112 nist_hash_drbg_destroy(struct nist_hash_drbg *D)
    113 {
    114 
    115 	explicit_memset(D, 0, sizeof(*D));
    116 	D->reseed_counter = UINT_MAX; /* paranoia: make generate fail */
    117 
    118 	/* Always return zero for hysterical raisins.  (XXX) */
    119 	return 0;
    120 }
    121 
    122 /* 10.1.1.2 Instantiation of Hash_DRBG */
    123 
    124 int
    125 nist_hash_drbg_instantiate(secret struct nist_hash_drbg *D,
    126     const secret void *entropy, size_t entropylen,
    127     const void *nonce, size_t noncelen,
    128     const void *personalization, size_t personalizationlen)
    129 {
    130 	/*
    131 	 * 1. seed_material = entropy_input || nonce || personalization_string
    132 	 */
    133 	const struct hvec seed_material[] = {
    134 		{ .hv_base = entropy, .hv_len = entropylen },
    135 		{ .hv_base = nonce, .hv_len = noncelen },
    136 		{ .hv_base = personalization, .hv_len = personalizationlen },
    137 	};
    138 
    139 	/*
    140 	 * 2. seed = Hash_df(seed_material, seedlen)
    141 	 * 3. V = seed
    142 	 */
    143 	CTASSERT(sizeof D->V == SEEDLEN_BYTES);
    144 	hash_df(D->V, sizeof D->V, seed_material, arraycount(seed_material));
    145 
    146 	/* 4. C = Hash_df((0x00 || V), seedlen) */
    147 	const struct hvec hv[] = {
    148 		{ .hv_base = (const uint8_t[]) {0x00}, .hv_len = 1 },
    149 		{ .hv_base = D->V, .hv_len = sizeof D->V },
    150 	};
    151 	CTASSERT(sizeof D->C == SEEDLEN_BYTES);
    152 	hash_df(D->C, sizeof D->C, hv, arraycount(hv));
    153 
    154 	/* 5. reseed_counter = 1 */
    155 	D->reseed_counter = 1;
    156 
    157 	/* Always return zero for hysterical raisins.  (XXX) */
    158 	return 0;
    159 }
    160 
    161 /* 10.1.1.3 Reseeding a Hash_DRBG Instantiation */
    163 
    164 int
    165 nist_hash_drbg_reseed(secret struct nist_hash_drbg *D,
    166     const secret void *entropy, size_t entropylen,
    167     const void *additional, size_t additionallen)
    168 {
    169 	/* 1. seed_material = 0x01 || V || entropy_input || additional_input */
    170 	const struct hvec seed_material[] = {
    171 		{ .hv_base = (const uint8_t[]) {0x01}, .hv_len = 1 },
    172 		{ .hv_base = D->V, .hv_len = sizeof D->V },
    173 		{ .hv_base = entropy, .hv_len = entropylen },
    174 		{ .hv_base = additional, .hv_len = additionallen },
    175 	};
    176 	uint8_t seed[SEEDLEN_BYTES];
    177 
    178 	/*
    179 	 * 2. seed = Hash_df(seed_material, seedlen)
    180 	 * 3. V = seed
    181 	 */
    182 	CTASSERT(sizeof D->V == SEEDLEN_BYTES);
    183 	hash_df(seed, sizeof seed, seed_material, arraycount(seed_material));
    184 	memcpy(D->V, seed, sizeof D->V);
    185 
    186 	/* 3. C = Hash_df((0x00 || V), seedlen) */
    187 	const struct hvec hv[] = {
    188 		{ .hv_base = (const uint8_t[]) {0x00}, .hv_len = 1 },
    189 		{ .hv_base = D->V, .hv_len = sizeof D->V },
    190 	};
    191 	CTASSERT(sizeof D->C == SEEDLEN_BYTES);
    192 	hash_df(D->C, sizeof D->C, hv, arraycount(hv));
    193 
    194 	/* 5. reseed_counter = 1 */
    195 	D->reseed_counter = 1;
    196 
    197 	/* Always return zero for hysterical raisins.  (XXX) */
    198 	return 0;
    199 }
    200 
    201 /* 10.1.1.4 Generating Pseudorandom Bits Using Hash_DRBG */
    203 
    204 int
    205 nist_hash_drbg_generate(secret struct nist_hash_drbg *D,
    206     secret void *output, size_t outputlen,
    207     const void *additional, size_t additionallen)
    208 {
    209 	secret HASH_CTX ctx;
    210 	secret uint8_t H[HASH_LENGTH];
    211 	uint8_t reseed_counter[4];
    212 
    213 	ASSERT(outputlen <= NIST_HASH_DRBG_MAX_REQUEST_BYTES);
    214 
    215 	/*
    216 	 * 1. If reseed_counter > reseed_interval, then return an
    217 	 * indication that a reseed is required.
    218 	 */
    219 	if (D->reseed_counter > NIST_HASH_DRBG_RESEED_INTERVAL)
    220 		return 1;
    221 
    222 	/* 2. If (additional_input != Null), then do: */
    223 	if (additionallen) {
    224 		/* 2.1 w = Hash(0x02 || V || additional_input) */
    225 		secret uint8_t w[HASH_LENGTH];
    226 
    227 		hash_init(&ctx);
    228 		hash_update(&ctx, (const uint8_t[]) {0x02}, 1);
    229 		hash_update(&ctx, D->V, sizeof D->V);
    230 		hash_update(&ctx, additional, additionallen);
    231 		hash_final(w, &ctx);
    232 
    233 		/* 2.2 V = (V + w) mod 2^seedlen */
    234 		add8(D->V, sizeof D->V, w, sizeof w);
    235 
    236 		explicit_memset(w, 0, sizeof w);
    237 	}
    238 
    239 	/* 3. (returned_bits) = Hashgen(requested_number_of_bits, V) */
    240 	hashgen(output, outputlen, D->V);
    241 
    242 	/* 4. H = Hash(0x03 || V) */
    243 	hash_init(&ctx);
    244 	hash_update(&ctx, (const uint8_t[]) {0x03}, 1);
    245 	hash_update(&ctx, D->V, sizeof D->V);
    246 	hash_final(H, &ctx);
    247 
    248 	/* 5. V = (V + H + C + reseed_counter) mod 2^seedlen */
    249 	be32enc(reseed_counter, D->reseed_counter);
    250 	add8(D->V, sizeof D->V, H, sizeof H);
    251 	add8(D->V, sizeof D->V, D->C, sizeof D->C);
    252 	add8(D->V, sizeof D->V, reseed_counter, sizeof reseed_counter);
    253 
    254 	/* 6. reseed_counter = reseed_counter + 1 */
    255 	D->reseed_counter++;
    256 
    257 	explicit_memset(&ctx, 0, sizeof ctx);
    258 	explicit_memset(H, 0, sizeof H);
    259 
    260 	/* 7. Return SUCCESS, ... */
    261 	return 0;
    262 }
    263 
    264 /*
    266  * p := H(V) || H(V + 1) || H(V + 2) || ...
    267  */
    268 static void
    269 hashgen(secret uint8_t *p, size_t n, const secret uint8_t V[SEEDLEN_BYTES])
    270 {
    271 	secret uint8_t data[SEEDLEN_BYTES];
    272 	secret HASH_CTX ctx;
    273 
    274 	/* Save a copy so that we can increment it.  */
    275 	memcpy(data, V, SEEDLEN_BYTES);
    276 
    277 	/* Generate block by block into p directly.  */
    278 	while (HASH_LENGTH <= n) {
    279 		hash_init(&ctx);
    280 		hash_update(&ctx, data, SEEDLEN_BYTES);
    281 		hash_final(p, &ctx);
    282 
    283 		p += HASH_LENGTH;
    284 		n -= HASH_LENGTH;
    285 		add8(data, sizeof data, (const uint8_t[]) {1}, 1);
    286 	}
    287 
    288 	/*
    289 	 * If any partial block requested, generate a full block and
    290 	 * copy the part we need.
    291 	 */
    292 	if (n) {
    293 		secret uint8_t t[HASH_LENGTH];
    294 
    295 		hash_init(&ctx);
    296 		hash_update(&ctx, data, SEEDLEN_BYTES);
    297 		hash_final(t, &ctx);
    298 
    299 		memcpy(p, t, n);
    300 		explicit_memset(t, 0, sizeof t);
    301 	}
    302 
    303 	explicit_memset(data, 0, sizeof data);
    304 	explicit_memset(&ctx, 0, sizeof ctx);
    305 }
    306 
    307 /*
    308  * s := s + a (big-endian, radix-2^8)
    309  */
    310 static void
    311 add8(secret uint8_t *s, size_t slen, const secret uint8_t *a, size_t alen)
    312 {
    313 	const size_t smax = slen - 1, amax = alen - 1;
    314 	size_t i;
    315 	secret unsigned c = 0;
    316 
    317 	/* 2^8 c + s_i := s_i + a_i + c */
    318 	for (i = 0; i < MIN(slen, alen); i++) {
    319 		c += s[smax - i] + a[amax - i];
    320 		s[smax - i] = c & 0xff;
    321 		c >>= 8;
    322 	}
    323 
    324 	/* 2^8 c + s_i := s_i + c */
    325 	for (; i < slen; i++) {
    326 		c += s[smax - i];
    327 		s[smax - i] = c & 0xff;
    328 		c >>= 8;
    329 	}
    330 
    331 	explicit_memset(&c, 0, sizeof c);
    332 }
    333 
    334 /* 10.4.1 Derivation Function Using a Hash Function (Hash_df) */
    336 
    337 static void
    338 hash_df(void *h, size_t hlen, const struct hvec *input, size_t inputlen)
    339 {
    340 	uint8_t *p = h;
    341 	size_t n = hlen;
    342 	uint8_t counter = 1;
    343 	uint8_t hbits[4];
    344 
    345 	ASSERT(hlen <= 255*HASH_LENGTH);
    346 	ASSERT(hlen <= UINT32_MAX/8);
    347 	be32enc(hbits, 8*hlen);
    348 
    349 	while (HASH_LENGTH <= n) {
    350 		hash_df_block(p, counter++, hbits, input, inputlen);
    351 		p += HASH_LENGTH;
    352 		n -= HASH_LENGTH;
    353 	}
    354 
    355 	if (n) {
    356 		secret uint8_t t[HASH_LENGTH];
    357 
    358 		hash_df_block(t, counter, hbits, input, inputlen);
    359 		memcpy(p, t, n);
    360 
    361 		explicit_memset(t, 0, sizeof t);
    362 	}
    363 }
    364 
    365 static void
    366 hash_df_block(secret void *h, uint8_t counter, uint8_t hbits[4],
    367     const struct hvec *input, size_t inputlen)
    368 {
    369 	secret HASH_CTX ctx;
    370 	size_t i;
    371 
    372 	/*
    373 	 * Hash_df Process, step 4.1:
    374 	 * Hash(counter || no_of_bits_to_return || input_string)
    375 	 */
    376 	hash_init(&ctx);
    377 	hash_update(&ctx, &counter, 1);
    378 	hash_update(&ctx, hbits, 4);
    379 	for (i = 0; i < inputlen; i++) {
    380 		if (input[i].hv_len)
    381 			hash_update(&ctx, input[i].hv_base, input[i].hv_len);
    382 	}
    383 	hash_final(h, &ctx);
    384 
    385 	explicit_memset(&ctx, 0, sizeof ctx);
    386 }
    387 
    388 /*
    390  * Known-answer test vectors for Hash_DRBG with SHA-256
    391  */
    392 
    393 /* Hash_DRBG.PDF, p. 190 */
    394 static const uint8_t kat_entropy[3][SEEDLEN_BYTES] = {
    395 	[0] = {
    396 		0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07,
    397 		0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f,
    398 		0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17,
    399 		0x18,0x19,0x1a,0x1b, 0x1c,0x1d,0x1e,0x1f,
    400 		0x20,0x21,0x22,0x23, 0x24,0x25,0x26,0x27,
    401 		0x28,0x29,0x2a,0x2b, 0x2c,0x2d,0x2e,0x2f,
    402 		0x30,0x31,0x32,0x33, 0x34,0x35,0x36,
    403 	},
    404 	[1] = {			/* for reseed1 */
    405 		0x80,0x81,0x82,0x83, 0x84,0x85,0x86,0x87,
    406 		0x88,0x89,0x8a,0x8b, 0x8c,0x8d,0x8e,0x8f,
    407 		0x90,0x91,0x92,0x93, 0x94,0x95,0x96,0x97,
    408 		0x98,0x99,0x9a,0x9b, 0x9c,0x9d,0x9e,0x9f,
    409 		0xa0,0xa1,0xa2,0xa3, 0xa4,0xa5,0xa6,0xa7,
    410 		0xa8,0xa9,0xaa,0xab, 0xac,0xad,0xae,0xaf,
    411 		0xb0,0xb1,0xb2,0xb3, 0xb4,0xb5,0xb6,
    412 	},
    413 	[2] = {			/* for reseed2 */
    414 		0xc0,0xc1,0xc2,0xc3, 0xc4,0xc5,0xc6,0xc7,
    415 		0xc8,0xc9,0xca,0xcb, 0xcc,0xcd,0xce,0xcf,
    416 		0xd0,0xd1,0xd2,0xd3, 0xd4,0xd5,0xd6,0xd7,
    417 		0xd8,0xd9,0xda,0xdb, 0xdc,0xdd,0xde,0xdf,
    418 		0xe0,0xe1,0xe2,0xe3, 0xe4,0xe5,0xe6,0xe7,
    419 		0xe8,0xe9,0xea,0xeb, 0xec,0xed,0xee,0xef,
    420 		0xf0,0xf1,0xf2,0xf3, 0xf4,0xf5,0xf6,
    421 	},
    422 };
    423 
    424 static const uint8_t kat_nonce[] = {
    425 	0x20,0x21,0x22,0x23, 0x24,0x25,0x26,0x27,
    426 };
    427 
    428 static const struct hvec kat_zero = { .hv_base = 0, .hv_len = 0 };
    429 
    430 static const struct hvec kat_personalization = {
    432 	.hv_len = 55,
    433 	.hv_base = (const void *)(const uint8_t[]) { /* p. 208 */
    434 		0x40,0x41,0x42,0x43, 0x44,0x45,0x46,0x47,
    435 		0x48,0x49,0x4a,0x4b, 0x4c,0x4d,0x4e,0x4f,
    436 		0x50,0x51,0x52,0x53, 0x54,0x55,0x56,0x57,
    437 		0x58,0x59,0x5a,0x5b, 0x5c,0x5d,0x5e,0x5f,
    438 		0x60,0x61,0x62,0x63, 0x64,0x65,0x66,0x67,
    439 		0x68,0x69,0x6a,0x6b, 0x6c,0x6d,0x6e,0x6f,
    440 		0x70,0x71,0x72,0x73, 0x74,0x75,0x76,
    441 	},
    442 };
    443 
    444 static const struct hvec *const kat_no_additional[] = {
    445 	[0] = &kat_zero,
    446 	[1] = &kat_zero,
    447 };
    448 
    449 static const struct hvec *const kat_additional[] = {
    450 	[0] = &(const struct hvec) {
    451 		.hv_len = 55,
    452 		.hv_base = (const void *)(const uint8_t[]) {
    453 			0x60,0x61,0x62,0x63, 0x64,0x65,0x66,0x67,
    454 			0x68,0x69,0x6a,0x6b, 0x6c,0x6d,0x6e,0x6f,
    455 			0x70,0x71,0x72,0x73, 0x74,0x75,0x76,0x77,
    456 			0x78,0x79,0x7a,0x7b, 0x7c,0x7d,0x7e,0x7f,
    457 			0x80,0x81,0x82,0x83, 0x84,0x85,0x86,0x87,
    458 			0x88,0x89,0x8a,0x8b, 0x8c,0x8d,0x8e,0x8f,
    459 			0x90,0x91,0x92,0x93, 0x94,0x95,0x96,
    460 		},
    461 	},
    462 	[1] = &(const struct hvec) {
    463 		.hv_len = 55,
    464 		.hv_base = (const void *)(const uint8_t[]) {
    465 			0xa0,0xa1,0xa2,0xa3, 0xa4,0xa5,0xa6,0xa7,
    466 			0xa8,0xa9,0xaa,0xab, 0xac,0xad,0xae,0xaf,
    467 			0xb0,0xb1,0xb2,0xb3, 0xb4,0xb5,0xb6,0xb7,
    468 			0xb8,0xb9,0xba,0xbb, 0xbc,0xbd,0xbe,0xbf,
    469 			0xc0,0xc1,0xc2,0xc3, 0xc4,0xc5,0xc6,0xc7,
    470 			0xc8,0xc9,0xca,0xcb, 0xcc,0xcd,0xce,0xcf,
    471 			0xd0,0xd1,0xd2,0xd3, 0xd4,0xd5,0xd6,
    472 		},
    473 	},
    474 };
    475 
    476 static const struct {
    478 	const struct hvec *personalization;
    479 	const struct hvec *const *additional;
    480 	bool reseed;
    481 	uint8_t C[SEEDLEN_BYTES];
    482 	uint8_t V[3][SEEDLEN_BYTES];
    483 	uint8_t rnd_val[2][64];
    484 } kat[] = {
    485 	[0] = {			/* Hash_DRBG.pdf, p. 190 */
    486 		.personalization = &kat_zero,
    487 		.additional = kat_no_additional,
    488 		.reseed = false,
    489 		.C = {		/* p. 193 */
    490 			0xe1,0x5d,0xe4,0xa8, 0xe3,0xb1,0x41,0x9b,
    491 			0x61,0xd5,0x34,0xf1, 0x5d,0xbd,0x31,0xee,
    492 			0x19,0xec,0x59,0x5f, 0x8b,0x98,0x11,0x1a,
    493 			0x94,0xf5,0x22,0x37, 0xad,0x5d,0x66,0xf0,
    494 			0xcf,0xaa,0xfd,0xdc, 0x90,0x19,0x59,0x02,
    495 			0xe9,0x79,0xf7,0x9b, 0x65,0x35,0x7f,0xea,
    496 			0x85,0x99,0x8e,0x4e, 0x37,0xd2,0xc1,
    497 		},
    498 		.V = {
    499 			[0] = {		/* p. 192 */
    500 				0xab,0x41,0xcd,0xe4, 0x37,0xab,0x8b,0x09,
    501 				0x1c,0xa7,0xc5,0x75, 0x5d,0x10,0xf0,0x11,
    502 				0x0c,0x1d,0xbd,0x46, 0x2f,0x22,0x6c,0xfd,
    503 				0xab,0xfb,0xb0,0x4a, 0x8b,0xcd,0xef,0x95,
    504 				0x16,0x7d,0x84,0xaf, 0x64,0x12,0x8c,0x0d,
    505 				0x71,0xf4,0xd5,0xb8, 0xc0,0xed,0xfb,0xbe,
    506 				0x3d,0xf4,0x04,0x48, 0xd2,0xd8,0xe1,
    507 			},
    508 			[1] = {		/* p. 195 */
    509 				0x8c,0x9f,0xb2,0x8d, 0x1b,0x5c,0xcc,0xa4,
    510 				0x7e,0x7c,0xfa,0x66, 0xba,0xce,0x21,0xff,
    511 				0x26,0x0a,0x16,0xa5, 0xba,0xba,0x7f,0x14,
    512 				0x4e,0x75,0x79,0x36, 0x8e,0x99,0x55,0xbe,
    513 				0xfb,0xe7,0x00,0xee, 0xf8,0x72,0x77,0x6b,
    514 				0x17,0xae,0xff,0xd5, 0x3d,0x76,0xf4,0xe3,
    515 				0xbe,0x65,0xe8,0xc9, 0x4b,0x70,0x8f,
    516 			},
    517 			[2] = {		/* p. 197 */
    518 				0x6d,0xfd,0x97,0x35, 0xff,0x0e,0x0e,0x3f,
    519 				0xe0,0x52,0x2f,0x58, 0x18,0x8b,0x53,0xed,
    520 				0x3f,0xf6,0x70,0x05, 0x46,0x52,0x90,0x44,
    521 				0xb6,0x2b,0xe1,0x7d, 0x1b,0x1c,0x21,0xd0,
    522 				0x91,0xb0,0x89,0xb1, 0x77,0x47,0x95,0xdb,
    523 				0x14,0x22,0xa8,0x6c, 0x95,0x46,0x34,0x80,
    524 				0x76,0xb4,0xb6,0x21, 0xc7,0x2f,0x91,
    525 			},
    526 		},
    527 		.rnd_val = {
    528 			[0] = {
    529 				0x77,0xe0,0x5a,0x0e, 0x7d,0xc7,0x8a,0xb5,
    530 				0xd8,0x93,0x4d,0x5e, 0x93,0xe8,0x2c,0x06,
    531 				0xa0,0x7c,0x04,0xce, 0xe6,0xc9,0xc5,0x30,
    532 				0x45,0xee,0xb4,0x85, 0x87,0x27,0x77,0xcf,
    533 				0x3b,0x3e,0x35,0xc4, 0x74,0xf9,0x76,0xb8,
    534 				0x94,0xbf,0x30,0x1a, 0x86,0xfa,0x65,0x1f,
    535 				0x46,0x39,0x70,0xe8, 0x9d,0x4a,0x05,0x34,
    536 				0xb2,0xec,0xad,0x29, 0xec,0x04,0x4e,0x7e,
    537 			},
    538 			{
    539 				0x5f,0xf4,0xba,0x49, 0x3c,0x40,0xcf,0xff,
    540 				0x3b,0x01,0xe4,0x72, 0xc5,0x75,0x66,0x8c,
    541 				0xce,0x38,0x80,0xb9, 0x29,0x0b,0x05,0xbf,
    542 				0xed,0xe5,0xec,0x96, 0xed,0x5e,0x9b,0x28,
    543 				0x98,0x50,0x8b,0x09, 0xbc,0x80,0x0e,0xee,
    544 				0x09,0x9a,0x3c,0x90, 0x60,0x2a,0xbd,0x4b,
    545 				0x1d,0x4f,0x34,0x3d, 0x49,0x7c,0x60,0x55,
    546 				0xc8,0x7b,0xb9,0x56, 0xd5,0x3b,0xf3,0x51,
    547 			},
    548 		},
    549 	},
    550 
    551 	[1] = {			/* Hash_DRBG.pdf, p. 198 */
    553 		.personalization = &kat_zero,
    554 		.additional = kat_additional,
    555 		.reseed = false,
    556 		.C = {		/* p. 201 */
    557 			0xe1,0x5d,0xe4,0xa8, 0xe3,0xb1,0x41,0x9b,
    558 			0x61,0xd5,0x34,0xf1, 0x5d,0xbd,0x31,0xee,
    559 			0x19,0xec,0x59,0x5f, 0x8b,0x98,0x11,0x1a,
    560 			0x94,0xf5,0x22,0x37, 0xad,0x5d,0x66,0xf0,
    561 			0xcf,0xaa,0xfd,0xdc, 0x90,0x19,0x59,0x02,
    562 			0xe9,0x79,0xf7,0x9b, 0x65,0x35,0x7f,0xea,
    563 			0x85,0x99,0x8e,0x4e, 0x37,0xd2,0xc1,
    564 		},
    565 		.V = {
    566 			[0] = {	/* p. 200 */
    567 				0xab,0x41,0xcd,0xe4, 0x37,0xab,0x8b,0x09,
    568 				0x1c,0xa7,0xc5,0x75, 0x5d,0x10,0xf0,0x11,
    569 				0x0c,0x1d,0xbd,0x46, 0x2f,0x22,0x6c,0xfd,
    570 				0xab,0xfb,0xb0,0x4a, 0x8b,0xcd,0xef,0x95,
    571 				0x16,0x7d,0x84,0xaf, 0x64,0x12,0x8c,0x0d,
    572 				0x71,0xf4,0xd5,0xb8, 0xc0,0xed,0xfb,0xbe,
    573 				0x3d,0xf4,0x04,0x48, 0xd2,0xd8,0xe1,
    574 			},
    575 			[1] = {	/* p. 204 */
    576 				0x8c,0x9f,0xb2,0x8d, 0x1b,0x5c,0xcc,0xa4,
    577 				0x7e,0x7c,0xfa,0x66, 0xba,0xce,0x21,0xff,
    578 				0x26,0x0a,0x16,0xa5, 0xba,0xba,0x7f,0x1f,
    579 				0xd3,0x3b,0x30,0x79, 0x8f,0xb2,0x9a,0x0f,
    580 				0xba,0x66,0x65,0x02, 0x7d,0x7f,0x10,0x58,
    581 				0x71,0xbf,0xb4,0x40, 0xdf,0xbe,0xde,0x81,
    582 				0xd0,0x4d,0x22,0xdf, 0xf7,0x89,0xe1,
    583 			},
    584 			[2] = {	/* p. 207 */
    585 				0x6d,0xfd,0x97,0x35, 0xff,0x0e,0x0e,0x3f,
    586 				0xe0,0x52,0x2f,0x58, 0x18,0x8b,0x53,0xed,
    587 				0x3f,0xf6,0x70,0x05, 0x46,0x52,0x90,0xe1,
    588 				0x7c,0x5a,0xd8,0x2d, 0xa9,0x2a,0x05,0x01,
    589 				0xaa,0x66,0x3a,0xa6, 0x9f,0xa5,0xa0,0xb0,
    590 				0x81,0x2b,0x4b,0x4f, 0xaf,0xf3,0xfe,0xce,
    591 				0x79,0xcc,0xf6,0xaa, 0xde,0xc1,0xd0,
    592 			},
    593 		},
    594 		.rnd_val = {
    595 			[0] = {	/* p. 203 */
    596 				0x51,0x07,0x24,0xb9, 0x3a,0xe9,0xa1,0x82,
    597 				0x70,0xe4,0x84,0x73, 0x71,0x1d,0x88,0x24,
    598 				0x63,0x1b,0xaa,0x7f, 0x1d,0x9a,0xc9,0x28,
    599 				0x4e,0x7e,0xc8,0xf3, 0x63,0x7f,0x7a,0x74,
    600 				0x3b,0x36,0x44,0xeb, 0x96,0xc9,0x86,0x27,
    601 				0xc8,0xfd,0x40,0x5a, 0x7a,0x46,0x03,0xf3,
    602 				0x8c,0xff,0x7c,0x89, 0xe9,0xc1,0x33,0xf5,
    603 				0x85,0x1f,0x40,0xe9, 0x20,0x30,0xfe,0xa2,
    604 			},
    605 			[1] = {	/* p. 206 */
    606 				0x62,0x53,0xda,0x3a, 0xae,0x8b,0x88,0xa3,
    607 				0xb7,0x46,0xe4,0xc8, 0xb2,0x63,0x5c,0x54,
    608 				0x0f,0x6e,0x9e,0xa7, 0x15,0x7e,0xe6,0x9d,
    609 				0xd7,0x1e,0xfb,0x2e, 0x8f,0xf7,0xbb,0xe1,
    610 				0xe3,0x33,0x68,0x88, 0x38,0xdd,0x7d,0xe4,
    611 				0x9c,0xc8,0x89,0x90, 0x30,0x9c,0x96,0xcd,
    612 				0xb2,0xab,0x92,0x95, 0x74,0x36,0xbf,0x83,
    613 				0xd1,0xbd,0x83,0x08, 0x19,0xc7,0x48,0xca,
    614 			},
    615 		},
    616 	},
    617 
    618 	[2] = {			/* Hash_DRBG.pdf, p. 208 */
    620 		.personalization = &kat_personalization,
    621 		.additional = kat_no_additional,
    622 		.reseed = false,
    623 		.C = {		/* p. 211 */
    624 			0x44,0x74,0x8a,0x78, 0xb1,0x6e,0x75,0x55,
    625 			0x9f,0x88,0x1d,0x51, 0xc1,0x5d,0xfe,0x6c,
    626 			0x52,0xcf,0xb0,0xbb, 0x71,0x62,0x01,0x69,
    627 			0xc7,0x93,0x34,0x27, 0x67,0xe7,0xf8,0x87,
    628 			0x5f,0x42,0xcb,0x6a, 0x20,0xc8,0x9d,0x7c,
    629 			0x6e,0xf3,0xdc,0x61, 0x0d,0x8f,0xf2,0x03,
    630 			0xd6,0x76,0x6c,0xed, 0x19,0x19,0xd0,
    631 		},
    632 		.V = {
    633 			[0] = {	/* p. 210 */
    634 				0xa3,0xe9,0x4e,0x39, 0x26,0xfd,0xa1,0x69,
    635 				0xc3,0x03,0xd6,0x64, 0x38,0x39,0x05,0xe0,
    636 				0xd7,0x99,0x62,0xd1, 0x65,0x44,0x6d,0x63,
    637 				0xbd,0xa6,0x54,0xd1, 0x32,0xf7,0x2d,0xb4,
    638 				0x71,0x56,0x4b,0x45, 0x6f,0xf2,0xee,0xc8,
    639 				0x36,0x42,0x2a,0xcc, 0x5a,0x02,0x99,0x35,
    640 				0xa7,0x99,0x29,0x90, 0x94,0xa1,0xca,
    641 			},
    642 			[1] = {	/* p. 213 */
    643 				0xe8,0x5d,0xd8,0xb1, 0xd8,0x6c,0x16,0xbf,
    644 				0x62,0x8b,0xf3,0xb5, 0xf9,0x97,0x04,0x4d,
    645 				0x2a,0x69,0x13,0x8c, 0xd6,0xa6,0x6e,0xe7,
    646 				0x36,0xdb,0xaa,0x3b, 0xf1,0xd0,0x28,0x3b,
    647 				0x71,0x7b,0x33,0x6e, 0xb3,0xae,0x5b,0xdd,
    648 				0x04,0x17,0x2e,0xa2, 0x6e,0x5a,0x48,0xf3,
    649 				0xb3,0xfb,0xab,0xf8, 0x2f,0x76,0x79,
    650 			},
    651 			[2] = {	/* p. 215 */
    652 				0x2c,0xd2,0x63,0x2a, 0x89,0xda,0x8c,0x15,
    653 				0x02,0x14,0x11,0x07, 0xba,0xf5,0x02,0xb9,
    654 				0x7d,0x38,0xc4,0x48, 0x48,0x08,0x71,0x0a,
    655 				0x66,0xf8,0x40,0x11, 0xd7,0x02,0x8d,0x14,
    656 				0xd3,0x15,0x5a,0x73, 0x79,0xad,0xd5,0x3c,
    657 				0xc8,0xea,0x84,0xd0, 0xfc,0x64,0x1d,0xfc,
    658 				0x62,0x9e,0x06,0x19, 0x1f,0x5f,0x6d,
    659 			},
    660 		},
    661 		.rnd_val = {
    662 			[0] = {	/* p. 213 */
    663 				0x4a,0x62,0x66,0x4f, 0x26,0x6e,0xe5,0x37,
    664 				0xb9,0x0d,0x64,0xb0, 0x5e,0x1d,0x81,0x3d,
    665 				0x28,0xb1,0x59,0xa9, 0x79,0xf1,0x50,0x9d,
    666 				0xde,0x31,0xb7,0x1d, 0xa4,0x3d,0x54,0x6e,
    667 				0xe8,0xe7,0x86,0x78, 0x20,0x2d,0xc2,0x37,
    668 				0xad,0x4a,0xfe,0x7d, 0xf3,0x10,0xc9,0xa4,
    669 				0x13,0xe3,0x8a,0xaf, 0x41,0x7d,0x2d,0x22,
    670 				0x5a,0xa3,0x65,0xec, 0x4a,0x7d,0x29,0x96,
    671 			},
    672 			[1] = {	/* p. 215 */
    673 				0x59,0x58,0x3d,0x3c, 0x0a,0xc3,0x71,0x30,
    674 				0xc4,0x78,0x9a,0x83, 0x11,0xb8,0xca,0x8f,
    675 				0x98,0x5e,0xf1,0xe8, 0xf9,0x4d,0x95,0x4e,
    676 				0x32,0xe3,0x44,0xa6, 0x21,0xc2,0x4b,0x2f,
    677 				0x37,0x1d,0xa9,0xba, 0x3c,0x33,0x15,0x3f,
    678 				0x09,0xe5,0x51,0x45, 0xe7,0x62,0x92,0x6b,
    679 				0x73,0xac,0x14,0x7a, 0x1e,0x86,0x31,0xd1,
    680 				0xcc,0xd0,0x85,0x67, 0xcf,0x67,0x7c,0x72,
    681 			},
    682 		},
    683 	},
    684 
    685 	[3] = {			/* Hash_DRBG.pdf, p. 215 */
    687 		.personalization = &kat_personalization,
    688 		.additional = kat_additional,
    689 		.reseed = false,
    690 		.C = {		/* p. 220 */
    691 			0x44,0x74,0x8a,0x78, 0xb1,0x6e,0x75,0x55,
    692 			0x9f,0x88,0x1d,0x51, 0xc1,0x5d,0xfe,0x6c,
    693 			0x52,0xcf,0xb0,0xbb, 0x71,0x62,0x01,0x69,
    694 			0xc7,0x93,0x34,0x27, 0x67,0xe7,0xf8,0x87,
    695 			0x5f,0x42,0xcb,0x6a, 0x20,0xc8,0x9d,0x7c,
    696 			0x6e,0xf3,0xdc,0x61, 0x0d,0x8f,0xf2,0x03,
    697 			0xd6,0x76,0x6c,0xed, 0x19,0x19,0xd0,
    698 		},
    699 		.V = {
    700 			[0] = {	/* p. 218 */
    701 				0xa3,0xe9,0x4e,0x39, 0x26,0xfd,0xa1,0x69,
    702 				0xc3,0x03,0xd6,0x64, 0x38,0x39,0x05,0xe0,
    703 				0xd7,0x99,0x62,0xd1, 0x65,0x44,0x6d,0x63,
    704 				0xbd,0xa6,0x54,0xd1, 0x32,0xf7,0x2d,0xb4,
    705 				0x71,0x56,0x4b,0x45, 0x6f,0xf2,0xee,0xc8,
    706 				0x36,0x42,0x2a,0xcc, 0x5a,0x02,0x99,0x35,
    707 				0xa7,0x99,0x29,0x90, 0x94,0xa1,0xca,
    708 			},
    709 			[1] = {	/* p. 222 */
    710 				0xe8,0x5d,0xd8,0xb1, 0xd8,0x6c,0x16,0xbf,
    711 				0x62,0x8b,0xf3,0xb5, 0xf9,0x97,0x04,0x4d,
    712 				0x2a,0x69,0x13,0x8c, 0xd6,0xa6,0x6f,0x8c,
    713 				0xa8,0x7b,0x87,0x43, 0x50,0x20,0x2e,0x1d,
    714 				0x8a,0xb0,0xb5,0xad, 0x47,0xac,0xc2,0x75,
    715 				0x40,0x28,0x9f,0xe3, 0xa8,0xe3,0x1f,0x7b,
    716 				0x56,0x58,0xdd,0xd1, 0x96,0x94,0x89,
    717 			},
    718 			[2] = {	/* p. 225 */
    719 				0x2c,0xd2,0x63,0x2a, 0x89,0xda,0x8c,0x15,
    720 				0x02,0x14,0x11,0x07, 0xba,0xf5,0x02,0xb9,
    721 				0x7d,0x38,0xc4,0x48, 0x48,0x08,0x71,0xb2,
    722 				0x77,0xae,0xc7,0xff, 0x8d,0xa2,0x3c,0x71,
    723 				0xef,0xf5,0x9d,0xc2, 0x4e,0x5e,0x4c,0x7f,
    724 				0x58,0x47,0xb0,0xc1, 0x2f,0x6a,0x59,0x9e,
    725 				0x6b,0x2e,0xda,0xc0, 0x30,0x6b,0xcd,
    726 			},
    727 		},
    728 		.rnd_val = {	/* p. 222 */
    729 			[0] = {
    730 				0xe0,0xb9,0x7c,0x82, 0x12,0x68,0xfd,0x3b,
    731 				0xb2,0xca,0xbf,0xd1, 0xf9,0x54,0x84,0x78,
    732 				0xae,0x8a,0x60,0x41, 0x7f,0x7b,0x09,0x4a,
    733 				0x26,0x13,0x95,0x46, 0x06,0x2b,0x52,0x1c,
    734 				0xfd,0x33,0xe4,0xe3, 0x9b,0x9d,0xcd,0x0a,
    735 				0x3d,0xa1,0x52,0x09, 0xc7,0x2a,0xdb,0xe5,
    736 				0x8c,0x20,0xab,0x34, 0x07,0x02,0x69,0x51,
    737 				0x29,0x7a,0xd2,0x54, 0x30,0x75,0x53,0xa5,
    738 			},
    739 			[1] = {	/* p. 225 */
    740 				0xc1,0xac,0xd3,0xad, 0xa4,0xc8,0xc4,0x95,
    741 				0xbf,0x17,0x9d,0xb5, 0x98,0x22,0xc3,0x51,
    742 				0xbc,0x47,0x9a,0xbe, 0x4e,0xb2,0x8f,0x84,
    743 				0x39,0x57,0xb1,0x1e, 0x3c,0x2b,0xc0,0x48,
    744 				0x83,0x96,0x42,0x97, 0x97,0x5b,0xd7,0x2d,
    745 				0x10,0x24,0xab,0xcf, 0x6f,0x66,0x15,0xd7,
    746 				0xf5,0xb4,0xfd,0x1e, 0x40,0xa6,0x4e,0xeb,
    747 				0x45,0xba,0x21,0x81, 0xb8,0x39,0x37,0xed,
    748 			},
    749 		},
    750 	},
    751 
    752 	[4] = {			/* Hash_DRBG.pdf, p. 225 */
    754 		.personalization = &kat_zero,
    755 		.additional = kat_no_additional,
    756 		.reseed = true,
    757 		.C = {		/* p. 229 */
    758 			0xe1,0x5d,0xe4,0xa8, 0xe3,0xb1,0x41,0x9b,
    759 			0x61,0xd5,0x34,0xf1, 0x5d,0xbd,0x31,0xee,
    760 			0x19,0xec,0x59,0x5f, 0x8b,0x98,0x11,0x1a,
    761 			0x94,0xf5,0x22,0x37, 0xad,0x5d,0x66,0xf0,
    762 			0xcf,0xaa,0xfd,0xdc, 0x90,0x19,0x59,0x02,
    763 			0xe9,0x79,0xf7,0x9b, 0x65,0x35,0x7f,0xea,
    764 			0x85,0x99,0x8e,0x4e, 0x37,0xd2,0xc1,
    765 		},
    766 		.V = {
    767 			[0] = {	/* p. 227 */
    768 				0xab,0x41,0xcd,0xe4, 0x37,0xab,0x8b,0x09,
    769 				0x1c,0xa7,0xc5,0x75, 0x5d,0x10,0xf0,0x11,
    770 				0x0c,0x1d,0xbd,0x46, 0x2f,0x22,0x6c,0xfd,
    771 				0xab,0xfb,0xb0,0x4a, 0x8b,0xcd,0xef,0x95,
    772 				0x16,0x7d,0x84,0xaf, 0x64,0x12,0x8c,0x0d,
    773 				0x71,0xf4,0xd5,0xb8, 0xc0,0xed,0xfb,0xbe,
    774 				0x3d,0xf4,0x04,0x48, 0xd2,0xd8,0xe1,
    775 			},
    776 			[1] = {	/* p. 234 */
    777 				0x23,0x97,0x6c,0x61, 0x63,0xd7,0xe2,0x4a,
    778 				0x1a,0x03,0x8f,0x2b, 0x2b,0x64,0x67,0x97,
    779 				0x50,0xca,0x9e,0xd8, 0xd1,0x40,0x69,0x8d,
    780 				0x64,0x22,0x39,0x7b, 0x02,0x96,0x9e,0x6e,
    781 				0xcd,0xd2,0x9d,0xac, 0xc5,0x76,0x7e,0x2c,
    782 				0xc2,0xd0,0xa1,0x56, 0xc8,0x7a,0xd0,0xb3,
    783 				0x57,0x89,0x05,0x07, 0xe0,0x37,0x77,
    784 			},
    785 			[2] = {	/* p. 239 */
    786 				0x92,0xfb,0x0e,0x48, 0x0e,0x86,0x99,0x13,
    787 				0xc7,0xad,0x45,0xc7, 0xe3,0xfd,0x46,0x10,
    788 				0x17,0xe5,0xa6,0xb7, 0x70,0xf3,0x3b,0x31,
    789 				0x3c,0x38,0x83,0xf1, 0xcc,0x56,0x71,0x89,
    790 				0x45,0x21,0xf5,0xed, 0xe6,0x2e,0xaa,0xb0,
    791 				0x83,0xb1,0x41,0xa7, 0x5b,0x5c,0xc0,0x22,
    792 				0x60,0x5a,0x8a,0x3d, 0xc7,0x1b,0xa7,
    793 			},
    794 		},
    795 		.rnd_val = {
    796 			[0] = {	/* p. 234 */
    797 				0x92,0x27,0x55,0x23, 0xc7,0x0e,0x56,0x7b,
    798 				0xcf,0x9b,0x35,0xec, 0x50,0xb9,0x33,0xf8,
    799 				0x12,0x61,0x6d,0xf5, 0x86,0xb7,0xf7,0x2e,
    800 				0xe1,0xbc,0x77,0x35, 0xa5,0xc2,0x65,0x43,
    801 				0x73,0xcb,0xbc,0x72, 0x31,0x6d,0xff,0x84,
    802 				0x20,0xa3,0x3b,0xf0, 0x2b,0x97,0xac,0x8d,
    803 				0x19,0x52,0x58,0x3f, 0x27,0x0a,0xcd,0x70,
    804 				0x05,0xcc,0x02,0x7f, 0x4c,0xf1,0x18,0x7e,
    805 			},
    806 			[1] = {	/* p. 239 */
    807 				0x68,0x1a,0x46,0xb2, 0xaa,0x86,0x94,0xa0,
    808 				0xfe,0x4d,0xee,0xa7, 0x20,0x92,0x7a,0x84,
    809 				0xea,0xaa,0x98,0x5e, 0x59,0xc1,0x9f,0x8b,
    810 				0xe0,0x98,0x4d,0x8c, 0xbe,0xf8,0xc6,0x9b,
    811 				0x75,0x41,0x67,0x64, 0x19,0x46,0xe0,0x40,
    812 				0xee,0x20,0x43,0xe1, 0xcc,0xb2,0x9d,0xcf,
    813 				0x06,0x3c,0x0a,0x50, 0x83,0x0e,0x42,0x8e,
    814 				0x6d,0xca,0x26,0x2e, 0xcd,0x77,0xc5,0x42,
    815 			},
    816 		},
    817 	},
    818 
    819 	[5] = {			/* Hash_DRBG.pdf, p. 239 */
    821 		.personalization = &kat_zero,
    822 		.additional = kat_additional,
    823 		.reseed = true,
    824 		.C = {		/* p. 243 */
    825 			0xe1,0x5d,0xe4,0xa8, 0xe3,0xb1,0x41,0x9b,
    826 			0x61,0xd5,0x34,0xf1, 0x5d,0xbd,0x31,0xee,
    827 			0x19,0xec,0x59,0x5f, 0x8b,0x98,0x11,0x1a,
    828 			0x94,0xf5,0x22,0x37, 0xad,0x5d,0x66,0xf0,
    829 			0xcf,0xaa,0xfd,0xdc, 0x90,0x19,0x59,0x02,
    830 			0xe9,0x79,0xf7,0x9b, 0x65,0x35,0x7f,0xea,
    831 			0x85,0x99,0x8e,0x4e, 0x37,0xd2,0xc1,
    832 		},
    833 		.V = {
    834 			[0] = {	/* p. 242 */
    835 				0xab,0x41,0xcd,0xe4, 0x37,0xab,0x8b,0x09,
    836 				0x1c,0xa7,0xc5,0x75, 0x5d,0x10,0xf0,0x11,
    837 				0x0c,0x1d,0xbd,0x46, 0x2f,0x22,0x6c,0xfd,
    838 				0xab,0xfb,0xb0,0x4a, 0x8b,0xcd,0xef,0x95,
    839 				0x16,0x7d,0x84,0xaf, 0x64,0x12,0x8c,0x0d,
    840 				0x71,0xf4,0xd5,0xb8, 0xc0,0xed,0xfb,0xbe,
    841 				0x3d,0xf4,0x04,0x48, 0xd2,0xd8,0xe1,
    842 			},
    843 			[1] = {	/* p. 249 */
    844 				0xb3,0x74,0x95,0x46, 0x81,0xcf,0xc9,0x5b,
    845 				0x8d,0xb8,0x39,0x52, 0x8c,0x71,0x08,0x83,
    846 				0x5e,0xb4,0xf3,0x0a, 0xd9,0x1c,0xbe,0x9e,
    847 				0xa0,0xd5,0x45,0xcc, 0xfd,0x18,0x13,0x2a,
    848 				0xf1,0xd3,0x76,0x8f, 0x47,0x02,0x77,0x2b,
    849 				0x69,0x15,0x9f,0x2c, 0xc0,0x7f,0x48,0x74,
    850 				0x1e,0xb5,0xb2,0xb1, 0x22,0x11,0x25,
    851 			},
    852 			[2] = {	/* p. 254 */
    853 				0xbf,0xe3,0xd6,0x81, 0xa2,0x0f,0xbe,0x39,
    854 				0x03,0x8f,0x4d,0x66, 0x77,0x7c,0x1b,0xe5,
    855 				0x79,0xee,0xb4,0x85, 0x7b,0x42,0xf2,0x1c,
    856 				0x3f,0x59,0x8b,0x59, 0x62,0xb7,0xaa,0x48,
    857 				0x0e,0xa5,0x65,0xfe, 0xea,0xbd,0xfb,0xd6,
    858 				0xa7,0xec,0xcb,0x96, 0x02,0xc1,0x4b,0xfa,
    859 				0x30,0xf0,0xf9,0x81, 0x90,0x0c,0xd0,
    860 			},
    861 		},
    862 		.rnd_val = {
    863 			[0] = {	/* p. 249 */
    864 				0x11,0x60,0x1b,0x72, 0xca,0x60,0x89,0x73,
    865 				0x6b,0x20,0x47,0x44, 0xb2,0x9d,0xa1,0xaa,
    866 				0xaf,0xba,0xca,0xa5, 0x28,0x8f,0x06,0xbe,
    867 				0x48,0x45,0x69,0xcc, 0xed,0xbe,0xce,0x03,
    868 				0xe8,0x22,0xea,0xa5, 0xb1,0x4f,0x0e,0x04,
    869 				0x94,0x8c,0x05,0xcd, 0x3c,0xc2,0xe2,0x88,
    870 				0x9a,0x89,0xfa,0x03, 0xd6,0x5d,0x4d,0x74,
    871 				0xac,0x50,0xff,0x6b, 0xd8,0x56,0xe5,0x79,
    872 			},
    873 			[1] = {	/* p. 255 */
    874 				0x05,0x5b,0xc1,0x28, 0xcc,0x2d,0x0e,0x25,
    875 				0x0f,0x47,0xe4,0xe4, 0xf5,0x82,0x37,0x5d,
    876 				0xe3,0xee,0x5e,0x9f, 0xe8,0x31,0x68,0x74,
    877 				0x97,0xe5,0xaf,0x1e, 0x7c,0xb6,0x9e,0xfd,
    878 				0xeb,0xd2,0xfd,0x31, 0xc7,0xce,0x2b,0xba,
    879 				0x0d,0xbc,0x6c,0x74, 0xc8,0xa2,0x0a,0x7d,
    880 				0x72,0xf6,0x0e,0x6d, 0x9f,0x63,0xed,0x50,
    881 				0x9e,0x96,0x3e,0x54, 0xa6,0x9e,0x90,0x48,
    882 			},
    883 		},
    884 	},
    885 
    886 	[6] = {			/* Hash_DRBG.pdf, p. 255 */
    888 		.personalization = &kat_personalization,
    889 		.additional = kat_no_additional,
    890 		.reseed = true,
    891 		.C = {		/* p. 259 */
    892 			0x44,0x74,0x8a,0x78, 0xb1,0x6e,0x75,0x55,
    893 			0x9f,0x88,0x1d,0x51, 0xc1,0x5d,0xfe,0x6c,
    894 			0x52,0xcf,0xb0,0xbb, 0x71,0x62,0x01,0x69,
    895 			0xc7,0x93,0x34,0x27, 0x67,0xe7,0xf8,0x87,
    896 			0x5f,0x42,0xcb,0x6a, 0x20,0xc8,0x9d,0x7c,
    897 			0x6e,0xf3,0xdc,0x61, 0x0d,0x8f,0xf2,0x03,
    898 			0xd6,0x76,0x6c,0xed, 0x19,0x19,0xd0,
    899 		},
    900 		.V = {
    901 			[0] = {	/* p. 257 */
    902 				0xa3,0xe9,0x4e,0x39, 0x26,0xfd,0xa1,0x69,
    903 				0xc3,0x03,0xd6,0x64, 0x38,0x39,0x05,0xe0,
    904 				0xd7,0x99,0x62,0xd1, 0x65,0x44,0x6d,0x63,
    905 				0xbd,0xa6,0x54,0xd1, 0x32,0xf7,0x2d,0xb4,
    906 				0x71,0x56,0x4b,0x45, 0x6f,0xf2,0xee,0xc8,
    907 				0x36,0x42,0x2a,0xcc, 0x5a,0x02,0x99,0x35,
    908 				0xa7,0x99,0x29,0x90, 0x94,0xa1,0xca,
    909 			},
    910 			[1] = {	/* p. 264 */
    911 				0xaa,0x11,0x1b,0x0e, 0xd5,0x6c,0xf4,0xa6,
    912 				0xcc,0xe4,0xad,0xe7, 0xf1,0x1b,0x06,0x10,
    913 				0x45,0xbf,0x10,0x92, 0xcb,0xb3,0x8f,0xf3,
    914 				0x23,0x95,0xea,0x62, 0xd2,0x6b,0x27,0xc8,
    915 				0x86,0x89,0x45,0xc5, 0x93,0xba,0x70,0xc3,
    916 				0x84,0xad,0xad,0x45, 0x77,0x1c,0x93,0xb0,
    917 				0x9c,0x27,0x69,0x07, 0x52,0xd1,0xd8,
    918 			},
    919 			[2] = {	/* p. 269 */
    920 				0x5f,0x0f,0xd4,0x0c, 0x8c,0x82,0xef,0x41,
    921 				0x03,0x14,0xb8,0x30, 0xc2,0x0f,0xcc,0xea,
    922 				0x71,0x59,0x18,0x9a, 0xea,0x13,0xe8,0x48,
    923 				0x75,0x68,0x68,0x18, 0xcd,0x4f,0x12,0xb9,
    924 				0xde,0xa8,0x82,0x58, 0x16,0xa4,0x13,0xa2,
    925 				0x95,0x72,0x5e,0xb3, 0x3e,0x33,0xb9,0xad,
    926 				0xfe,0xe0,0xb1,0xc2, 0x34,0x0a,0xe0,
    927 			},
    928 		},
    929 		.rnd_val = {
    930 			[0] = {	/* p. 264 */
    931 				0x7a,0x33,0xd3,0x90, 0x33,0xf8,0x60,0x58,
    932 				0x9f,0x37,0x5e,0x73, 0x35,0x30,0x75,0x52,
    933 				0x96,0x58,0xbb,0xed, 0x99,0xc8,0xa0,0xef,
    934 				0x5e,0x28,0xb3,0x51, 0xb2,0xdf,0x33,0x58,
    935 				0xb3,0xd8,0x9b,0xac, 0x72,0x25,0xdf,0x9e,
    936 				0x3b,0xcd,0x08,0x36, 0xb9,0x9b,0x5d,0xbf,
    937 				0x36,0x3a,0x17,0x0c, 0x7b,0xb9,0xbe,0x41,
    938 				0xa4,0xaa,0x97,0x44, 0x5e,0xce,0xe4,0x1e,
    939 			},
    940 			[1] = {	/* p. 269 */
    941 				0x04,0x1a,0xbd,0x94, 0x07,0x9a,0x05,0x71,
    942 				0x88,0x5f,0x16,0x65, 0x94,0x4e,0x0e,0x7f,
    943 				0x1b,0xfa,0xcd,0xea, 0xea,0xe9,0xd4,0x4e,
    944 				0xed,0xc1,0x1f,0xad, 0xd8,0x4c,0x34,0xc7,
    945 				0xca,0xa7,0x3d,0x09, 0xa0,0x19,0x31,0x93,
    946 				0xfa,0x40,0xa1,0x9f, 0x64,0x4f,0x04,0x8d,
    947 				0x2a,0x54,0x17,0x04, 0x25,0x53,0xdf,0x52,
    948 				0x51,0x74,0x1b,0x40, 0xea,0xcf,0xeb,0x98,
    949 			},
    950 		},
    951 	},
    952 
    953 	[7] = {			/* Hash_DRBG.pdf, p. 269 */
    955 		.personalization = &kat_personalization,
    956 		.additional = kat_additional,
    957 		.reseed = true,
    958 		.C = {		/* p. 274 */
    959 			0x44,0x74,0x8a,0x78, 0xb1,0x6e,0x75,0x55,
    960 			0x9f,0x88,0x1d,0x51, 0xc1,0x5d,0xfe,0x6c,
    961 			0x52,0xcf,0xb0,0xbb, 0x71,0x62,0x01,0x69,
    962 			0xc7,0x93,0x34,0x27, 0x67,0xe7,0xf8,0x87,
    963 			0x5f,0x42,0xcb,0x6a, 0x20,0xc8,0x9d,0x7c,
    964 			0x6e,0xf3,0xdc,0x61, 0x0d,0x8f,0xf2,0x03,
    965 			0xd6,0x76,0x6c,0xed, 0x19,0x19,0xd0,
    966 		},
    967 		.V = {
    968 			[0] = {	/* p. 272 */
    969 				0xa3,0xe9,0x4e,0x39, 0x26,0xfd,0xa1,0x69,
    970 				0xc3,0x03,0xd6,0x64, 0x38,0x39,0x05,0xe0,
    971 				0xd7,0x99,0x62,0xd1, 0x65,0x44,0x6d,0x63,
    972 				0xbd,0xa6,0x54,0xd1, 0x32,0xf7,0x2d,0xb4,
    973 				0x71,0x56,0x4b,0x45, 0x6f,0xf2,0xee,0xc8,
    974 				0x36,0x42,0x2a,0xcc, 0x5a,0x02,0x99,0x35,
    975 				0xa7,0x99,0x29,0x90, 0x94,0xa1,0xca,
    976 			},
    977 			[1] = {	/* p. 279 */
    978 				0xaa,0xf6,0xb9,0x9b, 0x7f,0x84,0xb0,0x36,
    979 				0xe1,0xcc,0xbc,0x9d, 0x57,0x3a,0x36,0xb8,
    980 				0xbd,0xd4,0x7c,0x35, 0x8b,0xb5,0xf3,0xc1,
    981 				0xd6,0xe7,0x90,0x3a, 0xaa,0x29,0xf1,0xc8,
    982 				0x7a,0xe6,0x66,0xb8, 0x86,0x93,0xbe,0xf4,
    983 				0x6c,0x51,0xc2,0x4c, 0x47,0xbe,0xfe,0x4b,
    984 				0x35,0x75,0x4d,0xcb, 0xfa,0x1e,0x7d,
    985 			},
    986 			[2] = {	/* p. 285 */
    987 				0x0c,0x75,0x77,0x4d, 0x61,0x02,0x69,0xad,
    988 				0x5b,0xb4,0xab,0xbb, 0x14,0x83,0x23,0xc9,
    989 				0x78,0x9f,0x8f,0x76, 0x25,0xcc,0x34,0x33,
    990 				0x7c,0x03,0x47,0x2d, 0x9a,0x0c,0x4f,0xac,
    991 				0x30,0xbe,0xd2,0xdd, 0xde,0x64,0xb8,0x7a,
    992 				0x2c,0x70,0x67,0x52, 0xc2,0x1a,0xc0,0x11,
    993 				0x27,0x43,0x59,0x2c, 0x4f,0xdf,0x67,
    994 			},
    995 		},
    996 		.rnd_val = {	/* p. 279 */
    997 			[0] = {
    998 				0x88,0x97,0x32,0x97, 0x5b,0x36,0xe8,0xe2,
    999 				0xe7,0xb7,0x40,0x50, 0xae,0xa1,0x71,0x39,
   1000 				0xda,0x2b,0x86,0x34, 0xdc,0xe2,0x13,0x3b,
   1001 				0x06,0x34,0x74,0x3f, 0x47,0x75,0x57,0xab,
   1002 				0x7b,0x84,0x4e,0xd3, 0xf2,0xa4,0x6c,0xc6,
   1003 				0x3e,0xb2,0x32,0x86, 0x46,0x4c,0x51,0xd5,
   1004 				0xd7,0x69,0x71,0xc4, 0x7b,0xc5,0xb5,0x5f,
   1005 				0xed,0x72,0xa8,0x04, 0x3c,0xbf,0x66,0x4f,
   1006 			},
   1007 			[1] = {
   1008 				0xbf,0x49,0xb8,0x89, 0xba,0x98,0x4d,0x34,
   1009 				0x63,0x87,0xe8,0x64, 0x7e,0x98,0xbb,0x99,
   1010 				0xcd,0x41,0xa3,0x2f, 0xbe,0xc1,0xfc,0xb3,
   1011 				0xb6,0xa1,0xb7,0xd9, 0x93,0x2b,0xa7,0xe1,
   1012 				0x1e,0xe6,0xbb,0xd9, 0x24,0x40,0x5a,0x2c,
   1013 				0x7f,0xca,0x89,0x0a, 0x5e,0x9a,0x8d,0xea,
   1014 				0x66,0xac,0x0c,0xac, 0xa0,0xca,0x7b,0xc1,
   1015 				0x8d,0x74,0xfb,0xc0, 0x2a,0x11,0xe4,0x53,
   1016 			},
   1017 		},
   1018 	},
   1019 };
   1020 
   1021 #ifdef NIST_HASH_DRBG_DEBUG
   1023 #define	DPRINTF(fmt, ...)						      \
   1024 	printf("%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__)
   1025 #define	DUSE(v)
   1026 #else
   1027 #define	DPRINTF(fmt, ...)
   1028 #define	DUSE(v)			(void)(v)
   1029 #endif
   1030 
   1031 #define	CHECK(i, name, actual, expected, n) do				      \
   1032 {									      \
   1033 	CTASSERT(sizeof(actual) == (n));				      \
   1034 	ok &= check(i, name, actual, expected, (n));			      \
   1035 } while (0)
   1036 
   1037 static bool
   1038 check(unsigned katno, const char *name, const uint8_t *actual,
   1039     const uint8_t *expected, size_t n)
   1040 {
   1041 	bool ok = true;
   1042 	size_t i;
   1043 
   1044 	DUSE(katno);
   1045 	DUSE(name);
   1046 
   1047 	for (i = 0; i < n; i++) {
   1048 		if (actual[i] != expected[i]) {
   1049 			DPRINTF("KAT %u %s[%zu] = %02x, expected %02x\n",
   1050 			    katno, name, i, actual[i], expected[i]);
   1051 			ok = false;
   1052 		}
   1053 	}
   1054 
   1055 	return ok;
   1056 }
   1057 
   1058 #ifdef NIST_HASH_DRBG_MAIN
   1059 int
   1060 main(void)
   1061 {
   1062 	int ret;
   1063 
   1064 	ret = nist_hash_drbg_initialize();
   1065 
   1066 	fflush(stdout);
   1067 	return ret || ferror(stdout);
   1068 }
   1069 #endif
   1070 
   1071 int
   1073 nist_hash_drbg_initialize(void)
   1074 {
   1075 	const unsigned truncation[] = { 0, 1, 32, 63 };
   1076 	bool ok = true;
   1077 	size_t i, j;
   1078 
   1079 	for (i = 0; i < arraycount(kat); i++) {
   1080 		for (j = 0; j < arraycount(truncation); j++) {
   1081 			const unsigned trunc = truncation[j];
   1082 			struct nist_hash_drbg drbg, *D = &drbg;
   1083 			uint8_t rnd_val[64];
   1084 			unsigned reseed_counter;
   1085 
   1086 			nist_hash_drbg_instantiate(D,
   1087 			    kat_entropy[0], sizeof kat_entropy[0],
   1088 			    kat_nonce, sizeof kat_nonce,
   1089 			    kat[i].personalization->hv_base,
   1090 			    kat[i].personalization->hv_len);
   1091 			reseed_counter = 1;
   1092 			CHECK(i, "C", D->C, kat[i].C, SEEDLEN_BYTES);
   1093 			CHECK(i, "V[0]", D->V, kat[i].V[0], SEEDLEN_BYTES);
   1094 			if (D->reseed_counter != reseed_counter) {
   1095 				DPRINTF("bad reseed counter: %u, expected %u",
   1096 				    D->reseed_counter, reseed_counter);
   1097 				ok = false;
   1098 			}
   1099 
   1100 			if (kat[i].reseed) {
   1101 				nist_hash_drbg_reseed(D,
   1102 				    kat_entropy[1], sizeof kat_entropy[1],
   1103 				    kat[i].additional[0]->hv_base,
   1104 				    kat[i].additional[0]->hv_len);
   1105 			}
   1106 
   1107 			nist_hash_drbg_generate(D, rnd_val,
   1108 			    sizeof(rnd_val) - trunc,
   1109 			    kat[i].reseed ? 0 : kat[i].additional[0]->hv_base,
   1110 			    kat[i].reseed ? 0 : kat[i].additional[0]->hv_len);
   1111 			reseed_counter++;
   1112 			CHECK(i, "V[1]", D->V, kat[i].V[1], SEEDLEN_BYTES);
   1113 			ASSERT(sizeof(kat[i].rnd_val[0]) - trunc <=
   1114 			    sizeof rnd_val);
   1115 			check(i, "rnd_val[0]", rnd_val, kat[i].rnd_val[0],
   1116 			    sizeof(kat[i].rnd_val[0]) - trunc);
   1117 			if (D->reseed_counter != reseed_counter) {
   1118 				DPRINTF("bad reseed counter: %u, expected %u",
   1119 				    D->reseed_counter, reseed_counter);
   1120 				ok = false;
   1121 			}
   1122 
   1123 			if (kat[i].reseed) {
   1124 				nist_hash_drbg_reseed(D,
   1125 				    kat_entropy[2], sizeof kat_entropy[2],
   1126 				    kat[i].additional[1]->hv_base,
   1127 				    kat[i].additional[1]->hv_len);
   1128 				reseed_counter = 1;
   1129 			}
   1130 
   1131 			nist_hash_drbg_generate(D, rnd_val,
   1132 			    sizeof(rnd_val) - trunc,
   1133 			    kat[i].reseed ? 0 : kat[i].additional[1]->hv_base,
   1134 			    kat[i].reseed ? 0 : kat[i].additional[1]->hv_len);
   1135 			reseed_counter++;
   1136 			CHECK(i, "V[2]", D->V, kat[i].V[2], SEEDLEN_BYTES);
   1137 			ASSERT(sizeof(kat[i].rnd_val[1]) - trunc <=
   1138 			    sizeof rnd_val);
   1139 			check(i, "rnd_val[1]", rnd_val, kat[i].rnd_val[1],
   1140 			    sizeof(kat[i].rnd_val[1]) - trunc);
   1141 			if (D->reseed_counter != reseed_counter) {
   1142 				DPRINTF("bad reseed counter: %u, expected %u",
   1143 				    D->reseed_counter, reseed_counter);
   1144 				ok = false;
   1145 			}
   1146 
   1147 			nist_hash_drbg_destroy(D);
   1148 		}
   1149 	}
   1150 
   1151 	if (!ok)
   1152 		return 1;
   1153 	return 0;
   1154 }
   1155