InChI
 
Loading...
Searching...
No Matches
sha2.c File Reference
#include <string.h>
#include <stdio.h>
#include "sha2.h"
#include "mode.h"
#include "bcf_s.h"
+ Include dependency graph for sha2.c:

Macros

#define _CRT_SECURE_NO_DEPRECATE   1
 
#define GET_UINT32_BE(n, b, i)
 
#define PUT_UINT32_BE(n, b, i)
 
#define SHR(x, n)   ((x & 0xFFFFFFFF) >> n)
 
#define ROTR(x, n)   (SHR(x, n) | (x << (32 - n)))
 
#define S0(x)   (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
 
#define S1(x)   (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
 
#define S2(x)   (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
 
#define S3(x)   (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
 
#define F0(x, y, z)   ((x & y) | (z & (x | y)))
 
#define F1(x, y, z)   (z ^ (x & (y ^ z)))
 
#define R(t)
 
#define P(a, b, c, d, e, f, g, h, x, K)
 

Functions

void sha2_starts (sha2_context *ctx)
 SHA-256 context setup.
 
static void sha2_process (sha2_context *ctx, unsigned char data[64])
 
void sha2_update (sha2_context *ctx, unsigned char *input, int ilen)
 SHA-256 process buffer.
 
void sha2_finish (sha2_context *ctx, unsigned char output[32])
 SHA-256 final digest.
 
int sha2_file (char *path, unsigned char output[32])
 Output = SHA-256( file contents )
 
void sha2_csum (unsigned char *input, int ilen, unsigned char output[32])
 Output = SHA-256( input buffer )
 
void sha2_hmac (unsigned char *key, int keylen, unsigned char *input, int ilen, unsigned char output[32])
 Output = HMAC-SHA-256( input buffer, hmac key )
 
int sha2_self_test (void)
 Checkup routine.
 

Variables

static const unsigned char sha2_padding [64]
 

Macro Definition Documentation

◆ _CRT_SECURE_NO_DEPRECATE

#define _CRT_SECURE_NO_DEPRECATE   1

◆ F0

#define F0 (   x,
  y,
 
)    ((x & y) | (z & (x | y)))

◆ F1

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

◆ GET_UINT32_BE

#define GET_UINT32_BE (   n,
  b,
 
)
Value:
{ \
(n) = ((unsigned long)(b)[(i)] << 24) | ((unsigned long)(b)[(i) + 1] << 16) | ((unsigned long)(b)[(i) + 2] << 8) | ((unsigned long)(b)[(i) + 3]); \
}

◆ P

#define P (   a,
  b,
  c,
  d,
  e,
  f,
  g,
  h,
  x,
 
)
Value:
{ \
temp1 = h + S3(e) + F1(e, f, g) + K + x; \
temp2 = S2(a) + F0(a, b, c); \
d += temp1; \
h = temp1 + temp2; \
}
#define F1(x, y, z)
#define S2(x)
#define F0(x, y, z)
#define S3(x)

◆ PUT_UINT32_BE

#define PUT_UINT32_BE (   n,
  b,
 
)
Value:
{ \
(b)[(i)] = (unsigned char)((n) >> 24); \
(b)[(i) + 1] = (unsigned char)((n) >> 16); \
(b)[(i) + 2] = (unsigned char)((n) >> 8); \
(b)[(i) + 3] = (unsigned char)((n)); \
}

◆ R

#define R (   t)
Value:
( \
W[t] = S1(W[t - 2]) + W[t - 7] + \
S0(W[t - 15]) + W[t - 16])
#define S1(x)

◆ ROTR

#define ROTR (   x,
 
)    (SHR(x, n) | (x << (32 - n)))

◆ S0

#define S0 (   x)    (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))

◆ S1

#define S1 (   x)    (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))

◆ S2

#define S2 (   x)    (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))

◆ S3

#define S3 (   x)    (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))

◆ SHR

#define SHR (   x,
 
)    ((x & 0xFFFFFFFF) >> n)

Function Documentation

◆ sha2_csum()

void sha2_csum ( unsigned char *  input,
int  ilen,
unsigned char  output[32] 
)

Output = SHA-256( input buffer )

Parameters
inputbuffer holding the data
ilenlength of the input data
outputSHA-256 checksum result
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sha2_file()

int sha2_file ( char *  path,
unsigned char  output[32] 
)

Output = SHA-256( file contents )

Parameters
pathinput file name
outputSHA-256 checksum result
Returns
0 if successful, or 1 if fopen failed
+ Here is the call graph for this function:

◆ sha2_finish()

void sha2_finish ( sha2_context ctx,
unsigned char  output[32] 
)

SHA-256 final digest.

Parameters
ctxSHA-256 context
outputSHA-256 checksum result
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sha2_hmac()

void sha2_hmac ( unsigned char *  key,
int  keylen,
unsigned char *  input,
int  ilen,
unsigned char  output[32] 
)

Output = HMAC-SHA-256( input buffer, hmac key )

Parameters
keyHMAC secret key
keylenlength of the HMAC key
inputbuffer holding the data
ilenlength of the input data
outputHMAC-SHA-256 result
+ Here is the call graph for this function:

◆ sha2_process()

static void sha2_process ( sha2_context ctx,
unsigned char  data[64] 
)
static
+ Here is the caller graph for this function:

◆ sha2_self_test()

int sha2_self_test ( void  )

Checkup routine.

Returns
0 if successful, or 1 if the test failed

◆ sha2_starts()

void sha2_starts ( sha2_context ctx)

SHA-256 context setup.

Parameters
ctxSHA-256 context to be initialized
+ Here is the caller graph for this function:

◆ sha2_update()

void sha2_update ( sha2_context ctx,
unsigned char *  input,
int  ilen 
)

SHA-256 process buffer.

Parameters
ctxSHA-256 context
inputbuffer holding the data
ilenlength of the input data
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ sha2_padding

const unsigned char sha2_padding[64]
static
Initial value:
=
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}