[crypto] Add SHA-512/224 algorithm
[ipxe.git] / src / include / ipxe / sha512.h
1 #ifndef _IPXE_SHA512_H
2 #define _IPXE_SHA512_H
3
4 /** @file
5 *
6 * SHA-512 algorithm
7 *
8 */
9
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
12 #include <stdint.h>
13 #include <ipxe/crypto.h>
14
15 /** SHA-512 number of rounds */
16 #define SHA512_ROUNDS 80
17
18 /** An SHA-512 digest */
19 struct sha512_digest {
20 /** Hash output */
21 uint64_t h[8];
22 };
23
24 /** An SHA-512 data block */
25 union sha512_block {
26 /** Raw bytes */
27 uint8_t byte[128];
28 /** Raw qwords */
29 uint64_t qword[16];
30 /** Final block structure */
31 struct {
32 /** Padding */
33 uint8_t pad[112];
34 /** High 64 bits of length in bits */
35 uint64_t len_hi;
36 /** Low 64 bits of length in bits */
37 uint64_t len_lo;
38 } final;
39 };
40
41 /** SHA-512 digest and data block
42 *
43 * The order of fields within this structure is designed to minimise
44 * code size.
45 */
46 struct sha512_digest_data {
47 /** Digest of data already processed */
48 struct sha512_digest digest;
49 /** Accumulated data */
50 union sha512_block data;
51 } __attribute__ (( packed ));
52
53 /** SHA-512 digest and data block */
54 union sha512_digest_data_qwords {
55 /** Digest and data block */
56 struct sha512_digest_data dd;
57 /** Raw qwords */
58 uint64_t qword[ sizeof ( struct sha512_digest_data ) /
59 sizeof ( uint64_t ) ];
60 };
61
62 /** An SHA-512 context */
63 struct sha512_context {
64 /** Amount of accumulated data */
65 size_t len;
66 /** Digest size */
67 size_t digestsize;
68 /** Digest and accumulated data */
69 union sha512_digest_data_qwords ddq;
70 } __attribute__ (( packed ));
71
72 /** SHA-512 context size */
73 #define SHA512_CTX_SIZE sizeof ( struct sha512_context )
74
75 /** SHA-512 digest size */
76 #define SHA512_DIGEST_SIZE sizeof ( struct sha512_digest )
77
78 /** SHA-384 digest size */
79 #define SHA384_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 384 / 512 )
80
81 /** SHA-512/256 digest size */
82 #define SHA512_256_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 256 / 512 )
83
84 /** SHA-512/224 digest size */
85 #define SHA512_224_DIGEST_SIZE ( SHA512_DIGEST_SIZE * 224 / 512 )
86
87 extern void sha512_family_init ( struct sha512_context *context,
88 const struct sha512_digest *init,
89 size_t digestsize );
90 extern void sha512_update ( void *ctx, const void *data, size_t len );
91 extern void sha512_final ( void *ctx, void *out );
92
93 extern struct digest_algorithm sha512_algorithm;
94 extern struct digest_algorithm sha384_algorithm;
95 extern struct digest_algorithm sha512_256_algorithm;
96 extern struct digest_algorithm sha512_224_algorithm;
97
98 #endif /* IPXE_SHA512_H */