| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <stddef.h> |
| 5 |
#include <stdint.h> |
| 6 |
|
| 7 |
#define SHA256_DIGEST_SIZE 32 |
| 8 |
|
| 9 |
struct sha256_ctx { |
| 10 |
uint32_t H[8]; |
| 11 |
|
| 12 |
union { |
| 13 |
uint64_t total64; |
| 14 |
#define TOTAL64_low (1 - (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) |
| 15 |
#define TOTAL64_high (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 16 |
uint32_t total[2]; |
| 17 |
}; |
| 18 |
|
| 19 |
uint32_t buflen; |
| 20 |
|
| 21 |
union { |
| 22 |
uint8_t buffer[128]; /* NB: always correctly aligned for UINT32. */ |
| 23 |
uint32_t buffer32[32]; |
| 24 |
uint64_t buffer64[16]; |
| 25 |
}; |
| 26 |
}; |
| 27 |
|
| 28 |
void sha256_init_ctx(struct sha256_ctx *ctx); |
| 29 |
uint8_t *sha256_finish_ctx(struct sha256_ctx *ctx, uint8_t resbuf[static SHA256_DIGEST_SIZE]); |
| 30 |
void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx); |
| 31 |
|
| 32 |
static inline void sha256_process_bytes_and_size(const void *buffer, size_t len, struct sha256_ctx *ctx) { |
| 33 |
sha256_process_bytes(&len, sizeof(len), ctx); |
| 34 |
sha256_process_bytes(buffer, len, ctx); |
| 35 |
} |
| 36 |
|
| 37 |
uint8_t* sha256_direct(const void *buffer, size_t sz, uint8_t result[static SHA256_DIGEST_SIZE]); |
| 38 |
|
| 39 |
#define SHA256_DIRECT(buffer, sz) sha256_direct(buffer, sz, (uint8_t[SHA256_DIGEST_SIZE]) {}) |