Skip to content

Commit 15a5c0b

Browse files
Add PBKDF2 HMAC functions
1 parent b2da40d commit 15a5c0b

File tree

7 files changed

+792
-0
lines changed

7 files changed

+792
-0
lines changed

src/crypto/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ add_library(bitcoin_crypto STATIC EXCLUDE_FROM_ALL
88
chacha20poly1305.cpp
99
hex_base.cpp
1010
hkdf_sha256_32.cpp
11+
hmac_ripemd160.cpp
12+
hmac_sha1.cpp
1113
hmac_sha256.cpp
1214
hmac_sha512.cpp
1315
muhash.cpp
16+
pbkdf2_hmac.cpp
1417
poly1305.cpp
1518
ripemd160.cpp
1619
sha1.cpp

src/crypto/hmac_ripemd160.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2014-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/hmac_ripemd160.h>
6+
7+
#include <string.h>
8+
9+
CHMAC_RIPEMD160::CHMAC_RIPEMD160(const unsigned char* key, size_t keylen)
10+
{
11+
unsigned char rkey[64];
12+
if (keylen <= 64) {
13+
memcpy(rkey, key, keylen);
14+
memset(rkey + keylen, 0, 64 - keylen);
15+
} else {
16+
CRIPEMD160().Write(key, keylen).Finalize(rkey);
17+
memset(rkey + 20, 0, 20);
18+
}
19+
20+
for (int n = 0; n < 64; n++)
21+
rkey[n] ^= 0x5c;
22+
outer.Write(rkey, 64);
23+
24+
for (int n = 0; n < 64; n++)
25+
rkey[n] ^= 0x5c ^ 0x36;
26+
inner.Write(rkey, 64);
27+
}
28+
29+
void CHMAC_RIPEMD160::Finalize(unsigned char hash[OUTPUT_SIZE])
30+
{
31+
unsigned char temp[20];
32+
inner.Finalize(temp);
33+
outer.Write(temp, 20).Finalize(hash);
34+
}

src/crypto/hmac_ripemd160.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2014-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_HMAC_RIPEMD160_H
6+
#define BITCOIN_CRYPTO_HMAC_RIPEMD160_H
7+
8+
#include <crypto/ripemd160.h>
9+
10+
#include <stdint.h>
11+
#include <stdlib.h>
12+
13+
/** A hasher class for HMAC-RIPEMD-160. */
14+
class CHMAC_RIPEMD160
15+
{
16+
private:
17+
CRIPEMD160 outer;
18+
CRIPEMD160 inner;
19+
20+
public:
21+
static const size_t OUTPUT_SIZE = 20;
22+
23+
CHMAC_RIPEMD160(const unsigned char* key, size_t keylen);
24+
CHMAC_RIPEMD160& Write(const unsigned char* data, size_t len)
25+
{
26+
inner.Write(data, len);
27+
return *this;
28+
}
29+
void Finalize(unsigned char hash[OUTPUT_SIZE]);
30+
};
31+
32+
#endif // BITCOIN_CRYPTO_HMAC_RIPEMD160_H

src/crypto/hmac_sha1.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2014-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/hmac_sha1.h>
6+
7+
#include <string.h>
8+
9+
CHMAC_SHA1::CHMAC_SHA1(const unsigned char* key, size_t keylen)
10+
{
11+
unsigned char rkey[64];
12+
if (keylen <= 64) {
13+
memcpy(rkey, key, keylen);
14+
memset(rkey + keylen, 0, 64 - keylen);
15+
} else {
16+
CSHA1().Write(key, keylen).Finalize(rkey);
17+
memset(rkey + 20, 0, 20);
18+
}
19+
20+
for (int n = 0; n < 64; n++)
21+
rkey[n] ^= 0x5c;
22+
outer.Write(rkey, 64);
23+
24+
for (int n = 0; n < 64; n++)
25+
rkey[n] ^= 0x5c ^ 0x36;
26+
inner.Write(rkey, 64);
27+
}
28+
29+
void CHMAC_SHA1::Finalize(unsigned char hash[OUTPUT_SIZE])
30+
{
31+
unsigned char temp[20];
32+
inner.Finalize(temp);
33+
outer.Write(temp, 20).Finalize(hash);
34+
}

src/crypto/hmac_sha1.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2014-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_HMAC_SHA1_H
6+
#define BITCOIN_CRYPTO_HMAC_SHA1_H
7+
8+
#include <crypto/sha1.h>
9+
10+
#include <stdint.h>
11+
#include <stdlib.h>
12+
13+
/** A hasher class for HMAC-SHA-1. */
14+
class CHMAC_SHA1
15+
{
16+
private:
17+
CSHA1 outer;
18+
CSHA1 inner;
19+
20+
public:
21+
static const size_t OUTPUT_SIZE = 20;
22+
23+
CHMAC_SHA1(const unsigned char* key, size_t keylen);
24+
CHMAC_SHA1& Write(const unsigned char* data, size_t len)
25+
{
26+
inner.Write(data, len);
27+
return *this;
28+
}
29+
void Finalize(unsigned char hash[OUTPUT_SIZE]);
30+
};
31+
32+
#endif // BITCOIN_CRYPTO_HMAC_SHA1_H

0 commit comments

Comments
 (0)