Skip to content

Commit 551d489

Browse files
committed
Add HKDF HMAC_SHA256 L=32 implementations
1 parent 3b64f85 commit 551d489

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ crypto_libbitcoin_crypto_base_a_SOURCES = \
342342
crypto/chacha20.h \
343343
crypto/chacha20.cpp \
344344
crypto/common.h \
345+
crypto/hkdf_sha256_32.cpp \
346+
crypto/hkdf_sha256_32.h \
345347
crypto/hmac_sha256.cpp \
346348
crypto/hmac_sha256.h \
347349
crypto/hmac_sha512.cpp \

src/crypto/hkdf_sha256_32.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <crypto/hkdf_sha256_32.h>
6+
7+
#include <assert.h>
8+
#include <string.h>
9+
10+
CHKDF_HMAC_SHA256_L32::CHKDF_HMAC_SHA256_L32(const unsigned char* ikm, size_t ikmlen, const std::string& salt)
11+
{
12+
CHMAC_SHA256((const unsigned char*)salt.c_str(), salt.size()).Write(ikm, ikmlen).Finalize(m_prk);
13+
}
14+
15+
void CHKDF_HMAC_SHA256_L32::Expand32(const std::string& info, unsigned char hash[OUTPUT_SIZE])
16+
{
17+
// expand a 32byte key (single round)
18+
assert(info.size() <= 128);
19+
static const unsigned char one[1] = {1};
20+
CHMAC_SHA256(m_prk, 32).Write((const unsigned char*)info.data(), info.size()).Write(one, 1).Finalize(hash);
21+
}

src/crypto/hkdf_sha256_32.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_CRYPTO_HKDF_SHA256_32_H
6+
#define BITCOIN_CRYPTO_HKDF_SHA256_32_H
7+
8+
#include <crypto/hmac_sha256.h>
9+
10+
#include <stdint.h>
11+
#include <stdlib.h>
12+
13+
/** A rfc5869 HKDF implementation with HMAC_SHA256 and fixed key output length of 32 bytes (L=32) */
14+
class CHKDF_HMAC_SHA256_L32
15+
{
16+
private:
17+
unsigned char m_prk[32];
18+
static const size_t OUTPUT_SIZE = 32;
19+
20+
public:
21+
CHKDF_HMAC_SHA256_L32(const unsigned char* ikm, size_t ikmlen, const std::string& salt);
22+
void Expand32(const std::string& info, unsigned char hash[OUTPUT_SIZE]);
23+
};
24+
25+
#endif // BITCOIN_CRYPTO_HKDF_SHA256_32_H

0 commit comments

Comments
 (0)