⬢ DragonFlyBSD Kernel Audit
DF-2368 / rijndael_harness.c
← back to finding ↓ download raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
345
346
347
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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
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
504
505
506
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
/*-
 * rijndael_harness.c -- kernel-compiled verification rig for
 * DF-2367 (data-dependent T-table lookups) and DF-2368 (no-op
 * keyBits validation -> Nr=0 -> runaway round loop).
 *
 * Compiles INTO the kernel (runtime kldload GPFs on this guest).
 * Exercises the REAL rijndael_* kernel functions exported from
 * sys/crypto/rijndael/.  The only local crypto code is the
 * instrumented "twin" of rijndaelEncrypt's round loop (mirror of
 * rijndael-alg-fst.c lines 948-1041) used to OBSERVE the table
 * indices the real function consumes; the twin is validated
 * byte-exact against the real functions on every run (twinval op)
 * and on every idx op (ct_real == ct_twin is asserted in the log).
 *
 * Interface: sysctl kern.rijnh.test="<op> <args...>"
 *   kat                 run FIPS-197 + openssl-derived KAT/ECB vectors
 *                       through real rijndael_set_key/encrypt/decrypt
 *   setkey <bits>       real rijndael_set_key with <bits>; reports Nr/ek
 *   poison <hexaddr>    build real ctx at user VA (valid set_key), force
 *                       Nr=0 (= state broken set_key stores), real
 *                       rijndael_encrypt -> expected kernel page fault
 *   direct <hexaddr>    same ctx, call rijndaelEncrypt(ek, 0, ...) directly
 *   twinval <n>         n pseudo-random vectors x 3 key sizes:
 *                       twin ct must equal real ct
 *   idx <keyhex> <pthex> full table-index trace + real/twin ct
 *   idxr1 <keyhex> <pthex> round-1 16 table indices only
 *   bench <n> <keyhex>  time n real rijndael_encrypt calls
 *
 * All results are kprintf'd with the "RIJNH:" tag and also stored in
 * the sysctl string for readback.
 */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <machine/stdarg.h>

#include <crypto/rijndael/rijndael.h>
#include <crypto/rijndael/rijndael_local.h>

#include <crypto/rijndael/rijndael_twin_tables.h>	/* Te0..Te4, rcon: verbatim copy */

#define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] <<  8) ^ ((u32)(pt)[3]))
#define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >>  8); (ct)[3] = (u8)(st); }

/*
 * Instrumented mirror of rijndael-alg-fst.c rijndaelEncrypt(),
 * !FULL_UNROLL path (lines 948-1041).  LOGI() records each table
 * index before the lookup, in exactly the evaluation order of the
 * real code.  Key schedule comes from the REAL
 * rijndaelKeySetupEnc(); only the round loop is mirrored.
 */
static void
twin_encrypt_idx(const u32 rk_arg[], int Nr, const u8 pt[16], u8 ct[16],
    u8 *idx, int *nidx)
{
	u32 s0, s1, s2, s3, t0, t1, t2, t3;
	const u32 *rk = rk_arg;
	int r, n = 0;

	s0 = GETU32(pt     ) ^ rk[0];
	s1 = GETU32(pt +  4) ^ rk[1];
	s2 = GETU32(pt +  8) ^ rk[2];
	s3 = GETU32(pt + 12) ^ rk[3];

	r = Nr >> 1;
	for (;;) {
		t0 = Te0[(s0 >> 24)       ] ^
		     Te1[(s1 >> 16) & 0xff] ^
		     Te2[(s2 >>  8) & 0xff] ^
		     Te3[(s3      ) & 0xff] ^ rk[4];
		t1 = Te0[(s1 >> 24)       ] ^
		     Te1[(s2 >> 16) & 0xff] ^
		     Te2[(s3 >>  8) & 0xff] ^
		     Te3[(s0      ) & 0xff] ^ rk[5];
		t2 = Te0[(s2 >> 24)       ] ^
		     Te1[(s3 >> 16) & 0xff] ^
		     Te2[(s0 >>  8) & 0xff] ^
		     Te3[(s1      ) & 0xff] ^ rk[6];
		t3 = Te0[(s3 >> 24)       ] ^
		     Te1[(s0 >> 16) & 0xff] ^
		     Te2[(s1 >>  8) & 0xff] ^
		     Te3[(s2      ) & 0xff] ^ rk[7];
		/* record the exact indices consumed above (one per stmt) */
		idx[n++] = (u8)((s0 >> 24));
		idx[n++] = (u8)((s1 >> 16) & 0xff);
		idx[n++] = (u8)((s2 >>  8) & 0xff);
		idx[n++] = (u8)((s3      ) & 0xff);
		idx[n++] = (u8)((s1 >> 24));
		idx[n++] = (u8)((s2 >> 16) & 0xff);
		idx[n++] = (u8)((s3 >>  8) & 0xff);
		idx[n++] = (u8)((s0      ) & 0xff);
		idx[n++] = (u8)((s2 >> 24));
		idx[n++] = (u8)((s3 >> 16) & 0xff);
		idx[n++] = (u8)((s0 >>  8) & 0xff);
		idx[n++] = (u8)((s1      ) & 0xff);
		idx[n++] = (u8)((s3 >> 24));
		idx[n++] = (u8)((s0 >> 16) & 0xff);
		idx[n++] = (u8)((s1 >>  8) & 0xff);
		idx[n++] = (u8)((s2      ) & 0xff);

		rk += 8;
		if (--r == 0)
			break;

		s0 = Te0[(t0 >> 24)       ] ^
		     Te1[(t1 >> 16) & 0xff] ^
		     Te2[(t2 >>  8) & 0xff] ^
		     Te3[(t3      ) & 0xff] ^ rk[0];
		s1 = Te0[(t1 >> 24)       ] ^
		     Te1[(t2 >> 16) & 0xff] ^
		     Te2[(t3 >>  8) & 0xff] ^
		     Te3[(t0      ) & 0xff] ^ rk[1];
		s2 = Te0[(t2 >> 24)       ] ^
		     Te1[(t3 >> 16) & 0xff] ^
		     Te2[(t0 >>  8) & 0xff] ^
		     Te3[(t1      ) & 0xff] ^ rk[2];
		s3 = Te0[(t3 >> 24)       ] ^
		     Te1[(t0 >> 16) & 0xff] ^
		     Te2[(t1 >>  8) & 0xff] ^
		     Te3[(t2      ) & 0xff] ^ rk[3];
		idx[n++] = (u8)((t0 >> 24));
		idx[n++] = (u8)((t1 >> 16) & 0xff);
		idx[n++] = (u8)((t2 >>  8) & 0xff);
		idx[n++] = (u8)((t3      ) & 0xff);
		idx[n++] = (u8)((t1 >> 24));
		idx[n++] = (u8)((t2 >> 16) & 0xff);
		idx[n++] = (u8)((t3 >>  8) & 0xff);
		idx[n++] = (u8)((t0      ) & 0xff);
		idx[n++] = (u8)((t2 >> 24));
		idx[n++] = (u8)((t3 >> 16) & 0xff);
		idx[n++] = (u8)((t0 >>  8) & 0xff);
		idx[n++] = (u8)((t1      ) & 0xff);
		idx[n++] = (u8)((t3 >> 24));
		idx[n++] = (u8)((t0 >> 16) & 0xff);
		idx[n++] = (u8)((t1 >>  8) & 0xff);
		idx[n++] = (u8)((t2      ) & 0xff);
	}
	s0 = (Te4[(t0 >> 24)       ] & 0xff000000) ^
	     (Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^
	     (Te4[(t2 >>  8) & 0xff] & 0x0000ff00) ^
	     (Te4[(t3      ) & 0xff] & 0x000000ff) ^ rk[0];
	PUTU32(ct     , s0);
	s1 = (Te4[(t1 >> 24)       ] & 0xff000000) ^
	     (Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^
	     (Te4[(t3 >>  8) & 0xff] & 0x0000ff00) ^
	     (Te4[(t0      ) & 0xff] & 0x000000ff) ^ rk[1];
	PUTU32(ct +  4, s1);
	s2 = (Te4[(t2 >> 24)       ] & 0xff000000) ^
	     (Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^
	     (Te4[(t0 >>  8) & 0xff] & 0x0000ff00) ^
	     (Te4[(t1      ) & 0xff] & 0x000000ff) ^ rk[2];
	PUTU32(ct +  8, s2);
	s3 = (Te4[(t3 >> 24)       ] & 0xff000000) ^
	     (Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^
	     (Te4[(t1 >>  8) & 0xff] & 0x0000ff00) ^
	     (Te4[(t2      ) & 0xff] & 0x000000ff) ^ rk[3];
	PUTU32(ct + 12, s3);
	idx[n++] = (u8)((t0 >> 24));
	idx[n++] = (u8)((t1 >> 16) & 0xff);
	idx[n++] = (u8)((t2 >>  8) & 0xff);
	idx[n++] = (u8)((t3      ) & 0xff);
	idx[n++] = (u8)((t1 >> 24));
	idx[n++] = (u8)((t2 >> 16) & 0xff);
	idx[n++] = (u8)((t3 >>  8) & 0xff);
	idx[n++] = (u8)((t0      ) & 0xff);
	idx[n++] = (u8)((t2 >> 24));
	idx[n++] = (u8)((t3 >> 16) & 0xff);
	idx[n++] = (u8)((t0 >>  8) & 0xff);
	idx[n++] = (u8)((t1      ) & 0xff);
	idx[n++] = (u8)((t3 >> 24));
	idx[n++] = (u8)((t0 >> 16) & 0xff);
	idx[n++] = (u8)((t1 >>  8) & 0xff);
	idx[n++] = (u8)((t2      ) & 0xff);
	*nidx = n;
}

/* ------------------------------------------------------------------ */
/* KAT vectors: FIPS-197 Appendix C (hand-verified) + extended set
 * cross-checked byte-exact against OpenSSL 3.0.22 AES-ECB on the
 * audit host (see pack checkvec.log). */
struct rijnh_kat {
	const char *name;
	const char *key;	/* hex */
	const char *pt;		/* hex */
	const char *ct;		/* hex, expected */
	int bits;
};
static const struct rijnh_kat rijnh_kats[] = {
	{ "FIPS197-C1", "000102030405060708090a0b0c0d0e0f",
	  "00112233445566778899aabbccddeeff",
	  "69c4e0d86a7b0430d8cdb78070b4c55a", 128 },
	{ "FIPS197-C2", "000102030405060708090a0b0c0d0e0f1011121314151617",
	  "00112233445566778899aabbccddeeff",
	  "dda97ca4864cdfe06eaf70a0ec0d7191", 192 },
	{ "FIPS197-C3", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
	  "00112233445566778899aabbccddeeff",
	  "8ea2b7ca516745bfeafc49904b496089", 256 },
	{ "SP80038A-F11-b1", "2b7e151628aed2a6abf7158809cf4f3c",
	  "6bc1bee22e409f96e93d7e117393172a",
	  "3ad77bb40d7a3660a89ecaf32466ef97", 128 },
	{ "SP80038A-F11-b2", "2b7e151628aed2a6abf7158809cf4f3c",
	  "ae2d8a571e03ac9c9eb76fac45af8e51",
	  "f5d3d58503b9699de785895a96fdbaaf", 128 },
	{ "SP80038A-F13-b1", "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
	  "6bc1bee22e409f96e93d7e117393172a",
	  "bd334f1d6e45f25ff712a214571fa5cc", 192 },
	{ "X128-a", "2b7e151628aed2a6abf7158809cf4f3c",
	  "2c6f2d78583e447ab3ae6d8b2a14b7c7",
	  "e0a7e949805348048515ca561be082e0", 128 },
	{ "X128-b", "2b7e151628aed2a6abf7158809cf4f3c",
	  "46f0514f71a9f206e6b358e3291ac21a",
	  "b91e18af7d7b0adc7b8c8b806288091a", 128 },
	{ "X128-c", "ffffffffffffffffffffffffffffffff",
	  "00000000000000000000000000000000",
	  "a1f6258c877d5fcd8964484538bfc92c", 128 },
	{ "X128-d", "00000000000000000000000000000000",
	  "ffffffffffffffffffffffffffffffff",
	  "3f5b8cc9ea855a0afa7347d23e8d664e", 128 },
	{ "X192-a", "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
	  "ae2d8a571e03ac9c9eb76fac45af8e51",
	  "974104846d0ad3ad7734ecb3ecee4eef", 192 },
	{ "X192-b", "ffffffffffffffffffffffffffffffffffffffffffffffff",
	  "0123456789abcdeffedcba9876543210",
	  "74374f53375715f4aaacf5e718b7a7af", 192 },
	{ "X256-a", "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff6",
	  "6bc1bee22e409f96e93d7e117393172a",
	  "ed336c7008cd2e5ce4c5741f54cf3b2a", 256 },
	{ "X256-b", "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff6",
	  "46f0514f71a9f206e6b358e3291ac21a",
	  "7c37a3847a3d3ec10bc8ed3a1cb30208", 256 },
	{ "X256-c", "0000000000000000000000000000000000000000000000000000000000000000",
	  "ffffffffffffffffffffffffffffffff",
	  "acdace8078a32b1a182bfa4987ca1347", 256 },
	{ "X256-d", "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
	  "00000000000000000000000000000000",
	  "4bf85f1b5d54adbc307b0a048389adcb", 256 },
};
#define	NKAT	(sizeof(rijnh_kats) / sizeof(rijnh_kats[0]))

/* ------------------------------------------------------------------ */

static int
hexval(char c)
{
	if (c >= '0' && c <= '9') return (c - '0');
	if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
	if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
	return (-1);
}

static int
hex2bin(const char *hex, u8 *out, int maxlen)
{
	int n = 0, hi, lo;

	while (hex[0] != '\0' && hex[1] != '\0' && n < maxlen) {
		hi = hexval(hex[0]);
		lo = hexval(hex[1]);
		if (hi < 0 || lo < 0)
			return (-1);
		out[n++] = (u8)((hi << 4) | lo);
		hex += 2;
	}
	return (n);
}

static void
bin2hex(const u8 *b, int n, char *out)
{
	static const char hd[] = "0123456789abcdef";
	int i;

	for (i = 0; i < n; i++) {
		out[2*i]   = hd[b[i] >> 4];
		out[2*i+1] = hd[b[i] & 15];
	}
	out[2*n] = '\0';
}

static u32
rijnh_rnd(u32 *s)
{
	u32 x = *s;
	x ^= x << 13; x ^= x >> 17; x ^= x << 5;
	*s = x;
	return (x);
}

static char rijnh_out[256];	/* readback result */

static void
rijnh_setout(const char *fmt, ...)
{
	__va_list ap;

	__va_start(ap, fmt);
	kvsnprintf(rijnh_out, sizeof(rijnh_out), fmt, ap);
	__va_end(ap);
}

/* ------------------------------------------------------------------ */

static int
rijnh_run_kat(void)
{
	u8 key[32], pt[16], ct[16], rt[16], exp[16];
	char got[33];
	int i, klen, npass = 0, nfail = 0;

	for (i = 0; i < (int)NKAT; i++) {
		const struct rijnh_kat *v = &rijnh_kats[i];
		rijndael_ctx ctx;

		klen = hex2bin(v->key, key, sizeof(key));
		hex2bin(v->pt, pt, 16);
		hex2bin(v->ct, exp, 16);

		rijndael_set_key(&ctx, key, v->bits);
		rijndael_encrypt(&ctx, pt, ct);
		rijndael_decrypt(&ctx, ct, rt);

		bin2hex(ct, 16, got);
		if (memcmp(ct, exp, 16) == 0 && memcmp(rt, pt, 16) == 0) {
			kprintf("RIJNH: KAT %s aes-%d ct=%s dec-rt=OK PASS\n",
			    v->name, v->bits, got);
			npass++;
		} else {
			kprintf("RIJNH: KAT %s aes-%d ct=%s FAIL (expected %s)"
			    " dec-rt=%s\n", v->name, v->bits, got, v->ct,
			    memcmp(rt, pt, 16) == 0 ? "OK" : "BAD");
			nfail++;
		}
		(void)klen;
	}
	kprintf("RIJNH: KAT summary %d pass %d fail of %d vectors\n",
	    npass, nfail, (int)NKAT);
	rijnh_setout("KAT %d pass %d fail", npass, nfail);
	return (nfail);
}

static void
rijnh_run_setkey(int bits)
{
	u8 key[32];
	rijndael_ctx ctx;
	int i;

	for (i = 0; i < 32; i++)
		key[i] = (u8)i;

	memset(&ctx, 0, sizeof(ctx));
	kprintf("RIJNH: setkey: calling real rijndael_set_key(bits=%d)"
	    " now (ctx pre-zeroed)\n", bits);
	rijndael_set_key(&ctx, key, bits);
	kprintf("RIJNH: setkey: bits=%d -> ctx.Nr=%d ek[0]=%08x ek[7]=%08x"
	    " dk[0]=%08x\n", bits, ctx.Nr, ctx.ek[0], ctx.ek[7], ctx.dk[0]);
	rijnh_setout("setkey bits=%d Nr=%d ek0=%08x ek7=%08x",
	    bits, ctx.Nr, ctx.ek[0], ctx.ek[7]);
}

/*
 * Build a REAL rijndael_ctx at user virtual address <addr> (the
 * caller's helper mmaps one mapped page followed by an unmapped
 * guard page, and places the ctx so the ek[] walk crosses the page
 * boundary), then force Nr=0 -- exactly the state rijndael_set_key
 * stores when rijndaelKeySetupEnc returns 0 for invalid keyBits --
 * and call the real rijndael_encrypt.
 */
static void
rijnh_run_poison(unsigned long addr, int direct)
{
	rijndael_ctx *ctx = (rijndael_ctx *)addr;
	u8 key[32], pt[16], ct[16];
	int i;

	for (i = 0; i < 32; i++)
		key[i] = (u8)(0xa0 ^ i);
	for (i = 0; i < 16; i++)
		pt[i] = (u8)(0x5a ^ i);
	memset(ct, 0, sizeof(ct));

	kprintf("RIJNH: poison: ctx=%p (user VA, next page unmapped)\n", ctx);
	rijndael_set_key(ctx, key, 128);	/* valid: fills ek/dk */
	ctx->Nr = 0;				/* the broken-set_key state */
	kprintf("RIJNH: poison: ctx built (Nr was 10), ctx->Nr forced to 0;"
	    " entering rijndael%s now\n",
	    direct ? "Encrypt(ek, 0, ...)" : "_encrypt(ctx)");
	if (direct)
		rijndaelEncrypt(ctx->ek, 0, pt, ct);
	else
		rijndael_encrypt(ctx, pt, ct);
	kprintf("RIJNH: poison: SURVIVED Nr=0 encrypt; ct=%02x%02x%02x%02x\n",
	    ct[0], ct[1], ct[2], ct[3]);
	rijnh_setout("poison survived ct=%02x%02x%02x%02x",
	    ct[0], ct[1], ct[2], ct[3]);
}

static void
rijnh_run_twinval(int n)
{
	u8 key[32], pt[16], ctr[16], ctt[16], idx[256];
	u32 seed = 0x12345678;
	int bits, i, j, nmis = 0, ntot = 0, nidx;

	static const int bits3[3] = { 128, 192, 256 };

	for (j = 0; j < 3; j++) {
		bits = bits3[j];
		for (i = 0; i < n; i++) {
			rijndael_ctx ctx;
			u32 rk[4 * (RIJNDAEL_MAXNR + 1)];
			int k, nb = bits / 8;

			for (k = 0; k < nb; k++)
				key[k] = (u8)(rijnh_rnd(&seed) >> 24);
			for (k = 0; k < 16; k++)
				pt[k] = (u8)(rijnh_rnd(&seed) >> 16);

			rijndael_set_key(&ctx, key, bits);
			rijndael_encrypt(&ctx, pt, ctr);
			rijndaelKeySetupEnc(rk, key, bits);
			twin_encrypt_idx(rk, ctx.Nr, pt, ctt, idx, &nidx);
			ntot++;
			if (memcmp(ctr, ctt, 16) != 0 || nidx != 16 * ctx.Nr) {
				nmis++;
				kprintf("RIJNH: twinval MISMATCH bits=%d i=%d"
				    " nidx=%d (want %d)\n", bits, i, nidx,
				    16 * ctx.Nr);
			}
		}
	}
	kprintf("RIJNH: twinval %d vectors, %d mismatches (twin==real"
	    " required)\n", ntot, nmis);
	rijnh_setout("twinval %d vectors %d mismatches", ntot, nmis);
}

static void
rijnh_run_idx(const char *keyhex, const char *pthex, int r1only)
{
	u8 key[32], pt[16], ctr[16], ctt[16], idx[256];
	char hex[256 * 2 + 1];
	rijndael_ctx ctx;
	u32 rk[4 * (RIJNDAEL_MAXNR + 1)];
	int klen, nidx, i;

	klen = hex2bin(keyhex, key, sizeof(key));
	if (hex2bin(pthex, pt, 16) != 16 || klen <= 0 ||
	    (klen != 16 && klen != 24 && klen != 32)) {
		rijnh_setout("idx bad args");
		return;
	}
	rijndael_set_key(&ctx, key, klen * 8);
	rijndael_encrypt(&ctx, pt, ctr);
	rijndaelKeySetupEnc(rk, key, klen * 8);
	twin_encrypt_idx(rk, ctx.Nr, pt, ctt, idx, &nidx);

	if (memcmp(ctr, ctt, 16) != 0) {
		kprintf("RIJNH: idx TWIN!=REAL (must never happen) -- dump"
		    " invalid\n");
		rijnh_setout("idx twin mismatch");
		return;
	}
	if (r1only) {
		bin2hex(idx, 16, hex);
		kprintf("RIJNH: idxr1 key=%s pt=%s r1=%s\n", keyhex, pthex,
		    hex);
		rijnh_setout("idxr1 %s", hex);
	} else {
		kprintf("RIJNH: idx key=%s pt=%s bits=%d nidx=%d\n", keyhex,
		    pthex, klen * 8, nidx);
		for (i = 0; i < nidx; i += 60) {
			int ch = nidx - i;
			if (ch > 60)
				ch = 60;
			bin2hex(idx + i, ch, hex);
			kprintf("RIJNH: idx.trace[%d:%d]=%s\n", i, i + ch,
			    hex);
		}
		bin2hex(ctr, 16, hex);
		kprintf("RIJNH: idx ct(real==twin)=%s\n", hex);
		rijnh_setout("idx nidx=%d ct=%s", nidx, hex);
	}
}

static void
rijnh_run_bench(int n, const char *keyhex)
{
	u8 key[32], pt[16], ct[16];
	rijndael_ctx ctx;
	struct timespec t0, t1;
	int klen, i;
	int64_t ns;

	klen = hex2bin(keyhex, key, sizeof(key));
	if (klen != 16 && klen != 24 && klen != 32) {
		rijnh_setout("bench bad key");
		return;
	}
	for (i = 0; i < 16; i++)
		pt[i] = (u8)(i * 7);
	rijndael_set_key(&ctx, key, klen * 8);
	rijndael_encrypt(&ctx, pt, ct);	/* warm */

	nanouptime(&t0);
	for (i = 0; i < n; i++)
		rijndael_encrypt(&ctx, pt, ct);
	nanouptime(&t1);
	ns = (t1.tv_sec - t0.tv_sec) * 1000000000LL +
	    (t1.tv_nsec - t0.tv_nsec);
	kprintf("RIJNH: bench n=%d key=%s total=%lld ns per-op=%lld ns\n",
	    n, keyhex, (long long)ns, (long long)(ns / n));
	rijnh_setout("bench %lld ns %lld ns/op", (long long)ns,
	    (long long)(ns / n));
}

/* ------------------------------------------------------------------ */

static char rijnh_cmd[512];

static int
rijnh_sysctl(SYSCTL_HANDLER_ARGS)
{
	int error;

	error = sysctl_handle_string(oidp, rijnh_cmd, sizeof(rijnh_cmd), req);
	if (error != 0 || req->newptr == NULL)
		return (error);

	kprintf("RIJNH: cmd=\"%s\"\n", rijnh_cmd);

	if (strcmp(rijnh_cmd, "kat") == 0) {
		rijnh_run_kat();
	} else if (strncmp(rijnh_cmd, "setkey ", 7) == 0) {
		rijnh_run_setkey(strtoul(rijnh_cmd + 7, NULL, 0));
	} else if (strncmp(rijnh_cmd, "poison ", 7) == 0) {
		rijnh_run_poison(strtoul(rijnh_cmd + 7, NULL, 16), 0);
	} else if (strncmp(rijnh_cmd, "direct ", 7) == 0) {
		rijnh_run_poison(strtoul(rijnh_cmd + 7, NULL, 16), 1);
	} else if (strncmp(rijnh_cmd, "twinval ", 8) == 0) {
		rijnh_run_twinval(strtoul(rijnh_cmd + 8, NULL, 0));
	} else if (strncmp(rijnh_cmd, "idx ", 4) == 0) {
		char *k = rijnh_cmd + 4, *p;
		p = strchr(k, ' ');
		if (p != NULL) {
			*p++ = '\0';
			rijnh_run_idx(k, p, 0);
		} else {
			rijnh_setout("idx need key pt");
		}
	} else if (strncmp(rijnh_cmd, "idxr1 ", 6) == 0) {
		char *k = rijnh_cmd + 6, *p;
		p = strchr(k, ' ');
		if (p != NULL) {
			*p++ = '\0';
			rijnh_run_idx(k, p, 1);
		} else {
			rijnh_setout("idxr1 need key pt");
		}
	} else if (strncmp(rijnh_cmd, "bench ", 6) == 0) {
		char *k = rijnh_cmd + 6, *p;
		long n = strtol(k, &p, 0);
		if (p != NULL && *p == ' ')
			rijnh_run_bench((int)n, p + 1);
		else
			rijnh_setout("bench need n key");
	} else {
		rijnh_setout("unknown op (kat|setkey|poison|direct|twinval|"
		    "idx|idxr1|bench)");
	}
	return (0);
}

SYSCTL_NODE(_kern, OID_AUTO, rijnh, CTLFLAG_RW, 0,
    "rijndael DF-2367/DF-2368 verification harness");

SYSCTL_PROC(_kern_rijnh, OID_AUTO, test,
    CTLTYPE_STRING | CTLFLAG_RW, 0, 0, rijnh_sysctl, "A",
    "rijndael harness command channel");