MentOS
0.8.0
The Mentoring Operating System
|
Implementation of the SHA-256 hashing algorithm. More...
Macros | |
#define | __DEBUG_HEADER__ "[SHA256]" |
Change header. | |
#define | __DEBUG_LEVEL__ LOGLEVEL_NOTICE |
Set log level. | |
#define | ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b)))) |
Rotate left operation on a 32-bit unsigned integer. More... | |
#define | ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b)))) |
Rotate right operation on a 32-bit unsigned integer. More... | |
#define | CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) |
Chooses bits from y if x is set, otherwise from z. More... | |
#define | MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
Majority function used in SHA-256. More... | |
#define | EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22)) |
First expansion function for the working variables. More... | |
#define | EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25)) |
Second expansion function for the working variables. More... | |
#define | SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3)) |
First Sigma function for message scheduling. More... | |
#define | SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10)) |
Second Sigma function for message scheduling. More... | |
#define | SHA256_MAX_DATA_LENGTH (SHA256_BLOCK_SIZE * 2) |
Max data length for message scheduling expansion. | |
Functions | |
static void | __sha256_transform (SHA256_ctx_t *ctx, const uint8_t data[]) |
Transforms the state of the SHA-256 context based on a block of input data. More... | |
void | sha256_bytes_to_hex (uint8_t *src, size_t src_length, char *out, size_t out_length) |
Converts a byte array into its hexadecimal string representation. More... | |
void | sha256_init (SHA256_ctx_t *ctx) |
Initializes the SHA-256 context. More... | |
void | sha256_update (SHA256_ctx_t *ctx, const uint8_t data[], size_t len) |
Adds data to the SHA-256 context for hashing. More... | |
void | sha256_final (SHA256_ctx_t *ctx, uint8_t hash[]) |
Finalizes the hashing and produces the final SHA-256 digest. More... | |
Variables | |
static const uint32_t | k [SHA256_MAX_DATA_LENGTH] |
The constants used in the SHA-256 algorithm, as defined by the specification. These are the first 32 bits of the fractional parts of the cube roots of the first 64 primes (2..311). More... | |
Implementation of the SHA-256 hashing algorithm.
The original code was written by Brad Conte, and is available at: https://github.com/B-Con/crypto-algorithms
SHA-256 is one of the three algorithms in the SHA2 specification. The others, SHA-384 and SHA-512, are not offered in this implementation. Algorithm specification can be found here: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf This implementation uses little endian byte order.
#define CH | ( | x, | |
y, | |||
z | |||
) | (((x) & (y)) ^ (~(x) & (z))) |
Chooses bits from y if x is set, otherwise from z.
x,y,z | Input values. |
First expansion function for the working variables.
x | Input value. |
Second expansion function for the working variables.
x | Input value. |
#define MAJ | ( | x, | |
y, | |||
z | |||
) | (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
Majority function used in SHA-256.
x,y,z | Input values. |
#define ROTLEFT | ( | a, | |
b | |||
) | (((a) << (b)) | ((a) >> (32 - (b)))) |
Rotate left operation on a 32-bit unsigned integer.
a | The value to rotate. |
b | The number of positions to rotate. |
#define ROTRIGHT | ( | a, | |
b | |||
) | (((a) >> (b)) | ((a) << (32 - (b)))) |
Rotate right operation on a 32-bit unsigned integer.
a | The value to rotate. |
b | The number of positions to rotate. |
First Sigma function for message scheduling.
x | Input value. |
Second Sigma function for message scheduling.
x | Input value. |
|
inlinestatic |
Transforms the state of the SHA-256 context based on a block of input data.
ctx | Pointer to the SHA-256 context. Must not be NULL. |
data | Input data block to process (64 bytes). Must not be NULL. |
Converts a byte array into its hexadecimal string representation.
Converts a byte array to its hexadecimal string representation.
src | Pointer to the source byte array. |
src_length | Length of the source byte array. |
out | Pointer to the output buffer for the hexadecimal string. |
out_length | Length of the output buffer (must be at least 2 * src_length + 1). |
The output string will be null-terminated if the buffer is large enough.
void sha256_final | ( | SHA256_ctx_t * | ctx, |
uint8_t | hash[] | ||
) |
Finalizes the hashing and produces the final SHA-256 digest.
ctx | Pointer to the SHA-256 context. |
hash | Pointer to a buffer where the final hash will be stored (must be at least 32 bytes long). |
void sha256_init | ( | SHA256_ctx_t * | ctx | ) |
Initializes the SHA-256 context.
ctx | Pointer to the SHA-256 context to initialize. |
void sha256_update | ( | SHA256_ctx_t * | ctx, |
const uint8_t | data[], | ||
size_t | len | ||
) |
Adds data to the SHA-256 context for hashing.
ctx | Pointer to the SHA-256 context. |
data | Pointer to the data to be hashed. |
len | Length of the data to hash, in bytes. |
|
static |
The constants used in the SHA-256 algorithm, as defined by the specification. These are the first 32 bits of the fractional parts of the cube roots of the first 64 primes (2..311).