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 / crypto / gcm_siv.c

180 lines · 6841 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/* AEAD_AES_256_GCM_SIV per RFC 8452, port of crates/aienos-crypto/src/aes_gcm_siv.rs.
18 *
19 * Secret intermediates (derived keys, key schedules, CTR keystream, padded
20 * tails, POLYVAL state) are wiped before every return, including failures.
21 * Lines tagged GUARD:<name> are removed one at a time by `make mutants`.
22 */
23#include <string.h>
24
25#include "aienos_crypto.h"
26#include "crypto_internal.h"
27
28void aienos_gcmsiv_derive_keys(const uint8_t key[32], const uint8_t nonce[12],
29 uint8_t auth_key[16], uint8_t enc_key[32])
30{
31 aienos_aes256_key kc;
32 uint8_t block[16];
33 aienos_aes256_init(&kc, key);
34 for (uint32_t i = 0; i < 6; i++) {
35 block[0] = (uint8_t)i;
36 block[1] = block[2] = block[3] = 0;
37 memcpy(block + 4, nonce, 12);
38 aienos_aes256_encrypt_block(&kc, block);
39 if (i < 2)
40 memcpy(auth_key + 8 * i, block, 8);
41 else
42 memcpy(enc_key + 8 * (i - 2), block, 8);
43 }
44 aienos_wipe(block, sizeof block);
45 aienos_aes256_wipe(&kc);
46}
47
48static void absorb_padded(aienos_polyval *pv, const uint8_t *d, size_t len)
49{
50 size_t full = len - len % 16;
51 for (size_t off = 0; off < full; off += 16)
52 aienos_polyval_update_block(pv, d + off);
53 if (len % 16 != 0) {
54 uint8_t b[16] = {0};
55 memcpy(b, d + full, len % 16);
56 aienos_polyval_update_block(pv, b);
57 aienos_wipe(b, sizeof b);
58 }
59}
60
61static void compute_tag(const uint8_t auth_key[16], const aienos_aes256_key *enc,
62 const uint8_t nonce[12], const uint8_t *aad, size_t aad_len,
63 const uint8_t *pt, size_t pt_len, uint8_t tag[16])
64{
65 aienos_polyval pv;
66 uint8_t lenblk[16];
67 uint64_t abits = (uint64_t)aad_len * 8u, pbits = (uint64_t)pt_len * 8u;
68
69 aienos_polyval_init(&pv, auth_key);
70 absorb_padded(&pv, aad, aad_len);
71 absorb_padded(&pv, pt, pt_len);
72 for (int i = 0; i < 8; i++) {
73 lenblk[i] = (uint8_t)(abits >> (8 * i));
74 lenblk[8 + i] = (uint8_t)(pbits >> (8 * i));
75 }
76 aienos_polyval_update_block(&pv, lenblk);
77 aienos_polyval_final(&pv, tag); /* wipes pv */
78
79 for (int i = 0; i < 12; i++)
80 tag[i] ^= nonce[i];
81 tag[15] &= 0x7f; /* GUARD:tag-msb */
82 aienos_aes256_encrypt_block(enc, tag);
83}
84
85/* AES-CTR with a 32-bit little-endian counter in bytes 0..3 that wraps. */
86static void apply_ctr(const aienos_aes256_key *enc, const uint8_t tag[16], uint8_t *buf, size_t len)
87{
88 uint8_t ctr[16], ks[16];
89 memcpy(ctr, tag, 16);
90 ctr[15] |= 0x80; /* GUARD:ctr-msb */
91 for (size_t off = 0; off < len; off += 16) {
92 memcpy(ks, ctr, 16);
93 aienos_aes256_encrypt_block(enc, ks);
94 uint32_t c = (uint32_t)ctr[0] | ((uint32_t)ctr[1] << 8) | ((uint32_t)ctr[2] << 16) |
95 ((uint32_t)ctr[3] << 24);
96 c += 1u; /* wraps mod 2^32 per RFC 8452 */
97 ctr[0] = (uint8_t)c;
98 ctr[1] = (uint8_t)(c >> 8);
99 ctr[2] = (uint8_t)(c >> 16);
100 ctr[3] = (uint8_t)(c >> 24);
101 size_t todo = len - off < 16 ? len - off : 16;
102 for (size_t i = 0; i < todo; i++)
103 buf[off + i] ^= ks[i];
104 }
105 aienos_wipe(ks, sizeof ks);
106 aienos_wipe(ctr, sizeof ctr);
107}
108
109int aienos_gcmsiv_seal(const uint8_t key[32], const uint8_t nonce[12],
110 const uint8_t *aad, size_t aad_len,
111 const uint8_t *pt, size_t pt_len,
112 uint8_t *out, size_t out_len)
113{
114 uint8_t auth_key[16], enc_key[32], tag[16];
115 aienos_aes256_key enc;
116
117 if (key == NULL || nonce == NULL || out == NULL ||
118 (aad == NULL && aad_len != 0) || (pt == NULL && pt_len != 0))
119 return AIENOS_CRYPTO_ERR_LENGTH;
120 if ((uint64_t)pt_len > AIENOS_GCMSIV_MAX_PT_LEN) return AIENOS_CRYPTO_ERR_LENGTH; /* GUARD:pt-max */
121 if ((uint64_t)aad_len > AIENOS_GCMSIV_MAX_AAD_LEN) return AIENOS_CRYPTO_ERR_LENGTH; /* GUARD:aad-max-seal */
122 if (out_len < AIENOS_GCMSIV_TAG_LEN || out_len - AIENOS_GCMSIV_TAG_LEN != pt_len)
123 return AIENOS_CRYPTO_ERR_LENGTH;
124
125 aienos_gcmsiv_derive_keys(key, nonce, auth_key, enc_key);
126 aienos_aes256_init(&enc, enc_key);
127 compute_tag(auth_key, &enc, nonce, aad, aad_len, pt, pt_len, tag);
128
129 if (out != pt && pt_len != 0)
130 memmove(out, pt, pt_len);
131 apply_ctr(&enc, tag, out, pt_len);
132 memcpy(out + pt_len, tag, 16);
133
134 aienos_wipe(auth_key, sizeof auth_key);
135 aienos_wipe(enc_key, sizeof enc_key);
136 aienos_aes256_wipe(&enc);
137 return AIENOS_CRYPTO_OK;
138}
139
140int aienos_gcmsiv_open(const uint8_t key[32], const uint8_t nonce[12],
141 const uint8_t *aad, size_t aad_len,
142 const uint8_t *in, size_t in_len,
143 uint8_t *out, size_t out_len)
144{
145 uint8_t auth_key[16], enc_key[32], got[16], want[16];
146 aienos_aes256_key enc;
147 size_t ct_len;
148 int ok;
149
150 if (key == NULL || nonce == NULL || in == NULL || (aad == NULL && aad_len != 0) ||
151 (out == NULL && out_len != 0))
152 return AIENOS_CRYPTO_ERR_LENGTH;
153 if (in_len < AIENOS_GCMSIV_TAG_LEN)
154 return AIENOS_CRYPTO_ERR_LENGTH;
155 if ((uint64_t)in_len > AIENOS_GCMSIV_MAX_CT_LEN) return AIENOS_CRYPTO_ERR_LENGTH; /* GUARD:ct-max */
156 if ((uint64_t)aad_len > AIENOS_GCMSIV_MAX_AAD_LEN) return AIENOS_CRYPTO_ERR_LENGTH; /* GUARD:aad-max-open */
157 ct_len = in_len - AIENOS_GCMSIV_TAG_LEN;
158 if (out_len != ct_len)
159 return AIENOS_CRYPTO_ERR_LENGTH;
160
161 memcpy(got, in + ct_len, 16); /* before any in-place write */
162 aienos_gcmsiv_derive_keys(key, nonce, auth_key, enc_key);
163 aienos_aes256_init(&enc, enc_key);
164
165 if (out != in && ct_len != 0)
166 memmove(out, in, ct_len);
167 apply_ctr(&enc, got, out, ct_len);
168 compute_tag(auth_key, &enc, nonce, aad, aad_len, out, ct_len, want);
169
170 ok = 1;
171 ok &= aienos_ct_equal(got, want, 16); /* GUARD:tag-check */
172 if (!ok)
173 aienos_wipe(out, out_len); /* GUARD:wipe-on-fail */
174
175 aienos_wipe(auth_key, sizeof auth_key);
176 aienos_wipe(enc_key, sizeof enc_key);
177 aienos_wipe(want, sizeof want);
178 aienos_aes256_wipe(&enc);
179 return ok ? AIENOS_CRYPTO_OK : AIENOS_CRYPTO_ERR_AUTH;
180}