btcs

bitcoin script parser/evaluator/compiler/decompiler
git clone git://jb55.com/btcs
Log | Files | Refs | README | LICENSE

commit cd7300d83144b66c936a32bc5d1009964fec6c99
parent 4ec313b99db973af87e88c6421fb4fb228531341
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  3 Jun 2019 12:12:56 -0700

add some missing files

Diffstat:
Acompiler.c | 12++++++++++++
Acompiler.h | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aendian.h | 360+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aripemd160.c | 404+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aripemd160.h | 148+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asha256.c | 321+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asha256.h | 158+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 1458 insertions(+), 0 deletions(-)

diff --git a/compiler.c b/compiler.c @@ -0,0 +1,12 @@ + +#include <stdlib.h> +#include "compiler.h" + +bool alignment_ok(const void *p, size_t n) +{ +#if HAVE_UNALIGNED_ACCESS + return true; +#else + return ((size_t)p % n == 0); +#endif +} diff --git a/compiler.h b/compiler.h @@ -0,0 +1,55 @@ + +#ifndef COMPILER_H +#define COMPILER_H + +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +bool alignment_ok(const void *p, size_t n); + +#define UNUSED __attribute__((__unused__)) + +/** + * BUILD_ASSERT - assert a build-time dependency. + * @cond: the compile-time condition which must be true. + * + * Your compile will fail if the condition isn't true, or can't be evaluated + * by the compiler. This can only be used within a function. + * + * Example: + * #include <stddef.h> + * ... + * static char *foo_to_char(struct foo *foo) + * { + * // This code needs string to be at start of foo. + * BUILD_ASSERT(offsetof(struct foo, string) == 0); + * return (char *)foo; + * } + */ +#define BUILD_ASSERT(cond) \ + do { (void) sizeof(char [1 - 2*!(cond)]); } while(0) + +/** + * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression. + * @cond: the compile-time condition which must be true. + * + * Your compile will fail if the condition isn't true, or can't be evaluated + * by the compiler. This can be used in an expression: its value is "0". + * + * Example: + * #define foo_to_char(foo) \ + * ((char *)(foo) \ + * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0)) + */ +#define BUILD_ASSERT_OR_ZERO(cond) \ + (sizeof(char [1 - 2*!(cond)]) - 1) + +#define memclear(mem, size) memset(mem, 0, size) +#define memclear_2(m1, s1, m2, s2) { memclear(m1, s1); memclear(m2, s2); } +#define memclear_3(m1, s1, m2, s2, m3, s3) { memclear(m1, s1); memclear(m2, s2); memclear(m3, s3); } +#define wally_clear memclear +#define wally_clear_2 memclear_2 +#define wally_clear_3 memclear_3 + +#endif /* COMPILER_H */ diff --git a/endian.h b/endian.h @@ -0,0 +1,360 @@ +/* CC0 (Public domain) */ +#ifndef CCAN_ENDIAN_H +#define CCAN_ENDIAN_H +#include <stdint.h> + +/** + * BSWAP_16 - reverse bytes in a constant uint16_t value. + * @val: constant value whose bytes to swap. + * + * Designed to be usable in constant-requiring initializers. + * + * Example: + * struct mystruct { + * char buf[BSWAP_16(0x1234)]; + * }; + */ +#define BSWAP_16(val) \ + ((((uint16_t)(val) & 0x00ff) << 8) \ + | (((uint16_t)(val) & 0xff00) >> 8)) + +/** + * BSWAP_32 - reverse bytes in a constant uint32_t value. + * @val: constant value whose bytes to swap. + * + * Designed to be usable in constant-requiring initializers. + * + * Example: + * struct mystruct { + * char buf[BSWAP_32(0xff000000)]; + * }; + */ +#define BSWAP_32(val) \ + ((((uint32_t)(val) & 0x000000ff) << 24) \ + | (((uint32_t)(val) & 0x0000ff00) << 8) \ + | (((uint32_t)(val) & 0x00ff0000) >> 8) \ + | (((uint32_t)(val) & 0xff000000) >> 24)) + +/** + * BSWAP_64 - reverse bytes in a constant uint64_t value. + * @val: constantvalue whose bytes to swap. + * + * Designed to be usable in constant-requiring initializers. + * + * Example: + * struct mystruct { + * char buf[BSWAP_64(0xff00000000000000ULL)]; + * }; + */ +#define BSWAP_64(val) \ + ((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \ + | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \ + | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \ + | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \ + | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \ + | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \ + | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \ + | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56)) + +#if HAVE_BYTESWAP_H +#include <byteswap.h> +#else +/** + * bswap_16 - reverse bytes in a uint16_t value. + * @val: value whose bytes to swap. + * + * Example: + * // Output contains "1024 is 4 as two bytes reversed" + * printf("1024 is %u as two bytes reversed\n", bswap_16(1024)); + */ +static inline uint16_t bswap_16(uint16_t val) +{ + return BSWAP_16(val); +} + +/** + * bswap_32 - reverse bytes in a uint32_t value. + * @val: value whose bytes to swap. + * + * Example: + * // Output contains "1024 is 262144 as four bytes reversed" + * printf("1024 is %u as four bytes reversed\n", bswap_32(1024)); + */ +static inline uint32_t bswap_32(uint32_t val) +{ + return BSWAP_32(val); +} +#endif /* !HAVE_BYTESWAP_H */ + +#if !HAVE_BSWAP_64 +/** + * bswap_64 - reverse bytes in a uint64_t value. + * @val: value whose bytes to swap. + * + * Example: + * // Output contains "1024 is 1125899906842624 as eight bytes reversed" + * printf("1024 is %llu as eight bytes reversed\n", + * (unsigned long long)bswap_64(1024)); + */ +static inline uint64_t bswap_64(uint64_t val) +{ + return BSWAP_64(val); +} +#endif + +/* Needed for Glibc like endiness check */ +#define __LITTLE_ENDIAN 1234 +#define __BIG_ENDIAN 4321 + +/* Sanity check the defines. We don't handle weird endianness. */ +#if HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN +#error "Can't compile for both big and little endian." +#elif HAVE_LITTLE_ENDIAN +#ifndef __BYTE_ORDER +#define __BYTE_ORDER __LITTLE_ENDIAN +#elif __BYTE_ORDER != __LITTLE_ENDIAN +#error "__BYTE_ORDER already defined, but not equal to __LITTLE_ENDIAN" +#endif +#elif HAVE_BIG_ENDIAN +#ifndef __BYTE_ORDER +#define __BYTE_ORDER __BIG_ENDIAN +#elif __BYTE_ORDER != __BIG_ENDIAN +#error "__BYTE_ORDER already defined, but not equal to __BIG_ENDIAN" +#endif +#endif + + +#ifdef __CHECKER__ +/* sparse needs forcing to remove bitwise attribute from ccan/short_types */ +#define ENDIAN_CAST __attribute__((force)) +#define ENDIAN_TYPE __attribute__((bitwise)) +#else +#define ENDIAN_CAST +#define ENDIAN_TYPE +#endif + +typedef uint64_t ENDIAN_TYPE leint64_t; +typedef uint64_t ENDIAN_TYPE beint64_t; +typedef uint32_t ENDIAN_TYPE leint32_t; +typedef uint32_t ENDIAN_TYPE beint32_t; +typedef uint16_t ENDIAN_TYPE leint16_t; +typedef uint16_t ENDIAN_TYPE beint16_t; + +#if HAVE_LITTLE_ENDIAN +/** + * CPU_TO_LE64 - convert a constant uint64_t value to little-endian + * @native: constant to convert + */ +#define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)(native)) + +/** + * CPU_TO_LE32 - convert a constant uint32_t value to little-endian + * @native: constant to convert + */ +#define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)(native)) + +/** + * CPU_TO_LE16 - convert a constant uint16_t value to little-endian + * @native: constant to convert + */ +#define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)(native)) + +/** + * LE64_TO_CPU - convert a little-endian uint64_t constant + * @le_val: little-endian constant to convert + */ +#define LE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val)) + +/** + * LE32_TO_CPU - convert a little-endian uint32_t constant + * @le_val: little-endian constant to convert + */ +#define LE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val)) + +/** + * LE16_TO_CPU - convert a little-endian uint16_t constant + * @le_val: little-endian constant to convert + */ +#define LE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val)) + +#else /* ... HAVE_BIG_ENDIAN */ +#define CPU_TO_LE64(native) ((ENDIAN_CAST leint64_t)BSWAP_64(native)) +#define CPU_TO_LE32(native) ((ENDIAN_CAST leint32_t)BSWAP_32(native)) +#define CPU_TO_LE16(native) ((ENDIAN_CAST leint16_t)BSWAP_16(native)) +#define LE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val) +#define LE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val) +#define LE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val) +#endif /* HAVE_BIG_ENDIAN */ + +#if HAVE_BIG_ENDIAN +/** + * CPU_TO_BE64 - convert a constant uint64_t value to big-endian + * @native: constant to convert + */ +#define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)(native)) + +/** + * CPU_TO_BE32 - convert a constant uint32_t value to big-endian + * @native: constant to convert + */ +#define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)(native)) + +/** + * CPU_TO_BE16 - convert a constant uint16_t value to big-endian + * @native: constant to convert + */ +#define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)(native)) + +/** + * BE64_TO_CPU - convert a big-endian uint64_t constant + * @le_val: big-endian constant to convert + */ +#define BE64_TO_CPU(le_val) ((ENDIAN_CAST uint64_t)(le_val)) + +/** + * BE32_TO_CPU - convert a big-endian uint32_t constant + * @le_val: big-endian constant to convert + */ +#define BE32_TO_CPU(le_val) ((ENDIAN_CAST uint32_t)(le_val)) + +/** + * BE16_TO_CPU - convert a big-endian uint16_t constant + * @le_val: big-endian constant to convert + */ +#define BE16_TO_CPU(le_val) ((ENDIAN_CAST uint16_t)(le_val)) + +#else /* ... HAVE_LITTLE_ENDIAN */ +#define CPU_TO_BE64(native) ((ENDIAN_CAST beint64_t)BSWAP_64(native)) +#define CPU_TO_BE32(native) ((ENDIAN_CAST beint32_t)BSWAP_32(native)) +#define CPU_TO_BE16(native) ((ENDIAN_CAST beint16_t)BSWAP_16(native)) +#define BE64_TO_CPU(le_val) BSWAP_64((ENDIAN_CAST uint64_t)le_val) +#define BE32_TO_CPU(le_val) BSWAP_32((ENDIAN_CAST uint32_t)le_val) +#define BE16_TO_CPU(le_val) BSWAP_16((ENDIAN_CAST uint16_t)le_val) +#endif /* HAVE_LITTE_ENDIAN */ + + +/** + * cpu_to_le64 - convert a uint64_t value to little-endian + * @native: value to convert + */ +static inline leint64_t cpu_to_le64(uint64_t native) +{ + return CPU_TO_LE64(native); +} + +/** + * cpu_to_le32 - convert a uint32_t value to little-endian + * @native: value to convert + */ +static inline leint32_t cpu_to_le32(uint32_t native) +{ + return CPU_TO_LE32(native); +} + +/** + * cpu_to_le16 - convert a uint16_t value to little-endian + * @native: value to convert + */ +static inline leint16_t cpu_to_le16(uint16_t native) +{ + return CPU_TO_LE16(native); +} + +/** + * le64_to_cpu - convert a little-endian uint64_t value + * @le_val: little-endian value to convert + */ +static inline uint64_t le64_to_cpu(leint64_t le_val) +{ + return LE64_TO_CPU(le_val); +} + +/** + * le32_to_cpu - convert a little-endian uint32_t value + * @le_val: little-endian value to convert + */ +static inline uint32_t le32_to_cpu(leint32_t le_val) +{ + return LE32_TO_CPU(le_val); +} + +/** + * le16_to_cpu - convert a little-endian uint16_t value + * @le_val: little-endian value to convert + */ +static inline uint16_t le16_to_cpu(leint16_t le_val) +{ + return LE16_TO_CPU(le_val); +} + +/** + * cpu_to_be64 - convert a uint64_t value to big endian. + * @native: value to convert + */ +static inline beint64_t cpu_to_be64(uint64_t native) +{ + return CPU_TO_BE64(native); +} + +/** + * cpu_to_be32 - convert a uint32_t value to big endian. + * @native: value to convert + */ +static inline beint32_t cpu_to_be32(uint32_t native) +{ + return CPU_TO_BE32(native); +} + +/** + * cpu_to_be16 - convert a uint16_t value to big endian. + * @native: value to convert + */ +static inline beint16_t cpu_to_be16(uint16_t native) +{ + return CPU_TO_BE16(native); +} + +/** + * be64_to_cpu - convert a big-endian uint64_t value + * @be_val: big-endian value to convert + */ +static inline uint64_t be64_to_cpu(beint64_t be_val) +{ + return BE64_TO_CPU(be_val); +} + +/** + * be32_to_cpu - convert a big-endian uint32_t value + * @be_val: big-endian value to convert + */ +static inline uint32_t be32_to_cpu(beint32_t be_val) +{ + return BE32_TO_CPU(be_val); +} + +/** + * be16_to_cpu - convert a big-endian uint16_t value + * @be_val: big-endian value to convert + */ +static inline uint16_t be16_to_cpu(beint16_t be_val) +{ + return BE16_TO_CPU(be_val); +} + +/* Whichever they include first, they get these definitions. */ +#ifdef CCAN_SHORT_TYPES_H +/** + * be64/be32/be16 - 64/32/16 bit big-endian representation. + */ +typedef beint64_t be64; +typedef beint32_t be32; +typedef beint16_t be16; + +/** + * le64/le32/le16 - 64/32/16 bit little-endian representation. + */ +typedef leint64_t le64; +typedef leint32_t le32; +typedef leint16_t le16; +#endif +#endif /* CCAN_ENDIAN_H */ diff --git a/ripemd160.c b/ripemd160.c @@ -0,0 +1,404 @@ +/* MIT (BSD) license - see LICENSE file for details */ +/* RIPEMD core code translated from the Bitcoin project's C++: + * + * src/crypto/ripemd160.cpp commit f914f1a746d7f91951c1da262a4a749dd3ebfa71 + * Copyright (c) 2014 The Bitcoin Core developers + * Distributed under the MIT software license, see the accompanying + * file COPYING or http://www.opensource.org/licenses/mit-license.php. + */ +#include "ripemd160.h" +#include "endian.h" +#include "compiler.h" +#include <stdbool.h> +#include <assert.h> +#include <string.h> + +static void invalidate_ripemd160(struct ripemd160_ctx *ctx) +{ +#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL + ctx->c.num = -1U; +#else + ctx->bytes = -1ULL; +#endif +} + +static void check_ripemd160(struct ripemd160_ctx *ctx) +{ +#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL + assert(ctx->c.num != -1U); +#else + assert(ctx->bytes != -1ULL); +#endif +} + +#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL +void ripemd160_init(struct ripemd160_ctx *ctx) +{ + RIPEMD160_Init(&ctx->c); +} + +void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size) +{ + check_ripemd160(ctx); + RIPEMD160_Update(&ctx->c, p, size); +} + +void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res) +{ + RIPEMD160_Final(res->u.u8, &ctx->c); + invalidate_ripemd160(ctx); +} +#else +inline static uint32_t f1(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; } +inline static uint32_t f2(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (~x & z); } +inline static uint32_t f3(uint32_t x, uint32_t y, uint32_t z) { return (x | ~y) ^ z; } +inline static uint32_t f4(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | (y & ~z); } +inline static uint32_t f5(uint32_t x, uint32_t y, uint32_t z) { return x ^ (y | ~z); } + +/** Initialize RIPEMD-160 state. */ +inline static void Initialize(uint32_t* s) +{ + s[0] = 0x67452301ul; + s[1] = 0xEFCDAB89ul; + s[2] = 0x98BADCFEul; + s[3] = 0x10325476ul; + s[4] = 0xC3D2E1F0ul; +} + +inline static uint32_t rol(uint32_t x, int i) { return (x << i) | (x >> (32 - i)); } + +inline static void Round(uint32_t *a, uint32_t b UNUSED, uint32_t *c, uint32_t d UNUSED, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r) +{ + *a = rol(*a + f + x + k, r) + e; + *c = rol(*c, 10); +} + +inline static void R11(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, *c, d), x, 0, r); } +inline static void R21(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, *c, d), x, 0x5A827999ul, r); } +inline static void R31(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, *c, d), x, 0x6ED9EBA1ul, r); } +inline static void R41(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, *c, d), x, 0x8F1BBCDCul, r); } +inline static void R51(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, *c, d), x, 0xA953FD4Eul, r); } + +inline static void R12(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f5(b, *c, d), x, 0x50A28BE6ul, r); } +inline static void R22(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f4(b, *c, d), x, 0x5C4DD124ul, r); } +inline static void R32(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f3(b, *c, d), x, 0x6D703EF3ul, r); } +inline static void R42(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f2(b, *c, d), x, 0x7A6D76E9ul, r); } +inline static void R52(uint32_t *a, uint32_t b, uint32_t *c, uint32_t d, uint32_t e, uint32_t x, int r) { Round(a, b, c, d, e, f1(b, *c, d), x, 0, r); } + +/** Perform a RIPEMD-160 transformation, processing a 64-byte chunk. */ +static void Transform(uint32_t *s, const uint32_t *chunk) +{ + uint32_t a1 = s[0], b1 = s[1], c1 = s[2], d1 = s[3], e1 = s[4]; + uint32_t a2 = a1, b2 = b1, c2 = c1, d2 = d1, e2 = e1; + uint32_t w0 = le32_to_cpu(chunk[0]), w1 = le32_to_cpu(chunk[1]), w2 = le32_to_cpu(chunk[2]), w3 = le32_to_cpu(chunk[3]); + uint32_t w4 = le32_to_cpu(chunk[4]), w5 = le32_to_cpu(chunk[5]), w6 = le32_to_cpu(chunk[6]), w7 = le32_to_cpu(chunk[7]); + uint32_t w8 = le32_to_cpu(chunk[8]), w9 = le32_to_cpu(chunk[9]), w10 = le32_to_cpu(chunk[10]), w11 = le32_to_cpu(chunk[11]); + uint32_t w12 = le32_to_cpu(chunk[12]), w13 = le32_to_cpu(chunk[13]), w14 = le32_to_cpu(chunk[14]), w15 = le32_to_cpu(chunk[15]); + uint32_t t; + + R11(&a1, b1, &c1, d1, e1, w0, 11); + R12(&a2, b2, &c2, d2, e2, w5, 8); + R11(&e1, a1, &b1, c1, d1, w1, 14); + R12(&e2, a2, &b2, c2, d2, w14, 9); + R11(&d1, e1, &a1, b1, c1, w2, 15); + R12(&d2, e2, &a2, b2, c2, w7, 9); + R11(&c1, d1, &e1, a1, b1, w3, 12); + R12(&c2, d2, &e2, a2, b2, w0, 11); + R11(&b1, c1, &d1, e1, a1, w4, 5); + R12(&b2, c2, &d2, e2, a2, w9, 13); + R11(&a1, b1, &c1, d1, e1, w5, 8); + R12(&a2, b2, &c2, d2, e2, w2, 15); + R11(&e1, a1, &b1, c1, d1, w6, 7); + R12(&e2, a2, &b2, c2, d2, w11, 15); + R11(&d1, e1, &a1, b1, c1, w7, 9); + R12(&d2, e2, &a2, b2, c2, w4, 5); + R11(&c1, d1, &e1, a1, b1, w8, 11); + R12(&c2, d2, &e2, a2, b2, w13, 7); + R11(&b1, c1, &d1, e1, a1, w9, 13); + R12(&b2, c2, &d2, e2, a2, w6, 7); + R11(&a1, b1, &c1, d1, e1, w10, 14); + R12(&a2, b2, &c2, d2, e2, w15, 8); + R11(&e1, a1, &b1, c1, d1, w11, 15); + R12(&e2, a2, &b2, c2, d2, w8, 11); + R11(&d1, e1, &a1, b1, c1, w12, 6); + R12(&d2, e2, &a2, b2, c2, w1, 14); + R11(&c1, d1, &e1, a1, b1, w13, 7); + R12(&c2, d2, &e2, a2, b2, w10, 14); + R11(&b1, c1, &d1, e1, a1, w14, 9); + R12(&b2, c2, &d2, e2, a2, w3, 12); + R11(&a1, b1, &c1, d1, e1, w15, 8); + R12(&a2, b2, &c2, d2, e2, w12, 6); + + R21(&e1, a1, &b1, c1, d1, w7, 7); + R22(&e2, a2, &b2, c2, d2, w6, 9); + R21(&d1, e1, &a1, b1, c1, w4, 6); + R22(&d2, e2, &a2, b2, c2, w11, 13); + R21(&c1, d1, &e1, a1, b1, w13, 8); + R22(&c2, d2, &e2, a2, b2, w3, 15); + R21(&b1, c1, &d1, e1, a1, w1, 13); + R22(&b2, c2, &d2, e2, a2, w7, 7); + R21(&a1, b1, &c1, d1, e1, w10, 11); + R22(&a2, b2, &c2, d2, e2, w0, 12); + R21(&e1, a1, &b1, c1, d1, w6, 9); + R22(&e2, a2, &b2, c2, d2, w13, 8); + R21(&d1, e1, &a1, b1, c1, w15, 7); + R22(&d2, e2, &a2, b2, c2, w5, 9); + R21(&c1, d1, &e1, a1, b1, w3, 15); + R22(&c2, d2, &e2, a2, b2, w10, 11); + R21(&b1, c1, &d1, e1, a1, w12, 7); + R22(&b2, c2, &d2, e2, a2, w14, 7); + R21(&a1, b1, &c1, d1, e1, w0, 12); + R22(&a2, b2, &c2, d2, e2, w15, 7); + R21(&e1, a1, &b1, c1, d1, w9, 15); + R22(&e2, a2, &b2, c2, d2, w8, 12); + R21(&d1, e1, &a1, b1, c1, w5, 9); + R22(&d2, e2, &a2, b2, c2, w12, 7); + R21(&c1, d1, &e1, a1, b1, w2, 11); + R22(&c2, d2, &e2, a2, b2, w4, 6); + R21(&b1, c1, &d1, e1, a1, w14, 7); + R22(&b2, c2, &d2, e2, a2, w9, 15); + R21(&a1, b1, &c1, d1, e1, w11, 13); + R22(&a2, b2, &c2, d2, e2, w1, 13); + R21(&e1, a1, &b1, c1, d1, w8, 12); + R22(&e2, a2, &b2, c2, d2, w2, 11); + + R31(&d1, e1, &a1, b1, c1, w3, 11); + R32(&d2, e2, &a2, b2, c2, w15, 9); + R31(&c1, d1, &e1, a1, b1, w10, 13); + R32(&c2, d2, &e2, a2, b2, w5, 7); + R31(&b1, c1, &d1, e1, a1, w14, 6); + R32(&b2, c2, &d2, e2, a2, w1, 15); + R31(&a1, b1, &c1, d1, e1, w4, 7); + R32(&a2, b2, &c2, d2, e2, w3, 11); + R31(&e1, a1, &b1, c1, d1, w9, 14); + R32(&e2, a2, &b2, c2, d2, w7, 8); + R31(&d1, e1, &a1, b1, c1, w15, 9); + R32(&d2, e2, &a2, b2, c2, w14, 6); + R31(&c1, d1, &e1, a1, b1, w8, 13); + R32(&c2, d2, &e2, a2, b2, w6, 6); + R31(&b1, c1, &d1, e1, a1, w1, 15); + R32(&b2, c2, &d2, e2, a2, w9, 14); + R31(&a1, b1, &c1, d1, e1, w2, 14); + R32(&a2, b2, &c2, d2, e2, w11, 12); + R31(&e1, a1, &b1, c1, d1, w7, 8); + R32(&e2, a2, &b2, c2, d2, w8, 13); + R31(&d1, e1, &a1, b1, c1, w0, 13); + R32(&d2, e2, &a2, b2, c2, w12, 5); + R31(&c1, d1, &e1, a1, b1, w6, 6); + R32(&c2, d2, &e2, a2, b2, w2, 14); + R31(&b1, c1, &d1, e1, a1, w13, 5); + R32(&b2, c2, &d2, e2, a2, w10, 13); + R31(&a1, b1, &c1, d1, e1, w11, 12); + R32(&a2, b2, &c2, d2, e2, w0, 13); + R31(&e1, a1, &b1, c1, d1, w5, 7); + R32(&e2, a2, &b2, c2, d2, w4, 7); + R31(&d1, e1, &a1, b1, c1, w12, 5); + R32(&d2, e2, &a2, b2, c2, w13, 5); + + R41(&c1, d1, &e1, a1, b1, w1, 11); + R42(&c2, d2, &e2, a2, b2, w8, 15); + R41(&b1, c1, &d1, e1, a1, w9, 12); + R42(&b2, c2, &d2, e2, a2, w6, 5); + R41(&a1, b1, &c1, d1, e1, w11, 14); + R42(&a2, b2, &c2, d2, e2, w4, 8); + R41(&e1, a1, &b1, c1, d1, w10, 15); + R42(&e2, a2, &b2, c2, d2, w1, 11); + R41(&d1, e1, &a1, b1, c1, w0, 14); + R42(&d2, e2, &a2, b2, c2, w3, 14); + R41(&c1, d1, &e1, a1, b1, w8, 15); + R42(&c2, d2, &e2, a2, b2, w11, 14); + R41(&b1, c1, &d1, e1, a1, w12, 9); + R42(&b2, c2, &d2, e2, a2, w15, 6); + R41(&a1, b1, &c1, d1, e1, w4, 8); + R42(&a2, b2, &c2, d2, e2, w0, 14); + R41(&e1, a1, &b1, c1, d1, w13, 9); + R42(&e2, a2, &b2, c2, d2, w5, 6); + R41(&d1, e1, &a1, b1, c1, w3, 14); + R42(&d2, e2, &a2, b2, c2, w12, 9); + R41(&c1, d1, &e1, a1, b1, w7, 5); + R42(&c2, d2, &e2, a2, b2, w2, 12); + R41(&b1, c1, &d1, e1, a1, w15, 6); + R42(&b2, c2, &d2, e2, a2, w13, 9); + R41(&a1, b1, &c1, d1, e1, w14, 8); + R42(&a2, b2, &c2, d2, e2, w9, 12); + R41(&e1, a1, &b1, c1, d1, w5, 6); + R42(&e2, a2, &b2, c2, d2, w7, 5); + R41(&d1, e1, &a1, b1, c1, w6, 5); + R42(&d2, e2, &a2, b2, c2, w10, 15); + R41(&c1, d1, &e1, a1, b1, w2, 12); + R42(&c2, d2, &e2, a2, b2, w14, 8); + + R51(&b1, c1, &d1, e1, a1, w4, 9); + R52(&b2, c2, &d2, e2, a2, w12, 8); + R51(&a1, b1, &c1, d1, e1, w0, 15); + R52(&a2, b2, &c2, d2, e2, w15, 5); + R51(&e1, a1, &b1, c1, d1, w5, 5); + R52(&e2, a2, &b2, c2, d2, w10, 12); + R51(&d1, e1, &a1, b1, c1, w9, 11); + R52(&d2, e2, &a2, b2, c2, w4, 9); + R51(&c1, d1, &e1, a1, b1, w7, 6); + R52(&c2, d2, &e2, a2, b2, w1, 12); + R51(&b1, c1, &d1, e1, a1, w12, 8); + R52(&b2, c2, &d2, e2, a2, w5, 5); + R51(&a1, b1, &c1, d1, e1, w2, 13); + R52(&a2, b2, &c2, d2, e2, w8, 14); + R51(&e1, a1, &b1, c1, d1, w10, 12); + R52(&e2, a2, &b2, c2, d2, w7, 6); + R51(&d1, e1, &a1, b1, c1, w14, 5); + R52(&d2, e2, &a2, b2, c2, w6, 8); + R51(&c1, d1, &e1, a1, b1, w1, 12); + R52(&c2, d2, &e2, a2, b2, w2, 13); + R51(&b1, c1, &d1, e1, a1, w3, 13); + R52(&b2, c2, &d2, e2, a2, w13, 6); + R51(&a1, b1, &c1, d1, e1, w8, 14); + R52(&a2, b2, &c2, d2, e2, w14, 5); + R51(&e1, a1, &b1, c1, d1, w11, 11); + R52(&e2, a2, &b2, c2, d2, w0, 15); + R51(&d1, e1, &a1, b1, c1, w6, 8); + R52(&d2, e2, &a2, b2, c2, w3, 13); + R51(&c1, d1, &e1, a1, b1, w15, 5); + R52(&c2, d2, &e2, a2, b2, w9, 11); + R51(&b1, c1, &d1, e1, a1, w13, 6); + R52(&b2, c2, &d2, e2, a2, w11, 11); + + t = s[0]; + s[0] = s[1] + c1 + d2; + s[1] = s[2] + d1 + e2; + s[2] = s[3] + e1 + a2; + s[3] = s[4] + a1 + b2; + s[4] = t + b1 + c2; +} + +static void add(struct ripemd160_ctx *ctx, const void *p, size_t len) +{ + const unsigned char *data = p; + size_t bufsize = ctx->bytes % 64; + + if (bufsize + len >= 64) { + /* Fill the buffer, and process it. */ + memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize); + ctx->bytes += 64 - bufsize; + data += 64 - bufsize; + len -= 64 - bufsize; + Transform(ctx->s, ctx->buf.u32); + bufsize = 0; + } + + while (len >= 64) { + /* Process full chunks directly from the source. */ + if (alignment_ok(data, sizeof(uint32_t))) + Transform(ctx->s, (const uint32_t *)data); + else { + memcpy(ctx->buf.u8, data, sizeof(ctx->buf)); + Transform(ctx->s, ctx->buf.u32); + } + ctx->bytes += 64; + data += 64; + len -= 64; + } + + if (len) { + /* Fill the buffer with what remains. */ + memcpy(ctx->buf.u8 + bufsize, data, len); + ctx->bytes += len; + } +} + +void ripemd160_init(struct ripemd160_ctx *ctx) +{ + struct ripemd160_ctx init = RIPEMD160_INIT; + *ctx = init; +} + +void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size) +{ + check_ripemd160(ctx); + add(ctx, p, size); +} + +void ripemd160_done(struct ripemd160_ctx *ctx, struct ripemd160 *res) +{ + static const unsigned char pad[64] = {0x80}; + uint64_t sizedesc; + size_t i; + + sizedesc = cpu_to_le64(ctx->bytes << 3); + /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */ + add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64)); + /* Add number of bits of data (big endian) */ + add(ctx, &sizedesc, 8); + for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++) + res->u.u32[i] = cpu_to_le32(ctx->s[i]); + invalidate_ripemd160(ctx); +} +#endif + +void ripemd160(struct ripemd160 *ripemd, const void *p, size_t size) +{ + struct ripemd160_ctx ctx; + + ripemd160_init(&ctx); + ripemd160_update(&ctx, p, size); + ripemd160_done(&ctx, ripemd); + memset(&ctx, 0, sizeof(ctx)); +} + +void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v) +{ + ripemd160_update(ctx, &v, sizeof(v)); +} + +void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v) +{ + ripemd160_update(ctx, &v, sizeof(v)); +} + +void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v) +{ + ripemd160_update(ctx, &v, sizeof(v)); +} + +void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v) +{ + ripemd160_update(ctx, &v, sizeof(v)); +} + +/* Add as little-endian */ +void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v) +{ + leint16_t lev = cpu_to_le16(v); + ripemd160_update(ctx, &lev, sizeof(lev)); +} + +void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v) +{ + leint32_t lev = cpu_to_le32(v); + ripemd160_update(ctx, &lev, sizeof(lev)); +} + +void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v) +{ + leint64_t lev = cpu_to_le64(v); + ripemd160_update(ctx, &lev, sizeof(lev)); +} + +/* Add as big-endian */ +void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v) +{ + beint16_t bev = cpu_to_be16(v); + ripemd160_update(ctx, &bev, sizeof(bev)); +} + +void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v) +{ + beint32_t bev = cpu_to_be32(v); + ripemd160_update(ctx, &bev, sizeof(bev)); +} + +void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v) +{ + beint64_t bev = cpu_to_be64(v); + ripemd160_update(ctx, &bev, sizeof(bev)); +} + + diff --git a/ripemd160.h b/ripemd160.h @@ -0,0 +1,148 @@ + +#ifndef CCAN_CRYPTO_RIPEMD160_H +#define CCAN_CRYPTO_RIPEMD160_H +/* BSD-MIT - see LICENSE file for details */ +#include <stdint.h> +#include <stdlib.h> + +/* Uncomment this to use openssl's RIPEMD160 routines (and link with -lcrypto) */ +/*#define CCAN_CRYPTO_RIPEMD160_USE_OPENSSL 1*/ + +#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL +#include <openssl/ripemd.h> +#endif + +/** + * struct ripemd160 - structure representing a completed RIPEMD160. + * @u.u8: an unsigned char array. + * @u.u32: a 32-bit integer array. + * + * Other fields may be added to the union in future. + */ +struct ripemd160 { + union { + /* Array of chars */ + unsigned char u8[20]; + /* Array of uint32_t */ + uint32_t u32[5]; + } u; +}; + +/** + * ripemd160 - return ripemd160 of an object. + * @ripemd160: the ripemd160 to fill in + * @p: pointer to memory, + * @size: the number of bytes pointed to by @p + * + * The bytes pointed to by @p is RIPEMD160 hashed into @ripemd160. This is + * equivalent to ripemd160_init(), ripemd160_update() then ripemd160_done(). + */ +void ripemd160(struct ripemd160 *ripemd, const void *p, size_t size); + +/** + * struct ripemd160_ctx - structure to store running context for ripemd160 + */ +struct ripemd160_ctx { +#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL + RIPEMD160_CTX c; +#else + uint32_t s[5]; + uint64_t bytes; + union { + uint32_t u32[16]; + unsigned char u8[64]; + } buf; +#endif +}; + +/** + * ripemd160_init - initialize an RIPEMD160 context. + * @ctx: the ripemd160_ctx to initialize + * + * This must be called before ripemd160_update or ripemd160_done, or + * alternately you can assign RIPEMD160_INIT. + * + * If it was already initialized, this forgets anything which was + * hashed before. + * + * Example: + * static void hash_all(const char **arr, struct ripemd160 *hash) + * { + * size_t i; + * struct ripemd160_ctx ctx; + * + * ripemd160_init(&ctx); + * for (i = 0; arr[i]; i++) + * ripemd160_update(&ctx, arr[i], strlen(arr[i])); + * ripemd160_done(&ctx, hash); + * } + */ +void ripemd160_init(struct ripemd160_ctx *ctx); + +/** + * RIPEMD160_INIT - initializer for an RIPEMD160 context. + * + * This can be used to staticly initialize an RIPEMD160 context (instead + * of ripemd160_init()). + * + * Example: + * static void hash_all(const char **arr, struct ripemd160 *hash) + * { + * size_t i; + * struct ripemd160_ctx ctx = RIPEMD160_INIT; + * + * for (i = 0; arr[i]; i++) + * ripemd160_update(&ctx, arr[i], strlen(arr[i])); + * ripemd160_done(&ctx, hash); + * } + */ +#ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL +#define RIPEMD160_INIT \ + { { 0x67452301ul, 0xEFCDAB89ul, 0x98BADCFEul, 0x10325476ul, \ + 0xC3D2E1F0ul, \ + 0x0, 0x0, \ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \ + 0 } } +#else +#define RIPEMD160_INIT \ + { { 0x67452301ul, 0xEFCDAB89ul, 0x98BADCFEul, 0x10325476ul, \ + 0xC3D2E1F0ul }, 0, {{ 0 }} } +#endif + +/** + * ripemd160_update - include some memory in the hash. + * @ctx: the ripemd160_ctx to use + * @p: pointer to memory, + * @size: the number of bytes pointed to by @p + * + * You can call this multiple times to hash more data, before calling + * ripemd160_done(). + */ +void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size); + +/** + * ripemd160_done - finish RIPEMD160 and return the hash + * @ctx: the ripemd160_ctx to complete + * @res: the hash to return. + * + * Note that @ctx is *destroyed* by this, and must be reinitialized. + * To avoid that, pass a copy instead. + */ +void ripemd160_done(struct ripemd160_ctx *ripemd160, struct ripemd160 *res); + +/* Add various types to an RIPEMD160 hash */ +void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v); +void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v); +void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v); +void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v); + +/* Add as little-endian */ +void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v); +void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v); +void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v); + +/* Add as big-endian */ +void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v); +void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v); +void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v); +#endif /* CCAN_CRYPTO_RIPEMD160_H */ diff --git a/sha256.c b/sha256.c @@ -0,0 +1,321 @@ + +/* MIT (BSD) license - see LICENSE file for details */ +/* SHA256 core code translated from the Bitcoin project's C++: + * + * src/crypto/sha256.cpp commit 417532c8acb93c36c2b6fd052b7c11b6a2906aa2 + * Copyright (c) 2014 The Bitcoin Core developers + * Distributed under the MIT software license, see the accompanying + * file COPYING or http://www.opensource.org/licenses/mit-license.php. + */ +#include "sha256.h" +#include "endian.h" +#include "compiler.h" +#include <stdbool.h> +#include <assert.h> +#include <string.h> + +static void invalidate_sha256(struct sha256_ctx *ctx) +{ +#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL + ctx->c.md_len = 0; +#else + ctx->bytes = (size_t)-1; +#endif +} + +static void check_sha256(struct sha256_ctx *ctx) +{ +#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL + assert(ctx->c.md_len != 0); +#else + assert(ctx->bytes != (size_t)-1); +#endif +} + +#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL +void sha256_init(struct sha256_ctx *ctx) +{ + SHA256_Init(&ctx->c); +} + +void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size) +{ + check_sha256(ctx); + SHA256_Update(&ctx->c, p, size); +} + +void sha256_done(struct sha256_ctx *ctx, struct sha256 *res) +{ + SHA256_Final(res->u.u8, &ctx->c); + invalidate_sha256(ctx); +} +#else +static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z) +{ + return z ^ (x & (y ^ z)); +} +static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z) +{ + return (x & y) | (z & (x | y)); +} +static uint32_t Sigma0(uint32_t x) +{ + return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10); +} +static uint32_t Sigma1(uint32_t x) +{ + return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7); +} +static uint32_t sigma0(uint32_t x) +{ + return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3); +} +static uint32_t sigma1(uint32_t x) +{ + return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10); +} + +/** One round of SHA-256. */ +static void Round(uint32_t a, uint32_t b, uint32_t c, uint32_t *d, uint32_t e, uint32_t f, uint32_t g, uint32_t *h, uint32_t k, uint32_t w) +{ + uint32_t t1 = *h + Sigma1(e) + Ch(e, f, g) + k + w; + uint32_t t2 = Sigma0(a) + Maj(a, b, c); + *d += t1; + *h = t1 + t2; +} + +/** Perform one SHA-256 transformation, processing a 64-byte chunk. */ +static void Transform(uint32_t *s, const uint32_t *chunk) +{ + uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7]; + uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15; + + Round(a, b, c, &d, e, f, g, &h, 0x428a2f98, w0 = be32_to_cpu(chunk[0])); + Round(h, a, b, &c, d, e, f, &g, 0x71374491, w1 = be32_to_cpu(chunk[1])); + Round(g, h, a, &b, c, d, e, &f, 0xb5c0fbcf, w2 = be32_to_cpu(chunk[2])); + Round(f, g, h, &a, b, c, d, &e, 0xe9b5dba5, w3 = be32_to_cpu(chunk[3])); + Round(e, f, g, &h, a, b, c, &d, 0x3956c25b, w4 = be32_to_cpu(chunk[4])); + Round(d, e, f, &g, h, a, b, &c, 0x59f111f1, w5 = be32_to_cpu(chunk[5])); + Round(c, d, e, &f, g, h, a, &b, 0x923f82a4, w6 = be32_to_cpu(chunk[6])); + Round(b, c, d, &e, f, g, h, &a, 0xab1c5ed5, w7 = be32_to_cpu(chunk[7])); + Round(a, b, c, &d, e, f, g, &h, 0xd807aa98, w8 = be32_to_cpu(chunk[8])); + Round(h, a, b, &c, d, e, f, &g, 0x12835b01, w9 = be32_to_cpu(chunk[9])); + Round(g, h, a, &b, c, d, e, &f, 0x243185be, w10 = be32_to_cpu(chunk[10])); + Round(f, g, h, &a, b, c, d, &e, 0x550c7dc3, w11 = be32_to_cpu(chunk[11])); + Round(e, f, g, &h, a, b, c, &d, 0x72be5d74, w12 = be32_to_cpu(chunk[12])); + Round(d, e, f, &g, h, a, b, &c, 0x80deb1fe, w13 = be32_to_cpu(chunk[13])); + Round(c, d, e, &f, g, h, a, &b, 0x9bdc06a7, w14 = be32_to_cpu(chunk[14])); + Round(b, c, d, &e, f, g, h, &a, 0xc19bf174, w15 = be32_to_cpu(chunk[15])); + + Round(a, b, c, &d, e, f, g, &h, 0xe49b69c1, w0 += sigma1(w14) + w9 + sigma0(w1)); + Round(h, a, b, &c, d, e, f, &g, 0xefbe4786, w1 += sigma1(w15) + w10 + sigma0(w2)); + Round(g, h, a, &b, c, d, e, &f, 0x0fc19dc6, w2 += sigma1(w0) + w11 + sigma0(w3)); + Round(f, g, h, &a, b, c, d, &e, 0x240ca1cc, w3 += sigma1(w1) + w12 + sigma0(w4)); + Round(e, f, g, &h, a, b, c, &d, 0x2de92c6f, w4 += sigma1(w2) + w13 + sigma0(w5)); + Round(d, e, f, &g, h, a, b, &c, 0x4a7484aa, w5 += sigma1(w3) + w14 + sigma0(w6)); + Round(c, d, e, &f, g, h, a, &b, 0x5cb0a9dc, w6 += sigma1(w4) + w15 + sigma0(w7)); + Round(b, c, d, &e, f, g, h, &a, 0x76f988da, w7 += sigma1(w5) + w0 + sigma0(w8)); + Round(a, b, c, &d, e, f, g, &h, 0x983e5152, w8 += sigma1(w6) + w1 + sigma0(w9)); + Round(h, a, b, &c, d, e, f, &g, 0xa831c66d, w9 += sigma1(w7) + w2 + sigma0(w10)); + Round(g, h, a, &b, c, d, e, &f, 0xb00327c8, w10 += sigma1(w8) + w3 + sigma0(w11)); + Round(f, g, h, &a, b, c, d, &e, 0xbf597fc7, w11 += sigma1(w9) + w4 + sigma0(w12)); + Round(e, f, g, &h, a, b, c, &d, 0xc6e00bf3, w12 += sigma1(w10) + w5 + sigma0(w13)); + Round(d, e, f, &g, h, a, b, &c, 0xd5a79147, w13 += sigma1(w11) + w6 + sigma0(w14)); + Round(c, d, e, &f, g, h, a, &b, 0x06ca6351, w14 += sigma1(w12) + w7 + sigma0(w15)); + Round(b, c, d, &e, f, g, h, &a, 0x14292967, w15 += sigma1(w13) + w8 + sigma0(w0)); + + Round(a, b, c, &d, e, f, g, &h, 0x27b70a85, w0 += sigma1(w14) + w9 + sigma0(w1)); + Round(h, a, b, &c, d, e, f, &g, 0x2e1b2138, w1 += sigma1(w15) + w10 + sigma0(w2)); + Round(g, h, a, &b, c, d, e, &f, 0x4d2c6dfc, w2 += sigma1(w0) + w11 + sigma0(w3)); + Round(f, g, h, &a, b, c, d, &e, 0x53380d13, w3 += sigma1(w1) + w12 + sigma0(w4)); + Round(e, f, g, &h, a, b, c, &d, 0x650a7354, w4 += sigma1(w2) + w13 + sigma0(w5)); + Round(d, e, f, &g, h, a, b, &c, 0x766a0abb, w5 += sigma1(w3) + w14 + sigma0(w6)); + Round(c, d, e, &f, g, h, a, &b, 0x81c2c92e, w6 += sigma1(w4) + w15 + sigma0(w7)); + Round(b, c, d, &e, f, g, h, &a, 0x92722c85, w7 += sigma1(w5) + w0 + sigma0(w8)); + Round(a, b, c, &d, e, f, g, &h, 0xa2bfe8a1, w8 += sigma1(w6) + w1 + sigma0(w9)); + Round(h, a, b, &c, d, e, f, &g, 0xa81a664b, w9 += sigma1(w7) + w2 + sigma0(w10)); + Round(g, h, a, &b, c, d, e, &f, 0xc24b8b70, w10 += sigma1(w8) + w3 + sigma0(w11)); + Round(f, g, h, &a, b, c, d, &e, 0xc76c51a3, w11 += sigma1(w9) + w4 + sigma0(w12)); + Round(e, f, g, &h, a, b, c, &d, 0xd192e819, w12 += sigma1(w10) + w5 + sigma0(w13)); + Round(d, e, f, &g, h, a, b, &c, 0xd6990624, w13 += sigma1(w11) + w6 + sigma0(w14)); + Round(c, d, e, &f, g, h, a, &b, 0xf40e3585, w14 += sigma1(w12) + w7 + sigma0(w15)); + Round(b, c, d, &e, f, g, h, &a, 0x106aa070, w15 += sigma1(w13) + w8 + sigma0(w0)); + + Round(a, b, c, &d, e, f, g, &h, 0x19a4c116, w0 += sigma1(w14) + w9 + sigma0(w1)); + Round(h, a, b, &c, d, e, f, &g, 0x1e376c08, w1 += sigma1(w15) + w10 + sigma0(w2)); + Round(g, h, a, &b, c, d, e, &f, 0x2748774c, w2 += sigma1(w0) + w11 + sigma0(w3)); + Round(f, g, h, &a, b, c, d, &e, 0x34b0bcb5, w3 += sigma1(w1) + w12 + sigma0(w4)); + Round(e, f, g, &h, a, b, c, &d, 0x391c0cb3, w4 += sigma1(w2) + w13 + sigma0(w5)); + Round(d, e, f, &g, h, a, b, &c, 0x4ed8aa4a, w5 += sigma1(w3) + w14 + sigma0(w6)); + Round(c, d, e, &f, g, h, a, &b, 0x5b9cca4f, w6 += sigma1(w4) + w15 + sigma0(w7)); + Round(b, c, d, &e, f, g, h, &a, 0x682e6ff3, w7 += sigma1(w5) + w0 + sigma0(w8)); + Round(a, b, c, &d, e, f, g, &h, 0x748f82ee, w8 += sigma1(w6) + w1 + sigma0(w9)); + Round(h, a, b, &c, d, e, f, &g, 0x78a5636f, w9 += sigma1(w7) + w2 + sigma0(w10)); + Round(g, h, a, &b, c, d, e, &f, 0x84c87814, w10 += sigma1(w8) + w3 + sigma0(w11)); + Round(f, g, h, &a, b, c, d, &e, 0x8cc70208, w11 += sigma1(w9) + w4 + sigma0(w12)); + Round(e, f, g, &h, a, b, c, &d, 0x90befffa, w12 += sigma1(w10) + w5 + sigma0(w13)); + Round(d, e, f, &g, h, a, b, &c, 0xa4506ceb, w13 += sigma1(w11) + w6 + sigma0(w14)); + Round(c, d, e, &f, g, h, a, &b, 0xbef9a3f7, w14 + sigma1(w12) + w7 + sigma0(w15)); + Round(b, c, d, &e, f, g, h, &a, 0xc67178f2, w15 + sigma1(w13) + w8 + sigma0(w0)); + + s[0] += a; + s[1] += b; + s[2] += c; + s[3] += d; + s[4] += e; + s[5] += f; + s[6] += g; + s[7] += h; +} + + +static void add(struct sha256_ctx *ctx, const void *p, size_t len) +{ + const unsigned char *data = p; + size_t bufsize = ctx->bytes % 64; + + if (bufsize + len >= 64) { + /* Fill the buffer, and process it. */ + memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize); + ctx->bytes += 64 - bufsize; + data += 64 - bufsize; + len -= 64 - bufsize; + Transform(ctx->s, ctx->buf.u32); + bufsize = 0; + } + + while (len >= 64) { + /* Process full chunks directly from the source. */ + if (alignment_ok(data, sizeof(uint32_t))) + Transform(ctx->s, (const uint32_t *)data); + else { + memcpy(ctx->buf.u8, data, sizeof(ctx->buf)); + Transform(ctx->s, ctx->buf.u32); + } + ctx->bytes += 64; + data += 64; + len -= 64; + } + + if (len) { + /* Fill the buffer with what remains. */ + memcpy(ctx->buf.u8 + bufsize, data, len); + ctx->bytes += len; + } +} + +void sha256_init(struct sha256_ctx *ctx) +{ + struct sha256_ctx init = SHA256_INIT; + *ctx = init; +} + +void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size) +{ + check_sha256(ctx); + add(ctx, p, size); +} + +void sha256_done(struct sha256_ctx *ctx, struct sha256 *res) +{ + static const unsigned char pad[64] = {0x80}; + uint64_t sizedesc; + size_t i; + + sizedesc = cpu_to_be64((uint64_t)ctx->bytes << 3); + /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */ + add(ctx, pad, 1 + ((128 - 8 - (ctx->bytes % 64) - 1) % 64)); + /* Add number of bits of data (big endian) */ + add(ctx, &sizedesc, 8); + for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++) + res->u.u32[i] = cpu_to_be32(ctx->s[i]); + invalidate_sha256(ctx); +} +#endif + +void sha256(struct sha256 *sha, const void *p, size_t size) +{ + struct sha256_ctx ctx; + + sha256_init(&ctx); + sha256_update(&ctx, p, size); + sha256_done(&ctx, sha); +} + +void sha256_u8(struct sha256_ctx *ctx, uint8_t v) +{ + sha256_update(ctx, &v, sizeof(v)); +} + +void sha256_u16(struct sha256_ctx *ctx, uint16_t v) +{ + sha256_update(ctx, &v, sizeof(v)); +} + +void sha256_u32(struct sha256_ctx *ctx, uint32_t v) +{ + sha256_update(ctx, &v, sizeof(v)); +} + +void sha256_u64(struct sha256_ctx *ctx, uint64_t v) +{ + sha256_update(ctx, &v, sizeof(v)); +} + +/* Add as little-endian */ +void sha256_le16(struct sha256_ctx *ctx, uint16_t v) +{ + leint16_t lev = cpu_to_le16(v); + sha256_update(ctx, &lev, sizeof(lev)); +} + +void sha256_le32(struct sha256_ctx *ctx, uint32_t v) +{ + leint32_t lev = cpu_to_le32(v); + sha256_update(ctx, &lev, sizeof(lev)); +} + +void sha256_le64(struct sha256_ctx *ctx, uint64_t v) +{ + leint64_t lev = cpu_to_le64(v); + sha256_update(ctx, &lev, sizeof(lev)); +} + +/* Add as big-endian */ +void sha256_be16(struct sha256_ctx *ctx, uint16_t v) +{ + beint16_t bev = cpu_to_be16(v); + sha256_update(ctx, &bev, sizeof(bev)); +} + +void sha256_be32(struct sha256_ctx *ctx, uint32_t v) +{ + beint32_t bev = cpu_to_be32(v); + sha256_update(ctx, &bev, sizeof(bev)); +} + +void sha256_be64(struct sha256_ctx *ctx, uint64_t v) +{ + beint64_t bev = cpu_to_be64(v); + sha256_update(ctx, &bev, sizeof(bev)); +} + + +int sha256d(const unsigned char *bytes, size_t bytes_len, + unsigned char *bytes_out, size_t len) +{ + struct sha256 sha_1, sha_2; + bool aligned = alignment_ok(bytes_out, sizeof(sha_1.u.u32)); + + if (!bytes || !bytes_out || len != SHA256_LEN) + return 0; + + sha256(&sha_1, bytes, bytes_len); + sha256(aligned ? (struct sha256 *)bytes_out : &sha_2, &sha_1, sizeof(sha_1)); + if (!aligned) { + memcpy(bytes_out, &sha_2, sizeof(sha_2)); + wally_clear(&sha_2, sizeof(sha_2)); + } + wally_clear(&sha_1, sizeof(sha_1)); + return 1; +} diff --git a/sha256.h b/sha256.h @@ -0,0 +1,158 @@ + +#ifndef CCAN_CRYPTO_SHA256_H +#define CCAN_CRYPTO_SHA256_H + + +/** Output length for `wally_sha256` */ +#define SHA256_LEN 32 + + +/* BSD-MIT - see LICENSE file for details */ +/* #include "config.h" */ +#include <stdint.h> +#include <stdlib.h> + +/* Uncomment this to use openssl's SHA256 routines (and link with -lcrypto) */ +/*#define CCAN_CRYPTO_SHA256_USE_OPENSSL 1*/ + +#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL +#include <openssl/sha.h> +#endif + +/** + * struct sha256 - structure representing a completed SHA256. + * @u.u8: an unsigned char array. + * @u.u32: a 32-bit integer array. + * + * Other fields may be added to the union in future. + */ +struct sha256 { + union { + uint32_t u32[8]; + unsigned char u8[32]; + } u; +}; + +/** + * sha256 - return sha256 of an object. + * @sha256: the sha256 to fill in + * @p: pointer to memory, + * @size: the number of bytes pointed to by @p + * + * The bytes pointed to by @p is SHA256 hashed into @sha256. This is + * equivalent to sha256_init(), sha256_update() then sha256_done(). + */ +void sha256(struct sha256 *sha, const void *p, size_t size); + +/** + * struct sha256_ctx - structure to store running context for sha256 + */ +struct sha256_ctx { +#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL + SHA256_CTX c; +#else + uint32_t s[8]; + union { + uint32_t u32[16]; + unsigned char u8[64]; + } buf; + size_t bytes; +#endif +}; + +/** + * sha256_init - initialize an SHA256 context. + * @ctx: the sha256_ctx to initialize + * + * This must be called before sha256_update or sha256_done, or + * alternately you can assign SHA256_INIT. + * + * If it was already initialized, this forgets anything which was + * hashed before. + * + * Example: + * static void hash_all(const char **arr, struct sha256 *hash) + * { + * size_t i; + * struct sha256_ctx ctx; + * + * sha256_init(&ctx); + * for (i = 0; arr[i]; i++) + * sha256_update(&ctx, arr[i], strlen(arr[i])); + * sha256_done(&ctx, hash); + * } + */ +void sha256_init(struct sha256_ctx *ctx); + +/** + * SHA256_INIT - initializer for an SHA256 context. + * + * This can be used to statically initialize an SHA256 context (instead + * of sha256_init()). + * + * Example: + * static void hash_all(const char **arr, struct sha256 *hash) + * { + * size_t i; + * struct sha256_ctx ctx = SHA256_INIT; + * + * for (i = 0; arr[i]; i++) + * sha256_update(&ctx, arr[i], strlen(arr[i])); + * sha256_done(&ctx, hash); + * } + */ +#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL +#define SHA256_INIT \ + { { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \ + 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \ + 0x0, 0x0, \ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \ + 0x0, 0x20 } } +#else +#define SHA256_INIT \ + { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \ + 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \ + { { 0 } }, 0 } +#endif + +/** + * sha256_update - include some memory in the hash. + * @ctx: the sha256_ctx to use + * @p: pointer to memory, + * @size: the number of bytes pointed to by @p + * + * You can call this multiple times to hash more data, before calling + * sha256_done(). + */ +void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size); + +/** + * sha256_done - finish SHA256 and return the hash + * @ctx: the sha256_ctx to complete + * @res: the hash to return. + * + * Note that @ctx is *destroyed* by this, and must be reinitialized. + * To avoid that, pass a copy instead. + */ +void sha256_done(struct sha256_ctx *sha256, struct sha256 *res); + +/* Add various types to an SHA256 hash */ +void sha256_u8(struct sha256_ctx *ctx, uint8_t v); +void sha256_u16(struct sha256_ctx *ctx, uint16_t v); +void sha256_u32(struct sha256_ctx *ctx, uint32_t v); +void sha256_u64(struct sha256_ctx *ctx, uint64_t v); + +/* Add as little-endian */ +void sha256_le16(struct sha256_ctx *ctx, uint16_t v); +void sha256_le32(struct sha256_ctx *ctx, uint32_t v); +void sha256_le64(struct sha256_ctx *ctx, uint64_t v); + +/* Add as big-endian */ +void sha256_be16(struct sha256_ctx *ctx, uint16_t v); +void sha256_be32(struct sha256_ctx *ctx, uint32_t v); +void sha256_be64(struct sha256_ctx *ctx, uint64_t v); + +int sha256d(const unsigned char *bytes, size_t bytes_len, + unsigned char *bytes_out, size_t len); + +#endif /* CCAN_CRYPTO_SHA256_H */