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.
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 @p
35 * The bytes pointed to by @p is SHA256 hashed into @sha256. This is
36 * equivalent to sha256_init(), sha256_update() then sha256_done().
38 void sha256(struct sha256 *sha, const void *p, size_t size);
41 * struct sha256_ctx - structure to store running context for sha256
44 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
57 * sha256_init - initialize an SHA256 context.
58 * @ctx: the sha256_ctx to initialize
60 * This must be called before sha256_update or sha256_done, or
61 * alternately you can assign SHA256_INIT.
63 * If it was already initialized, this forgets anything which was
67 * static void hash_all(const char **arr, struct sha256 *hash)
70 * struct sha256_ctx ctx;
73 * for (i = 0; arr[i]; i++)
74 * sha256_update(&ctx, arr[i], strlen(arr[i]));
75 * sha256_done(&ctx, hash);
78 void sha256_init(struct sha256_ctx *ctx);
81 * SHA256_INIT - initializer for an SHA256 context.
83 * This can be used to statically initialize an SHA256 context (instead
87 * static void hash_all(const char **arr, struct sha256 *hash)
90 * struct sha256_ctx ctx = SHA256_INIT;
92 * for (i = 0; arr[i]; i++)
93 * sha256_update(&ctx, arr[i], strlen(arr[i]));
94 * sha256_done(&ctx, hash);
97 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
99 { { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
100 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
102 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
105 #define SHA256_INIT \
106 { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, \
107 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
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 @p
117 * You can call this multiple times to hash more data, before calling
120 void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size);
123 * sha256_done - finish SHA256 and return the hash
124 * @ctx: the sha256_ctx to complete
125 * @res: the hash to return.
127 * Note that @ctx is *destroyed* by this, and must be reinitialized.
128 * To avoid that, pass a copy instead.
130 void sha256_done(struct sha256_ctx *sha256, struct sha256 *res);
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);
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);
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 */