ReactOS 0.4.15-dev-7958-gcd0bb1a
sha2.c File Reference
#include <config.h>
#include <string.h>
#include <assert.h>
#include "sha2.h"
Include dependency graph for sha2.c:

Go to the source code of this file.

Macros

#define SHA256_SHORT_BLOCK_LENGTH   (SHA256_BLOCK_LENGTH - 8)
 
#define SHA384_SHORT_BLOCK_LENGTH   (SHA384_BLOCK_LENGTH - 16)
 
#define SHA512_SHORT_BLOCK_LENGTH   (SHA512_BLOCK_LENGTH - 16)
 
#define SHA2_WORD64_CONST(dw1, dw2)   (((sha2_word64)(dw1) << 32) | (dw2))
 
#define REVERSE32(w, x)
 
#define REVERSE64(w, x)
 
#define ADDINC128(w, n)
 
#define SHA2_USE_MEMSET_MEMCPY   1
 
#define MEMSET_BZERO(p, l)   memset((p), 0, (l))
 
#define MEMCPY_BCOPY(d, s, l)   memcpy((d), (s), (l))
 
#define R(b, x)   ((x) >> (b))
 
#define S32(b, x)   (((x) >> (b)) | ((x) << (32 - (b))))
 
#define S64(b, x)   (((x) >> (b)) | ((x) << (64 - (b))))
 
#define Ch(x, y, z)   (((x) & (y)) ^ ((~(x)) & (z)))
 
#define Maj(x, y, z)   (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
 
#define Sigma0_256(x)   (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
 
#define Sigma1_256(x)   (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
 
#define sigma0_256(x)   (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
 
#define sigma1_256(x)   (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
 
#define Sigma0_512(x)   (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
 
#define Sigma1_512(x)   (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
 
#define sigma0_512(x)   (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
 
#define sigma1_512(x)   (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
 

Functions

void SHA512_Last (SHA512_CTX *)
 
void SHA256_Transform (SHA256_CTX *, const sha2_word32 *)
 
void SHA512_Transform (SHA512_CTX *, const sha2_word64 *)
 
void SHA256_Init (SHA256_CTX *context)
 
void SHA256_Update (SHA256_CTX *context, const sha2_byte *data, size_t len)
 
void SHA256_Final (sha2_byte digest[], SHA256_CTX *context)
 
charSHA256_End (SHA256_CTX *context, char buffer[])
 
charSHA256_Data (const sha2_byte *data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH])
 
void SHA512_Init (SHA512_CTX *context)
 
void SHA512_Update (SHA512_CTX *context, const sha2_byte *data, size_t len)
 
void SHA512_Final (sha2_byte digest[], SHA512_CTX *context)
 
charSHA512_End (SHA512_CTX *context, char buffer[])
 
charSHA512_Data (const sha2_byte *data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH])
 
void SHA384_Init (SHA384_CTX *context)
 
void SHA384_Update (SHA384_CTX *context, const sha2_byte *data, size_t len)
 
void SHA384_Final (sha2_byte digest[], SHA384_CTX *context)
 
charSHA384_End (SHA384_CTX *context, char buffer[])
 
charSHA384_Data (const sha2_byte *data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH])
 

Variables

static const sha2_word32 K256 [64]
 
static const sha2_word32 sha256_initial_hash_value [8]
 
static const sha2_word64 K512 [80]
 
static const sha2_word64 sha384_initial_hash_value [8]
 
static const sha2_word64 sha512_initial_hash_value [8]
 
static const char sha2_hex_digits [] = "0123456789abcdef"
 

Macro Definition Documentation

◆ ADDINC128

#define ADDINC128 (   w,
  n 
)
Value:
{ \
(w)[0] += (sha2_word64)(n); \
if ((w)[0] < (n)) { \
(w)[1]++; \
} \
}
GLdouble n
Definition: glext.h:7729
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
UINT64 sha2_word64
Definition: sha2.h:53

Definition at line 89 of file sha2.c.

◆ Ch

#define Ch (   x,
  y,
  z 
)    (((x) & (y)) ^ ((~(x)) & (z)))

Definition at line 141 of file sha2.c.

◆ Maj

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

Definition at line 142 of file sha2.c.

◆ MEMCPY_BCOPY

#define MEMCPY_BCOPY (   d,
  s,
  l 
)    memcpy((d), (s), (l))

Definition at line 116 of file sha2.c.

◆ MEMSET_BZERO

#define MEMSET_BZERO (   p,
  l 
)    memset((p), 0, (l))

Definition at line 115 of file sha2.c.

◆ R

#define R (   b,
  x 
)    ((x) >> (b))

Definition at line 134 of file sha2.c.

◆ REVERSE32

#define REVERSE32 (   w,
  x 
)
Value:
{ \
sha2_word32 tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xff00ff00) >> 8) | ((tmp & 0x00ff00ff) << 8); \
}
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
UINT32 sha2_word32
Definition: sha2.h:52

Definition at line 69 of file sha2.c.

◆ REVERSE64

#define REVERSE64 (   w,
  x 
)
Value:
{ \
sha2_word64 tmp = (w); \
tmp = (tmp >> 32) | (tmp << 32); \
tmp = ((tmp & SHA2_WORD64_CONST(0xff00ff00, 0xff00ff00)) >> 8) | \
((tmp & SHA2_WORD64_CONST(0x00ff00ff, 0x00ff00ff)) << 8); \
(x) = ((tmp & SHA2_WORD64_CONST(0xffff0000, 0xffff0000)) >> 16) | \
((tmp & SHA2_WORD64_CONST(0x0000ffff, 0x0000ffff)) << 16); \
}
#define SHA2_WORD64_CONST(dw1, dw2)
Definition: sha2.c:65

Definition at line 74 of file sha2.c.

◆ S32

#define S32 (   b,
  x 
)    (((x) >> (b)) | ((x) << (32 - (b))))

Definition at line 136 of file sha2.c.

◆ S64

#define S64 (   b,
  x 
)    (((x) >> (b)) | ((x) << (64 - (b))))

Definition at line 138 of file sha2.c.

◆ SHA256_SHORT_BLOCK_LENGTH

#define SHA256_SHORT_BLOCK_LENGTH   (SHA256_BLOCK_LENGTH - 8)

Definition at line 61 of file sha2.c.

◆ SHA2_USE_MEMSET_MEMCPY

#define SHA2_USE_MEMSET_MEMCPY   1

Definition at line 107 of file sha2.c.

◆ SHA2_WORD64_CONST

#define SHA2_WORD64_CONST (   dw1,
  dw2 
)    (((sha2_word64)(dw1) << 32) | (dw2))

Definition at line 65 of file sha2.c.

◆ SHA384_SHORT_BLOCK_LENGTH

#define SHA384_SHORT_BLOCK_LENGTH   (SHA384_BLOCK_LENGTH - 16)

Definition at line 62 of file sha2.c.

◆ SHA512_SHORT_BLOCK_LENGTH

#define SHA512_SHORT_BLOCK_LENGTH   (SHA512_BLOCK_LENGTH - 16)

Definition at line 63 of file sha2.c.

◆ Sigma0_256

#define Sigma0_256 (   x)    (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))

Definition at line 145 of file sha2.c.

◆ sigma0_256

#define sigma0_256 (   x)    (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))

Definition at line 147 of file sha2.c.

◆ Sigma0_512

#define Sigma0_512 (   x)    (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))

Definition at line 151 of file sha2.c.

◆ sigma0_512

#define sigma0_512 (   x)    (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))

Definition at line 153 of file sha2.c.

◆ Sigma1_256

#define Sigma1_256 (   x)    (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))

Definition at line 146 of file sha2.c.

◆ sigma1_256

#define sigma1_256 (   x)    (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))

Definition at line 148 of file sha2.c.

◆ Sigma1_512

#define Sigma1_512 (   x)    (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))

Definition at line 152 of file sha2.c.

◆ sigma1_512

#define sigma1_512 (   x)    (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))

Definition at line 154 of file sha2.c.

Function Documentation

◆ SHA256_Data()

char * SHA256_Data ( const sha2_byte data,
size_t  len,
char  digest[SHA256_DIGEST_STRING_LENGTH] 
)

Definition at line 593 of file sha2.c.

593 {
595
598 return SHA256_End(&context, digest);
599}
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLenum GLsizei len
Definition: glext.h:6722
void SHA256_Init(SHA256_CTX *context)
Definition: sha2.c:275
void SHA256_Update(SHA256_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:459
char * SHA256_End(SHA256_CTX *context, char buffer[])
Definition: sha2.c:570
Definition: http.c:7252

◆ SHA256_End()

char * SHA256_End ( SHA256_CTX context,
char  buffer[] 
)

Definition at line 570 of file sha2.c.

570 {
571 sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
572 int i;
573
574 /* Sanity check: */
575 assert(context != NULL);
576
577 if (buffer != NULL) {
578 SHA256_Final(digest, context);
579
580 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
581 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
582 *buffer++ = sha2_hex_digits[*d & 0x0f];
583 d++;
584 }
585 *buffer = 0;
586 } else {
587 MEMSET_BZERO(context, sizeof(*context));
588 }
590 return buffer;
591}
#define NULL
Definition: types.h:112
#define assert(x)
Definition: debug.h:53
GLuint buffer
Definition: glext.h:5915
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define d
Definition: ke_i.h:81
void SHA256_Final(sha2_byte digest[], SHA256_CTX *context)
Definition: sha2.c:507
#define MEMSET_BZERO(p, l)
Definition: sha2.c:115
static const char sha2_hex_digits[]
Definition: sha2.c:271
UINT8 sha2_byte
Definition: sha2.h:51
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:40

Referenced by SHA256_Data().

◆ SHA256_Final()

void SHA256_Final ( sha2_byte  digest[],
SHA256_CTX context 
)

Definition at line 507 of file sha2.c.

507 {
508 sha2_word32 *d = (sha2_word32*)digest;
509 unsigned int usedspace;
510
511 /* Sanity check: */
512 assert(context != NULL);
513
514 /* If no digest buffer is passed, we don't bother doing this: */
515 if (digest != NULL) {
516 usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
517#ifndef WORDS_BIGENDIAN
518 /* Convert FROM host byte order */
519 REVERSE64(context->bitcount,context->bitcount);
520#endif
521 if (usedspace > 0) {
522 /* Begin padding with a 1 bit: */
523 context->buffer[usedspace++] = 0x80;
524
525 if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
526 /* Set-up for the last transform: */
527 MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
528 } else {
529 if (usedspace < SHA256_BLOCK_LENGTH) {
530 MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
531 }
532 /* Do second-to-last transform: */
534
535 /* And set-up for the last transform: */
537 }
538 } else {
539 /* Set-up for the last transform: */
541
542 /* Begin padding with a 1 bit: */
543 *context->buffer = 0x80;
544 }
545 /* Set the bit count: */
546 *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
547
548 /* Final transform: */
550
551#ifndef WORDS_BIGENDIAN
552 {
553 /* Convert TO host byte order */
554 int j;
555 for (j = 0; j < 8; j++) {
556 REVERSE32(context->state[j],context->state[j]);
557 *d++ = context->state[j];
558 }
559 }
560#else
562#endif
563 }
564
565 /* Clean up state data: */
566 MEMSET_BZERO(context, sizeof(*context));
567 usedspace = 0;
568}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define REVERSE64(w, x)
Definition: sha2.c:74
#define REVERSE32(w, x)
Definition: sha2.c:69
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:116
#define SHA256_SHORT_BLOCK_LENGTH
Definition: sha2.c:61
void SHA256_Transform(SHA256_CTX *, const sha2_word32 *)
Definition: sha2.c:379
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:39

Referenced by finalize_hash_impl(), and SHA256_End().

◆ SHA256_Init()

void SHA256_Init ( SHA256_CTX context)

Definition at line 275 of file sha2.c.

275 {
276 if (context == NULL) {
277 return;
278 }
281 context->bitcount = 0;
282}
static const sha2_word32 sha256_initial_hash_value[8]
Definition: sha2.c:188

Referenced by init_hash_impl(), and SHA256_Data().

◆ SHA256_Transform()

void SHA256_Transform ( SHA256_CTX context,
const sha2_word32 data 
)

Definition at line 379 of file sha2.c.

379 {
380 sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
381 sha2_word32 T1, T2, *W256;
382 int j;
383
384 W256 = (sha2_word32*)context->buffer;
385
386 /* Initialize registers with the prev. intermediate value */
387 a = context->state[0];
388 b = context->state[1];
389 c = context->state[2];
390 d = context->state[3];
391 e = context->state[4];
392 f = context->state[5];
393 g = context->state[6];
394 h = context->state[7];
395
396 j = 0;
397 do {
398#ifndef WORDS_BIGENDIAN
399 /* Copy data while converting to host byte order */
400 REVERSE32(*data++,W256[j]);
401 /* Apply the SHA-256 compression function to update a..h */
402 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
403#else
404 /* Apply the SHA-256 compression function to update a..h with copy */
405 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
406#endif
407 T2 = Sigma0_256(a) + Maj(a, b, c);
408 h = g;
409 g = f;
410 f = e;
411 e = d + T1;
412 d = c;
413 c = b;
414 b = a;
415 a = T1 + T2;
416
417 j++;
418 } while (j < 16);
419
420 do {
421 /* Part of the message block expansion: */
422 s0 = W256[(j+1)&0x0f];
423 s0 = sigma0_256(s0);
424 s1 = W256[(j+14)&0x0f];
425 s1 = sigma1_256(s1);
426
427 /* Apply the SHA-256 compression function to update a..h */
428 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
429 (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
430 T2 = Sigma0_256(a) + Maj(a, b, c);
431 h = g;
432 g = f;
433 f = e;
434 e = d + T1;
435 d = c;
436 c = b;
437 b = a;
438 a = T1 + T2;
439
440 j++;
441 } while (j < 64);
442
443 /* Compute the current intermediate hash value */
444 context->state[0] += a;
445 context->state[1] += b;
446 context->state[2] += c;
447 context->state[3] += d;
448 context->state[4] += e;
449 context->state[5] += f;
450 context->state[6] += g;
451 context->state[7] += h;
452
453 /* Clean up */
454 a = b = c = d = e = f = g = h = T1 = T2 = 0;
455}
const GLubyte * c
Definition: glext.h:8905
GLfloat f
Definition: glext.h:7540
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean g
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
#define e
Definition: ke_i.h:82
#define f
Definition: ke_i.h:83
#define a
Definition: ke_i.h:78
#define c
Definition: ke_i.h:80
#define b
Definition: ke_i.h:79
struct S1 s1
#define Sigma0_256(x)
Definition: sha2.c:145
#define Maj(x, y, z)
Definition: sha2.c:142
#define sigma1_256(x)
Definition: sha2.c:148
#define Sigma1_256(x)
Definition: sha2.c:146
#define Ch(x, y, z)
Definition: sha2.c:141
static const sha2_word32 K256[64]
Definition: sha2.c:168
#define sigma0_256(x)
Definition: sha2.c:147

Referenced by SHA256_Final(), and SHA256_Update().

◆ SHA256_Update()

void SHA256_Update ( SHA256_CTX context,
const sha2_byte data,
size_t  len 
)

Definition at line 459 of file sha2.c.

459 {
460 unsigned int freespace, usedspace;
461
462 if (len == 0) {
463 /* Calling with no data is valid - we do nothing */
464 return;
465 }
466
467 /* Sanity check: */
468 assert(context != NULL && data != NULL);
469
470 usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
471 if (usedspace > 0) {
472 /* Calculate how much free space is available in the buffer */
473 freespace = SHA256_BLOCK_LENGTH - usedspace;
474
475 if (len >= freespace) {
476 /* Fill the buffer completely and process it */
477 MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
478 context->bitcount += freespace << 3;
479 len -= freespace;
480 data += freespace;
482 } else {
483 /* The buffer is not yet full */
484 MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
485 context->bitcount += len << 3;
486 /* Clean up: */
487 usedspace = freespace = 0;
488 return;
489 }
490 }
491 while (len >= SHA256_BLOCK_LENGTH) {
492 /* Process as many complete blocks as we can */
494 context->bitcount += SHA256_BLOCK_LENGTH << 3;
497 }
498 if (len > 0) {
499 /* There's left-overs, so save 'em */
500 MEMCPY_BCOPY(context->buffer, data, len);
501 context->bitcount += len << 3;
502 }
503 /* Clean up: */
504 usedspace = freespace = 0;
505}

Referenced by SHA256_Data(), and update_hash_impl().

◆ SHA384_Data()

char * SHA384_Data ( const sha2_byte data,
size_t  len,
char  digest[SHA384_DIGEST_STRING_LENGTH] 
)

Definition at line 997 of file sha2.c.

997 {
999
1002 return SHA384_End(&context, digest);
1003}
char * SHA384_End(SHA384_CTX *context, char buffer[])
Definition: sha2.c:974
void SHA384_Init(SHA384_CTX *context)
Definition: sha2.c:932
void SHA384_Update(SHA384_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:941

◆ SHA384_End()

char * SHA384_End ( SHA384_CTX context,
char  buffer[] 
)

Definition at line 974 of file sha2.c.

974 {
975 sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
976 int i;
977
978 /* Sanity check: */
979 assert(context != NULL);
980
981 if (buffer != NULL) {
982 SHA384_Final(digest, context);
983
984 for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
985 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
986 *buffer++ = sha2_hex_digits[*d & 0x0f];
987 d++;
988 }
989 *buffer = 0;
990 } else {
991 MEMSET_BZERO(context, sizeof(*context));
992 }
994 return buffer;
995}
void SHA384_Final(sha2_byte digest[], SHA384_CTX *context)
Definition: sha2.c:945
#define SHA384_DIGEST_LENGTH
Definition: sha2.h:43

Referenced by SHA384_Data().

◆ SHA384_Final()

void SHA384_Final ( sha2_byte  digest[],
SHA384_CTX context 
)

Definition at line 945 of file sha2.c.

945 {
946 sha2_word64 *d = (sha2_word64*)digest;
947
948 /* Sanity check: */
949 assert(context != NULL);
950
951 /* If no digest buffer is passed, we don't bother doing this: */
952 if (digest != NULL) {
954
955 /* Save the hash data for output: */
956#ifndef WORDS_BIGENDIAN
957 {
958 /* Convert TO host byte order */
959 int j;
960 for (j = 0; j < 6; j++) {
961 REVERSE64(context->state[j],context->state[j]);
962 *d++ = context->state[j];
963 }
964 }
965#else
967#endif
968 }
969
970 /* Zero out state data */
971 MEMSET_BZERO(context, sizeof(*context));
972}
void SHA512_Last(SHA512_CTX *)
Definition: sha2.c:829

Referenced by finalize_hash_impl(), and SHA384_End().

◆ SHA384_Init()

void SHA384_Init ( SHA384_CTX context)

Definition at line 932 of file sha2.c.

932 {
933 if (context == NULL) {
934 return;
935 }
938 context->bitcount[0] = context->bitcount[1] = 0;
939}
static const sha2_word64 sha384_initial_hash_value[8]
Definition: sha2.c:244
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:46
#define SHA384_BLOCK_LENGTH
Definition: sha2.h:42

Referenced by init_hash_impl(), and SHA384_Data().

◆ SHA384_Update()

void SHA384_Update ( SHA384_CTX context,
const sha2_byte data,
size_t  len 
)

Definition at line 941 of file sha2.c.

941 {
943}
void SHA512_Update(SHA512_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:781

Referenced by SHA384_Data(), and update_hash_impl().

◆ SHA512_Data()

char * SHA512_Data ( const sha2_byte data,
size_t  len,
char  digest[SHA512_DIGEST_STRING_LENGTH] 
)

Definition at line 922 of file sha2.c.

922 {
924
927 return SHA512_End(&context, digest);
928}
char * SHA512_End(SHA512_CTX *context, char buffer[])
Definition: sha2.c:899
void SHA512_Init(SHA512_CTX *context)
Definition: sha2.c:603

◆ SHA512_End()

char * SHA512_End ( SHA512_CTX context,
char  buffer[] 
)

Definition at line 899 of file sha2.c.

899 {
900 sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
901 int i;
902
903 /* Sanity check: */
904 assert(context != NULL);
905
906 if (buffer != NULL) {
907 SHA512_Final(digest, context);
908
909 for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
910 *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
911 *buffer++ = sha2_hex_digits[*d & 0x0f];
912 d++;
913 }
914 *buffer = 0;
915 } else {
916 MEMSET_BZERO(context, sizeof(*context));
917 }
919 return buffer;
920}
void SHA512_Final(sha2_byte digest[], SHA512_CTX *context)
Definition: sha2.c:870

Referenced by SHA512_Data().

◆ SHA512_Final()

void SHA512_Final ( sha2_byte  digest[],
SHA512_CTX context 
)

Definition at line 870 of file sha2.c.

870 {
871 sha2_word64 *d = (sha2_word64*)digest;
872
873 /* Sanity check: */
874 assert(context != NULL);
875
876 /* If no digest buffer is passed, we don't bother doing this: */
877 if (digest != NULL) {
879
880 /* Save the hash data for output: */
881#ifndef WORDS_BIGENDIAN
882 {
883 /* Convert TO host byte order */
884 int j;
885 for (j = 0; j < 8; j++) {
886 REVERSE64(context->state[j],context->state[j]);
887 *d++ = context->state[j];
888 }
889 }
890#else
892#endif
893 }
894
895 /* Zero out state data */
896 MEMSET_BZERO(context, sizeof(*context));
897}

Referenced by finalize_hash_impl(), and SHA512_End().

◆ SHA512_Init()

void SHA512_Init ( SHA512_CTX context)

Definition at line 603 of file sha2.c.

603 {
604 if (context == NULL) {
605 return;
606 }
609 context->bitcount[0] = context->bitcount[1] = 0;
610}
static const sha2_word64 sha512_initial_hash_value[8]
Definition: sha2.c:256
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:45

Referenced by init_hash_impl(), and SHA512_Data().

◆ SHA512_Last()

void SHA512_Last ( SHA512_CTX context)

Definition at line 829 of file sha2.c.

829 {
830 unsigned int usedspace;
831
832 usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
833#ifndef WORDS_BIGENDIAN
834 /* Convert FROM host byte order */
835 REVERSE64(context->bitcount[0],context->bitcount[0]);
836 REVERSE64(context->bitcount[1],context->bitcount[1]);
837#endif
838 if (usedspace > 0) {
839 /* Begin padding with a 1 bit: */
840 context->buffer[usedspace++] = 0x80;
841
842 if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
843 /* Set-up for the last transform: */
844 MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
845 } else {
846 if (usedspace < SHA512_BLOCK_LENGTH) {
847 MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
848 }
849 /* Do second-to-last transform: */
851
852 /* And set-up for the last transform: */
854 }
855 } else {
856 /* Prepare for final transform: */
858
859 /* Begin padding with a 1 bit: */
860 *context->buffer = 0x80;
861 }
862 /* Store the length of input data (in bits): */
863 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
864 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
865
866 /* Final transform: */
868}
void SHA512_Transform(SHA512_CTX *, const sha2_word64 *)
Definition: sha2.c:703
#define SHA512_SHORT_BLOCK_LENGTH
Definition: sha2.c:63

Referenced by SHA384_Final(), and SHA512_Final().

◆ SHA512_Transform()

void SHA512_Transform ( SHA512_CTX context,
const sha2_word64 data 
)

Definition at line 703 of file sha2.c.

703 {
704 sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
705 sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
706 int j;
707
708 /* Initialize registers with the prev. intermediate value */
709 a = context->state[0];
710 b = context->state[1];
711 c = context->state[2];
712 d = context->state[3];
713 e = context->state[4];
714 f = context->state[5];
715 g = context->state[6];
716 h = context->state[7];
717
718 j = 0;
719 do {
720#ifndef WORDS_BIGENDIAN
721 /* Convert TO host byte order */
722 REVERSE64(*data++, W512[j]);
723 /* Apply the SHA-512 compression function to update a..h */
724 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
725#else
726 /* Apply the SHA-512 compression function to update a..h with copy */
727 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
728#endif
729 T2 = Sigma0_512(a) + Maj(a, b, c);
730 h = g;
731 g = f;
732 f = e;
733 e = d + T1;
734 d = c;
735 c = b;
736 b = a;
737 a = T1 + T2;
738
739 j++;
740 } while (j < 16);
741
742 do {
743 /* Part of the message block expansion: */
744 s0 = W512[(j+1)&0x0f];
745 s0 = sigma0_512(s0);
746 s1 = W512[(j+14)&0x0f];
747 s1 = sigma1_512(s1);
748
749 /* Apply the SHA-512 compression function to update a..h */
750 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
751 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
752 T2 = Sigma0_512(a) + Maj(a, b, c);
753 h = g;
754 g = f;
755 f = e;
756 e = d + T1;
757 d = c;
758 c = b;
759 b = a;
760 a = T1 + T2;
761
762 j++;
763 } while (j < 80);
764
765 /* Compute the current intermediate hash value */
766 context->state[0] += a;
767 context->state[1] += b;
768 context->state[2] += c;
769 context->state[3] += d;
770 context->state[4] += e;
771 context->state[5] += f;
772 context->state[6] += g;
773 context->state[7] += h;
774
775 /* Clean up */
776 a = b = c = d = e = f = g = h = T1 = T2 = 0;
777}
#define Sigma0_512(x)
Definition: sha2.c:151
#define sigma0_512(x)
Definition: sha2.c:153
static const sha2_word64 K512[80]
Definition: sha2.c:200
#define Sigma1_512(x)
Definition: sha2.c:152
#define sigma1_512(x)
Definition: sha2.c:154

Referenced by SHA512_Last(), and SHA512_Update().

◆ SHA512_Update()

void SHA512_Update ( SHA512_CTX context,
const sha2_byte data,
size_t  len 
)

Definition at line 781 of file sha2.c.

781 {
782 unsigned int freespace, usedspace;
783
784 if (len == 0) {
785 /* Calling with no data is valid - we do nothing */
786 return;
787 }
788
789 /* Sanity check: */
790 assert(context != NULL && data != NULL);
791
792 usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
793 if (usedspace > 0) {
794 /* Calculate how much free space is available in the buffer */
795 freespace = SHA512_BLOCK_LENGTH - usedspace;
796
797 if (len >= freespace) {
798 /* Fill the buffer completely and process it */
799 MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
800 ADDINC128(context->bitcount, freespace << 3);
801 len -= freespace;
802 data += freespace;
804 } else {
805 /* The buffer is not yet full */
806 MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
807 ADDINC128(context->bitcount, len << 3);
808 /* Clean up: */
809 usedspace = freespace = 0;
810 return;
811 }
812 }
813 while (len >= SHA512_BLOCK_LENGTH) {
814 /* Process as many complete blocks as we can */
816 ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
819 }
820 if (len > 0) {
821 /* There's left-overs, so save 'em */
822 MEMCPY_BCOPY(context->buffer, data, len);
823 ADDINC128(context->bitcount, len << 3);
824 }
825 /* Clean up: */
826 usedspace = freespace = 0;
827}
#define ADDINC128(w, n)
Definition: sha2.c:89

Referenced by SHA384_Update(), SHA512_Data(), and update_hash_impl().

Variable Documentation

◆ K256

const sha2_word32 K256[64]
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 168 of file sha2.c.

Referenced by SHA256_Transform().

◆ K512

const sha2_word64 K512[80]
static

Definition at line 200 of file sha2.c.

Referenced by SHA512_Transform().

◆ sha256_initial_hash_value

const sha2_word32 sha256_initial_hash_value[8]
static
Initial value:
= {
0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19
}

Definition at line 188 of file sha2.c.

Referenced by SHA256_Init().

◆ sha2_hex_digits

const char sha2_hex_digits[] = "0123456789abcdef"
static

Definition at line 271 of file sha2.c.

Referenced by SHA256_End(), SHA384_End(), and SHA512_End().

◆ sha384_initial_hash_value

const sha2_word64 sha384_initial_hash_value[8]
static
Initial value:
= {
SHA2_WORD64_CONST(0xcbbb9d5d, 0xc1059ed8),
SHA2_WORD64_CONST(0x629a292a, 0x367cd507),
SHA2_WORD64_CONST(0x9159015a, 0x3070dd17),
SHA2_WORD64_CONST(0x152fecd8, 0xf70e5939),
SHA2_WORD64_CONST(0x67332667, 0xffc00b31),
SHA2_WORD64_CONST(0x8eb44a87, 0x68581511),
SHA2_WORD64_CONST(0xdb0c2e0d, 0x64f98fa7),
}

Definition at line 244 of file sha2.c.

Referenced by SHA384_Init().

◆ sha512_initial_hash_value

const sha2_word64 sha512_initial_hash_value[8]
static
Initial value:
= {
SHA2_WORD64_CONST(0x6a09e667, 0xf3bcc908),
SHA2_WORD64_CONST(0xbb67ae85, 0x84caa73b),
SHA2_WORD64_CONST(0x3c6ef372, 0xfe94f82b),
SHA2_WORD64_CONST(0xa54ff53a, 0x5f1d36f1),
SHA2_WORD64_CONST(0x510e527f, 0xade682d1),
SHA2_WORD64_CONST(0x9b05688c, 0x2b3e6c1f),
SHA2_WORD64_CONST(0x1f83d9ab, 0xfb41bd6b),
}

Definition at line 256 of file sha2.c.

Referenced by SHA512_Init().