Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
sha256fst.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file sha256fst.c
20 * @brief Portable SHA-256 block compression, streaming and digest formatting.
21 * @details Implements the SHA-256 operations from FIPS 180-4 using unsigned
22 * 32-bit arithmetic. Input words and output bytes are explicitly big-endian;
23 * neither pointer alignment nor the host byte order affects the result.
24 * Complete input blocks bypass the context buffer. The compression schedule
25 * occupies a fixed 256-byte local array; no heap or mutable global state is used.
26 */
27
28#include "sha256fst.h"
29#include <errno.h>
30#include <fcntl.h>
31#include <sys/stat.h>
32#include <unistd.h>
33
34/**
35 * @brief Rotate a 32-bit word without signed arithmetic.
36 * @param value Word to rotate.
37 * @param count Rotation distance; every caller uses a constant in [1, 31].
38 * @return Rotated word.
39 */
40static inline uint32_t rotate_right(uint32_t value, unsigned count) {
41 return (value >> count) | (value << (32 - count));
42}
43
44/**
45 * @brief Load a message word safely even when input is unaligned.
46 * @param bytes Readable four-byte region, most significant byte first.
47 * @return Decoded word.
48 * @details Explicit byte loads avoid alignment and strict-aliasing violations.
49 * Optimizing compilers can still select native load/byte-reversal instructions.
50 */
51static inline uint32_t load_big_endian(const uint8_t *bytes) {
52 return ((uint32_t)bytes[0] << 24) |
53 ((uint32_t)bytes[1] << 16) |
54 ((uint32_t)bytes[2] << 8) |
55 (uint32_t)bytes[3];
56}
57
58/**
59 * @brief Lowercase sigma-0 for expansion of the message schedule.
60 * @param value Earlier schedule word.
61 * @return Two rotations XORed with a logical right shift.
62 */
63static inline uint32_t schedule_sigma_zero(uint32_t value) {
64 return rotate_right(value, 7) ^ rotate_right(value, 18) ^ (value >> 3);
65}
66
67/**
68 * @brief Lowercase sigma-1 for expansion of the message schedule.
69 * @param value Earlier schedule word.
70 * @return Two rotations XORed with a logical right shift.
71 */
72static inline uint32_t schedule_sigma_one(uint32_t value) {
73 return rotate_right(value, 17) ^ rotate_right(value, 19) ^ (value >> 10);
74}
75
76/**
77 * @brief Uppercase Sigma-0 for the first working word in a compression round.
78 * @param value Current working word A.
79 * @return XOR of the three SHA-256 rotations.
80 */
81static inline uint32_t round_sigma_zero(uint32_t value) {
82 return rotate_right(value, 2) ^ rotate_right(value, 13) ^ rotate_right(value, 22);
83}
84
85/**
86 * @brief Uppercase Sigma-1 for the fifth working word in a compression round.
87 * @param value Current working word E.
88 * @return XOR of the three SHA-256 rotations.
89 */
90static inline uint32_t round_sigma_one(uint32_t value) {
91 return rotate_right(value, 6) ^ rotate_right(value, 11) ^ rotate_right(value, 25);
92}
93
94/**
95 * @brief Fold exactly one complete input block into the chaining state.
96 * @param[in,out] context Context whose eight chaining words are updated.
97 * @param[in] block Readable SHA256_BLOCK_SIZE-byte region of any alignment.
98 * @details Does not change the byte count or pending buffer. Working words A-H
99 * and the schedule follow the standard directly; uint32_t addition provides
100 * the required modulo-2^32 wraparound on both supported architectures.
101 */
102static void compress_block(struct sha256 *context, const uint8_t *block) {
103 /** @brief FIPS 180-4 section 4.2.2 constants, ordered by compression round. */
104 static const uint32_t round_constants[64] = {
105 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
106 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
107 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
108 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
109 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
110 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
111 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
112 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
113 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
114 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
115 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
116 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
117 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
118 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
119 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
120 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
121 };
122
123 uint32_t words[64];
124 for (size_t index = 0; index < 16; ++index) {
125 words[index] = load_big_endian(block + index * 4);
126 }
127 for (size_t index = 16; index < 64; ++index) {
128 words[index] = schedule_sigma_one(words[index - 2]) + words[index - 7] +
129 schedule_sigma_zero(words[index - 15]) + words[index - 16];
130 }
131
132 uint32_t state_a = context->state[0];
133 uint32_t state_b = context->state[1];
134 uint32_t state_c = context->state[2];
135 uint32_t state_d = context->state[3];
136 uint32_t state_e = context->state[4];
137 uint32_t state_f = context->state[5];
138 uint32_t state_g = context->state[6];
139 uint32_t state_h = context->state[7];
140
141 for (size_t round = 0; round < 64; ++round) {
142 uint32_t choose = (state_e & state_f) ^ (~state_e & state_g);
143 uint32_t majority = (state_a & state_b) ^ (state_a & state_c) ^ (state_b & state_c);
144 uint32_t first_sum = state_h + round_sigma_one(state_e) + choose +
145 round_constants[round] + words[round];
146 uint32_t second_sum = round_sigma_zero(state_a) + majority;
147
148 state_h = state_g;
149 state_g = state_f;
150 state_f = state_e;
151 state_e = state_d + first_sum;
152 state_d = state_c;
153 state_c = state_b;
154 state_b = state_a;
155 state_a = first_sum + second_sum;
156 }
157
158 context->state[0] += state_a;
159 context->state[1] += state_b;
160 context->state[2] += state_c;
161 context->state[3] += state_d;
162 context->state[4] += state_e;
163 context->state[5] += state_f;
164 context->state[6] += state_g;
165 context->state[7] += state_h;
166}
167
168void sha256_init(struct sha256 *context) {
169 context->state[0] = 0x6a09e667;
170 context->state[1] = 0xbb67ae85;
171 context->state[2] = 0x3c6ef372;
172 context->state[3] = 0xa54ff53a;
173 context->state[4] = 0x510e527f;
174 context->state[5] = 0x9b05688c;
175 context->state[6] = 0x1f83d9ab;
176 context->state[7] = 0x5be0cd19;
177 context->n_bits = 0;
178 context->buffer_counter = 0;
179}
180
181void sha256_append_byte(struct sha256 *context, uint8_t byte) {
182 context->buffer[context->buffer_counter++] = byte;
183 context->n_bits += 8;
184 if (context->buffer_counter == SHA256_BLOCK_SIZE) {
185 compress_block(context, context->buffer);
186 context->buffer_counter = 0;
187 }
188}
189
190void sha256_append(struct sha256 *context, const void *data, size_t length) {
191 if (length == 0) {
192 return;
193 }
194 const uint8_t *bytes = (const uint8_t *)data;
195 context->n_bits += (uint64_t)length * 8;
196
197 /* Finish a previous chunk's partial block before reading directly from data. */
198 if (context->buffer_counter != 0) {
199 size_t available = SHA256_BLOCK_SIZE - context->buffer_counter;
200 size_t count = length < available ? length : available;
201 for (size_t index = 0; index < count; ++index) {
202 context->buffer[context->buffer_counter + index] = bytes[index];
203 }
204 context->buffer_counter += (uint8_t)count;
205 bytes += count;
206 length -= count;
207 if (context->buffer_counter != SHA256_BLOCK_SIZE) {
208 return;
209 }
210 compress_block(context, context->buffer);
211 context->buffer_counter = 0;
212 }
213
214 /* Full blocks need no copy; only the final 0-63 bytes must survive this call. */
215 while (length >= SHA256_BLOCK_SIZE) {
216 compress_block(context, bytes);
218 length -= SHA256_BLOCK_SIZE;
219 }
220 for (size_t index = 0; index < length; ++index) {
221 context->buffer[index] = bytes[index];
222 }
223 context->buffer_counter = (uint8_t)length;
224}
225
226void sha256_finalize(struct sha256 *context) {
227 const uint64_t message_bits = context->n_bits;
228 size_t count = context->buffer_counter;
229 context->buffer[count++] = 0x80;
230
231 /* The eight-byte length must fit entirely in the final block. */
232 if (count > SHA256_BLOCK_SIZE - 8) {
233 while (count < SHA256_BLOCK_SIZE) {
234 context->buffer[count++] = 0;
235 }
236 compress_block(context, context->buffer);
237 count = 0;
238 }
239 while (count < SHA256_BLOCK_SIZE - 8) {
240 context->buffer[count++] = 0;
241 }
242 for (size_t index = 0; index < 8; ++index) {
243 context->buffer[count + index] = (uint8_t)(message_bits >> (56 - index * 8));
244 }
245 compress_block(context, context->buffer);
246 context->buffer_counter = 0;
247}
248
249void sha256_finalize_hex(struct sha256 *context, char *output) {
250 static const char hexadecimal[] = "0123456789abcdef";
251 sha256_finalize(context);
252 for (size_t word = 0; word < 8; ++word) {
253 for (size_t byte_index = 0; byte_index < 4; ++byte_index) {
254 uint8_t byte = (uint8_t)(context->state[word] >> (24 - byte_index * 8));
255 output[word * 8 + byte_index * 2] = hexadecimal[byte >> 4];
256 output[word * 8 + byte_index * 2 + 1] = hexadecimal[byte & 15];
257 }
258 }
259 output[SHA256_HEX_SIZE - 1] = '\0';
260}
261
262void sha256_finalize_bytes(struct sha256 *context, void *output) {
263 uint8_t *bytes = (uint8_t *)output;
264 sha256_finalize(context);
265 for (size_t word = 0; word < 8; ++word) {
266 for (size_t byte_index = 0; byte_index < 4; ++byte_index) {
267 bytes[word * 4 + byte_index] = (uint8_t)(context->state[word] >> (24 - byte_index * 8));
268 }
269 }
270}
271
272void sha256_hex(const void *data, size_t length, char *output) {
273 struct sha256 context;
274 sha256_init(&context);
275 sha256_append(&context, data, length);
276 sha256_finalize_hex(&context, output);
277}
278
279void sha256_bytes(const void *data, size_t length, void *output) {
280 struct sha256 context;
281 sha256_init(&context);
282 sha256_append(&context, data, length);
283 sha256_finalize_bytes(&context, output);
284}
285
286bool sha256_file(const char *filename, char output[SHA256_HEX_SIZE]) {
287 if (!filename || !output) return false;
288 const int descriptor = open(filename, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
289 if (descriptor < 0) return false;
290 struct stat before, after;
291 bool valid = fstat(descriptor, &before) == 0 && S_ISREG(before.st_mode) && before.st_size > 0;
292 struct sha256 hash;
293 sha256_init(&hash);
294 unsigned char buffer[16384];
295 off_t consumed = 0;
296 while (valid) {
297 const ssize_t count = read(descriptor, buffer, sizeof(buffer));
298 if (count < 0 && errno == EINTR) continue;
299 if (count < 0) valid = false;
300 if (count <= 0) break;
301 if (count > before.st_size - consumed) { valid = false; break; }
302 consumed += count;
303 sha256_append(&hash, buffer, (size_t)count);
304 }
305 valid = valid && consumed == before.st_size && fstat(descriptor, &after) == 0 && before.st_dev == after.st_dev &&
306 before.st_ino == after.st_ino && before.st_size == after.st_size &&
307 before.st_mtim.tv_sec == after.st_mtim.tv_sec && before.st_mtim.tv_nsec == after.st_mtim.tv_nsec;
308 if (close(descriptor)) valid = false;
309 if (valid) sha256_finalize_hex(&hash, output);
310 return valid;
311}
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
int descriptor
Definition chdk_pipe.c:142
void(* close)(struct usbfs_handle *)
Definition chdkbridge.c:57
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
Definition chdkbridge.c:56
static pthread_cond_t available
Definition pose_log.c:73
static size_t count
Definition pose_log.c:79
static FILE * output
Definition pose_log.c:77
void sha256_bytes(const void *data, size_t length, void *output)
Hash one memory region to its standard 32-byte binary digest.
Definition sha256fst.c:279
static uint32_t round_sigma_one(uint32_t value)
Uppercase Sigma-1 for the fifth working word in a compression round.
Definition sha256fst.c:90
void sha256_append_byte(struct sha256 *context, uint8_t byte)
Append one byte to an unfinished message.
Definition sha256fst.c:181
static uint32_t schedule_sigma_zero(uint32_t value)
Lowercase sigma-0 for expansion of the message schedule.
Definition sha256fst.c:63
bool sha256_file(const char *filename, char output[SHA256_HEX_SIZE])
Hash an immutable nonempty regular file with bounded streaming scratch.
Definition sha256fst.c:286
void sha256_hex(const void *data, size_t length, char *output)
Hash one memory region to a lowercase hexadecimal string.
Definition sha256fst.c:272
void sha256_finalize_hex(struct sha256 *context, char *output)
Finish a message and write its lowercase hexadecimal digest.
Definition sha256fst.c:249
static uint32_t round_sigma_zero(uint32_t value)
Uppercase Sigma-0 for the first working word in a compression round.
Definition sha256fst.c:81
void sha256_finalize(struct sha256 *context)
Finish a message, leaving its eight digest words in context->state.
Definition sha256fst.c:226
void sha256_append(struct sha256 *context, const void *data, size_t length)
Append an arbitrarily aligned chunk; empty chunks are harmless.
Definition sha256fst.c:190
void sha256_init(struct sha256 *context)
Initialize or reset a context for a new message.
Definition sha256fst.c:168
static void compress_block(struct sha256 *context, const uint8_t *block)
Fold exactly one complete input block into the chaining state.
Definition sha256fst.c:102
static uint32_t load_big_endian(const uint8_t *bytes)
Load a message word safely even when input is unaligned.
Definition sha256fst.c:51
static uint32_t rotate_right(uint32_t value, unsigned count)
Rotate a 32-bit word without signed arithmetic.
Definition sha256fst.c:40
void sha256_finalize_bytes(struct sha256 *context, void *output)
Finish a message and write its standard big-endian digest bytes.
Definition sha256fst.c:262
static uint32_t schedule_sigma_one(uint32_t value)
Lowercase sigma-1 for expansion of the message schedule.
Definition sha256fst.c:72
Allocation-free, portable SHA-256 checksums for memory and streams.
#define SHA256_BLOCK_SIZE
Number of input bytes in one SHA-256 compression block.
Definition sha256fst.h:45
#define SHA256_HEX_SIZE
Required hexadecimal output capacity, including the trailing NUL.
Definition sha256fst.h:47
Caller-owned incremental checksum state; treat members as read-only.
Definition sha256fst.h:57
uint8_t buffer[SHA256_BLOCK_SIZE]
Definition sha256fst.h:59
uint32_t state[8]
Definition sha256fst.h:58
uint8_t buffer_counter
Definition sha256fst.h:61
uint64_t n_bits
Definition sha256fst.h:60
bool valid
Definition usher.c:537