]> git.ozlabs.org Git - ccan/blob - ccan/crypto/sha256/sha256.h
Merge remote-tracking branch 'origin/pr/45'
[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                 /* Array of chars */
25                 unsigned char u8[32];
26                 /* Array of uint32_t */
27                 uint32_t u32[8];
28         } u;
29 };
30
31 /**
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
36  *
37  * The bytes pointed to by @p is SHA256 hashed into @sha256.  This is
38  * equivalent to sha256_init(), sha256_update() then sha256_done().
39  */
40 void sha256(struct sha256 *sha, const void *p, size_t size);
41
42 /**
43  * struct sha256_ctx - structure to store running context for sha256
44  */
45 struct sha256_ctx {
46 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
47         SHA256_CTX c;
48 #else
49         uint32_t s[8];
50         uint64_t bytes;
51         union {
52                 uint32_t u32[8];
53                 unsigned char u8[64];
54         } buf;
55 #endif
56 };
57
58 /**
59  * sha256_init - initialize an SHA256 context.
60  * @ctx: the sha256_ctx to initialize
61  *
62  * This must be called before sha256_update or sha256_done, or
63  * alternately you can assign SHA256_INIT.
64  *
65  * If it was already initialized, this forgets anything which was
66  * hashed before.
67  *
68  * Example:
69  * static void hash_all(const char **arr, struct sha256 *hash)
70  * {
71  *      size_t i;
72  *      struct sha256_ctx ctx;
73  *
74  *      sha256_init(&ctx);
75  *      for (i = 0; arr[i]; i++)
76  *              sha256_update(&ctx, arr[i], strlen(arr[i]));
77  *      sha256_done(&ctx, hash);
78  * }
79  */
80 void sha256_init(struct sha256_ctx *ctx);
81
82 /**
83  * SHA256_INIT - initializer for an SHA256 context.
84  *
85  * This can be used to statically initialize an SHA256 context (instead
86  * of sha256_init()).
87  *
88  * Example:
89  * static void hash_all(const char **arr, struct sha256 *hash)
90  * {
91  *      size_t i;
92  *      struct sha256_ctx ctx = SHA256_INIT;
93  *
94  *      for (i = 0; arr[i]; i++)
95  *              sha256_update(&ctx, arr[i], strlen(arr[i]));
96  *      sha256_done(&ctx, hash);
97  * }
98  */
99 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
100 #define SHA256_INIT                                                     \
101         { { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,   \
102               0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, \
103                 0x0, 0x0,                                               \
104                 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },     \
105                         0x0, 0x20 } }
106 #else
107 #define SHA256_INIT                                                     \
108         { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,     \
109             0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, 0 }
110 #endif
111
112 /**
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
117  *
118  * You can call this multiple times to hash more data, before calling
119  * sha256_done().
120  */
121 void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size);
122
123 /**
124  * sha256_done - finish SHA256 and return the hash
125  * @ctx: the sha256_ctx to complete
126  * @res: the hash to return.
127  *
128  * Note that @ctx is *destroyed* by this, and must be reinitialized.
129  * To avoid that, pass a copy instead.
130  */
131 void sha256_done(struct sha256_ctx *sha256, struct sha256 *res);
132
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);
138
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);
143
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 */