GNU libmicrohttpd  0.9.63
sha256.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "sha256.h"
Include dependency graph for sha256.c:

Go to the source code of this file.

Macros

#define SHA256_DATA_LENGTH   16
 
#define Choice(x, y, z)   ( (z) ^ ( (x) & ( (y) ^ (z) ) ) )
 
#define Majority(x, y, z)   ( ((x) & (y)) ^ ((z) & ((x) ^ (y))) )
 
#define S0(x)   (ROTL32(30,(x)) ^ ROTL32(19,(x)) ^ ROTL32(10,(x)))
 
#define S1(x)   (ROTL32(26,(x)) ^ ROTL32(21,(x)) ^ ROTL32(7,(x)))
 
#define s0(x)   (ROTL32(25,(x)) ^ ROTL32(14,(x)) ^ ((x) >> 3))
 
#define s1(x)   (ROTL32(15,(x)) ^ ROTL32(13,(x)) ^ ((x) >> 10))
 
#define EXPAND(W, i)   ( W[(i) & 15 ] += (s1(W[((i)-2) & 15]) + W[((i)-7) & 15] + s0(W[((i)-15) & 15])) )
 
#define ROUND(a, b, c, d, e, f, g, h, k, data)
 
#define READ_UINT32(p)
 
#define WRITE_UINT32(p, i)
 
#define WRITE_UINT64(p, i)
 
#define ROTL32(n, x)   (((x)<<(n)) | ((x)>>((-(n)&31))))
 
#define COMPRESS(ctx, data)   (_nettle_sha256_compress((ctx)->state, (data), K))
 
#define MD_UPDATE(ctx, length, data, f, incr)
 
#define MD_PAD(ctx, size, f)
 

Functions

static void _nettle_sha256_compress (uint32_t *state, const uint8_t *input, const uint32_t *k)
 
void sha256_init (void *ctx_)
 
void sha256_update (void *ctx_, const uint8_t *data, size_t length)
 
void _nettle_write_be32 (size_t length, uint8_t *dst, const uint32_t *src)
 
static void sha256_write_digest (struct sha256_ctx *ctx, size_t length, uint8_t *digest)
 
void sha256_digest (void *ctx_, uint8_t *digest)
 

Variables

static const uint32_t K [64]
 

Macro Definition Documentation

◆ Choice

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

Definition at line 80 of file sha256.c.

◆ COMPRESS

#define COMPRESS (   ctx,
  data 
)    (_nettle_sha256_compress((ctx)->state, (data), K))

Definition at line 233 of file sha256.c.

Referenced by sha256_update(), and sha256_write_digest().

◆ EXPAND

#define EXPAND (   W,
 
)    ( W[(i) & 15 ] += (s1(W[((i)-2) & 15]) + W[((i)-7) & 15] + s0(W[((i)-15) & 15])) )

Definition at line 101 of file sha256.c.

Referenced by _nettle_sha256_compress().

◆ Majority

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

Definition at line 82 of file sha256.c.

◆ MD_PAD

#define MD_PAD (   ctx,
  size,
 
)
Value:
do { \
unsigned __md_i; \
__md_i = (ctx)->index; \
\
/* Set the first char of padding to 0x80. This is safe since there \
is always at least one byte free */ \
\
assert(__md_i < sizeof((ctx)->block)); \
(ctx)->block[__md_i++] = 0x80; \
\
if (__md_i > (sizeof((ctx)->block) - (size))) \
{ /* No room for length in this block. Process it and \
pad with another one */ \
memset((ctx)->block + __md_i, 0, sizeof((ctx)->block) - __md_i); \
\
f((ctx), (ctx)->block); \
__md_i = 0; \
} \
memset((ctx)->block + __md_i, 0, \
sizeof((ctx)->block) - (size) - __md_i); \
\
} while (0)

Definition at line 300 of file sha256.c.

Referenced by sha256_write_digest().

◆ MD_UPDATE

#define MD_UPDATE (   ctx,
  length,
  data,
  f,
  incr 
)

Definition at line 260 of file sha256.c.

Referenced by sha256_update().

◆ READ_UINT32

#define READ_UINT32 (   p)
Value:
( (((uint32_t) (p)[0]) << 24) \
| (((uint32_t) (p)[1]) << 16) \
| (((uint32_t) (p)[2]) << 8) \
| ((uint32_t) (p)[3]))

Definition at line 132 of file sha256.c.

Referenced by _nettle_sha256_compress().

◆ ROTL32

#define ROTL32 (   n,
 
)    (((x)<<(n)) | ((x)>>((-(n)&31))))

Definition at line 161 of file sha256.c.

◆ ROUND

#define ROUND (   a,
  b,
  c,
  d,
  e,
  f,
  g,
  h,
  k,
  data 
)
Value:
do { \
h += S1(e) + Choice(e,f,g) + k + data; \
d += h; \
h += S0(a) + Majority(a,b,c); \
} while (0)
void * data
Definition: microhttpd.h:2709
#define Choice(x, y, z)
Definition: sha256.c:80
#define S0(x)
Definition: sha256.c:84
#define Majority(x, y, z)
Definition: sha256.c:82
#define S1(x)
Definition: sha256.c:85

Definition at line 124 of file sha256.c.

Referenced by _nettle_sha256_compress().

◆ S0

#define S0 (   x)    (ROTL32(30,(x)) ^ ROTL32(19,(x)) ^ ROTL32(10,(x)))

Definition at line 84 of file sha256.c.

◆ s0

#define s0 (   x)    (ROTL32(25,(x)) ^ ROTL32(14,(x)) ^ ((x) >> 3))

Definition at line 87 of file sha256.c.

◆ S1

#define S1 (   x)    (ROTL32(26,(x)) ^ ROTL32(21,(x)) ^ ROTL32(7,(x)))

Definition at line 85 of file sha256.c.

◆ s1

#define s1 (   x)    (ROTL32(15,(x)) ^ ROTL32(13,(x)) ^ ((x) >> 10))

Definition at line 88 of file sha256.c.

◆ SHA256_DATA_LENGTH

#define SHA256_DATA_LENGTH   16

Definition at line 71 of file sha256.c.

Referenced by _nettle_sha256_compress().

◆ WRITE_UINT32

#define WRITE_UINT32 (   p,
 
)
Value:
do { \
(p)[0] = ((i) >> 24) & 0xff; \
(p)[1] = ((i) >> 16) & 0xff; \
(p)[2] = ((i) >> 8) & 0xff; \
(p)[3] = (i) & 0xff; \
} while(0)

Definition at line 138 of file sha256.c.

Referenced by _nettle_write_be32().

◆ WRITE_UINT64

#define WRITE_UINT64 (   p,
 
)
Value:
do { \
(p)[0] = ((i) >> 56) & 0xff; \
(p)[1] = ((i) >> 48) & 0xff; \
(p)[2] = ((i) >> 40) & 0xff; \
(p)[3] = ((i) >> 32) & 0xff; \
(p)[4] = ((i) >> 24) & 0xff; \
(p)[5] = ((i) >> 16) & 0xff; \
(p)[6] = ((i) >> 8) & 0xff; \
(p)[7] = (i) & 0xff; \
} while(0)

Definition at line 146 of file sha256.c.

Referenced by sha256_write_digest().

Function Documentation

◆ _nettle_sha256_compress()

static void _nettle_sha256_compress ( uint32_t *  state,
const uint8_t *  input,
const uint32_t *  k 
)
static

Definition at line 164 of file sha256.c.

References data, EXPAND, READ_UINT32, ROUND, and SHA256_DATA_LENGTH.

◆ _nettle_write_be32()

void _nettle_write_be32 ( size_t  length,
uint8_t *  dst,
const uint32_t *  src 
)

Definition at line 337 of file sha256.c.

References WRITE_UINT32.

Referenced by sha256_write_digest().

Here is the caller graph for this function:

◆ sha256_digest()

void sha256_digest ( void *  ctx_,
uint8_t *  digest 
)

Definition at line 398 of file sha256.c.

References SHA256_DIGEST_SIZE, sha256_init(), and sha256_write_digest().

Here is the call graph for this function:

◆ sha256_init()

void sha256_init ( void *  ctx_)

Start SHA256 calculation.

Parameters
ctx_must be a struct sha256_ctx *

Definition at line 238 of file sha256.c.

References _SHA256_DIGEST_LENGTH, sha256_ctx::count, sha256_ctx::index, and sha256_ctx::state.

Referenced by sha256_digest().

Here is the caller graph for this function:

◆ sha256_update()

void sha256_update ( void *  ctx_,
const uint8_t *  data,
size_t  length 
)

Update hash calculation.

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

Definition at line 326 of file sha256.c.

References COMPRESS, sha256_ctx::count, data, and MD_UPDATE.

◆ sha256_write_digest()

static void sha256_write_digest ( struct sha256_ctx ctx,
size_t  length,
uint8_t *  digest 
)
static

Definition at line 375 of file sha256.c.

References _nettle_write_be32(), sha256_ctx::block, COMPRESS, sha256_ctx::count, sha256_ctx::index, MD_PAD, SHA256_BLOCK_SIZE, SHA256_DIGEST_SIZE, sha256_ctx::state, and WRITE_UINT64.

Referenced by sha256_digest().

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

Variable Documentation

◆ K

const uint32_t K[64]
static
Initial value:
=
{
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL,
}

Definition at line 49 of file sha256.c.