PIVX Core  5.6.99
P2P Digital Currency
rfc6979_hmac_sha256.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 
7 #include <string.h>
8 
9 #include <algorithm>
10 
11 static const unsigned char zero[1] = {0x00};
12 static const unsigned char one[1] = {0x01};
13 
14 RFC6979_HMAC_SHA256::RFC6979_HMAC_SHA256(const unsigned char* key, size_t keylen, const unsigned char* msg, size_t msglen) : retry(false)
15 {
16  memset(V, 0x01, sizeof(V));
17  memset(K, 0x00, sizeof(K));
18 
19  CHMAC_SHA256(K, sizeof(K)).Write(V, sizeof(V)).Write(zero, sizeof(zero)).Write(key, keylen).Write(msg, msglen).Finalize(K);
20  CHMAC_SHA256(K, sizeof(K)).Write(V, sizeof(V)).Finalize(V);
21  CHMAC_SHA256(K, sizeof(K)).Write(V, sizeof(V)).Write(one, sizeof(one)).Write(key, keylen).Write(msg, msglen).Finalize(K);
22  CHMAC_SHA256(K, sizeof(K)).Write(V, sizeof(V)).Finalize(V);
23 }
24 
26 {
27  memset(V, 0x01, sizeof(V));
28  memset(K, 0x00, sizeof(K));
29 }
30 
31 void RFC6979_HMAC_SHA256::Generate(unsigned char* output, size_t outputlen)
32 {
33  if (retry) {
34  CHMAC_SHA256(K, sizeof(K)).Write(V, sizeof(V)).Write(zero, sizeof(zero)).Finalize(K);
35  CHMAC_SHA256(K, sizeof(K)).Write(V, sizeof(V)).Finalize(V);
36  }
37 
38  while (outputlen > 0) {
39  CHMAC_SHA256(K, sizeof(K)).Write(V, sizeof(V)).Finalize(V);
40  size_t len = std::min(outputlen, sizeof(V));
41  memcpy(output, V, len);
42  output += len;
43  outputlen -= len;
44  }
45 
46  retry = true;
47 }
false
Definition: bls_dkg.cpp:151
A hasher class for HMAC-SHA-256.
Definition: hmac_sha256.h:15
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hmac_sha256.cpp:29
CHMAC_SHA256 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha256.h:24
void Generate(unsigned char *output, size_t outputlen)
Generate a byte array.
unsigned char V[CHMAC_SHA256::OUTPUT_SIZE]
unsigned char K[CHMAC_SHA256::OUTPUT_SIZE]
RFC6979_HMAC_SHA256(const unsigned char *key, size_t keylen, const unsigned char *msg, size_t msglen)
Construct a new RFC6979 PRNG, using the given key and message.
void * memcpy(void *a, const void *b, size_t c)