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