]> git.ozlabs.org Git - ccan/blob - ccan/crypto/sha256/sha256.h
1297c41f48f0fabbc9bfda41c9dd93627e0c1858
[ccan] / ccan / crypto / sha256 / sha256.h
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 @p
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         uint64_t bytes;
49         union {
50                 uint32_t u32[16];
51                 unsigned char u8[64];
52         } buf;
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 staticly 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 }, 0 }
108 #endif
109
110 /**
111  * sha256_update - include some memory in the hash.
112  * @ctx: the sha256_ctx to use
113  * @p: pointer to memory,
114  * @size: the number of bytes pointed to by @p
115  *
116  * You can call this multiple times to hash more data, before calling
117  * sha256_done().
118  */
119 void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size);
120
121 /**
122  * sha256_done - finish SHA256 and return the hash
123  * @ctx: the sha256_ctx to complete
124  * @res: the hash to return.
125  *
126  * Note that @ctx is *destroyed* by this, and must be reinitialized.
127  * To avoid that, pass a copy instead.
128  */
129 void sha256_done(struct sha256_ctx *sha256, struct sha256 *res);
130
131 /* Add various types to an SHA256 hash */
132 void sha256_u8(struct sha256_ctx *ctx, uint8_t v);
133 void sha256_u16(struct sha256_ctx *ctx, uint16_t v);
134 void sha256_u32(struct sha256_ctx *ctx, uint32_t v);
135 void sha256_u64(struct sha256_ctx *ctx, uint64_t v);
136
137 /* Add as little-endian */
138 void sha256_le16(struct sha256_ctx *ctx, uint16_t v);
139 void sha256_le32(struct sha256_ctx *ctx, uint32_t v);
140 void sha256_le64(struct sha256_ctx *ctx, uint64_t v);
141
142 /* Add as big-endian */
143 void sha256_be16(struct sha256_ctx *ctx, uint16_t v);
144 void sha256_be32(struct sha256_ctx *ctx, uint32_t v);
145 void sha256_be64(struct sha256_ctx *ctx, uint64_t v);
146 #endif /* CCAN_CRYPTO_SHA256_H */