| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include <cstddef> |
| 32 | #include <cstring> |
| 33 | #include <fstream> |
| 34 | #include <string> |
| 35 | #include <vector> |
| 36 | |
| 37 | #include "warnings.h" |
| 38 | #include "misc_log_ex.h" |
| 39 | #include "crypto/crypto.h" |
| 40 | #include "crypto/hash.h" |
| 41 | #include "crypto-tests.h" |
| 42 | #include "../io.h" |
| 43 | |
| 44 | using namespace std; |
| 45 | using namespace crypto; |
| 46 | typedef crypto::hash chash; |
| 47 | |
| 48 | bool operator !=(const ec_scalar &a, const ec_scalar &b) { |
| 49 | return 0 != memcmp(&a, &b, sizeof(ec_scalar)); |
| 50 | } |
| 51 | |
| 52 | bool operator !=(const ec_point &a, const ec_point &b) { |
| 53 | return 0 != memcmp(&a, &b, sizeof(ec_point)); |
| 54 | } |
| 55 | |
| 56 | bool operator !=(const key_derivation &a, const key_derivation &b) { |
| 57 | return 0 != memcmp(&a, &b, sizeof(key_derivation)); |
| 58 | } |
| 59 | |
| 60 | DISABLE_GCC_WARNING(maybe-uninitialized) |
| 61 | |
| 62 | int main(int argc, char *argv[]) { |
| 63 | TRY_ENTRY(); |
| 64 | fstream input; |
| 65 | string cmd; |
| 66 | size_t test = 0; |
| 67 | bool error = false; |
| 68 | setup_random(); |
| 69 | if (argc != 2) { |
| 70 | cerr << "invalid arguments" << endl; |
| 71 | return 1; |
| 72 | } |
| 73 | input.open(argv[1], ios_base::in); |
| 74 | for (;;) { |
| 75 | ++test; |
| 76 | input.exceptions(ios_base::badbit); |
| 77 | if (!(input >> cmd)) { |
| 78 | break; |
| 79 | } |
| 80 | input.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit); |
| 81 | if (cmd == "check_scalar") { |
| 82 | ec_scalar scalar; |
| 83 | bool expected, actual; |
| 84 | get(input, scalar, expected); |
| 85 | actual = check_scalar(scalar); |
| 86 | if (expected != actual) { |
| 87 | goto error; |
| 88 | } |
| 89 | } else if (cmd == "random_scalar") { |
| 90 | ec_scalar expected, actual; |
| 91 | get(input, expected); |
| 92 | random_scalar(actual); |
| 93 | if (expected != actual) { |
| 94 | goto error; |
| 95 | } |
| 96 | } else if (cmd == "hash_to_scalar") { |
| 97 | vector<char> data; |
| 98 | ec_scalar expected, actual; |
| 99 | get(input, data, expected); |
| 100 | crypto::hash_to_scalar(data.data(), data.size(), actual); |
| 101 | if (expected != actual) { |
| 102 | goto error; |
| 103 | } |
| 104 | } else if (cmd == "generate_keys") { |
| 105 | public_key expected1, actual1; |
| 106 | secret_key expected2, actual2; |
| 107 | get(input, expected1, expected2); |
| 108 | generate_keys(actual1, actual2); |
| 109 | if (expected1 != actual1 || expected2 != actual2) { |
| 110 | goto error; |
| 111 | } |
| 112 | } else if (cmd == "check_key") { |
| 113 | public_key key; |
| 114 | bool expected, actual; |
| 115 | get(input, key, expected); |
| 116 | actual = check_key(key); |
| 117 | if (expected != actual) { |
| 118 | goto error; |
| 119 | } |
| 120 | } else if (cmd == "secret_key_to_public_key") { |
| 121 | secret_key sec; |
| 122 | bool expected1, actual1; |
| 123 | public_key expected2, actual2; |
| 124 | get(input, sec, expected1); |
| 125 | if (expected1) { |
| 126 | get(input, expected2); |
| 127 | } |
| 128 | actual1 = secret_key_to_public_key(sec, actual2); |
| 129 | if (expected1 != actual1 || (expected1 && expected2 != actual2)) { |
| 130 | goto error; |
| 131 | } |
| 132 | } else if (cmd == "generate_key_derivation") { |
| 133 | public_key key1; |
| 134 | secret_key key2; |
| 135 | bool expected1, actual1; |
| 136 | key_derivation expected2, actual2; |
| 137 | get(input, key1, key2, expected1); |
| 138 | if (expected1) { |
| 139 | get(input, expected2); |
| 140 | } |
| 141 | actual1 = generate_key_derivation(key1, key2, actual2); |
| 142 | if (expected1 != actual1 || (expected1 && expected2 != actual2)) { |
| 143 | goto error; |
| 144 | } |
| 145 | } else if (cmd == "derive_public_key") { |
| 146 | key_derivation derivation; |
| 147 | size_t output_index; |
| 148 | public_key base; |
| 149 | bool expected1, actual1; |
| 150 | public_key expected2, actual2; |
| 151 | get(input, derivation, output_index, base, expected1); |
| 152 | if (expected1) { |
| 153 | get(input, expected2); |
| 154 | } |
| 155 | actual1 = derive_public_key(derivation, output_index, base, actual2); |
| 156 | if (expected1 != actual1 || (expected1 && expected2 != actual2)) { |
| 157 | goto error; |
| 158 | } |
| 159 | } else if (cmd == "derive_secret_key") { |
| 160 | key_derivation derivation; |
| 161 | size_t output_index; |
| 162 | secret_key base; |
| 163 | secret_key expected, actual; |
| 164 | get(input, derivation, output_index, base, expected); |
| 165 | derive_secret_key(derivation, output_index, base, actual); |
| 166 | if (expected != actual) { |
| 167 | goto error; |
| 168 | } |
| 169 | } else if (cmd == "generate_signature") { |
| 170 | chash prefix_hash; |
| 171 | public_key pub; |
| 172 | secret_key sec; |
| 173 | signature expected, actual; |
| 174 | get(input, prefix_hash, pub, sec, expected); |
| 175 | generate_signature(prefix_hash, pub, sec, actual); |
| 176 | if (expected != actual) { |
| 177 | goto error; |
| 178 | } |
| 179 | } else if (cmd == "check_signature") { |
| 180 | chash prefix_hash; |
| 181 | public_key pub; |
| 182 | signature sig; |
| 183 | bool expected, actual; |
| 184 | get(input, prefix_hash, pub, sig, expected); |
| 185 | actual = check_signature(prefix_hash, pub, sig); |
| 186 | if (expected != actual) { |
| 187 | goto error; |
| 188 | } |
| 189 | } else if (cmd == "hash_to_point") { |
| 190 | chash h; |
| 191 | ec_point expected, actual; |
| 192 | get(input, h, expected); |
| 193 | hash_to_point(h, actual); |
| 194 | if (expected != actual) { |
| 195 | goto error; |
| 196 | } |
| 197 | } else if (cmd == "hash_to_ec") { |
| 198 | public_key key; |
| 199 | ec_point expected, actual; |
| 200 | get(input, key, expected); |
| 201 | hash_to_ec(key, actual); |
| 202 | if (expected != actual) { |
| 203 | goto error; |
| 204 | } |
| 205 | } else if (cmd == "generate_key_image") { |
| 206 | public_key pub; |
| 207 | secret_key sec; |
| 208 | key_image expected, actual; |
| 209 | get(input, pub, sec, expected); |
| 210 | generate_key_image(pub, sec, actual); |
| 211 | if (expected != actual) { |
| 212 | goto error; |
| 213 | } |
| 214 | } else if (cmd == "generate_ring_signature") { |
| 215 | chash prefix_hash; |
| 216 | key_image image; |
| 217 | vector<public_key> vpubs; |
| 218 | vector<const public_key *> pubs; |
| 219 | size_t pubs_count; |
| 220 | secret_key sec; |
| 221 | size_t sec_index; |
| 222 | vector<signature> expected, actual; |
| 223 | size_t i; |
| 224 | get(input, prefix_hash, image, pubs_count); |
| 225 | vpubs.resize(pubs_count); |
| 226 | pubs.resize(pubs_count); |
| 227 | for (i = 0; i < pubs_count; i++) { |
| 228 | get(input, vpubs[i]); |
| 229 | pubs[i] = &vpubs[i]; |
| 230 | } |
| 231 | get(input, sec, sec_index); |
| 232 | expected.resize(pubs_count); |
| 233 | getvar(input, pubs_count * sizeof(signature), expected.data()); |
| 234 | actual.resize(pubs_count); |
| 235 | generate_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sec, sec_index, actual.data()); |
| 236 | if (expected != actual) { |
| 237 | goto error; |
| 238 | } |
| 239 | } else if (cmd == "check_ring_signature") { |
| 240 | chash prefix_hash; |
| 241 | key_image image; |
| 242 | vector<public_key> vpubs; |
| 243 | vector<const public_key *> pubs; |
| 244 | size_t pubs_count; |
| 245 | vector<signature> sigs; |
| 246 | bool expected, actual; |
| 247 | size_t i; |
| 248 | get(input, prefix_hash, image, pubs_count); |
| 249 | vpubs.resize(pubs_count); |
| 250 | pubs.resize(pubs_count); |
| 251 | for (i = 0; i < pubs_count; i++) { |
| 252 | get(input, vpubs[i]); |
| 253 | pubs[i] = &vpubs[i]; |
| 254 | } |
| 255 | sigs.resize(pubs_count); |
| 256 | getvar(input, pubs_count * sizeof(signature), sigs.data()); |
| 257 | get(input, expected); |
| 258 | actual = check_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sigs.data()); |
| 259 | if (expected != actual) { |
| 260 | goto error; |
| 261 | } |
| 262 | } else if (cmd == "check_ge_p3_identity") { |
| 263 | public_key point; |
| 264 | bool expected_bad, expected_good, result_badfunc, result_goodfunc; |
| 265 | get(input, point, expected_bad, expected_good); |
| 266 | result_badfunc = check_ge_p3_identity_failure(point); |
| 267 | result_goodfunc = check_ge_p3_identity_success(point); |
| 268 | if (expected_bad != result_badfunc || expected_good != result_goodfunc) { |
| 269 | goto error; |
| 270 | } |
| 271 | } else if (cmd == "derive_view_tag") { |
| 272 | key_derivation derivation; |
| 273 | size_t output_index; |
| 274 | view_tag expected, actual; |
| 275 | get(input, derivation, output_index, expected); |
| 276 | derive_view_tag(derivation, output_index, actual); |
| 277 | if (expected != actual) { |
| 278 | goto error; |
| 279 | } |
| 280 | } else { |
| 281 | throw ios_base::failure("Unknown function: " + cmd); |
| 282 | } |
| 283 | continue; |
| 284 | error: |
| 285 | cerr << "Wrong result on test " << test << endl; |
| 286 | error = true; |
| 287 | } |
| 288 | return error ? 1 : 0; |
| 289 | CATCH_ENTRY_L0("main", 1); |
| 290 | } |