avb_sha.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #ifdef AVB_INSIDE_LIBAVB_H
  6. #error "You can't include avb_sha.h in the public header libavb.h."
  7. #endif
  8. #ifndef AVB_COMPILATION
  9. #error "Never include this file, it may only be used from internal avb code."
  10. #endif
  11. #ifndef AVB_SHA_H_
  12. #define AVB_SHA_H_
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #include "avb_crypto.h"
  17. #include "avb_sysdeps.h"
  18. /* Block size in bytes of a SHA-256 digest. */
  19. #define AVB_SHA256_BLOCK_SIZE 64
  20. /* Block size in bytes of a SHA-512 digest. */
  21. #define AVB_SHA512_BLOCK_SIZE 128
  22. /* Data structure used for SHA-256. */
  23. typedef struct {
  24. uint32_t h[8];
  25. uint64_t tot_len;
  26. size_t len;
  27. uint8_t block[2 * AVB_SHA256_BLOCK_SIZE];
  28. uint8_t buf[AVB_SHA256_DIGEST_SIZE]; /* Used for storing the final digest. */
  29. } AvbSHA256Ctx;
  30. /* Data structure used for SHA-512. */
  31. typedef struct {
  32. uint64_t h[8];
  33. uint64_t tot_len;
  34. size_t len;
  35. uint8_t block[2 * AVB_SHA512_BLOCK_SIZE];
  36. uint8_t buf[AVB_SHA512_DIGEST_SIZE]; /* Used for storing the final digest. */
  37. } AvbSHA512Ctx;
  38. /* Initializes the SHA-256 context. */
  39. void avb_sha256_init(AvbSHA256Ctx* ctx);
  40. /* Updates the SHA-256 context with |len| bytes from |data|. */
  41. void avb_sha256_update(AvbSHA256Ctx* ctx, const uint8_t* data, size_t len);
  42. /* Returns the SHA-256 digest. */
  43. uint8_t* avb_sha256_final(AvbSHA256Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT;
  44. /* Initializes the SHA-512 context. */
  45. void avb_sha512_init(AvbSHA512Ctx* ctx);
  46. /* Updates the SHA-512 context with |len| bytes from |data|. */
  47. void avb_sha512_update(AvbSHA512Ctx* ctx, const uint8_t* data, size_t len);
  48. /* Returns the SHA-512 digest. */
  49. uint8_t* avb_sha512_final(AvbSHA512Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT;
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #endif /* AVB_SHA_H_ */