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 / kernel / svc / setup_store.c
| 1 | /* setup_store.c -- the setup wizard's records on disk; see setup_store.h. */ |
| 2 | #include "setup_store.h" |
| 3 | #include "sha256.h" |
| 4 | |
| 5 | static uint8_t g_unit[CK_ALLEN_MEM_UNIT], g_back[CK_ALLEN_MEM_UNIT]; |
| 6 | static const uint8_t magic[8] = {'A', 'I', 'E', 'N', 'S', 'E', 'T', '1'}; |
| 7 | |
| 8 | static void put32(uint8_t *p, uint32_t v) |
| 9 | { |
| 10 | for (int i = 0; i < 4; i++) |
| 11 | p[i] = (uint8_t)(v >> (8 * i)); |
| 12 | } |
| 13 | static uint32_t get32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; } |
| 14 | static void put64(uint8_t *p, uint64_t v) |
| 15 | { |
| 16 | put32(p, (uint32_t)v); |
| 17 | put32(p + 4, (uint32_t)(v >> 32)); |
| 18 | } |
| 19 | static uint64_t get64(const uint8_t *p) { return (uint64_t)get32(p) | (uint64_t)get32(p + 4) << 32; } |
| 20 | |
| 21 | static int geom(const disk_dev *d, uint32_t *per) |
| 22 | { |
| 23 | if (!d || !d->block_size || CK_ALLEN_MEM_UNIT % d->block_size) |
| 24 | return CK_ALLEN_MEM_EGEOM; |
| 25 | *per = CK_ALLEN_MEM_UNIT / d->block_size; |
| 26 | if (d->block_count < (uint64_t)(CK_SETUP_FIRST + CK_SETUP_UNITS) * *per) |
| 27 | return CK_ALLEN_MEM_EGEOM; |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static void fingerprint(const uint8_t *unit, uint32_t len, uint8_t out[32]) |
| 32 | { |
| 33 | sha256_ctx h; |
| 34 | sha256_init(&h); |
| 35 | sha256_update(&h, unit, 32); |
| 36 | sha256_update(&h, unit + 64, len); |
| 37 | sha256_final(&h, out); |
| 38 | } |
| 39 | |
| 40 | /* 1 when the unit is a valid copy */ |
| 41 | static int valid(const uint8_t *u, uint64_t *gen, uint32_t *len) |
| 42 | { |
| 43 | for (int i = 0; i < 8; i++) |
| 44 | if (u[i] != magic[i]) |
| 45 | return 0; |
| 46 | *len = get32(u + 12); |
| 47 | *gen = get64(u + 16); |
| 48 | if (get32(u + 8) != 1 || *len > CK_SETUP_TEXT || !*gen || get64(u + 24)) |
| 49 | return 0; |
| 50 | for (uint32_t i = 0; i < *len; i++) |
| 51 | if ((u[64 + i] < ' ' || u[64 + i] > '~') && u[64 + i] != '\n') |
| 52 | return 0; |
| 53 | /* the fingerprint covers bytes 0..31 and the text only: the rest of the unit is zero by |
| 54 | * the format (ck_setup_save writes it so), and a copy that is not is spoiled */ |
| 55 | for (uint32_t i = 64 + *len; i < CK_ALLEN_MEM_UNIT; i++) |
| 56 | if (u[i]) |
| 57 | return 0; |
| 58 | uint8_t want[32]; |
| 59 | fingerprint(u, *len, want); |
| 60 | uint8_t diff = 0; |
| 61 | for (int i = 0; i < 32; i++) |
| 62 | diff |= (uint8_t)(want[i] ^ u[32 + i]); |
| 63 | return diff == 0; |
| 64 | } |
| 65 | |
| 66 | int ck_setup_load(ck_setup_store *st, const disk_dev *part, char *text, unsigned cap, unsigned *n) |
| 67 | { |
| 68 | uint32_t per = 0; |
| 69 | st->dev = part, st->gen = 0, st->newest = 0; |
| 70 | *n = 0; |
| 71 | if (cap) |
| 72 | text[0] = 0; |
| 73 | int rc = geom(part, &per); |
| 74 | if (rc) |
| 75 | return rc; |
| 76 | for (unsigned c = 0; c < CK_SETUP_UNITS; c++) { |
| 77 | uint64_t g; |
| 78 | uint32_t len; |
| 79 | if (disk_read(part, (uint64_t)(CK_SETUP_FIRST + c) * per, per, g_unit) != DISK_OK) |
| 80 | return CK_ALLEN_MEM_EIO; |
| 81 | if (!valid(g_unit, &g, &len) || g <= st->gen) |
| 82 | continue; |
| 83 | st->gen = g, st->newest = c; |
| 84 | uint32_t m = cap && len >= cap ? cap - 1 : len; |
| 85 | for (uint32_t i = 0; cap && i < m; i++) |
| 86 | text[i] = (char)g_unit[64 + i]; |
| 87 | if (cap) |
| 88 | text[m] = 0; |
| 89 | *n = m; |
| 90 | } |
| 91 | return st->gen ? 0 : 1; |
| 92 | } |
| 93 | |
| 94 | int ck_setup_save(ck_setup_store *st, const char *text, unsigned n) |
| 95 | { |
| 96 | uint32_t per = 0; |
| 97 | int rc = geom(st->dev, &per); |
| 98 | if (rc) |
| 99 | return rc; |
| 100 | if (n > CK_SETUP_TEXT) |
| 101 | return CK_ALLEN_MEM_EGEOM; |
| 102 | unsigned target = st->gen ? 1u - st->newest : 0u; |
| 103 | for (unsigned i = 0; i < CK_ALLEN_MEM_UNIT; i++) |
| 104 | g_unit[i] = 0; |
| 105 | for (int i = 0; i < 8; i++) |
| 106 | g_unit[i] = magic[i]; |
| 107 | put32(g_unit + 8, 1); |
| 108 | put32(g_unit + 12, n); |
| 109 | put64(g_unit + 16, st->gen + 1); |
| 110 | for (unsigned i = 0; i < n; i++) |
| 111 | g_unit[64 + i] = (uint8_t)text[i]; |
| 112 | fingerprint(g_unit, n, g_unit + 32); |
| 113 | uint64_t lba = (uint64_t)(CK_SETUP_FIRST + target) * per; |
| 114 | if (disk_write(st->dev, lba, per, g_unit) != DISK_OK || disk_flush(st->dev) != DISK_OK) |
| 115 | return CK_ALLEN_MEM_EIO; |
| 116 | if (disk_read(st->dev, lba, per, g_back) != DISK_OK) |
| 117 | return CK_ALLEN_MEM_EIO; |
| 118 | for (unsigned i = 0; i < CK_ALLEN_MEM_UNIT; i++) |
| 119 | if (g_unit[i] != g_back[i]) |
| 120 | return CK_ALLEN_MEM_EVERIFY; |
| 121 | st->gen++; |
| 122 | st->newest = target; |
| 123 | return 0; |
| 124 | } |