MentOS  0.8.0
The Mentoring Operating System
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Macros | Functions | Variables
sha256.c File Reference

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...
 

Detailed Description

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.

Macro Definition Documentation

◆ CH

#define CH (   x,
  y,
 
)    (((x) & (y)) ^ (~(x) & (z)))

Chooses bits from y if x is set, otherwise from z.

Parameters
x,y,zInput values.
Returns
Result of CH function.

◆ EP0

#define EP0 (   x)    (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22))

First expansion function for the working variables.

Parameters
xInput value.
Returns
Result of EP0.

◆ EP1

#define EP1 (   x)    (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25))

Second expansion function for the working variables.

Parameters
xInput value.
Returns
Result of EP1.

◆ MAJ

#define MAJ (   x,
  y,
 
)    (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))

Majority function used in SHA-256.

Parameters
x,y,zInput values.
Returns
Result of the majority function.

◆ ROTLEFT

#define ROTLEFT (   a,
 
)    (((a) << (b)) | ((a) >> (32 - (b))))

Rotate left operation on a 32-bit unsigned integer.

Parameters
aThe value to rotate.
bThe number of positions to rotate.
Returns
The rotated value.

◆ ROTRIGHT

#define ROTRIGHT (   a,
 
)    (((a) >> (b)) | ((a) << (32 - (b))))

Rotate right operation on a 32-bit unsigned integer.

Parameters
aThe value to rotate.
bThe number of positions to rotate.
Returns
The rotated value.

◆ SIG0

#define SIG0 (   x)    (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3))

First Sigma function for message scheduling.

Parameters
xInput value.
Returns
Result of SIG0.

◆ SIG1

#define SIG1 (   x)    (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10))

Second Sigma function for message scheduling.

Parameters
xInput value.
Returns
Result of SIG1.

Function Documentation

◆ __sha256_transform()

static void __sha256_transform ( SHA256_ctx_t ctx,
const uint8_t  data[] 
)
inlinestatic

Transforms the state of the SHA-256 context based on a block of input data.

Parameters
ctxPointer to the SHA-256 context. Must not be NULL.
dataInput data block to process (64 bytes). Must not be NULL.

◆ sha256_bytes_to_hex()

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.

Converts a byte array to its hexadecimal string representation.

Parameters
srcPointer to the source byte array.
src_lengthLength of the source byte array.
outPointer to the output buffer for the hexadecimal string.
out_lengthLength 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.

◆ sha256_final()

void sha256_final ( SHA256_ctx_t ctx,
uint8_t  hash[] 
)

Finalizes the hashing and produces the final SHA-256 digest.

Parameters
ctxPointer to the SHA-256 context.
hashPointer to a buffer where the final hash will be stored (must be at least 32 bytes long).

◆ sha256_init()

void sha256_init ( SHA256_ctx_t ctx)

Initializes the SHA-256 context.

Parameters
ctxPointer to the SHA-256 context to initialize.

◆ sha256_update()

void sha256_update ( SHA256_ctx_t ctx,
const uint8_t  data[],
size_t  len 
)

Adds data to the SHA-256 context for hashing.

Parameters
ctxPointer to the SHA-256 context.
dataPointer to the data to be hashed.
lenLength of the data to hash, in bytes.

Variable Documentation

◆ k

Initial value:
= {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
}

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).