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