Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ccan/ccan/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,16 @@
#define WARN_UNUSED_RESULT
#endif
#endif

/* ALIGNED - ensure a structure/variable is aligned to a given number of bytes
*
*/
#ifndef ALIGNED
#if (defined(__clang__) || defined(__GNUC__))
#define ALIGNED(N) __attribute__((aligned(N)))
#else
#define ALIGNED(N)
#endif
#endif /* ALIGNED */

#endif /* CCAN_COMPILER_H */
1 change: 1 addition & 0 deletions src/ccan/ccan/crypto/sha256/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ inline void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size)
inline void sha256_done(struct sha256_ctx *ctx, struct sha256* res)
{
mbedtls_sha256_finish(&ctx->c, res->u.u8);
mbedtls_sha256_free(&ctx->c);
}
void sha256_optimize(void)
{
Expand Down
3 changes: 0 additions & 3 deletions src/ccan/ccan/crypto/sha256/sha256_sse4.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include <stdlib.h>

#if defined(__x86_64__) || defined(__amd64__)
/* TODO: Support alignment in compiler.h */
#define ALIGNED(N) __attribute__((aligned(N)))

void TransformSSE4(uint32_t* s, const uint32_t* chunk, size_t blocks)
{
static const uint32_t K256[] ALIGNED(16) = {
Expand Down
26 changes: 14 additions & 12 deletions src/ccan/ccan/crypto/sha512/sha512.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,19 @@
#endif
#include <string.h>

#ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
static void invalidate_sha512(struct sha512_ctx *ctx)
{
#ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
ctx->c.md_len = 0;
#elif defined(CCAN_CRYPTO_SHA512_USE_MBEDTLS)
#else
ctx->bytes = (size_t)-1;
#endif
}

static void check_sha512(struct sha512_ctx *ctx UNUSED)
{
#if 0
#ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
assert(ctx->c.md_len != 0);
#else
assert(ctx->bytes != (size_t)-1);
#endif
#endif
}

#ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
void sha512_init(struct sha512_ctx *ctx)
{
SHA512_Init(&ctx->c);
Expand All @@ -62,16 +53,27 @@ inline void sha512_init(struct sha512_ctx *ctx)

inline void sha512_update(struct sha512_ctx *ctx, const void *p, size_t size)
{
check_sha512(ctx);
mbedtls_sha512_update(&ctx->c, p, size);
}

inline void sha512_done(struct sha512_ctx *ctx, struct sha512* res)
{
mbedtls_sha512_finish(&ctx->c, res->u.u8);
invalidate_sha512(ctx);
mbedtls_sha512_free(&ctx->c);
}
#else
static void invalidate_sha512(struct sha512_ctx *ctx)
{
ctx->bytes = (size_t)-1;
}

static void check_sha512(struct sha512_ctx *ctx UNUSED)
{
#if 0
assert(ctx->bytes != (size_t)-1);
#endif
}

static uint64_t Ch(uint64_t x, uint64_t y, uint64_t z)
{
return z ^ (x & (y ^ z));
Expand Down
26 changes: 19 additions & 7 deletions src/tx_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

#define SIGTYPE_ALL (WALLY_SIGTYPE_PRE_SW | WALLY_SIGTYPE_SW_V0 | WALLY_SIGTYPE_SW_V1)

#if defined(CCAN_CRYPTO_SHA256_USE_OPENSSL) || defined(CCAN_CRYPTO_SHA256_USE_MBEDTLS)
/* For external sha256 implementations, we cannot cache the sha256 context as
* they require extra setup before use that only sha256_init() provides.
*/
#define TXIO_CTX_CACHEABLE 0
#else
/* For our built-in sha256 implementation we can cache and use the context */
#define TXIO_CTX_CACHEABLE 1
#endif

/* Cache keys for data that is constant while signing a given tx.
* We also cache other data keyed by their binary value directly.
*/
Expand Down Expand Up @@ -761,20 +771,22 @@ static int bip143_signature_hash(
static void txio_bip341_init(cursor_io *io,
const unsigned char *genesis_blockhash, size_t genesis_blockhash_len)
{
const struct wally_map_item *item;
item = io->cache ? wally_map_get_integer(io->cache, TXIO_SHA_TAPSIGHASH_CTX) : NULL;
if (item) {
/* Note we hash the intial sha256_ctx itself here and so memcpy it */
memcpy(&io->ctx, item->value, item->value_len);
return;
if (TXIO_CTX_CACHEABLE && io->cache) {
const struct wally_map_item *item = NULL;
item = wally_map_get_integer(io->cache, TXIO_SHA_TAPSIGHASH_CTX);
if (item) {
/* Note we cached the intial sha256_ctx itself here and so memcpy it */
memcpy(&io->ctx, item->value, item->value_len);
return;
}
}

tagged_hash_init(&io->ctx, TAPSIGHASH_SHA256(genesis_blockhash != NULL), SHA256_LEN);
if (genesis_blockhash) {
hash_bytes(&io->ctx, genesis_blockhash, genesis_blockhash_len);
hash_bytes(&io->ctx, genesis_blockhash, genesis_blockhash_len);
}
if (io->cache)
if (TXIO_CTX_CACHEABLE && io->cache)
wally_map_add_integer(io->cache, TXIO_SHA_TAPSIGHASH_CTX,
(const unsigned char*)&io->ctx, sizeof(io->ctx));
}
Expand Down