Reference implementations of PQC
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.
 
 
 
 

175 lines
3.9 KiB

  1. #ifndef SHA2_H
  2. #define SHA2_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. /* The incremental API allows hashing of individual input blocks; these blocks
  6. must be exactly 64 bytes each.
  7. Use the 'finalize' functions for any remaining bytes (possibly over 64). */
  8. #define PQC_SHA256CTX_BYTES 40
  9. /* Structure for the incremental API */
  10. typedef struct {
  11. uint8_t *ctx;
  12. } sha224ctx;
  13. /* Structure for the incremental API */
  14. typedef struct {
  15. uint8_t *ctx;
  16. } sha256ctx;
  17. #define PQC_SHA512CTX_BYTES 72
  18. /* Structure for the incremental API */
  19. typedef struct {
  20. uint8_t *ctx;
  21. } sha384ctx;
  22. /* Structure for the incremental API */
  23. typedef struct {
  24. uint8_t *ctx;
  25. } sha512ctx;
  26. /* ====== SHA224 API ==== */
  27. /**
  28. * Initialize the incremental hashing API.
  29. *
  30. * Can't be called multiple times.
  31. */
  32. void sha224_inc_init(sha224ctx *state);
  33. /**
  34. * Copy the hashing state
  35. */
  36. void sha224_inc_ctx_clone(sha224ctx *stateout, const sha224ctx *statein);
  37. /**
  38. * Absorb blocks
  39. */
  40. void sha224_inc_blocks(sha224ctx *state, const uint8_t *in, size_t inblocks);
  41. /**
  42. * Finalize and obtain the digest
  43. *
  44. * If applicable, this function will free the memory associated with the sha224ctx.
  45. *
  46. * If not calling this function, call `sha224_inc_ctx_release`
  47. */
  48. void sha224_inc_finalize(uint8_t *out, sha224ctx *state, const uint8_t *in, size_t inlen);
  49. /**
  50. * Destroy the state. Make sure to use this, as this API may not always be stack-based.
  51. */
  52. void sha224_inc_ctx_release(sha224ctx *state);
  53. /**
  54. * All-in-one sha224 function
  55. */
  56. void sha224(uint8_t *out, const uint8_t *in, size_t inlen);
  57. /* ====== SHA256 API ==== */
  58. /**
  59. * Initialize the incremental hashing API
  60. */
  61. void sha256_inc_init(sha256ctx *state);
  62. /**
  63. * Copy the hashing state
  64. */
  65. void sha256_inc_ctx_clone(sha256ctx *stateout, const sha256ctx *statein);
  66. /**
  67. * Absorb blocks
  68. */
  69. void sha256_inc_blocks(sha256ctx *state, const uint8_t *in, size_t inblocks);
  70. /**
  71. * Finalize and obtain the digest
  72. *
  73. * If applicable, this function will free the memory associated with the sha256ctx.
  74. */
  75. void sha256_inc_finalize(uint8_t *out, sha256ctx *state, const uint8_t *in, size_t inlen);
  76. /**
  77. * Destroy the state. Make sure to use this, as this API may not always be stack-based.
  78. */
  79. void sha256_inc_ctx_release(sha256ctx *state);
  80. /**
  81. * All-in-one sha256 function
  82. */
  83. void sha256(uint8_t *out, const uint8_t *in, size_t inlen);
  84. /* ====== SHA384 API ==== */
  85. /**
  86. * Initialize the incremental hashing API
  87. */
  88. void sha384_inc_init(sha384ctx *state);
  89. /**
  90. * Copy the hashing state
  91. */
  92. void sha384_inc_ctx_clone(sha384ctx *stateout, const sha384ctx *statein);
  93. /**
  94. * Absorb blocks
  95. */
  96. void sha384_inc_blocks(sha384ctx *state, const uint8_t *in, size_t inblocks);
  97. /**
  98. * Finalize and obtain the digest.
  99. *
  100. * If applicable, this function will free the memory associated with the sha384ctx.
  101. */
  102. void sha384_inc_finalize(uint8_t *out, sha384ctx *state, const uint8_t *in, size_t inlen);
  103. /**
  104. * Destroy the state. Make sure to use this if not calling finalize, as this API may not always be stack-based.
  105. */
  106. void sha384_inc_ctx_release(sha384ctx *state);
  107. /**
  108. * All-in-one sha384 function
  109. */
  110. void sha384(uint8_t *out, const uint8_t *in, size_t inlen);
  111. /* ====== SHA512 API ==== */
  112. /**
  113. * Initialize the incremental hashing API
  114. */
  115. void sha512_inc_init(sha512ctx *state);
  116. /**
  117. * Copy the hashing state
  118. */
  119. void sha512_inc_ctx_clone(sha512ctx *stateout, const sha512ctx *statein);
  120. /**
  121. * Absorb blocks
  122. */
  123. void sha512_inc_blocks(sha512ctx *state, const uint8_t *in, size_t inblocks);
  124. /**
  125. * Finalize and obtain the digest
  126. *
  127. * If applicable, this function will free the memory associated with the sha512ctx.
  128. */
  129. void sha512_inc_finalize(uint8_t *out, sha512ctx *state, const uint8_t *in, size_t inlen);
  130. /**
  131. * Destroy the state. Make sure to use this if not calling finalize, as this API may not always be stack-based.
  132. */
  133. void sha512_inc_ctx_release(sha512ctx *state);
  134. /**
  135. * All-in-one sha512 function
  136. */
  137. void sha512(uint8_t *out, const uint8_t *in, size_t inlen);
  138. #endif