/src/cryptofuzz/tests.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "tests.h" |
2 | | #include <fuzzing/datasource/id.hpp> |
3 | | #include <cryptofuzz/repository.h> |
4 | | #include <cryptofuzz/util.h> |
5 | | #include <boost/multiprecision/cpp_int.hpp> |
6 | | #include <iostream> |
7 | | |
8 | | namespace cryptofuzz { |
9 | | namespace tests { |
10 | | |
11 | | template <class ResultType, class OperationType> |
12 | 2.86k | void verifyKeySize(const OperationType& op, const ResultType& result) { |
13 | 2.86k | if ( result != std::nullopt && op.keySize != result->GetSize() ) { |
14 | | /* TODO include module name in abort message */ |
15 | 0 | util::abort({op.Name(), "invalid keySize"}); |
16 | 0 | } |
17 | 2.86k | } void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_SCRYPT>(cryptofuzz::operation::KDF_SCRYPT const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 153 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 153 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 153 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_HKDF>(cryptofuzz::operation::KDF_HKDF const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 930 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 930 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 930 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_TLS1_PRF>(cryptofuzz::operation::KDF_TLS1_PRF const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 134 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 134 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 134 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_PBKDF>(cryptofuzz::operation::KDF_PBKDF const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 161 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 161 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 161 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_PBKDF1>(cryptofuzz::operation::KDF_PBKDF1 const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 99 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 99 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 99 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_PBKDF2>(cryptofuzz::operation::KDF_PBKDF2 const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 443 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 443 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 443 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_ARGON2>(cryptofuzz::operation::KDF_ARGON2 const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 305 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 305 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 305 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_SSH>(cryptofuzz::operation::KDF_SSH const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 97 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 97 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 97 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_X963>(cryptofuzz::operation::KDF_X963 const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 113 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 113 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 113 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_BCRYPT>(cryptofuzz::operation::KDF_BCRYPT const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 64 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 64 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 64 | } |
void cryptofuzz::tests::verifyKeySize<std::__1::optional<cryptofuzz::Buffer>, cryptofuzz::operation::KDF_SP_800_108>(cryptofuzz::operation::KDF_SP_800_108 const&, std::__1::optional<cryptofuzz::Buffer> const&) Line | Count | Source | 12 | 366 | void verifyKeySize(const OperationType& op, const ResultType& result) { | 13 | 366 | if ( result != std::nullopt && op.keySize != result->GetSize() ) { | 14 | | /* TODO include module name in abort message */ | 15 | 0 | util::abort({op.Name(), "invalid keySize"}); | 16 | 0 | } | 17 | 366 | } |
|
18 | | |
19 | 961 | static void checkZeroResult(const std::optional<Buffer>& b) { |
20 | 961 | if ( b == std::nullopt ) { |
21 | 0 | return; |
22 | 0 | } |
23 | | |
24 | 961 | if ( b->GetSize() >= 16 ) { |
25 | 811 | const std::vector<uint8_t> zeroes(b->GetSize(), 0); |
26 | 811 | if ( b->Get() == zeroes ) { |
27 | 0 | printf("An all-zero hash was returned. This might indicate a bug.\n"); |
28 | 0 | abort(); |
29 | 0 | } |
30 | 811 | } |
31 | 961 | } |
32 | | |
33 | 1.23k | void test(const operation::Digest& op, const std::optional<component::Digest>& result) { |
34 | 1.23k | if ( result == std::nullopt ) { |
35 | 574 | return; |
36 | 574 | } |
37 | | |
38 | 664 | { |
39 | 664 | const auto expectedSize = repository::DigestSize(op.digestType.Get()); |
40 | | |
41 | 664 | if ( expectedSize != std::nullopt ) { |
42 | 636 | if ( result->GetSize() != *expectedSize ) { |
43 | 0 | printf("Expected vs actual digest size: %zu / %zu\n", *expectedSize, result->GetSize()); |
44 | 0 | abort(); |
45 | 0 | } |
46 | 636 | } |
47 | 664 | } |
48 | | |
49 | 664 | checkZeroResult(result); |
50 | 664 | } |
51 | | |
52 | 731 | void test(const operation::HMAC& op, const std::optional<component::MAC>& result) { |
53 | 731 | if ( result == std::nullopt ) { |
54 | 434 | return; |
55 | 434 | } |
56 | | |
57 | 297 | { |
58 | 297 | const auto expectedSize = repository::DigestSize(op.digestType.Get()); |
59 | | |
60 | 297 | if ( expectedSize != std::nullopt ) { |
61 | 181 | if ( result->GetSize() != *expectedSize ) { |
62 | 0 | printf("Expected vs actual digest size: %zu / %zu\n", *expectedSize, result->GetSize()); |
63 | 0 | abort(); |
64 | 0 | } |
65 | 181 | } |
66 | 297 | } |
67 | | |
68 | 297 | checkZeroResult(result); |
69 | 297 | } |
70 | | |
71 | 759 | void test(const operation::UMAC& op, const std::optional<component::MAC>& result) { |
72 | 759 | if ( result == std::nullopt ) { |
73 | 391 | return; |
74 | 391 | } |
75 | | |
76 | 368 | if ( |
77 | 368 | ( op.type == 0 && result->GetSize() > (32/8) ) || |
78 | 368 | ( op.type == 1 && result->GetSize() > (64/8) ) || |
79 | 368 | ( op.type == 2 && result->GetSize() > (96/8) ) || |
80 | 368 | ( op.type == 3 && result->GetSize() > (128/8) ) |
81 | 368 | ) { |
82 | 0 | printf("UMAC: Overlong result: %zu\n", result->GetSize()); |
83 | 0 | abort(); |
84 | 0 | } |
85 | 368 | } |
86 | | |
87 | 3.59k | static void test_ChaCha20_Poly1305_IV(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) { |
88 | 3.59k | using fuzzing::datasource::ID; |
89 | | |
90 | | /* |
91 | | * OpenSSL CVE-2019-1543 |
92 | | * https://www.openssl.org/news/secadv/20190306.txt |
93 | | */ |
94 | | |
95 | 3.59k | if ( op.cipher.cipherType.Get() != CF_CIPHER("CHACHA20_POLY1305") ) { |
96 | 3.51k | return; |
97 | 3.51k | } |
98 | | |
99 | 87 | if ( result == std::nullopt ) { |
100 | 71 | return; |
101 | 71 | } |
102 | | |
103 | 16 | if ( op.cipher.iv.GetSize() > 12 ) { |
104 | 0 | abort(); |
105 | 0 | } |
106 | 16 | } |
107 | | |
108 | 3.59k | static void test_XChaCha20_Poly1305_IV(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) { |
109 | 3.59k | using fuzzing::datasource::ID; |
110 | | |
111 | 3.59k | if ( op.cipher.cipherType.Get() != CF_CIPHER("XCHACHA20_POLY1305") ) { |
112 | 3.59k | return; |
113 | 3.59k | } |
114 | | |
115 | 7 | if ( result == std::nullopt ) { |
116 | 7 | return; |
117 | 7 | } |
118 | | |
119 | 0 | if ( op.cipher.iv.GetSize() != 24 ) { |
120 | 0 | printf("XChaCha20-Poly1305 succeeded with an IV of %zu bytes large, but only IVs of 24 bytes are valid\n", op.cipher.iv.GetSize()); |
121 | 0 | abort(); |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | 3.59k | static void test_AES_CCM_Wycheproof(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) { |
126 | 3.59k | bool fail = false; |
127 | | |
128 | 3.59k | if ( result == std::nullopt ) { |
129 | 2.43k | return; |
130 | 2.43k | } |
131 | | |
132 | 1.16k | switch ( op.cipher.cipherType.Get() ) { |
133 | 19 | case CF_CIPHER("AES_128_CCM"): |
134 | 34 | case CF_CIPHER("AES_192_CCM"): |
135 | 56 | case CF_CIPHER("AES_256_CCM"): |
136 | 56 | break; |
137 | 1.10k | default: |
138 | 1.10k | return; |
139 | 1.16k | } |
140 | | |
141 | 56 | if ( op.cipher.iv.GetSize() < 7 || op.cipher.iv.GetSize() > 13 ) { |
142 | 0 | printf("AES CCM: Invalid IV size\n"); |
143 | 0 | fail = true; |
144 | 0 | } |
145 | | |
146 | 56 | if ( result->tag != std::nullopt ) { |
147 | 56 | static const std::vector<size_t> validTagSizes = {4, 6, 8, 10, 12, 14, 16}; |
148 | | |
149 | 56 | if ( std::find(validTagSizes.begin(), validTagSizes.end(), result->tag->GetSize()) == validTagSizes.end() ) { |
150 | 0 | printf("AES CCM: Invalid tag size\n"); |
151 | 0 | fail = true; |
152 | 0 | } |
153 | 56 | } |
154 | | |
155 | 56 | if ( fail == true ) { |
156 | 0 | printf("AES CCM tests based on Wycheproof: https://github.com/google/wycheproof/blob/4672ff74d68766e7785c2cac4c597effccef2c5c/testvectors/aes_ccm_test.json#L11\n"); |
157 | 0 | abort(); |
158 | 0 | } |
159 | 56 | } |
160 | | |
161 | 3.59k | static void test_AES_GCM_Wycheproof(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) { |
162 | 3.59k | bool fail = false; |
163 | | |
164 | 3.59k | if ( result == std::nullopt ) { |
165 | 2.43k | return; |
166 | 2.43k | } |
167 | | |
168 | 1.16k | switch ( op.cipher.cipherType.Get() ) { |
169 | 20 | case CF_CIPHER("AES_128_GCM"): |
170 | 68 | case CF_CIPHER("AES_192_GCM"): |
171 | 117 | case CF_CIPHER("AES_256_GCM"): |
172 | 117 | break; |
173 | 1.04k | default: |
174 | 1.04k | return; |
175 | 1.16k | } |
176 | | |
177 | 117 | if ( op.cipher.iv.GetSize() == 0 ) { |
178 | 0 | printf("AES GCM: Invalid IV size\n"); |
179 | 0 | fail = true; |
180 | 0 | } |
181 | | |
182 | 117 | if ( fail == true ) { |
183 | 0 | printf("AES GCM tests based on Wycheproof: https://github.com/google/wycheproof/blob/4672ff74d68766e7785c2cac4c597effccef2c5c/testvectors/aes_gcm_test.json#L13\n"); |
184 | 0 | abort(); |
185 | 0 | } |
186 | 117 | } |
187 | | |
188 | 3.59k | void test(const operation::SymmetricEncrypt& op, const std::optional<component::Ciphertext>& result) { |
189 | 3.59k | test_ChaCha20_Poly1305_IV(op, result); |
190 | 3.59k | test_XChaCha20_Poly1305_IV(op, result); |
191 | 3.59k | test_AES_CCM_Wycheproof(op, result); |
192 | 3.59k | test_AES_GCM_Wycheproof(op, result); |
193 | 3.59k | } |
194 | | |
195 | 1.91k | void test(const operation::SymmetricDecrypt& op, const std::optional<component::Cleartext>& result) { |
196 | 1.91k | (void)op; |
197 | 1.91k | (void)result; |
198 | 1.91k | } |
199 | | |
200 | 673 | void test(const operation::CMAC& op, const std::optional<component::MAC>& result) { |
201 | 673 | (void)op; |
202 | 673 | (void)result; |
203 | 673 | } |
204 | | |
205 | 153 | void test(const operation::KDF_SCRYPT& op, const std::optional<component::Key>& result) { |
206 | 153 | verifyKeySize(op, result); |
207 | 153 | } |
208 | | |
209 | 930 | static void test_HKDF_OutputSize(const operation::KDF_HKDF& op, const std::optional<component::Key>& result) { |
210 | 930 | if ( result == std::nullopt ) { |
211 | 415 | return; |
212 | 415 | } |
213 | | |
214 | 515 | const auto expectedSize = repository::DigestSize(op.digestType.Get()); |
215 | | |
216 | 515 | if ( expectedSize == std::nullopt ) { |
217 | 60 | return; |
218 | 60 | } |
219 | | |
220 | 455 | const size_t maxOutputSize = 255 * *expectedSize; |
221 | | |
222 | 455 | if ( result->GetSize() > maxOutputSize ) { |
223 | 0 | printf("The output size of HKDF (%zu) is more than 255 * the size of the hash digest (%zu)\n", result->GetSize(), maxOutputSize); |
224 | 0 | abort(); |
225 | 0 | } |
226 | 455 | } |
227 | | |
228 | 930 | void test(const operation::KDF_HKDF& op, const std::optional<component::Key>& result) { |
229 | 930 | verifyKeySize(op, result); |
230 | | |
231 | 930 | test_HKDF_OutputSize(op, result); |
232 | 930 | } |
233 | | |
234 | 134 | void test(const operation::KDF_TLS1_PRF& op, const std::optional<component::Key>& result) { |
235 | 134 | verifyKeySize(op, result); |
236 | 134 | } |
237 | | |
238 | 161 | void test(const operation::KDF_PBKDF& op, const std::optional<component::Key>& result) { |
239 | 161 | verifyKeySize(op, result); |
240 | 161 | } |
241 | | |
242 | 99 | void test(const operation::KDF_PBKDF1& op, const std::optional<component::Key>& result) { |
243 | 99 | verifyKeySize(op, result); |
244 | 99 | } |
245 | | |
246 | 443 | void test(const operation::KDF_PBKDF2& op, const std::optional<component::Key>& result) { |
247 | 443 | verifyKeySize(op, result); |
248 | 443 | } |
249 | | |
250 | 305 | void test(const operation::KDF_ARGON2& op, const std::optional<component::Key>& result) { |
251 | 305 | verifyKeySize(op, result); |
252 | 305 | } |
253 | | |
254 | 97 | void test(const operation::KDF_SSH& op, const std::optional<component::Key>& result) { |
255 | 97 | verifyKeySize(op, result); |
256 | 97 | } |
257 | | |
258 | 113 | void test(const operation::KDF_X963& op, const std::optional<component::Key>& result) { |
259 | 113 | verifyKeySize(op, result); |
260 | 113 | } |
261 | | |
262 | 64 | void test(const operation::KDF_BCRYPT& op, const std::optional<component::Key>& result) { |
263 | 64 | verifyKeySize(op, result); |
264 | 64 | } |
265 | | |
266 | 366 | void test(const operation::KDF_SP_800_108& op, const std::optional<component::Key>& result) { |
267 | 366 | verifyKeySize(op, result); |
268 | 366 | } |
269 | | |
270 | 361 | static bool IsSpecialCurve(const uint64_t curveID) { |
271 | 361 | switch ( curveID ) { |
272 | 0 | case CF_ECC_CURVE("ed448"): |
273 | 110 | case CF_ECC_CURVE("ed25519"): |
274 | 110 | case CF_ECC_CURVE("x25519"): |
275 | 110 | case CF_ECC_CURVE("x448"): |
276 | 110 | return true; |
277 | 251 | default: |
278 | 251 | return false; |
279 | 361 | } |
280 | 361 | } |
281 | | |
282 | 434 | static void test_ECC_PrivateKey(const uint64_t curveID, const std::string priv) { |
283 | | /* Disabled until all modules comply by default */ |
284 | 434 | return; |
285 | | |
286 | | /* Private key may be 0 with these curves */ |
287 | 0 | if ( IsSpecialCurve(curveID) ) { |
288 | 0 | return; |
289 | 0 | } |
290 | | |
291 | 0 | if ( priv == "0" ) { |
292 | 0 | std::cout << "0 is an invalid elliptic curve private key" << std::endl; |
293 | 0 | ::abort(); |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | | |
298 | 556 | void test(const operation::ECC_PrivateToPublic& op, const std::optional<component::ECC_PublicKey>& result) { |
299 | 556 | if ( result != std::nullopt ) { |
300 | 173 | test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString()); |
301 | 173 | } |
302 | 556 | } |
303 | | |
304 | 188 | void test(const operation::ECC_ValidatePubkey& op, const std::optional<bool>& result) { |
305 | 188 | (void)op; |
306 | 188 | (void)result; |
307 | 188 | } |
308 | | |
309 | 304 | void test(const operation::ECC_GenerateKeyPair& op, const std::optional<component::ECC_KeyPair>& result) { |
310 | 304 | if ( result != std::nullopt ) { |
311 | 57 | test_ECC_PrivateKey(op.curveType.Get(), result->priv.ToTrimmedString()); |
312 | 57 | } |
313 | 304 | } |
314 | | |
315 | 328 | static void test_ECDSA_Signature(const uint64_t curveID, const std::string R, const std::string S) { |
316 | 328 | if ( IsSpecialCurve(curveID) ) { |
317 | 77 | return; |
318 | 77 | } |
319 | | |
320 | 251 | const boost::multiprecision::cpp_int r(R), s(S); |
321 | | |
322 | 251 | if ( r < 1 ) { |
323 | 0 | std::cout << "ECDSA signature invalid: R < 1" << std::endl; |
324 | 0 | ::abort(); |
325 | 0 | } |
326 | 251 | if ( s < 1 ) { |
327 | 0 | std::cout << "ECDSA signature invalid: S < 1" << std::endl; |
328 | 0 | ::abort(); |
329 | 0 | } |
330 | | |
331 | 251 | const auto O = cryptofuzz::repository::ECC_CurveToOrder(curveID); |
332 | 251 | if ( O == std::nullopt ) { |
333 | 24 | return; |
334 | 24 | } |
335 | | |
336 | 227 | const boost::multiprecision::cpp_int o(*O); |
337 | | |
338 | 227 | if ( r >= o ) { |
339 | 0 | std::cout << "ECDSA signature invalid: R >= order" << std::endl; |
340 | 0 | ::abort(); |
341 | 0 | } |
342 | | |
343 | 227 | if ( s >= o ) { |
344 | 0 | std::cout << "ECDSA signature invalid: S >= order" << std::endl; |
345 | 0 | ::abort(); |
346 | 0 | } |
347 | 227 | } |
348 | | |
349 | 0 | static void test_BIP340_Schnorr_Signature(const uint64_t curveID, const std::string R, const std::string S) { |
350 | 0 | boost::multiprecision::cpp_int r(R); |
351 | 0 | boost::multiprecision::cpp_int s(S); |
352 | 0 | if ( r < 1 ) { |
353 | 0 | std::cout << "BIP340 Schnorr signature invalid: R < 1" << std::endl; |
354 | 0 | ::abort(); |
355 | 0 | } |
356 | 0 | if ( s < 1 ) { |
357 | 0 | std::cout << "BIP340 Schnorr signature invalid: S < 1" << std::endl; |
358 | 0 | ::abort(); |
359 | 0 | } |
360 | | |
361 | 0 | const auto prime = cryptofuzz::repository::ECC_CurveToPrime(curveID); |
362 | 0 | if ( prime != std::nullopt ) { |
363 | 0 | const boost::multiprecision::cpp_int p(*prime); |
364 | 0 | CF_ASSERT(r < p, "BIP340 Schnorr signature R should be less than curve P"); |
365 | 0 | } |
366 | | |
367 | 0 | const auto order = cryptofuzz::repository::ECC_CurveToOrder(curveID); |
368 | 0 | if ( order != std::nullopt ) { |
369 | 0 | const boost::multiprecision::cpp_int n(*order); |
370 | 0 | CF_ASSERT(s < n, "BIP340 Schnorr signature S should be less than curve N"); |
371 | 0 | } |
372 | 0 | } |
373 | | |
374 | 48 | void test(const operation::ECCSI_Sign& op, const std::optional<component::ECCSI_Signature>& result) { |
375 | 48 | (void)op; |
376 | 48 | (void)result; |
377 | 48 | } |
378 | 455 | void test(const operation::ECDSA_Sign& op, const std::optional<component::ECDSA_Signature>& result) { |
379 | 455 | if ( result != std::nullopt ) { |
380 | 189 | test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString()); |
381 | | |
382 | 189 | if ( |
383 | 189 | op.UseSpecifiedNonce() == true && |
384 | 189 | !IsSpecialCurve(op.curveType.Get()) && |
385 | 189 | op.nonce.ToTrimmedString() == "0" |
386 | 189 | ) { |
387 | 0 | std::cout << "0 is an invalid ECDSA nonce" << std::endl; |
388 | 0 | ::abort(); |
389 | 0 | } |
390 | | |
391 | 189 | test_ECDSA_Signature(op.curveType.Get(), |
392 | 189 | result->signature.first.ToTrimmedString(), |
393 | 189 | result->signature.second.ToTrimmedString()); |
394 | 189 | } |
395 | 455 | } |
396 | | |
397 | 79 | void test(const operation::ECGDSA_Sign& op, const std::optional<component::ECGDSA_Signature>& result) { |
398 | 79 | if ( result != std::nullopt ) { |
399 | 15 | test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString()); |
400 | | |
401 | 15 | if ( |
402 | 15 | op.UseSpecifiedNonce() == true && |
403 | 15 | !IsSpecialCurve(op.curveType.Get()) && |
404 | 15 | op.nonce.ToTrimmedString() == "0" |
405 | 15 | ) { |
406 | 0 | std::cout << "0 is an invalid ECGDSA nonce" << std::endl; |
407 | 0 | ::abort(); |
408 | 0 | } |
409 | | |
410 | 15 | test_ECDSA_Signature(op.curveType.Get(), |
411 | 15 | result->signature.first.ToTrimmedString(), |
412 | 15 | result->signature.second.ToTrimmedString()); |
413 | 15 | } |
414 | 79 | } |
415 | | |
416 | 26 | void test(const operation::ECRDSA_Sign& op, const std::optional<component::ECRDSA_Signature>& result) { |
417 | 26 | if ( result != std::nullopt ) { |
418 | 0 | test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString()); |
419 | |
|
420 | 0 | if ( |
421 | 0 | op.UseSpecifiedNonce() == true && |
422 | 0 | !IsSpecialCurve(op.curveType.Get()) && |
423 | 0 | op.nonce.ToTrimmedString() == "0" |
424 | 0 | ) { |
425 | 0 | std::cout << "0 is an invalid ECRDSA nonce" << std::endl; |
426 | 0 | ::abort(); |
427 | 0 | } |
428 | | |
429 | 0 | test_ECDSA_Signature(op.curveType.Get(), |
430 | 0 | result->signature.first.ToTrimmedString(), |
431 | 0 | result->signature.second.ToTrimmedString()); |
432 | 0 | } |
433 | 26 | } |
434 | | |
435 | 35 | void test(const operation::Schnorr_Sign& op, const std::optional<component::Schnorr_Signature>& result) { |
436 | 35 | if ( result != std::nullopt ) { |
437 | 0 | test_ECC_PrivateKey(op.curveType.Get(), op.priv.ToTrimmedString()); |
438 | |
|
439 | 0 | if ( |
440 | 0 | op.UseSpecifiedNonce() == true && |
441 | 0 | !IsSpecialCurve(op.curveType.Get()) && |
442 | 0 | op.nonce.ToTrimmedString() == "0" |
443 | 0 | ) { |
444 | 0 | std::cout << "0 is an invalid Schnorr nonce" << std::endl; |
445 | 0 | ::abort(); |
446 | 0 | } |
447 | | |
448 | 0 | test_BIP340_Schnorr_Signature(op.curveType.Get(), |
449 | 0 | result->signature.first.ToTrimmedString(), |
450 | 0 | result->signature.second.ToTrimmedString()); |
451 | 0 | } |
452 | 35 | } |
453 | | |
454 | 11 | void test(const operation::ECCSI_Verify& op, const std::optional<bool>& result) { |
455 | 11 | (void)op; |
456 | 11 | (void)result; |
457 | 11 | } |
458 | | |
459 | 210 | void test(const operation::ECDSA_Verify& op, const std::optional<bool>& result) { |
460 | 210 | if ( result != std::nullopt && *result == true ) { |
461 | 5 | test_ECDSA_Signature(op.curveType.Get(), |
462 | 5 | op.signature.signature.first.ToTrimmedString(), |
463 | 5 | op.signature.signature.second.ToTrimmedString()); |
464 | 5 | } |
465 | 210 | } |
466 | | |
467 | 64 | void test(const operation::ECGDSA_Verify& op, const std::optional<bool>& result) { |
468 | 64 | if ( result != std::nullopt && *result == true ) { |
469 | 1 | test_ECDSA_Signature(op.curveType.Get(), |
470 | 1 | op.signature.signature.first.ToTrimmedString(), |
471 | 1 | op.signature.signature.second.ToTrimmedString()); |
472 | 1 | } |
473 | 64 | } |
474 | | |
475 | 12 | void test(const operation::ECRDSA_Verify& op, const std::optional<bool>& result) { |
476 | 12 | if ( result != std::nullopt && *result == true ) { |
477 | 0 | test_ECDSA_Signature(op.curveType.Get(), |
478 | 0 | op.signature.signature.first.ToTrimmedString(), |
479 | 0 | op.signature.signature.second.ToTrimmedString()); |
480 | 0 | } |
481 | 12 | } |
482 | | |
483 | 22 | void test(const operation::Schnorr_Verify& op, const std::optional<bool>& result) { |
484 | 22 | if ( result != std::nullopt && *result == true ) { |
485 | 0 | test_BIP340_Schnorr_Signature(op.curveType.Get(), |
486 | 0 | op.signature.signature.first.ToTrimmedString(), |
487 | 0 | op.signature.signature.second.ToTrimmedString()); |
488 | 0 | } |
489 | 22 | } |
490 | | |
491 | 293 | void test(const operation::ECDSA_Recover& op, const std::optional<component::ECC_PublicKey>& result) { |
492 | 293 | if ( result != std::nullopt ) { |
493 | 118 | if ( op.id > 3 ) { |
494 | 0 | std::cout << "Invalid recovery ID" << std::endl; |
495 | 0 | ::abort(); |
496 | 0 | } |
497 | 118 | } |
498 | 293 | if ( result != std::nullopt ) { |
499 | 118 | test_ECDSA_Signature(op.curveType.Get(), |
500 | 118 | op.signature.first.ToTrimmedString(), |
501 | 118 | op.signature.second.ToTrimmedString()); |
502 | 118 | } |
503 | 293 | } |
504 | | |
505 | 118 | void test(const operation::DSA_Verify& op, const std::optional<bool>& result) { |
506 | 118 | (void)op; |
507 | | |
508 | 118 | if ( result == std::nullopt || *result == false ) { |
509 | 112 | return; |
510 | 112 | } |
511 | | |
512 | 6 | if ( !op.signature.first.IsPositive() ) { |
513 | 0 | std::cout << "DSA signature must be rejected if R is smaller than 1" << std::endl; |
514 | 0 | ::abort(); |
515 | 0 | } |
516 | 6 | if ( !op.signature.second.IsPositive() ) { |
517 | 0 | std::cout << "DSA signature must be rejected is S is smaller than 1" << std::endl; |
518 | 0 | ::abort(); |
519 | 0 | } |
520 | | |
521 | | /* Q > R */ |
522 | 6 | if ( op.signature.first.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) { |
523 | 0 | std::cout << "DSA signature must be rejected if R is larger than Q" << std::endl; |
524 | 0 | ::abort(); |
525 | 0 | } |
526 | | /* Q > S */ |
527 | 6 | if ( op.signature.second.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) { |
528 | 0 | std::cout << "DSA signature must be rejected if S is larger than Q" << std::endl; |
529 | 0 | ::abort(); |
530 | 0 | } |
531 | 6 | } |
532 | | |
533 | 38 | void test(const operation::DSA_Sign& op, const std::optional<component::DSA_Signature>& result) { |
534 | 38 | if ( result == std::nullopt ) { |
535 | 38 | return; |
536 | 38 | } |
537 | | |
538 | 0 | if ( !result->signature.first.IsPositive() ) { |
539 | 0 | std::cout << "DSA signature R must be larger than 0" << std::endl; |
540 | 0 | ::abort(); |
541 | 0 | } |
542 | 0 | if ( !result->signature.second.IsPositive() ) { |
543 | 0 | std::cout << "DSA signature S must be larger than 0" << std::endl; |
544 | 0 | ::abort(); |
545 | 0 | } |
546 | | |
547 | | /* Q > R */ |
548 | 0 | if ( result->signature.first.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) { |
549 | 0 | std::cout << "DSA signature R must be smaller than P" << std::endl; |
550 | 0 | ::abort(); |
551 | 0 | } |
552 | | /* Q > S */ |
553 | 0 | if ( result->signature.second.ToTrimmedString().size() > op.parameters.q.ToTrimmedString().size() ) { |
554 | 0 | std::cout << "DSA signature S must be smaller than Q" << std::endl; |
555 | 0 | ::abort(); |
556 | 0 | } |
557 | | |
558 | | /* R > 0 */ |
559 | 0 | if ( !result->signature.first.IsPositive() ) { |
560 | 0 | std::cout << "DSA signature R must be larger than 0" << std::endl; |
561 | 0 | ::abort(); |
562 | 0 | } |
563 | | /* S > 0 */ |
564 | 0 | if ( !result->signature.second.IsPositive() ) { |
565 | 0 | std::cout << "DSA signature R must be larger than 0" << std::endl; |
566 | 0 | ::abort(); |
567 | 0 | } |
568 | 0 | } |
569 | | |
570 | 0 | static bool isComposite(const std::string &num) { |
571 | 0 | if ( num.size() == 0 ) { |
572 | 0 | return true; |
573 | 0 | } |
574 | | |
575 | 0 | size_t sum = 0; |
576 | 0 | for (char c : num) { |
577 | 0 | sum += c - '0'; |
578 | 0 | } |
579 | 0 | if (sum % 3 == 0) { |
580 | 0 | return true; |
581 | 0 | } |
582 | | |
583 | 0 | return false; |
584 | 0 | } |
585 | | |
586 | | |
587 | 25 | void test(const operation::DSA_GenerateParameters& op, const std::optional<component::DSA_Parameters>& result) { |
588 | 25 | (void)op; |
589 | | |
590 | 25 | if ( result == std::nullopt ) { |
591 | 25 | return; |
592 | 25 | } |
593 | | |
594 | | /* Larger than 0 */ |
595 | 0 | if ( !result->p.IsPositive() ) { |
596 | 0 | std::cout << "DSA P parameter must be larger than 0" << std::endl; |
597 | 0 | ::abort(); |
598 | 0 | } |
599 | 0 | if ( !result->q.IsPositive() ) { |
600 | 0 | std::cout << "DSA Q parameter must be larger than 0" << std::endl; |
601 | 0 | ::abort(); |
602 | 0 | } |
603 | 0 | if ( !result->g.IsPositive() ) { |
604 | 0 | std::cout << "DSA G parameter must be larger than 0" << std::endl; |
605 | 0 | ::abort(); |
606 | 0 | } |
607 | | |
608 | | /* P > Q */ |
609 | 0 | if ( result->q.ToTrimmedString().size() > result->p.ToTrimmedString().size() ) { |
610 | 0 | std::cout << "DSA Q must be smaller than P" << std::endl; |
611 | 0 | ::abort(); |
612 | 0 | } |
613 | | |
614 | | /* P > G */ |
615 | 0 | if ( result->q.ToTrimmedString().size() > result->p.ToTrimmedString().size() ) { |
616 | 0 | std::cout << "DSA G must be smaller than P" << std::endl; |
617 | 0 | ::abort(); |
618 | 0 | } |
619 | | |
620 | | /* G != 1 */ |
621 | 0 | if ( result->p.ToTrimmedString() == "1" ) { |
622 | 0 | std::cout << "DSA G must not be 1" << std::endl; |
623 | 0 | ::abort(); |
624 | 0 | } |
625 | | |
626 | | /* P, Q must be prime */ |
627 | 0 | if ( isComposite(result->p.ToTrimmedString()) ) { |
628 | 0 | std::cout << "DSA P must be prime" << std::endl; |
629 | 0 | ::abort(); |
630 | 0 | } |
631 | | |
632 | 0 | if ( isComposite(result->q.ToTrimmedString()) ) { |
633 | 0 | std::cout << "DSA Q must be prime" << std::endl; |
634 | 0 | ::abort(); |
635 | 0 | } |
636 | 0 | } |
637 | | |
638 | 21 | void test(const operation::DSA_PrivateToPublic& op, const std::optional<component::Bignum>& result) { |
639 | 21 | (void)op; |
640 | 21 | (void)result; |
641 | 21 | } |
642 | | |
643 | 26 | void test(const operation::DSA_GenerateKeyPair& op, const std::optional<component::DSA_KeyPair>& result) { |
644 | 26 | if ( result == std::nullopt ) { |
645 | 26 | return; |
646 | 26 | } |
647 | | |
648 | 0 | if ( !result->first.IsPositive() ) { |
649 | 0 | std::cout << "Private key must be larger than 0" << std::endl; |
650 | 0 | ::abort(); |
651 | 0 | } |
652 | | |
653 | | /* Q > priv */ |
654 | 0 | if ( result->first.ToTrimmedString().size() > op.q.ToTrimmedString().size() ) { |
655 | 0 | std::cout << "Q must be larger than private key" << std::endl; |
656 | 0 | ::abort(); |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | 27 | void test(const operation::ECDH_Derive& op, const std::optional<component::Secret>& result) { |
661 | 27 | (void)op; |
662 | 27 | (void)result; |
663 | 27 | } |
664 | | |
665 | 36 | void test(const operation::ECIES_Encrypt& op, const std::optional<component::Ciphertext>& result) { |
666 | | /* TODO check minimum size? */ |
667 | 36 | (void)op; |
668 | 36 | (void)result; |
669 | 36 | } |
670 | | |
671 | 42 | void test(const operation::ECIES_Decrypt& op, const std::optional<component::Cleartext>& result) { |
672 | 42 | (void)op; |
673 | 42 | (void)result; |
674 | 42 | } |
675 | | |
676 | 53 | void test(const operation::ECC_Point_Add& op, const std::optional<component::ECC_Point>& result) { |
677 | 53 | (void)op; |
678 | 53 | (void)result; |
679 | 53 | } |
680 | | |
681 | 52 | void test(const operation::ECC_Point_Sub& op, const std::optional<component::ECC_Point>& result) { |
682 | 52 | if ( result == std::nullopt ) { |
683 | 48 | return; |
684 | 48 | } |
685 | | |
686 | 4 | if ( !(op.a.first == op.b.first) ) { |
687 | 2 | return; |
688 | 2 | } |
689 | | |
690 | 2 | if ( !(op.a.second == op.b.second) ) { |
691 | 2 | return; |
692 | 2 | } |
693 | | |
694 | 0 | if ( !result->first.IsZero() || !result->second.IsZero() ) { |
695 | 0 | std::cout << "Subtracting equal points should result in point at infinity" << std::endl; |
696 | 0 | ::abort(); |
697 | 0 | } |
698 | 0 | } |
699 | | |
700 | 387 | void test(const operation::ECC_Point_Mul& op, const std::optional<component::ECC_Point>& result) { |
701 | 387 | (void)op; |
702 | 387 | (void)result; |
703 | 387 | } |
704 | | |
705 | 71 | void test(const operation::ECC_Point_Neg& op, const std::optional<component::ECC_Point>& result) { |
706 | 71 | (void)op; |
707 | 71 | (void)result; |
708 | 71 | } |
709 | | |
710 | 51 | void test(const operation::ECC_Point_Dbl& op, const std::optional<component::ECC_Point>& result) { |
711 | 51 | (void)op; |
712 | 51 | (void)result; |
713 | 51 | } |
714 | | |
715 | 68 | void test(const operation::ECC_Point_Cmp& op, const std::optional<bool>& result) { |
716 | 68 | (void)op; |
717 | 68 | (void)result; |
718 | 68 | } |
719 | | |
720 | 45 | void test(const operation::DH_GenerateKeyPair& op, const std::optional<component::DH_KeyPair>& result) { |
721 | 45 | (void)op; |
722 | 45 | (void)result; |
723 | 45 | } |
724 | | |
725 | 156 | void test(const operation::DH_Derive& op, const std::optional<component::Bignum>& result) { |
726 | 156 | (void)op; |
727 | 156 | (void)result; |
728 | 156 | } |
729 | | |
730 | 25 | void test(const operation::BLS_PrivateToPublic& op, const std::optional<component::BLS_PublicKey>& result) { |
731 | 25 | (void)op; |
732 | 25 | (void)result; |
733 | 25 | } |
734 | | |
735 | 55 | void test(const operation::BLS_PrivateToPublic_G2& op, const std::optional<component::G2>& result) { |
736 | 55 | (void)op; |
737 | 55 | (void)result; |
738 | 55 | } |
739 | | |
740 | 38 | void test(const operation::BLS_Sign& op, const std::optional<component::BLS_Signature>& result) { |
741 | 38 | (void)op; |
742 | 38 | (void)result; |
743 | 38 | } |
744 | | |
745 | 40 | void test(const operation::BLS_Verify& op, const std::optional<bool>& result) { |
746 | 40 | (void)op; |
747 | 40 | (void)result; |
748 | 40 | } |
749 | | |
750 | 52 | void test(const operation::BLS_BatchSign& op, const std::optional<component::BLS_BatchSignature>& result) { |
751 | 52 | (void)op; |
752 | 52 | (void)result; |
753 | 52 | } |
754 | | |
755 | 47 | void test(const operation::BLS_BatchVerify& op, const std::optional<bool>& result) { |
756 | 47 | (void)op; |
757 | 47 | (void)result; |
758 | 47 | } |
759 | | |
760 | 32 | void test(const operation::BLS_Aggregate_G1& op, const std::optional<component::G1>& result) { |
761 | 32 | (void)op; |
762 | 32 | (void)result; |
763 | 32 | } |
764 | | |
765 | 29 | void test(const operation::BLS_Aggregate_G2& op, const std::optional<component::G2>& result) { |
766 | 29 | (void)op; |
767 | 29 | (void)result; |
768 | 29 | } |
769 | | |
770 | 38 | void test(const operation::BLS_Pairing& op, const std::optional<component::Fp12>& result) { |
771 | 38 | (void)op; |
772 | 38 | (void)result; |
773 | 38 | } |
774 | | |
775 | 26 | void test(const operation::BLS_MillerLoop& op, const std::optional<component::Fp12>& result) { |
776 | 26 | (void)op; |
777 | 26 | (void)result; |
778 | 26 | } |
779 | | |
780 | 39 | void test(const operation::BLS_FinalExp& op, const std::optional<component::Fp12>& result) { |
781 | 39 | (void)op; |
782 | 39 | (void)result; |
783 | 39 | } |
784 | | |
785 | 22 | void test(const operation::BLS_HashToG1& op, const std::optional<component::G1>& result) { |
786 | 22 | (void)op; |
787 | 22 | (void)result; |
788 | 22 | } |
789 | | |
790 | 23 | void test(const operation::BLS_HashToG2& op, const std::optional<component::G2>& result) { |
791 | 23 | (void)op; |
792 | 23 | (void)result; |
793 | 23 | } |
794 | | |
795 | 24 | void test(const operation::BLS_MapToG1& op, const std::optional<component::G1>& result) { |
796 | 24 | (void)op; |
797 | 24 | (void)result; |
798 | 24 | } |
799 | | |
800 | 37 | void test(const operation::BLS_MapToG2& op, const std::optional<component::G2>& result) { |
801 | 37 | (void)op; |
802 | 37 | (void)result; |
803 | 37 | } |
804 | | |
805 | 38 | void test(const operation::BLS_IsG1OnCurve& op, const std::optional<bool>& result) { |
806 | 38 | (void)op; |
807 | 38 | (void)result; |
808 | 38 | } |
809 | | |
810 | 40 | void test(const operation::BLS_IsG2OnCurve& op, const std::optional<bool>& result) { |
811 | 40 | (void)op; |
812 | 40 | (void)result; |
813 | 40 | } |
814 | | |
815 | 25 | void test(const operation::BLS_GenerateKeyPair& op, const std::optional<component::BLS_KeyPair>& result) { |
816 | 25 | (void)op; |
817 | 25 | (void)result; |
818 | 25 | } |
819 | | |
820 | 24 | void test(const operation::BLS_Decompress_G1& op, const std::optional<component::G1>& result) { |
821 | 24 | (void)op; |
822 | 24 | (void)result; |
823 | 24 | } |
824 | | |
825 | 25 | void test(const operation::BLS_Compress_G1& op, const std::optional<component::Bignum>& result) { |
826 | 25 | (void)op; |
827 | 25 | (void)result; |
828 | 25 | } |
829 | | |
830 | 38 | void test(const operation::BLS_Decompress_G2& op, const std::optional<component::G2>& result) { |
831 | 38 | (void)op; |
832 | 38 | (void)result; |
833 | 38 | } |
834 | | |
835 | 39 | void test(const operation::BLS_Compress_G2& op, const std::optional<component::G1>& result) { |
836 | 39 | (void)op; |
837 | 39 | (void)result; |
838 | 39 | } |
839 | | |
840 | 39 | void test(const operation::BLS_G1_Add& op, const std::optional<component::G1>& result) { |
841 | 39 | (void)op; |
842 | 39 | (void)result; |
843 | 39 | } |
844 | | |
845 | 39 | void test(const operation::BLS_G1_Mul& op, const std::optional<component::G1>& result) { |
846 | 39 | (void)op; |
847 | 39 | (void)result; |
848 | 39 | } |
849 | | |
850 | 49 | void test(const operation::BLS_G1_IsEq& op, const std::optional<bool>& result) { |
851 | 49 | (void)op; |
852 | 49 | (void)result; |
853 | 49 | } |
854 | | |
855 | 28 | void test(const operation::BLS_G1_Neg& op, const std::optional<component::G1>& result) { |
856 | 28 | (void)op; |
857 | 28 | (void)result; |
858 | 28 | } |
859 | | |
860 | 44 | void test(const operation::BLS_G2_Add& op, const std::optional<component::G2>& result) { |
861 | 44 | (void)op; |
862 | 44 | (void)result; |
863 | 44 | } |
864 | | |
865 | 37 | void test(const operation::BLS_G2_Mul& op, const std::optional<component::G2>& result) { |
866 | 37 | (void)op; |
867 | 37 | (void)result; |
868 | 37 | } |
869 | | |
870 | 71 | void test(const operation::BLS_G2_IsEq& op, const std::optional<bool>& result) { |
871 | 71 | (void)op; |
872 | 71 | (void)result; |
873 | 71 | } |
874 | | |
875 | 39 | void test(const operation::BLS_G2_Neg& op, const std::optional<component::G2>& result) { |
876 | 39 | (void)op; |
877 | 39 | (void)result; |
878 | 39 | } |
879 | | |
880 | 49 | void test(const operation::BLS_G1_MultiExp& op, const std::optional<component::G1>& result) { |
881 | 49 | (void)op; |
882 | 49 | (void)result; |
883 | 49 | } |
884 | | |
885 | 8 | void test(const operation::Misc& op, const std::optional<Buffer>& result) { |
886 | 8 | (void)op; |
887 | 8 | (void)result; |
888 | 8 | } |
889 | | |
890 | 35 | void test(const operation::SR25519_Verify& op, const std::optional<bool>& result) { |
891 | 35 | (void)op; |
892 | 35 | (void)result; |
893 | 35 | } |
894 | | |
895 | | namespace BignumCalc { |
896 | 0 | static void Abort(const std::string& message, const std::string& opStr) { |
897 | 0 | std::cout << "BignumCalc ( " << opStr << " ): " << message << std::endl; |
898 | 0 | ::abort(); |
899 | 0 | } |
900 | 78 | static void AssertBinary(const component::Bignum& result, const std::string& opStr) { |
901 | 78 | const auto resultStr = result.ToTrimmedString(); |
902 | 78 | if ( !(resultStr == "0" || resultStr == "1") ) { |
903 | 0 | Abort("Result must be 0 or 1", opStr); |
904 | 0 | } |
905 | 78 | } |
906 | 19 | static void AssertTertiary(const component::Bignum& result, const std::string& opStr) { |
907 | 19 | const auto resultStr = result.ToTrimmedString(); |
908 | 19 | if ( !(resultStr == "0" || resultStr == "1" || resultStr == "-1") ) { |
909 | 0 | Abort("Result must be 0 or 1 or -1", opStr); |
910 | 0 | } |
911 | 19 | } |
912 | 0 | static bool IsEqual(const component::Bignum& A, const component::Bignum& B) { |
913 | 0 | return A.ToTrimmedString() == B.ToTrimmedString(); |
914 | 0 | } |
915 | 119 | static bool IsZero(const component::Bignum& A) { |
916 | 119 | return A.ToTrimmedString() == "0"; |
917 | 119 | } |
918 | 107 | static bool SmallerThan(const component::Bignum& A, const component::Bignum& B) { |
919 | 107 | return A.ToTrimmedString().size() < B.ToTrimmedString().size(); |
920 | 107 | } |
921 | 117 | static bool LargerThan(const component::Bignum& A, const component::Bignum& B) { |
922 | 117 | return A.ToTrimmedString().size() > B.ToTrimmedString().size(); |
923 | 117 | } |
924 | 145 | static bool IsEqualOrLargerThan(const component::Bignum& A, const component::Bignum& B) { |
925 | 145 | const auto a = A.ToTrimmedString(); |
926 | 145 | const auto b = B.ToTrimmedString(); |
927 | 145 | if ( a.size() > b.size() ) { |
928 | 0 | return true; |
929 | 0 | } |
930 | 145 | if ( a.size() == b.size() ) { |
931 | 120 | if ( a == b ) { |
932 | 0 | return true; |
933 | 0 | } |
934 | 120 | } |
935 | 145 | return false; |
936 | 145 | } |
937 | 145 | static void AssertModResult(const component::Bignum& result, const component::Bignum& mod, const std::string& opStr) { |
938 | 145 | if ( IsEqualOrLargerThan(result, mod) ) { |
939 | 0 | Abort("Result is equal to or larger than modulo", opStr); |
940 | 0 | } |
941 | 145 | } |
942 | 36 | static void AssertNotSmallerThan(const component::Bignum& result, const component::Bignum& A, const std::string& opStr) { |
943 | 36 | if ( SmallerThan(result, A) ) { |
944 | 0 | Abort("Result is smaller than the input", opStr); |
945 | 0 | } |
946 | 36 | } |
947 | | static void AssertNotSmallerThan( |
948 | | const component::Bignum& result, |
949 | | const component::Bignum& A, |
950 | | const component::Bignum& B, |
951 | 65 | const std::string& opStr) { |
952 | 65 | if ( SmallerThan(result, A) && SmallerThan(result, B) ) { |
953 | 0 | Abort("Result is smaller than the input", opStr); |
954 | 0 | } |
955 | 65 | } |
956 | 47 | static void AssertNotLargerThan(const component::Bignum& result, const component::Bignum& A, const std::string& opStr) { |
957 | 47 | if ( LargerThan(result, A) ) { |
958 | 0 | Abort("Result is larger than the input", opStr); |
959 | 0 | } |
960 | 47 | } |
961 | | static void AssertNotLargerThan( |
962 | | const component::Bignum& result, |
963 | | const component::Bignum& A, |
964 | | const component::Bignum& B, |
965 | 35 | const std::string& opStr) { |
966 | 35 | if ( LargerThan(result, A) && LargerThan(result, B) ) { |
967 | 0 | Abort("Result is larger than the input", opStr); |
968 | 0 | } |
969 | 35 | } |
970 | | static void AssertPositive( |
971 | | const component::Bignum& result, |
972 | 271 | const std::string& opStr) { |
973 | 271 | if ( !result.IsPositive() ) { |
974 | 0 | Abort("Result is not positive", opStr); |
975 | 0 | } |
976 | 271 | } |
977 | | static void AssertOdd( |
978 | | const component::Bignum& result, |
979 | 288 | const std::string& opStr) { |
980 | 288 | if ( !result.IsOdd() ) { |
981 | 0 | Abort("Result is not odd", opStr); |
982 | 0 | } |
983 | 288 | } |
984 | | static void AssertZero( |
985 | | const component::Bignum& result, |
986 | 2 | const std::string& opStr) { |
987 | 2 | if ( !result.IsZero() ) { |
988 | 0 | Abort("Result is not zero", opStr); |
989 | 0 | } |
990 | 2 | } |
991 | | } |
992 | | |
993 | 4.29k | void test(const operation::BignumCalc& op, const std::optional<component::Bignum>& result) { |
994 | 4.29k | if ( result == std::nullopt ) { |
995 | 3.22k | return; |
996 | 3.22k | } |
997 | | |
998 | 1.07k | using namespace BignumCalc; |
999 | | |
1000 | 1.07k | const auto calcOp = op.calcOp.Get(); |
1001 | | |
1002 | 1.07k | if ( |
1003 | 1.07k | calcOp != CF_CALCOP("IsPrime(A)") && |
1004 | 1.07k | calcOp != CF_CALCOP("Prime()") ) { |
1005 | | /* Negative numbers are not supported yet */ |
1006 | 757 | if ( op.bn0.IsNegative() || |
1007 | 757 | op.bn1.IsNegative() || |
1008 | 757 | op.bn2.IsNegative() ) { |
1009 | 0 | return; |
1010 | 0 | } |
1011 | 757 | } |
1012 | | |
1013 | | /* Modular calculations are not supported yet */ |
1014 | 1.07k | if ( op.modulo != std::nullopt ) { |
1015 | 206 | return; |
1016 | 206 | } |
1017 | | |
1018 | 866 | switch ( calcOp ) { |
1019 | 2 | case CF_CALCOP("Add(A,B)"): |
1020 | 2 | if ( SmallerThan(*result, op.bn0) || |
1021 | 2 | SmallerThan(*result, op.bn1) ) { |
1022 | 0 | Abort("Result is smaller than its operands", repository::CalcOpToString(calcOp)); |
1023 | 0 | } |
1024 | 2 | break; |
1025 | 27 | case CF_CALCOP("Div(A,B)"): |
1026 | 27 | if ( IsZero(op.bn1) ) { |
1027 | 0 | Abort("Division by zero should not produce a result", repository::CalcOpToString(calcOp)); |
1028 | 0 | } |
1029 | | |
1030 | 27 | if ( LargerThan(*result, op.bn0) ) { |
1031 | 0 | Abort("Result is larger than the dividend", repository::CalcOpToString(calcOp)); |
1032 | 0 | } |
1033 | 27 | break; |
1034 | 12 | case CF_CALCOP("Mul(A,B)"): |
1035 | 12 | if ( IsZero(op.bn0) || IsZero(op.bn1) ) { |
1036 | 7 | if ( !IsZero(*result) ) { |
1037 | 0 | Abort("Result of Mul with zero operand is not zero", repository::CalcOpToString(calcOp)); |
1038 | 0 | } |
1039 | 7 | } |
1040 | 12 | break; |
1041 | 14 | case CF_CALCOP("Mod(A,B)"): |
1042 | 14 | BignumCalc::AssertModResult(*result, op.bn1, "Mod"); |
1043 | 14 | break; |
1044 | 106 | case CF_CALCOP("ExpMod(A,B,C)"): |
1045 | 106 | BignumCalc::AssertModResult(*result, op.bn2, "ExpMod"); |
1046 | 106 | break; |
1047 | 5 | case CF_CALCOP("AddMod(A,B,C)"): |
1048 | 5 | BignumCalc::AssertModResult(*result, op.bn2, "AddMod"); |
1049 | 5 | break; |
1050 | 5 | case CF_CALCOP("SubMod(A,B,C)"): |
1051 | 5 | BignumCalc::AssertModResult(*result, op.bn2, "SubMod"); |
1052 | 5 | break; |
1053 | 10 | case CF_CALCOP("MulMod(A,B,C)"): |
1054 | 10 | BignumCalc::AssertModResult(*result, op.bn2, "MulMod"); |
1055 | 10 | break; |
1056 | 5 | case CF_CALCOP("SqrMod(A,B)"): |
1057 | 5 | BignumCalc::AssertModResult(*result, op.bn1, "SqrMod"); |
1058 | 5 | break; |
1059 | 0 | case CF_CALCOP("SqrtMod(A,B)"): |
1060 | 0 | BignumCalc::AssertModResult(*result, op.bn1, "SqrtMod"); |
1061 | 0 | break; |
1062 | 0 | case CF_CALCOP("ModLShift(A,B,C)"): |
1063 | 0 | BignumCalc::AssertModResult(*result, op.bn2, "ModLShift"); |
1064 | 0 | break; |
1065 | 2 | case CF_CALCOP("Bit(A,B)"): |
1066 | 2 | BignumCalc::AssertBinary(*result, "Bit"); |
1067 | 2 | break; |
1068 | 0 | case CF_CALCOP("IsCoprime(A,B)"): |
1069 | 0 | BignumCalc::AssertBinary(*result, "IsCoprime"); |
1070 | 0 | break; |
1071 | 1 | case CF_CALCOP("IsEq(A,B)"): |
1072 | 1 | BignumCalc::AssertBinary(*result, "IsEq"); |
1073 | 1 | break; |
1074 | 5 | case CF_CALCOP("IsGt(A,B)"): |
1075 | 5 | BignumCalc::AssertBinary(*result, "IsGt"); |
1076 | 5 | break; |
1077 | 6 | case CF_CALCOP("IsGte(A,B)"): |
1078 | 6 | BignumCalc::AssertBinary(*result, "IsGte"); |
1079 | 6 | break; |
1080 | 4 | case CF_CALCOP("IsLt(A,B)"): |
1081 | 4 | BignumCalc::AssertBinary(*result, "IsLt"); |
1082 | 4 | break; |
1083 | 2 | case CF_CALCOP("IsLte(A,B)"): |
1084 | 2 | BignumCalc::AssertBinary(*result, "IsLte"); |
1085 | 2 | break; |
1086 | 1 | case CF_CALCOP("IsEven(A)"): |
1087 | 1 | BignumCalc::AssertBinary(*result, "IsEven"); |
1088 | 1 | break; |
1089 | 7 | case CF_CALCOP("IsOdd(A)"): |
1090 | 7 | BignumCalc::AssertBinary(*result, "IsOdd"); |
1091 | 7 | break; |
1092 | 3 | case CF_CALCOP("IsOne(A)"): |
1093 | 3 | BignumCalc::AssertBinary(*result, "IsOne"); |
1094 | 3 | break; |
1095 | 0 | case CF_CALCOP("IsPow2(A)"): |
1096 | 0 | BignumCalc::AssertBinary(*result, "IsPow2"); |
1097 | 0 | break; |
1098 | 44 | case CF_CALCOP("IsPrime(A)"): |
1099 | 44 | BignumCalc::AssertBinary(*result, "IsPrime"); |
1100 | 44 | if ( !op.bn0.IsPositive() ) { |
1101 | 2 | BignumCalc::AssertZero(*result, "IsPrime"); |
1102 | 2 | } |
1103 | 44 | if ( result->IsOne() ) { |
1104 | 17 | if ( op.bn0.ToTrimmedString() != "2" ) { |
1105 | 17 | BignumCalc::AssertOdd(op.bn0, "IsPrime"); |
1106 | 17 | } |
1107 | 17 | } |
1108 | 44 | break; |
1109 | 2 | case CF_CALCOP("IsZero(A)"): |
1110 | 2 | BignumCalc::AssertBinary(*result, "IsZero"); |
1111 | 2 | break; |
1112 | 0 | case CF_CALCOP("IsSquare(A)"): |
1113 | 0 | BignumCalc::AssertBinary(*result, "IsSquare"); |
1114 | 0 | break; |
1115 | 0 | case CF_CALCOP("IsPower(A)"): |
1116 | 0 | BignumCalc::AssertBinary(*result, "IsPower"); |
1117 | 0 | break; |
1118 | 1 | case CF_CALCOP("IsNeg(A)"): |
1119 | 1 | BignumCalc::AssertBinary(*result, "IsNeg"); |
1120 | 1 | break; |
1121 | 0 | case CF_CALCOP("IsNotZero(A)"): |
1122 | 0 | BignumCalc::AssertBinary(*result, "IsNotZero"); |
1123 | 0 | break; |
1124 | 7 | case CF_CALCOP("Cmp(A,B)"): |
1125 | 7 | BignumCalc::AssertTertiary(*result, "Cmp"); |
1126 | 7 | break; |
1127 | 2 | case CF_CALCOP("CmpAbs(A,B)"): |
1128 | 2 | BignumCalc::AssertTertiary(*result, "CmpAbs"); |
1129 | 2 | break; |
1130 | 10 | case CF_CALCOP("Jacobi(A,B)"): |
1131 | 10 | BignumCalc::AssertTertiary(*result, "Jacobi"); |
1132 | 10 | break; |
1133 | 4 | case CF_CALCOP("Sqr(A)"): |
1134 | 4 | AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1135 | 4 | break; |
1136 | 11 | case CF_CALCOP("RShift(A,B)"): |
1137 | 11 | if ( IsZero(op.bn0) || IsZero(op.bn1) ) { |
1138 | 5 | if ( op.bn0.ToTrimmedString() != result->ToTrimmedString() ) { |
1139 | 0 | Abort("Zero operand should not alter input", repository::CalcOpToString(calcOp)); |
1140 | 0 | } |
1141 | 5 | } |
1142 | | |
1143 | 11 | AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1144 | 11 | break; |
1145 | 7 | case CF_CALCOP("LShift1(A)"): |
1146 | 7 | if ( IsZero(op.bn0) ) { |
1147 | 4 | if ( op.bn0.ToTrimmedString() != result->ToTrimmedString() ) { |
1148 | 0 | Abort("Zero input should remain zero", repository::CalcOpToString(calcOp)); |
1149 | 0 | } |
1150 | 4 | } |
1151 | | |
1152 | 7 | AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1153 | 7 | break; |
1154 | 12 | case CF_CALCOP("SetBit(A,B)"): |
1155 | 12 | AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1156 | 12 | break; |
1157 | 0 | case CF_CALCOP("ClearBit(A,B)"): |
1158 | 0 | AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1159 | 0 | break; |
1160 | 5 | case CF_CALCOP("Sqrt(A)"): |
1161 | 5 | AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1162 | 5 | break; |
1163 | 0 | case CF_CALCOP("Cbrt(A)"): |
1164 | 0 | AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1165 | 0 | break; |
1166 | 13 | case CF_CALCOP("MulAdd(A,B,C)"): |
1167 | 13 | AssertNotSmallerThan(*result, op.bn2, repository::CalcOpToString(calcOp)); |
1168 | 13 | break; |
1169 | 0 | case CF_CALCOP("Min(A,B)"): |
1170 | 0 | case CF_CALCOP("Max(A,B)"): |
1171 | 0 | if ( !IsEqual(*result, op.bn0) && !IsEqual(*result, op.bn1) ) { |
1172 | 0 | Abort("Result is not an operand", repository::CalcOpToString(calcOp)); |
1173 | 0 | } |
1174 | 0 | break; |
1175 | 0 | case CF_CALCOP("Mask(A,B)"): |
1176 | 0 | if ( LargerThan(*result, op.bn0) ) { |
1177 | 0 | Abort("Result is larger than input", repository::CalcOpToString(calcOp)); |
1178 | 0 | } |
1179 | 0 | break; |
1180 | 0 | case CF_CALCOP("And(A,B)"): |
1181 | 0 | AssertNotLargerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1182 | 0 | AssertNotLargerThan(*result, op.bn1, repository::CalcOpToString(calcOp)); |
1183 | 0 | break; |
1184 | 0 | case CF_CALCOP("Or(A,B)"): |
1185 | 0 | AssertNotSmallerThan(*result, op.bn0, repository::CalcOpToString(calcOp)); |
1186 | 0 | AssertNotSmallerThan(*result, op.bn1, repository::CalcOpToString(calcOp)); |
1187 | 0 | break; |
1188 | 0 | case CF_CALCOP("Nthrt(A,B)"): |
1189 | 0 | case CF_CALCOP("NthrtRem(A,B)"): |
1190 | 0 | if ( IsZero(op.bn1) ) { |
1191 | 0 | Abort("Root of zero should not produce a result", repository::CalcOpToString(calcOp)); |
1192 | 0 | } |
1193 | 0 | break; |
1194 | 0 | case CF_CALCOP("Zero()"): |
1195 | 0 | if ( !IsZero(*result) ) { |
1196 | 0 | Abort("Result should be zero", repository::CalcOpToString(calcOp)); |
1197 | 0 | } |
1198 | 0 | break; |
1199 | 35 | case CF_CALCOP("GCD(A,B)"): |
1200 | 35 | AssertNotLargerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp)); |
1201 | 35 | break; |
1202 | 65 | case CF_CALCOP("LCM(A,B)"): |
1203 | 65 | AssertNotSmallerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp)); |
1204 | 65 | break; |
1205 | 41 | case CF_CALCOP("InvMod(A,B)"): |
1206 | 41 | if ( !IsZero(*result) ) { |
1207 | 25 | AssertNotLargerThan(*result, op.bn1, repository::CalcOpToString(calcOp)); |
1208 | 25 | } |
1209 | 41 | break; |
1210 | 0 | case CF_CALCOP("Exp(A,B)"): |
1211 | 0 | AssertNotSmallerThan(*result, op.bn0, op.bn1, repository::CalcOpToString(calcOp)); |
1212 | 0 | break; |
1213 | 0 | case CF_CALCOP("RandMod(A)"): |
1214 | 0 | BignumCalc::AssertModResult(*result, op.bn0, "RandMod"); |
1215 | 0 | break; |
1216 | 271 | case CF_CALCOP("Prime()"): |
1217 | 271 | BignumCalc::AssertPositive(*result, repository::CalcOpToString(calcOp)); |
1218 | 271 | if ( result->ToTrimmedString() != "2" ) { |
1219 | 271 | BignumCalc::AssertOdd(*result, repository::CalcOpToString(calcOp)); |
1220 | 271 | } |
1221 | 271 | break; |
1222 | 6 | case CF_CALCOP("RandRange(A,B)"): |
1223 | 6 | AssertNotLargerThan(*result, op.bn1, repository::CalcOpToString(calcOp)); |
1224 | 6 | break; |
1225 | 866 | } |
1226 | 866 | } |
1227 | | |
1228 | 89 | void test(const operation::BignumCalc_Fp2& op, const std::optional<component::Fp2>& result) { |
1229 | 89 | (void)op; |
1230 | 89 | (void)result; |
1231 | 89 | } |
1232 | | |
1233 | 169 | void test(const operation::BignumCalc_Fp12& op, const std::optional<component::Fp12>& result) { |
1234 | 169 | (void)op; |
1235 | 169 | (void)result; |
1236 | 169 | } |
1237 | | |
1238 | | } /* namespace tests */ |
1239 | | } /* namespace cryptofuzz */ |