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 / net / x25519.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 | /* AIENOS X25519 (RFC 7748). See x25519.h. |
| 18 | * |
| 19 | * Field GF(2^255 - 19) in 16 signed 64-bit limbs of 16 bits each (the |
| 20 | * TweetNaCl representation), rewritten here with every shift of a possibly |
| 21 | * negative value replaced by a multiply so the code has no undefined |
| 22 | * behaviour. Lines tagged GUARD:<name> are removed one at a time by |
| 23 | * `make mutants`; the tests must catch each removal. |
| 24 | */ |
| 25 | #include "x25519.h" |
| 26 | |
| 27 | #include <string.h> |
| 28 | |
| 29 | typedef int64_t fe[16]; |
| 30 | |
| 31 | static const fe K121665 = {0xDB41, 1}; |
| 32 | |
| 33 | static void carry(fe o) |
| 34 | { |
| 35 | for (int i = 0; i < 16; i++) { |
| 36 | o[i] += (int64_t)1 << 16; |
| 37 | int64_t c = o[i] >> 16; /* arithmetic shift (gcc/clang); value-preserving floor */ |
| 38 | if (i < 15) o[i + 1] += c - 1; |
| 39 | else o[0] += 38 * (c - 1); /* GUARD:x-fold */ |
| 40 | o[i] -= c * 65536; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /* Swap p and q when b = 1, keep them when b = 0, without a branch. */ |
| 45 | static void cswap(fe p, fe q, int64_t b) |
| 46 | { |
| 47 | int64_t m = -b; /* 0 or all ones */ |
| 48 | for (int i = 0; i < 16; i++) { |
| 49 | int64_t t = m & (p[i] ^ q[i]); |
| 50 | p[i] ^= t; |
| 51 | q[i] ^= t; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static void add(fe o, const fe a, const fe b) { for (int i = 0; i < 16; i++) o[i] = a[i] + b[i]; } |
| 56 | static void sub(fe o, const fe a, const fe b) { for (int i = 0; i < 16; i++) o[i] = a[i] - b[i]; } |
| 57 | |
| 58 | static void mul(fe o, const fe a, const fe b) |
| 59 | { |
| 60 | int64_t t[31]; |
| 61 | for (int i = 0; i < 31; i++) t[i] = 0; |
| 62 | for (int i = 0; i < 16; i++) |
| 63 | for (int j = 0; j < 16; j++) t[i + j] += a[i] * b[j]; |
| 64 | for (int i = 0; i < 15; i++) t[i] += 38 * t[i + 16]; |
| 65 | for (int i = 0; i < 16; i++) o[i] = t[i]; |
| 66 | carry(o); |
| 67 | carry(o); |
| 68 | } |
| 69 | |
| 70 | static void sqr(fe o, const fe a) { mul(o, a, a); } |
| 71 | |
| 72 | /* o = i^(p-2) = 1/i. The exponent is public, so its branches are too. */ |
| 73 | static void invert(fe o, const fe in) |
| 74 | { |
| 75 | fe c; |
| 76 | memcpy(c, in, sizeof c); |
| 77 | for (int a = 253; a >= 0; a--) { |
| 78 | sqr(c, c); |
| 79 | if (a != 2 && a != 4) mul(c, c, in); |
| 80 | } |
| 81 | memcpy(o, c, sizeof c); |
| 82 | } |
| 83 | |
| 84 | static void unpack(fe o, const uint8_t n[32]) |
| 85 | { |
| 86 | for (int i = 0; i < 16; i++) o[i] = n[2 * i] + ((int64_t)n[2 * i + 1] << 8); |
| 87 | o[15] &= 0x7fff; /* GUARD:u-mask */ |
| 88 | } |
| 89 | |
| 90 | /* Fully reduce mod p (two conditional subtractions) and encode. */ |
| 91 | static void pack(uint8_t o[32], const fe n) |
| 92 | { |
| 93 | fe m, t; |
| 94 | memcpy(t, n, sizeof t); |
| 95 | carry(t); |
| 96 | carry(t); |
| 97 | carry(t); |
| 98 | for (int j = 0; j < 2; j++) { |
| 99 | m[0] = t[0] - 0xffed; |
| 100 | for (int i = 1; i < 15; i++) { |
| 101 | m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1); |
| 102 | m[i - 1] &= 0xffff; |
| 103 | } |
| 104 | m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1); |
| 105 | int64_t b = (m[15] >> 16) & 1; |
| 106 | m[14] &= 0xffff; |
| 107 | cswap(t, m, 1 - b); /* GUARD:x-reduce */ |
| 108 | } |
| 109 | for (int i = 0; i < 16; i++) { |
| 110 | o[2 * i] = (uint8_t)(t[i] & 0xff); |
| 111 | o[2 * i + 1] = (uint8_t)((t[i] >> 8) & 0xff); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static void wipe(void *p, size_t n) |
| 116 | { |
| 117 | volatile uint8_t *v = (volatile uint8_t *)p; |
| 118 | while (n--) *v++ = 0; |
| 119 | } |
| 120 | |
| 121 | int aienos_x25519(uint8_t out[32], const uint8_t scalar[32], const uint8_t u[32]) |
| 122 | { |
| 123 | uint8_t z[32]; |
| 124 | fe x, a, b, c, d, e, f; |
| 125 | memcpy(z, scalar, 32); |
| 126 | z[31] &= 127; /* bit 255 is never read by the ladder; kept for clarity */ |
| 127 | z[31] |= 64; /* GUARD:clamp-254 */ |
| 128 | z[0] &= 248; /* GUARD:clamp-low */ |
| 129 | unpack(x, u); |
| 130 | for (int i = 0; i < 16; i++) { b[i] = x[i]; a[i] = c[i] = d[i] = 0; } |
| 131 | a[0] = d[0] = 1; |
| 132 | /* Montgomery ladder over bits 254..0 (RFC 7748 Section 5). */ |
| 133 | for (int i = 254; i >= 0; i--) { |
| 134 | int64_t r = (z[i >> 3] >> (i & 7)) & 1; |
| 135 | cswap(a, b, r); |
| 136 | cswap(c, d, r); |
| 137 | add(e, a, c); |
| 138 | sub(a, a, c); |
| 139 | add(c, b, d); |
| 140 | sub(b, b, d); |
| 141 | sqr(d, e); |
| 142 | sqr(f, a); |
| 143 | mul(a, c, a); |
| 144 | mul(c, b, e); |
| 145 | add(e, a, c); |
| 146 | sub(a, a, c); |
| 147 | sqr(b, a); |
| 148 | sub(c, d, f); |
| 149 | mul(a, c, K121665); |
| 150 | add(a, a, d); |
| 151 | mul(c, c, a); |
| 152 | mul(a, d, f); |
| 153 | mul(d, b, x); |
| 154 | sqr(b, e); |
| 155 | cswap(a, b, r); |
| 156 | cswap(c, d, r); |
| 157 | } |
| 158 | invert(c, c); |
| 159 | mul(a, a, c); |
| 160 | pack(out, a); |
| 161 | uint8_t acc = 0; |
| 162 | for (int i = 0; i < 32; i++) acc |= out[i]; |
| 163 | wipe(z, sizeof z); |
| 164 | wipe(a, sizeof a); wipe(b, sizeof b); wipe(c, sizeof c); |
| 165 | wipe(d, sizeof d); wipe(e, sizeof e); wipe(f, sizeof f); wipe(x, sizeof x); |
| 166 | /* all-zero output <=> small-order input (RFC 7748 Section 6.1) */ |
| 167 | return ((acc | (uint8_t)-acc) >> 7) ? 0 : -1; |
| 168 | } |
| 169 | |
| 170 | void aienos_x25519_base(uint8_t out[32], const uint8_t scalar[32]) |
| 171 | { |
| 172 | static const uint8_t nine[32] = {9}; |
| 173 | (void)aienos_x25519(out, scalar, nine); |
| 174 | } |