]> git.ozlabs.org Git - ccan/blob - ccan/crypto/sha512/sha512.h
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / crypto / sha512 / sha512.h
1 #ifndef CCAN_CRYPTO_SHA512_H
2 #define CCAN_CRYPTO_SHA512_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 SHA512 routines (and link with -lcrypto) */
9 /*#define CCAN_CRYPTO_SHA512_USE_OPENSSL 1*/
10
11 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
12 #include <openssl/sha.h>
13 #endif
14
15 /**
16  * struct sha512 - structure representing a completed SHA512.
17  * @u.u8: an unsigned char array.
18  * @u.u64: a 64-bit integer array.
19  *
20  * Other fields may be added to the union in future.
21  */
22 struct sha512 {
23         union {
24                 uint64_t u64[8];
25                 unsigned char u8[64];
26         } u;
27 };
28
29 /**
30  * sha512 - return sha512 of an object.
31  * @sha512: the sha512 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 SHA512 hashed into @sha512.  This is
36  * equivalent to sha512_init(), sha512_update() then sha512_done().
37  */
38 void sha512(struct sha512 *sha, const void *p, size_t size);
39
40 /**
41  * struct sha512_ctx - structure to store running context for sha512
42  */
43 struct sha512_ctx {
44 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
45         SHA512_CTX c;
46 #else
47         uint64_t s[8];
48         union {
49                 uint64_t u64[16];
50                 unsigned char u8[128];
51         } buf;
52         size_t bytes;
53 #endif
54 };
55
56 /**
57  * sha512_init - initialize an SHA512 context.
58  * @ctx: the sha512_ctx to initialize
59  *
60  * This must be called before sha512_update or sha512_done, or
61  * alternately you can assign SHA512_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 sha512 *hash)
68  * {
69  *      size_t i;
70  *      struct sha512_ctx ctx;
71  *
72  *      sha512_init(&ctx);
73  *      for (i = 0; arr[i]; i++)
74  *              sha512_update(&ctx, arr[i], strlen(arr[i]));
75  *      sha512_done(&ctx, hash);
76  * }
77  */
78 void sha512_init(struct sha512_ctx *ctx);
79
80 /**
81  * SHA512_INIT - initializer for an SHA512 context.
82  *
83  * This can be used to statically initialize an SHA512 context (instead
84  * of sha512_init()).
85  *
86  * Example:
87  * static void hash_all(const char **arr, struct sha512 *hash)
88  * {
89  *      size_t i;
90  *      struct sha512_ctx ctx = SHA512_INIT;
91  *
92  *      for (i = 0; arr[i]; i++)
93  *              sha512_update(&ctx, arr[i], strlen(arr[i]));
94  *      sha512_done(&ctx, hash);
95  * }
96  */
97 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
98 #define SHA512_INIT                                           \
99         { { { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull,   \
100               0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull,   \
101               0x510e527fade682d1ull, 0x9b05688c2b3e6c1full,   \
102               0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull }, \
103             0, 0, { { 0 } }, 0, 0x40 } }
104 #else
105 #define SHA512_INIT                                         \
106         { { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull,   \
107             0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull,   \
108             0x510e527fade682d1ull, 0x9b05688c2b3e6c1full,   \
109             0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull }, \
110           { { 0 } }, 0 }
111 #endif
112
113 /**
114  * sha512_update - include some memory in the hash.
115  * @ctx: the sha512_ctx to use
116  * @p: pointer to memory,
117  * @size: the number of bytes pointed to by @p
118  *
119  * You can call this multiple times to hash more data, before calling
120  * sha512_done().
121  */
122 void sha512_update(struct sha512_ctx *ctx, const void *p, size_t size);
123
124 /**
125  * sha512_done - finish SHA512 and return the hash
126  * @ctx: the sha512_ctx to complete
127  * @res: the hash to return.
128  *
129  * Note that @ctx is *destroyed* by this, and must be reinitialized.
130  * To avoid that, pass a copy instead.
131  */
132 void sha512_done(struct sha512_ctx *sha512, struct sha512 *res);
133
134 #endif /* CCAN_CRYPTO_SHA512_H */