| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef VBOOT_REFERENCE_2SHA_H_ |
| 12 |
#define VBOOT_REFERENCE_2SHA_H_ |
| 13 |
|
| 14 |
#include "2crypto.h" |
| 15 |
#include "2return_codes.h" |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef VB2_SUPPORT_SHA1 |
| 20 |
#define VB2_SUPPORT_SHA1 1 |
| 21 |
#endif |
| 22 |
|
| 23 |
#ifndef VB2_SUPPORT_SHA256 |
| 24 |
#define VB2_SUPPORT_SHA256 1 |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifndef VB2_SUPPORT_SHA512 |
| 28 |
#define VB2_SUPPORT_SHA512 1 |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define VB2_MAX_DIGEST_SIZE VB2_SHA512_DIGEST_SIZE |
| 34 |
#define VB2_MAX_BLOCK_SIZE VB2_SHA512_BLOCK_SIZE |
| 35 |
#define VB2_INVALID_ALG_NAME "INVALID" |
| 36 |
|
| 37 |
#define VB2_SHA1_DIGEST_SIZE 20 |
| 38 |
#define VB2_SHA1_BLOCK_SIZE 64 |
| 39 |
#define VB2_SHA1_ALG_NAME "SHA1" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
struct vb2_sha1_context { |
| 44 |
uint32_t count; |
| 45 |
uint32_t state[5]; |
| 46 |
#if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN) |
| 47 |
union { |
| 48 |
uint8_t b[VB2_SHA1_BLOCK_SIZE]; |
| 49 |
uint32_t w[VB2_SHA1_BLOCK_SIZE / sizeof(uint32_t)]; |
| 50 |
} buf; |
| 51 |
#else |
| 52 |
uint8_t buf[VB2_SHA1_BLOCK_SIZE]; |
| 53 |
#endif |
| 54 |
}; |
| 55 |
|
| 56 |
#define VB2_SHA256_DIGEST_SIZE 32 |
| 57 |
#define VB2_SHA256_BLOCK_SIZE 64 |
| 58 |
#define VB2_SHA256_ALG_NAME "SHA256" |
| 59 |
|
| 60 |
struct vb2_sha256_context { |
| 61 |
uint32_t h[8]; |
| 62 |
uint32_t total_size; |
| 63 |
uint32_t size; |
| 64 |
uint8_t block[2 * VB2_SHA256_BLOCK_SIZE]; |
| 65 |
}; |
| 66 |
|
| 67 |
#define VB2_SHA512_DIGEST_SIZE 64 |
| 68 |
#define VB2_SHA512_BLOCK_SIZE 128 |
| 69 |
#define VB2_SHA512_ALG_NAME "SHA512" |
| 70 |
|
| 71 |
struct vb2_sha512_context { |
| 72 |
uint64_t h[8]; |
| 73 |
uint32_t total_size; |
| 74 |
uint32_t size; |
| 75 |
uint8_t block[2 * VB2_SHA512_BLOCK_SIZE]; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#define VB2_SHA224_DIGEST_SIZE 28 |
| 83 |
#define VB2_SHA224_ALG_NAME "SHA224" |
| 84 |
#define VB2_SHA384_DIGEST_SIZE 48 |
| 85 |
#define VB2_SHA384_ALG_NAME "SHA384" |
| 86 |
|
| 87 |
|
| 88 |
struct vb2_digest_context { |
| 89 |
|
| 90 |
union { |
| 91 |
#if VB2_SUPPORT_SHA1 |
| 92 |
struct vb2_sha1_context sha1; |
| 93 |
#endif |
| 94 |
#if VB2_SUPPORT_SHA256 |
| 95 |
struct vb2_sha256_context sha256; |
| 96 |
#endif |
| 97 |
#if VB2_SUPPORT_SHA512 |
| 98 |
struct vb2_sha512_context sha512; |
| 99 |
#endif |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
enum vb2_hash_algorithm hash_alg; |
| 104 |
|
| 105 |
|
| 106 |
bool using_hwcrypto; |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
struct vb2_hash { |
| 118 |
|
| 119 |
|
| 120 |
uint8_t reserved[3]; |
| 121 |
|
| 122 |
uint8_t algo; |
| 123 |
|
| 124 |
union { |
| 125 |
uint8_t raw[0]; |
| 126 |
#if VB2_SUPPORT_SHA1 |
| 127 |
uint8_t sha1[VB2_SHA1_DIGEST_SIZE]; |
| 128 |
#endif |
| 129 |
#if VB2_SUPPORT_SHA256 |
| 130 |
uint8_t sha256[VB2_SHA256_DIGEST_SIZE]; |
| 131 |
#endif |
| 132 |
#if VB2_SUPPORT_SHA512 |
| 133 |
uint8_t sha512[VB2_SHA512_DIGEST_SIZE]; |
| 134 |
#endif |
| 135 |
}; |
| 136 |
}; |
| 137 |
_Static_assert(sizeof(struct vb2_hash) - offsetof(struct vb2_hash, raw) |
| 138 |
<= VB2_MAX_DIGEST_SIZE, "Update VB2_MAX_DIGEST_SIZE for new digests!"); |
| 139 |
_Static_assert(VB2_HASH_ALG_COUNT <= UINT8_MAX, "vb2_hash.algo overflow!"); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
void vb2_sha1_init(struct vb2_sha1_context *ctx); |
| 148 |
void vb2_sha256_init(struct vb2_sha256_context *ctx, |
| 149 |
enum vb2_hash_algorithm algo); |
| 150 |
void vb2_sha512_init(struct vb2_sha512_context *ctx, |
| 151 |
enum vb2_hash_algorithm algo); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
void vb2_sha1_update(struct vb2_sha1_context *ctx, |
| 161 |
const uint8_t *data, |
| 162 |
uint32_t size); |
| 163 |
void vb2_sha256_update(struct vb2_sha256_context *ctx, |
| 164 |
const uint8_t *data, |
| 165 |
uint32_t size); |
| 166 |
void vb2_sha512_update(struct vb2_sha512_context *ctx, |
| 167 |
const uint8_t *data, |
| 168 |
uint32_t size); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
void vb2_sha1_finalize(struct vb2_sha1_context *ctx, uint8_t *digest); |
| 178 |
void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest, |
| 179 |
enum vb2_hash_algorithm algo); |
| 180 |
void vb2_sha512_finalize(struct vb2_sha512_context *ctx, uint8_t *digest, |
| 181 |
enum vb2_hash_algorithm algo); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
void vb2_sha256_extend(const uint8_t *from, const uint8_t *by, uint8_t *to); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
size_t vb2_digest_size(enum vb2_hash_algorithm hash_alg); |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
size_t vb2_hash_block_size(enum vb2_hash_algorithm alg); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
vb2_error_t vb2_digest_init(struct vb2_digest_context *dc, bool allow_hwcrypto, |
| 220 |
enum vb2_hash_algorithm algo, uint32_t data_size); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
vb2_error_t vb2_digest_extend(struct vb2_digest_context *dc, const uint8_t *buf, |
| 231 |
uint32_t size); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
vb2_error_t vb2_digest_finalize(struct vb2_digest_context *dc, |
| 244 |
uint8_t *digest, uint32_t digest_size); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
vb2_error_t vb2_hash_calculate(bool allow_hwcrypto, const void *buf, |
| 257 |
uint32_t size, enum vb2_hash_algorithm algo, |
| 258 |
struct vb2_hash *hash); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
vb2_error_t vb2_hash_verify(bool allow_hwcrypto, const void *buf, uint32_t size, |
| 271 |
const struct vb2_hash *hash); |
| 272 |
|
| 273 |
#endif |