Skip to content

Commit

Permalink
external/bitcoin-sha256/: Use SHA256 implementation from Bitcoin.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZmnSCPxj committed Mar 3, 2021
1 parent bf8a447 commit 968f210
Show file tree
Hide file tree
Showing 9 changed files with 828 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,12 @@ libclboss_la_SOURCES = \

libclboss_la_LIBADD = \
external/bitcoin-ripemd160/libbitcoin-ripemd160.la \
external/bitcoin-sha256/libbitcoin-sha256.la \
external/libsodium/src/libsodium/libsodium.la \
external/secp256k1/libsecp256k1.la
libclboss_la_CPPFLAGS = \
-I$(top_srcdir)/external/bitcoin-ripemd160/ \
-I$(top_srcdir)/external/bitcoin-sha256/ \
-I$(top_srcdir)/external/libsodium/src/libsodium/include/ \
-I$(top_srcdir)/external/secp256k1/include/ \
-I$(top_srcdir)/external/jsmn
Expand All @@ -465,6 +467,7 @@ dev_boltz_CPPFLAGS = \
# Externals
SUBDIRS = \
external/bitcoin-ripemd160 \
external/bitcoin-sha256 \
external/libsodium/ \
external/secp256k1/
EXTRA_DIST = \
Expand Down
12 changes: 5 additions & 7 deletions Sha256/Hasher.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
#include"Sha256/Hash.hpp"
#include"Sha256/Hasher.hpp"
#include"Util/make_unique.hpp"
#include<sodium/crypto_hash_sha256.h>
#include<crypto/sha256.h>
#include<sodium/utils.h>

namespace Sha256 {

class Hasher::Impl {
private:
crypto_hash_sha256_state s;
CSHA256 s;

public:
Impl() {
crypto_hash_sha256_init(&s);
}
Impl() { }
~Impl() {
sodium_memzero(&s, sizeof(s));
}
Impl(Impl const&) =default;

void feed(void const* p, std::size_t len) {
crypto_hash_sha256_update(&s, (unsigned char const*) p, len);
s.Write((const unsigned char*) p, len);
}
void finalize(std::uint8_t buff[32]) {
crypto_hash_sha256_final(&s, buff);
s.Finalize((unsigned char *) buff);
}
};

Expand Down
5 changes: 4 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ int fake_printf(void* dummy, char const* fmt, ...)

# Checks for library functions.

AC_CONFIG_FILES([Makefile external/bitcoin-ripemd160/Makefile])
AC_CONFIG_FILES([Makefile
external/bitcoin-ripemd160/Makefile
external/bitcoin-sha256/Makefile
])
AC_CONFIG_SUBDIRS([external/libsodium
external/secp256k1
])
Expand Down
7 changes: 7 additions & 0 deletions external/bitcoin-sha256/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
noinst_LTLIBRARIES = libbitcoin-sha256.la
libbitcoin_sha256_la_SOURCES = \
compat/cpuid.h \
crypto/common.h \
crypto/sha256.cpp \
crypto/sha256.h
AM_CPPFLAGS = -I $(top_srcdir)
7 changes: 7 additions & 0 deletions external/bitcoin-sha256/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This is the SHA256 implementation copied from the
obscure project https://github.com/bitcoin/bitcoin .

Specifically, the files `crypto/sha256.cpp` and
`crypto/sha256.h` are copied verbatim, while the
files `crypto/common.h` and `compat/cpuid.h` are minimal
shims to get the SHA256 implementation working.
6 changes: 6 additions & 0 deletions external/bitcoin-sha256/compat/cpuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef BITCOIN_SHA256_COMPAT_CPUID_H
#define BITCOIN_SHA256_COMPAT_CPUID_H

/* Created by ZmnSCPxj as a minimal compatibility shim. */

#endif /* !defined(BITCOIN_SHA256_COMPAT_CPUID_H) */
36 changes: 36 additions & 0 deletions external/bitcoin-sha256/crypto/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef BITCOIN_SHA256_CRYPTO_COMMON_H
#define BITCOIN_SHA256_CRYPTO_COMMON_H

/* Created by ZmnSCPxj as a minimal compatibility shim. */

#include<stdint.h>

namespace {

void WriteBE32(unsigned char* ptr, uint32_t x) {
*(ptr++) = uint8_t((x >> 24) & 0xFF);
*(ptr++) = uint8_t((x >> 16) & 0xFF);
*(ptr++) = uint8_t((x >> 8) & 0xFF);
*(ptr++) = uint8_t((x >> 0) & 0xFF);
}
void WriteBE64(unsigned char* ptr, uint64_t x) {
*(ptr++) = uint8_t((x >> 56) & 0xFF);
*(ptr++) = uint8_t((x >> 48) & 0xFF);
*(ptr++) = uint8_t((x >> 40) & 0xFF);
*(ptr++) = uint8_t((x >> 32) & 0xFF);
*(ptr++) = uint8_t((x >> 24) & 0xFF);
*(ptr++) = uint8_t((x >> 16) & 0xFF);
*(ptr++) = uint8_t((x >> 8) & 0xFF);
*(ptr++) = uint8_t((x >> 0) & 0xFF);
}
uint32_t ReadBE32(unsigned char const* ptr) {
return (uint32_t(ptr[0]) << 24)
| (uint32_t(ptr[1]) << 16)
| (uint32_t(ptr[2]) << 8)
| (uint32_t(ptr[3]) << 0)
;
}

}

#endif /* !defined(BITCOIN_SHA256_CRYPTO_COMMON_H) */
Loading

0 comments on commit 968f210

Please sign in to comment.