GNU libmicrohttpd 1.0.0
Loading...
Searching...
No Matches
sha256.c File Reference

Calculation of SHA-256 digest as defined in FIPS PUB 180-4 (2015) More...

#include "sha256.h"
#include <string.h>
#include "mhd_bithelpers.h"
#include "mhd_assert.h"
Include dependency graph for sha256.c:

Go to the source code of this file.

Macros

#define Ch(x, y, z)   ( (z) ^ ((x) & ((y) ^ (z))) )
 
#define Maj(x, y, z)   ( ((x) & (y)) ^ ((z) & ((x) ^ (y))) )
 
#define SIG0(x)
 
#define SIG1(x)
 
#define sig0(x)
 
#define sig1(x)
 
#define SHA2STEP32(vA, vB, vC, vD, vE, vF, vG, vH, kt, wt)
 
#define GET_W_FROM_DATA(buf, t)
 
#define Wgen(w, t)
 
#define SHA256_SIZE_OF_LEN_ADD   (64 / 8)
 

Functions

void MHD_SHA256_init (struct Sha256Ctx *ctx)
 
static MHD_DATA_TRUNCATION_RUNTIME_CHECK_DISABLE_ void sha256_transform (uint32_t H[SHA256_DIGEST_SIZE_WORDS], const void *data)
 
void MHD_SHA256_update (struct Sha256Ctx *ctx, const uint8_t *data, size_t length)
 
void MHD_SHA256_finish (struct Sha256Ctx *ctx, uint8_t digest[SHA256_DIGEST_SIZE])
 

Detailed Description

Calculation of SHA-256 digest as defined in FIPS PUB 180-4 (2015)

Author
Karlson2k (Evgeny Grin)

Definition in file sha256.c.

Macro Definition Documentation

◆ Ch

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

◆ GET_W_FROM_DATA

#define GET_W_FROM_DATA (   buf,
 
)
Value:
_MHD_GET_32BIT_BE ((const void*)(((const uint8_t*) (buf)) + \
#define _MHD_GET_32BIT_BE(addr)
#define SHA256_BYTES_IN_WORD
Definition: sha256.h:50

◆ Maj

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

◆ SHA256_SIZE_OF_LEN_ADD

#define SHA256_SIZE_OF_LEN_ADD   (64 / 8)

Size of "length" padding addition in bytes. See FIPS PUB 180-4 paragraph 5.1.1.

Definition at line 464 of file sha256.c.

◆ SHA2STEP32

#define SHA2STEP32 (   vA,
  vB,
  vC,
  vD,
  vE,
  vF,
  vG,
  vH,
  kt,
  wt 
)
Value:
do { \
(vD) += ((vH) += SIG1 ((vE)) + Ch ((vE),(vF),(vG)) + (kt) + (wt)); \
(vH) += SIG0 ((vA)) + Maj ((vA),(vB),(vC)); } while (0)
#define SIG0(x)
#define Maj(x, y, z)
#define SIG1(x)
#define Ch(x, y, z)

◆ SIG0

#define SIG0 (   x)
Value:
(_MHD_ROTR32 ((x), 2) ^ _MHD_ROTR32 ((x), 13) ^ \
_MHD_ROTR32 ((x), 22) )
_MHD_static_inline uint32_t _MHD_ROTR32(uint32_t value32, int bits)

◆ sig0

#define sig0 (   x)
Value:
(_MHD_ROTR32 ((x), 7) ^ _MHD_ROTR32 ((x), 18) ^ \
((x) >> 3) )

◆ SIG1

#define SIG1 (   x)
Value:
(_MHD_ROTR32 ((x), 6) ^ _MHD_ROTR32 ((x), 11) ^ \
_MHD_ROTR32 ((x), 25) )

◆ sig1

#define sig1 (   x)
Value:
(_MHD_ROTR32 ((x), 17) ^ _MHD_ROTR32 ((x),19) ^ \
((x) >> 10) )

◆ Wgen

#define Wgen (   w,
 
)
Value:
( (w)[(t - 16) & 0xf] + sig1 ((w)[((t) - 2) & 0xf]) \
+ (w)[((t) - 7) & 0xf] + sig0 ((w)[((t) - 15) & 0xf]) )
#define sig1(x)
#define sig0(x)

Function Documentation

◆ MHD_SHA256_finish()

void MHD_SHA256_finish ( struct Sha256Ctx ctx,
uint8_t  digest[SHA256_DIGEST_SIZE] 
)

Finalise SHA256 calculation, return digest.

Parameters
ctx_must be a struct Sha256Ctx *
[out]digestset to the hash, must be SHA256_DIGEST_SIZE bytes

< Number of processed bits

< Number of bytes in buffer

Definition at line 473 of file sha256.c.

References _MHD_PUT_32BIT_BE, _MHD_PUT_64BIT_BE_SAFE(), _MHD_UINT32_ALIGN, Sha256Ctx::buffer, Sha256Ctx::count, Sha256Ctx::H, SHA256_BLOCK_SIZE, SHA256_BLOCK_SIZE_WORDS, SHA256_BYTES_IN_WORD, SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE_WORDS, SHA256_SIZE_OF_LEN_ADD, and sha256_transform().

Referenced by digest_calc_hash().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MHD_SHA256_init()

void MHD_SHA256_init ( struct Sha256Ctx ctx)

Initialise structure for SHA256 calculation.

Parameters
ctxmust be a struct Sha256Ctx *

Definition at line 41 of file sha256.c.

References Sha256Ctx::count, and Sha256Ctx::H.

◆ MHD_SHA256_update()

void MHD_SHA256_update ( struct Sha256Ctx ctx,
const uint8_t *  data,
size_t  length 
)

Process portion of bytes.

Parameters
ctx_must be a struct Sha256Ctx *
databytes to add to hash
lengthnumber of bytes in data

< Number of bytes in buffer

Definition at line 410 of file sha256.c.

References Sha256Ctx::buffer, Sha256Ctx::count, data, Sha256Ctx::H, mhd_assert, NULL, SHA256_BLOCK_SIZE, and sha256_transform().

Referenced by digest_update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sha256_transform()

static MHD_DATA_TRUNCATION_RUNTIME_CHECK_DISABLE_ void sha256_transform ( uint32_t  H[SHA256_DIGEST_SIZE_WORDS],
const void *  data 
)
static

Base of SHA-256 transformation. Gets full 64 bytes block of data and updates hash values;

Parameters
Hhash values
datadata, must be exactly 64 bytes long

Definition at line 70 of file sha256.c.

References _MHD_UINT32_ALIGN, data, GET_W_FROM_DATA, SHA256_BLOCK_SIZE, SHA2STEP32, and Wgen.

Referenced by MHD_SHA256_finish(), and MHD_SHA256_update().

Here is the caller graph for this function: