1 #ifndef CCAN_CRYPTO_SHA256_H
2 #define CCAN_CRYPTO_SHA256_H
3 /* BSD-MIT - see LICENSE file for details */
8 /* Uncomment this to use openssl's SHA256 routines (and link with -lcrypto) */
9 /*#define CCAN_CRYPTO_SHA256_USE_OPENSSL 1*/
11 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
12 #include <openssl/sha.h>
16 * struct sha256 - structure representing a completed SHA256.
17 * @u.u8: an unsigned char array.
18 * @u.u32: a 32-bit integer array.
20 * Other fields may be added to the union in future.
26 /* Array of uint32_t */
32 * sha256 - return sha256 of an object.
33 * @sha256: the sha256 to fill in
34 * @p: pointer to memory,
35 * @size: the number of bytes pointed to by @p
37 * The bytes pointed to by @p is SHA256 hashed into @sha256. This is
38 * equivalent to sha256_init(), sha256_update() then sha256_done().
40 void sha256(struct sha256 *sha, const void *p, size_t size);
43 * struct sha256_ctx - structure to store running context for sha256
46 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
59 * sha256_init - initialize an SHA256 context.
60 * @ctx: the sha256_ctx to initialize
62 * This must be called before sha256_update or sha256_done, or
63 * alternately you can assign SHA256_INIT.
65 * If it was already initialized, this forgets anything which was
69 * static void hash_all(const char **arr, struct sha256 *hash)
72 * struct sha256_ctx ctx;
75 * for (i = 0; arr[i]; i++)
76 * sha256_update(&ctx, arr[i], strlen(arr[i]));
77 * sha256_done(&ctx, hash);
80 void sha256_init(struct sha256_ctx *ctx);
83 * SHA256_INIT - initializer for an SHA256 context.
85 * This can be used to statically initialize an SHA256 context (instead
89 * static void hash_all(const char **arr, struct sha256 *hash)
92 * struct sha256_ctx ctx = SHA256_INIT;
94 * for (i = 0; arr[i]; i++)
95 * sha256_update(&ctx, arr[i], strlen(arr[i]));
96 * sha256_done(&ctx, hash);
99 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
100 #define SHA256_INIT \
101 { { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
102 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
104 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
107 #define SHA256_INIT \
108 { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
109 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, 0 }
113 * sha256_update - include some memory in the hash.
114 * @ctx: the sha256_ctx to use
115 * @p: pointer to memory,
116 * @size: the number of bytes pointed to by @p
118 * You can call this multiple times to hash more data, before calling
121 void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size);
124 * sha256_done - finish SHA256 and return the hash
125 * @ctx: the sha256_ctx to complete
126 * @res: the hash to return.
128 * Note that @ctx is *destroyed* by this, and must be reinitialized.
129 * To avoid that, pass a copy instead.
131 void sha256_done(struct sha256_ctx *sha256, struct sha256 *res);
133 /* Add various types to an SHA256 hash */
134 void sha256_u8(struct sha256_ctx *ctx, uint8_t v);
135 void sha256_u16(struct sha256_ctx *ctx, uint16_t v);
136 void sha256_u32(struct sha256_ctx *ctx, uint32_t v);
137 void sha256_u64(struct sha256_ctx *ctx, uint64_t v);
139 /* Add as little-endian */
140 void sha256_le16(struct sha256_ctx *ctx, uint16_t v);
141 void sha256_le32(struct sha256_ctx *ctx, uint32_t v);
142 void sha256_le64(struct sha256_ctx *ctx, uint64_t v);
144 /* Add as big-endian */
145 void sha256_be16(struct sha256_ctx *ctx, uint16_t v);
146 void sha256_be32(struct sha256_ctx *ctx, uint32_t v);
147 void sha256_be64(struct sha256_ctx *ctx, uint64_t v);
148 #endif /* CCAN_CRYPTO_SHA256_H */