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 / kernel / tests / test_unlock_req.c

102 lines · 4158 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/* test_unlock_req.c -- core/unlock_req.c: the card unlock request file of the Pi qualification
18 * image (CK_PI5_UNLOCK_CARD). Every malformed request is refused with a reason that never
19 * contains the secret; a well-formed one is handed out exactly once and then wiped. */
20#include <string.h>
21#include "ck_test.h"
22#include "unlock_req.h"
23
24_Static_assert(sizeof(struct ck_unlock_req) == CK_UNLOCK_REQ_SIZE, "request is exactly one sector");
25
26static struct ck_unlock_req good(void)
27{
28 struct ck_unlock_req q;
29 memset(&q, 0, sizeof q);
30 memcpy(q.magic, CK_UNLOCK_REQ_MAGIC, 8);
31 q.version = CK_UNLOCK_REQ_VERSION;
32 memcpy(q.tag, CK_UNLOCK_REQ_TAG, 16);
33 for (int i = 0; i < 32; i++)
34 q.secret[i] = (uint8_t)(0x30 + i * 3);
35 return q;
36}
37
38static int refused(const struct ck_unlock_req *q, const char *want)
39{
40 const char *why = 0;
41 int rc = ck_unlock_req_check(q, &why);
42 return rc != 0 && why && strstr(why, want) != 0;
43}
44
45int main(void)
46{
47 struct ck_unlock_req q = good();
48 const char *why = 0;
49 CHECK(ck_unlock_req_check(&q, &why) == 0 && why == 0);
50 CHECK(ck_unlock_req_check(0, &why) != 0);
51 CHECK(strlen(CK_UNLOCK_REQ_TAG) == 16);
52 CHECK(strstr(CK_UNLOCK_REQ_SOURCE, "QUALIFICATION ONLY, throwaway secret") != 0);
53
54 q = good(); q.magic[0] ^= 1;
55 CHECK(refused(&q, "magic"));
56 q = good(); q.version = 2;
57 CHECK(refused(&q, "version"));
58 q = good(); q.flags = 1;
59 CHECK(refused(&q, "flags"));
60 q = good(); q.tag[15] = 'X';
61 CHECK(refused(&q, "THROWAWAY")); /* a request that does not say throwaway is not accepted */
62 q = good(); memset(q.tag, 0, 16);
63 CHECK(refused(&q, "THROWAWAY"));
64 q = good(); memset(q.secret, 0, 32);
65 CHECK(refused(&q, "all zero"));
66 q = good(); memset(q.secret, 0x41, 32);
67 CHECK(refused(&q, "repeated byte"));
68 q = good(); q.secret[31] ^= 0x01; /* a one-bit change is still a valid (different) secret */
69 CHECK(ck_unlock_req_check(&q, &why) == 0);
70 q = good(); q.pad[0] = 1;
71 CHECK(refused(&q, "padding"));
72 q = good(); q.pad[sizeof q.pad - 1] = 1;
73 CHECK(refused(&q, "padding"));
74 /* the reason text never carries secret bytes */
75 q = good(); q.version = 7;
76 ck_unlock_req_check(&q, &why);
77 CHECK(why && !strstr(why, "0x") && strlen(why) < 64);
78
79 /* One-shot take. */
80 uint8_t out[32];
81 memset(&ck_unlock_req_boot, 0, sizeof ck_unlock_req_boot);
82 memset(out, 0xEE, sizeof out);
83 CHECK(ck_unlock_req_take(out) == -1); /* nothing stored: refused */
84 int z = 0;
85 for (int i = 0; i < 32; i++) z |= out[i];
86 CHECK(z == 0); /* and the caller's buffer is zeroed */
87 q = good();
88 ck_unlock_req_store(&q);
89 CHECK(ck_unlock_req_boot.state == CK_UNLOCK_REQ_FOUND);
90 CHECK(ck_unlock_req_take(out) == 0 && memcmp(out, q.secret, 32) == 0);
91 int left = 0;
92 for (int i = 0; i < 32; i++) left |= ck_unlock_req_boot.secret[i];
93 CHECK(left == 0 && ck_unlock_req_boot.state == CK_UNLOCK_REQ_NONE); /* the boot copy is wiped */
94 CHECK(ck_unlock_req_take(out) == -1); /* a second take gets nothing */
95 /* ABSENT and REFUSED states hand out nothing even if bytes were somehow left. */
96 ck_unlock_req_boot.state = CK_UNLOCK_REQ_REFUSED;
97 memset(ck_unlock_req_boot.secret, 0x55, 32);
98 CHECK(ck_unlock_req_take(out) == -1);
99 ck_unlock_req_boot.state = CK_UNLOCK_REQ_ABSENT;
100 CHECK(ck_unlock_req_take(out) == -1);
101 return ck_t_verdict("test_unlock_req");
102}