Skip to content

File syn_sha256.c

FileList > src > syntropic > util > syn_sha256.c

Go to the source code of this file

SHA-256 implementation — FIPS 180-4 compliant, pure C99. More...

  • #include "syn_assert.h"
  • #include "syn_pack.h"
  • #include "syn_sha256.h"
  • #include <string.h>

Public Static Attributes

Type Name
const uint32_t H_INIT = /* multi line expression */
const uint32_t K = /* multi line expression */

Public Functions

Type Name
void syn_sha256_final (SYN_SHA256 * ctx, uint8_t hash)
Finalize the hash and produce the 32-byte digest.
void syn_sha256_init (SYN_SHA256 * ctx)
Initialize a SHA-256 context.
void syn_sha256_update (SYN_SHA256 * ctx, const void * data, size_t len)
Feed data into the hash.

Public Static Functions

Type Name
uint32_t ch (uint32_t x, uint32_t y, uint32_t z)
Choice function: Ch(x,y,z) = (x ∧ y) ⊕ (¬x ∧ z).
uint32_t gamma0 (uint32_t x)
Small sigma-0: σ₀(x) = ROTR⁷(x) ⊕ ROTR¹⁸(x) ⊕ SHR³(x).
uint32_t gamma1 (uint32_t x)
Small sigma-1: σ₁(x) = ROTR¹⁷(x) ⊕ ROTR¹⁹(x) ⊕ SHR¹⁰(x).
uint32_t maj (uint32_t x, uint32_t y, uint32_t z)
Majority function: Maj(x,y,z) = (x ∧ y) ⊕ (x ∧ z) ⊕ (y ∧ z).
uint32_t rotr (uint32_t x, unsigned n)
Circular right rotate.
void sha256_transform (uint32_t state, const uint8_t block)
Process a single 64-byte block.
uint32_t sigma0 (uint32_t x)
Big Sigma-0: Σ₀(x) = ROTR²(x) ⊕ ROTR¹³(x) ⊕ ROTR²²(x).
uint32_t sigma1 (uint32_t x)
Big Sigma-1: Σ₁(x) = ROTR⁶(x) ⊕ ROTR¹¹(x) ⊕ ROTR²⁵(x).

Detailed Description

No heap allocation, no floating point, no external dependencies. Static K[64] round constants table consumes ~256 bytes of ROM.

Public Static Attributes Documentation

variable H_INIT

const uint32_t H_INIT[8];

FIPS 180-4 initial hash values (H0–H7).


variable K

const uint32_t K[64];

FIPS 180-4 round constants (K0–K63).


Public Functions Documentation

function syn_sha256_final

Finalize the hash and produce the 32-byte digest.

void syn_sha256_final (
    SYN_SHA256 * ctx,
    uint8_t hash
) 

After calling this, the context must be re-initialized before reuse.

Parameters:

  • ctx SHA-256 context.
  • hash Output buffer (must be at least 32 bytes).

function syn_sha256_init

Initialize a SHA-256 context.

void syn_sha256_init (
    SYN_SHA256 * ctx
) 

Parameters:

  • ctx Context to initialize.

function syn_sha256_update

Feed data into the hash.

void syn_sha256_update (
    SYN_SHA256 * ctx,
    const void * data,
    size_t len
) 

Can be called repeatedly with arbitrary chunk sizes.

Parameters:

  • ctx SHA-256 context.
  • data Data to hash.
  • len Length in bytes.

Public Static Functions Documentation

function ch

Choice function: Ch(x,y,z) = (x ∧ y) ⊕ (¬x ∧ z).

static inline uint32_t ch (
    uint32_t x,
    uint32_t y,
    uint32_t z
) 

Parameters:

  • x First input.
  • y Second input.
  • z Third input.

Returns:

Ch(x, y, z).


function gamma0

Small sigma-0: σ₀(x) = ROTR⁷(x) ⊕ ROTR¹⁸(x) ⊕ SHR³(x).

static inline uint32_t gamma0 (
    uint32_t x
) 

Parameters:

  • x Input word.

Returns:

σ₀(x).


function gamma1

Small sigma-1: σ₁(x) = ROTR¹⁷(x) ⊕ ROTR¹⁹(x) ⊕ SHR¹⁰(x).

static inline uint32_t gamma1 (
    uint32_t x
) 

Parameters:

  • x Input word.

Returns:

σ₁(x).


function maj

Majority function: Maj(x,y,z) = (x ∧ y) ⊕ (x ∧ z) ⊕ (y ∧ z).

static inline uint32_t maj (
    uint32_t x,
    uint32_t y,
    uint32_t z
) 

Parameters:

  • x First input.
  • y Second input.
  • z Third input.

Returns:

Maj(x, y, z).


function rotr

Circular right rotate.

static inline uint32_t rotr (
    uint32_t x,
    unsigned n
) 

Parameters:

  • x Value to rotate.
  • n Number of bits to rotate.

Returns:

Rotated value.


function sha256_transform

Process a single 64-byte block.

static void sha256_transform (
    uint32_t state,
    const uint8_t block
) 

Parameters:

  • state Running hash state (8 × uint32_t).
  • block 64-byte input block.

function sigma0

Big Sigma-0: Σ₀(x) = ROTR²(x) ⊕ ROTR¹³(x) ⊕ ROTR²²(x).

static inline uint32_t sigma0 (
    uint32_t x
) 

Parameters:

  • x Input word.

Returns:

Σ₀(x).


function sigma1

Big Sigma-1: Σ₁(x) = ROTR⁶(x) ⊕ ROTR¹¹(x) ⊕ ROTR²⁵(x).

static inline uint32_t sigma1 (
    uint32_t x
) 

Parameters:

  • x Input word.

Returns:

Σ₁(x).



The documentation for this class was generated from the following file src/syntropic/util/syn_sha256.c