Skip to content

Commit f250a21

Browse files
laanwjgades
authored andcommitted
Merge bitcoin#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 bitcoin#14032 and a pre-requirement for the v2 transport protocol. ACKs for commit 8794a4: Tree-SHA512: 5341929dfa29f5da766ec3612784baec6a3ad69972f08b5a985a8aafdae4dae36f104a2b888d1f5d1f33561456bd111f960d7e32c2cc4fd18e48358468f26c1a
1 parent a1a5dbf commit f250a21

7 files changed

+128
-2
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ crypto_libcosanta_crypto_base_a_SOURCES = \
463463
crypto/chacha20.h \
464464
crypto/chacha20.cpp \
465465
crypto/common.h \
466+
crypto/hkdf_sha256_32.cpp \
467+
crypto/hkdf_sha256_32.h \
466468
crypto/hmac_sha256.cpp \
467469
crypto/hmac_sha256.h \
468470
crypto/hmac_sha512.cpp \

src/crypto/hkdf_sha256_32.cpp

+21
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

+25
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

+6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ void CKey::MakeNewKey(bool fCompressedIn) {
164164
fCompressed = fCompressedIn;
165165
}
166166

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

src/key.h

+3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class CKey
9999
//! Generate a new private key using a cryptographic PRNG.
100100
void MakeNewKey(bool fCompressed);
101101

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

src/test/crypto_tests.cpp

+39-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
#include <crypto/chacha20.h>
77
#include <crypto/chacha_poly_aead.h>
88
#include <crypto/poly1305.h>
9+
#include <crypto/hkdf_sha256_32.h>
10+
#include <crypto/hmac_sha256.h>
11+
#include <crypto/hmac_sha512.h>
912
#include <crypto/ripemd160.h>
1013
#include <crypto/sha1.h>
1114
#include <crypto/sha256.h>
1215
#include <crypto/sha3.h>
1316
#include <crypto/sha512.h>
14-
#include <crypto/hmac_sha256.h>
15-
#include <crypto/hmac_sha512.h>
1617
#include <random.h>
1718
#include <util/strencodings.h>
1819
#include <test/test_cosanta.h>
@@ -233,6 +234,22 @@ static void TestPoly1305(const std::string &hexmessage, const std::string &hexke
233234
BOOST_CHECK(tag == tagres);
234235
}
235236

237+
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) {
238+
std::vector<unsigned char> initial_key_material = ParseHex(ikm_hex);
239+
std::vector<unsigned char> salt = ParseHex(salt_hex);
240+
std::vector<unsigned char> info = ParseHex(info_hex);
241+
242+
243+
// our implementation only supports strings for the "info" and "salt", stringify them
244+
std::string salt_stringified(reinterpret_cast<char*>(salt.data()), salt.size());
245+
std::string info_stringified(reinterpret_cast<char*>(info.data()), info.size());
246+
247+
CHKDF_HMAC_SHA256_L32 hkdf32(initial_key_material.data(), initial_key_material.size(), salt_stringified);
248+
unsigned char out[32];
249+
hkdf32.Expand32(info_stringified, out);
250+
BOOST_CHECK(HexStr(out) == okm_check_hex);
251+
}
252+
236253
static std::string LongTestString() {
237254
std::string ret;
238255
for (int i=0; i<200000; i++) {
@@ -621,6 +638,26 @@ BOOST_AUTO_TEST_CASE(poly1305_testvector)
621638
"13000000000000000000000000000000");
622639
}
623640

641+
BOOST_AUTO_TEST_CASE(hkdf_hmac_sha256_l32_tests)
642+
{
643+
// Use rfc5869 test vectors but trucated to 32 bytes (our implementation only support length 32)
644+
TestHKDF_SHA256_32(
645+
/* IKM */ "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
646+
/* salt */ "000102030405060708090a0b0c",
647+
/* info */ "f0f1f2f3f4f5f6f7f8f9",
648+
/* expected OKM */ "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf");
649+
TestHKDF_SHA256_32(
650+
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
651+
"606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
652+
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
653+
"b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c");
654+
TestHKDF_SHA256_32(
655+
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
656+
"",
657+
"",
658+
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d");
659+
}
660+
624661
static void TestChaCha20Poly1305AEAD(bool must_succeed, unsigned int expected_aad_length, const std::string& hex_m, const std::string& hex_k1, const std::string& hex_k2, const std::string& hex_aad_keystream, const std::string& hex_encrypted_message, const std::string& hex_encrypted_message_seq_999)
625662
{
626663
// we need two sequence numbers, one for the payload cipher instance...

src/test/key_tests.cpp

+32
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(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
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)