| 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 "crypto/crypto.cpp" |
| 32 | |
| 33 | #include "crypto-tests.h" |
| 34 | |
| 35 | static void get_ge_p3_for_identity_test(const crypto::public_key &point, crypto::ge_p3 &result_out_p3) |
| 36 | { |
| 37 | // compute (K + K) - K - K to get a specific ge_p3 point representation of identity |
| 38 | crypto::ge_cached temp_cache; |
| 39 | crypto::ge_p1p1 temp_p1p1; |
| 40 | |
| 41 | crypto::ge_frombytes_vartime(&result_out_p3, &point); // K |
| 42 | crypto::ge_p3_to_cached(&temp_cache, &result_out_p3); |
| 43 | crypto::ge_add(&temp_p1p1, &result_out_p3, &temp_cache); // K + K |
| 44 | crypto::ge_p1p1_to_p3(&result_out_p3, &temp_p1p1); |
| 45 | crypto::ge_sub(&temp_p1p1, &result_out_p3, &temp_cache); // (K + K) - K |
| 46 | crypto::ge_p1p1_to_p3(&result_out_p3, &temp_p1p1); |
| 47 | crypto::ge_sub(&temp_p1p1, &result_out_p3, &temp_cache); // ((K + K) - K) - K |
| 48 | crypto::ge_p1p1_to_p3(&result_out_p3, &temp_p1p1); |
| 49 | } |
| 50 | |
| 51 | static int ge_p3_is_point_at_infinity_vartime_bad(const crypto::ge_p3 *p) { |
| 52 | // X = 0 and Y == Z |
| 53 | // bad: components of 'p' are not reduced mod q |
| 54 | int n; |
| 55 | for (n = 0; n < 10; ++n) |
| 56 | { |
| 57 | if (p->X[n] | p->T[n]) |
| 58 | return 0; |
| 59 | if (p->Y[n] != p->Z[n]) |
| 60 | return 0; |
| 61 | } |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | bool check_scalar(const crypto::ec_scalar &scalar) { |
| 66 | return crypto::sc_check(crypto::operator &(scalar)) == 0; |
| 67 | } |
| 68 | |
| 69 | void random_scalar(crypto::ec_scalar &res) { |
| 70 | crypto::random_scalar(res); |
| 71 | } |
| 72 | |
| 73 | void hash_to_scalar(const void *data, std::size_t length, crypto::ec_scalar &res) { |
| 74 | crypto::hash_to_scalar(data, length, res); |
| 75 | } |
| 76 | |
| 77 | void hash_to_point(const crypto::hash &h, crypto::ec_point &res) { |
| 78 | crypto::ge_p2 point; |
| 79 | crypto::ge_fromfe_frombytes_vartime(&point, reinterpret_cast<const unsigned char *>(&h)); |
| 80 | crypto::ge_tobytes(crypto::operator &(res), &point); |
| 81 | } |
| 82 | |
| 83 | void hash_to_ec(const crypto::public_key &key, crypto::ec_point &res) { |
| 84 | crypto::ge_p3 tmp; |
| 85 | crypto::hash_to_ec(key, tmp); |
| 86 | crypto::ge_p3_tobytes(crypto::operator &(res), &tmp); |
| 87 | } |
| 88 | |
| 89 | bool check_ge_p3_identity_failure(const crypto::public_key &point) |
| 90 | { |
| 91 | crypto::ge_p3 ident_p3; |
| 92 | get_ge_p3_for_identity_test(point, ident_p3); |
| 93 | |
| 94 | return ge_p3_is_point_at_infinity_vartime_bad(&ident_p3) == 1; |
| 95 | } |
| 96 | |
| 97 | bool check_ge_p3_identity_success(const crypto::public_key &point) |
| 98 | { |
| 99 | crypto::ge_p3 ident_p3; |
| 100 | get_ge_p3_for_identity_test(point, ident_p3); |
| 101 | |
| 102 | return crypto::ge_p3_is_point_at_infinity_vartime(&ident_p3) == 1; |
| 103 | } |