You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sha256.h 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef _IPXE_SHA256_H
  2. #define _IPXE_SHA256_H
  3. /** @file
  4. *
  5. * SHA-256 algorithm
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/crypto.h>
  11. /** SHA-256 number of rounds */
  12. #define SHA256_ROUNDS 64
  13. /** An SHA-256 digest */
  14. struct sha256_digest {
  15. /** Hash output */
  16. uint32_t h[8];
  17. };
  18. /** An SHA-256 data block */
  19. union sha256_block {
  20. /** Raw bytes */
  21. uint8_t byte[64];
  22. /** Raw dwords */
  23. uint32_t dword[16];
  24. /** Final block structure */
  25. struct {
  26. /** Padding */
  27. uint8_t pad[56];
  28. /** Length in bits */
  29. uint64_t len;
  30. } final;
  31. };
  32. /** SHA-256 digest and data block
  33. *
  34. * The order of fields within this structure is designed to minimise
  35. * code size.
  36. */
  37. struct sha256_digest_data {
  38. /** Digest of data already processed */
  39. struct sha256_digest digest;
  40. /** Accumulated data */
  41. union sha256_block data;
  42. } __attribute__ (( packed ));
  43. /** SHA-256 digest and data block */
  44. union sha256_digest_data_dwords {
  45. /** Digest and data block */
  46. struct sha256_digest_data dd;
  47. /** Raw dwords */
  48. uint32_t dword[ sizeof ( struct sha256_digest_data ) /
  49. sizeof ( uint32_t ) ];
  50. };
  51. /** An SHA-256 context */
  52. struct sha256_context {
  53. /** Amount of accumulated data */
  54. size_t len;
  55. /** Digest size */
  56. size_t digestsize;
  57. /** Digest and accumulated data */
  58. union sha256_digest_data_dwords ddd;
  59. } __attribute__ (( packed ));
  60. /** SHA-256 context size */
  61. #define SHA256_CTX_SIZE sizeof ( struct sha256_context )
  62. /** SHA-256 digest size */
  63. #define SHA256_DIGEST_SIZE sizeof ( struct sha256_digest )
  64. /** SHA-224 digest size */
  65. #define SHA224_DIGEST_SIZE ( SHA256_DIGEST_SIZE * 224 / 256 )
  66. extern void sha256_family_init ( struct sha256_context *context,
  67. const struct sha256_digest *init,
  68. size_t digestsize );
  69. extern void sha256_update ( void *ctx, const void *data, size_t len );
  70. extern void sha256_final ( void *ctx, void *out );
  71. extern struct digest_algorithm sha256_algorithm;
  72. extern struct digest_algorithm sha224_algorithm;
  73. #endif /* _IPXE_SHA256_H */