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 / hmac.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 | /* HMAC-SHA-256 with a 32-byte key, port of sha256.rs hmac_sha256. |
| 18 | * SHA-256 itself is the in-house native/argus/sha256.c. */ |
| 19 | #include "aienos_crypto.h" |
| 20 | |
| 21 | void aienos_hmac_sha256_init(aienos_hmac_sha256_ctx *c, const uint8_t key[32]) |
| 22 | { |
| 23 | uint8_t ipad[64]; |
| 24 | for (int i = 0; i < 64; i++) { |
| 25 | uint8_t k = i < 32 ? key[i] : 0; |
| 26 | ipad[i] = (uint8_t)(0x36 ^ k); |
| 27 | c->opad[i] = (uint8_t)(0x5c ^ k); |
| 28 | } |
| 29 | sha256_init(&c->inner); |
| 30 | sha256_update(&c->inner, ipad, sizeof ipad); |
| 31 | aienos_wipe(ipad, sizeof ipad); |
| 32 | } |
| 33 | |
| 34 | void aienos_hmac_sha256_update(aienos_hmac_sha256_ctx *c, const uint8_t *data, size_t len) |
| 35 | { |
| 36 | if (len != 0) |
| 37 | sha256_update(&c->inner, data, len); |
| 38 | } |
| 39 | |
| 40 | void aienos_hmac_sha256_final(aienos_hmac_sha256_ctx *c, uint8_t out[32]) |
| 41 | { |
| 42 | uint8_t inner_digest[32]; |
| 43 | sha256_ctx outer; |
| 44 | sha256_final(&c->inner, inner_digest); |
| 45 | sha256_init(&outer); |
| 46 | sha256_update(&outer, c->opad, sizeof c->opad); |
| 47 | sha256_update(&outer, inner_digest, sizeof inner_digest); |
| 48 | sha256_final(&outer, out); |
| 49 | aienos_wipe(inner_digest, sizeof inner_digest); |
| 50 | aienos_wipe(&outer, sizeof outer); |
| 51 | aienos_wipe(c, sizeof *c); |
| 52 | } |
| 53 | |
| 54 | void aienos_hmac_sha256(const uint8_t key[32], const uint8_t *data, size_t len, uint8_t out[32]) |
| 55 | { |
| 56 | aienos_hmac_sha256_ctx c; |
| 57 | aienos_hmac_sha256_init(&c, key); |
| 58 | aienos_hmac_sha256_update(&c, data, len); |
| 59 | aienos_hmac_sha256_final(&c, out); |
| 60 | } |