Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/net/tests/x25519_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / net / tests / x25519_test.c` 146 lines ยท 6938 bytes Raw Blame History 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 /* X25519 tests: RFC 7748 Section 5.2 vectors (both single vectors, the 18 * 1-iteration and 1,000-iteration checks; 1,000,000 with X25519_LONG=1) and 19 * the Section 6.1 Diffie-Hellman vector, copied verbatim from the RFC text 20 * (https://www.rfc-editor.org/rfc/rfc7748.txt, file sha256 21 * `279ca0ecc5e92e2962e27b846986aeb74729d9dd34bd4a04a362f80dcb596ad3`), plus 22 * small-order refusal, u top-bit masking, non-canonical u and DH symmetry. 23 * Prints X25519_RFC7748: PASS or FAIL. */ 24 #include 25 #include 26 #include 27 28 #include "../x25519.h" 29 30 static unsigned long checks, failures; 31 #define CHECK(c, ...) do { checks++; if (!(c)) { failures++; fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0) 32 33 static void hex(uint8_t *o, const char *h) 34 { 35 for (size_t i = 0; i < 32; i++) { unsigned v; sscanf(h + 2 * i, "%2x", &v); o[i] = (uint8_t)v; } 36 } 37 38 static int eqhex(const uint8_t *b, const char *h) 39 { 40 uint8_t t[32]; 41 hex(t, h); 42 return memcmp(b, t, 32) == 0; 43 } 44 45 /* RFC 7748 Section 5.2 */ 46 static const char *V[2][3] = { 47 {"`a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4`", 48 "`e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c`", 49 "`c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552`"}, 50 {"`4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d`", 51 "`e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493`", 52 "`95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957`"}, 53 }; 54 static const char *ITER1 = "`422c8e7a6227d7bca1350b3e2bb7279f7897b87bb6854b783c60e80311ae3079`"; 55 static const char *ITER1000 = "`684cf59ba83309552800ef566f2f4d3c1c3887c49360e3875f2eb94d99532c51`"; 56 static const char *ITER1M = "`7c3911e0ab2586fd864497297e575e6f3bc601c0883c30df5f4dd2d24f665424`"; 57 /* RFC 7748 Section 6.1 */ 58 static const char *ALICE_SK = "`77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a`"; 59 static const char *ALICE_PK = "`8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a`"; 60 static const char *BOB_SK = "`5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb`"; 61 static const char *BOB_PK = "`de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f`"; 62 static const char *SHARED = "`4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742`"; 63 64 int main(void) 65 { 66 uint8_t k[32], u[32], o[32], t[32]; 67 for (int i = 0; i < 2; i++) { 68 hex(k, V[i][0]); hex(u, V[i][1]); 69 CHECK(aienos_x25519(o, k, u) == 0, "vector %d status", i); 70 CHECK(eqhex(o, V[i][2]), "RFC 7748 5.2 vector %d", i); 71 /* aliasing out == u */ 72 memcpy(t, u, 32); 73 aienos_x25519(t, k, t); 74 CHECK(memcmp(t, o, 32) == 0, "aliased vector %d", i); 75 } 76 /* iterated: k = X25519(k, u), u = old k */ 77 memset(k, 0, 32); k[0] = 9; 78 memcpy(u, k, 32); 79 long n = getenv("X25519_LONG") ? `1000000` : 1000; 80 for (long it = 1; it <= n; it++) { 81 aienos_x25519(o, k, u); 82 memcpy(u, k, 32); 83 memcpy(k, o, 32); 84 if (it == 1) CHECK(eqhex(k, ITER1), "after 1 iteration"); 85 if (it == 1000) CHECK(eqhex(k, ITER1000), "after 1000 iterations"); 86 if (it == `1000000`) CHECK(eqhex(k, ITER1M), "after `1000000` iterations"); 87 } 88 /* Diffie-Hellman */ 89 uint8_t ask[32], bsk[32], apk[32], bpk[32], s1[32], s2[32]; 90 hex(ask, ALICE_SK); hex(bsk, BOB_SK); 91 aienos_x25519_base(apk, ask); 92 aienos_x25519_base(bpk, bsk); 93 CHECK(eqhex(apk, ALICE_PK), "alice pk"); 94 CHECK(eqhex(bpk, BOB_PK), "bob pk"); 95 CHECK(aienos_x25519(s1, ask, bpk) == 0 && eqhex(s1, SHARED), "alice shared"); 96 CHECK(aienos_x25519(s2, bsk, apk) == 0 && eqhex(s2, SHARED), "bob shared"); 97 /* small-order inputs give all-zero output and status -1: u = 0, 1, 98 * p-1 (order 2/4 points of the curve and twist), p, p+1 (non-canonical 99 * encodings of 0 and 1) and the two order-8 points. */ 100 static const char *SMALL[] = { 101 "`0000000000000000000000000000000000000000000000000000000000000000`", 102 "`0100000000000000000000000000000000000000000000000000000000000000`", 103 "`ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f`", 104 "`edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f`", 105 "`eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f`", 106 "`e0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800`", 107 "`5f9c95bca3508c24b1d0b1559c83ef5b04445cc4581c8e86d8224eddd09f1157`", 108 }; 109 for (size_t i = 0; i < sizeof SMALL / sizeof SMALL[0]; i++) { 110 hex(u, SMALL[i]); 111 int r = aienos_x25519(o, ask, u); 112 uint8_t acc = 0; 113 for (int j = 0; j < 32; j++) acc |= o[j]; 114 CHECK(r == -1 && acc == 0, "small-order input %zu refused (r=%d)", i, r); 115 } 116 /* top bit of u is ignored (RFC 7748 Section 5) */ 117 hex(u, V[0][1]); hex(k, V[0][0]); 118 aienos_x25519(t, k, u); 119 u[31] ^= 0x80; 120 aienos_x25519(o, k, u); 121 CHECK(memcmp(o, t, 32) == 0, "u top bit masked"); 122 /* non-canonical u = p + 9 must act as u = 9 */ 123 static const char *P9 = "`f6ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f`"; 124 hex(u, P9); 125 aienos_x25519(o, ask, u); 126 CHECK(eqhex(o, ALICE_PK), "u = p + 9 equals u = 9"); 127 /* DH symmetry on a deterministic family */ 128 uint8_t x[32], y[32]; 129 uint64_t s = 0x7748; 130 for (int it = 0; it < 64; it++) { 131 for (int j = 0; j < 32; j++) { s = s * 6364136223846793005ULL + 1442695040888963407ULL; x[j] = (uint8_t)(s >> 56); } 132 for (int j = 0; j < 32; j++) { s = s * 6364136223846793005ULL + 1442695040888963407ULL; y[j] = (uint8_t)(s >> 56); } 133 aienos_x25519_base(apk, x); 134 aienos_x25519_base(bpk, y); 135 aienos_x25519(s1, x, bpk); 136 aienos_x25519(s2, y, apk); 137 CHECK(memcmp(s1, s2, 32) == 0, "DH symmetry %d", it); 138 /* clamping: low 3 bits and bit 255 of the scalar do not matter */ 139 memcpy(t, x, 32); t[0] ^= 7; t[31] ^= 0x80; 140 aienos_x25519_base(o, t); 141 CHECK(memcmp(o, apk, 32) == 0, "clamp-equivalent scalar %d", it); 142 } 143 printf("X25519_RFC7748: %s (%lu checks, %lu failures, %ld iterations)\n", 144 failures ? "FAIL" : "PASS", checks, failures, n); 145 return failures ? 1 : 0; 146 }