damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

sha256.h (4201B)


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