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 1.9KB

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