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 / argus / sha256.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 | /* Copied from aien-dev/omega src/sha256.{c,h} at ceb68d6 (in-house lineage), unchanged except this note */ |
| 18 | #include "sha256.h" |
| 19 | #include <string.h> |
| 20 | |
| 21 | #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) |
| 22 | #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) |
| 23 | #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
| 24 | #define EP0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) |
| 25 | #define EP1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) |
| 26 | #define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3)) |
| 27 | #define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10)) |
| 28 | |
| 29 | static const uint32_t K[64] = { |
| 30 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
| 31 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 32 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| 33 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 34 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
| 35 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 36 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 37 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 38 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| 39 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 40 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
| 41 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 42 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
| 43 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 44 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| 45 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| 46 | }; |
| 47 | |
| 48 | static void sha256_transform(sha256_ctx *ctx, const uint8_t *data) { |
| 49 | uint32_t a, b, c, d, e, f, g, h, t1, t2, m[64]; |
| 50 | size_t i, j; |
| 51 | |
| 52 | for (i = 0, j = 0; i < 16; ++i, j += 4) { |
| 53 | m[i] = ((uint32_t)data[j] << 24) | |
| 54 | ((uint32_t)data[j + 1] << 16) | |
| 55 | ((uint32_t)data[j + 2] << 8) | |
| 56 | ((uint32_t)data[j + 3]); |
| 57 | } |
| 58 | for (; i < 64; ++i) { |
| 59 | m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; |
| 60 | } |
| 61 | |
| 62 | a = ctx->state[0]; |
| 63 | b = ctx->state[1]; |
| 64 | c = ctx->state[2]; |
| 65 | d = ctx->state[3]; |
| 66 | e = ctx->state[4]; |
| 67 | f = ctx->state[5]; |
| 68 | g = ctx->state[6]; |
| 69 | h = ctx->state[7]; |
| 70 | |
| 71 | for (i = 0; i < 64; ++i) { |
| 72 | t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i]; |
| 73 | t2 = EP0(a) + MAJ(a, b, c); |
| 74 | h = g; |
| 75 | g = f; |
| 76 | f = e; |
| 77 | e = d + t1; |
| 78 | d = c; |
| 79 | c = b; |
| 80 | b = a; |
| 81 | a = t1 + t2; |
| 82 | } |
| 83 | |
| 84 | ctx->state[0] += a; |
| 85 | ctx->state[1] += b; |
| 86 | ctx->state[2] += c; |
| 87 | ctx->state[3] += d; |
| 88 | ctx->state[4] += e; |
| 89 | ctx->state[5] += f; |
| 90 | ctx->state[6] += g; |
| 91 | ctx->state[7] += h; |
| 92 | } |
| 93 | |
| 94 | void sha256_init(sha256_ctx *ctx) { |
| 95 | ctx->count = 0; |
| 96 | ctx->state[0] = 0x6a09e667; |
| 97 | ctx->state[1] = 0xbb67ae85; |
| 98 | ctx->state[2] = 0x3c6ef372; |
| 99 | ctx->state[3] = 0xa54ff53a; |
| 100 | ctx->state[4] = 0x510e527f; |
| 101 | ctx->state[5] = 0x9b05688c; |
| 102 | ctx->state[6] = 0x1f83d9ab; |
| 103 | ctx->state[7] = 0x5be0cd19; |
| 104 | } |
| 105 | |
| 106 | void sha256_update(sha256_ctx *ctx, const uint8_t *data, size_t len) { |
| 107 | size_t buffer_index = (size_t)(ctx->count & 0x3f); |
| 108 | ctx->count += len; |
| 109 | |
| 110 | if (buffer_index > 0) { |
| 111 | size_t left = SHA256_BLOCK_SIZE - buffer_index; |
| 112 | if (len < left) { |
| 113 | memcpy(&ctx->buffer[buffer_index], data, len); |
| 114 | return; |
| 115 | } |
| 116 | memcpy(&ctx->buffer[buffer_index], data, left); |
| 117 | sha256_transform(ctx, ctx->buffer); |
| 118 | data += left; |
| 119 | len -= left; |
| 120 | } |
| 121 | |
| 122 | while (len >= SHA256_BLOCK_SIZE) { |
| 123 | sha256_transform(ctx, data); |
| 124 | data += SHA256_BLOCK_SIZE; |
| 125 | len -= SHA256_BLOCK_SIZE; |
| 126 | } |
| 127 | |
| 128 | if (len > 0) { |
| 129 | memcpy(ctx->buffer, data, len); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void sha256_final(sha256_ctx *ctx, uint8_t digest[SHA256_DIGEST_SIZE]) { |
| 134 | uint8_t count_bytes[8]; |
| 135 | uint64_t bit_count = ctx->count * 8; |
| 136 | size_t pad_len; |
| 137 | size_t buffer_index = (size_t)(ctx->count & 0x3f); |
| 138 | static const uint8_t padding[64] = { 0x80 }; |
| 139 | |
| 140 | for (int i = 0; i < 8; ++i) { |
| 141 | count_bytes[7 - i] = (uint8_t)(bit_count >> (i * 8)); |
| 142 | } |
| 143 | |
| 144 | pad_len = (buffer_index < 56) ? (56 - buffer_index) : (120 - buffer_index); |
| 145 | sha256_update(ctx, padding, 1); |
| 146 | if (pad_len > 1) { |
| 147 | static const uint8_t zeroes[64] = { 0 }; |
| 148 | sha256_update(ctx, zeroes, pad_len - 1); |
| 149 | } |
| 150 | sha256_update(ctx, count_bytes, 8); |
| 151 | |
| 152 | for (int i = 0; i < 8; ++i) { |
| 153 | digest[i * 4] = (uint8_t)(ctx->state[i] >> 24); |
| 154 | digest[i * 4 + 1] = (uint8_t)(ctx->state[i] >> 16); |
| 155 | digest[i * 4 + 2] = (uint8_t)(ctx->state[i] >> 8); |
| 156 | digest[i * 4 + 3] = (uint8_t)(ctx->state[i]); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void sha256_hash(const uint8_t *data, size_t len, uint8_t digest[SHA256_DIGEST_SIZE]) { |
| 161 | sha256_ctx ctx; |
| 162 | sha256_init(&ctx); |
| 163 | sha256_update(&ctx, data, len); |
| 164 | sha256_final(&ctx, digest); |
| 165 | } |