libbtc
bitcoinclibrary
Data Structures | Macros | Typedefs | Functions
sha2.h File Reference
#include <stdint.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  _SHA256_CTX
 
struct  _SHA512_CTX
 

Macros

#define SHA256_BLOCK_LENGTH   64
 
#define SHA256_DIGEST_LENGTH   32
 
#define SHA256_DIGEST_STRING_LENGTH   (SHA256_DIGEST_LENGTH * 2 + 1)
 
#define SHA512_BLOCK_LENGTH   128
 
#define SHA512_DIGEST_LENGTH   64
 
#define SHA512_DIGEST_STRING_LENGTH   (SHA512_DIGEST_LENGTH * 2 + 1)
 

Typedefs

typedef struct _SHA256_CTX SHA256_CTX
 
typedef struct _SHA512_CTX SHA512_CTX
 

Functions

void sha256_Init (SHA256_CTX *)
 
void sha256_Update (SHA256_CTX *, const uint8_t *, size_t)
 
void sha256_Final (uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX *)
 
void sha256_Raw (const uint8_t *, size_t, uint8_t[SHA256_DIGEST_LENGTH])
 
void sha512_Init (SHA512_CTX *)
 
void sha512_Update (SHA512_CTX *, const uint8_t *, size_t)
 
void sha512_Final (uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX *)
 
void sha512_Raw (const uint8_t *, size_t, uint8_t[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)
 

Macro Definition Documentation

#define SHA256_BLOCK_LENGTH   64

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 38 of file sha2.h.

Referenced by hmac_sha256(), sha256_Final(), sha256_Init(), and sha256_Update().

#define SHA256_DIGEST_LENGTH   32

Definition at line 39 of file sha2.h.

Referenced by hmac_sha256(), sha256_Final(), and sha256_Init().

#define SHA256_DIGEST_STRING_LENGTH   (SHA256_DIGEST_LENGTH * 2 + 1)

Definition at line 40 of file sha2.h.

#define SHA512_BLOCK_LENGTH   128

Definition at line 41 of file sha2.h.

Referenced by hmac_sha512(), sha512_Init(), sha512_Last(), and sha512_Update().

#define SHA512_DIGEST_LENGTH   64

Definition at line 42 of file sha2.h.

Referenced by hmac_sha512(), sha512_Final(), and sha512_Init().

#define SHA512_DIGEST_STRING_LENGTH   (SHA512_DIGEST_LENGTH * 2 + 1)

Definition at line 43 of file sha2.h.

Typedef Documentation

typedef struct _SHA256_CTX SHA256_CTX
typedef struct _SHA512_CTX SHA512_CTX

Function Documentation

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 ( uint8_t  [SHA256_DIGEST_LENGTH],
SHA256_CTX  
)
void sha256_Init ( SHA256_CTX )

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  }
366  MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
367  context->bitcount = 0;
368 }
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:38
#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 uint8_t *  ,
size_t  ,
uint8_t  [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_Update ( SHA256_CTX ,
const uint8_t *  ,
size_t   
)

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
#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 ( uint8_t  [SHA512_DIGEST_LENGTH],
SHA512_CTX  
)
void sha512_Init ( SHA512_CTX )

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  }
668  MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
669  context->bitcount[0] = context->bitcount[1] = 0;
670 }
static const sha2_word64 sha512_initial_hash_value[8]
Definition: sha2.c:348
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
#define MEMSET_BZERO(p, l)
Definition: sha2.c:140
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:42
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:41
void sha512_Raw ( const uint8_t *  ,
size_t  ,
uint8_t  [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_Update ( SHA512_CTX ,
const uint8_t *  ,
size_t   
)

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
#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
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:41