summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/crypto/crypto_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/crypto/crypto_test.c')
-rw-r--r--src/regress/lib/libcrypto/crypto/crypto_test.c146
1 files changed, 120 insertions, 26 deletions
diff --git a/src/regress/lib/libcrypto/crypto/crypto_test.c b/src/regress/lib/libcrypto/crypto/crypto_test.c
index 38ee2d57d4..1b89b0b378 100644
--- a/src/regress/lib/libcrypto/crypto/crypto_test.c
+++ b/src/regress/lib/libcrypto/crypto/crypto_test.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_test.c,v 1.1 2024/04/25 14:27:29 jsing Exp $ */ 1/* $OpenBSD: crypto_test.c,v 1.2 2024/11/08 14:06:34 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -17,68 +17,161 @@
17 17
18#include <stdint.h> 18#include <stdint.h>
19#include <stdio.h> 19#include <stdio.h>
20#include <stdlib.h>
20 21
21#include "crypto_internal.h" 22#include "crypto_internal.h"
22 23
23static int 24static int
25test_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
116static int
24test_ct_u8(void) 117test_ct_u8(void)
25{ 118{
26 uint8_t i, j, mask; 119 uint8_t a, b, mask;
27 int failed = 1; 120 int failed = 1;
28 121
29 i = 0; 122 a = 0;
30 123
31 do { 124 do {
32 if ((i != 0) != crypto_ct_ne_zero_u8(i)) { 125 if ((a != 0) != crypto_ct_ne_zero_u8(a)) {
33 fprintf(stderr, "FAIL: crypto_ct_ne_zero_u8(%d) = %d, " 126 fprintf(stderr, "FAIL: crypto_ct_ne_zero_u8(%d) = %d, "
34 "want %d\n", i, crypto_ct_ne_zero_u8(i), i != 0); 127 "want %d\n", a, crypto_ct_ne_zero_u8(a), a != 0);
35 goto failure; 128 goto failure;
36 } 129 }
37 mask = (i != 0) ? 0xff : 0x00; 130 mask = (a != 0) ? -1 : 0;
38 if (mask != crypto_ct_ne_zero_mask_u8(i)) { 131 if (mask != crypto_ct_ne_zero_mask_u8(a)) {
39 fprintf(stderr, "FAIL: crypto_ct_ne_zero_mask_u8(%d) = %x, " 132 fprintf(stderr, "FAIL: crypto_ct_ne_zero_mask_u8(%d) = %x, "
40 "want %x\n", i, crypto_ct_ne_zero_mask_u8(i), mask); 133 "want %x\n", a, crypto_ct_ne_zero_mask_u8(a), mask);
41 goto failure; 134 goto failure;
42 } 135 }
43 if ((i == 0) != crypto_ct_eq_zero_u8(i)) { 136 if ((a == 0) != crypto_ct_eq_zero_u8(a)) {
44 fprintf(stderr, "FAIL: crypto_ct_eq_zero_u8(%d) = %d, " 137 fprintf(stderr, "FAIL: crypto_ct_eq_zero_u8(%d) = %d, "
45 "want %d\n", i, crypto_ct_ne_zero_u8(i), i != 0); 138 "want %d\n", a, crypto_ct_ne_zero_u8(a), a != 0);
46 goto failure; 139 goto failure;
47 } 140 }
48 mask = (i == 0) ? 0xff : 0x00; 141 mask = (a == 0) ? -1 : 0;
49 if (mask != crypto_ct_eq_zero_mask_u8(i)) { 142 if (mask != crypto_ct_eq_zero_mask_u8(a)) {
50 fprintf(stderr, "FAIL: crypto_ct_eq_zero_mask_u8(%d) = %x, " 143 fprintf(stderr, "FAIL: crypto_ct_eq_zero_mask_u8(%d) = %x, "
51 "want %x\n", i, crypto_ct_ne_zero_mask_u8(i), mask); 144 "want %x\n", a, crypto_ct_ne_zero_mask_u8(a), mask);
52 goto failure; 145 goto failure;
53 } 146 }
54 147
55 j = 0; 148 b = 0;
56 149
57 do { 150 do {
58 if ((i != j) != crypto_ct_ne_u8(i, j)) { 151 if ((a != b) != crypto_ct_ne_u8(a, b)) {
59 fprintf(stderr, "FAIL: crypto_ct_ne_u8(%d, %d) = %d, " 152 fprintf(stderr, "FAIL: crypto_ct_ne_u8(%d, %d) = %d, "
60 "want %d\n", i, j, crypto_ct_ne_u8(i, j), i != j); 153 "want %d\n", a, b, crypto_ct_ne_u8(a, b), a != b);
61 goto failure; 154 goto failure;
62 } 155 }
63 mask = (i != j) ? 0xff : 0x00; 156 mask = (a != b) ? -1 : 0;
64 if (mask != crypto_ct_ne_mask_u8(i, j)) { 157 if (mask != crypto_ct_ne_mask_u8(a, b)) {
65 fprintf(stderr, "FAIL: crypto_ct_ne_mask_u8(%d, %d) = %x, " 158 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); 159 "want %x\n", a, b, crypto_ct_ne_mask_u8(a, b), mask);
67 goto failure; 160 goto failure;
68 } 161 }
69 if ((i == j) != crypto_ct_eq_u8(i, j)) { 162 if ((a == b) != crypto_ct_eq_u8(a, b)) {
70 fprintf(stderr, "FAIL: crypto_ct_eq_u8(%d, %d) = %d, " 163 fprintf(stderr, "FAIL: crypto_ct_eq_u8(%d, %d) = %d, "
71 "want %d\n", i, j, crypto_ct_eq_u8(i, j), i != j); 164 "want %d\n", a, b, crypto_ct_eq_u8(a, b), a != b);
72 goto failure; 165 goto failure;
73 } 166 }
74 mask = (i == j) ? 0xff : 0x00; 167 mask = (a == b) ? -1 : 0;
75 if (mask != crypto_ct_eq_mask_u8(i, j)) { 168 if (mask != crypto_ct_eq_mask_u8(a, b)) {
76 fprintf(stderr, "FAIL: crypto_ct_eq_mask_u8(%d, %d) = %x, " 169 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); 170 "want %x\n", a, b, crypto_ct_eq_mask_u8(a, b), mask);
78 goto failure; 171 goto failure;
79 } 172 }
80 } while (++j != 0); 173 } while (++b != 0);
81 } while (++i != 0); 174 } while (++a != 0);
82 175
83 failed = 0; 176 failed = 0;
84 177
@@ -91,6 +184,7 @@ main(int argc, char **argv)
91{ 184{
92 int failed = 0; 185 int failed = 0;
93 186
187 failed |= test_ct_size_t();
94 failed |= test_ct_u8(); 188 failed |= test_ct_u8();
95 189
96 return failed; 190 return failed;