libbtc
bitcoinclibrary
Macros | Typedefs | Functions | Variables
sha2.c File Reference
#include <string.h>
#include <stdint.h>
#include "sha2.h"
#include "btc/hash.h"

Go to the source code of this file.

Macros

#define LITTLE_ENDIAN   1234
 
#define BIG_ENDIAN   4321
 
#define BYTE_ORDER   LITTLE_ENDIAN
 
#define SHA256_SHORT_BLOCK_LENGTH   (SHA256_BLOCK_LENGTH - 8)
 
#define SHA512_SHORT_BLOCK_LENGTH   (SHA512_BLOCK_LENGTH - 16)
 
#define REVERSE32(w, x)
 
#define REVERSE64(w, x)
 
#define ADDINC128(w, n)
 
#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)))
 

Typedefs

typedef uint8_t sha2_byte
 
typedef uint32_t sha2_word32
 
typedef uint64_t sha2_word64
 

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)
 
void sha256_Raw (const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_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)
 
void sha512_Raw (const sha2_byte *data, size_t len, uint8_t digest[SHA512_DIGEST_LENGTH])
 
void hmac_sha256 (const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
 
void hmac_sha512 (const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
 
void btc_hash (const unsigned char *datain, size_t length, uint256 hashout)
 
void btc_hash_sngl_sha256 (const unsigned char *datain, size_t length, uint256 hashout)
 

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 sha512_initial_hash_value [8]
 

Macro Definition Documentation

#define ADDINC128 (   w,
 
)
Value:
{ \
(w)[0] += (sha2_word64)(n); \
if ((w)[0] < (n)) { \
(w)[1]++; \
} \
}
uint64_t sha2_word64
Definition: sha2.c:102

Definition at line 132 of file sha2.c.

Referenced by sha512_Update().

#define BIG_ENDIAN   4321

Definition at line 89 of file sha2.c.

#define BYTE_ORDER   LITTLE_ENDIAN

Definition at line 93 of file sha2.c.

Referenced by sha256_Transform(), and sha512_Transform().

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

Definition at line 160 of file sha2.c.

Referenced by sha256_Transform(), and sha512_Transform().

#define LITTLE_ENDIAN   1234

Copyright (c) 2000-2001 Aaron D. Gifford Copyright (c) 2013 Pavol Rusnak Copyright (c) 2015 Jonas Schnelli All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition at line 88 of file sha2.c.

Referenced by sha256_Transform(), and sha512_Transform().

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

Definition at line 161 of file sha2.c.

Referenced by sha256_Transform(), and sha512_Transform().

#define MEMCPY_BCOPY (   d,
  s,
 
)    memcpy((d), (s), (l))
#define MEMSET_BZERO (   p,
 
)    memset((p), 0, (l))

Definition at line 140 of file sha2.c.

Referenced by sha256_Final(), sha256_Init(), sha512_Final(), sha512_Init(), and sha512_Last().

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

Definition at line 153 of file sha2.c.

#define REVERSE32 (   w,
 
)
Value:
{ \
sha2_word32 tmp = (w); \
tmp = (tmp >> 16) | (tmp << 16); \
(x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
}

Definition at line 112 of file sha2.c.

Referenced by sha256_Final(), and sha256_Transform().

#define REVERSE64 (   w,
 
)
Value:
{ \
sha2_word64 tmp = (w); \
tmp = (tmp >> 32) | (tmp << 32); \
tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
(x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | ((tmp & 0x0000ffff0000ffffULL) << 16); \
}

Definition at line 118 of file sha2.c.

Referenced by sha256_Final(), sha512_Final(), sha512_Last(), and sha512_Transform().

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

Definition at line 155 of file sha2.c.

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

Definition at line 157 of file sha2.c.

#define SHA256_SHORT_BLOCK_LENGTH   (SHA256_BLOCK_LENGTH - 8)

Definition at line 106 of file sha2.c.

Referenced by sha256_Final().

#define SHA512_SHORT_BLOCK_LENGTH   (SHA512_BLOCK_LENGTH - 16)

Definition at line 107 of file sha2.c.

Referenced by sha512_Last().

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

Definition at line 164 of file sha2.c.

Referenced by sha256_Transform().

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

Definition at line 166 of file sha2.c.

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

Definition at line 170 of file sha2.c.

Referenced by sha512_Transform().

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

Definition at line 172 of file sha2.c.

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

Definition at line 165 of file sha2.c.

Referenced by sha256_Transform().

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

Definition at line 167 of file sha2.c.

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

Definition at line 171 of file sha2.c.

Referenced by sha512_Transform().

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

Definition at line 173 of file sha2.c.

Typedef Documentation

typedef uint8_t sha2_byte

Definition at line 100 of file sha2.c.

typedef uint32_t sha2_word32

Definition at line 101 of file sha2.c.

typedef uint64_t sha2_word64

Definition at line 102 of file sha2.c.

Function Documentation

void btc_hash ( const unsigned char *  datain,
size_t  length,
uint256  hashout 
)

Definition at line 1026 of file sha2.c.

References sha256_Raw().

Referenced by btc_privkey_verify_pubkey().

1027 {
1028  //bitcoin double sha256 hash
1029  sha256_Raw((const uint8_t*)datain, length, hashout);
1030  sha256_Raw(hashout, 32, hashout);
1031 }
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha2.c:652
void btc_hash_sngl_sha256 ( const unsigned char *  datain,
size_t  length,
uint256  hashout 
)

Definition at line 1033 of file sha2.c.

References sha256_Raw().

1034 {
1035  sha256_Raw((const uint8_t*)datain, length, hashout);
1036 }
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha2.c:652
void hmac_sha256 ( const uint8_t *  key,
const uint32_t  keylen,
const uint8_t *  msg,
const uint32_t  msglen,
uint8_t *  hmac 
)

Definition at line 966 of file sha2.c.

References SHA256_BLOCK_LENGTH, SHA256_DIGEST_LENGTH, sha256_Final(), sha256_Init(), sha256_Raw(), and sha256_Update().

967 {
968  int i;
969  uint8_t buf[SHA256_BLOCK_LENGTH], o_key_pad[SHA256_BLOCK_LENGTH],
970  i_key_pad[SHA256_BLOCK_LENGTH];
971  SHA256_CTX ctx;
972 
973  memset(buf, 0, SHA256_BLOCK_LENGTH);
974  if (keylen > SHA256_BLOCK_LENGTH) {
975  sha256_Raw(key, keylen, buf);
976  } else {
977  memcpy(buf, key, keylen);
978  }
979 
980  for (i = 0; i < SHA256_BLOCK_LENGTH; i++) {
981  o_key_pad[i] = buf[i] ^ 0x5c;
982  i_key_pad[i] = buf[i] ^ 0x36;
983  }
984 
985  sha256_Init(&ctx);
986  sha256_Update(&ctx, i_key_pad, SHA256_BLOCK_LENGTH);
987  sha256_Update(&ctx, msg, msglen);
988  sha256_Final(buf, &ctx);
989 
990  sha256_Init(&ctx);
991  sha256_Update(&ctx, o_key_pad, SHA256_BLOCK_LENGTH);
993  sha256_Final(hmac, &ctx);
994 }
void sha256_Update(SHA256_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:544
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:38
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha2.c:652
void sha256_Final(sha2_byte digest[], SHA256_CTX *context)
Definition: sha2.c:590
void sha256_Init(SHA256_CTX *context)
Definition: sha2.c:360
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:39
void hmac_sha512 ( const uint8_t *  key,
const uint32_t  keylen,
const uint8_t *  msg,
const uint32_t  msglen,
uint8_t *  hmac 
)

Definition at line 996 of file sha2.c.

References SHA512_BLOCK_LENGTH, SHA512_DIGEST_LENGTH, sha512_Final(), sha512_Init(), sha512_Raw(), and sha512_Update().

Referenced by btc_hdnode_from_seed(), btc_hdnode_private_ckd(), and btc_hdnode_public_ckd().

997 {
998  int i;
999  uint8_t buf[SHA512_BLOCK_LENGTH], o_key_pad[SHA512_BLOCK_LENGTH],
1000  i_key_pad[SHA512_BLOCK_LENGTH];
1001  SHA512_CTX ctx;
1002 
1003  memset(buf, 0, SHA512_BLOCK_LENGTH);
1004  if (keylen > SHA512_BLOCK_LENGTH) {
1005  sha512_Raw(key, keylen, buf);
1006  } else {
1007  memcpy(buf, key, keylen);
1008  }
1009 
1010  for (i = 0; i < SHA512_BLOCK_LENGTH; i++) {
1011  o_key_pad[i] = buf[i] ^ 0x5c;
1012  i_key_pad[i] = buf[i] ^ 0x36;
1013  }
1014 
1015  sha512_Init(&ctx);
1016  sha512_Update(&ctx, i_key_pad, SHA512_BLOCK_LENGTH);
1017  sha512_Update(&ctx, msg, msglen);
1018  sha512_Final(buf, &ctx);
1019 
1020  sha512_Init(&ctx);
1021  sha512_Update(&ctx, o_key_pad, SHA512_BLOCK_LENGTH);
1022  sha512_Update(&ctx, buf, SHA512_DIGEST_LENGTH);
1023  sha512_Final(hmac, &ctx);
1024 }
void sha512_Init(SHA512_CTX *context)
Definition: sha2.c:662
void sha512_Update(SHA512_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:840
void sha512_Final(sha2_byte digest[], SHA512_CTX *context)
Definition: sha2.c:931
void sha512_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA512_DIGEST_LENGTH])
Definition: sha2.c:958
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:42
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:41
void sha256_Final ( sha2_byte  digest[],
SHA256_CTX context 
)

Definition at line 590 of file sha2.c.

References _SHA256_CTX::bitcount, _SHA256_CTX::buffer, MEMCPY_BCOPY, MEMSET_BZERO, REVERSE32, REVERSE64, SHA256_BLOCK_LENGTH, SHA256_DIGEST_LENGTH, SHA256_SHORT_BLOCK_LENGTH, sha256_Transform(), and _SHA256_CTX::state.

Referenced by hmac_sha256(), and sha256_Raw().

591 {
592  sha2_word32* d = (sha2_word32*)digest;
593  unsigned int usedspace;
594 
595  /* If no digest buffer is passed, we don't bother doing this: */
596  if (digest != (sha2_byte*)0) {
597  usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
598 #if BYTE_ORDER == LITTLE_ENDIAN
599  /* Convert FROM host byte order */
600  REVERSE64(context->bitcount, context->bitcount);
601 #endif
602  if (usedspace > 0) {
603  /* Begin padding with a 1 bit: */
604  context->buffer[usedspace++] = 0x80;
605 
606  if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
607  /* Set-up for the last transform: */
608  MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
609  } else {
610  if (usedspace < SHA256_BLOCK_LENGTH) {
611  MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
612  }
613  /* Do second-to-last transform: */
614  sha256_Transform(context, (sha2_word32*)context->buffer);
615 
616  /* And set-up for the last transform: */
618  }
619  } else {
620  /* Set-up for the last transform: */
622 
623  /* Begin padding with a 1 bit: */
624  *context->buffer = 0x80;
625  }
626  /* Set the bit count: */
628  *t = context->bitcount;
629 
630  /* Final transform: */
631  sha256_Transform(context, (sha2_word32*)context->buffer);
632 
633 #if BYTE_ORDER == LITTLE_ENDIAN
634  {
635  /* Convert TO host byte order */
636  int j;
637  for (j = 0; j < 8; j++) {
638  REVERSE32(context->state[j], context->state[j]);
639  *d++ = context->state[j];
640  }
641  }
642 #else
644 #endif
645  }
646 
647  /* Clean up state data: */
648  MEMSET_BZERO(context, sizeof(SHA256_CTX));
649  usedspace = 0;
650 }
uint32_t state[8]
Definition: sha2.h:47
uint32_t sha2_word32
Definition: sha2.c:101
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:38
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha2.h:49
uint64_t bitcount
Definition: sha2.h:48
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
void sha256_Transform(SHA256_CTX *, const sha2_word32 *)
Definition: sha2.c:463
#define MEMSET_BZERO(p, l)
Definition: sha2.c:140
#define REVERSE64(w, x)
Definition: sha2.c:118
uint64_t sha2_word64
Definition: sha2.c:102
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:39
uint8_t sha2_byte
Definition: sha2.c:100
#define REVERSE32(w, x)
Definition: sha2.c:112
#define SHA256_SHORT_BLOCK_LENGTH
Definition: sha2.c:106
void sha256_Init ( SHA256_CTX context)

Definition at line 360 of file sha2.c.

References _SHA256_CTX::bitcount, _SHA256_CTX::buffer, MEMCPY_BCOPY, MEMSET_BZERO, SHA256_BLOCK_LENGTH, SHA256_DIGEST_LENGTH, sha256_initial_hash_value, and _SHA256_CTX::state.

Referenced by hmac_sha256(), and sha256_Raw().

361 {
362  if (context == (SHA256_CTX*)0) {
363  return;
364  }
367  context->bitcount = 0;
368 }
uint32_t state[8]
Definition: sha2.h:47
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:38
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha2.h:49
uint64_t bitcount
Definition: sha2.h:48
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
#define MEMSET_BZERO(p, l)
Definition: sha2.c:140
static const sha2_word32 sha256_initial_hash_value[8]
Definition: sha2.c:254
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:39
void sha256_Raw ( const sha2_byte data,
size_t  len,
uint8_t  digest[SHA256_DIGEST_LENGTH] 
)

Definition at line 652 of file sha2.c.

References sha256_Final(), sha256_Init(), and sha256_Update().

Referenced by btc_b58check(), btc_base58_encode_check(), btc_hash(), btc_hash_sngl_sha256(), btc_hdnode_private_ckd(), btc_hdnode_public_ckd(), btc_tx_hash(), btc_tx_sighash(), and hmac_sha256().

653 {
654  SHA256_CTX context;
655  sha256_Init(&context);
656  sha256_Update(&context, data, len);
657  sha256_Final(digest, &context);
658 }
size_t len
Definition: buffer.h:22
void sha256_Update(SHA256_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:544
void sha256_Final(sha2_byte digest[], SHA256_CTX *context)
Definition: sha2.c:590
void sha256_Init(SHA256_CTX *context)
Definition: sha2.c:360
void sha256_Transform ( SHA256_CTX context,
const sha2_word32 data 
)

Definition at line 463 of file sha2.c.

References _SHA256_CTX::buffer, BYTE_ORDER, Ch, K256, LITTLE_ENDIAN, Maj, REVERSE32, Sigma0_256, Sigma1_256, and _SHA256_CTX::state.

Referenced by sha256_Final(), and sha256_Update().

464 {
465  sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
466  sha2_word32 T1, T2, *W256;
467  int j;
468 
469  W256 = (sha2_word32*)context->buffer;
470 
471  /* Initialize registers with the prev. intermediate value */
472  a = context->state[0];
473  b = context->state[1];
474  c = context->state[2];
475  d = context->state[3];
476  e = context->state[4];
477  f = context->state[5];
478  g = context->state[6];
479  h = context->state[7];
480 
481  j = 0;
482  do {
484  /* Copy data while converting to host byte order */
485  REVERSE32(*data++, W256[j]);
486  /* Apply the SHA-256 compression function to update a..h */
487  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
488 #else /* BYTE_ORDER == LITTLE_ENDIAN */
489  /* Apply the SHA-256 compression function to update a..h with copy */
490  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
491 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
492  T2 = Sigma0_256(a) + Maj(a, b, c);
493  h = g;
494  g = f;
495  f = e;
496  e = d + T1;
497  d = c;
498  c = b;
499  b = a;
500  a = T1 + T2;
501 
502  j++;
503  } while (j < 16);
504 
505  do {
506  /* Part of the message block expansion: */
507  s0 = W256[(j + 1) & 0x0f];
508  s0 = sigma0_256(s0);
509  s1 = W256[(j + 14) & 0x0f];
510  s1 = sigma1_256(s1);
511 
512  /* Apply the SHA-256 compression function to update a..h */
513  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
514  (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
515  T2 = Sigma0_256(a) + Maj(a, b, c);
516  h = g;
517  g = f;
518  f = e;
519  e = d + T1;
520  d = c;
521  c = b;
522  b = a;
523  a = T1 + T2;
524 
525  j++;
526  } while (j < 64);
527 
528  /* Compute the current intermediate hash value */
529  context->state[0] += a;
530  context->state[1] += b;
531  context->state[2] += c;
532  context->state[3] += d;
533  context->state[4] += e;
534  context->state[5] += f;
535  context->state[6] += g;
536  context->state[7] += h;
537 
538  /* Clean up */
539  a = b = c = d = e = f = g = h = T1 = T2 = 0;
540 }
#define BYTE_ORDER
Definition: sha2.c:93
uint32_t state[8]
Definition: sha2.h:47
uint32_t sha2_word32
Definition: sha2.c:101
static const sha2_word32 K256[64]
Definition: sha2.c:187
#define Ch(x, y, z)
Definition: sha2.c:160
#define Sigma1_256(x)
Definition: sha2.c:165
#define sigma1_256(x)
Definition: sha2.c:167
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha2.h:49
#define sigma0_256(x)
Definition: sha2.c:166
#define Sigma0_256(x)
Definition: sha2.c:164
#define LITTLE_ENDIAN
Definition: sha2.c:88
#define Maj(x, y, z)
Definition: sha2.c:161
#define REVERSE32(w, x)
Definition: sha2.c:112
void sha256_Update ( SHA256_CTX context,
const sha2_byte data,
size_t  len 
)

Definition at line 544 of file sha2.c.

References _SHA256_CTX::bitcount, _SHA256_CTX::buffer, MEMCPY_BCOPY, SHA256_BLOCK_LENGTH, and sha256_Transform().

Referenced by hmac_sha256(), and sha256_Raw().

545 {
546  unsigned int freespace, usedspace;
547 
548  if (len == 0) {
549  /* Calling with no data is valid - we do nothing */
550  return;
551  }
552 
553  usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
554  if (usedspace > 0) {
555  /* Calculate how much free space is available in the buffer */
556  freespace = SHA256_BLOCK_LENGTH - usedspace;
557 
558  if (len >= freespace) {
559  /* Fill the buffer completely and process it */
560  MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
561  context->bitcount += freespace << 3;
562  len -= freespace;
563  data += freespace;
564  sha256_Transform(context, (sha2_word32*)context->buffer);
565  } else {
566  /* The buffer is not yet full */
567  MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
568  context->bitcount += len << 3;
569  /* Clean up: */
570  usedspace = freespace = 0;
571  return;
572  }
573  }
574  while (len >= SHA256_BLOCK_LENGTH) {
575  /* Process as many complete blocks as we can */
576  sha256_Transform(context, (const sha2_word32*)data);
577  context->bitcount += SHA256_BLOCK_LENGTH << 3;
579  data += SHA256_BLOCK_LENGTH;
580  }
581  if (len > 0) {
582  /* There's left-overs, so save 'em */
583  MEMCPY_BCOPY(context->buffer, data, len);
584  context->bitcount += len << 3;
585  }
586  /* Clean up: */
587  usedspace = freespace = 0;
588 }
size_t len
Definition: buffer.h:22
uint32_t sha2_word32
Definition: sha2.c:101
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:38
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha2.h:49
uint64_t bitcount
Definition: sha2.h:48
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
void sha256_Transform(SHA256_CTX *, const sha2_word32 *)
Definition: sha2.c:463
void sha512_Final ( sha2_byte  digest[],
SHA512_CTX context 
)

Definition at line 931 of file sha2.c.

References MEMCPY_BCOPY, MEMSET_BZERO, REVERSE64, SHA512_DIGEST_LENGTH, sha512_Last(), and _SHA512_CTX::state.

Referenced by hmac_sha512(), and sha512_Raw().

932 {
933  sha2_word64* d = (sha2_word64*)digest;
934 
935  /* If no digest buffer is passed, we don't bother doing this: */
936  if (digest != (sha2_byte*)0) {
937  sha512_Last(context);
938 
939 /* Save the hash data for output: */
940 #if BYTE_ORDER == LITTLE_ENDIAN
941  {
942  /* Convert TO host byte order */
943  int j;
944  for (j = 0; j < 8; j++) {
945  REVERSE64(context->state[j], context->state[j]);
946  *d++ = context->state[j];
947  }
948  }
949 #else
951 #endif
952  }
953 
954  /* Zero out state data */
955  MEMSET_BZERO(context, sizeof(SHA512_CTX));
956 }
void sha512_Last(SHA512_CTX *)
Definition: sha2.c:886
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
#define MEMSET_BZERO(p, l)
Definition: sha2.c:140
#define REVERSE64(w, x)
Definition: sha2.c:118
uint64_t sha2_word64
Definition: sha2.c:102
uint8_t sha2_byte
Definition: sha2.c:100
uint64_t state[8]
Definition: sha2.h:53
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:42
void sha512_Init ( SHA512_CTX context)

Definition at line 662 of file sha2.c.

References _SHA512_CTX::bitcount, _SHA512_CTX::buffer, MEMCPY_BCOPY, MEMSET_BZERO, SHA512_BLOCK_LENGTH, SHA512_DIGEST_LENGTH, sha512_initial_hash_value, and _SHA512_CTX::state.

Referenced by hmac_sha512(), and sha512_Raw().

663 {
664  if (context == (SHA512_CTX*)0) {
665  return;
666  }
669  context->bitcount[0] = context->bitcount[1] = 0;
670 }
static const sha2_word64 sha512_initial_hash_value[8]
Definition: sha2.c:348
uint64_t bitcount[2]
Definition: sha2.h:54
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
#define MEMSET_BZERO(p, l)
Definition: sha2.c:140
uint64_t state[8]
Definition: sha2.h:53
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:42
uint8_t buffer[SHA512_BLOCK_LENGTH]
Definition: sha2.h:55
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:41
void sha512_Last ( SHA512_CTX context)

Definition at line 886 of file sha2.c.

References _SHA512_CTX::bitcount, _SHA512_CTX::buffer, MEMSET_BZERO, REVERSE64, SHA512_BLOCK_LENGTH, SHA512_SHORT_BLOCK_LENGTH, and sha512_Transform().

Referenced by sha512_Final().

887 {
888  unsigned int usedspace;
889 
890  usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
891 #if BYTE_ORDER == LITTLE_ENDIAN
892  /* Convert FROM host byte order */
893  REVERSE64(context->bitcount[0], context->bitcount[0]);
894  REVERSE64(context->bitcount[1], context->bitcount[1]);
895 #endif
896  if (usedspace > 0) {
897  /* Begin padding with a 1 bit: */
898  context->buffer[usedspace++] = 0x80;
899 
900  if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
901  /* Set-up for the last transform: */
902  MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
903  } else {
904  if (usedspace < SHA512_BLOCK_LENGTH) {
905  MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
906  }
907  /* Do second-to-last transform: */
908  sha512_Transform(context, (sha2_word64*)context->buffer);
909 
910  /* And set-up for the last transform: */
911  MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
912  }
913  } else {
914  /* Prepare for final transform: */
916 
917  /* Begin padding with a 1 bit: */
918  *context->buffer = 0x80;
919  }
920  /* Store the length of input data (in bits): */
921  sha2_word64* t;
923  *t = context->bitcount[1];
924  t = (sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8];
925  *t = context->bitcount[0];
926 
927  /* Final transform: */
928  sha512_Transform(context, (sha2_word64*)context->buffer);
929 }
uint64_t bitcount[2]
Definition: sha2.h:54
#define MEMSET_BZERO(p, l)
Definition: sha2.c:140
void sha512_Transform(SHA512_CTX *, const sha2_word64 *)
Definition: sha2.c:761
#define REVERSE64(w, x)
Definition: sha2.c:118
uint64_t sha2_word64
Definition: sha2.c:102
#define SHA512_SHORT_BLOCK_LENGTH
Definition: sha2.c:107
uint8_t buffer[SHA512_BLOCK_LENGTH]
Definition: sha2.h:55
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:41
void sha512_Raw ( const sha2_byte data,
size_t  len,
uint8_t  digest[SHA512_DIGEST_LENGTH] 
)

Definition at line 958 of file sha2.c.

References sha512_Final(), sha512_Init(), and sha512_Update().

Referenced by hmac_sha512().

959 {
960  SHA512_CTX context;
961  sha512_Init(&context);
962  sha512_Update(&context, data, len);
963  sha512_Final(digest, &context);
964 }
size_t len
Definition: buffer.h:22
void sha512_Init(SHA512_CTX *context)
Definition: sha2.c:662
void sha512_Update(SHA512_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:840
void sha512_Final(sha2_byte digest[], SHA512_CTX *context)
Definition: sha2.c:931
void sha512_Transform ( SHA512_CTX context,
const sha2_word64 data 
)

Definition at line 761 of file sha2.c.

References _SHA512_CTX::buffer, BYTE_ORDER, Ch, K512, LITTLE_ENDIAN, Maj, REVERSE64, Sigma0_512, Sigma1_512, and _SHA512_CTX::state.

Referenced by sha512_Last(), and sha512_Update().

762 {
763  sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
764  sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
765  int j;
766 
767  /* Initialize registers with the prev. intermediate value */
768  a = context->state[0];
769  b = context->state[1];
770  c = context->state[2];
771  d = context->state[3];
772  e = context->state[4];
773  f = context->state[5];
774  g = context->state[6];
775  h = context->state[7];
776 
777  j = 0;
778  do {
780  /* Convert TO host byte order */
781  REVERSE64(*data++, W512[j]);
782  /* Apply the SHA-512 compression function to update a..h */
783  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
784 #else /* BYTE_ORDER == LITTLE_ENDIAN */
785  /* Apply the SHA-512 compression function to update a..h with copy */
786  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
787 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
788  T2 = Sigma0_512(a) + Maj(a, b, c);
789  h = g;
790  g = f;
791  f = e;
792  e = d + T1;
793  d = c;
794  c = b;
795  b = a;
796  a = T1 + T2;
797 
798  j++;
799  } while (j < 16);
800 
801  do {
802  /* Part of the message block expansion: */
803  s0 = W512[(j + 1) & 0x0f];
804  s0 = sigma0_512(s0);
805  s1 = W512[(j + 14) & 0x0f];
806  s1 = sigma1_512(s1);
807 
808  /* Apply the SHA-512 compression function to update a..h */
809  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
810  (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
811  T2 = Sigma0_512(a) + Maj(a, b, c);
812  h = g;
813  g = f;
814  f = e;
815  e = d + T1;
816  d = c;
817  c = b;
818  b = a;
819  a = T1 + T2;
820 
821  j++;
822  } while (j < 80);
823 
824  /* Compute the current intermediate hash value */
825  context->state[0] += a;
826  context->state[1] += b;
827  context->state[2] += c;
828  context->state[3] += d;
829  context->state[4] += e;
830  context->state[5] += f;
831  context->state[6] += g;
832  context->state[7] += h;
833 
834  /* Clean up */
835  a = b = c = d = e = f = g = h = T1 = T2 = 0;
836 }
#define Sigma0_512(x)
Definition: sha2.c:170
#define BYTE_ORDER
Definition: sha2.c:93
#define Ch(x, y, z)
Definition: sha2.c:160
#define sigma1_512(x)
Definition: sha2.c:173
static const sha2_word64 K512[80]
Definition: sha2.c:265
#define REVERSE64(w, x)
Definition: sha2.c:118
uint64_t sha2_word64
Definition: sha2.c:102
#define sigma0_512(x)
Definition: sha2.c:172
#define LITTLE_ENDIAN
Definition: sha2.c:88
#define Maj(x, y, z)
Definition: sha2.c:161
uint64_t state[8]
Definition: sha2.h:53
#define Sigma1_512(x)
Definition: sha2.c:171
uint8_t buffer[SHA512_BLOCK_LENGTH]
Definition: sha2.h:55
void sha512_Update ( SHA512_CTX context,
const sha2_byte data,
size_t  len 
)

Definition at line 840 of file sha2.c.

References ADDINC128, _SHA512_CTX::bitcount, _SHA512_CTX::buffer, MEMCPY_BCOPY, SHA512_BLOCK_LENGTH, and sha512_Transform().

Referenced by hmac_sha512(), and sha512_Raw().

841 {
842  unsigned int freespace, usedspace;
843 
844  if (len == 0) {
845  /* Calling with no data is valid - we do nothing */
846  return;
847  }
848 
849  usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
850  if (usedspace > 0) {
851  /* Calculate how much free space is available in the buffer */
852  freespace = SHA512_BLOCK_LENGTH - usedspace;
853 
854  if (len >= freespace) {
855  /* Fill the buffer completely and process it */
856  MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
857  ADDINC128(context->bitcount, freespace << 3);
858  len -= freespace;
859  data += freespace;
860  sha512_Transform(context, (sha2_word64*)context->buffer);
861  } else {
862  /* The buffer is not yet full */
863  MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
864  ADDINC128(context->bitcount, len << 3);
865  /* Clean up: */
866  usedspace = freespace = 0;
867  return;
868  }
869  }
870  while (len >= SHA512_BLOCK_LENGTH) {
871  /* Process as many complete blocks as we can */
872  sha512_Transform(context, (const sha2_word64*)data);
873  ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
875  data += SHA512_BLOCK_LENGTH;
876  }
877  if (len > 0) {
878  /* There's left-overs, so save 'em */
879  MEMCPY_BCOPY(context->buffer, data, len);
880  ADDINC128(context->bitcount, len << 3);
881  }
882  /* Clean up: */
883  usedspace = freespace = 0;
884 }
size_t len
Definition: buffer.h:22
uint64_t bitcount[2]
Definition: sha2.h:54
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
void sha512_Transform(SHA512_CTX *, const sha2_word64 *)
Definition: sha2.c:761
#define ADDINC128(w, n)
Definition: sha2.c:132
uint64_t sha2_word64
Definition: sha2.c:102
uint8_t buffer[SHA512_BLOCK_LENGTH]
Definition: sha2.h:55
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:41

Variable Documentation

const sha2_word32 K256[64]
static

Definition at line 187 of file sha2.c.

Referenced by sha256_Transform().

const sha2_word64 K512[80]
static

Definition at line 265 of file sha2.c.

Referenced by sha512_Transform().

const sha2_word32 sha256_initial_hash_value[8]
static
Initial value:
= {
0x6a09e667UL,
0xbb67ae85UL,
0x3c6ef372UL,
0xa54ff53aUL,
0x510e527fUL,
0x9b05688cUL,
0x1f83d9abUL,
0x5be0cd19UL}

Definition at line 254 of file sha2.c.

Referenced by sha256_Init().

const sha2_word64 sha512_initial_hash_value[8]
static
Initial value:
= {
0x6a09e667f3bcc908ULL,
0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL,
0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL,
0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL,
0x5be0cd19137e2179ULL}

Definition at line 348 of file sha2.c.

Referenced by sha512_Init().