-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsha512.h
138 lines (126 loc) · 3.5 KB
/
sha512.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef CCAN_CRYPTO_SHA512_H
#define CCAN_CRYPTO_SHA512_H
/* BSD-MIT - see LICENSE file for details */
#include <stdint.h>
#include <stdlib.h>
/** Output length for `wally_sha512` */
#define SHA512_LEN 64
/* Uncomment this to use openssl's SHA512 routines (and link with -lcrypto) */
/*#define CCAN_CRYPTO_SHA512_USE_OPENSSL 1*/
#ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
#include <openssl/sha.h>
#endif
/**
* struct sha512 - structure representing a completed SHA512.
* @u.u8: an unsigned char array.
* @u.u64: a 64-bit integer array.
*
* Other fields may be added to the union in future.
*/
struct sha512 {
union {
uint64_t u64[8];
unsigned char u8[64];
} u;
};
/**
* sha512 - return sha512 of an object.
* @sha512: the sha512 to fill in
* @p: pointer to memory,
* @size: the number of bytes pointed to by @p
*
* The bytes pointed to by @p is SHA512 hashed into @sha512. This is
* equivalent to sha512_init(), sha512_update() then sha512_done().
*/
void sha512(struct sha512 *sha, const void *p, size_t size);
/**
* struct sha512_ctx - structure to store running context for sha512
*/
struct sha512_ctx {
#ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
SHA512_CTX c;
#else
uint64_t s[8];
union {
uint64_t u64[16];
unsigned char u8[128];
} buf;
size_t bytes;
#endif
};
/**
* sha512_init - initialize an SHA512 context.
* @ctx: the sha512_ctx to initialize
*
* This must be called before sha512_update or sha512_done, or
* alternately you can assign SHA512_INIT.
*
* If it was already initialized, this forgets anything which was
* hashed before.
*
* Example:
* static void hash_all(const char **arr, struct sha512 *hash)
* {
* size_t i;
* struct sha512_ctx ctx;
*
* sha512_init(&ctx);
* for (i = 0; arr[i]; i++)
* sha512_update(&ctx, arr[i], strlen(arr[i]));
* sha512_done(&ctx, hash);
* }
*/
void sha512_init(struct sha512_ctx *ctx);
/**
* SHA512_INIT - initializer for an SHA512 context.
*
* This can be used to statically initialize an SHA512 context (instead
* of sha512_init()).
*
* Example:
* static void hash_all(const char **arr, struct sha512 *hash)
* {
* size_t i;
* struct sha512_ctx ctx = SHA512_INIT;
*
* for (i = 0; arr[i]; i++)
* sha512_update(&ctx, arr[i], strlen(arr[i]));
* sha512_done(&ctx, hash);
* }
*/
#ifdef CCAN_CRYPTO_SHA512_USE_OPENSSL
{ { { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull, \
0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull, \
0x510e527fade682d1ull, 0x9b05688c2b3e6c1full, \
0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull }, \
0, 0, \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, \
0, 0x40 } }
#else
#define SHA512_INIT \
{ { 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull, \
0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull, \
0x510e527fade682d1ull, 0x9b05688c2b3e6c1full, \
0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull }, \
{ { 0 } }, 0 }
#endif
/**
* sha512_update - include some memory in the hash.
* @ctx: the sha512_ctx to use
* @p: pointer to memory,
* @size: the number of bytes pointed to by @p
*
* You can call this multiple times to hash more data, before calling
* sha512_done().
*/
void sha512_update(struct sha512_ctx *ctx, const void *p, size_t size);
/**
* sha512_done - finish SHA512 and return the hash
* @ctx: the sha512_ctx to complete
* @res: the hash to return.
*
* Note that @ctx is *destroyed* by this, and must be reinitialized.
* To avoid that, pass a copy instead.
*/
void sha512_done(struct sha512_ctx *sha512, struct sha512 *res);
#endif /* CCAN_CRYPTO_SHA512_H */