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 / crypto / aienos_crypto.h
| 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 | /* AIENOS in-house crypto primitives, C port of crates/aienos-crypto. |
| 18 | * |
| 19 | * AES-256 (FIPS 197), POLYVAL and AEAD_AES_256_GCM_SIV (RFC 8452), and |
| 20 | * HMAC-SHA-256 over the in-house SHA-256 in native/argus/sha256.c. |
| 21 | * No heap, no clock, no I/O. Secret scratch is wiped before returning. |
| 22 | */ |
| 23 | #ifndef AIENOS_CRYPTO_H |
| 24 | #define AIENOS_CRYPTO_H |
| 25 | |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | #include "../argus/sha256.h" |
| 30 | |
| 31 | #define AIENOS_AES256_KEY_LEN 32 |
| 32 | #define AIENOS_AES_BLOCK_LEN 16 |
| 33 | #define AIENOS_POLYVAL_BLOCK_LEN 16 |
| 34 | #define AIENOS_GCMSIV_KEY_LEN 32 |
| 35 | #define AIENOS_GCMSIV_NONCE_LEN 12 |
| 36 | #define AIENOS_GCMSIV_TAG_LEN 16 |
| 37 | |
| 38 | /* RFC 8452 Section 6: P_MAX = A_MAX = 2^36 bytes, C_MAX = 2^36 + 16. */ |
| 39 | #define AIENOS_GCMSIV_MAX_PT_LEN (UINT64_C(1) << 36) |
| 40 | #define AIENOS_GCMSIV_MAX_AAD_LEN (UINT64_C(1) << 36) |
| 41 | #define AIENOS_GCMSIV_MAX_CT_LEN (AIENOS_GCMSIV_MAX_PT_LEN + AIENOS_GCMSIV_TAG_LEN) |
| 42 | |
| 43 | /* Return codes. */ |
| 44 | #define AIENOS_CRYPTO_OK 0 |
| 45 | #define AIENOS_CRYPTO_ERR_LENGTH (-1) /* bad/oversize length, size mismatch or NULL buffer */ |
| 46 | #define AIENOS_CRYPTO_ERR_AUTH (-2) /* tag did not verify */ |
| 47 | |
| 48 | /* ---- AES-256 (encrypt direction only; GCM-SIV never decrypts a block) ---- */ |
| 49 | |
| 50 | typedef struct { |
| 51 | uint32_t rk[60]; /* 15 round keys, big-endian words as in FIPS 197 */ |
| 52 | } aienos_aes256_key; |
| 53 | |
| 54 | void aienos_aes256_init(aienos_aes256_key *k, const uint8_t key[AIENOS_AES256_KEY_LEN]); |
| 55 | /* Encrypt one block in place. Constant-time: computed S-box, no tables. */ |
| 56 | void aienos_aes256_encrypt_block(const aienos_aes256_key *k, uint8_t block[AIENOS_AES_BLOCK_LEN]); |
| 57 | /* Zero the key schedule (volatile writes). Call when done with the key. */ |
| 58 | void aienos_aes256_wipe(aienos_aes256_key *k); |
| 59 | |
| 60 | /* ---- POLYVAL (RFC 8452 Section 3) ---- */ |
| 61 | |
| 62 | typedef struct { |
| 63 | uint8_t h[16]; /* hash key H */ |
| 64 | uint8_t s[16]; /* accumulator S_j */ |
| 65 | } aienos_polyval; |
| 66 | |
| 67 | /* out = dot(a, b) = a * b * x^-128 mod x^128 + x^127 + x^126 + x^121 + 1. |
| 68 | * out may alias a or b. */ |
| 69 | void aienos_polyval_dot(uint8_t out[16], const uint8_t a[16], const uint8_t b[16]); |
| 70 | void aienos_polyval_init(aienos_polyval *p, const uint8_t h[16]); |
| 71 | /* S = dot(S ^ X, H). */ |
| 72 | void aienos_polyval_update_block(aienos_polyval *p, const uint8_t x[16]); |
| 73 | /* Whole 16-byte blocks only: returns AIENOS_CRYPTO_ERR_LENGTH (and absorbs |
| 74 | * nothing) if len is not a multiple of 16. */ |
| 75 | int aienos_polyval_update(aienos_polyval *p, const uint8_t *data, size_t len); |
| 76 | /* Write S to out and wipe the state. */ |
| 77 | void aienos_polyval_final(aienos_polyval *p, uint8_t out[16]); |
| 78 | void aienos_polyval_wipe(aienos_polyval *p); |
| 79 | |
| 80 | /* ---- AEAD_AES_256_GCM_SIV (RFC 8452) ---- */ |
| 81 | |
| 82 | /* Per-nonce key derivation (RFC 8452 Section 4). Exposed for tests and |
| 83 | * callers that need it; the outputs are secret, wipe them after use. */ |
| 84 | void aienos_gcmsiv_derive_keys(const uint8_t key[AIENOS_GCMSIV_KEY_LEN], |
| 85 | const uint8_t nonce[AIENOS_GCMSIV_NONCE_LEN], |
| 86 | uint8_t auth_key[16], uint8_t enc_key[32]); |
| 87 | |
| 88 | /* Seal: out = ciphertext || tag, out_len must equal pt_len + 16. |
| 89 | * Limits: pt_len <= 2^36, aad_len <= 2^36. A NULL pointer is allowed only |
| 90 | * with a zero length. out may be exactly pt (in place); any other overlap is |
| 91 | * undefined. Length errors return AIENOS_CRYPTO_ERR_LENGTH before |
| 92 | * anything is read or written. */ |
| 93 | int aienos_gcmsiv_seal(const uint8_t key[AIENOS_GCMSIV_KEY_LEN], |
| 94 | const uint8_t nonce[AIENOS_GCMSIV_NONCE_LEN], |
| 95 | const uint8_t *aad, size_t aad_len, |
| 96 | const uint8_t *pt, size_t pt_len, |
| 97 | uint8_t *out, size_t out_len); |
| 98 | |
| 99 | /* Open: in = ciphertext || tag (in_len >= 16, in_len <= 2^36 + 16), |
| 100 | * out_len must equal in_len - 16, aad_len <= 2^36. |
| 101 | * |
| 102 | * RFC 8452 computes the tag over the plaintext, so the plaintext must exist |
| 103 | * before the tag can be checked. This function decrypts into out, computes |
| 104 | * the expected tag over out, compares tags in constant time, and on a |
| 105 | * mismatch zeroes all of out[0..out_len) with volatile writes before |
| 106 | * returning AIENOS_CRYPTO_ERR_AUTH. Length errors return |
| 107 | * AIENOS_CRYPTO_ERR_LENGTH before anything is read or written, so out then |
| 108 | * holds whatever the caller put there (never plaintext). out may be |
| 109 | * exactly in (in place); then a failed open zeroes the ciphertext too. |
| 110 | * Any other overlap is undefined. */ |
| 111 | int aienos_gcmsiv_open(const uint8_t key[AIENOS_GCMSIV_KEY_LEN], |
| 112 | const uint8_t nonce[AIENOS_GCMSIV_NONCE_LEN], |
| 113 | const uint8_t *aad, size_t aad_len, |
| 114 | const uint8_t *in, size_t in_len, |
| 115 | uint8_t *out, size_t out_len); |
| 116 | |
| 117 | /* ---- HMAC-SHA-256 with a 32-byte key (port of sha256.rs hmac_sha256) ---- */ |
| 118 | |
| 119 | typedef struct { |
| 120 | sha256_ctx inner; |
| 121 | uint8_t opad[64]; |
| 122 | } aienos_hmac_sha256_ctx; |
| 123 | |
| 124 | void aienos_hmac_sha256_init(aienos_hmac_sha256_ctx *c, const uint8_t key[32]); |
| 125 | void aienos_hmac_sha256_update(aienos_hmac_sha256_ctx *c, const uint8_t *data, size_t len); |
| 126 | /* Writes the MAC and wipes the context. */ |
| 127 | void aienos_hmac_sha256_final(aienos_hmac_sha256_ctx *c, uint8_t out[32]); |
| 128 | void aienos_hmac_sha256(const uint8_t key[32], const uint8_t *data, size_t len, uint8_t out[32]); |
| 129 | |
| 130 | /* Constant-time equality of two n-byte buffers: 1 if equal, 0 if not. |
| 131 | * Run time depends only on n. */ |
| 132 | int aienos_ct_equal(const uint8_t *a, const uint8_t *b, size_t n); |
| 133 | |
| 134 | /* Zero n bytes with volatile writes the optimizer cannot drop. */ |
| 135 | void aienos_wipe(void *p, size_t n); |
| 136 | |
| 137 | #endif /* AIENOS_CRYPTO_H */ |