clightning-dumpkeys

dump clightning output descriptors
git clone git://jb55.com/clightning-dumpkeys
Log | Files | Refs | README | LICENSE

sha256.h (4192B)


      1 
      2 #ifndef CCAN_CRYPTO_SHA256_H
      3 #define CCAN_CRYPTO_SHA256_H
      4 
      5 
      6 /** Output length for `wally_sha256` */
      7 #define SHA256_LEN 32
      8 
      9 
     10 /* BSD-MIT - see LICENSE file for details */
     11 /* #include "config.h" */
     12 #include <stdint.h>
     13 #include <stdlib.h>
     14 
     15 /* Uncomment this to use openssl's SHA256 routines (and link with -lcrypto) */
     16 /*#define CCAN_CRYPTO_SHA256_USE_OPENSSL 1*/
     17 
     18 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
     19 #include <openssl/sha.h>
     20 #endif
     21 
     22 /**
     23  * struct sha256 - structure representing a completed SHA256.
     24  * @u.u8: an unsigned char array.
     25  * @u.u32: a 32-bit integer array.
     26  *
     27  * Other fields may be added to the union in future.
     28  */
     29 struct sha256 {
     30 	union {
     31 		uint32_t u32[8];
     32 		unsigned char u8[32];
     33 	} u;
     34 };
     35 
     36 /**
     37  * sha256 - return sha256 of an object.
     38  * @sha256: the sha256 to fill in
     39  * @p: pointer to memory,
     40  * @size: the number of bytes pointed to by @p
     41  *
     42  * The bytes pointed to by @p is SHA256 hashed into @sha256.  This is
     43  * equivalent to sha256_init(), sha256_update() then sha256_done().
     44  */
     45 void sha256(struct sha256 *sha, const void *p, size_t size);
     46 
     47 /**
     48  * struct sha256_ctx - structure to store running context for sha256
     49  */
     50 struct sha256_ctx {
     51 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
     52 	SHA256_CTX c;
     53 #else
     54 	uint32_t s[8];
     55 	union {
     56 		uint32_t u32[16];
     57 		unsigned char u8[64];
     58 	} buf;
     59 	size_t bytes;
     60 #endif
     61 };
     62 
     63 /**
     64  * sha256_init - initialize an SHA256 context.
     65  * @ctx: the sha256_ctx to initialize
     66  *
     67  * This must be called before sha256_update or sha256_done, or
     68  * alternately you can assign SHA256_INIT.
     69  *
     70  * If it was already initialized, this forgets anything which was
     71  * hashed before.
     72  *
     73  * Example:
     74  * static void hash_all(const char **arr, struct sha256 *hash)
     75  * {
     76  *	size_t i;
     77  *	struct sha256_ctx ctx;
     78  *
     79  *	sha256_init(&ctx);
     80  *	for (i = 0; arr[i]; i++)
     81  *		sha256_update(&ctx, arr[i], strlen(arr[i]));
     82  *	sha256_done(&ctx, hash);
     83  * }
     84  */
     85 void sha256_init(struct sha256_ctx *ctx);
     86 
     87 /**
     88  * SHA256_INIT - initializer for an SHA256 context.
     89  *
     90  * This can be used to statically initialize an SHA256 context (instead
     91  * of sha256_init()).
     92  *
     93  * Example:
     94  * static void hash_all(const char **arr, struct sha256 *hash)
     95  * {
     96  *	size_t i;
     97  *	struct sha256_ctx ctx = SHA256_INIT;
     98  *
     99  *	for (i = 0; arr[i]; i++)
    100  *		sha256_update(&ctx, arr[i], strlen(arr[i]));
    101  *	sha256_done(&ctx, hash);
    102  * }
    103  */
    104 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
    105 #define SHA256_INIT							\
    106 	{ { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,	\
    107 	      0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
    108 		0x0, 0x0,						\
    109 		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	\
    110 			0x0, 0x20 } }
    111 #else
    112 #define SHA256_INIT							\
    113 	{ { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,	\
    114 	    0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul },	\
    115 	  { { 0 } }, 0 }
    116 #endif
    117 
    118 /**
    119  * sha256_update - include some memory in the hash.
    120  * @ctx: the sha256_ctx to use
    121  * @p: pointer to memory,
    122  * @size: the number of bytes pointed to by @p
    123  *
    124  * You can call this multiple times to hash more data, before calling
    125  * sha256_done().
    126  */
    127 void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size);
    128 
    129 /**
    130  * sha256_done - finish SHA256 and return the hash
    131  * @ctx: the sha256_ctx to complete
    132  * @res: the hash to return.
    133  *
    134  * Note that @ctx is *destroyed* by this, and must be reinitialized.
    135  * To avoid that, pass a copy instead.
    136  */
    137 void sha256_done(struct sha256_ctx *sha256, struct sha256 *res);
    138 
    139 /* Add various types to an SHA256 hash */
    140 void sha256_u8(struct sha256_ctx *ctx, uint8_t v);
    141 void sha256_u16(struct sha256_ctx *ctx, uint16_t v);
    142 void sha256_u32(struct sha256_ctx *ctx, uint32_t v);
    143 void sha256_u64(struct sha256_ctx *ctx, uint64_t v);
    144 
    145 /* Add as little-endian */
    146 void sha256_le16(struct sha256_ctx *ctx, uint16_t v);
    147 void sha256_le32(struct sha256_ctx *ctx, uint32_t v);
    148 void sha256_le64(struct sha256_ctx *ctx, uint64_t v);
    149 
    150 /* Add as big-endian */
    151 void sha256_be16(struct sha256_ctx *ctx, uint16_t v);
    152 void sha256_be32(struct sha256_ctx *ctx, uint32_t v);
    153 void sha256_be64(struct sha256_ctx *ctx, uint64_t v);
    154 
    155 int sha256d(const unsigned char *bytes, size_t bytes_len,
    156 	    unsigned char *bytes_out, size_t len);
    157 
    158 #endif /* CCAN_CRYPTO_SHA256_H */