MIDAS
Loading...
Searching...
No Matches
sha256.h File Reference
#include <stddef.h>
#include <stdint.h>
Include dependency graph for sha256.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  mbedtls_sha256_context
 SHA-256 context structure. More...
 

Functions

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_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_process (mbedtls_sha256_context *ctx, const unsigned char data[64])
 
void mbedtls_sha256 (const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
 Output = SHA-256( input buffer )
 
int mbedtls_sha256_self_test (int verbose)
 Checkup routine.
 

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
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
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
INT i
Definition mdump.cxx:32
#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
static const uint32_t K[]
Definition sha256.cxx:136
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha256_self_test()

int mbedtls_sha256_self_test ( int  verbose)

Checkup routine.

Returns
0 if successful, or 1 if the test failed

◆ 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: