ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

sha2.cpp File Reference
#include <string.h>
#include "sha2.h"
#include "brg_endian.h"
Include dependency graph for sha2.cpp:

Go to the source code of this file.

Macros

#define rotl32(x, n)   (((x) << n) | ((x) >> (32 - n)))
 
#define rotr32(x, n)   (((x) >> n) | ((x) << (32 - n)))
 
#define bswap_32(x)   ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
 
#define ch(x, y, z)   ((z) ^ ((x) & ((y) ^ (z))))
 
#define maj(x, y, z)   (((x) & (y)) | ((z) & ((x) ^ (y))))
 
#define vf(n, i)   v[(n - i) & 7]
 
#define hf(i)
 
#define v_cycle(i, j)
 
#define SHA256_MASK   (SHA256_BLOCK_SIZE - 1)
 
#define bsw_32(p, n)
 
#define s_0(x)   (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
 
#define s_1(x)   (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
 
#define g_0(x)   (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
 
#define g_1(x)   (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
 
#define k_0   k256
 
#define q(n)   v##n
 
#define one_cycle(a, b, c, d, e, f, g, h, k, w)
 
#define SHA512_MASK   (SHA512_BLOCK_SIZE - 1)
 
#define rotr64(x, n)   (((x) >> n) | ((x) << (64 - n)))
 
#define bswap_64(x)   (((uint_64t)(bswap_32((uint_32t)(x)))) << 32 | bswap_32((uint_32t)((x) >> 32)))
 
#define bsw_64(p, n)
 
#define s_0(x)   (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))
 
#define s_1(x)   (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))
 
#define g_0(x)   (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))
 
#define g_1(x)   (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))
 
#define k_0   k512
 
#define CTX_224(x)   ((x)->uu->ctx256)
 
#define CTX_256(x)   ((x)->uu->ctx256)
 
#define CTX_384(x)   ((x)->uu->ctx512)
 
#define CTX_512(x)   ((x)->uu->ctx512)
 

Functions

void sha256_compile (sha256_ctx ctx[1])
 
void sha256_hash (const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
 
static void sha_end1 (unsigned char hval[], sha256_ctx ctx[1], const unsigned int hlen)
 
void sha224_begin (sha224_ctx ctx[1])
 
void sha224_end (unsigned char hval[], sha224_ctx ctx[1])
 
void sha224 (unsigned char hval[], const unsigned char data[], unsigned long len)
 
void sha256_begin (sha256_ctx ctx[1])
 
void sha256_end (unsigned char hval[], sha256_ctx ctx[1])
 
void sha256 (unsigned char hval[], const unsigned char data[], unsigned long len)
 
void sha512_compile (sha512_ctx ctx[1])
 
void sha512_hash (const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
 
static void sha_end2 (unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
 
void sha384_begin (sha384_ctx ctx[1])
 
void sha384_end (unsigned char hval[], sha384_ctx ctx[1])
 
void sha384 (unsigned char hval[], const unsigned char data[], unsigned long len)
 
void sha512_begin (sha512_ctx ctx[1])
 
void sha512_end (unsigned char hval[], sha512_ctx ctx[1])
 
void sha512 (unsigned char hval[], const unsigned char data[], unsigned long len)
 
int sha2_begin (unsigned long len, sha2_ctx ctx[1])
 
void sha2_hash (const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
 
void sha2_end (unsigned char hval[], sha2_ctx ctx[1])
 
int sha2 (unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len)
 

Variables

const uint_32t k256 [64]
 
const uint_32t i224 [8]
 
const uint_32t i256 [8]
 
const uint_64t k512 [80]
 
const uint_64t i384 [80]
 
const uint_64t i512 [80]
 

Macro Definition Documentation

◆ bsw_32

#define bsw_32 (   p,
 
)

Definition at line 146 of file sha2.cpp.

◆ bsw_64

#define bsw_64 (   p,
 
)

Definition at line 443 of file sha2.cpp.

◆ bswap_32

#define bswap_32 (   x)    ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))

Definition at line 104 of file sha2.cpp.

◆ bswap_64

#define bswap_64 (   x)    (((uint_64t)(bswap_32((uint_32t)(x)))) << 32 | bswap_32((uint_32t)((x) >> 32)))

Definition at line 436 of file sha2.cpp.

◆ ch

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

Definition at line 120 of file sha2.cpp.

◆ CTX_224

#define CTX_224 (   x)    ((x)->uu->ctx256)

Definition at line 680 of file sha2.cpp.

◆ CTX_256

#define CTX_256 (   x)    ((x)->uu->ctx256)

Definition at line 681 of file sha2.cpp.

◆ CTX_384

#define CTX_384 (   x)    ((x)->uu->ctx512)

Definition at line 682 of file sha2.cpp.

◆ CTX_512

#define CTX_512 (   x)    ((x)->uu->ctx512)

Definition at line 683 of file sha2.cpp.

◆ g_0 [1/2]

#define g_0 (   x)    (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))

Definition at line 458 of file sha2.cpp.

◆ g_0 [2/2]

#define g_0 (   x)    (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))

Definition at line 458 of file sha2.cpp.

◆ g_1 [1/2]

#define g_1 (   x)    (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))

Definition at line 459 of file sha2.cpp.

◆ g_1 [2/2]

#define g_1 (   x)    (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))

Definition at line 459 of file sha2.cpp.

◆ hf

#define hf (   i)
Value:
(p[i & 15] += \
g_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g_0(p[(i + 1) & 15]))

Definition at line 129 of file sha2.cpp.

◆ k_0 [1/2]

#define k_0   k256

Definition at line 460 of file sha2.cpp.

◆ k_0 [2/2]

#define k_0   k512

Definition at line 460 of file sha2.cpp.

◆ maj

#define maj (   x,
  y,
 
)    (((x) & (y)) | ((z) & ((x) ^ (y))))

Definition at line 121 of file sha2.cpp.

◆ one_cycle

#define one_cycle (   a,
  b,
  c,
  d,
  e,
  f,
  g,
  h,
  k,
 
)
Value:
q(h) += s_1(q(e)) + ch(q(e), q(f), q(g)) + k + w; \
q(d) += q(h); q(h) += s_0(q(a)) + maj(q(a), q(b), q(c))

Definition at line 161 of file sha2.cpp.

◆ q

#define q (   n)    v##n

Definition at line 159 of file sha2.cpp.

◆ rotl32

#define rotl32 (   x,
 
)    (((x) << n) | ((x) >> (32 - n)))

Definition at line 99 of file sha2.cpp.

◆ rotr32

#define rotr32 (   x,
 
)    (((x) >> n) | ((x) << (32 - n)))

Definition at line 100 of file sha2.cpp.

◆ rotr64

#define rotr64 (   x,
 
)    (((x) >> n) | ((x) << (64 - n)))

Definition at line 433 of file sha2.cpp.

◆ s_0 [1/2]

#define s_0 (   x)    (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))

Definition at line 456 of file sha2.cpp.

◆ s_0 [2/2]

#define s_0 (   x)    (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))

Definition at line 456 of file sha2.cpp.

◆ s_1 [1/2]

#define s_1 (   x)    (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))

Definition at line 457 of file sha2.cpp.

◆ s_1 [2/2]

#define s_1 (   x)    (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))

Definition at line 457 of file sha2.cpp.

◆ SHA256_MASK

#define SHA256_MASK   (SHA256_BLOCK_SIZE - 1)

Definition at line 140 of file sha2.cpp.

◆ SHA512_MASK

#define SHA512_MASK   (SHA512_BLOCK_SIZE - 1)

Definition at line 431 of file sha2.cpp.

◆ v_cycle

#define v_cycle (   i,
 
)
Value:
vf(7,i) += (j ? hf(i) : p[i]) + k_0[i+j] \
+ s_1(vf(4,i)) + ch(vf(4,i),vf(5,i),vf(6,i)); \
vf(3,i) += vf(7,i); \
vf(7,i) += s_0(vf(0,i))+ maj(vf(0,i),vf(1,i),vf(2,i))

Definition at line 132 of file sha2.cpp.

◆ vf

#define vf (   n,
 
)    v[(n - i) & 7]

Definition at line 127 of file sha2.cpp.

Function Documentation

◆ sha2()

int sha2 ( unsigned char  hval[],
unsigned long  size,
const unsigned char  data[],
unsigned long  len 
)

Definition at line 757 of file sha2.cpp.

References sha2_begin(), sha2_end(), and sha2_hash().

◆ sha224()

void sha224 ( unsigned char  hval[],
const unsigned char  data[],
unsigned long  len 
)

Definition at line 390 of file sha2.cpp.

References sha224_begin(), SHA224_DIGEST_SIZE, sha224_hash, and sha_end1().

◆ sha224_begin()

void sha224_begin ( sha224_ctx  ctx[1])

Definition at line 379 of file sha2.cpp.

References sha256_ctx::count, sha256_ctx::hash, and i224.

Referenced by sha224().

◆ sha224_end()

void sha224_end ( unsigned char  hval[],
sha224_ctx  ctx[1] 
)

Definition at line 385 of file sha2.cpp.

References SHA224_DIGEST_SIZE, and sha_end1().

◆ sha256()

void sha256 ( unsigned char  hval[],
const unsigned char  data[],
unsigned long  len 
)

Definition at line 419 of file sha2.cpp.

References sha256_begin(), SHA256_DIGEST_SIZE, sha256_hash(), and sha_end1().

◆ sha256_begin()

void sha256_begin ( sha256_ctx  ctx[1])

Definition at line 408 of file sha2.cpp.

References sha256_ctx::count, sha256_ctx::hash, and i256.

Referenced by hmacSha256Init(), and sha256().

◆ sha256_compile()

void sha256_compile ( sha256_ctx  ctx[1])

Definition at line 192 of file sha2.cpp.

References sha256_ctx::hash, hf, k256, one_cycle, v_cycle, and sha256_ctx::wbuf.

Referenced by sha256_hash(), and sha_end1().

◆ sha256_end()

void sha256_end ( unsigned char  hval[],
sha256_ctx  ctx[1] 
)

Definition at line 414 of file sha2.cpp.

References SHA256_DIGEST_SIZE, and sha_end1().

Referenced by hmacSha256Final(), and hmacSha256Init().

◆ sha256_hash()

void sha256_hash ( const unsigned char  data[],
unsigned long  len,
sha256_ctx  ctx[1] 
)

◆ sha2_begin()

int sha2_begin ( unsigned long  len,
sha2_ctx  ctx[1] 
)

Definition at line 687 of file sha2.cpp.

References CTX_256, CTX_384, CTX_512, hash, i224, i256, i384, i512, and sha2_ctx::sha2_len.

Referenced by sha2().

◆ sha2_end()

void sha2_end ( unsigned char  hval[],
sha2_ctx  ctx[1] 
)

◆ sha2_hash()

void sha2_hash ( const unsigned char  data[],
unsigned long  len,
sha2_ctx  ctx[1] 
)

Definition at line 719 of file sha2.cpp.

References CTX_224, CTX_256, CTX_384, CTX_512, sha224_hash, sha256_hash(), sha2_ctx::sha2_len, sha384_hash, and sha512_hash().

Referenced by sha2().

◆ sha384()

void sha384 ( unsigned char  hval[],
const unsigned char  data[],
unsigned long  len 
)

Definition at line 635 of file sha2.cpp.

References sha384_begin(), SHA384_DIGEST_SIZE, sha384_hash, and sha_end2().

◆ sha384_begin()

void sha384_begin ( sha384_ctx  ctx[1])

Definition at line 624 of file sha2.cpp.

References sha512_ctx::count, sha512_ctx::hash, and i384.

Referenced by hmacSha384Init(), and sha384().

◆ sha384_end()

void sha384_end ( unsigned char  hval[],
sha384_ctx  ctx[1] 
)

Definition at line 630 of file sha2.cpp.

References SHA384_DIGEST_SIZE, and sha_end2().

Referenced by hmacSha384Final(), and hmacSha384Init().

◆ sha512()

void sha512 ( unsigned char  hval[],
const unsigned char  data[],
unsigned long  len 
)

Definition at line 668 of file sha2.cpp.

References sha512_begin(), SHA512_DIGEST_SIZE, sha512_hash(), and sha_end2().

◆ sha512_begin()

void sha512_begin ( sha512_ctx  ctx[1])

Definition at line 657 of file sha2.cpp.

References sha512_ctx::count, sha512_ctx::hash, and i512.

Referenced by sha512().

◆ sha512_compile()

void sha512_compile ( sha512_ctx  ctx[1])

Definition at line 514 of file sha2.cpp.

References sha512_ctx::hash, v_cycle, and sha512_ctx::wbuf.

Referenced by sha512_hash(), and sha_end2().

◆ sha512_end()

void sha512_end ( unsigned char  hval[],
sha512_ctx  ctx[1] 
)

Definition at line 663 of file sha2.cpp.

References SHA512_DIGEST_SIZE, and sha_end2().

◆ sha512_hash()

void sha512_hash ( const unsigned char  data[],
unsigned long  len,
sha512_ctx  ctx[1] 
)

Definition at line 545 of file sha2.cpp.

References bsw_64, sha512_ctx::count, SHA512_BLOCK_SIZE, sha512_compile(), SHA512_MASK, and sha512_ctx::wbuf.

Referenced by sha2_hash(), and sha512().

◆ sha_end1()

static void sha_end1 ( unsigned char  hval[],
sha256_ctx  ctx[1],
const unsigned int  hlen 
)
static

◆ sha_end2()

static void sha_end2 ( unsigned char  hval[],
sha512_ctx  ctx[1],
const unsigned int  hlen 
)
static

Variable Documentation

◆ i224

const uint_32t i224[8]
Initial value:
=
{
0xc1059ed8ul, 0x367cd507ul, 0x3070dd17ul, 0xf70e5939ul,
0xffc00b31ul, 0x68581511ul, 0x64f98fa7ul, 0xbefa4fa4ul
}

Definition at line 373 of file sha2.cpp.

Referenced by sha224_begin(), and sha2_begin().

◆ i256

const uint_32t i256[8]
Initial value:
=
{
0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul
}

Definition at line 402 of file sha2.cpp.

Referenced by sha256_begin(), and sha2_begin().

◆ i384

const uint_64t i384[80]
Initial value:
=
{
li_64(cbbb9d5dc1059ed8), li_64(629a292a367cd507),
li_64(9159015a3070dd17), li_64(152fecd8f70e5939),
li_64(67332667ffc00b31), li_64(8eb44a8768581511),
li_64(db0c2e0d64f98fa7), li_64(47b5481dbefa4fa4)
}

Definition at line 616 of file sha2.cpp.

Referenced by sha2_begin(), and sha384_begin().

◆ i512

const uint_64t i512[80]
Initial value:
=
{
li_64(6a09e667f3bcc908), li_64(bb67ae8584caa73b),
li_64(3c6ef372fe94f82b), li_64(a54ff53a5f1d36f1),
li_64(510e527fade682d1), li_64(9b05688c2b3e6c1f),
li_64(1f83d9abfb41bd6b), li_64(5be0cd19137e2179)
}

Definition at line 649 of file sha2.cpp.

Referenced by sha2_begin(), and sha512_begin().

◆ k256

const uint_32t k256[64]
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 167 of file sha2.cpp.

Referenced by sha256_compile().

◆ k512

const uint_64t k512[80]

Definition at line 464 of file sha2.cpp.

g_0
#define g_0(x)
Definition: sha2.cpp:458
q
#define q(n)
Definition: sha2.cpp:159
k_0
#define k_0
Definition: sha2.cpp:460
s_0
#define s_0(x)
Definition: sha2.cpp:456
ch
#define ch(x, y, z)
Definition: sha2.cpp:120
s_1
#define s_1(x)
Definition: sha2.cpp:457
vf
#define vf(n, i)
Definition: sha2.cpp:127
hf
#define hf(i)
Definition: sha2.cpp:129
maj
#define maj(x, y, z)
Definition: sha2.cpp:121