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 / sig / sha512.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 | /* SHA-512 (FIPS 180-4). Round constants K are the FIPS 180-4 Section 4.2.3 |
| 18 | * values. Lines tagged GUARD:<name> are removed one at a time by |
| 19 | * `make mutants`; the tests must fail for each. */ |
| 20 | #include <string.h> |
| 21 | |
| 22 | #include "aienos_sig.h" |
| 23 | #include "sig_internal.h" |
| 24 | |
| 25 | static const uint64_t K[80] = { |
| 26 | 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, |
| 27 | 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, |
| 28 | 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, |
| 29 | 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694, |
| 30 | 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, |
| 31 | 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, |
| 32 | 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, |
| 33 | 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70, |
| 34 | 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, |
| 35 | 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, |
| 36 | 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, |
| 37 | 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, |
| 38 | 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, |
| 39 | 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, |
| 40 | 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, |
| 41 | 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, |
| 42 | 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, |
| 43 | 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b, |
| 44 | 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, |
| 45 | 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817, |
| 46 | }; |
| 47 | |
| 48 | #define ROR(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) |
| 49 | |
| 50 | static uint64_t load_be64(const uint8_t *p) |
| 51 | { |
| 52 | uint64_t v = 0; |
| 53 | for (int i = 0; i < 8; i++) |
| 54 | v = (v << 8) | p[i]; |
| 55 | return v; |
| 56 | } |
| 57 | |
| 58 | static void store_be64(uint8_t *p, uint64_t v) |
| 59 | { |
| 60 | for (int i = 7; i >= 0; i--) { |
| 61 | p[i] = (uint8_t)v; |
| 62 | v >>= 8; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void compress(uint64_t h[8], const uint8_t blk[128]) |
| 67 | { |
| 68 | uint64_t w[80]; |
| 69 | for (int i = 0; i < 16; i++) |
| 70 | w[i] = load_be64(blk + 8 * i); |
| 71 | for (int i = 16; i < 80; i++) { |
| 72 | uint64_t s0 = ROR(w[i - 15], 1) ^ ROR(w[i - 15], 8) ^ (w[i - 15] >> 7); |
| 73 | uint64_t s1 = ROR(w[i - 2], 19) ^ ROR(w[i - 2], 61) ^ (w[i - 2] >> 6); |
| 74 | w[i] = w[i - 16] + s0 + w[i - 7] + s1; |
| 75 | } |
| 76 | uint64_t a = h[0], b = h[1], c = h[2], d = h[3]; |
| 77 | uint64_t e = h[4], f = h[5], g = h[6], hh = h[7]; |
| 78 | for (int i = 0; i < 80; i++) { |
| 79 | uint64_t S1 = ROR(e, 14) ^ ROR(e, 18) ^ ROR(e, 41); |
| 80 | uint64_t ch = (e & f) ^ (~e & g); |
| 81 | uint64_t t1 = hh + S1 + ch + K[i] + w[i]; |
| 82 | uint64_t S0 = ROR(a, 28) ^ ROR(a, 34) ^ ROR(a, 39); |
| 83 | uint64_t maj = (a & b) ^ (a & c) ^ (b & c); |
| 84 | uint64_t t2 = S0 + maj; |
| 85 | hh = g; g = f; f = e; e = d + t1; |
| 86 | d = c; c = b; b = a; a = t1 + t2; |
| 87 | } |
| 88 | h[0] += a; h[1] += b; h[2] += c; h[3] += d; |
| 89 | h[4] += e; h[5] += f; h[6] += g; h[7] += hh; |
| 90 | aienos_sig_wipe(w, sizeof w); |
| 91 | } |
| 92 | |
| 93 | void aienos_sha512_init(aienos_sha512_ctx *c) |
| 94 | { |
| 95 | static const uint64_t iv[8] = { |
| 96 | 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, |
| 97 | 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, |
| 98 | }; |
| 99 | for (int i = 0; i < 8; i++) |
| 100 | c->h[i] = iv[i]; |
| 101 | c->bytes = 0; |
| 102 | c->n = 0; |
| 103 | } |
| 104 | |
| 105 | void aienos_sha512_update(aienos_sha512_ctx *c, const uint8_t *p, size_t len) |
| 106 | { |
| 107 | if (len == 0) |
| 108 | return; |
| 109 | c->bytes += (uint64_t)len; |
| 110 | if (c->n > 0) { |
| 111 | size_t take = 128 - c->n; |
| 112 | if (take > len) |
| 113 | take = len; |
| 114 | memcpy(c->buf + c->n, p, take); |
| 115 | c->n += take; |
| 116 | p += take; |
| 117 | len -= take; |
| 118 | if (c->n < 128) |
| 119 | return; |
| 120 | compress(c->h, c->buf); |
| 121 | c->n = 0; |
| 122 | } |
| 123 | while (len >= 128) { |
| 124 | compress(c->h, p); |
| 125 | p += 128; |
| 126 | len -= 128; |
| 127 | } |
| 128 | if (len > 0) { |
| 129 | memcpy(c->buf, p, len); |
| 130 | c->n = len; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void aienos_sha512_final(aienos_sha512_ctx *c, uint8_t out[64]) |
| 135 | { |
| 136 | uint64_t bits_hi = c->bytes >> 61, bits_lo = c->bytes << 3; |
| 137 | size_t n = c->n; |
| 138 | c->buf[n++] = 0x80; /* GUARD:sha-pad */ |
| 139 | if (n > 112) { |
| 140 | memset(c->buf + n, 0, 128 - n); |
| 141 | compress(c->h, c->buf); |
| 142 | n = 0; |
| 143 | } |
| 144 | memset(c->buf + n, 0, 112 - n); |
| 145 | store_be64(c->buf + 112, bits_hi); |
| 146 | store_be64(c->buf + 120, bits_lo); /* GUARD:sha-len */ |
| 147 | compress(c->h, c->buf); |
| 148 | for (int i = 0; i < 8; i++) |
| 149 | store_be64(out + 8 * i, c->h[i]); |
| 150 | aienos_sig_wipe(c, sizeof *c); |
| 151 | } |
| 152 | |
| 153 | void aienos_sha512(const uint8_t *p, size_t len, uint8_t out[64]) |
| 154 | { |
| 155 | aienos_sha512_ctx c; |
| 156 | aienos_sha512_init(&c); |
| 157 | aienos_sha512_update(&c, p, len); |
| 158 | aienos_sha512_final(&c, out); |
| 159 | } |