1 /* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #ifndef VBOOT_REFERENCE_2SHA_H_
7 #define VBOOT_REFERENCE_2SHA_H_
8 
9 #include "2crypto.h"
10 #include "2struct.h"
11 
12 /* Hash algorithms may be disabled individually to save code space */
13 
14 #ifndef VB2_SUPPORT_SHA1
15 #define VB2_SUPPORT_SHA1 1
16 #endif
17 
18 #ifndef VB2_SUPPORT_SHA256
19 #define VB2_SUPPORT_SHA256 1
20 #endif
21 
22 #ifndef VB2_SUPPORT_SHA512
23 #define VB2_SUPPORT_SHA512 1
24 #endif
25 
26 #define VB2_SHA1_DIGEST_SIZE 20
27 #define VB2_SHA1_BLOCK_SIZE 64
28 
29 /* Context structs for hash algorithms */
30 
31 struct vb2_sha1_context {
32 	uint32_t count;
33 	uint32_t state[5];
34 #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
35 	union {
36 		uint8_t b[VB2_SHA1_BLOCK_SIZE];
37 		uint32_t w[VB2_SHA1_BLOCK_SIZE / sizeof(uint32_t)];
38 	} buf;
39 #else
40 	uint8_t buf[VB2_SHA1_BLOCK_SIZE];
41 #endif
42 };
43 
44 #define VB2_SHA256_DIGEST_SIZE 32
45 #define VB2_SHA256_BLOCK_SIZE 64
46 
47 struct vb2_sha256_context {
48 	uint32_t h[8];
49 	uint32_t total_size;
50 	uint32_t size;
51 	uint8_t block[2 * VB2_SHA256_BLOCK_SIZE];
52 };
53 
54 #define VB2_SHA512_DIGEST_SIZE 64
55 #define VB2_SHA512_BLOCK_SIZE 128
56 
57 struct vb2_sha512_context {
58 	uint64_t h[8];
59 	uint32_t total_size;
60 	uint32_t size;
61 	uint8_t block[2 * VB2_SHA512_BLOCK_SIZE];
62 };
63 
64 /* Hash algorithm independent digest context; includes all of the above. */
65 struct vb2_digest_context {
66 	/* Context union for all algorithms */
67 	union {
68 #if VB2_SUPPORT_SHA1
69 		struct vb2_sha1_context sha1;
70 #endif
71 #if VB2_SUPPORT_SHA256
72 		struct vb2_sha256_context sha256;
73 #endif
74 #if VB2_SUPPORT_SHA512
75 		struct vb2_sha512_context sha512;
76 #endif
77 	};
78 
79 	/* Current hash algorithm */
80 	enum vb2_hash_algorithm hash_alg;
81 
82 	/* 1 if digest is computed with vb2ex_hwcrypto routines, else 0 */
83 	int using_hwcrypto;
84 };
85 
86 /**
87  * Initialize a hash context.
88  *
89  * @param ctx		Hash context
90  */
91 void vb2_sha1_init(struct vb2_sha1_context *ctx);
92 void vb2_sha256_init(struct vb2_sha256_context *ctx);
93 void vb2_sha512_init(struct vb2_sha512_context *ctx);
94 
95 /**
96  * Update (extend) a hash.
97  *
98  * @param ctx		Hash context
99  * @param data		Data to hash
100  * @param size		Length of data in bytes
101  */
102 void vb2_sha1_update(struct vb2_sha1_context *ctx,
103 		     const uint8_t *data,
104 		     uint32_t size);
105 void vb2_sha256_update(struct vb2_sha256_context *ctx,
106 		       const uint8_t *data,
107 		       uint32_t size);
108 void vb2_sha512_update(struct vb2_sha512_context *ctx,
109 		       const uint8_t *data,
110 		       uint32_t size);
111 
112 /**
113  * Finalize a hash digest.
114  *
115  * @param ctx		Hash context
116  * @param digest	Destination for hash; must be VB_SHA*_DIGEST_SIZE bytes
117  */
118 void vb2_sha1_finalize(struct vb2_sha1_context *ctx, uint8_t *digest);
119 void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest);
120 void vb2_sha512_finalize(struct vb2_sha512_context *ctx, uint8_t *digest);
121 
122 /**
123  * Convert vb2_crypto_algorithm to vb2_hash_algorithm.
124  *
125  * @param algorithm	Crypto algorithm (vb2_crypto_algorithm)
126  *
127  * @return The hash algorithm for that crypto algorithm, or VB2_HASH_INVALID if
128  * the crypto algorithm or its corresponding hash algorithm is invalid or not
129  * supported.
130  */
131 enum vb2_hash_algorithm vb2_crypto_to_hash(uint32_t algorithm);
132 
133 /**
134  * Return the size of the digest for a hash algorithm.
135  *
136  * @param hash_alg	Hash algorithm
137  * @return The size of the digest, or 0 if error.
138  */
139 int vb2_digest_size(enum vb2_hash_algorithm hash_alg);
140 
141 /**
142  * Initialize a digest context for doing block-style digesting.
143  *
144  * @param dc		Digest context
145  * @param hash_alg	Hash algorithm
146  * @return VB2_SUCCESS, or non-zero on error.
147  */
148 int vb2_digest_init(struct vb2_digest_context *dc,
149 		    enum vb2_hash_algorithm hash_alg);
150 
151 /**
152  * Extend a digest's hash with another block of data.
153  *
154  * @param dc		Digest context
155  * @param buf		Data to hash
156  * @param size		Length of data in bytes
157  * @return VB2_SUCCESS, or non-zero on error.
158  */
159 int vb2_digest_extend(struct vb2_digest_context *dc,
160 		      const uint8_t *buf,
161 		      uint32_t size);
162 
163 /**
164  * Finalize a digest and store the result.
165  *
166  * The destination digest should be at least vb2_digest_size(algorithm).
167  *
168  * @param dc		Digest context
169  * @param digest	Destination for digest
170  * @param digest_size	Length of digest buffer in bytes.
171  * @return VB2_SUCCESS, or non-zero on error.
172  */
173 int vb2_digest_finalize(struct vb2_digest_context *dc,
174 			uint8_t *digest,
175 			uint32_t digest_size);
176 
177 #endif  /* VBOOT_REFERENCE_2SHA_H_ */
178