Skip to content

Commit 376638a

Browse files
committed
Merge #14047: Add HKDF_HMAC256_L32 and method to negate a private key
8794a4b QA: add test for HKDF HMAC_SHA256 L32 (Jonas Schnelli) 551d489 Add HKDF HMAC_SHA256 L=32 implementations (Jonas Schnelli) 3b64f85 QA: add test for CKey::Negate() (Jonas Schnelli) 463921b CKey: add method to negate the key (Jonas Schnelli) Pull request description: This adds a limited implementation of `HKDF` (defined by rfc5869) that supports only HMAC-SHA256 and length output of 32 bytes (will be required for v2 transport protocol). This PR also includes a method to negate a private key which is useful to enforce public keys starting with 0x02 (or 0x03) (a requirement for the v2 transport protocol). The new `CKey::Negate()` method is pretty much a wrapper around `secp256k1_ec_privkey_negate()`. Including tests. This is a subset of #14032 and a pre-requirement for the v2 transport protocol. ACKs for commit 8794a4: Tree-SHA512: 5341929dfa29f5da766ec3612784baec6a3ad69972f08b5a985a8aafdae4dae36f104a2b888d1f5d1f33561456bd111f960d7e32c2cc4fd18e48358468f26c1a
2 parents df7addc + 8794a4b commit 376638a

File tree

7 files changed

+128
-2
lines changed

7 files changed

+128
-2
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ crypto_libbitcoin_crypto_base_a_SOURCES = \
353353
crypto/chacha20.h \
354354
crypto/chacha20.cpp \
355355
crypto/common.h \
356+
crypto/hkdf_sha256_32.cpp \
357+
crypto/hkdf_sha256_32.h \
356358
crypto/hmac_sha256.cpp \
357359
crypto/hmac_sha256.h \
358360
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

src/key.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ void CKey::MakeNewKey(bool fCompressedIn) {
163163
fCompressed = fCompressedIn;
164164
}
165165

166+
bool CKey::Negate()
167+
{
168+
assert(fValid);
169+
return secp256k1_ec_privkey_negate(secp256k1_context_sign, keydata.data());
170+
}
171+
166172
CPrivKey CKey::GetPrivKey() const {
167173
assert(fValid);
168174
CPrivKey privkey;

src/key.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ class CKey
9898
//! Generate a new private key using a cryptographic PRNG.
9999
void MakeNewKey(bool fCompressed);
100100

101+
//! Negate private key
102+
bool Negate();
103+
101104
/**
102105
* Convert the private key to a CPrivKey (serialized OpenSSL private key data).
103106
* This is expensive.

src/test/crypto_tests.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
#include <crypto/aes.h>
66
#include <crypto/chacha20.h>
77
#include <crypto/poly1305.h>
8+
#include <crypto/hkdf_sha256_32.h>
9+
#include <crypto/hmac_sha256.h>
10+
#include <crypto/hmac_sha512.h>
811
#include <crypto/ripemd160.h>
912
#include <crypto/sha1.h>
1013
#include <crypto/sha256.h>
1114
#include <crypto/sha512.h>
12-
#include <crypto/hmac_sha256.h>
13-
#include <crypto/hmac_sha512.h>
1415
#include <random.h>
1516
#include <util/strencodings.h>
1617
#include <test/setup_common.h>
@@ -168,6 +169,22 @@ static void TestPoly1305(const std::string &hexmessage, const std::string &hexke
168169
BOOST_CHECK(tag == tagres);
169170
}
170171

172+
static void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &salt_hex, const std::string &info_hex, const std::string &okm_check_hex) {
173+
std::vector<unsigned char> initial_key_material = ParseHex(ikm_hex);
174+
std::vector<unsigned char> salt = ParseHex(salt_hex);
175+
std::vector<unsigned char> info = ParseHex(info_hex);
176+
177+
178+
// our implementation only supports strings for the "info" and "salt", stringify them
179+
std::string salt_stringified(reinterpret_cast<char*>(salt.data()), salt.size());
180+
std::string info_stringified(reinterpret_cast<char*>(info.data()), info.size());
181+
182+
CHKDF_HMAC_SHA256_L32 hkdf32(initial_key_material.data(), initial_key_material.size(), salt_stringified);
183+
unsigned char out[32];
184+
hkdf32.Expand32(info_stringified, out);
185+
BOOST_CHECK(HexStr(out, out + 32) == okm_check_hex);
186+
}
187+
171188
static std::string LongTestString() {
172189
std::string ret;
173190
for (int i=0; i<200000; i++) {
@@ -548,6 +565,26 @@ BOOST_AUTO_TEST_CASE(poly1305_testvector)
548565
"13000000000000000000000000000000");
549566
}
550567

568+
BOOST_AUTO_TEST_CASE(hkdf_hmac_sha256_l32_tests)
569+
{
570+
// Use rfc5869 test vectors but trucated to 32 bytes (our implementation only support length 32)
571+
TestHKDF_SHA256_32(
572+
/* IKM */ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
573+
/* salt */ "000102030405060708090a0b0c",
574+
/* info */ "f0f1f2f3f4f5f6f7f8f9",
575+
/* expected OKM */ "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf");
576+
TestHKDF_SHA256_32(
577+
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
578+
"606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
579+
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
580+
"b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c");
581+
TestHKDF_SHA256_32(
582+
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
583+
"",
584+
"",
585+
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d");
586+
}
587+
551588
BOOST_AUTO_TEST_CASE(countbits_tests)
552589
{
553590
FastRandomContext ctx;

src/test/key_tests.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,36 @@ BOOST_AUTO_TEST_CASE(key_signature_tests)
188188
BOOST_CHECK(found_small);
189189
}
190190

191+
BOOST_AUTO_TEST_CASE(key_key_negation)
192+
{
193+
// create a dummy hash for signature comparison
194+
unsigned char rnd[8];
195+
std::string str = "Bitcoin key verification\n";
196+
GetRandBytes(rnd, sizeof(rnd));
197+
uint256 hash;
198+
CHash256().Write((unsigned char*)str.data(), str.size()).Write(rnd, sizeof(rnd)).Finalize(hash.begin());
199+
200+
// import the static test key
201+
CKey key = DecodeSecret(strSecret1C);
202+
203+
// create a signature
204+
std::vector<unsigned char> vch_sig;
205+
std::vector<unsigned char> vch_sig_cmp;
206+
key.Sign(hash, vch_sig);
207+
208+
// negate the key twice
209+
BOOST_CHECK(key.GetPubKey().data()[0] == 0x03);
210+
key.Negate();
211+
// after the first negation, the signature must be different
212+
key.Sign(hash, vch_sig_cmp);
213+
BOOST_CHECK(vch_sig_cmp != vch_sig);
214+
BOOST_CHECK(key.GetPubKey().data()[0] == 0x02);
215+
key.Negate();
216+
// after the second negation, we should have the original key and thus the
217+
// same signature
218+
key.Sign(hash, vch_sig_cmp);
219+
BOOST_CHECK(vch_sig_cmp == vch_sig);
220+
BOOST_CHECK(key.GetPubKey().data()[0] == 0x03);
221+
}
222+
191223
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)