omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / fabric / fab_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 | /* fab_hmac.c -- HMAC-SHA256. See fab_hmac.h. */ |
| 18 | #include "fab_hmac.h" |
| 19 | #include "omega_native.h" |
| 20 | |
| 21 | #include "sha256.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | /* Link-map marker (test build only): tools/r16_prod_hygiene.sh refuses any |
| 26 | * production binary that carries an aien_test_build_* symbol. */ |
| 27 | __attribute__((used)) const char aien_test_build_fab_hmac[] = |
| 28 | "AIEN_TEST_BUILD piece: Fabric F5-0 HMAC stand-in (not a real signature)"; |
| 29 | |
| 30 | int fab_hmac_sha256(const uint8_t *key, size_t key_len, const uint8_t *msg, size_t msg_len, |
| 31 | uint8_t out[32]) { |
| 32 | uint8_t k[SHA256_BLOCK_SIZE], pad[SHA256_BLOCK_SIZE], inner[SHA256_DIGEST_SIZE]; |
| 33 | memset(k, 0, sizeof k); |
| 34 | if (key_len > SHA256_BLOCK_SIZE) sha256_hash(key, key_len, k); |
| 35 | else if (key_len) memcpy(k, key, key_len); |
| 36 | sha256_ctx h; |
| 37 | uint64_t u = 0; |
| 38 | if (omega2_fab_om_hmac_xor_pad(&u, k, SHA256_BLOCK_SIZE, pad, SHA256_BLOCK_SIZE, 0x36) != OMEGA_OK) goto failed; |
| 39 | sha256_init(&h); |
| 40 | sha256_update(&h, pad, sizeof pad); |
| 41 | if (msg_len) sha256_update(&h, msg, msg_len); |
| 42 | sha256_final(&h, inner); |
| 43 | if (omega2_fab_om_hmac_xor_pad(&u, k, SHA256_BLOCK_SIZE, pad, SHA256_BLOCK_SIZE, 0x5c) != OMEGA_OK) goto failed; |
| 44 | sha256_init(&h); |
| 45 | sha256_update(&h, pad, sizeof pad); |
| 46 | sha256_update(&h, inner, sizeof inner); |
| 47 | sha256_final(&h, out); |
| 48 | memset(k, 0, sizeof k); |
| 49 | memset(pad, 0, sizeof pad); |
| 50 | return 0; |
| 51 | failed: |
| 52 | memset(k, 0, sizeof k); |
| 53 | memset(pad, 0, sizeof pad); |
| 54 | memset(out, 0, 32); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | int fab_ct_equal(const uint8_t *a, const uint8_t *b, size_t n) { |
| 59 | uint64_t ok = 0; |
| 60 | if (n == 0) return 1; |
| 61 | return omega2_fab_om_ct_equal(&ok, a, n, b, n, n) == OMEGA_OK && ok; |
| 62 | } |
| 63 | |
| 64 | int fab_hmac_sig64(const uint8_t key[32], const uint8_t *msg, size_t msg_len, uint8_t out[64]) { |
| 65 | static const char half2[] = "AFAB stand-in sig64 half 2"; |
| 66 | uint8_t k2[32]; |
| 67 | int failed = fab_hmac_sha256(key, 32, msg, msg_len, out) || |
| 68 | fab_hmac_sha256(key, 32, (const uint8_t *)half2, sizeof half2 - 1, k2) || |
| 69 | fab_hmac_sha256(k2, 32, msg, msg_len, out + 32); |
| 70 | memset(k2, 0, sizeof k2); |
| 71 | if (failed) memset(out, 0, 64); |
| 72 | return failed ? -1 : 0; |
| 73 | } |