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