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

Go to the source code of this file.

Macros

#define UL64(x)   x##ULL
 
#define GET_UINT64_BE(n, b, i)
 
#define PUT_UINT64_BE(n, b, i)
 
#define SHR(x, n)   (x >> n)
 
#define ROTR(x, n)   (SHR(x,n) | (x << (64 - n)))
 
#define S0(x)   (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
 
#define S1(x)   (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))
 
#define S2(x)   (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
 
#define S3(x)   (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
 
#define F0(x, y, z)   ((x & y) | (z & (x | y)))
 
#define F1(x, y, z)   (z ^ (x & (y ^ z)))
 
#define P(a, b, c, d, e, f, g, h, x, K)
 

Functions

static void mbedtls_zeroize (void *v, size_t n)
 
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_process (mbedtls_sha512_context *ctx, const unsigned char data[128])
 
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 )
 

Variables

static const uint64_t K [80]
 
static const unsigned char sha512_padding [128]
 

Macro Definition Documentation

◆ F0

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

◆ F1

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

◆ GET_UINT64_BE

#define GET_UINT64_BE (   n,
  b,
  i 
)
Value:
{ \
(n) = ( (uint64_t) (b)[(i) ] << 56 ) \
| ( (uint64_t) (b)[(i) + 1] << 48 ) \
| ( (uint64_t) (b)[(i) + 2] << 40 ) \
| ( (uint64_t) (b)[(i) + 3] << 32 ) \
| ( (uint64_t) (b)[(i) + 4] << 24 ) \
| ( (uint64_t) (b)[(i) + 5] << 16 ) \
| ( (uint64_t) (b)[(i) + 6] << 8 ) \
| ( (uint64_t) (b)[(i) + 7] ); \
}
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 67 of file sha512.cxx.

68{ \
69 (n) = ( (uint64_t) (b)[(i) ] << 56 ) \
70 | ( (uint64_t) (b)[(i) + 1] << 48 ) \
71 | ( (uint64_t) (b)[(i) + 2] << 40 ) \
72 | ( (uint64_t) (b)[(i) + 3] << 32 ) \
73 | ( (uint64_t) (b)[(i) + 4] << 24 ) \
74 | ( (uint64_t) (b)[(i) + 5] << 16 ) \
75 | ( (uint64_t) (b)[(i) + 6] << 8 ) \
76 | ( (uint64_t) (b)[(i) + 7] ); \
77}

◆ 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)
#define S2(x)
#define F0(x, y, z)
static const uint64_t K[80]
Definition sha512.cxx:97
#define S3(x)
double d
Definition system.cxx:1311
char c
Definition system.cxx:1310
static double e(void)
Definition tinyexpr.c:136

◆ PUT_UINT64_BE

#define PUT_UINT64_BE (   n,
  b,
  i 
)
Value:
{ \
(b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
(b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
(b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 7] = (unsigned char) ( (n) ); \
}

Definition at line 81 of file sha512.cxx.

82{ \
83 (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
84 (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
85 (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
86 (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
87 (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
88 (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
89 (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
90 (b)[(i) + 7] = (unsigned char) ( (n) ); \
91}

◆ ROTR

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

◆ S0

#define S0 (   x)    (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))

◆ S1

#define S1 (   x)    (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6))

◆ S2

#define S2 (   x)    (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))

◆ S3

#define S3 (   x)    (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))

◆ SHR

#define SHR (   x,
  n 
)    (x >> n)

◆ UL64

#define UL64 (   x)    x##ULL

Definition at line 42 of file sha512.cxx.

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
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
#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
Here is the call graph for this function:
Here is the caller graph for this function:

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

◆ mbedtls_zeroize()

static void mbedtls_zeroize ( void v,
size_t  n 
)
static

Definition at line 57 of file sha512.cxx.

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

Variable Documentation

◆ K

const uint64_t K[80]
static

Definition at line 97 of file sha512.cxx.

98{
99 UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD),
100 UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC),
101 UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019),
102 UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118),
103 UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE),
104 UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2),
105 UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1),
106 UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694),
107 UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3),
108 UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65),
109 UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483),
110 UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5),
111 UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210),
112 UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4),
113 UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725),
114 UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70),
115 UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926),
116 UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF),
117 UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8),
118 UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B),
119 UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001),
120 UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30),
121 UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910),
122 UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8),
123 UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53),
124 UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8),
125 UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB),
126 UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3),
127 UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60),
128 UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC),
129 UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9),
130 UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B),
131 UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207),
132 UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178),
133 UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6),
134 UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B),
135 UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493),
136 UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C),
137 UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A),
138 UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
139};

◆ sha512_padding

const unsigned char sha512_padding[128]
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,
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, 0
}

Definition at line 307 of file sha512.cxx.

308{
309 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
311 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
312 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
313 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
314 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
315 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
316 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
317};