| 1 | // Copyright 2010 Dolphin Emulator Project |
| 2 | // Copyright 2007,2008 Segher Boessenkool <[email protected]> |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | |
| 5 | #include "Common/Crypto/ec.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <cstring> |
| 9 | |
| 10 | #include "Common/Crypto/bn.h" |
| 11 | #include "Common/Random.h" |
| 12 | |
| 13 | namespace Common::ec |
| 14 | { |
| 15 | static const u8 square[16] = {0x00, 0x01, 0x04, 0x05, 0x10, 0x11, 0x14, 0x15, |
| 16 | 0x40, 0x41, 0x44, 0x45, 0x50, 0x51, 0x54, 0x55}; |
| 17 | |
| 18 | struct Elt; |
| 19 | static Elt operator*(const Elt& a, const Elt& b); |
| 20 | |
| 21 | struct Elt |
| 22 | { |
| 23 | bool IsZero() const |
| 24 | { |
| 25 | return std::ranges::all_of(data, [](u8 b) { return b == 0; }); |
| 26 | } |
| 27 | |
| 28 | void MulX() |
| 29 | { |
| 30 | u8 carry = data[0] & 1; |
| 31 | u8 x = 0; |
| 32 | for (std::size_t i = 0; i < data.size() - 1; i++) |
| 33 | { |
| 34 | u8 y = data[i + 1]; |
| 35 | data[i] = x ^ (y >> 7); |
| 36 | x = y << 1; |
| 37 | } |
| 38 | data[29] = x ^ carry; |
| 39 | data[20] ^= carry << 2; |
| 40 | } |
| 41 | |
| 42 | Elt Square() const |
| 43 | { |
| 44 | std::array<u8, 60> wide; |
| 45 | for (std::size_t i = 0; i < data.size(); i++) |
| 46 | { |
| 47 | wide[2 * i] = square[data[i] >> 4]; |
| 48 | wide[2 * i + 1] = square[data[i] & 15]; |
| 49 | } |
| 50 | for (std::size_t i = 0; i < data.size(); i++) |
| 51 | { |
| 52 | u8 x = wide[i]; |
| 53 | |
| 54 | wide[i + 19] ^= x >> 7; |
| 55 | wide[i + 20] ^= x << 1; |
| 56 | |
| 57 | wide[i + 29] ^= x >> 1; |
| 58 | wide[i + 30] ^= x << 7; |
| 59 | } |
| 60 | |
| 61 | u8 x = wide[30] & ~1; |
| 62 | wide[49] ^= x >> 7; |
| 63 | wide[50] ^= x << 1; |
| 64 | wide[59] ^= x >> 1; |
| 65 | wide[30] &= 1; |
| 66 | |
| 67 | Elt result; |
| 68 | std::copy(wide.cbegin() + 30, wide.cend(), result.data.begin()); |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | Elt ItohTsujii(const Elt& b, std::size_t j) const |
| 73 | { |
| 74 | Elt t = *this; |
| 75 | while (j--) |
| 76 | t = t.Square(); |
| 77 | return t * b; |
| 78 | } |
| 79 | |
| 80 | Elt Inv() const |
| 81 | { |
| 82 | Elt t = ItohTsujii(*this, 1); |
| 83 | Elt s = t.ItohTsujii(*this, 1); |
| 84 | t = s.ItohTsujii(s, 3); |
| 85 | s = t.ItohTsujii(*this, 1); |
| 86 | t = s.ItohTsujii(s, 7); |
| 87 | s = t.ItohTsujii(t, 14); |
| 88 | t = s.ItohTsujii(*this, 1); |
| 89 | s = t.ItohTsujii(t, 29); |
| 90 | t = s.ItohTsujii(s, 58); |
| 91 | s = t.ItohTsujii(t, 116); |
| 92 | return s.Square(); |
| 93 | } |
| 94 | |
| 95 | std::array<u8, 30> data{}; |
| 96 | }; |
| 97 | |
| 98 | static Elt operator+(const Elt& a, const Elt& b) |
| 99 | { |
| 100 | Elt d; |
| 101 | for (std::size_t i = 0; i < std::tuple_size<decltype(Elt::data)>{}; i++) |
| 102 | d.data[i] = a.data[i] ^ b.data[i]; |
| 103 | return d; |
| 104 | } |
| 105 | |
| 106 | static Elt operator*(const Elt& a, const Elt& b) |
| 107 | { |
| 108 | Elt d; |
| 109 | std::size_t i = 0; |
| 110 | u8 mask = 1; |
| 111 | for (std::size_t n = 0; n < 233; n++) |
| 112 | { |
| 113 | d.MulX(); |
| 114 | |
| 115 | if ((a.data[i] & mask) != 0) |
| 116 | d = d + b; |
| 117 | |
| 118 | mask >>= 1; |
| 119 | if (mask == 0) |
| 120 | { |
| 121 | mask = 0x80; |
| 122 | i++; |
| 123 | } |
| 124 | } |
| 125 | return d; |
| 126 | } |
| 127 | |
| 128 | static Elt operator/(const Elt& dividend, const Elt& divisor) |
| 129 | { |
| 130 | return dividend * divisor.Inv(); |
| 131 | } |
| 132 | |
| 133 | struct Point |
| 134 | { |
| 135 | Point() = default; |
| 136 | constexpr explicit Point(Elt x, Elt y) : m_data{{x, y}} {} |
| 137 | explicit Point(const u8* data) { std::copy_n(data, sizeof(m_data), Data()); } |
| 138 | |
| 139 | bool IsZero() const { return X().IsZero() && Y().IsZero(); } |
| 140 | Elt& X() { return m_data[0]; } |
| 141 | Elt& Y() { return m_data[1]; } |
| 142 | u8* Data() { return m_data[0].data.data(); } |
| 143 | const Elt& X() const { return m_data[0]; } |
| 144 | const Elt& Y() const { return m_data[1]; } |
| 145 | const u8* Data() const { return m_data[0].data.data(); } |
| 146 | |
| 147 | Point Double() const |
| 148 | { |
| 149 | Point r; |
| 150 | if (X().IsZero()) |
| 151 | return r; |
| 152 | |
| 153 | const auto s = Y() / X() + X(); |
| 154 | r.X() = s.Square() + s; |
| 155 | r.X().data[29] ^= 1; |
| 156 | r.Y() = s * r.X() + r.X() + X().Square(); |
| 157 | return r; |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | std::array<Elt, 2> m_data{}; |
| 162 | static_assert(sizeof(decltype(m_data)) == 60, "Wrong size for m_data"); |
| 163 | }; |
| 164 | |
| 165 | // y**2 + x*y = x**3 + x + b |
| 166 | [[maybe_unused]] static const u8 ec_b[30] = { |
| 167 | 0x00, 0x66, 0x64, 0x7e, 0xde, 0x6c, 0x33, 0x2c, 0x7f, 0x8c, 0x09, 0x23, 0xbb, 0x58, 0x21, |
| 168 | 0x3b, 0x33, 0x3b, 0x20, 0xe9, 0xce, 0x42, 0x81, 0xfe, 0x11, 0x5f, 0x7d, 0x8f, 0x90, 0xad}; |
| 169 | |
| 170 | // order of the addition group of points |
| 171 | static const u8 ec_N[30] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 172 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0xe9, 0x74, 0xe7, 0x2f, |
| 173 | 0x8a, 0x69, 0x22, 0x03, 0x1d, 0x26, 0x03, 0xcf, 0xe0, 0xd7}; |
| 174 | |
| 175 | // base point |
| 176 | constexpr Point ec_G{ |
| 177 | {{{0x00, 0xfa, 0xc9, 0xdf, 0xcb, 0xac, 0x83, 0x13, 0xbb, 0x21, 0x39, 0xf1, 0xbb, 0x75, 0x5f, |
| 178 | 0xef, 0x65, 0xbc, 0x39, 0x1f, 0x8b, 0x36, 0xf8, 0xf8, 0xeb, 0x73, 0x71, 0xfd, 0x55, 0x8b}}}, |
| 179 | {{{0x01, 0x00, 0x6a, 0x08, 0xa4, 0x19, 0x03, 0x35, 0x06, 0x78, 0xe5, 0x85, 0x28, 0xbe, 0xbf, |
| 180 | 0x8a, 0x0b, 0xef, 0xf8, 0x67, 0xa7, 0xca, 0x36, 0x71, 0x6f, 0x7e, 0x01, 0xf8, 0x10, 0x52}}}}; |
| 181 | |
| 182 | static Point operator+(const Point& a, const Point& b) |
| 183 | { |
| 184 | if (a.IsZero()) |
| 185 | return b; |
| 186 | if (b.IsZero()) |
| 187 | return a; |
| 188 | |
| 189 | Elt u = a.X() + b.X(); |
| 190 | if (u.IsZero()) |
| 191 | { |
| 192 | u = a.Y() + b.Y(); |
| 193 | if (u.IsZero()) |
| 194 | return a.Double(); |
| 195 | return Point{}; |
| 196 | } |
| 197 | |
| 198 | const Elt s = (a.Y() + b.Y()) / u; |
| 199 | Elt t = s.Square() + s + b.X(); |
| 200 | t.data[29] ^= 1; |
| 201 | |
| 202 | const Elt rx = t + a.X(); |
| 203 | const Elt ry = s * t + a.Y() + rx; |
| 204 | return Point{rx, ry}; |
| 205 | } |
| 206 | |
| 207 | static Point operator*(const u8* a, const Point& b) |
| 208 | { |
| 209 | Point d; |
| 210 | for (std::size_t i = 0; i < 30; i++) |
| 211 | { |
| 212 | for (u8 mask = 0x80; mask != 0; mask >>= 1) |
| 213 | { |
| 214 | d = d.Double(); |
| 215 | if ((a[i] & mask) != 0) |
| 216 | d = d + b; |
| 217 | } |
| 218 | } |
| 219 | return d; |
| 220 | } |
| 221 | |
| 222 | Signature Sign(const u8* key, const u8* hash) |
| 223 | { |
| 224 | u8 e[30]{}; |
| 225 | memcpy(e + 10, hash, 20); |
| 226 | |
| 227 | u8 m[30]; |
| 228 | do |
| 229 | { |
| 230 | // Generate 240 bits and keep 233. |
| 231 | Common::Random::Generate(m, sizeof(m)); |
| 232 | m[0] &= 1; |
| 233 | } while (bn_compare(m, ec_N, sizeof(m)) >= 0); |
| 234 | |
| 235 | Elt r = (m * ec_G).X(); |
| 236 | if (bn_compare(r.data.data(), ec_N, 30) >= 0) |
| 237 | bn_sub_modulus(r.data.data(), ec_N, 30); |
| 238 | |
| 239 | // S = m**-1*(e + Rk) (mod N) |
| 240 | |
| 241 | u8 kk[30]; |
| 242 | std::copy_n(key, sizeof(kk), kk); |
| 243 | if (bn_compare(kk, ec_N, sizeof(kk)) >= 0) |
| 244 | bn_sub_modulus(kk, ec_N, sizeof(kk)); |
| 245 | Elt s; |
| 246 | bn_mul(s.data.data(), r.data.data(), kk, ec_N, 30); |
| 247 | bn_add(kk, s.data.data(), e, ec_N, sizeof(kk)); |
| 248 | u8 minv[30]; |
| 249 | bn_inv(minv, m, ec_N, sizeof(minv)); |
| 250 | bn_mul(s.data.data(), minv, kk, ec_N, 30); |
| 251 | |
| 252 | Signature signature; |
| 253 | std::ranges::copy(r.data, signature.begin()); |
| 254 | std::ranges::copy(s.data, signature.begin() + 30); |
| 255 | return signature; |
| 256 | } |
| 257 | |
| 258 | bool VerifySignature(const u8* public_key, const u8* signature, const u8* hash) |
| 259 | { |
| 260 | const u8* R = signature; |
| 261 | const u8* S = signature + 30; |
| 262 | u8 Sinv[30]; |
| 263 | |
| 264 | bn_inv(Sinv, S, ec_N, 30); |
| 265 | u8 e[30]{}; |
| 266 | memcpy(e + 10, hash, 20); |
| 267 | |
| 268 | u8 w1[30], w2[30]; |
| 269 | bn_mul(w1, e, Sinv, ec_N, 30); |
| 270 | bn_mul(w2, R, Sinv, ec_N, 30); |
| 271 | |
| 272 | Point r1 = w1 * ec_G + w2 * Point{public_key}; |
| 273 | auto& rx = r1.X().data; |
| 274 | if (bn_compare(rx.data(), ec_N, 30) >= 0) |
| 275 | bn_sub_modulus(rx.data(), ec_N, 30); |
| 276 | |
| 277 | return (bn_compare(rx.data(), R, 30) == 0); |
| 278 | } |
| 279 | |
| 280 | PublicKey PrivToPub(const u8* key) |
| 281 | { |
| 282 | const Point data = key * ec_G; |
| 283 | PublicKey result; |
| 284 | std::copy_n(data.Data(), result.size(), result.begin()); |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | std::array<u8, 60> ComputeSharedSecret(const u8* private_key, const u8* public_key) |
| 289 | { |
| 290 | std::array<u8, 60> shared_secret; |
| 291 | const Point data = private_key * Point{public_key}; |
| 292 | std::copy_n(data.Data(), shared_secret.size(), shared_secret.begin()); |
| 293 | return shared_secret; |
| 294 | } |
| 295 | } // namespace Common::ec |