1 /*	$OpenBSD: crypto_test.c,v 1.1 2024/04/25 14:27:29 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 
21 #include "crypto_internal.h"
22 
23 static int
24 test_ct_u8(void)
25 {
26 	uint8_t i, j, mask;
27 	int failed = 1;
28 
29 	i = 0;
30 
31 	do {
32 		if ((i != 0) != crypto_ct_ne_zero_u8(i)) {
33 			fprintf(stderr, "FAIL: crypto_ct_ne_zero_u8(%d) = %d, "
34 			    "want %d\n", i, crypto_ct_ne_zero_u8(i), i != 0);
35 			goto failure;
36 		}
37 		mask = (i != 0) ? 0xff : 0x00;
38 		if (mask != crypto_ct_ne_zero_mask_u8(i)) {
39 			fprintf(stderr, "FAIL: crypto_ct_ne_zero_mask_u8(%d) = %x, "
40 			    "want %x\n", i, crypto_ct_ne_zero_mask_u8(i), mask);
41 			goto failure;
42 		}
43 		if ((i == 0) != crypto_ct_eq_zero_u8(i)) {
44 			fprintf(stderr, "FAIL: crypto_ct_eq_zero_u8(%d) = %d, "
45 			    "want %d\n", i, crypto_ct_ne_zero_u8(i), i != 0);
46 			goto failure;
47 		}
48 		mask = (i == 0) ? 0xff : 0x00;
49 		if (mask != crypto_ct_eq_zero_mask_u8(i)) {
50 			fprintf(stderr, "FAIL: crypto_ct_eq_zero_mask_u8(%d) = %x, "
51 			    "want %x\n", i, crypto_ct_ne_zero_mask_u8(i), mask);
52 			goto failure;
53 		}
54 
55 		j = 0;
56 
57 		do {
58 			if ((i != j) != crypto_ct_ne_u8(i, j)) {
59 				fprintf(stderr, "FAIL: crypto_ct_ne_u8(%d, %d) = %d, "
60 				    "want %d\n", i, j, crypto_ct_ne_u8(i, j), i != j);
61 				goto failure;
62 			}
63 			mask = (i != j) ? 0xff : 0x00;
64 			if (mask != crypto_ct_ne_mask_u8(i, j)) {
65 				fprintf(stderr, "FAIL: crypto_ct_ne_mask_u8(%d, %d) = %x, "
66 				    "want %x\n", i, j, crypto_ct_ne_mask_u8(i, j), mask);
67 				goto failure;
68 			}
69 			if ((i == j) != crypto_ct_eq_u8(i, j)) {
70 				fprintf(stderr, "FAIL: crypto_ct_eq_u8(%d, %d) = %d, "
71 				    "want %d\n", i, j, crypto_ct_eq_u8(i, j), i != j);
72 				goto failure;
73 			}
74 			mask = (i == j) ? 0xff : 0x00;
75 			if (mask != crypto_ct_eq_mask_u8(i, j)) {
76 				fprintf(stderr, "FAIL: crypto_ct_eq_mask_u8(%d, %d) = %x, "
77 				    "want %x\n", i, j, crypto_ct_eq_mask_u8(i, j), mask);
78 				goto failure;
79 			}
80 		} while (++j != 0);
81 	} while (++i != 0);
82 
83 	failed = 0;
84 
85  failure:
86 	return failed;
87 }
88 
89 int
90 main(int argc, char **argv)
91 {
92 	int failed = 0;
93 
94 	failed |= test_ct_u8();
95 
96 	return failed;
97 }
98