[crypto] Add SHA-224 algorithm
[ipxe.git] / src / include / ipxe / sha256.h
1 #ifndef _IPXE_SHA256_H
2 #define _IPXE_SHA256_H
3
4 /** @file
5 *
6 * SHA-256 algorithm
7 *
8 */
9
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
12 #include <stdint.h>
13 #include <ipxe/crypto.h>
14
15 /** An SHA-256 digest */
16 struct sha256_digest {
17 /** Hash output */
18 uint32_t h[8];
19 };
20
21 /** An SHA-256 data block */
22 union sha256_block {
23 /** Raw bytes */
24 uint8_t byte[64];
25 /** Raw dwords */
26 uint32_t dword[16];
27 /** Final block structure */
28 struct {
29 /** Padding */
30 uint8_t pad[56];
31 /** Length in bits */
32 uint64_t len;
33 } final;
34 };
35
36 /** SHA-256 digest and data block
37 *
38 * The order of fields within this structure is designed to minimise
39 * code size.
40 */
41 struct sha256_digest_data {
42 /** Digest of data already processed */
43 struct sha256_digest digest;
44 /** Accumulated data */
45 union sha256_block data;
46 } __attribute__ (( packed ));
47
48 /** SHA-256 digest and data block */
49 union sha256_digest_data_dwords {
50 /** Digest and data block */
51 struct sha256_digest_data dd;
52 /** Raw dwords */
53 uint32_t dword[ sizeof ( struct sha256_digest_data ) /
54 sizeof ( uint32_t ) ];
55 };
56
57 /** An SHA-256 context */
58 struct sha256_context {
59 /** Amount of accumulated data */
60 size_t len;
61 /** Digest size */
62 size_t digestsize;
63 /** Digest and accumulated data */
64 union sha256_digest_data_dwords ddd;
65 } __attribute__ (( packed ));
66
67 /** SHA-256 context size */
68 #define SHA256_CTX_SIZE sizeof ( struct sha256_context )
69
70 /** SHA-256 digest size */
71 #define SHA256_DIGEST_SIZE sizeof ( struct sha256_digest )
72
73 /** SHA-224 digest size */
74 #define SHA224_DIGEST_SIZE ( SHA256_DIGEST_SIZE * 224 / 256 )
75
76 extern void sha256_family_init ( struct sha256_context *context,
77 const struct sha256_digest *init,
78 size_t digestsize );
79 extern void sha256_update ( void *ctx, const void *data, size_t len );
80 extern void sha256_final ( void *ctx, void *out );
81
82 extern struct digest_algorithm sha256_algorithm;
83 extern struct digest_algorithm sha224_algorithm;
84
85 #endif /* _IPXE_SHA256_H */