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 / m5 / tests / m5_migsig_test.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 | /* native/m5 owner-signed migration record tests (Ed25519 from native/sig). |
| 18 | * Host-only (hosted libc), not QEMU. Every key here is a TEST key derived |
| 19 | * from a public label; no real owner key is used or needed. |
| 20 | * Last line: "AIENOS_M5_MIGRATION_SIG: PASS" (exit 0) or "... FAIL" (exit 1). */ |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "../m5.h" |
| 26 | #include "../../argus/sha256.h" |
| 27 | #include "../../sig/aienos_sig.h" |
| 28 | |
| 29 | static int g_fail; |
| 30 | #define CHECK(c) do { if (!(c)) { printf(" check failed: %s (line %d)\n", #c, __LINE__); g_fail = 1; } } while (0) |
| 31 | |
| 32 | static int all_zero(const void *p, size_t n) { const uint8_t *b = p; for (size_t i = 0; i < n; i++) if (b[i]) return 0; return 1; } |
| 33 | static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } |
| 34 | static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } |
| 35 | |
| 36 | #define DOMAIN "AIENOS-M5-OWNER-MIGRATION-V1" |
| 37 | #define L M5_OWNER_MIG_LEN |
| 38 | |
| 39 | /* ---------- TEST keys (public labels, never an owner key) ---------- */ |
| 40 | static uint8_t TEST_SK[32], TEST_PK[32], TEST_SK2[32], TEST_PK2[32]; |
| 41 | static uint8_t K_VOL[32]; |
| 42 | static m5_subkeys SK; |
| 43 | static uint8_t IDS[3][32]; |
| 44 | static uint8_t MIG[M5_MIGRATION_LEN]; |
| 45 | |
| 46 | static void test_key(const char *label, uint8_t sk[32], uint8_t pk[32]) |
| 47 | { |
| 48 | uint8_t h[64]; |
| 49 | aienos_sha512((const uint8_t *)label, strlen(label), h); |
| 50 | memcpy(sk, h, 32); |
| 51 | aienos_ed25519_public_key(pk, sk); |
| 52 | } |
| 53 | |
| 54 | static m5_migration mig_fixture(void) |
| 55 | { |
| 56 | m5_migration m; |
| 57 | memset(&m, 0, sizeof m); |
| 58 | m.identity_class = M5_ID_PRODUCTION; |
| 59 | memset(m.agent_root_id, 0x32, 32); |
| 60 | memset(m.genesis_store_uuid, 0xA0, 16); |
| 61 | memset(m.source_store_uuid, 0xA0, 16); |
| 62 | memset(m.dest_store_uuid, 0xD0, 16); |
| 63 | m.source_store_generation = 9; |
| 64 | m.source_anchor_counter = 5; |
| 65 | m.migration_counter = 2; |
| 66 | m.owner_hierarchy_generation = 1; |
| 67 | memset(m.source_commit_digest, 0x5C, 32); |
| 68 | return m; |
| 69 | } |
| 70 | |
| 71 | static m5_owner_migration own_fixture(void) |
| 72 | { |
| 73 | m5_owner_migration o; |
| 74 | memset(&o, 0, sizeof o); |
| 75 | o.identity_class = M5_ID_PRODUCTION; |
| 76 | memset(o.agent_root_id, 0x32, 32); |
| 77 | memset(o.source_store_uuid, 0xA0, 16); |
| 78 | memset(o.dest_store_uuid, 0xD0, 16); |
| 79 | o.source_store_generation = 9; |
| 80 | o.store_format_version = 1; |
| 81 | o.envelope_count = 3; |
| 82 | m5_envelope_set_digest((const uint8_t (*)[32])IDS, 3, o.envelope_set_digest); |
| 83 | sha256_hash(MIG, sizeof MIG, o.migration_manifest_digest); |
| 84 | o.migration_counter = 2; |
| 85 | o.owner_hierarchy_generation = 1; |
| 86 | return o; |
| 87 | } |
| 88 | |
| 89 | static void fixtures(void) |
| 90 | { |
| 91 | test_key("AIENOS TEST-ONLY owner key 1 (not an owner key)", TEST_SK, TEST_PK); |
| 92 | test_key("AIENOS TEST-ONLY owner key 2 (not an owner key)", TEST_SK2, TEST_PK2); |
| 93 | for (int i = 0; i < 32; i++) K_VOL[i] = (uint8_t)(0x11 + i); |
| 94 | m5_derive_subkeys(K_VOL, M5_ID_PRODUCTION, 1, &SK); |
| 95 | for (int i = 0; i < 3; i++) memset(IDS[i], 0x10 * (i + 1), 32); /* ascending */ |
| 96 | m5_migration m = mig_fixture(); |
| 97 | m5_migration_seal(&m, SK.k_root_auth, MIG); |
| 98 | } |
| 99 | |
| 100 | #define VERIFY(buf, len, pk, e, last, o) m5_owner_migration_verify(buf, len, pk, M5_ID_PRODUCTION, e, last, o) |
| 101 | |
| 102 | /* Sign an arbitrary 208-byte body directly (bypasses sign_body's format check). */ |
| 103 | static void raw_sign(uint8_t rec[L], const uint8_t sk[32]) |
| 104 | { |
| 105 | uint8_t msg[sizeof(DOMAIN) + M5_OWNER_MIG_BODY_LEN]; |
| 106 | memcpy(msg, DOMAIN, sizeof(DOMAIN)); |
| 107 | memcpy(msg + sizeof(DOMAIN), rec, M5_OWNER_MIG_BODY_LEN); |
| 108 | aienos_ed25519_sign(rec + M5_OWNER_MIG_BODY_LEN, msg, sizeof msg, sk); |
| 109 | } |
| 110 | |
| 111 | /* ---------- tests ---------- */ |
| 112 | static void t_roundtrip(void) |
| 113 | { |
| 114 | m5_owner_migration m = own_fixture(), o; |
| 115 | uint8_t r[L]; |
| 116 | CHECK(m5_owner_migration_sign(&m, TEST_SK, r) == M5_OK); |
| 117 | CHECK(VERIFY(r, L, TEST_PK, &m, 1, &o) == M5_OK); |
| 118 | CHECK(o.migration_counter == 2 && o.store_format_version == 1 && o.envelope_count == 3); |
| 119 | uint8_t kid[32]; |
| 120 | sha256_hash(TEST_PK, 32, kid); |
| 121 | CHECK(memcmp(o.owner_key_id, kid, 32) == 0); |
| 122 | CHECK(memcmp(o.envelope_set_digest, m.envelope_set_digest, 32) == 0); |
| 123 | /* deterministic (Ed25519 has no random nonce) */ |
| 124 | uint8_t r2[L]; |
| 125 | m5_owner_migration_sign(&m, TEST_SK, r2); |
| 126 | CHECK(memcmp(r, r2, L) == 0); |
| 127 | } |
| 128 | |
| 129 | static void t_layout_pinned(void) |
| 130 | { |
| 131 | m5_owner_migration m = own_fixture(); |
| 132 | uint8_t r[L], kid[32]; |
| 133 | m5_owner_migration_sign(&m, TEST_SK, r); |
| 134 | sha256_hash(TEST_PK, 32, kid); |
| 135 | CHECK(memcmp(r, "AIENOMG1", 8) == 0 && r[8] == 1 && r[9] == 0 && r[10] == 0 && r[11] == 0); |
| 136 | CHECK(r[12] == M5_ID_PRODUCTION && all_zero(r + 13, 3)); |
| 137 | CHECK(memcmp(r + 16, kid, 32) == 0); |
| 138 | CHECK(memcmp(r + 48, m.agent_root_id, 32) == 0); |
| 139 | CHECK(memcmp(r + 80, m.source_store_uuid, 16) == 0 && memcmp(r + 96, m.dest_store_uuid, 16) == 0); |
| 140 | CHECK(r[112] == 9 && all_zero(r + 113, 7)); |
| 141 | CHECK(r[120] == 1 && all_zero(r + 121, 3) && r[124] == 3 && all_zero(r + 125, 3)); |
| 142 | CHECK(memcmp(r + 128, m.envelope_set_digest, 32) == 0); |
| 143 | CHECK(memcmp(r + 160, m.migration_manifest_digest, 32) == 0); |
| 144 | CHECK(r[192] == 2 && all_zero(r + 193, 7) && r[200] == 1 && all_zero(r + 201, 7)); |
| 145 | /* the signature is plain Ed25519 over domain || body: checked with native/sig directly */ |
| 146 | uint8_t msg[sizeof(DOMAIN) + M5_OWNER_MIG_BODY_LEN]; |
| 147 | memcpy(msg, DOMAIN, sizeof(DOMAIN)); |
| 148 | memcpy(msg + sizeof(DOMAIN), r, M5_OWNER_MIG_BODY_LEN); |
| 149 | CHECK(aienos_ed25519_verify(r + 208, msg, sizeof msg, TEST_PK) == AIENOS_SIG_OK); |
| 150 | CHECK(aienos_ed25519_verify(r + 208, r, M5_OWNER_MIG_BODY_LEN, TEST_PK) != AIENOS_SIG_OK); /* domain matters */ |
| 151 | } |
| 152 | |
| 153 | static void t_wrong_key_refused(void) |
| 154 | { |
| 155 | m5_owner_migration m = own_fixture(), o; |
| 156 | uint8_t r[L]; |
| 157 | m5_owner_migration_sign(&m, TEST_SK, r); |
| 158 | CHECK(VERIFY(r, L, TEST_PK2, &m, 1, &o) == M5_ERR_AUTH); |
| 159 | CHECK(all_zero(&o, sizeof o)); |
| 160 | /* key id swapped to the verifier's key, signature still by key 1 */ |
| 161 | uint8_t kid2[32]; |
| 162 | sha256_hash(TEST_PK2, 32, kid2); |
| 163 | memcpy(r + 16, kid2, 32); |
| 164 | CHECK(VERIFY(r, L, TEST_PK2, &m, 1, &o) == M5_ERR_AUTH); |
| 165 | /* signed by key 2 over a body that names key 1 */ |
| 166 | uint8_t b[L]; |
| 167 | m5_owner_migration_body(&m, TEST_PK, b); |
| 168 | raw_sign(b, TEST_SK2); |
| 169 | CHECK(VERIFY(b, L, TEST_PK, &m, 1, &o) == M5_ERR_AUTH); |
| 170 | /* validly signed by key 2, verified with key 2, but the body names key 1 */ |
| 171 | CHECK(VERIFY(b, L, TEST_PK2, &m, 1, &o) == M5_ERR_AUTH); |
| 172 | CHECK(all_zero(&o, sizeof o)); |
| 173 | } |
| 174 | |
| 175 | static void t_unsigned_refused(void) |
| 176 | { |
| 177 | m5_owner_migration m = own_fixture(), o; |
| 178 | uint8_t b[L]; |
| 179 | CHECK(m5_owner_migration_body(&m, TEST_PK, b) == M5_OK); |
| 180 | CHECK(all_zero(b + 208, 64)); |
| 181 | CHECK(VERIFY(b, L, TEST_PK, &m, 1, &o) == M5_ERR_AUTH); |
| 182 | CHECK(all_zero(&o, sizeof o)); |
| 183 | } |
| 184 | |
| 185 | static void t_every_byte_flip_refused(void) |
| 186 | { |
| 187 | m5_owner_migration m = own_fixture(), o; |
| 188 | uint8_t r[L]; |
| 189 | m5_owner_migration_sign(&m, TEST_SK, r); |
| 190 | static const uint8_t flips[] = { 0x01, 0x80 }; |
| 191 | for (size_t i = 0; i < L; i++) |
| 192 | for (size_t f = 0; f < 2; f++) { |
| 193 | r[i] ^= flips[f]; |
| 194 | int rc = VERIFY(r, L, TEST_PK, &m, 1, &o); |
| 195 | if (rc >= 0 || !all_zero(&o, sizeof o)) { printf(" byte %zu flip %02x accepted\n", i, flips[f]); g_fail = 1; } |
| 196 | r[i] ^= flips[f]; |
| 197 | } |
| 198 | CHECK(VERIFY(r, L, TEST_PK, &m, 1, &o) == M5_OK); |
| 199 | } |
| 200 | |
| 201 | /* A field changed and correctly re-signed by the owner must still be refused |
| 202 | * when it does not match what the verifier expects. */ |
| 203 | static void t_field_mismatch_refused(void) |
| 204 | { |
| 205 | m5_owner_migration m = own_fixture(), o; |
| 206 | uint8_t r[L]; |
| 207 | #define RESIGNED(stmt, want) do { m5_owner_migration x = m; stmt; m5_owner_migration_sign(&x, TEST_SK, r); \ |
| 208 | CHECK(VERIFY(r, L, TEST_PK, &m, 1, &o) == (want)); CHECK(all_zero(&o, sizeof o)); } while (0) |
| 209 | RESIGNED(x.source_store_uuid[3] ^= 1, M5_ERR_BINDING); |
| 210 | RESIGNED(x.dest_store_uuid[15] ^= 1, M5_ERR_BINDING); |
| 211 | RESIGNED(x.agent_root_id[31] ^= 1, M5_ERR_BINDING); |
| 212 | RESIGNED(x.source_store_generation = 8, M5_ERR_BINDING); |
| 213 | RESIGNED(x.source_store_generation = 10, M5_ERR_BINDING); |
| 214 | RESIGNED(x.store_format_version = 2, M5_ERR_BINDING); |
| 215 | RESIGNED(x.envelope_count = 2, M5_ERR_BINDING); |
| 216 | RESIGNED(x.envelope_set_digest[0] ^= 1, M5_ERR_BINDING); |
| 217 | RESIGNED(x.migration_manifest_digest[0] ^= 1, M5_ERR_BINDING); |
| 218 | RESIGNED(x.owner_hierarchy_generation = 2, M5_ERR_BINDING); |
| 219 | RESIGNED(x.identity_class = M5_ID_TEST, M5_ERR_IDENTITY); |
| 220 | /* migrating a store onto itself, even when the verifier expects it */ |
| 221 | m5_owner_migration s = m; |
| 222 | memcpy(s.dest_store_uuid, s.source_store_uuid, 16); |
| 223 | m5_owner_migration_sign(&s, TEST_SK, r); |
| 224 | CHECK(VERIFY(r, L, TEST_PK, &s, 1, &o) == M5_ERR_BINDING); |
| 225 | #undef RESIGNED |
| 226 | } |
| 227 | |
| 228 | static void t_format_refused_even_when_signed(void) |
| 229 | { |
| 230 | m5_owner_migration m = own_fixture(), o; |
| 231 | uint8_t b[L]; |
| 232 | #define BADFMT(stmt) do { m5_owner_migration_body(&m, TEST_PK, b); stmt; raw_sign(b, TEST_SK); \ |
| 233 | CHECK(VERIFY(b, L, TEST_PK, &m, 1, &o) == M5_ERR_FORMAT); CHECK(all_zero(&o, sizeof o)); } while (0) |
| 234 | BADFMT(b[7] = '2'); |
| 235 | BADFMT(put16(b + 8, 2)); |
| 236 | BADFMT(put16(b + 8, 0)); |
| 237 | BADFMT(put16(b + 10, 1)); |
| 238 | BADFMT(b[13] = 1); |
| 239 | BADFMT(b[15] = 0x80); |
| 240 | BADFMT(b[12] = 0); |
| 241 | BADFMT(b[12] = 3); |
| 242 | #undef BADFMT |
| 243 | } |
| 244 | |
| 245 | static void t_replay_refused(void) |
| 246 | { |
| 247 | m5_owner_migration m = own_fixture(), o; |
| 248 | uint8_t r[L]; |
| 249 | m5_owner_migration_sign(&m, TEST_SK, r); /* counter 2 */ |
| 250 | CHECK(VERIFY(r, L, TEST_PK, &m, 0, &o) == M5_OK); |
| 251 | CHECK(VERIFY(r, L, TEST_PK, &m, 1, &o) == M5_OK); |
| 252 | CHECK(VERIFY(r, L, TEST_PK, &m, 2, &o) == M5_ERR_REPLAY); |
| 253 | CHECK(VERIFY(r, L, TEST_PK, &m, 3, &o) == M5_ERR_REPLAY); |
| 254 | CHECK(VERIFY(r, L, TEST_PK, &m, UINT64_MAX, &o) == M5_ERR_REPLAY); |
| 255 | CHECK(all_zero(&o, sizeof o)); |
| 256 | /* an older, validly signed record after a newer one was accepted */ |
| 257 | m5_owner_migration n = m; n.migration_counter = 7; |
| 258 | uint8_t rn[L]; |
| 259 | m5_owner_migration_sign(&n, TEST_SK, rn); |
| 260 | CHECK(VERIFY(rn, L, TEST_PK, &m, 2, &o) == M5_OK); |
| 261 | CHECK(VERIFY(r, L, TEST_PK, &m, o.migration_counter, &o) == M5_ERR_REPLAY); |
| 262 | /* counter 0 can never be accepted */ |
| 263 | m5_owner_migration z = m; z.migration_counter = 0; |
| 264 | m5_owner_migration_sign(&z, TEST_SK, r); |
| 265 | CHECK(VERIFY(r, L, TEST_PK, &m, 0, &o) == M5_ERR_REPLAY); |
| 266 | /* top of the range */ |
| 267 | z.migration_counter = UINT64_MAX; |
| 268 | m5_owner_migration_sign(&z, TEST_SK, r); |
| 269 | CHECK(VERIFY(r, L, TEST_PK, &m, UINT64_MAX - 1, &o) == M5_OK); |
| 270 | } |
| 271 | |
| 272 | static void t_truncated_and_extended_refused(void) |
| 273 | { |
| 274 | m5_owner_migration m = own_fixture(), o; |
| 275 | uint8_t r[2 * L]; |
| 276 | m5_owner_migration_sign(&m, TEST_SK, r); |
| 277 | memset(r + L, 0, L); |
| 278 | for (size_t n = 0; n < L; n++) { |
| 279 | /* exact-size heap copy so ASan sees any read past the given length */ |
| 280 | uint8_t *t = malloc(n ? n : 1); |
| 281 | memcpy(t, r, n); |
| 282 | if (VERIFY(t, n, TEST_PK, &m, 1, &o) != M5_ERR_BOUNDS) { printf(" length %zu not refused\n", n); g_fail = 1; } |
| 283 | free(t); |
| 284 | } |
| 285 | CHECK(VERIFY(r, L + 1, TEST_PK, &m, 1, &o) == M5_ERR_BOUNDS); |
| 286 | CHECK(VERIFY(r, 2 * L, TEST_PK, &m, 1, &o) == M5_ERR_BOUNDS); |
| 287 | CHECK(all_zero(&o, sizeof o)); |
| 288 | CHECK(VERIFY(r, L, TEST_PK, &m, 1, &o) == M5_OK); |
| 289 | } |
| 290 | |
| 291 | static void t_sign_body_refusals(void) |
| 292 | { |
| 293 | m5_owner_migration m = own_fixture(), o; |
| 294 | uint8_t b[L]; |
| 295 | m5_owner_migration_body(&m, TEST_PK, b); |
| 296 | CHECK(m5_owner_migration_sign_body(b, TEST_SK2) == M5_ERR_BINDING); /* key does not match the body */ |
| 297 | CHECK(all_zero(b + 208, 64)); |
| 298 | CHECK(m5_owner_migration_sign_body(b, TEST_SK) == M5_OK); |
| 299 | CHECK(m5_owner_migration_sign_body(b, TEST_SK) == M5_ERR_FORMAT); /* already signed */ |
| 300 | CHECK(VERIFY(b, L, TEST_PK, &m, 1, &o) == M5_OK); |
| 301 | m5_owner_migration_body(&m, TEST_PK, b); |
| 302 | b[0] = 'X'; |
| 303 | CHECK(m5_owner_migration_sign_body(b, TEST_SK) == M5_ERR_FORMAT); |
| 304 | m5_owner_migration bad = m; bad.identity_class = 0; |
| 305 | CHECK(m5_owner_migration_body(&bad, TEST_PK, b) == M5_ERR_ARG); |
| 306 | CHECK(m5_owner_migration_sign(&bad, TEST_SK, b) == M5_ERR_ARG && all_zero(b, L)); |
| 307 | CHECK(m5_owner_migration_verify(NULL, L, TEST_PK, M5_ID_PRODUCTION, &m, 0, &o) == M5_ERR_ARG); |
| 308 | CHECK(m5_owner_migration_verify(b, L, TEST_PK, M5_ID_PRODUCTION, NULL, 0, &o) == M5_ERR_ARG); |
| 309 | } |
| 310 | |
| 311 | static void t_test_class_refused_in_production(void) |
| 312 | { |
| 313 | m5_owner_migration m = own_fixture(), o; |
| 314 | m5_owner_migration t = m; t.identity_class = M5_ID_TEST; |
| 315 | uint8_t r[L]; |
| 316 | m5_owner_migration_sign(&t, TEST_SK, r); |
| 317 | CHECK(VERIFY(r, L, TEST_PK, &m, 1, &o) == M5_ERR_IDENTITY); |
| 318 | CHECK(m5_owner_migration_verify(r, L, TEST_PK, M5_ID_TEST, &t, 1, &o) == M5_OK); |
| 319 | CHECK(m5_owner_migration_verify(r, L, TEST_PK, M5_ID_TEST, &m, 1, &o) == M5_ERR_IDENTITY); |
| 320 | } |
| 321 | |
| 322 | static void t_envelope_set_digest(void) |
| 323 | { |
| 324 | uint8_t d[32], want[32], ids[3][32]; |
| 325 | /* known answer computed independently: SHA-256(domain\0 || n u32 || ids) */ |
| 326 | uint8_t buf[sizeof("AIENOS-M5-ENVSET-V1") + 4 + 96]; |
| 327 | memcpy(buf, "AIENOS-M5-ENVSET-V1", sizeof("AIENOS-M5-ENVSET-V1")); |
| 328 | put32(buf + sizeof("AIENOS-M5-ENVSET-V1"), 3); |
| 329 | memcpy(buf + sizeof("AIENOS-M5-ENVSET-V1") + 4, IDS, 96); |
| 330 | sha256_hash(buf, sizeof buf, want); |
| 331 | CHECK(m5_envelope_set_digest((const uint8_t (*)[32])IDS, 3, d) == M5_OK && memcmp(d, want, 32) == 0); |
| 332 | /* empty set is allowed and differs */ |
| 333 | CHECK(m5_envelope_set_digest(NULL, 0, d) == M5_OK && memcmp(d, want, 32) != 0); |
| 334 | /* a subset differs */ |
| 335 | CHECK(m5_envelope_set_digest((const uint8_t (*)[32])IDS, 2, d) == M5_OK && memcmp(d, want, 32) != 0); |
| 336 | /* non-canonical: unsorted or duplicate */ |
| 337 | memcpy(ids, IDS, sizeof ids); |
| 338 | memcpy(ids[0], IDS[1], 32); memcpy(ids[1], IDS[0], 32); |
| 339 | CHECK(m5_envelope_set_digest((const uint8_t (*)[32])ids, 3, d) == M5_ERR_FORMAT); |
| 340 | memcpy(ids, IDS, sizeof ids); |
| 341 | memcpy(ids[2], ids[1], 32); |
| 342 | CHECK(m5_envelope_set_digest((const uint8_t (*)[32])ids, 3, d) == M5_ERR_FORMAT); |
| 343 | CHECK(m5_envelope_set_digest(NULL, 1, d) == M5_ERR_ARG); |
| 344 | /* one byte of one id changes the digest */ |
| 345 | memcpy(ids, IDS, sizeof ids); |
| 346 | ids[2][31] ^= 1; |
| 347 | CHECK(m5_envelope_set_digest((const uint8_t (*)[32])ids, 3, d) == M5_OK && memcmp(d, want, 32) != 0); |
| 348 | } |
| 349 | |
| 350 | /* ---------- full authorization: MAC'd manifest + owner signature ---------- */ |
| 351 | #define AUTH_OWNER(mig, own, last) m5_migration_authorize_owner(mig, M5_MIGRATION_LEN, SK.k_root_auth, own, L, \ |
| 352 | TEST_PK, M5_ID_PRODUCTION, &em, &eo, last, &om, &oo) |
| 353 | |
| 354 | static void t_authorize_owner(void) |
| 355 | { |
| 356 | m5_migration em = mig_fixture(), om; |
| 357 | m5_owner_migration eo = own_fixture(), oo; |
| 358 | uint8_t own[L], mig[M5_MIGRATION_LEN]; |
| 359 | memcpy(mig, MIG, sizeof mig); |
| 360 | m5_owner_migration_sign(&eo, TEST_SK, own); |
| 361 | CHECK(AUTH_OWNER(mig, own, 1) == M5_OK); |
| 362 | CHECK(om.migration_counter == 2 && oo.migration_counter == 2 && oo.envelope_count == 3); |
| 363 | /* replay of both records */ |
| 364 | CHECK(AUTH_OWNER(mig, own, 2) == M5_ERR_REPLAY); |
| 365 | CHECK(all_zero(&om, sizeof om) && all_zero(&oo, sizeof oo)); |
| 366 | /* owner record from the wrong key */ |
| 367 | uint8_t own2[L]; |
| 368 | m5_owner_migration_sign(&eo, TEST_SK2, own2); |
| 369 | CHECK(AUTH_OWNER(mig, own2, 1) == M5_ERR_AUTH); |
| 370 | CHECK(all_zero(&om, sizeof om) && all_zero(&oo, sizeof oo)); |
| 371 | /* manifest under the wrong store key */ |
| 372 | uint8_t migbad[M5_MIGRATION_LEN]; |
| 373 | m5_migration_seal(&em, SK.k_artifact, migbad); |
| 374 | CHECK(AUTH_OWNER(migbad, own, 1) == M5_ERR_AUTH); |
| 375 | /* owner signed a different (validly MAC'd) manifest */ |
| 376 | m5_migration other = em; other.source_anchor_counter = 6; |
| 377 | uint8_t mig_other[M5_MIGRATION_LEN]; |
| 378 | m5_migration_seal(&other, SK.k_root_auth, mig_other); |
| 379 | em.source_anchor_counter = 6; |
| 380 | CHECK(AUTH_OWNER(mig_other, own, 1) == M5_ERR_BINDING); |
| 381 | em.source_anchor_counter = 5; |
| 382 | /* owner counter differs from the manifest counter */ |
| 383 | m5_owner_migration c = eo; c.migration_counter = 3; |
| 384 | m5_owner_migration_sign(&c, TEST_SK, own2); |
| 385 | CHECK(AUTH_OWNER(mig, own2, 1) == M5_ERR_BINDING); |
| 386 | CHECK(all_zero(&om, sizeof om) && all_zero(&oo, sizeof oo)); |
| 387 | /* owner record disagrees with the manifest on destination */ |
| 388 | m5_owner_migration dd = eo; dd.dest_store_uuid[0] ^= 1; |
| 389 | m5_owner_migration_sign(&dd, TEST_SK, own2); |
| 390 | CHECK(AUTH_OWNER(mig, own2, 1) == M5_ERR_BINDING); |
| 391 | /* migrated envelope set differs from what the verifier holds */ |
| 392 | eo.envelope_set_digest[5] ^= 1; |
| 393 | CHECK(AUTH_OWNER(mig, own, 1) == M5_ERR_BINDING); |
| 394 | eo.envelope_set_digest[5] ^= 1; |
| 395 | /* truncated owner record */ |
| 396 | CHECK(m5_migration_authorize_owner(mig, M5_MIGRATION_LEN, SK.k_root_auth, own, L - 1, TEST_PK, |
| 397 | M5_ID_PRODUCTION, &em, &eo, 1, &om, &oo) == M5_ERR_BOUNDS); |
| 398 | CHECK(AUTH_OWNER(mig, own, 1) == M5_OK); |
| 399 | } |
| 400 | |
| 401 | static const struct { const char *name; void (*fn)(void); } TESTS[] = { |
| 402 | #define T(f) { #f, f } |
| 403 | T(t_roundtrip), T(t_layout_pinned), T(t_wrong_key_refused), T(t_unsigned_refused), |
| 404 | T(t_every_byte_flip_refused), T(t_field_mismatch_refused), T(t_format_refused_even_when_signed), |
| 405 | T(t_replay_refused), T(t_truncated_and_extended_refused), T(t_sign_body_refusals), |
| 406 | T(t_test_class_refused_in_production), T(t_envelope_set_digest), T(t_authorize_owner), |
| 407 | #undef T |
| 408 | }; |
| 409 | |
| 410 | int main(void) |
| 411 | { |
| 412 | int failed = 0; |
| 413 | fixtures(); |
| 414 | for (size_t i = 0; i < sizeof TESTS / sizeof TESTS[0]; i++) { |
| 415 | g_fail = 0; |
| 416 | TESTS[i].fn(); |
| 417 | printf("%s %s\n", g_fail ? "FAIL" : "ok ", TESTS[i].name); |
| 418 | if (g_fail) failed++; |
| 419 | } |
| 420 | printf("%zu tests, %d failed\n", sizeof TESTS / sizeof TESTS[0], failed); |
| 421 | printf("AIENOS_M5_MIGRATION_SIG: %s\n", failed ? "FAIL" : "PASS"); |
| 422 | return failed ? 1 : 0; |
| 423 | } |