xref: /openbsd-src/regress/lib/libcrypto/crypto/crypto_test.c (revision d7063ec0403eacbb17141dfc1708a6dabf061034)
1*d7063ec0Sjsing /*	$OpenBSD: crypto_test.c,v 1.2 2024/11/08 14:06:34 jsing Exp $	*/
2bd76828aSjsing /*
3bd76828aSjsing  * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4bd76828aSjsing  *
5bd76828aSjsing  * Permission to use, copy, modify, and distribute this software for any
6bd76828aSjsing  * purpose with or without fee is hereby granted, provided that the above
7bd76828aSjsing  * copyright notice and this permission notice appear in all copies.
8bd76828aSjsing  *
9bd76828aSjsing  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10bd76828aSjsing  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11bd76828aSjsing  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12bd76828aSjsing  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13bd76828aSjsing  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14bd76828aSjsing  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15bd76828aSjsing  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16bd76828aSjsing  */
17bd76828aSjsing 
18bd76828aSjsing #include <stdint.h>
19bd76828aSjsing #include <stdio.h>
20*d7063ec0Sjsing #include <stdlib.h>
21bd76828aSjsing 
22bd76828aSjsing #include "crypto_internal.h"
23bd76828aSjsing 
24bd76828aSjsing static int
25*d7063ec0Sjsing test_ct_size_t(void)
26bd76828aSjsing {
27*d7063ec0Sjsing 	size_t a, b, mask;
28*d7063ec0Sjsing 	uint8_t buf[8];
29*d7063ec0Sjsing 	int i, j;
30bd76828aSjsing 	int failed = 1;
31bd76828aSjsing 
32*d7063ec0Sjsing 	CTASSERT(sizeof(a) <= sizeof(buf));
33*d7063ec0Sjsing 
34*d7063ec0Sjsing 	for (i = 0; i < 4096; i++) {
35*d7063ec0Sjsing 		arc4random_buf(buf, sizeof(buf));
36*d7063ec0Sjsing 		memcpy(&a, buf, sizeof(a));
37*d7063ec0Sjsing 
38*d7063ec0Sjsing 		if ((a != 0) != crypto_ct_ne_zero(a)) {
39*d7063ec0Sjsing 			fprintf(stderr, "FAIL: crypto_ct_ne_zero(0x%llx) = %d, "
40*d7063ec0Sjsing 			    "want %d\n", (unsigned long long)a,
41*d7063ec0Sjsing 			    crypto_ct_ne_zero(a), a != 0);
42*d7063ec0Sjsing 			goto failure;
43*d7063ec0Sjsing 		}
44*d7063ec0Sjsing 		mask = (a != 0) ? -1 : 0;
45*d7063ec0Sjsing 		if (mask != crypto_ct_ne_zero_mask(a)) {
46*d7063ec0Sjsing 			fprintf(stderr, "FAIL: crypto_ct_ne_zero_mask(0x%llx) = "
47*d7063ec0Sjsing 			    "0x%llx, want 0x%llx\n", (unsigned long long)a,
48*d7063ec0Sjsing 			    (unsigned long long)crypto_ct_ne_zero_mask(a),
49*d7063ec0Sjsing 			    (unsigned long long)mask);
50*d7063ec0Sjsing 			goto failure;
51*d7063ec0Sjsing 		}
52*d7063ec0Sjsing 		if ((a == 0) != crypto_ct_eq_zero(a)) {
53*d7063ec0Sjsing 			fprintf(stderr, "FAIL: crypto_ct_eq_zero(0x%llx) = %d, "
54*d7063ec0Sjsing 			    "want %d\n", (unsigned long long)a,
55*d7063ec0Sjsing 			    crypto_ct_ne_zero(a), a != 0);
56*d7063ec0Sjsing 			goto failure;
57*d7063ec0Sjsing 		}
58*d7063ec0Sjsing 		mask = (a == 0) ? -1 : 0;
59*d7063ec0Sjsing 		if (mask != crypto_ct_eq_zero_mask(a)) {
60*d7063ec0Sjsing 			fprintf(stderr, "FAIL: crypto_ct_eq_zero_mask(0x%llx) = "
61*d7063ec0Sjsing 			    "0x%llx, want 0x%llx\n", (unsigned long long)a,
62*d7063ec0Sjsing 			    (unsigned long long)crypto_ct_ne_zero_mask(a),
63*d7063ec0Sjsing 			    (unsigned long long)mask);
64*d7063ec0Sjsing 			goto failure;
65*d7063ec0Sjsing 		}
66*d7063ec0Sjsing 
67*d7063ec0Sjsing 		for (j = 0; j < 4096; j++) {
68*d7063ec0Sjsing 			arc4random_buf(buf, sizeof(buf));
69*d7063ec0Sjsing 			memcpy(&b, buf, sizeof(b));
70*d7063ec0Sjsing 
71*d7063ec0Sjsing 			if ((a < b) != crypto_ct_lt(a, b)) {
72*d7063ec0Sjsing 				fprintf(stderr, "FAIL: crypto_ct_lt(0x%llx, "
73*d7063ec0Sjsing 				    "0x%llx) = %d, want %d\n",
74*d7063ec0Sjsing 				    (unsigned long long)a,
75*d7063ec0Sjsing 				    (unsigned long long)b,
76*d7063ec0Sjsing 				    crypto_ct_lt(a, b), a < b);
77*d7063ec0Sjsing 				goto failure;
78*d7063ec0Sjsing 			}
79*d7063ec0Sjsing 			mask = (a < b) ? -1 : 0;
80*d7063ec0Sjsing 			if (mask != crypto_ct_lt_mask(a, b)) {
81*d7063ec0Sjsing 				fprintf(stderr, "FAIL: crypto_ct_lt_mask(0x%llx, "
82*d7063ec0Sjsing 				    "0x%llx) = 0x%llx, want 0x%llx\n",
83*d7063ec0Sjsing 				    (unsigned long long)a,
84*d7063ec0Sjsing 				    (unsigned long long)b,
85*d7063ec0Sjsing 				    (unsigned long long)crypto_ct_lt_mask(a, b),
86*d7063ec0Sjsing 				    (unsigned long long)mask);
87*d7063ec0Sjsing 				goto failure;
88*d7063ec0Sjsing 			}
89*d7063ec0Sjsing 			if ((a > b) != crypto_ct_gt(a, b)) {
90*d7063ec0Sjsing 				fprintf(stderr, "FAIL: crypto_ct_gt(0x%llx, "
91*d7063ec0Sjsing 				    "0x%llx) = %d, want %d\n",
92*d7063ec0Sjsing 				    (unsigned long long)a,
93*d7063ec0Sjsing 				    (unsigned long long)b,
94*d7063ec0Sjsing 				    crypto_ct_gt(a, b), a > b);
95*d7063ec0Sjsing 				goto failure;
96*d7063ec0Sjsing 			}
97*d7063ec0Sjsing 			mask = (a > b) ? -1 : 0;
98*d7063ec0Sjsing 			if (mask != crypto_ct_gt_mask(a, b)) {
99*d7063ec0Sjsing 				fprintf(stderr, "FAIL: crypto_ct_gt_mask(0x%llx, "
100*d7063ec0Sjsing 				    "0x%llx) = 0x%llx, want 0x%llx\n",
101*d7063ec0Sjsing 				    (unsigned long long)a,
102*d7063ec0Sjsing 				    (unsigned long long)b,
103*d7063ec0Sjsing 				    (unsigned long long)crypto_ct_gt_mask(a, b),
104*d7063ec0Sjsing 				    (unsigned long long)mask);
105*d7063ec0Sjsing 				goto failure;
106*d7063ec0Sjsing 			}
107*d7063ec0Sjsing 		}
108*d7063ec0Sjsing 	}
109*d7063ec0Sjsing 
110*d7063ec0Sjsing 	failed = 0;
111*d7063ec0Sjsing 
112*d7063ec0Sjsing  failure:
113*d7063ec0Sjsing 	return failed;
114*d7063ec0Sjsing }
115*d7063ec0Sjsing 
116*d7063ec0Sjsing static int
117*d7063ec0Sjsing test_ct_u8(void)
118*d7063ec0Sjsing {
119*d7063ec0Sjsing 	uint8_t a, b, mask;
120*d7063ec0Sjsing 	int failed = 1;
121*d7063ec0Sjsing 
122*d7063ec0Sjsing 	a = 0;
123bd76828aSjsing 
124bd76828aSjsing 	do {
125*d7063ec0Sjsing 		if ((a != 0) != crypto_ct_ne_zero_u8(a)) {
126bd76828aSjsing 			fprintf(stderr, "FAIL: crypto_ct_ne_zero_u8(%d) = %d, "
127*d7063ec0Sjsing 			    "want %d\n", a, crypto_ct_ne_zero_u8(a), a != 0);
128bd76828aSjsing 			goto failure;
129bd76828aSjsing 		}
130*d7063ec0Sjsing 		mask = (a != 0) ? -1 : 0;
131*d7063ec0Sjsing 		if (mask != crypto_ct_ne_zero_mask_u8(a)) {
132bd76828aSjsing 			fprintf(stderr, "FAIL: crypto_ct_ne_zero_mask_u8(%d) = %x, "
133*d7063ec0Sjsing 			    "want %x\n", a, crypto_ct_ne_zero_mask_u8(a), mask);
134bd76828aSjsing 			goto failure;
135bd76828aSjsing 		}
136*d7063ec0Sjsing 		if ((a == 0) != crypto_ct_eq_zero_u8(a)) {
137bd76828aSjsing 			fprintf(stderr, "FAIL: crypto_ct_eq_zero_u8(%d) = %d, "
138*d7063ec0Sjsing 			    "want %d\n", a, crypto_ct_ne_zero_u8(a), a != 0);
139bd76828aSjsing 			goto failure;
140bd76828aSjsing 		}
141*d7063ec0Sjsing 		mask = (a == 0) ? -1 : 0;
142*d7063ec0Sjsing 		if (mask != crypto_ct_eq_zero_mask_u8(a)) {
143bd76828aSjsing 			fprintf(stderr, "FAIL: crypto_ct_eq_zero_mask_u8(%d) = %x, "
144*d7063ec0Sjsing 			    "want %x\n", a, crypto_ct_ne_zero_mask_u8(a), mask);
145bd76828aSjsing 			goto failure;
146bd76828aSjsing 		}
147bd76828aSjsing 
148*d7063ec0Sjsing 		b = 0;
149bd76828aSjsing 
150bd76828aSjsing 		do {
151*d7063ec0Sjsing 			if ((a != b) != crypto_ct_ne_u8(a, b)) {
152bd76828aSjsing 				fprintf(stderr, "FAIL: crypto_ct_ne_u8(%d, %d) = %d, "
153*d7063ec0Sjsing 				    "want %d\n", a, b, crypto_ct_ne_u8(a, b), a != b);
154bd76828aSjsing 				goto failure;
155bd76828aSjsing 			}
156*d7063ec0Sjsing 			mask = (a != b) ? -1 : 0;
157*d7063ec0Sjsing 			if (mask != crypto_ct_ne_mask_u8(a, b)) {
158bd76828aSjsing 				fprintf(stderr, "FAIL: crypto_ct_ne_mask_u8(%d, %d) = %x, "
159*d7063ec0Sjsing 				    "want %x\n", a, b, crypto_ct_ne_mask_u8(a, b), mask);
160bd76828aSjsing 				goto failure;
161bd76828aSjsing 			}
162*d7063ec0Sjsing 			if ((a == b) != crypto_ct_eq_u8(a, b)) {
163bd76828aSjsing 				fprintf(stderr, "FAIL: crypto_ct_eq_u8(%d, %d) = %d, "
164*d7063ec0Sjsing 				    "want %d\n", a, b, crypto_ct_eq_u8(a, b), a != b);
165bd76828aSjsing 				goto failure;
166bd76828aSjsing 			}
167*d7063ec0Sjsing 			mask = (a == b) ? -1 : 0;
168*d7063ec0Sjsing 			if (mask != crypto_ct_eq_mask_u8(a, b)) {
169bd76828aSjsing 				fprintf(stderr, "FAIL: crypto_ct_eq_mask_u8(%d, %d) = %x, "
170*d7063ec0Sjsing 				    "want %x\n", a, b, crypto_ct_eq_mask_u8(a, b), mask);
171bd76828aSjsing 				goto failure;
172bd76828aSjsing 			}
173*d7063ec0Sjsing 		} while (++b != 0);
174*d7063ec0Sjsing 	} while (++a != 0);
175bd76828aSjsing 
176bd76828aSjsing 	failed = 0;
177bd76828aSjsing 
178bd76828aSjsing  failure:
179bd76828aSjsing 	return failed;
180bd76828aSjsing }
181bd76828aSjsing 
182bd76828aSjsing int
183bd76828aSjsing main(int argc, char **argv)
184bd76828aSjsing {
185bd76828aSjsing 	int failed = 0;
186bd76828aSjsing 
187*d7063ec0Sjsing 	failed |= test_ct_size_t();
188bd76828aSjsing 	failed |= test_ct_u8();
189bd76828aSjsing 
190bd76828aSjsing 	return failed;
191bd76828aSjsing }
192