123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- #ifndef VBOOT_REFERENCE_2SHA_H_
- #define VBOOT_REFERENCE_2SHA_H_
- #include "2crypto.h"
- #ifndef VB2_SUPPORT_SHA1
- #define VB2_SUPPORT_SHA1 1
- #endif
- #ifndef VB2_SUPPORT_SHA256
- #define VB2_SUPPORT_SHA256 1
- #endif
- #ifndef VB2_SUPPORT_SHA512
- #define VB2_SUPPORT_SHA512 1
- #endif
- #define VB2_MAX_DIGEST_SIZE VB2_SHA512_DIGEST_SIZE
- #define VB2_MAX_BLOCK_SIZE VB2_SHA512_BLOCK_SIZE
- #define VB2_INVALID_ALG_NAME "INVALID"
- #define VB2_SHA1_DIGEST_SIZE 20
- #define VB2_SHA1_BLOCK_SIZE 64
- #define VB2_SHA1_ALG_NAME "SHA1"
- struct vb2_sha1_context {
- uint32_t count;
- uint32_t state[5];
- #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
- union {
- uint8_t b[VB2_SHA1_BLOCK_SIZE];
- uint32_t w[VB2_SHA1_BLOCK_SIZE / sizeof(uint32_t)];
- } buf;
- #else
- uint8_t buf[VB2_SHA1_BLOCK_SIZE];
- #endif
- };
- #define VB2_SHA256_DIGEST_SIZE 32
- #define VB2_SHA256_BLOCK_SIZE 64
- #define VB2_SHA256_ALG_NAME "SHA256"
- struct vb2_sha256_context {
- uint32_t h[8];
- uint32_t total_size;
- uint32_t size;
- uint8_t block[2 * VB2_SHA256_BLOCK_SIZE];
- };
- #define VB2_SHA512_DIGEST_SIZE 64
- #define VB2_SHA512_BLOCK_SIZE 128
- #define VB2_SHA512_ALG_NAME "SHA512"
- struct vb2_sha512_context {
- uint64_t h[8];
- uint32_t total_size;
- uint32_t size;
- uint8_t block[2 * VB2_SHA512_BLOCK_SIZE];
- };
- struct vb2_digest_context {
-
- union {
- #if VB2_SUPPORT_SHA1
- struct vb2_sha1_context sha1;
- #endif
- #if VB2_SUPPORT_SHA256
- struct vb2_sha256_context sha256;
- #endif
- #if VB2_SUPPORT_SHA512
- struct vb2_sha512_context sha512;
- #endif
- };
-
- enum vb2_hash_algorithm hash_alg;
-
- int using_hwcrypto;
- };
- void vb2_sha1_init(struct vb2_sha1_context *ctx);
- void vb2_sha256_init(struct vb2_sha256_context *ctx);
- void vb2_sha512_init(struct vb2_sha512_context *ctx);
- void vb2_sha1_update(struct vb2_sha1_context *ctx,
- const uint8_t *data,
- uint32_t size);
- void vb2_sha256_update(struct vb2_sha256_context *ctx,
- const uint8_t *data,
- uint32_t size);
- void vb2_sha512_update(struct vb2_sha512_context *ctx,
- const uint8_t *data,
- uint32_t size);
- void vb2_sha1_finalize(struct vb2_sha1_context *ctx, uint8_t *digest);
- void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest);
- void vb2_sha512_finalize(struct vb2_sha512_context *ctx, uint8_t *digest);
- void vb2_sha256_extend(const uint8_t *from, const uint8_t *by, uint8_t *to);
- enum vb2_hash_algorithm vb2_crypto_to_hash(uint32_t algorithm);
- int vb2_digest_size(enum vb2_hash_algorithm hash_alg);
- int vb2_hash_block_size(enum vb2_hash_algorithm alg);
- const char *vb2_get_hash_algorithm_name(enum vb2_hash_algorithm alg);
- int vb2_digest_init(struct vb2_digest_context *dc,
- enum vb2_hash_algorithm hash_alg);
- int vb2_digest_extend(struct vb2_digest_context *dc,
- const uint8_t *buf,
- uint32_t size);
- int vb2_digest_finalize(struct vb2_digest_context *dc,
- uint8_t *digest,
- uint32_t digest_size);
- int vb2_digest_buffer(const uint8_t *buf,
- uint32_t size,
- enum vb2_hash_algorithm hash_alg,
- uint8_t *digest,
- uint32_t digest_size);
- #endif
|