aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / sig / ed25519.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Ed25519 (RFC 8032 Section 5.1), pure Ed25519 only. |
| 18 | * |
| 19 | * Field GF(2^255 - 19): five 51-bit limbs, 128-bit products. |
| 20 | * Points: extended twisted Edwards coordinates (X:Y:Z:T), a = -1, with the |
| 21 | * RFC 8032 Section 5.1.4 addition and doubling formulas (complete). |
| 22 | * Scalars mod L: bit-serial reduction with a masked conditional subtract. |
| 23 | * |
| 24 | * Constant time (signing, key derivation): the scalar ladder always doubles |
| 25 | * and adds and keeps the sum with a masked select; there is no branch and |
| 26 | * no memory index that depends on a secret. Exponents used in inversion and |
| 27 | * square roots are public constants. Verification and point decoding take |
| 28 | * only public input and use ordinary branches. |
| 29 | * |
| 30 | * Lines tagged GUARD:<name> are removed one at a time by `make mutants`. |
| 31 | */ |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include "aienos_sig.h" |
| 35 | #include "sig_internal.h" |
| 36 | |
| 37 | typedef unsigned __int128 u128; |
| 38 | typedef uint64_t fe[5]; |
| 39 | typedef struct { |
| 40 | fe X, Y, Z, T; |
| 41 | } ge; |
| 42 | typedef struct { |
| 43 | fe d, d2, sqrtm1; |
| 44 | ge B; |
| 45 | } consts; |
| 46 | |
| 47 | #define MASK51 ((uint64_t)0x7ffffffffffff) |
| 48 | |
| 49 | /* ---------------------------------------------------------------- field */ |
| 50 | |
| 51 | static void fe_0(fe h) { h[0] = h[1] = h[2] = h[3] = h[4] = 0; } |
| 52 | static void fe_1(fe h) { fe_0(h); h[0] = 1; } |
| 53 | static void fe_copy(fe h, const fe f) { for (int i = 0; i < 5; i++) h[i] = f[i]; } |
| 54 | |
| 55 | /* Weak reduction: afterwards h1..h4 < 2^51 and h0 < 2^51 + 2^18. */ |
| 56 | static void fe_carry(fe h) |
| 57 | { |
| 58 | uint64_t c; |
| 59 | c = h[0] >> 51; h[0] &= MASK51; h[1] += c; |
| 60 | c = h[1] >> 51; h[1] &= MASK51; h[2] += c; |
| 61 | c = h[2] >> 51; h[2] &= MASK51; h[3] += c; |
| 62 | c = h[3] >> 51; h[3] &= MASK51; h[4] += c; |
| 63 | c = h[4] >> 51; h[4] &= MASK51; h[0] += 19 * c; |
| 64 | } |
| 65 | |
| 66 | static void fe_add(fe h, const fe f, const fe g) |
| 67 | { |
| 68 | for (int i = 0; i < 5; i++) h[i] = f[i] + g[i]; |
| 69 | fe_carry(h); |
| 70 | } |
| 71 | |
| 72 | /* h = f - g computed as f + 4p - g (g limbs are below 2^53 - 76). */ |
| 73 | static void fe_sub(fe h, const fe f, const fe g) |
| 74 | { |
| 75 | h[0] = f[0] + 0x1fffffffffffb4 - g[0]; |
| 76 | h[1] = f[1] + 0x1ffffffffffffc - g[1]; |
| 77 | h[2] = f[2] + 0x1ffffffffffffc - g[2]; |
| 78 | h[3] = f[3] + 0x1ffffffffffffc - g[3]; |
| 79 | h[4] = f[4] + 0x1ffffffffffffc - g[4]; |
| 80 | fe_carry(h); |
| 81 | } |
| 82 | |
| 83 | static void fe_neg(fe h, const fe f) |
| 84 | { |
| 85 | fe z; |
| 86 | fe_0(z); |
| 87 | fe_sub(h, z, f); |
| 88 | } |
| 89 | |
| 90 | static void fe_mul(fe h, const fe f, const fe g) |
| 91 | { |
| 92 | uint64_t f0 = f[0], f1 = f[1], f2 = f[2], f3 = f[3], f4 = f[4]; |
| 93 | uint64_t g0 = g[0], g1 = g[1], g2 = g[2], g3 = g[3], g4 = g[4]; |
| 94 | uint64_t g1_19 = 19 * g1, g2_19 = 19 * g2, g3_19 = 19 * g3, g4_19 = 19 * g4; |
| 95 | u128 r0 = (u128)f0 * g0 + (u128)f1 * g4_19 + (u128)f2 * g3_19 + (u128)f3 * g2_19 + (u128)f4 * g1_19; |
| 96 | u128 r1 = (u128)f0 * g1 + (u128)f1 * g0 + (u128)f2 * g4_19 + (u128)f3 * g3_19 + (u128)f4 * g2_19; |
| 97 | u128 r2 = (u128)f0 * g2 + (u128)f1 * g1 + (u128)f2 * g0 + (u128)f3 * g4_19 + (u128)f4 * g3_19; |
| 98 | u128 r3 = (u128)f0 * g3 + (u128)f1 * g2 + (u128)f2 * g1 + (u128)f3 * g0 + (u128)f4 * g4_19; |
| 99 | u128 r4 = (u128)f0 * g4 + (u128)f1 * g3 + (u128)f2 * g2 + (u128)f3 * g1 + (u128)f4 * g0; |
| 100 | r1 += r0 >> 51; |
| 101 | r2 += r1 >> 51; |
| 102 | r3 += r2 >> 51; |
| 103 | r4 += r3 >> 51; |
| 104 | u128 c = r4 >> 51; |
| 105 | uint64_t h0 = (uint64_t)r0 & MASK51, h1 = (uint64_t)r1 & MASK51, h2 = (uint64_t)r2 & MASK51; |
| 106 | uint64_t h3 = (uint64_t)r3 & MASK51, h4 = (uint64_t)r4 & MASK51; |
| 107 | u128 t = (u128)h0 + c * 19; |
| 108 | h0 = (uint64_t)t & MASK51; |
| 109 | h1 += (uint64_t)(t >> 51); |
| 110 | h[0] = h0; h[1] = h1; h[2] = h2; h[3] = h3; h[4] = h4; |
| 111 | } |
| 112 | |
| 113 | static void fe_sq(fe h, const fe f) { fe_mul(h, f, f); } |
| 114 | |
| 115 | static uint64_t load_le64(const uint8_t *p) |
| 116 | { |
| 117 | uint64_t v = 0; |
| 118 | for (int i = 7; i >= 0; i--) |
| 119 | v = (v << 8) | p[i]; |
| 120 | return v; |
| 121 | } |
| 122 | |
| 123 | static void store_le64(uint8_t *p, uint64_t v) |
| 124 | { |
| 125 | for (int i = 0; i < 8; i++) { |
| 126 | p[i] = (uint8_t)v; |
| 127 | v >>= 8; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Reads 255 bits (bit 255 ignored); the value may be >= p. */ |
| 132 | static void fe_frombytes(fe h, const uint8_t s[32]) |
| 133 | { |
| 134 | h[0] = load_le64(s) & MASK51; |
| 135 | h[1] = (load_le64(s + 6) >> 3) & MASK51; |
| 136 | h[2] = (load_le64(s + 12) >> 6) & MASK51; |
| 137 | h[3] = (load_le64(s + 19) >> 1) & MASK51; |
| 138 | h[4] = (load_le64(s + 24) >> 12) & MASK51; |
| 139 | } |
| 140 | |
| 141 | /* Canonical encoding (fully reduced mod p), branch free. */ |
| 142 | static void fe_tobytes(uint8_t s[32], const fe f) |
| 143 | { |
| 144 | fe t; |
| 145 | fe_copy(t, f); |
| 146 | fe_carry(t); |
| 147 | fe_carry(t); /* now t < 2^255 + 19 < 2p */ |
| 148 | uint64_t q = (t[0] + 19) >> 51; |
| 149 | q = (t[1] + q) >> 51; |
| 150 | q = (t[2] + q) >> 51; |
| 151 | q = (t[3] + q) >> 51; |
| 152 | q = (t[4] + q) >> 51; /* q = 1 iff t >= p */ |
| 153 | t[0] += 19 * q; |
| 154 | t[1] += t[0] >> 51; t[0] &= MASK51; |
| 155 | t[2] += t[1] >> 51; t[1] &= MASK51; |
| 156 | t[3] += t[2] >> 51; t[2] &= MASK51; |
| 157 | t[4] += t[3] >> 51; t[3] &= MASK51; |
| 158 | t[4] &= MASK51; /* drop q * 2^255 */ |
| 159 | store_le64(s, t[0] | (t[1] << 51)); |
| 160 | store_le64(s + 8, (t[1] >> 13) | (t[2] << 38)); |
| 161 | store_le64(s + 16, (t[2] >> 26) | (t[3] << 25)); |
| 162 | store_le64(s + 24, (t[3] >> 39) | (t[4] << 12)); |
| 163 | } |
| 164 | |
| 165 | /* f = g if b == 1, unchanged if b == 0; b must be 0 or 1. */ |
| 166 | static void fe_cmov(fe f, const fe g, uint64_t b) |
| 167 | { |
| 168 | uint64_t m = 0 - b; |
| 169 | AIENOS_SIG_BARRIER(m); |
| 170 | for (int i = 0; i < 5; i++) |
| 171 | f[i] ^= m & (f[i] ^ g[i]); |
| 172 | } |
| 173 | |
| 174 | /* Low bit of the canonical value (the RFC 8032 "sign" of x). */ |
| 175 | static uint64_t fe_isneg(const fe f) |
| 176 | { |
| 177 | uint8_t s[32]; |
| 178 | fe_tobytes(s, f); |
| 179 | return s[0] & 1; |
| 180 | } |
| 181 | |
| 182 | /* Public data only. */ |
| 183 | static int fe_iszero(const fe f) |
| 184 | { |
| 185 | uint8_t s[32], d = 0; |
| 186 | fe_tobytes(s, f); |
| 187 | for (int i = 0; i < 32; i++) d |= s[i]; |
| 188 | return d == 0; |
| 189 | } |
| 190 | |
| 191 | static int fe_eq(const fe f, const fe g) |
| 192 | { |
| 193 | uint8_t a[32], b[32]; |
| 194 | fe_tobytes(a, f); |
| 195 | fe_tobytes(b, g); |
| 196 | return aienos_sig_ct_equal(a, b, 32); |
| 197 | } |
| 198 | |
| 199 | /* out = z^e, e a PUBLIC little-endian 256-bit exponent. */ |
| 200 | static void fe_pow(fe out, const fe z, const uint8_t e[32]) |
| 201 | { |
| 202 | fe r, b; |
| 203 | fe_1(r); |
| 204 | fe_copy(b, z); |
| 205 | for (int i = 255; i >= 0; i--) { |
| 206 | fe_sq(r, r); |
| 207 | if ((e[i >> 3] >> (i & 7)) & 1) |
| 208 | fe_mul(r, r, b); |
| 209 | } |
| 210 | fe_copy(out, r); |
| 211 | } |
| 212 | |
| 213 | /* p - 2 = 2^255 - 21, (p - 5) / 8 = 2^252 - 3, (p - 1) / 4 = 2^253 - 5 */ |
| 214 | static const uint8_t EXP_INV[32] = { |
| 215 | 0xeb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 216 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}; |
| 217 | static const uint8_t EXP_P58[32] = { |
| 218 | 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 219 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f}; |
| 220 | static const uint8_t EXP_SQRTM1[32] = { |
| 221 | 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 222 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f}; |
| 223 | |
| 224 | static void fe_invert(fe out, const fe z) { fe_pow(out, z, EXP_INV); } |
| 225 | |
| 226 | /* ---------------------------------------------------------------- points */ |
| 227 | |
| 228 | static void ge_identity(ge *p) |
| 229 | { |
| 230 | fe_0(p->X); fe_1(p->Y); fe_1(p->Z); fe_0(p->T); |
| 231 | } |
| 232 | |
| 233 | static void ge_add(ge *r, const ge *p, const ge *q, const consts *k) |
| 234 | { |
| 235 | fe a, b, c, d, e, f, g, h, t; |
| 236 | fe_sub(a, p->Y, p->X); fe_sub(t, q->Y, q->X); fe_mul(a, a, t); |
| 237 | fe_add(b, p->Y, p->X); fe_add(t, q->Y, q->X); fe_mul(b, b, t); |
| 238 | fe_mul(c, p->T, q->T); fe_mul(c, c, k->d2); |
| 239 | fe_mul(d, p->Z, q->Z); fe_add(d, d, d); |
| 240 | fe_sub(e, b, a); fe_sub(f, d, c); fe_add(g, d, c); fe_add(h, b, a); |
| 241 | fe_mul(r->X, e, f); fe_mul(r->Y, g, h); fe_mul(r->T, e, h); fe_mul(r->Z, f, g); |
| 242 | } |
| 243 | |
| 244 | static void ge_dbl(ge *r, const ge *p) |
| 245 | { |
| 246 | fe a, b, c, e, f, g, h, t; |
| 247 | fe_sq(a, p->X); fe_sq(b, p->Y); fe_sq(c, p->Z); fe_add(c, c, c); |
| 248 | fe_add(h, a, b); fe_add(t, p->X, p->Y); fe_sq(t, t); fe_sub(e, h, t); |
| 249 | fe_sub(g, a, b); fe_add(f, c, g); |
| 250 | fe_mul(r->X, e, f); fe_mul(r->Y, g, h); fe_mul(r->T, e, h); fe_mul(r->Z, f, g); |
| 251 | } |
| 252 | |
| 253 | static void ge_cmov(ge *r, const ge *p, uint64_t b) |
| 254 | { |
| 255 | fe_cmov(r->X, p->X, b); fe_cmov(r->Y, p->Y, b); |
| 256 | fe_cmov(r->Z, p->Z, b); fe_cmov(r->T, p->T, b); |
| 257 | } |
| 258 | |
| 259 | static void ge_neg(ge *r, const ge *p) |
| 260 | { |
| 261 | fe_neg(r->X, p->X); fe_copy(r->Y, p->Y); fe_copy(r->Z, p->Z); fe_neg(r->T, p->T); |
| 262 | } |
| 263 | |
| 264 | /* r = [s]p, constant time in s: 256 doublings and 256 additions always, |
| 265 | * the sum kept by a masked select. */ |
| 266 | static void ge_scalarmult(ge *r, const ge *p, const uint8_t s[32], const consts *k) |
| 267 | { |
| 268 | ge q, t; |
| 269 | ge_identity(&q); |
| 270 | for (int i = 255; i >= 0; i--) { |
| 271 | uint64_t bit = (uint64_t)((s[i >> 3] >> (i & 7)) & 1); |
| 272 | ge_dbl(&q, &q); |
| 273 | ge_add(&t, &q, p, k); |
| 274 | ge_cmov(&q, &t, bit); |
| 275 | } |
| 276 | *r = q; |
| 277 | aienos_sig_wipe(&q, sizeof q); |
| 278 | aienos_sig_wipe(&t, sizeof t); |
| 279 | } |
| 280 | |
| 281 | static void ge_tobytes(uint8_t s[32], const ge *p) |
| 282 | { |
| 283 | fe zi, x, y; |
| 284 | fe_invert(zi, p->Z); |
| 285 | fe_mul(x, p->X, zi); |
| 286 | fe_mul(y, p->Y, zi); |
| 287 | fe_tobytes(s, y); |
| 288 | s[31] ^= (uint8_t)(fe_isneg(x) << 7); |
| 289 | aienos_sig_wipe(zi, sizeof zi); |
| 290 | aienos_sig_wipe(x, sizeof x); |
| 291 | aienos_sig_wipe(y, sizeof y); |
| 292 | } |
| 293 | |
| 294 | /* 1 iff the 255-bit value y (bit 255 already cleared) is >= p. */ |
| 295 | static int y_noncanonical(const uint8_t y[32]) |
| 296 | { |
| 297 | if (y[31] != 0x7f) return 0; |
| 298 | for (int i = 30; i >= 1; i--) |
| 299 | if (y[i] != 0xff) return 0; |
| 300 | return y[0] >= 0xed; |
| 301 | } |
| 302 | |
| 303 | /* RFC 8032 Section 5.1.3 decoding. Public input; returns 0 or -1. */ |
| 304 | static int ge_frombytes(ge *p, const uint8_t s[32], const consts *k) |
| 305 | { |
| 306 | uint8_t yb[32]; |
| 307 | memcpy(yb, s, 32); |
| 308 | uint64_t sign = yb[31] >> 7; |
| 309 | yb[31] &= 0x7f; |
| 310 | if (y_noncanonical(yb)) return -1; /* GUARD:y-canonical */ |
| 311 | fe y, y2, u, v, v3, x, vx2, nu, one; |
| 312 | fe_frombytes(y, yb); |
| 313 | fe_1(one); |
| 314 | fe_sq(y2, y); |
| 315 | fe_sub(u, y2, one); /* u = y^2 - 1 */ |
| 316 | fe_mul(v, y2, k->d); |
| 317 | fe_add(v, v, one); /* v = d y^2 + 1 */ |
| 318 | fe_sq(v3, v); |
| 319 | fe_mul(v3, v3, v); /* v^3 */ |
| 320 | fe_sq(x, v3); |
| 321 | fe_mul(x, x, v); |
| 322 | fe_mul(x, x, u); /* u v^7 */ |
| 323 | fe_pow(x, x, EXP_P58); |
| 324 | fe_mul(x, x, v3); |
| 325 | fe_mul(x, x, u); /* candidate root u v^3 (u v^7)^((p-5)/8) */ |
| 326 | fe_sq(vx2, x); |
| 327 | fe_mul(vx2, vx2, v); |
| 328 | fe_neg(nu, u); |
| 329 | int root_ok = fe_eq(vx2, u); |
| 330 | if (!root_ok && fe_eq(vx2, nu)) { |
| 331 | fe_mul(x, x, k->sqrtm1); |
| 332 | root_ok = 1; |
| 333 | } |
| 334 | if (!root_ok) return -1; /* GUARD:on-curve */ |
| 335 | if (fe_iszero(x) && sign) return -1; /* GUARD:x-zero-sign */ |
| 336 | if (fe_isneg(x) != sign) fe_neg(x, x); /* GUARD:x-sign */ |
| 337 | fe_copy(p->X, x); |
| 338 | fe_copy(p->Y, y); |
| 339 | fe_1(p->Z); |
| 340 | fe_mul(p->T, x, y); |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /* Public input. 1 iff [8]p is the identity (order of p divides 8). */ |
| 345 | static int ge_small_order(const ge *p) |
| 346 | { |
| 347 | ge q; |
| 348 | ge_dbl(&q, p); |
| 349 | ge_dbl(&q, &q); |
| 350 | ge_dbl(&q, &q); |
| 351 | return fe_iszero(q.X) && fe_eq(q.Y, q.Z); |
| 352 | } |
| 353 | |
| 354 | /* Public input: -x^2 + y^2 == 1 + d x^2 y^2 for the affine point. */ |
| 355 | static int ge_on_curve(const ge *p, const consts *k) |
| 356 | { |
| 357 | fe zi, x, y, x2, y2, l, r, one; |
| 358 | fe_invert(zi, p->Z); |
| 359 | fe_mul(x, p->X, zi); fe_mul(y, p->Y, zi); |
| 360 | fe_sq(x2, x); fe_sq(y2, y); |
| 361 | fe_sub(l, y2, x2); |
| 362 | fe_1(one); |
| 363 | fe_mul(r, x2, y2); fe_mul(r, r, k->d); fe_add(r, r, one); |
| 364 | return fe_eq(l, r); |
| 365 | } |
| 366 | |
| 367 | static void consts_init(consts *k) |
| 368 | { |
| 369 | /* d = -121665 / 121666, sqrt(-1) = 2^((p-1)/4), B = encoding 0x58 0x66.. */ |
| 370 | fe a, b, two; |
| 371 | fe_0(a); a[0] = 121665; |
| 372 | fe_0(b); b[0] = 121666; |
| 373 | fe_invert(b, b); |
| 374 | fe_mul(a, a, b); |
| 375 | fe_neg(k->d, a); |
| 376 | fe_add(k->d2, k->d, k->d); |
| 377 | fe_0(two); two[0] = 2; |
| 378 | fe_pow(k->sqrtm1, two, EXP_SQRTM1); |
| 379 | uint8_t enc[32]; |
| 380 | memset(enc, 0x66, 32); |
| 381 | enc[0] = 0x58; |
| 382 | (void)ge_frombytes(&k->B, enc, k); |
| 383 | } |
| 384 | |
| 385 | /* ---------------------------------------------------------------- scalars */ |
| 386 | |
| 387 | /* L = 2^252 + 27742317777372353535851937790883648493, little endian words. */ |
| 388 | static const uint32_t LW[8] = {0x5cf5d3ed, 0x5812631a, 0xa2f79cd6, 0x14def9de, |
| 389 | 0x00000000, 0x00000000, 0x00000000, 0x10000000}; |
| 390 | |
| 391 | static uint32_t ld32(const uint8_t *p) |
| 392 | { |
| 393 | return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; |
| 394 | } |
| 395 | |
| 396 | static void st32(uint8_t *p, uint32_t v) |
| 397 | { |
| 398 | p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); |
| 399 | } |
| 400 | |
| 401 | /* r = x mod L for a 512-bit x given as 16 words. Constant time: one shift |
| 402 | * and one masked subtract per input bit. */ |
| 403 | static void sc_reduce_words(uint8_t out[32], const uint32_t x[16]) |
| 404 | { |
| 405 | uint32_t r[8] = {0}, t[8]; |
| 406 | for (int i = 511; i >= 0; i--) { |
| 407 | uint32_t carry = (x[i >> 5] >> (i & 31)) & 1; |
| 408 | for (int j = 0; j < 8; j++) { |
| 409 | uint32_t nc = r[j] >> 31; |
| 410 | r[j] = (r[j] << 1) | carry; |
| 411 | carry = nc; |
| 412 | } |
| 413 | uint64_t borrow = 0; |
| 414 | for (int j = 0; j < 8; j++) { |
| 415 | uint64_t d = (uint64_t)r[j] - LW[j] - borrow; |
| 416 | t[j] = (uint32_t)d; |
| 417 | borrow = (d >> 63) & 1; |
| 418 | } |
| 419 | uint32_t m = (uint32_t)borrow - 1; /* all ones iff r >= L */ |
| 420 | AIENOS_SIG_BARRIER(m); |
| 421 | for (int j = 0; j < 8; j++) r[j] = (t[j] & m) | (r[j] & ~m); /* GUARD:sc-sub */ |
| 422 | } |
| 423 | for (int j = 0; j < 8; j++) st32(out + 4 * j, r[j]); |
| 424 | aienos_sig_wipe(r, sizeof r); |
| 425 | aienos_sig_wipe(t, sizeof t); |
| 426 | } |
| 427 | |
| 428 | void aienos_sig_sc_reduce(uint8_t out[32], const uint8_t in[64]) |
| 429 | { |
| 430 | uint32_t x[16]; |
| 431 | for (int i = 0; i < 16; i++) x[i] = ld32(in + 4 * i); |
| 432 | sc_reduce_words(out, x); |
| 433 | aienos_sig_wipe(x, sizeof x); |
| 434 | } |
| 435 | |
| 436 | void aienos_sig_sc_muladd(uint8_t out[32], const uint8_t a[32], const uint8_t b[32], |
| 437 | const uint8_t c[32]) |
| 438 | { |
| 439 | uint32_t aw[8], bw[8], p[16] = {0}; |
| 440 | for (int i = 0; i < 8; i++) { aw[i] = ld32(a + 4 * i); bw[i] = ld32(b + 4 * i); } |
| 441 | for (int i = 0; i < 8; i++) { |
| 442 | uint64_t carry = 0; |
| 443 | for (int j = 0; j < 8; j++) { |
| 444 | uint64_t t = (uint64_t)aw[i] * bw[j] + p[i + j] + carry; |
| 445 | p[i + j] = (uint32_t)t; |
| 446 | carry = t >> 32; |
| 447 | } |
| 448 | p[i + 8] = (uint32_t)carry; |
| 449 | } |
| 450 | uint64_t carry = 0; |
| 451 | for (int i = 0; i < 16; i++) { |
| 452 | uint64_t t = (uint64_t)p[i] + (i < 8 ? ld32(c + 4 * i) : 0) + carry; |
| 453 | p[i] = (uint32_t)t; |
| 454 | carry = t >> 32; |
| 455 | } |
| 456 | /* a, b, c < 2^256 with a*b + c < 2^512 for every caller here (a < L, |
| 457 | * b < 2^255); carry is 0. */ |
| 458 | sc_reduce_words(out, p); |
| 459 | aienos_sig_wipe(aw, sizeof aw); |
| 460 | aienos_sig_wipe(bw, sizeof bw); |
| 461 | aienos_sig_wipe(p, sizeof p); |
| 462 | } |
| 463 | |
| 464 | /* Public input: 1 iff s < L. */ |
| 465 | static int sc_canonical(const uint8_t s[32]) |
| 466 | { |
| 467 | for (int j = 7; j >= 0; j--) { |
| 468 | uint32_t w = ld32(s + 4 * j); |
| 469 | if (w < LW[j]) return 1; |
| 470 | if (w > LW[j]) return 0; |
| 471 | } |
| 472 | return 0; /* s == L */ |
| 473 | } |
| 474 | |
| 475 | /* ---------------------------------------------------------------- Ed25519 */ |
| 476 | |
| 477 | /* h = SHA-512(sk); a = clamp(h[0..32]); A = encode([a]B). */ |
| 478 | static void expand_key(uint8_t h[64], uint8_t pk[32], const uint8_t sk[32], const consts *k) |
| 479 | { |
| 480 | ge A; |
| 481 | aienos_sha512(sk, 32, h); |
| 482 | h[0] &= 248; /* GUARD:clamp-low */ |
| 483 | h[31] &= 127; /* GUARD:clamp-high */ |
| 484 | h[31] |= 64; /* GUARD:clamp-254 */ |
| 485 | ge_scalarmult(&A, &k->B, h, k); |
| 486 | ge_tobytes(pk, &A); |
| 487 | aienos_sig_wipe(&A, sizeof A); |
| 488 | } |
| 489 | |
| 490 | int aienos_ed25519_public_key(uint8_t pk[32], const uint8_t sk[32]) |
| 491 | { |
| 492 | if (!pk || !sk) return AIENOS_SIG_ERR_ARG; |
| 493 | consts k; |
| 494 | uint8_t h[64]; |
| 495 | consts_init(&k); |
| 496 | expand_key(h, pk, sk, &k); |
| 497 | aienos_sig_wipe(h, sizeof h); |
| 498 | return AIENOS_SIG_OK; |
| 499 | } |
| 500 | |
| 501 | int aienos_ed25519_sign(uint8_t sig[64], const uint8_t *msg, size_t msg_len, |
| 502 | const uint8_t sk[32]) |
| 503 | { |
| 504 | if (!sig || !sk || (!msg && msg_len)) return AIENOS_SIG_ERR_ARG; |
| 505 | consts k; |
| 506 | uint8_t h[64], pk[32], rh[64], r[32], Rb[32], kh[64], ks[32], S[32]; |
| 507 | ge R; |
| 508 | aienos_sha512_ctx c; |
| 509 | consts_init(&k); |
| 510 | expand_key(h, pk, sk, &k); |
| 511 | |
| 512 | aienos_sha512_init(&c); /* r = SHA-512(prefix || M) mod L */ |
| 513 | aienos_sha512_update(&c, h + 32, 32); |
| 514 | aienos_sha512_update(&c, msg, msg_len); |
| 515 | aienos_sha512_final(&c, rh); |
| 516 | aienos_sig_sc_reduce(r, rh); |
| 517 | ge_scalarmult(&R, &k.B, r, &k); |
| 518 | ge_tobytes(Rb, &R); |
| 519 | |
| 520 | aienos_sha512_init(&c); /* k = SHA-512(R || A || M) mod L */ |
| 521 | aienos_sha512_update(&c, Rb, 32); |
| 522 | aienos_sha512_update(&c, pk, 32); |
| 523 | aienos_sha512_update(&c, msg, msg_len); |
| 524 | aienos_sha512_final(&c, kh); |
| 525 | aienos_sig_sc_reduce(ks, kh); |
| 526 | |
| 527 | aienos_sig_sc_muladd(S, ks, h, r); /* S = (r + k a) mod L */ |
| 528 | memcpy(sig, Rb, 32); |
| 529 | memcpy(sig + 32, S, 32); |
| 530 | |
| 531 | aienos_sig_wipe(h, sizeof h); |
| 532 | aienos_sig_wipe(rh, sizeof rh); |
| 533 | aienos_sig_wipe(r, sizeof r); |
| 534 | aienos_sig_wipe(&R, sizeof R); |
| 535 | aienos_sig_wipe(S, sizeof S); |
| 536 | return AIENOS_SIG_OK; |
| 537 | } |
| 538 | |
| 539 | int aienos_ed25519_verify(const uint8_t sig[64], const uint8_t *msg, size_t msg_len, |
| 540 | const uint8_t pk[32]) |
| 541 | { |
| 542 | if (!sig || !pk || (!msg && msg_len)) return AIENOS_SIG_ERR_ARG; |
| 543 | consts k; |
| 544 | ge A, R, sB, kA, chk; |
| 545 | uint8_t kh[64], ks[32], enc[32]; |
| 546 | aienos_sha512_ctx c; |
| 547 | consts_init(&k); |
| 548 | if (!sc_canonical(sig + 32)) return AIENOS_SIG_ERR_INVALID; /* GUARD:s-range */ |
| 549 | if (ge_frombytes(&A, pk, &k)) return AIENOS_SIG_ERR_INVALID; |
| 550 | if (ge_small_order(&A)) return AIENOS_SIG_ERR_INVALID; /* GUARD:a-small-order */ |
| 551 | if (ge_frombytes(&R, sig, &k)) return AIENOS_SIG_ERR_INVALID; |
| 552 | if (ge_small_order(&R)) return AIENOS_SIG_ERR_INVALID; /* GUARD:r-small-order */ |
| 553 | |
| 554 | aienos_sha512_init(&c); |
| 555 | aienos_sha512_update(&c, sig, 32); |
| 556 | aienos_sha512_update(&c, pk, 32); |
| 557 | aienos_sha512_update(&c, msg, msg_len); |
| 558 | aienos_sha512_final(&c, kh); |
| 559 | aienos_sig_sc_reduce(ks, kh); |
| 560 | |
| 561 | ge_scalarmult(&sB, &k.B, sig + 32, &k); |
| 562 | ge_scalarmult(&kA, &A, ks, &k); |
| 563 | ge_neg(&kA, &kA); |
| 564 | ge_add(&chk, &sB, &kA, &k); /* [S]B - [k]A, must equal R */ |
| 565 | ge_tobytes(enc, &chk); |
| 566 | if (!aienos_sig_ct_equal(enc, sig, 32)) return AIENOS_SIG_ERR_INVALID; /* GUARD:r-check */ |
| 567 | return AIENOS_SIG_OK; |
| 568 | } |
| 569 | |
| 570 | /* ---------------------------------------------------------------- test hooks */ |
| 571 | |
| 572 | void aienos_sig_basemult(uint8_t out[32], const uint8_t s[32]) |
| 573 | { |
| 574 | consts k; |
| 575 | ge P; |
| 576 | consts_init(&k); |
| 577 | ge_scalarmult(&P, &k.B, s, &k); |
| 578 | ge_tobytes(out, &P); |
| 579 | } |
| 580 | |
| 581 | int aienos_sig_decode_check(uint8_t out[32], int *small, const uint8_t in[32]) |
| 582 | { |
| 583 | consts k; |
| 584 | ge P; |
| 585 | consts_init(&k); |
| 586 | if (ge_frombytes(&P, in, &k)) return -1; |
| 587 | if (!ge_on_curve(&P, &k)) return -2; |
| 588 | *small = ge_small_order(&P); |
| 589 | ge_tobytes(out, &P); |
| 590 | return 0; |
| 591 | } |