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

Go to the source code of this file.

Classes

struct  mbedtls_sha512_context
 SHA-512 context structure. More...
 

Functions

void mbedtls_sha512_init (mbedtls_sha512_context *ctx)
 Initialize SHA-512 context.
 
void mbedtls_sha512_free (mbedtls_sha512_context *ctx)
 Clear SHA-512 context.
 
void mbedtls_sha512_clone (mbedtls_sha512_context *dst, const mbedtls_sha512_context *src)
 Clone (the state of) a SHA-512 context.
 
void mbedtls_sha512_starts (mbedtls_sha512_context *ctx, int is384)
 SHA-512 context setup.
 
void mbedtls_sha512_update (mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
 SHA-512 process buffer.
 
void mbedtls_sha512_finish (mbedtls_sha512_context *ctx, unsigned char output[64])
 SHA-512 final digest.
 
void mbedtls_sha512 (const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
 Output = SHA-512( input buffer )
 
int mbedtls_sha512_self_test (int verbose)
 Checkup routine.
 
void mbedtls_sha512_process (mbedtls_sha512_context *ctx, const unsigned char data[128])
 

Function Documentation

◆ mbedtls_sha512()

void mbedtls_sha512 ( const unsigned char input,
size_t  ilen,
unsigned char  output[64],
int  is384 
)

Output = SHA-512( input buffer )

Parameters
inputbuffer holding the data
ilenlength of the input data
outputSHA-384/512 checksum result
is3840 = use SHA512, 1 = use SHA384

Definition at line 360 of file sha512.cxx.

362{
364
365 mbedtls_sha512_init( &ctx );
366 mbedtls_sha512_starts( &ctx, is384 );
369 mbedtls_sha512_free( &ctx );
370}
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_sha512_starts(mbedtls_sha512_context *ctx, int is384)
SHA-512 context setup.
Definition sha512.cxx:163
void mbedtls_sha512_update(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
SHA-512 process buffer.
Definition sha512.cxx:270
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
Clear SHA-512 context.
Definition sha512.cxx:146
void mbedtls_sha512_finish(mbedtls_sha512_context *ctx, unsigned char output[64])
SHA-512 final digest.
Definition sha512.cxx:322
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
Initialize SHA-512 context.
Definition sha512.cxx:141
SHA-512 context structure.
Definition sha512.h:48
Here is the call graph for this function:

◆ mbedtls_sha512_clone()

void mbedtls_sha512_clone ( mbedtls_sha512_context dst,
const mbedtls_sha512_context src 
)

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

Parameters
dstThe destination context
srcThe context to be cloned

Definition at line 154 of file sha512.cxx.

156{
157 *dst = *src;
158}

◆ mbedtls_sha512_finish()

void mbedtls_sha512_finish ( mbedtls_sha512_context ctx,
unsigned char  output[64] 
)

SHA-512 final digest.

Parameters
ctxSHA-512 context
outputSHA-384/512 checksum result

Definition at line 322 of file sha512.cxx.

323{
324 size_t last, padn;
326 unsigned char msglen[16];
327
328 high = ( ctx->total[0] >> 61 )
329 | ( ctx->total[1] << 3 );
330 low = ( ctx->total[0] << 3 );
331
333 PUT_UINT64_BE( low, msglen, 8 );
334
335 last = (size_t)( ctx->total[0] & 0x7F );
336 padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last );
337
339 mbedtls_sha512_update( ctx, msglen, 16 );
340
341 PUT_UINT64_BE( ctx->state[0], output, 0 );
342 PUT_UINT64_BE( ctx->state[1], output, 8 );
343 PUT_UINT64_BE( ctx->state[2], output, 16 );
344 PUT_UINT64_BE( ctx->state[3], output, 24 );
345 PUT_UINT64_BE( ctx->state[4], output, 32 );
346 PUT_UINT64_BE( ctx->state[5], output, 40 );
347
348 if( ctx->is384 == 0 )
349 {
350 PUT_UINT64_BE( ctx->state[6], output, 48 );
351 PUT_UINT64_BE( ctx->state[7], output, 56 );
352 }
353}
static const unsigned char sha512_padding[128]
Definition sha512.cxx:307
#define PUT_UINT64_BE(n, b, i)
Definition sha512.cxx:81
uint64_t total[2]
Definition sha512.h:49
uint64_t state[8]
Definition sha512.h:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha512_free()

void mbedtls_sha512_free ( mbedtls_sha512_context ctx)

Clear SHA-512 context.

Parameters
ctxSHA-512 context to be cleared

Definition at line 146 of file sha512.cxx.

147{
148 if( ctx == NULL )
149 return;
150
151 mbedtls_zeroize( ctx, sizeof( mbedtls_sha512_context ) );
152}
static void mbedtls_zeroize(void *v, size_t n)
Definition sha512.cxx:57
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha512_init()

void mbedtls_sha512_init ( mbedtls_sha512_context ctx)

Initialize SHA-512 context.

Parameters
ctxSHA-512 context to be initialized

Definition at line 141 of file sha512.cxx.

142{
143 memset( ctx, 0, sizeof( mbedtls_sha512_context ) );
144}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha512_process()

void mbedtls_sha512_process ( mbedtls_sha512_context ctx,
const unsigned char  data[128] 
)

Definition at line 197 of file sha512.cxx.

198{
199 int i;
200 uint64_t temp1, temp2, W[80];
201 uint64_t A, B, C, D, E, F, G, H;
202
203#define SHR(x,n) (x >> n)
204#define ROTR(x,n) (SHR(x,n) | (x << (64 - n)))
205
206#define S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
207#define S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))
208
209#define S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
210#define S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
211
212#define F0(x,y,z) ((x & y) | (z & (x | y)))
213#define F1(x,y,z) (z ^ (x & (y ^ z)))
214
215#define P(a,b,c,d,e,f,g,h,x,K) \
216{ \
217 temp1 = h + S3(e) + F1(e,f,g) + K + x; \
218 temp2 = S2(a) + F0(a,b,c); \
219 d += temp1; h = temp1 + temp2; \
220}
221
222 for( i = 0; i < 16; i++ )
223 {
224 GET_UINT64_BE( W[i], data, i << 3 );
225 }
226
227 for( ; i < 80; i++ )
228 {
229 W[i] = S1(W[i - 2]) + W[i - 7] +
230 S0(W[i - 15]) + W[i - 16];
231 }
232
233 A = ctx->state[0];
234 B = ctx->state[1];
235 C = ctx->state[2];
236 D = ctx->state[3];
237 E = ctx->state[4];
238 F = ctx->state[5];
239 G = ctx->state[6];
240 H = ctx->state[7];
241 i = 0;
242
243 do
244 {
245 P( A, B, C, D, E, F, G, H, W[i], K[i] ); i++;
246 P( H, A, B, C, D, E, F, G, W[i], K[i] ); i++;
247 P( G, H, A, B, C, D, E, F, W[i], K[i] ); i++;
248 P( F, G, H, A, B, C, D, E, W[i], K[i] ); i++;
249 P( E, F, G, H, A, B, C, D, W[i], K[i] ); i++;
250 P( D, E, F, G, H, A, B, C, W[i], K[i] ); i++;
251 P( C, D, E, F, G, H, A, B, W[i], K[i] ); i++;
252 P( B, C, D, E, F, G, H, A, W[i], K[i] ); i++;
253 }
254 while( i < 80 );
255
256 ctx->state[0] += A;
257 ctx->state[1] += B;
258 ctx->state[2] += C;
259 ctx->state[3] += D;
260 ctx->state[4] += E;
261 ctx->state[5] += F;
262 ctx->state[6] += G;
263 ctx->state[7] += H;
264}
Definition test_init.cxx:9
void * data
Definition mana.cxx:268
INT i
Definition mdump.cxx:32
#define S0(x)
#define S1(x)
#define P(a, b, c, d, e, f, g, h, x, K)
#define GET_UINT64_BE(n, b, i)
Definition sha512.cxx:67
static const uint64_t K[80]
Definition sha512.cxx:97
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mbedtls_sha512_self_test()

int mbedtls_sha512_self_test ( int  verbose)

Checkup routine.

Returns
0 if successful, or 1 if the test failed

◆ mbedtls_sha512_starts()

void mbedtls_sha512_starts ( mbedtls_sha512_context ctx,
int  is384 
)

SHA-512 context setup.

Parameters
ctxcontext to be initialized
is3840 = use SHA512, 1 = use SHA384

Definition at line 163 of file sha512.cxx.

164{
165 ctx->total[0] = 0;
166 ctx->total[1] = 0;
167
168 if( is384 == 0 )
169 {
170 /* SHA-512 */
171 ctx->state[0] = UL64(0x6A09E667F3BCC908);
172 ctx->state[1] = UL64(0xBB67AE8584CAA73B);
173 ctx->state[2] = UL64(0x3C6EF372FE94F82B);
174 ctx->state[3] = UL64(0xA54FF53A5F1D36F1);
175 ctx->state[4] = UL64(0x510E527FADE682D1);
176 ctx->state[5] = UL64(0x9B05688C2B3E6C1F);
177 ctx->state[6] = UL64(0x1F83D9ABFB41BD6B);
178 ctx->state[7] = UL64(0x5BE0CD19137E2179);
179 }
180 else
181 {
182 /* SHA-384 */
183 ctx->state[0] = UL64(0xCBBB9D5DC1059ED8);
184 ctx->state[1] = UL64(0x629A292A367CD507);
185 ctx->state[2] = UL64(0x9159015A3070DD17);
186 ctx->state[3] = UL64(0x152FECD8F70E5939);
187 ctx->state[4] = UL64(0x67332667FFC00B31);
188 ctx->state[5] = UL64(0x8EB44A8768581511);
189 ctx->state[6] = UL64(0xDB0C2E0D64F98FA7);
190 ctx->state[7] = UL64(0x47B5481DBEFA4FA4);
191 }
192
193 ctx->is384 = is384;
194}
#define UL64(x)
Definition sha512.cxx:42
Here is the caller graph for this function:

◆ mbedtls_sha512_update()

void mbedtls_sha512_update ( mbedtls_sha512_context ctx,
const unsigned char input,
size_t  ilen 
)

SHA-512 process buffer.

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

Definition at line 270 of file sha512.cxx.

272{
273 size_t fill;
274 unsigned int left;
275
276 if( ilen == 0 )
277 return;
278
279 left = (unsigned int) (ctx->total[0] & 0x7F);
280 fill = 128 - left;
281
282 ctx->total[0] += (uint64_t) ilen;
283
284 if( ctx->total[0] < (uint64_t) ilen )
285 ctx->total[1]++;
286
287 if( left && ilen >= fill )
288 {
289 memcpy( (void *) (ctx->buffer + left), input, fill );
290 mbedtls_sha512_process( ctx, ctx->buffer );
291 input += fill;
292 ilen -= fill;
293 left = 0;
294 }
295
296 while( ilen >= 128 )
297 {
299 input += 128;
300 ilen -= 128;
301 }
302
303 if( ilen > 0 )
304 memcpy( (void *) (ctx->buffer + left), input, ilen );
305}
static int left(const struct frozen *f)
void mbedtls_sha512_process(mbedtls_sha512_context *ctx, const unsigned char data[128])
Definition sha512.cxx:197
unsigned char buffer[128]
Definition sha512.h:51
Here is the call graph for this function:
Here is the caller graph for this function: