lnsocket

A minimal C library for connecting to the lightning network
git clone git://jb55.com/lnsocket
Log | Files | Refs | Submodules | README | LICENSE

sha512.h (3580B)


      1 
      2 #ifndef CCAN_CRYPTO_SHA512_H
      3 #define CCAN_CRYPTO_SHA512_H
      4 /* BSD-MIT - see LICENSE file for details */
      5 #include <stdint.h>
      6 #include <stdlib.h>
      7 
      8 /** Output length for `wally_sha512` */
      9 #define SHA512_LEN 64
     10 
     11 /* Uncomment this to use openssl's SHA512 routines (and link with -lcrypto) */
     12 /*#define CCAN_CRYPTO_SHA512_USE_OPENSSL 1*/
     13 
     14 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
     15 #include <openssl/sha.h>
     16 #endif
     17 
     18 /**
     19  * struct sha512 - structure representing a completed SHA512.
     20  * @u.u8: an unsigned char array.
     21  * @u.u64: a 64-bit integer array.
     22  *
     23  * Other fields may be added to the union in future.
     24  */
     25 struct sha512 {
     26 	union {
     27 		uint64_t u64[8];
     28 		unsigned char u8[64];
     29 	} u;
     30 };
     31 
     32 /**
     33  * sha512 - return sha512 of an object.
     34  * @sha512: the sha512 to fill in
     35  * @p: pointer to memory,
     36  * @size: the number of bytes pointed to by @p
     37  *
     38  * The bytes pointed to by @p is SHA512 hashed into @sha512.  This is
     39  * equivalent to sha512_init(), sha512_update() then sha512_done().
     40  */
     41 void sha512(struct sha512 *sha, const void *p, size_t size);
     42 
     43 /**
     44  * struct sha512_ctx - structure to store running context for sha512
     45  */
     46 struct sha512_ctx {
     47 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
     48 	SHA512_CTX c;
     49 #else
     50 	uint64_t s[8];
     51 	union {
     52 		uint64_t u64[16];
     53 		unsigned char u8[128];
     54 	} buf;
     55 	size_t bytes;
     56 #endif
     57 };
     58 
     59 /**
     60  * sha512_init - initialize an SHA512 context.
     61  * @ctx: the sha512_ctx to initialize
     62  *
     63  * This must be called before sha512_update or sha512_done, or
     64  * alternately you can assign SHA512_INIT.
     65  *
     66  * If it was already initialized, this forgets anything which was
     67  * hashed before.
     68  *
     69  * Example:
     70  * static void hash_all(const char **arr, struct sha512 *hash)
     71  * {
     72  *	size_t i;
     73  *	struct sha512_ctx ctx;
     74  *
     75  *	sha512_init(&ctx);
     76  *	for (i = 0; arr[i]; i++)
     77  *		sha512_update(&ctx, arr[i], strlen(arr[i]));
     78  *	sha512_done(&ctx, hash);
     79  * }
     80  */
     81 void sha512_init(struct sha512_ctx *ctx);
     82 
     83 /**
     84  * SHA512_INIT - initializer for an SHA512 context.
     85  *
     86  * This can be used to statically initialize an SHA512 context (instead
     87  * of sha512_init()).
     88  *
     89  * Example:
     90  * static void hash_all(const char **arr, struct sha512 *hash)
     91  * {
     92  *	size_t i;
     93  *	struct sha512_ctx ctx = SHA512_INIT;
     94  *
     95  *	for (i = 0; arr[i]; i++)
     96  *		sha512_update(&ctx, arr[i], strlen(arr[i]));
     97  *	sha512_done(&ctx, hash);
     98  * }
     99  */
    100 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
    101 	{ { { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull,	\
    102 	      0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull,	\
    103 	      0x510e527fade682d1ull, 0x9b05688c2b3e6c1full,	\
    104 	      0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull },	\
    105 	    0, 0,						\
    106 	    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	\
    107 	    0, 0x40 } }
    108 #else
    109 #define SHA512_INIT						\
    110 	{ { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull,	\
    111 	    0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull,	\
    112 	    0x510e527fade682d1ull, 0x9b05688c2b3e6c1full,	\
    113 	    0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull },	\
    114 	  { { 0 } }, 0 }
    115 #endif
    116 
    117 /**
    118  * sha512_update - include some memory in the hash.
    119  * @ctx: the sha512_ctx to use
    120  * @p: pointer to memory,
    121  * @size: the number of bytes pointed to by @p
    122  *
    123  * You can call this multiple times to hash more data, before calling
    124  * sha512_done().
    125  */
    126 void sha512_update(struct sha512_ctx *ctx, const void *p, size_t size);
    127 
    128 /**
    129  * sha512_done - finish SHA512 and return the hash
    130  * @ctx: the sha512_ctx to complete
    131  * @res: the hash to return.
    132  *
    133  * Note that @ctx is *destroyed* by this, and must be reinitialized.
    134  * To avoid that, pass a copy instead.
    135  */
    136 void sha512_done(struct sha512_ctx *sha512, struct sha512 *res);
    137 
    138 #endif /* CCAN_CRYPTO_SHA512_H */