sha2.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef SHA2_H
  15. #define SHA2_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include "chash.h"
  19. /**
  20. * SHA224/SHA256
  21. * =============
  22. */
  23. /* .. c:macro:: CF_SHA224_HASHSZ
  24. * The output size of SHA224: 28 bytes. */
  25. #define CF_SHA224_HASHSZ 28
  26. /* .. c:macro:: CF_SHA224_BLOCKSZ
  27. * The block size of SHA224: 64 bytes. */
  28. #define CF_SHA224_BLOCKSZ 64
  29. /* .. c:macro:: CF_SHA256_HASHSZ
  30. * The output size of SHA256: 32 bytes. */
  31. #define CF_SHA256_HASHSZ 32
  32. /* .. c:macro:: CF_SHA256_BLOCKSZ
  33. * The block size of SHA256: 64 bytes. */
  34. #define CF_SHA256_BLOCKSZ 64
  35. /* .. c:type:: cf_sha256_context
  36. * Incremental SHA256 hashing context.
  37. *
  38. * .. c:member:: cf_sha256_context.H
  39. * Intermediate values.
  40. *
  41. * .. c:member:: cf_sha256_context.partial
  42. * Unprocessed input.
  43. *
  44. * .. c:member:: cf_sha256_context.npartial
  45. * Number of bytes of unprocessed input.
  46. *
  47. * .. c:member:: cf_sha256_context.blocks
  48. * Number of full blocks processed.
  49. */
  50. typedef struct
  51. {
  52. uint32_t H[8]; /* State. */
  53. uint8_t partial[CF_SHA256_BLOCKSZ]; /* Partial block of input. */
  54. uint32_t blocks; /* Number of full blocks processed into H. */
  55. size_t npartial; /* Number of bytes in prefix of partial. */
  56. } cf_sha256_context;
  57. /* .. c:function:: $DECL
  58. * Sets up `ctx` ready to hash a new message.
  59. */
  60. extern void cf_sha256_init(cf_sha256_context *ctx);
  61. /* .. c:function:: $DECL
  62. * Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
  63. * a full block.
  64. */
  65. extern void cf_sha256_update(cf_sha256_context *ctx, const void *data, size_t nbytes);
  66. /* .. c:function:: $DECL
  67. * Finishes the hash operation, writing `CF_SHA256_HASHSZ` bytes to `hash`.
  68. *
  69. * This leaves `ctx` unchanged.
  70. */
  71. extern void cf_sha256_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ]);
  72. /* .. c:function:: $DECL
  73. * Finishes the hash operation, writing `CF_SHA256_HASHSZ` bytes to `hash`.
  74. *
  75. * This destroys `ctx`, but uses less stack than :c:func:`cf_sha256_digest`.
  76. */
  77. extern void cf_sha256_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ]);
  78. /* .. c:function:: $DECL
  79. * Sets up `ctx` ready to hash a new message.
  80. *
  81. * nb. SHA224 uses SHA256's underlying types.
  82. */
  83. extern void cf_sha224_init(cf_sha256_context *ctx);
  84. /* .. c:function:: $DECL
  85. * Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
  86. * a full block.
  87. */
  88. extern void cf_sha224_update(cf_sha256_context *ctx, const void *data, size_t nbytes);
  89. /* .. c:function:: $DECL
  90. * Finishes the hash operation, writing `CF_SHA224_HASHSZ` bytes to `hash`.
  91. *
  92. * This leaves `ctx` unchanged.
  93. */
  94. extern void cf_sha224_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ]);
  95. /* .. c:function:: $DECL
  96. * Finishes the hash operation, writing `CF_SHA224_HASHSZ` bytes to `hash`.
  97. *
  98. * This destroys `ctx`, but uses less stack than :c:func:`cf_sha224_digest`.
  99. */
  100. extern void cf_sha224_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ]);
  101. /* .. c:var:: cf_sha224
  102. * Abstract interface to SHA224. See :c:type:`cf_chash` for more information.
  103. */
  104. extern const cf_chash cf_sha224;
  105. /* .. c:var:: cf_sha256
  106. * Abstract interface to SHA256. See :c:type:`cf_chash` for more information.
  107. */
  108. extern const cf_chash cf_sha256;
  109. /**
  110. * SHA384/SHA512
  111. * =============
  112. */
  113. /* .. c:macro:: CF_SHA384_HASHSZ
  114. * The output size of SHA384: 48 bytes. */
  115. #define CF_SHA384_HASHSZ 48
  116. /* .. c:macro:: CF_SHA384_BLOCKSZ
  117. * The block size of SHA384: 128 bytes. */
  118. #define CF_SHA384_BLOCKSZ 128
  119. /* .. c:macro:: CF_SHA512_HASHSZ
  120. * The output size of SHA512: 64 bytes. */
  121. #define CF_SHA512_HASHSZ 64
  122. /* .. c:macro:: CF_SHA512_BLOCKSZ
  123. * The block size of SHA512: 128 bytes. */
  124. #define CF_SHA512_BLOCKSZ 128
  125. /* .. c:type:: cf_sha512_context
  126. * Incremental SHA512 hashing context.
  127. *
  128. * .. c:member:: cf_sha512_context.H
  129. * Intermediate values.
  130. *
  131. * .. c:member:: cf_sha512_context.partial
  132. * Unprocessed input.
  133. *
  134. * .. c:member:: cf_sha512_context.npartial
  135. * Number of bytes of unprocessed input.
  136. *
  137. * .. c:member:: cf_sha512_context.blocks
  138. * Number of full blocks processed.
  139. */
  140. typedef struct
  141. {
  142. uint64_t H[8];
  143. uint8_t partial[CF_SHA512_BLOCKSZ];
  144. uint32_t blocks;
  145. size_t npartial;
  146. } cf_sha512_context;
  147. /* .. c:function:: $DECL
  148. * Sets up `ctx` ready to hash a new message.
  149. */
  150. extern void cf_sha512_init(cf_sha512_context *ctx);
  151. /* .. c:function:: $DECL
  152. * Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
  153. * a full block.
  154. */
  155. extern void cf_sha512_update(cf_sha512_context *ctx, const void *data, size_t nbytes);
  156. /* .. c:function:: $DECL
  157. * Finishes the hash operation, writing `CF_SHA512_HASHSZ` bytes to `hash`.
  158. *
  159. * This leaves `ctx` unchanged.
  160. */
  161. extern void cf_sha512_digest(const cf_sha512_context *ctx, uint8_t hash[CF_SHA512_HASHSZ]);
  162. /* .. c:function:: $DECL
  163. * Finishes the hash operation, writing `CF_SHA512_HASHSZ` bytes to `hash`.
  164. *
  165. * This destroys `ctx`, but uses less stack than :c:func:`cf_sha512_digest`.
  166. */
  167. extern void cf_sha512_digest_final(cf_sha512_context *ctx, uint8_t hash[CF_SHA512_HASHSZ]);
  168. /* .. c:function:: $DECL
  169. * Sets up `ctx` ready to hash a new message.
  170. *
  171. * nb. SHA384 uses SHA512's underlying types.
  172. */
  173. extern void cf_sha384_init(cf_sha512_context *ctx);
  174. /* .. c:function:: $DECL
  175. * Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
  176. * a full block.
  177. */
  178. extern void cf_sha384_update(cf_sha512_context *ctx, const void *data, size_t nbytes);
  179. /* .. c:function:: $DECL
  180. * Finishes the hash operation, writing `CF_SHA384_HASHSZ` bytes to `hash`.
  181. *
  182. * This leaves `ctx` unchanged.
  183. */
  184. extern void cf_sha384_digest(const cf_sha512_context *ctx, uint8_t hash[CF_SHA384_HASHSZ]);
  185. /* .. c:function:: $DECL
  186. * Finishes the hash operation, writing `CF_SHA384_HASHSZ` bytes to `hash`.
  187. *
  188. * This destroys `ctx`, but uses less stack than :c:func:`cf_sha384_digest`.
  189. */
  190. extern void cf_sha384_digest_final(cf_sha512_context *ctx, uint8_t hash[CF_SHA384_HASHSZ]);
  191. /* .. c:var:: cf_sha384
  192. * Abstract interface to SHA384. See :c:type:`cf_chash` for more information.
  193. */
  194. extern const cf_chash cf_sha384;
  195. /* .. c:var:: cf_sha512
  196. * Abstract interface to SHA512. See :c:type:`cf_chash` for more information.
  197. */
  198. extern const cf_chash cf_sha512;
  199. #endif