1 #ifndef CCAN_CRYPTO_SHA512_H
2 #define CCAN_CRYPTO_SHA512_H
3 /* BSD-MIT - see LICENSE file for details */
8 /* Uncomment this to use openssl's SHA512 routines (and link with -lcrypto) */
9 /*#define CCAN_CRYPTO_SHA512_USE_OPENSSL 1*/
11 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
12 #include <openssl/sha.h>
16 * struct sha512 - structure representing a completed SHA512.
17 * @u.u8: an unsigned char array.
18 * @u.u64: a 64-bit integer array.
20 * Other fields may be added to the union in future.
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
35 * The bytes pointed to by @p is SHA512 hashed into @sha512. This is
36 * equivalent to sha512_init(), sha512_update() then sha512_done().
38 void sha512(struct sha512 *sha, const void *p, size_t size);
41 * struct sha512_ctx - structure to store running context for sha512
44 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
50 unsigned char u8[128];
57 * sha512_init - initialize an SHA512 context.
58 * @ctx: the sha512_ctx to initialize
60 * This must be called before sha512_update or sha512_done, or
61 * alternately you can assign SHA512_INIT.
63 * If it was already initialized, this forgets anything which was
67 * static void hash_all(const char **arr, struct sha512 *hash)
70 * struct sha512_ctx ctx;
73 * for (i = 0; arr[i]; i++)
74 * sha512_update(&ctx, arr[i], strlen(arr[i]));
75 * sha512_done(&ctx, hash);
78 void sha512_init(struct sha512_ctx *ctx);
81 * SHA512_INIT - initializer for an SHA512 context.
83 * This can be used to statically initialize an SHA512 context (instead
87 * static void hash_all(const char **arr, struct sha512 *hash)
90 * struct sha512_ctx ctx = SHA512_INIT;
92 * for (i = 0; arr[i]; i++)
93 * sha512_update(&ctx, arr[i], strlen(arr[i]));
94 * sha512_done(&ctx, hash);
97 #ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
99 { { { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull, \
100 0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull, \
101 0x510e527fade682d1ull, 0x9b05688c2b3e6c1full, \
102 0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull }, \
103 0, 0, { { 0 } }, 0, 0x40 } }
105 #define SHA512_INIT \
106 { { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull, \
107 0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull, \
108 0x510e527fade682d1ull, 0x9b05688c2b3e6c1full, \
109 0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull }, \
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
119 * You can call this multiple times to hash more data, before calling
122 void sha512_update(struct sha512_ctx *ctx, const void *p, size_t size);
125 * sha512_done - finish SHA512 and return the hash
126 * @ctx: the sha512_ctx to complete
127 * @res: the hash to return.
129 * Note that @ctx is *destroyed* by this, and must be reinitialized.
130 * To avoid that, pass a copy instead.
132 void sha512_done(struct sha512_ctx *sha512, struct sha512 *res);
134 #endif /* CCAN_CRYPTO_SHA512_H */