interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / sha256.c

202 lines · 6950 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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#include "sha256.h"
18#include <string.h>
19
20/* Freestanding (OMEGA_GPU_NO_POSIX) keeps the C oracle: MIGRATION-PENDING, see omega_core_binding.h. */
21#if !defined(CUDAPP_MIGRATION_PENDING_C_CORE) && !defined(OMEGA_GPU_NO_POSIX)
22#include "omega_core_binding.h"
23#include <stdlib.h>
24_Static_assert(sizeof(sha256_ctx) == 104, "seeded SHA context ABI");
25_Static_assert(offsetof(sha256_ctx, count) == 32, "seeded SHA count ABI");
26_Static_assert(offsetof(sha256_ctx, buffer) == 40, "seeded SHA buffer ABI");
27static void sha_call(sha256_ctx *ctx, const uint8_t *data, size_t len,
28 uint64_t op, uint8_t *digest) {
29 uint64_t input[512], scratch[64], result;
30 if (len) memcpy(input, data, len);
31 if (CORE_CALL(omsha, &result, (uint8_t *)ctx, 104, (const uint8_t *)input, 4096,
32 len, op, scratch, 64, input, 512) != OMEGA_OK || result) CORE_STOP();
33 if (digest) memcpy(digest, scratch, SHA256_DIGEST_SIZE);
34}
35void sha256_init(sha256_ctx *ctx) { sha_call(ctx, NULL, 0, 0, NULL); }
36void sha256_update(sha256_ctx *ctx, const uint8_t *data, size_t len) {
37 while (len) {
38 size_t n = len > 4096 ? 4096 : len;
39 sha_call(ctx, data, n, 1, NULL);
40 data += n;
41 len -= n;
42 }
43}
44void sha256_final(sha256_ctx *ctx, uint8_t digest[SHA256_DIGEST_SIZE]) {
45 sha_call(ctx, NULL, 0, 2, digest);
46}
47void sha256_hash(const uint8_t *data, size_t len, uint8_t digest[SHA256_DIGEST_SIZE]) {
48 sha256_ctx ctx;
49 /* Short inputs take the unit's one-shot path (op 3, om/core/sha256.om oneshot/tail8); it ignores ctx. */
50 if (len < 4096) { sha_call(&ctx, data, len, 3, digest); return; }
51 sha256_init(&ctx);
52 sha256_update(&ctx, data, len);
53 sha256_final(&ctx, digest);
54}
55#else
56
57#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
58#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
59#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
60#define EP0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
61#define EP1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
62#define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3))
63#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
64
65static const uint32_t K[64] = {
66 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
67 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
68 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
69 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
70 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
71 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
72 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
73 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
74 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
75 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
76 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
77 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
78 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
79 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
80 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
81 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
82};
83
84static void sha256_transform(sha256_ctx *ctx, const uint8_t *data) {
85 uint32_t a, b, c, d, e, f, g, h, t1, t2, m[64];
86 size_t i, j;
87
88 for (i = 0, j = 0; i < 16; ++i, j += 4) {
89 m[i] = ((uint32_t)data[j] << 24) |
90 ((uint32_t)data[j + 1] << 16) |
91 ((uint32_t)data[j + 2] << 8) |
92 ((uint32_t)data[j + 3]);
93 }
94 for (; i < 64; ++i) {
95 m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
96 }
97
98 a = ctx->state[0];
99 b = ctx->state[1];
100 c = ctx->state[2];
101 d = ctx->state[3];
102 e = ctx->state[4];
103 f = ctx->state[5];
104 g = ctx->state[6];
105 h = ctx->state[7];
106
107 for (i = 0; i < 64; ++i) {
108 t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i];
109 t2 = EP0(a) + MAJ(a, b, c);
110 h = g;
111 g = f;
112 f = e;
113 e = d + t1;
114 d = c;
115 c = b;
116 b = a;
117 a = t1 + t2;
118 }
119
120 ctx->state[0] += a;
121 ctx->state[1] += b;
122 ctx->state[2] += c;
123 ctx->state[3] += d;
124 ctx->state[4] += e;
125 ctx->state[5] += f;
126 ctx->state[6] += g;
127 ctx->state[7] += h;
128}
129
130void sha256_init(sha256_ctx *ctx) {
131 ctx->count = 0;
132 ctx->state[0] = 0x6a09e667;
133 ctx->state[1] = 0xbb67ae85;
134 ctx->state[2] = 0x3c6ef372;
135 ctx->state[3] = 0xa54ff53a;
136 ctx->state[4] = 0x510e527f;
137 ctx->state[5] = 0x9b05688c;
138 ctx->state[6] = 0x1f83d9ab;
139 ctx->state[7] = 0x5be0cd19;
140}
141
142void sha256_update(sha256_ctx *ctx, const uint8_t *data, size_t len) {
143 size_t buffer_index = (size_t)(ctx->count & 0x3f);
144 ctx->count += len;
145
146 if (buffer_index > 0) {
147 size_t left = SHA256_BLOCK_SIZE - buffer_index;
148 if (len < left) {
149 memcpy(&ctx->buffer[buffer_index], data, len);
150 return;
151 }
152 memcpy(&ctx->buffer[buffer_index], data, left);
153 sha256_transform(ctx, ctx->buffer);
154 data += left;
155 len -= left;
156 }
157
158 while (len >= SHA256_BLOCK_SIZE) {
159 sha256_transform(ctx, data);
160 data += SHA256_BLOCK_SIZE;
161 len -= SHA256_BLOCK_SIZE;
162 }
163
164 if (len > 0) {
165 memcpy(ctx->buffer, data, len);
166 }
167}
168
169void sha256_final(sha256_ctx *ctx, uint8_t digest[SHA256_DIGEST_SIZE]) {
170 uint8_t count_bytes[8];
171 uint64_t bit_count = ctx->count * 8;
172 size_t pad_len;
173 size_t buffer_index = (size_t)(ctx->count & 0x3f);
174 static const uint8_t padding[64] = { 0x80 };
175
176 for (int i = 0; i < 8; ++i) {
177 count_bytes[7 - i] = (uint8_t)(bit_count >> (i * 8));
178 }
179
180 pad_len = (buffer_index < 56) ? (56 - buffer_index) : (120 - buffer_index);
181 sha256_update(ctx, padding, 1);
182 if (pad_len > 1) {
183 static const uint8_t zeroes[64] = { 0 };
184 sha256_update(ctx, zeroes, pad_len - 1);
185 }
186 sha256_update(ctx, count_bytes, 8);
187
188 for (int i = 0; i < 8; ++i) {
189 digest[i * 4] = (uint8_t)(ctx->state[i] >> 24);
190 digest[i * 4 + 1] = (uint8_t)(ctx->state[i] >> 16);
191 digest[i * 4 + 2] = (uint8_t)(ctx->state[i] >> 8);
192 digest[i * 4 + 3] = (uint8_t)(ctx->state[i]);
193 }
194}
195
196void sha256_hash(const uint8_t *data, size_t len, uint8_t digest[SHA256_DIGEST_SIZE]) {
197 sha256_ctx ctx;
198 sha256_init(&ctx);
199 sha256_update(&ctx, data, len);
200 sha256_final(&ctx, digest);
201}
202#endif