summaryrefslogtreecommitdiff
path: root/test/tpm_test/bn_test.c
blob: 01a2119daabcc2c072ce9cc23670b658bce5d1dc (plain)
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
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifdef CR50_DCRYPTO
/* some of the functions became internal in CR50 */
#include "internal.h"
#else
#include "dcrypto.h"
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>

#include <openssl/bn.h>

/**
 * Compatibility layer for OpenSSL 1.0.x.
 * BN_bn2lebinpad and BN_lebin2bn were added in OpenSSL 1.1, to provide
 * import/export functionality as BIGNUM struct became opaque.
 */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define BN_RAND_TOP_ANY -1
#define BN_RAND_TOP_ONE 0
#define BN_RAND_TOP_TWO 1
#define BN_RAND_BOTTOM_ODD 1
#define BN_RAND_BOTTOM_ANY 0

/* export BIGNUM as little-endian padded to tolen bytes binary */
static int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
{
	int i;
	BN_ULONG l;

	bn_check_top(a);
	i = BN_num_bytes(a);
	if (tolen < i)
		return -1;
	/* Add trailing zeroes if necessary */
	if (tolen > i)
		memset(to + i, 0, tolen - i);
	to += i;
	while (i--) {
		l = a->d[i / BN_BYTES];
		to--;
		*to = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
	}
	return tolen;
}

/* import BIGNUM from little-endian binary of specified length */
static BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
{
	unsigned int i, m;
	unsigned int n;
	BN_ULONG l;
	BIGNUM *bn = NULL;

	if (ret == NULL)
		ret = bn = BN_new();
	if (ret == NULL)
		return (NULL);
	bn_check_top(ret);
	s += len;
	/* Skip trailing zeroes. */
	for (; len > 0 && s[-1] == 0; s--, len--)
		continue;
	n = len;
	if (n == 0) {
		ret->top = 0;
		return ret;
	}
	i = ((n - 1) / BN_BYTES) + 1;
	m = ((n - 1) % (BN_BYTES));
	if (bn_wexpand(ret, (int)i) == NULL) {
		BN_free(bn);
		return NULL;
	}
	ret->top = i;
	ret->neg = 0;
	l = 0;
	while (n--) {
		s--;
		l = (l << 8L) | *s;
		if (m-- == 0) {
			ret->d[--i] = l;
			l = 0;
			m = BN_BYTES - 1;
		}
	}
	/*
	 * need to call this due to clear byte at top if avoiding
	 * having the top bit set (-ve number)
	 */
	bn_correct_top(ret);
	return ret;
}
#endif

#define MAX_BN_TEST_SIZE 2048

static char to_hexchar(unsigned char c)
{
	return (c < 10) ? c + '0' : c - 10 + 'A';
}

static void hex_print(FILE *fp, unsigned char *d, int size)
{
	char buf[MAX_BN_TEST_SIZE / 4 + 1];
	int i = 0;

	assert((size * 2) + 1 <= sizeof(buf));
	while (i < size) {
		buf[i * 2] = to_hexchar((d[size - i - 1] >> 4) & 0xF);
		buf[i * 2 + 1] = to_hexchar(d[size - i - 1] & 0xF);
		i++;
	};
	buf[size * 2] = 0;
	fprintf(fp, buf);
}

static void dcrypto_print(FILE *fp, struct LITE_BIGNUM *d, int size)
{
	hex_print(fp, (unsigned char *)d->d, size);
}

static int bn_dcrypto_cmpeq(const BIGNUM *b, struct LITE_BIGNUM *d)
{
	unsigned char buf[MAX_BN_TEST_SIZE / 8];
	int size = BN_num_bytes(b);

	assert(size <= sizeof(buf));
	BN_bn2lebinpad(b, buf, size);
	return memcmp(d->d, buf, size);
}

/* Convert OpenSSL BIGNUM to Dcrypto, assumes caller provides buffer */
static void bn_to_dcrypto(const BIGNUM *b, struct LITE_BIGNUM *d, uint32_t *buf,
			  size_t bufsize)
{
	int bn_size = BN_num_bytes(b);

	assert(bn_size <= bufsize);
	memset(buf, 0, bufsize);
	/**
	 * OpenSSL 1.0 was only working for little-endian architectures (x86)
	 * and had direct access to BIGNUM structure, so DCRYPTO_bn_wrap which
	 * just sets a pointer to user provided buffer as source for
	 * LITE_BIGNUM could be applied to data in BIGNUM as is.
	 * In OpenSSL 1.1 BIGNUM became opaque, so we need to export binary
	 * to get data in little-endian format which used by DCRYPTO_*.
	 */
	BN_bn2lebinpad(b, (unsigned char *)buf, bn_size);
	DCRYPTO_bn_wrap(d, buf, bufsize);
}

static int test_bn_modinv_helper(const BIGNUM *E, BN_CTX *ctx, int mod_top,
				 int mod_bottom)
{
	int i, result = 0;
	BIGNUM *MOD, *r;

	BN_CTX_start(ctx);
	MOD = BN_CTX_get(ctx);
	r = BN_CTX_get(ctx);

	for (i = 0; i < 1000; i++) {
		uint32_t m_buf[MAX_BN_TEST_SIZE / LITE_BN_BITS2];
		uint32_t d_buf[MAX_BN_TEST_SIZE / LITE_BN_BITS2];
		uint32_t e_buf[MAX_BN_TEST_SIZE / LITE_BN_BITS2];
		int has_inverse;
		int test_inverse;

		struct LITE_BIGNUM m;
		struct LITE_BIGNUM e;
		struct LITE_BIGNUM d;

		/* Top bit set, bottom bit clear. */
		BN_rand(MOD, MAX_BN_TEST_SIZE, mod_top, mod_bottom);

		if (BN_mod_inverse(r, E, MOD, ctx))
			has_inverse = 1;
		else
			has_inverse = 0;
		bn_to_dcrypto(MOD, &m, m_buf, sizeof(m_buf));
		bn_to_dcrypto(E, &e, e_buf, sizeof(e_buf));

		bn_init(&d, d_buf, sizeof(d_buf));

		test_inverse = bn_modinv_vartime(&d, &e, &m);

		if (test_inverse != has_inverse) {
			fprintf(stderr,
				"ossl inverse: %d, dcrypto inverse: %d\n",
				has_inverse, test_inverse);
			fprintf(stderr, "d : ");
			BN_print_fp(stderr, r);
			fprintf(stderr, "\n");

			fprintf(stderr, "e : ");
			BN_print_fp(stderr, E);
			fprintf(stderr, "\n");

			fprintf(stderr, "M : ");
			BN_print_fp(stderr, MOD);
			fprintf(stderr, "\n");
			result = 1;
			goto fail;
		}

		if (has_inverse) {
			if (bn_dcrypto_cmpeq(r, &d) != 0) {
				fprintf(stderr,
					"dcrypto bn_modinv_vartime fail\n");
				fprintf(stderr, "d : ");
				BN_print_fp(stderr, r);
				fprintf(stderr, "\n dd: ");
				dcrypto_print(stderr, &d, BN_num_bytes(r));
				fprintf(stderr, "\n");

				fprintf(stderr, "e : ");
				BN_print_fp(stderr, E);
				fprintf(stderr, "\n");

				fprintf(stderr, "M : ");
				BN_print_fp(stderr, MOD);
				fprintf(stderr, "\n");

				result = 1;
				goto fail;
			}
		}
	}
fail:
	BN_CTX_end(ctx);
	return result;
}

static int test_bn_modinv(void)
{
	BN_CTX *ctx;
	BIGNUM *E;
	int result = 1;

	ctx = BN_CTX_new();
	BN_CTX_start(ctx);

	E = BN_CTX_get(ctx);

	BN_rand(E, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
	/* Top bit set, bottom bit clear. */
	if (test_bn_modinv_helper(E, ctx, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
		goto fail;

	if (test_bn_modinv_helper(E, ctx, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))
		goto fail;

	BN_rand(E, 32, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD);
	if (test_bn_modinv_helper(E, ctx, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
		goto fail;

	BN_rand(E, 17, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ODD);
	if (test_bn_modinv_helper(E, ctx, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
		goto fail;

	BN_set_word(E, 3);
	if (test_bn_modinv_helper(E, ctx, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
		goto fail;

	BN_set_word(E, 65537);
	if (test_bn_modinv_helper(E, ctx, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
		goto fail;

	result = 0;
fail:
	BN_CTX_end(ctx);
	BN_CTX_free(ctx);
	return result;
}

/* Build a BIGNUM with following template:
 * 11111111111111110000001111111111111000000000000123455667
 * size - size in bits
 * front_ones        mid_ones_pos,mid_ones,     rand_low
 * front_ones - number of 1 bits in highest position
 * mid_ones_pos - starting position of middle ones
 * mid_ones - number of 1 bits in the middle
 * rand_low - number of random low bits
 */
static BIGNUM *bn_gen(BIGNUM *out, int size, int front_ones, int mid_ones_pos,
		      int mid_ones, int rand_low)
{
	unsigned char n[MAX_BN_TEST_SIZE / 8] = {};

	assert(size <= sizeof(n) * 8);
	assert(front_ones < size);
	assert(mid_ones_pos < (size - front_ones - 1));
	assert(mid_ones < (size - mid_ones_pos - 1));
	assert(rand_low < (size - mid_ones_pos - mid_ones - 1));
	/* generate little-endian representation */
	while (front_ones) {
		n[(size - front_ones) / 8] |= 1 << ((size - front_ones) & 7);
		front_ones--;
	}
	while (mid_ones) {
		n[(mid_ones_pos - mid_ones) / 8] |=
			1 << ((mid_ones_pos - mid_ones) & 7);
		mid_ones--;
	}
	while (rand_low) {
		n[(rand_low - 1) / 8] |= (rand() & 1) << ((rand_low - 1) & 7);
		rand_low--;
	}

	return BN_lebin2bn(n, size / 8, out);
}

static int test_bn_div(void)
{
	const int NSIZE = MAX_BN_TEST_SIZE;
	const int PSIZE = MAX_BN_TEST_SIZE / 2;
	BIGNUM *N, *P, *Q, *R;
	BN_CTX *ctx;
	int result = 0, total = 0, prev = 1;
	int nf, nmps, nms, pf, pmps, pms;
	struct LITE_BIGNUM p;
	struct LITE_BIGNUM q;
	struct LITE_BIGNUM n;
	struct LITE_BIGNUM r;

	uint32_t p_buff[MAX_BN_TEST_SIZE / LITE_BN_BITS2];
	uint32_t q_buff[MAX_BN_TEST_SIZE / LITE_BN_BITS2];
	uint32_t n_buff[MAX_BN_TEST_SIZE / LITE_BN_BITS2];
	uint32_t r_buff[MAX_BN_TEST_SIZE / LITE_BN_BITS2];

	ctx = BN_CTX_new();
	BN_CTX_start(ctx);
	N = BN_CTX_get(ctx);
	P = BN_CTX_get(ctx);
	Q = BN_CTX_get(ctx);
	R = BN_CTX_get(ctx);

	for (nf = 1; nf <= NSIZE / 8; nf++)
	for (nmps = NSIZE / 16; nmps < (NSIZE / 16) + 2; nmps++)
	for (nms = NSIZE / 32; nms < (NSIZE / 32) + 2; nms++) {
		N = bn_gen(N, NSIZE, nf, nmps, nms, (nmps - nms) / 2);
		for (pf = 1; pf <= PSIZE / 4; pf++)
		for (pmps = PSIZE / 16; pmps < (PSIZE / 16) + 2; pmps++)
		for (pms = PSIZE / 32; pms < (PSIZE / 32) + 2; pms++) {
			P = bn_gen(P, PSIZE, pf, pmps, pms, (pmps - pms) / 2);
			total++;
			bn_to_dcrypto(N, &n, n_buff, sizeof(n_buff));
			bn_to_dcrypto(P, &p, p_buff, sizeof(p_buff));
			DCRYPTO_bn_wrap(&q, q_buff, sizeof(q_buff));
			DCRYPTO_bn_wrap(&r, r_buff, sizeof(r_buff));

			BN_div(Q, R, N, P, ctx);
			DCRYPTO_bn_div(&q, &r, &n, &p);

			if ((bn_dcrypto_cmpeq(Q, &q) != 0) ||
			    (bn_dcrypto_cmpeq(R, &r) != 0)) {
				result++;
				if (result > prev) {
					/* print only 1 sample in 100000 */
					prev = result + 100000;
					fprintf(stderr, "N : ");
					BN_print_fp(stderr, N);
					fprintf(stderr, "\n");
					fprintf(stderr, "P : ");
					BN_print_fp(stderr, P);
					fprintf(stderr, "\n");

					fprintf(stderr, "Q : ");
					BN_print_fp(stderr, Q);
					fprintf(stderr, "\nQd: ");
					dcrypto_print(stderr, &q,
						      BN_num_bytes(Q));
					fprintf(stderr, "\n");

					fprintf(stderr, "R : ");
					BN_print_fp(stderr, R);
					fprintf(stderr, "\nRd: ");
					dcrypto_print(stderr, &r,
						      BN_num_bytes(R));
					fprintf(stderr, "\n");
				}
			}
		}
	}
	if (result)
		fprintf(stderr, "DCRYPTO_bn_div: total=%d, failures=%d\n",
			total, result);
	BN_CTX_end(ctx);
	BN_CTX_free(ctx);

	return result;
}

void *always_memset(void *s, int c, size_t n)
{
	memset(s, c, n);
	return s;
}

void watchdog_reload(void)
{
}

#ifdef CR50_DCRYPTO
/* For CR50 we need to mock additional functions. */
bool fips_rand_bytes(void *buffer, size_t len)
{
	uint8_t *b, *end;
	static unsigned int seed = 1;

	for (b = buffer, end = b+len; b != end; b++)
		*b = (uint8_t)rand_r(&seed);
	return true;
}

const struct fips_vtable *fips_vtable;

void fips_throw_err(enum fips_status err)
{
}

uint64_t fips_trng_rand32(void)
{
	static unsigned int seed = 100;

	return (uint64_t)(rand_r(&seed) & 0xffffffff) | (1ULL << 32);
}
#endif

int main(void)
{
	assert(test_bn_modinv() == 0);
	assert(test_bn_div() == 0);
	fprintf(stderr, "PASS\n");
	return 0;
}