interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / sig / tests / sig_test.c

533 lines · 23275 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/* Known-answer, negative and round-trip tests for native/sig.
18 *
19 * Vector sources (all hard-coded, none generated at build time):
20 * - RFC 8032 Section 7.1: TEST 1, 2, 3, 1024 and SHA(abc), verbatim
21 * (tests/rfc8032_vectors.h).
22 * - SHA-512: FIPS 180-4 examples ("abc", the 896-bit message, one million
23 * 'a'), the empty string, and padding-boundary lengths; digests checked
24 * against GNU coreutils sha512sum at authoring time.
25 * - Ed25519 small-order points: the eight points of order dividing 8.
26 * Round trips use a fixed-seed splitmix64 generator (reproducible).
27 * Last line is exactly AIENOS_SIG_NATIVE: PASS (exit 0) or ... FAIL (exit 1).
28 */
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include "../aienos_sig.h"
34#include "../sig_internal.h"
35#include "rfc8032_vectors.h"
36
37static int g_fail, g_checks;
38
39#define CHECK(cond, ...) \
40 do { \
41 g_checks++; \
42 if (!(cond)) { \
43 g_fail++; \
44 fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
45 fprintf(stderr, __VA_ARGS__); \
46 fprintf(stderr, "\n"); \
47 } \
48 } while (0)
49
50static int hexval(char c)
51{
52 if (c >= '0' && c <= '9') return c - '0';
53 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
54 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
55 return -1;
56}
57
58/* Decode hex into a freshly allocated buffer (at least 1 byte). */
59static uint8_t *unhex(const char *h, size_t *len)
60{
61 size_t n = strlen(h);
62 if (n % 2 != 0) { fprintf(stderr, "odd hex\n"); exit(1); }
63 uint8_t *b = malloc(n / 2 + 1);
64 if (!b) exit(1);
65 for (size_t i = 0; i < n / 2; i++) {
66 int hi = hexval(h[2 * i]), lo = hexval(h[2 * i + 1]);
67 if (hi < 0 || lo < 0) { fprintf(stderr, "bad hex\n"); exit(1); }
68 b[i] = (uint8_t)(hi << 4 | lo);
69 }
70 *len = n / 2;
71 return b;
72}
73
74static void unhex_fixed(const char *h, uint8_t *out, size_t want)
75{
76 size_t n;
77 uint8_t *b = unhex(h, &n);
78 if (n != want) { fprintf(stderr, "hex length %zu != %zu\n", n, want); exit(1); }
79 memcpy(out, b, n);
80 free(b);
81}
82
83static uint64_t g_rng = 0x41494e4f53534947ull; /* fixed seed */
84static uint64_t rnd(void)
85{
86 uint64_t z = (g_rng += 0x9e3779b97f4a7c15ull);
87 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
88 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
89 return z ^ (z >> 31);
90}
91static void rnd_bytes(uint8_t *p, size_t n)
92{
93 for (size_t i = 0; i < n; i++) p[i] = (uint8_t)rnd();
94}
95
96/* L, little endian. */
97static const uint8_t L_BYTES[32] = {
98 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
99 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10};
100
101/* a = b + c (256-bit little endian), returns carry. */
102static int add256(uint8_t a[32], const uint8_t b[32], const uint8_t c[32])
103{
104 unsigned carry = 0;
105 for (int i = 0; i < 32; i++) {
106 unsigned t = (unsigned)b[i] + c[i] + carry;
107 a[i] = (uint8_t)t;
108 carry = t >> 8;
109 }
110 return (int)carry;
111}
112
113/* ------------------------------------------------------------ SHA-512 */
114
115static void test_sha512(void)
116{
117 static const struct { const char *msg; size_t reps; const char *hex; } kat[] = {
118 {"", 1, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"},
119 {"abc", 1, "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"},
120 {"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
121 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"},
122 {"a", 1000000, "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"},
123 {"a", 111, "fa9121c7b32b9e01733d034cfc78cbf67f926c7ed83e82200ef86818196921760b4beff48404df811b953828274461673c68d04e297b0eb7b2b4d60fc6b566a2"},
124 {"a", 112, "c01d080efd492776a1c43bd23dd99d0a2e626d481e16782e75d54c2503b5dc32bd05f0f1ba33e568b88fd2d970929b719ecbb152f58f130a407c8830604b70ca"},
125 {"a", 127, "828613968b501dc00a97e08c73b118aa8876c26b8aac93df128502ab360f91bab50a51e088769a5c1eff4782ace147dce3642554199876374291f5d921629502"},
126 {"a", 128, "b73d1929aa615934e61a871596b3f3b33359f42b8175602e89f7e06e5f658a243667807ed300314b95cacdd579f3e33abdfbe351909519a846d465c59582f321"},
127 {"a", 239, "52c853cb8d907f3d4d6b889beb027985d7c273486d75f8baf26f80d24e90c74c6c3de3e22131582380a7d14d43f2941a31385439cd6ddc469f628015e50bf286"},
128 {"a", 240, "4c296d90c61052a62ffb1dd196f1b7b09373b1f93e71836baebf89690546b7595684dbe9467a8e484fa0d1094272b4344a7c24f5fee8daedeb0bf549c985ab5f"},
129 };
130 for (size_t v = 0; v < sizeof kat / sizeof kat[0]; v++) {
131 uint8_t want[64], got[64];
132 unhex_fixed(kat[v].hex, want, 64);
133 size_t ml = strlen(kat[v].msg);
134 aienos_sha512_ctx c;
135 aienos_sha512_init(&c);
136 for (size_t r = 0; r < kat[v].reps; r++)
137 aienos_sha512_update(&c, (const uint8_t *)kat[v].msg, ml);
138 aienos_sha512_final(&c, got);
139 CHECK(memcmp(got, want, 64) == 0, "sha512 kat %zu", v);
140 if (kat[v].reps == 1) {
141 aienos_sha512((const uint8_t *)kat[v].msg, ml, got);
142 CHECK(memcmp(got, want, 64) == 0, "sha512 one-shot kat %zu", v);
143 }
144 }
145 /* Every split point of a 300-byte message gives the one-shot digest. */
146 uint8_t m[300], one[64], got[64];
147 rnd_bytes(m, sizeof m);
148 aienos_sha512(m, sizeof m, one);
149 for (size_t s = 0; s <= sizeof m; s++) {
150 aienos_sha512_ctx c;
151 aienos_sha512_init(&c);
152 aienos_sha512_update(&c, m, s);
153 aienos_sha512_update(&c, NULL, 0);
154 aienos_sha512_update(&c, m + s, sizeof m - s);
155 aienos_sha512_final(&c, got);
156 CHECK(memcmp(got, one, 64) == 0, "sha512 split %zu", s);
157 }
158}
159
160/* ------------------------------------------------------------ RFC 8032 */
161
162static void test_rfc_vectors(void)
163{
164 for (size_t v = 0; v < sizeof RFC8032_71 / sizeof RFC8032_71[0]; v++) {
165 const struct rfc_vec *t = &RFC8032_71[v];
166 uint8_t sk[32], pk[32], sig[64], got_pk[32], got_sig[64], bad[64], badpk[32];
167 size_t ml;
168 unhex_fixed(t->sk, sk, 32);
169 unhex_fixed(t->pk, pk, 32);
170 unhex_fixed(t->sig, sig, 64);
171 uint8_t *msg = unhex(t->msg, &ml);
172
173 CHECK(aienos_ed25519_public_key(got_pk, sk) == AIENOS_SIG_OK, "TEST %s pk rc", t->name);
174 CHECK(memcmp(got_pk, pk, 32) == 0, "TEST %s public key", t->name);
175 CHECK(aienos_ed25519_sign(got_sig, msg, ml, sk) == AIENOS_SIG_OK, "TEST %s sign rc", t->name);
176 CHECK(memcmp(got_sig, sig, 64) == 0, "TEST %s signature", t->name);
177 CHECK(aienos_ed25519_verify(sig, msg, ml, pk) == AIENOS_SIG_OK, "TEST %s verify", t->name);
178
179 /* Byte flips in the signature (two different flips per byte). */
180 for (int i = 0; i < 64; i++)
181 for (int f = 0; f < 2; f++) {
182 memcpy(bad, sig, 64);
183 bad[i] ^= f ? 0x80 : 0x01;
184 CHECK(aienos_ed25519_verify(bad, msg, ml, pk) == AIENOS_SIG_ERR_INVALID,
185 "TEST %s sig flip byte %d", t->name, i);
186 }
187 /* Byte flips in the public key. */
188 for (int i = 0; i < 32; i++)
189 for (int f = 0; f < 2; f++) {
190 memcpy(badpk, pk, 32);
191 badpk[i] ^= f ? 0x80 : 0x01;
192 CHECK(aienos_ed25519_verify(sig, msg, ml, badpk) == AIENOS_SIG_ERR_INVALID,
193 "TEST %s pk flip byte %d", t->name, i);
194 }
195 /* Byte flips in the message (every byte; stride 7 on the long one). */
196 size_t stride = ml > 64 ? 7 : 1;
197 for (size_t i = 0; i < ml; i += stride) {
198 msg[i] ^= 0x01;
199 CHECK(aienos_ed25519_verify(sig, msg, ml, pk) == AIENOS_SIG_ERR_INVALID,
200 "TEST %s msg flip byte %zu", t->name, i);
201 msg[i] ^= 0x01;
202 }
203 if (ml > 0) {
204 CHECK(aienos_ed25519_verify(sig, msg, ml - 1, pk) == AIENOS_SIG_ERR_INVALID,
205 "TEST %s truncated msg", t->name);
206 msg[ml - 1] ^= 0x01;
207 CHECK(aienos_ed25519_verify(sig, msg, ml, pk) == AIENOS_SIG_ERR_INVALID,
208 "TEST %s last msg byte", t->name);
209 msg[ml - 1] ^= 0x01;
210 }
211 uint8_t *longer = malloc(ml + 1);
212 if (!longer) exit(1);
213 memcpy(longer, msg, ml);
214 longer[ml] = 0;
215 CHECK(aienos_ed25519_verify(sig, longer, ml + 1, pk) == AIENOS_SIG_ERR_INVALID,
216 "TEST %s extended msg", t->name);
217 free(longer);
218
219 /* Non-canonical S: S + L names the same scalar but must be refused. */
220 memcpy(bad, sig, 64);
221 CHECK(add256(bad + 32, sig + 32, L_BYTES) == 0, "S+L fits");
222 CHECK(aienos_ed25519_verify(bad, msg, ml, pk) == AIENOS_SIG_ERR_INVALID,
223 "TEST %s S + L accepted", t->name);
224 /* S = L exactly, and S with the top bit set. */
225 memcpy(bad, sig, 32);
226 memcpy(bad + 32, L_BYTES, 32);
227 CHECK(aienos_ed25519_verify(bad, msg, ml, pk) == AIENOS_SIG_ERR_INVALID, "S = L");
228 memcpy(bad, sig, 64);
229 bad[63] |= 0xe0;
230 CHECK(aienos_ed25519_verify(bad, msg, ml, pk) == AIENOS_SIG_ERR_INVALID, "S high bits");
231 free(msg);
232 }
233}
234
235/* ------------------------------------------------------------ scalars and base point */
236
237static void test_scalars(void)
238{
239 uint8_t out[32], in64[64], one[32] = {1}, zero[32] = {0}, enc[32];
240 /* [L]B is the identity, [1]B is the base point, [L+1]B is B. */
241 uint8_t ident[32] = {1};
242 uint8_t B[32];
243 memset(B, 0x66, 32);
244 B[0] = 0x58;
245 aienos_sig_basemult(enc, L_BYTES);
246 CHECK(memcmp(enc, ident, 32) == 0, "[L]B != identity");
247 aienos_sig_basemult(enc, one);
248 CHECK(memcmp(enc, B, 32) == 0, "[1]B != B");
249 uint8_t l1[32];
250 add256(l1, L_BYTES, one);
251 aienos_sig_basemult(enc, l1);
252 CHECK(memcmp(enc, B, 32) == 0, "[L+1]B != B");
253 aienos_sig_basemult(enc, zero);
254 CHECK(memcmp(enc, ident, 32) == 0, "[0]B != identity");
255
256 /* Reduction: L -> 0, L + 5 -> 5, L - 1 -> L - 1, 2^512 - 1 consistent with
257 * muladd, (L-1)^2 -> 1. */
258 memset(in64, 0, 64);
259 memcpy(in64, L_BYTES, 32);
260 aienos_sig_sc_reduce(out, in64);
261 CHECK(memcmp(out, zero, 32) == 0, "L mod L");
262 uint8_t five[32] = {5};
263 add256(in64, L_BYTES, five);
264 aienos_sig_sc_reduce(out, in64);
265 CHECK(memcmp(out, five, 32) == 0, "L+5 mod L");
266 uint8_t lm1[32];
267 memcpy(lm1, L_BYTES, 32);
268 lm1[0] -= 1;
269 memset(in64, 0, 64);
270 memcpy(in64, lm1, 32);
271 aienos_sig_sc_reduce(out, in64);
272 CHECK(memcmp(out, lm1, 32) == 0, "L-1 mod L");
273 aienos_sig_sc_muladd(out, lm1, lm1, zero);
274 CHECK(memcmp(out, one, 32) == 0, "(L-1)^2 mod L");
275 /* 2^512 - 1 mod L: check direct reduction against muladd construction. */
276 uint8_t all_ones[64], max256[32], m1[32], m2[32];
277 memset(all_ones, 0xff, 64);
278 memset(max256, 0xff, 32);
279 aienos_sig_sc_reduce(out, all_ones);
280 /* 2^512 - 1 = (2^256 - 1)^2 + 2 * (2^256 - 1) = max256 * max256 + max256 + max256 */
281 aienos_sig_sc_muladd(m1, max256, max256, max256);
282 aienos_sig_sc_muladd(m2, one, max256, m1);
283 CHECK(memcmp(out, m2, 32) == 0, "2^512 - 1 consistent with muladd");
284 uint8_t two[32] = {2}, three[32] = {3}, four[32] = {4}, ten[32] = {10};
285 aienos_sig_sc_muladd(out, two, three, four);
286 CHECK(memcmp(out, ten, 32) == 0, "2*3+4");
287 aienos_sig_sc_muladd(out, lm1, one, two);
288 CHECK(memcmp(out, one, 32) == 0, "(L-1)+2 mod L");
289 /* Random: [x mod L]B == [x]B for random 256-bit x (x < 2^256). */
290 for (int i = 0; i < 16; i++) {
291 uint8_t x[32], e1[32], e2[32];
292 rnd_bytes(x, 32);
293 memset(in64, 0, 64);
294 memcpy(in64, x, 32);
295 aienos_sig_sc_reduce(out, in64);
296 aienos_sig_basemult(e1, x);
297 aienos_sig_basemult(e2, out);
298 CHECK(memcmp(e1, e2, 32) == 0, "reduce consistent with group order %d", i);
299 }
300}
301
302/* ------------------------------------------------------------ point decoding */
303
304static const char *SMALL_ORDER[] = {
305 "0100000000000000000000000000000000000000000000000000000000000000", /* identity */
306 "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", /* order 2 */
307 "0000000000000000000000000000000000000000000000000000000000000000", /* order 4 */
308 "0000000000000000000000000000000000000000000000000000000000000080", /* order 4 */
309 "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05", /* order 8 */
310 "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85", /* order 8 */
311 "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a", /* order 8 */
312 "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa", /* order 8 */
313};
314
315static void test_decoding(void)
316{
317 uint8_t in[32], out[32];
318 int small = -1;
319 /* Every non-canonical y (p <= y < 2^255), with both sign bits. */
320 for (int k = 0; k < 19; k++)
321 for (int s = 0; s < 2; s++) {
322 memset(in, 0xff, 32);
323 in[0] = (uint8_t)(0xed + k);
324 in[31] = (uint8_t)(0x7f | (s << 7));
325 CHECK(aienos_sig_decode_check(out, &small, in) == -1, "non-canonical y=p+%d sign %d", k, s);
326 }
327 /* x = 0 with the sign bit set (y = 1 and y = -1). */
328 memset(in, 0, 32);
329 in[0] = 1;
330 in[31] = 0x80;
331 CHECK(aienos_sig_decode_check(out, &small, in) == -1, "x=0 sign=1 (y=1)");
332 memset(in, 0xff, 32);
333 in[0] = 0xec;
334 in[31] = 0xff;
335 CHECK(aienos_sig_decode_check(out, &small, in) == -1, "x=0 sign=1 (y=-1)");
336 /* Small y values: each either refused or a valid, re-encodable point. */
337 int refused = 0, accepted = 0;
338 for (int y = 2; y < 64; y++)
339 for (int s = 0; s < 2; s++) {
340 memset(in, 0, 32);
341 in[0] = (uint8_t)y;
342 in[31] = (uint8_t)(s << 7);
343 int rc = aienos_sig_decode_check(out, &small, in);
344 CHECK(rc != -2, "y=%d decoded off the curve", y);
345 if (rc == -1) refused++;
346 if (rc == 0) {
347 accepted++;
348 CHECK(memcmp(in, out, 32) == 0, "y=%d sign %d re-encoding", y, s);
349 }
350 }
351 CHECK(refused > 0 && accepted > 0, "on-curve split refused=%d accepted=%d", refused, accepted);
352 /* The eight small-order points decode and are flagged small. */
353 for (size_t i = 0; i < sizeof SMALL_ORDER / sizeof SMALL_ORDER[0]; i++) {
354 unhex_fixed(SMALL_ORDER[i], in, 32);
355 small = 0;
356 CHECK(aienos_sig_decode_check(out, &small, in) == 0, "small-order %zu decodes", i);
357 CHECK(small == 1, "small-order %zu flagged", i);
358 CHECK(memcmp(in, out, 32) == 0, "small-order %zu re-encodes", i);
359 }
360 /* The base point decodes and is not small. */
361 memset(in, 0x66, 32);
362 in[0] = 0x58;
363 CHECK(aienos_sig_decode_check(out, &small, in) == 0 && small == 0, "B decodes, not small");
364}
365
366/* ------------------------------------------------------------ verification policy */
367
368/* Builds S = r + k a for a chosen R encoding, as a signer would. */
369static void forge_with_r(uint8_t sig[64], const uint8_t Renc[32], const uint8_t r[32],
370 const uint8_t a[32], const uint8_t A[32], const uint8_t *m, size_t ml)
371{
372 uint8_t kh[64], ks[32];
373 aienos_sha512_ctx c;
374 aienos_sha512_init(&c);
375 aienos_sha512_update(&c, Renc, 32);
376 aienos_sha512_update(&c, A, 32);
377 aienos_sha512_update(&c, m, ml);
378 aienos_sha512_final(&c, kh);
379 aienos_sig_sc_reduce(ks, kh);
380 memcpy(sig, Renc, 32);
381 aienos_sig_sc_muladd(sig + 32, ks, a, r);
382}
383
384static void test_policy(void)
385{
386 static const uint8_t m[] = "owner key signed migration record";
387 size_t ml = sizeof m - 1;
388 uint8_t sk[32], pk[32], h[64], a[32], sig[64], Renc[32], zero[32] = {0};
389 uint8_t r[32] = {5};
390 rnd_bytes(sk, 32);
391 aienos_ed25519_public_key(pk, sk);
392 aienos_sha512(sk, 32, h);
393 memcpy(a, h, 32);
394 a[0] &= 248; a[31] &= 127; a[31] |= 64;
395
396 /* Positive control: R = [5]B, S = 5 + k a is an ordinary valid signature. */
397 aienos_sig_basemult(Renc, r);
398 forge_with_r(sig, Renc, r, a, pk, m, ml);
399 CHECK(aienos_ed25519_verify(sig, m, ml, pk) == AIENOS_SIG_OK, "control signature");
400
401 /* R = identity with S = k a satisfies [S]B = R + [k]A; must be refused. */
402 uint8_t ident[32] = {1};
403 forge_with_r(sig, ident, zero, a, pk, m, ml);
404 CHECK(aienos_ed25519_verify(sig, m, ml, pk) == AIENOS_SIG_ERR_INVALID, "small-order R accepted");
405 /* With an honest public key in the prime-order subgroup, [S]B - [k]A is always
406 * in the prime-order subgroup and can only equal small-order R if R is the
407 * identity point. For R != identity, [S]B - [k]A != R holds regardless of
408 * the small-order guard. We verify all 8 small-order R values are rejected. */
409 for (size_t i = 0; i < sizeof SMALL_ORDER / sizeof SMALL_ORDER[0]; i++) {
410 uint8_t Rs[32];
411 unhex_fixed(SMALL_ORDER[i], Rs, 32);
412 forge_with_r(sig, Rs, zero, a, pk, m, ml);
413 CHECK(aienos_ed25519_verify(sig, m, ml, pk) == AIENOS_SIG_ERR_INVALID, "small R %zu", i);
414 }
415
416 /* A small order: R = [r]B, S = r satisfies [S]B = R + [k]A when [k]A is
417 * the identity. We find a message nonce for each small-order A such that
418 * k mod 8 == 0, guaranteeing [k]A is the identity. Then the signature is
419 * mathematically valid under [S]B - [k]A == R, so it is refused ONLY
420 * by the small-order public-key check. */
421 uint8_t s8[64];
422 memcpy(s8, Renc, 32);
423 memcpy(s8 + 32, r, 32);
424 CHECK(aienos_ed25519_verify(s8, m, ml, ident) == AIENOS_SIG_ERR_INVALID, "identity A accepted");
425 for (size_t i = 0; i < sizeof SMALL_ORDER / sizeof SMALL_ORDER[0]; i++) {
426 uint8_t As[32];
427 unhex_fixed(SMALL_ORDER[i], As, 32);
428 uint8_t mi[sizeof m + 4];
429 memcpy(mi, m, ml);
430 int found = 0;
431 for (uint32_t ctr = 0; ctr < 1000; ctr++) {
432 mi[ml] = (uint8_t)ctr;
433 mi[ml + 1] = (uint8_t)(ctr >> 8);
434 mi[ml + 2] = (uint8_t)(ctr >> 16);
435 mi[ml + 3] = (uint8_t)(ctr >> 24);
436 uint8_t kh[64], ks[32];
437 aienos_sha512_ctx c;
438 aienos_sha512_init(&c);
439 aienos_sha512_update(&c, Renc, 32);
440 aienos_sha512_update(&c, As, 32);
441 aienos_sha512_update(&c, mi, ml + 4);
442 aienos_sha512_final(&c, kh);
443 aienos_sig_sc_reduce(ks, kh);
444 if ((ks[0] & 7) == 0) {
445 CHECK(aienos_ed25519_verify(s8, mi, ml + 4, As) == AIENOS_SIG_ERR_INVALID, "small A %zu", i);
446 found = 1;
447 break;
448 }
449 }
450 CHECK(found, "found k mod 8 == 0 for small A %zu", i);
451 }
452
453 /* Non-canonical R and A encodings (y >= p) are refused. */
454 aienos_ed25519_sign(sig, m, ml, sk);
455 for (int k = 0; k < 19; k++) {
456 uint8_t bad[64], nc[32];
457 memset(nc, 0xff, 32);
458 nc[0] = (uint8_t)(0xed + k);
459 nc[31] = 0x7f;
460 memcpy(bad, sig, 64);
461 memcpy(bad, nc, 32);
462 CHECK(aienos_ed25519_verify(bad, m, ml, pk) == AIENOS_SIG_ERR_INVALID, "non-canonical R %d", k);
463 CHECK(aienos_ed25519_verify(sig, m, ml, nc) == AIENOS_SIG_ERR_INVALID, "non-canonical A %d", k);
464 }
465
466 /* Argument checks. */
467 CHECK(aienos_ed25519_sign(NULL, m, ml, sk) == AIENOS_SIG_ERR_ARG, "sign NULL sig");
468 CHECK(aienos_ed25519_sign(sig, NULL, 1, sk) == AIENOS_SIG_ERR_ARG, "sign NULL msg");
469 CHECK(aienos_ed25519_sign(sig, m, ml, NULL) == AIENOS_SIG_ERR_ARG, "sign NULL sk");
470 CHECK(aienos_ed25519_verify(NULL, m, ml, pk) == AIENOS_SIG_ERR_ARG, "verify NULL sig");
471 CHECK(aienos_ed25519_verify(sig, NULL, 1, pk) == AIENOS_SIG_ERR_ARG, "verify NULL msg");
472 CHECK(aienos_ed25519_verify(sig, m, ml, NULL) == AIENOS_SIG_ERR_ARG, "verify NULL pk");
473 CHECK(aienos_ed25519_public_key(NULL, sk) == AIENOS_SIG_ERR_ARG, "pk NULL");
474 CHECK(aienos_ed25519_sign(sig, NULL, 0, sk) == AIENOS_SIG_OK, "sign empty NULL msg");
475 CHECK(aienos_ed25519_verify(sig, NULL, 0, pk) == AIENOS_SIG_OK, "verify empty NULL msg");
476}
477
478/* ------------------------------------------------------------ round trips */
479
480static void test_round_trips(int n)
481{
482 uint8_t sk[32], pk[32], sig[64], sig2[64], bad[64], m[300];
483 int ok = 0;
484 for (int i = 0; i < n; i++) {
485 rnd_bytes(sk, 32);
486 size_t ml = (size_t)(rnd() % sizeof m);
487 rnd_bytes(m, ml);
488 aienos_ed25519_public_key(pk, sk);
489 aienos_ed25519_sign(sig, m, ml, sk);
490 int good = aienos_ed25519_verify(sig, m, ml, pk) == AIENOS_SIG_OK;
491 CHECK(good, "round trip %d", i);
492 aienos_ed25519_sign(sig2, m, ml, sk);
493 CHECK(memcmp(sig, sig2, 64) == 0, "deterministic %d", i);
494 memcpy(bad, sig, 64);
495 bad[rnd() % 64] ^= (uint8_t)(1u << (rnd() % 8));
496 CHECK(aienos_ed25519_verify(bad, m, ml, pk) == AIENOS_SIG_ERR_INVALID, "random sig flip %d", i);
497 if (ml > 0) {
498 size_t j = (size_t)(rnd() % ml);
499 m[j] ^= 0x10;
500 CHECK(aienos_ed25519_verify(sig, m, ml, pk) == AIENOS_SIG_ERR_INVALID, "random msg flip %d", i);
501 m[j] ^= 0x10;
502 }
503 ok += good;
504 }
505 /* Signing in place: the signature buffer overlaps the message. */
506 uint8_t buf[100];
507 rnd_bytes(buf, sizeof buf);
508 rnd_bytes(sk, 32);
509 uint8_t copy[100];
510 memcpy(copy, buf, sizeof buf);
511 aienos_ed25519_public_key(pk, sk);
512 aienos_ed25519_sign(sig, copy, sizeof copy, sk);
513 aienos_ed25519_sign(buf, buf, sizeof buf, sk);
514 CHECK(memcmp(buf, sig, 64) == 0, "in-place sign");
515 printf("round trips: %d/%d verified\n", ok, n);
516}
517
518int main(void)
519{
520 test_sha512();
521 test_rfc_vectors();
522 test_scalars();
523 test_decoding();
524 test_policy();
525 test_round_trips(1000);
526 printf("checks: %d, failures: %d\n", g_checks, g_fail);
527 if (g_fail) {
528 printf("AIENOS_SIG_NATIVE: FAIL\n");
529 return 1;
530 }
531 printf("AIENOS_SIG_NATIVE: PASS\n");
532 return 0;
533}