sha3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values for SHA-3 algorithms
  4. *
  5. * See also Documentation/crypto/sha3.rst
  6. */
  7. #ifndef __CRYPTO_SHA3_H__
  8. #define __CRYPTO_SHA3_H__
  9. #include <linux/types.h>
  10. #include <linux/string.h>
  11. #define SHA3_224_DIGEST_SIZE (224 / 8)
  12. #define SHA3_224_BLOCK_SIZE (200 - 2 * SHA3_224_DIGEST_SIZE)
  13. #define SHA3_224_EXPORT_SIZE SHA3_STATE_SIZE + SHA3_224_BLOCK_SIZE + 1
  14. #define SHA3_256_DIGEST_SIZE (256 / 8)
  15. #define SHA3_256_BLOCK_SIZE (200 - 2 * SHA3_256_DIGEST_SIZE)
  16. #define SHA3_256_EXPORT_SIZE SHA3_STATE_SIZE + SHA3_256_BLOCK_SIZE + 1
  17. #define SHA3_384_DIGEST_SIZE (384 / 8)
  18. #define SHA3_384_BLOCK_SIZE (200 - 2 * SHA3_384_DIGEST_SIZE)
  19. #define SHA3_384_EXPORT_SIZE SHA3_STATE_SIZE + SHA3_384_BLOCK_SIZE + 1
  20. #define SHA3_512_DIGEST_SIZE (512 / 8)
  21. #define SHA3_512_BLOCK_SIZE (200 - 2 * SHA3_512_DIGEST_SIZE)
  22. #define SHA3_512_EXPORT_SIZE SHA3_STATE_SIZE + SHA3_512_BLOCK_SIZE + 1
  23. /*
  24. * SHAKE128 and SHAKE256 actually have variable output size, but this is used to
  25. * calculate the block size (rate) analogously to the above.
  26. */
  27. #define SHAKE128_DEFAULT_SIZE (128 / 8)
  28. #define SHAKE128_BLOCK_SIZE (200 - 2 * SHAKE128_DEFAULT_SIZE)
  29. #define SHAKE256_DEFAULT_SIZE (256 / 8)
  30. #define SHAKE256_BLOCK_SIZE (200 - 2 * SHAKE256_DEFAULT_SIZE)
  31. #define SHA3_STATE_SIZE 200
  32. /*
  33. * State for the Keccak-f[1600] permutation: 25 64-bit words.
  34. *
  35. * We usually keep the state words as little-endian, to make absorbing and
  36. * squeezing easier. (It means that absorbing and squeezing can just treat the
  37. * state as a byte array.) The state words are converted to native-endian only
  38. * temporarily by implementations of the permutation that need native-endian
  39. * words. Of course, that conversion is a no-op on little-endian machines.
  40. */
  41. struct sha3_state {
  42. union {
  43. __le64 words[SHA3_STATE_SIZE / 8];
  44. u8 bytes[SHA3_STATE_SIZE];
  45. u64 native_words[SHA3_STATE_SIZE / 8]; /* see comment above */
  46. };
  47. };
  48. /* Internal context, shared by the digests (SHA3-*) and the XOFs (SHAKE*) */
  49. struct __sha3_ctx {
  50. struct sha3_state state;
  51. u8 digest_size; /* Digests only: the digest size in bytes */
  52. u8 block_size; /* Block size in bytes */
  53. u8 absorb_offset; /* Index of next state byte to absorb into */
  54. u8 squeeze_offset; /* XOFs only: index of next state byte to extract */
  55. };
  56. void __sha3_update(struct __sha3_ctx *ctx, const u8 *in, size_t in_len);
  57. /**
  58. * struct sha3_ctx - Context for SHA3-224, SHA3-256, SHA3-384, or SHA3-512
  59. * @ctx: private
  60. */
  61. struct sha3_ctx {
  62. struct __sha3_ctx ctx;
  63. };
  64. /**
  65. * sha3_zeroize_ctx() - Zeroize a SHA-3 context
  66. * @ctx: The context to zeroize
  67. *
  68. * This is already called by sha3_final(). Call this explicitly when abandoning
  69. * a context without calling sha3_final().
  70. */
  71. static inline void sha3_zeroize_ctx(struct sha3_ctx *ctx)
  72. {
  73. memzero_explicit(ctx, sizeof(*ctx));
  74. }
  75. /**
  76. * struct shake_ctx - Context for SHAKE128 or SHAKE256
  77. * @ctx: private
  78. */
  79. struct shake_ctx {
  80. struct __sha3_ctx ctx;
  81. };
  82. /**
  83. * shake_zeroize_ctx() - Zeroize a SHAKE context
  84. * @ctx: The context to zeroize
  85. *
  86. * Call this after the last squeeze.
  87. */
  88. static inline void shake_zeroize_ctx(struct shake_ctx *ctx)
  89. {
  90. memzero_explicit(ctx, sizeof(*ctx));
  91. }
  92. /**
  93. * sha3_224_init() - Initialize a context for SHA3-224
  94. * @ctx: The context to initialize
  95. *
  96. * This begins a new SHA3-224 message digest computation.
  97. *
  98. * Context: Any context.
  99. */
  100. static inline void sha3_224_init(struct sha3_ctx *ctx)
  101. {
  102. *ctx = (struct sha3_ctx){
  103. .ctx.digest_size = SHA3_224_DIGEST_SIZE,
  104. .ctx.block_size = SHA3_224_BLOCK_SIZE,
  105. };
  106. }
  107. /**
  108. * sha3_256_init() - Initialize a context for SHA3-256
  109. * @ctx: The context to initialize
  110. *
  111. * This begins a new SHA3-256 message digest computation.
  112. *
  113. * Context: Any context.
  114. */
  115. static inline void sha3_256_init(struct sha3_ctx *ctx)
  116. {
  117. *ctx = (struct sha3_ctx){
  118. .ctx.digest_size = SHA3_256_DIGEST_SIZE,
  119. .ctx.block_size = SHA3_256_BLOCK_SIZE,
  120. };
  121. }
  122. /**
  123. * sha3_384_init() - Initialize a context for SHA3-384
  124. * @ctx: The context to initialize
  125. *
  126. * This begins a new SHA3-384 message digest computation.
  127. *
  128. * Context: Any context.
  129. */
  130. static inline void sha3_384_init(struct sha3_ctx *ctx)
  131. {
  132. *ctx = (struct sha3_ctx){
  133. .ctx.digest_size = SHA3_384_DIGEST_SIZE,
  134. .ctx.block_size = SHA3_384_BLOCK_SIZE,
  135. };
  136. }
  137. /**
  138. * sha3_512_init() - Initialize a context for SHA3-512
  139. * @ctx: The context to initialize
  140. *
  141. * This begins a new SHA3-512 message digest computation.
  142. *
  143. * Context: Any context.
  144. */
  145. static inline void sha3_512_init(struct sha3_ctx *ctx)
  146. {
  147. *ctx = (struct sha3_ctx){
  148. .ctx.digest_size = SHA3_512_DIGEST_SIZE,
  149. .ctx.block_size = SHA3_512_BLOCK_SIZE,
  150. };
  151. }
  152. /**
  153. * sha3_update() - Update a SHA-3 digest context with input data
  154. * @ctx: The context to update; must have been initialized
  155. * @in: The input data
  156. * @in_len: Length of the input data in bytes
  157. *
  158. * This can be called any number of times to add data to a SHA3-224, SHA3-256,
  159. * SHA3-384, or SHA3-512 digest (depending on which init function was called).
  160. *
  161. * Context: Any context.
  162. */
  163. static inline void sha3_update(struct sha3_ctx *ctx,
  164. const u8 *in, size_t in_len)
  165. {
  166. __sha3_update(&ctx->ctx, in, in_len);
  167. }
  168. /**
  169. * sha3_final() - Finish computing a SHA-3 message digest
  170. * @ctx: The context to finalize; must have been initialized
  171. * @out: (output) The resulting SHA3-224, SHA3-256, SHA3-384, or SHA3-512
  172. * message digest, matching the init function that was called. Note that
  173. * the size differs for each one; see SHA3_*_DIGEST_SIZE.
  174. *
  175. * After finishing, this zeroizes @ctx. So the caller does not need to do it.
  176. *
  177. * Context: Any context.
  178. */
  179. void sha3_final(struct sha3_ctx *ctx, u8 *out);
  180. /**
  181. * shake128_init() - Initialize a context for SHAKE128
  182. * @ctx: The context to initialize
  183. *
  184. * This begins a new SHAKE128 extendable-output function (XOF) computation.
  185. *
  186. * Context: Any context.
  187. */
  188. static inline void shake128_init(struct shake_ctx *ctx)
  189. {
  190. *ctx = (struct shake_ctx){
  191. .ctx.block_size = SHAKE128_BLOCK_SIZE,
  192. };
  193. }
  194. /**
  195. * shake256_init() - Initialize a context for SHAKE256
  196. * @ctx: The context to initialize
  197. *
  198. * This begins a new SHAKE256 extendable-output function (XOF) computation.
  199. *
  200. * Context: Any context.
  201. */
  202. static inline void shake256_init(struct shake_ctx *ctx)
  203. {
  204. *ctx = (struct shake_ctx){
  205. .ctx.block_size = SHAKE256_BLOCK_SIZE,
  206. };
  207. }
  208. /**
  209. * shake_update() - Update a SHAKE context with input data
  210. * @ctx: The context to update; must have been initialized
  211. * @in: The input data
  212. * @in_len: Length of the input data in bytes
  213. *
  214. * This can be called any number of times to add more input data to SHAKE128 or
  215. * SHAKE256. This cannot be called after squeezing has begun.
  216. *
  217. * Context: Any context.
  218. */
  219. static inline void shake_update(struct shake_ctx *ctx,
  220. const u8 *in, size_t in_len)
  221. {
  222. __sha3_update(&ctx->ctx, in, in_len);
  223. }
  224. /**
  225. * shake_squeeze() - Generate output from SHAKE128 or SHAKE256
  226. * @ctx: The context to squeeze; must have been initialized
  227. * @out: Where to write the resulting output data
  228. * @out_len: The amount of data to extract to @out in bytes
  229. *
  230. * This may be called multiple times. A number of consecutive squeezes laid
  231. * end-to-end will yield the same output as one big squeeze generating the same
  232. * total amount of output. More input cannot be provided after squeezing has
  233. * begun. After the last squeeze, call shake_zeroize_ctx().
  234. *
  235. * Context: Any context.
  236. */
  237. void shake_squeeze(struct shake_ctx *ctx, u8 *out, size_t out_len);
  238. /**
  239. * sha3_224() - Compute SHA3-224 digest in one shot
  240. * @in: The input data to be digested
  241. * @in_len: Length of the input data in bytes
  242. * @out: The buffer into which the digest will be stored
  243. *
  244. * Convenience function that computes a SHA3-224 digest. Use this instead of
  245. * the incremental API if you're able to provide all the input at once.
  246. *
  247. * Context: Any context.
  248. */
  249. void sha3_224(const u8 *in, size_t in_len, u8 out[SHA3_224_DIGEST_SIZE]);
  250. /**
  251. * sha3_256() - Compute SHA3-256 digest in one shot
  252. * @in: The input data to be digested
  253. * @in_len: Length of the input data in bytes
  254. * @out: The buffer into which the digest will be stored
  255. *
  256. * Convenience function that computes a SHA3-256 digest. Use this instead of
  257. * the incremental API if you're able to provide all the input at once.
  258. *
  259. * Context: Any context.
  260. */
  261. void sha3_256(const u8 *in, size_t in_len, u8 out[SHA3_256_DIGEST_SIZE]);
  262. /**
  263. * sha3_384() - Compute SHA3-384 digest in one shot
  264. * @in: The input data to be digested
  265. * @in_len: Length of the input data in bytes
  266. * @out: The buffer into which the digest will be stored
  267. *
  268. * Convenience function that computes a SHA3-384 digest. Use this instead of
  269. * the incremental API if you're able to provide all the input at once.
  270. *
  271. * Context: Any context.
  272. */
  273. void sha3_384(const u8 *in, size_t in_len, u8 out[SHA3_384_DIGEST_SIZE]);
  274. /**
  275. * sha3_512() - Compute SHA3-512 digest in one shot
  276. * @in: The input data to be digested
  277. * @in_len: Length of the input data in bytes
  278. * @out: The buffer into which the digest will be stored
  279. *
  280. * Convenience function that computes a SHA3-512 digest. Use this instead of
  281. * the incremental API if you're able to provide all the input at once.
  282. *
  283. * Context: Any context.
  284. */
  285. void sha3_512(const u8 *in, size_t in_len, u8 out[SHA3_512_DIGEST_SIZE]);
  286. /**
  287. * shake128() - Compute SHAKE128 in one shot
  288. * @in: The input data to be used
  289. * @in_len: Length of the input data in bytes
  290. * @out: The buffer into which the output will be stored
  291. * @out_len: Length of the output to produce in bytes
  292. *
  293. * Convenience function that computes SHAKE128 in one shot. Use this instead of
  294. * the incremental API if you're able to provide all the input at once as well
  295. * as receive all the output at once. All output lengths are supported.
  296. *
  297. * Context: Any context.
  298. */
  299. void shake128(const u8 *in, size_t in_len, u8 *out, size_t out_len);
  300. /**
  301. * shake256() - Compute SHAKE256 in one shot
  302. * @in: The input data to be used
  303. * @in_len: Length of the input data in bytes
  304. * @out: The buffer into which the output will be stored
  305. * @out_len: Length of the output to produce in bytes
  306. *
  307. * Convenience function that computes SHAKE256 in one shot. Use this instead of
  308. * the incremental API if you're able to provide all the input at once as well
  309. * as receive all the output at once. All output lengths are supported.
  310. *
  311. * Context: Any context.
  312. */
  313. void shake256(const u8 *in, size_t in_len, u8 *out, size_t out_len);
  314. #endif /* __CRYPTO_SHA3_H__ */