The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
sha2.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file sha2.h
5 * @brief SHA-256 (FIPS 180-4) in steps, for data that does not fit in
6 * memory at one time. the names are the ones of the BSD sha2.h.
7 * ginnOS has SHA-256 only, not SHA-384 or SHA-512.
8 *
9 * usage:
10 *
11 * SHA2_CTX ctx;
12 * SHA256Init(&ctx);
13 * SHA256Update(&ctx, part, part_length); // as many times as needed
14 * SHA256Final(digest, &ctx);
15 *
16 * for data in one buffer, sha256() in crypt.h does the three steps.
17 */
18
19#include <stddef.h>
20#include <stdint.h>
21
22#define SHA256_BLOCK_LENGTH 64
23#define SHA256_DIGEST_LENGTH 32
24#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
25
26/** the state of a SHA-256 calculation. its fields are private. */
27typedef struct
28{
30 uint64_t length; // bytes added
32 size_t used; // bytes in block
33} SHA2_CTX;
34
35/** start a new calculation in ctx. */
37
38/** add length bytes of data. */
39void SHA256Update(SHA2_CTX *ctx, const void *data, size_t length);
40
41/**
42 * finish the calculation and write the digest. ctx must get SHA256Init
43 * again before it is used for new data.
44 */
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
NULL, size_t, ptrdiff_t, offsetof.
integer types of fixed width.
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
void SHA256Update(SHA2_CTX *ctx, const void *data, size_t length)
add length bytes of data.
#define SHA256_DIGEST_LENGTH
Definition sha2.h:23
void SHA256Init(SHA2_CTX *ctx)
start a new calculation in ctx.
void SHA256Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *ctx)
finish the calculation and write the digest.
#define SHA256_BLOCK_LENGTH
Definition sha2.h:22
the state of a SHA-256 calculation.
Definition sha2.h:28
uint32_t h[8]
Definition sha2.h:29
uint64_t length
Definition sha2.h:30
uint8_t block[SHA256_BLOCK_LENGTH]
Definition sha2.h:31
size_t used
Definition sha2.h:32