Always pad fingerprints to 16 characters
[project/usign.git] / sha512.h
1 /*
2 * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /* SHA512
18 * Daniel Beer <dlbeer@gmail.com>, 22 Apr 2014
19 *
20 * This file is in the public domain.
21 */
22
23 #ifndef SHA512_H_
24 #define SHA512_H_
25
26 #include <sys/types.h>
27 #include <stdint.h>
28 #include <stddef.h>
29 #include <string.h>
30
31 /* Feed a full block in */
32 #define SHA512_BLOCK_SIZE 128
33
34 /* SHA512 state. State is updated as data is fed in, and then the final
35 * hash can be read out in slices.
36 *
37 * Data is fed in as a sequence of full blocks terminated by a single
38 * partial block.
39 */
40 struct sha512_state {
41 uint64_t h[8];
42 uint8_t partial[SHA512_BLOCK_SIZE];
43 size_t len;
44 };
45
46 /* Set up a new context */
47 void sha512_init(struct sha512_state *s);
48
49 void sha512_add(struct sha512_state *s, const void *data, size_t len);
50
51 /* Fetch a slice of the hash result. */
52 #define SHA512_HASH_SIZE 64
53
54 void sha512_final(struct sha512_state *s, uint8_t *hash);
55
56 static inline void *
57 sha512_final_get(struct sha512_state *s)
58 {
59 sha512_final(s, s->partial);
60 return s->partial;
61 }
62
63 #endif