MIDAS
Loading...
Searching...
No Matches
sha256.cxx File Reference
#include "sha256.h"
#include <string.h>
Include dependency graph for sha256.cxx:

Go to the source code of this file.

Macros

#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

static void mbedtls_zeroize (void *v, size_t n)
 
void mbedtls_sha256_init (mbedtls_sha256_context *ctx)
 Initialize SHA-256 context.
 
void mbedtls_sha256_free (mbedtls_sha256_context *ctx)
 Clear SHA-256 context.
 
void mbedtls_sha256_clone (mbedtls_sha256_context *dst, const mbedtls_sha256_context *src)
 Clone (the state of) a SHA-256 context.
 
void mbedtls_sha256_starts (mbedtls_sha256_context *ctx, int is224)
 SHA-256 context setup.
 
void mbedtls_sha256_process (mbedtls_sha256_context *ctx, const unsigned char data[64])
 
void mbedtls_sha256_update (mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen)
 SHA-256 process buffer.
 
void mbedtls_sha256_finish (mbedtls_sha256_context *ctx, unsigned char output[32])
 SHA-256 final digest.
 
void mbedtls_sha256 (const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
 Output = SHA-256( input buffer )
 

Variables

static const uint32_t K []
 
static const unsigned char sha256_padding [64]
 

Macro Definition Documentation

◆ F0

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

Definition at line 165 of file sha256.cxx.

◆ F1

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

Definition at line 166 of file sha256.cxx.

◆ GET_UINT32_BE

#define GET_UINT32_BE (   n,
  b,
  i 
)
Value:
do { \
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
| ( (uint32_t) (b)[(i) + 3] ); \
} while( 0 )
DWORD n[4]
Definition mana.cxx:247
INT i
Definition mdump.cxx:32
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24

Definition at line 61 of file sha256.cxx.

62 { \
63 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
64 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
65 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
66 | ( (uint32_t) (b)[(i) + 3] ); \
67} while( 0 )

◆ P

#define P (   a,
  b,
  c,
  d,
  e,
  f,
  g,
  h,
  x,
  K 
)
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)
Definition sha256.cxx:166
#define S2(x)
Definition sha256.cxx:162
#define F0(x, y, z)
Definition sha256.cxx:165
static const uint32_t K[]
Definition sha256.cxx:136
#define S3(x)
Definition sha256.cxx:163
double d
Definition system.cxx:1311
char c
Definition system.cxx:1310
static double e(void)
Definition tinyexpr.c:136

Definition at line 174 of file sha256.cxx.

175{ \
176 temp1 = h + S3(e) + F1(e,f,g) + K + x; \
177 temp2 = S2(a) + F0(a,b,c); \
178 d += temp1; h = temp1 + temp2; \
179}

◆ PUT_UINT32_BE

#define PUT_UINT32_BE (   n,
  b,
  i 
)
Value:
do { \
(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) ); \
} while( 0 )

Definition at line 71 of file sha256.cxx.

72 { \
73 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
74 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
75 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
76 (b)[(i) + 3] = (unsigned char) ( (n) ); \
77} while( 0 )

◆ R

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

Definition at line 168 of file sha256.cxx.

◆ ROTR

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

Definition at line 157 of file sha256.cxx.

◆ S0

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

Definition at line 159 of file sha256.cxx.

◆ S1

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

Definition at line 160 of file sha256.cxx.

◆ S2

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

Definition at line 162 of file sha256.cxx.

◆ S3

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

Definition at line 163 of file sha256.cxx.

◆ SHR

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

Definition at line 156 of file sha256.cxx.

Function Documentation

◆ mbedtls_sha256()

void mbedtls_sha256 ( const unsigned char input,
size_t  ilen,
unsigned char  output[32],
int  is224 
)

Output = SHA-256( input buffer )

Parameters
inputbuffer holding the data
ilenlength of the input data
outputSHA-224/256 checksum result
is2240 = use SHA256, 1 = use SHA224

Definition at line 325 of file sha256.cxx.

327{
329
330 mbedtls_sha256_init( &ctx );
331 mbedtls_sha256_starts( &ctx, is224 );
334 mbedtls_sha256_free( &ctx );
335}
static void output(code_int code)
Definition mgd.cxx:1647
void mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char output[32])
SHA-256 final digest.
Definition sha256.cxx:289
void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen)
SHA-256 process buffer.
Definition sha256.cxx:240
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
Clear SHA-256 context.
Definition sha256.cxx:85
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
Initialize SHA-256 context.
Definition sha256.cxx:80
void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
SHA-256 context setup.
Definition sha256.cxx:102
SHA-256 context structure.
Definition sha256.h:48
Here is the call graph for this function:

◆ mbedtls_sha256_clone()

void mbedtls_sha256_clone ( mbedtls_sha256_context dst,
const mbedtls_sha256_context src 
)

Clone (the state of) a SHA-256 context.

Parameters
dstThe destination context
srcThe context to be cloned

Definition at line 93 of file sha256.cxx.

95{
96 *dst = *src;
97}

◆ mbedtls_sha256_finish()

void mbedtls_sha256_finish ( mbedtls_sha256_context ctx,
unsigned char  output[32] 
)

SHA-256 final digest.

Parameters
ctxSHA-256 context
outputSHA-224/256 checksum result

Definition at line 289 of file sha256.cxx.

290{
293 unsigned char msglen[8];
294
295 high = ( ctx->total[0] >> 29 )
296 | ( ctx->total[1] << 3 );
297 low = ( ctx->total[0] << 3 );
298
300 PUT_UINT32_BE( low, msglen, 4 );
301
302 last = ctx->total[0] & 0x3F;
303 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
304
306 mbedtls_sha256_update( ctx, msglen, 8 );
307
308 PUT_UINT32_BE( ctx->state[0], output, 0 );
309 PUT_UINT32_BE( ctx->state[1], output, 4 );
310 PUT_UINT32_BE( ctx->state[2], output, 8 );
311 PUT_UINT32_BE( ctx->state[3], output, 12 );
312 PUT_UINT32_BE( ctx->state[4], output, 16 );
313 PUT_UINT32_BE( ctx->state[5], output, 20 );
314 PUT_UINT32_BE( ctx->state[6], output, 24 );
315
316 if( ctx->is224 == 0 )
317 PUT_UINT32_BE( ctx->state[7], output, 28 );
318}
#define PUT_UINT32_BE(n, b, i)
Definition sha256.cxx:71
static const unsigned char sha256_padding[64]
Definition sha256.cxx:278
uint32_t state[8]
Definition sha256.h:50
uint32_t total[2]
Definition sha256.h:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha256_free()

void mbedtls_sha256_free ( mbedtls_sha256_context ctx)

Clear SHA-256 context.

Parameters
ctxSHA-256 context to be cleared

Definition at line 85 of file sha256.cxx.

86{
87 if( ctx == NULL )
88 return;
89
91}
static void mbedtls_zeroize(void *v, size_t n)
Definition sha256.cxx:51
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha256_init()

void mbedtls_sha256_init ( mbedtls_sha256_context ctx)

Initialize SHA-256 context.

Parameters
ctxSHA-256 context to be initialized

Definition at line 80 of file sha256.cxx.

81{
82 memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
83}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha256_process()

void mbedtls_sha256_process ( mbedtls_sha256_context ctx,
const unsigned char  data[64] 
)

Definition at line 181 of file sha256.cxx.

182{
183 uint32_t temp1, temp2, W[64];
184 uint32_t A[8];
185 unsigned int i;
186
187 for( i = 0; i < 8; i++ )
188 A[i] = ctx->state[i];
189
190#if defined(MBEDTLS_SHA256_SMALLER)
191 for( i = 0; i < 64; i++ )
192 {
193 if( i < 16 )
194 GET_UINT32_BE( W[i], data, 4 * i );
195 else
196 R( i );
197
198 P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i] );
199
200 temp1 = A[7]; A[7] = A[6]; A[6] = A[5]; A[5] = A[4]; A[4] = A[3];
201 A[3] = A[2]; A[2] = A[1]; A[1] = A[0]; A[0] = temp1;
202 }
203#else /* MBEDTLS_SHA256_SMALLER */
204 for( i = 0; i < 16; i++ )
205 GET_UINT32_BE( W[i], data, 4 * i );
206
207 for( i = 0; i < 16; i += 8 )
208 {
209 P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i+0], K[i+0] );
210 P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i+1], K[i+1] );
211 P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i+2], K[i+2] );
212 P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i+3], K[i+3] );
213 P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i+4], K[i+4] );
214 P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i+5], K[i+5] );
215 P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i+6], K[i+6] );
216 P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i+7], K[i+7] );
217 }
218
219 for( i = 16; i < 64; i += 8 )
220 {
221 P( A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i+0), K[i+0] );
222 P( A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i+1), K[i+1] );
223 P( A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i+2), K[i+2] );
224 P( A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i+3), K[i+3] );
225 P( A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i+4), K[i+4] );
226 P( A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i+5), K[i+5] );
227 P( A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i+6), K[i+6] );
228 P( A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i+7), K[i+7] );
229 }
230#endif /* MBEDTLS_SHA256_SMALLER */
231
232 for( i = 0; i < 8; i++ )
233 ctx->state[i] += A[i];
234}
Definition test_init.cxx:9
void * data
Definition mana.cxx:268
#define R(t)
Definition sha256.cxx:168
#define GET_UINT32_BE(n, b, i)
Definition sha256.cxx:61
#define P(a, b, c, d, e, f, g, h, x, K)
Definition sha256.cxx:174
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha256_starts()

void mbedtls_sha256_starts ( mbedtls_sha256_context ctx,
int  is224 
)

SHA-256 context setup.

Parameters
ctxcontext to be initialized
is2240 = use SHA256, 1 = use SHA224

Definition at line 102 of file sha256.cxx.

103{
104 ctx->total[0] = 0;
105 ctx->total[1] = 0;
106
107 if( is224 == 0 )
108 {
109 /* SHA-256 */
110 ctx->state[0] = 0x6A09E667;
111 ctx->state[1] = 0xBB67AE85;
112 ctx->state[2] = 0x3C6EF372;
113 ctx->state[3] = 0xA54FF53A;
114 ctx->state[4] = 0x510E527F;
115 ctx->state[5] = 0x9B05688C;
116 ctx->state[6] = 0x1F83D9AB;
117 ctx->state[7] = 0x5BE0CD19;
118 }
119 else
120 {
121 /* SHA-224 */
122 ctx->state[0] = 0xC1059ED8;
123 ctx->state[1] = 0x367CD507;
124 ctx->state[2] = 0x3070DD17;
125 ctx->state[3] = 0xF70E5939;
126 ctx->state[4] = 0xFFC00B31;
127 ctx->state[5] = 0x68581511;
128 ctx->state[6] = 0x64F98FA7;
129 ctx->state[7] = 0xBEFA4FA4;
130 }
131
132 ctx->is224 = is224;
133}
Here is the caller graph for this function:

◆ mbedtls_sha256_update()

void mbedtls_sha256_update ( mbedtls_sha256_context ctx,
const unsigned char input,
size_t  ilen 
)

SHA-256 process buffer.

Parameters
ctxSHA-256 context
inputbuffer holding the data
ilenlength of the input data

Definition at line 240 of file sha256.cxx.

242{
243 size_t fill;
245
246 if( ilen == 0 )
247 return;
248
249 left = ctx->total[0] & 0x3F;
250 fill = 64 - left;
251
252 ctx->total[0] += (uint32_t) ilen;
253 ctx->total[0] &= 0xFFFFFFFF;
254
255 if( ctx->total[0] < (uint32_t) ilen )
256 ctx->total[1]++;
257
258 if( left && ilen >= fill )
259 {
260 memcpy( (void *) (ctx->buffer + left), input, fill );
261 mbedtls_sha256_process( ctx, ctx->buffer );
262 input += fill;
263 ilen -= fill;
264 left = 0;
265 }
266
267 while( ilen >= 64 )
268 {
270 input += 64;
271 ilen -= 64;
272 }
273
274 if( ilen > 0 )
275 memcpy( (void *) (ctx->buffer + left), input, ilen );
276}
static int left(const struct frozen *f)
void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64])
Definition sha256.cxx:181
unsigned char buffer[64]
Definition sha256.h:51
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_zeroize()

static void mbedtls_zeroize ( void v,
size_t  n 
)
static

Definition at line 51 of file sha256.cxx.

51 {
52 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
53}
Here is the caller graph for this function:

Variable Documentation

◆ K

const uint32_t K[]
static
Initial value:
=
{
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
}

Definition at line 136 of file sha256.cxx.

137{
138 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
139 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
140 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
141 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
142 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
143 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
144 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
145 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
146 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
147 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
148 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
149 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
150 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
151 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
152 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
153 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
154};

◆ sha256_padding

const unsigned char sha256_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
}

Definition at line 278 of file sha256.cxx.

279{
280 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
284};