Skip to content

Commit db799de

Browse files
committed
crypto: streaming blake2b for validation
exposes the internals of the streaming blake2b checksum and integrates the cryptography code with the standard fake-vtable mechanism for file i/o in Limine, allowing for validation of the checksum without reading the whole file into memory in one shot and caching it until closed.
1 parent 42eb5f9 commit db799de

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

common/crypt/blake2b.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdint.h>
66
#include <stddef.h>
77
#include <crypt/blake2b.h>
8+
#include <fs/file.h>
89
#include <lib/libc.h>
910

1011
#define BLAKE2B_BLOCK_BYTES 128
@@ -218,3 +219,25 @@ void blake2b(void *out, const void *in, size_t in_len) {
218219
blake2b_update(&state, in, in_len);
219220
blake2b_final(&state, out);
220221
}
222+
223+
bool blake2b_verify_file(struct file_handle *fd, const uint8_t expected[BLAKE2B_OUT_BYTES]) {
224+
uint8_t out_buf[BLAKE2B_OUT_BYTES];
225+
226+
if (fd->is_memfile) {
227+
blake2b(out_buf, fd->fd, fd->size);
228+
return memcmp(out_buf, expected, BLAKE2B_OUT_BYTES) == 0;
229+
}
230+
231+
struct blake2b_state state;
232+
blake2b_init(&state);
233+
char chunk_buf[4096];
234+
235+
for (uint64_t r = fd->size, off = 0, sz; r > 0; off += sz, r -= sz) {
236+
fd->read(fd, chunk_buf, off, sz = r < 4096 ? r : 4096);
237+
blake2b_update(&state, chunk_buf, sz);
238+
}
239+
240+
blake2b_final(&state, out_buf);
241+
242+
return memcmp(out_buf, expected, BLAKE2B_OUT_BYTES) == 0;
243+
}

common/crypt/blake2b.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77

88
void blake2b(void *out, const void *in, size_t in_len);
99

10+
struct file_handle;
11+
bool blake2b_verify_file(struct file_handle *fd, const uint8_t expected[BLAKE2B_OUT_BYTES]);
12+
1013
#endif

common/lib/uri.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,13 @@ struct file_handle *uri_open(char *uri) {
257257
}
258258

259259
if (hash != NULL && ret != NULL) {
260-
uint8_t out_buf[BLAKE2B_OUT_BYTES];
261-
#if defined (UEFI) && defined (__x86_64__)
262-
void *file_buf = freadall_mode(ret, MEMMAP_BOOTLOADER_RECLAIMABLE, true);
263-
#else
264-
void *file_buf = freadall(ret, MEMMAP_BOOTLOADER_RECLAIMABLE);
265-
#endif
266-
blake2b(out_buf, file_buf, ret->size);
267260
uint8_t hash_buf[BLAKE2B_OUT_BYTES];
268261

269262
for (size_t i = 0; i < sizeof(hash_buf); i++) {
270263
hash_buf[i] = digit_to_int(hash[i * 2]) << 4 | digit_to_int(hash[i * 2 + 1]);
271264
}
272265

273-
if (memcmp(hash_buf, out_buf, sizeof(out_buf)) != 0) {
266+
if (!blake2b_verify_file(ret, hash_buf)) {
274267
if (hash_mismatch_panic) {
275268
panic(true, "Blake2b hash for URI `%#` does not match!", uri);
276269
} else {

0 commit comments

Comments
 (0)