sha3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SHA-3, as specified in
  4. * https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
  5. *
  6. * SHA-3 code by Jeff Garzik <jeff@garzik.org>
  7. * Ard Biesheuvel <ard.biesheuvel@linaro.org>
  8. * David Howells <dhowells@redhat.com>
  9. *
  10. * See also Documentation/crypto/sha3.rst
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <crypto/sha3.h>
  14. #include <crypto/utils.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/unaligned.h>
  19. #include "fips.h"
  20. /*
  21. * On some 32-bit architectures, such as h8300, GCC ends up using over 1 KB of
  22. * stack if the round calculation gets inlined into the loop in
  23. * sha3_keccakf_generic(). On the other hand, on 64-bit architectures with
  24. * plenty of [64-bit wide] general purpose registers, not inlining it severely
  25. * hurts performance. So let's use 64-bitness as a heuristic to decide whether
  26. * to inline or not.
  27. */
  28. #ifdef CONFIG_64BIT
  29. #define SHA3_INLINE inline
  30. #else
  31. #define SHA3_INLINE noinline
  32. #endif
  33. #define SHA3_KECCAK_ROUNDS 24
  34. static const u64 sha3_keccakf_rndc[SHA3_KECCAK_ROUNDS] = {
  35. 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
  36. 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
  37. 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
  38. 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
  39. 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
  40. 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
  41. 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
  42. 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
  43. };
  44. /*
  45. * Perform a single round of Keccak mixing.
  46. */
  47. static SHA3_INLINE void sha3_keccakf_one_round_generic(u64 st[25], int round)
  48. {
  49. u64 t[5], tt, bc[5];
  50. /* Theta */
  51. bc[0] = st[0] ^ st[5] ^ st[10] ^ st[15] ^ st[20];
  52. bc[1] = st[1] ^ st[6] ^ st[11] ^ st[16] ^ st[21];
  53. bc[2] = st[2] ^ st[7] ^ st[12] ^ st[17] ^ st[22];
  54. bc[3] = st[3] ^ st[8] ^ st[13] ^ st[18] ^ st[23];
  55. bc[4] = st[4] ^ st[9] ^ st[14] ^ st[19] ^ st[24];
  56. t[0] = bc[4] ^ rol64(bc[1], 1);
  57. t[1] = bc[0] ^ rol64(bc[2], 1);
  58. t[2] = bc[1] ^ rol64(bc[3], 1);
  59. t[3] = bc[2] ^ rol64(bc[4], 1);
  60. t[4] = bc[3] ^ rol64(bc[0], 1);
  61. st[0] ^= t[0];
  62. /* Rho Pi */
  63. tt = st[1];
  64. st[ 1] = rol64(st[ 6] ^ t[1], 44);
  65. st[ 6] = rol64(st[ 9] ^ t[4], 20);
  66. st[ 9] = rol64(st[22] ^ t[2], 61);
  67. st[22] = rol64(st[14] ^ t[4], 39);
  68. st[14] = rol64(st[20] ^ t[0], 18);
  69. st[20] = rol64(st[ 2] ^ t[2], 62);
  70. st[ 2] = rol64(st[12] ^ t[2], 43);
  71. st[12] = rol64(st[13] ^ t[3], 25);
  72. st[13] = rol64(st[19] ^ t[4], 8);
  73. st[19] = rol64(st[23] ^ t[3], 56);
  74. st[23] = rol64(st[15] ^ t[0], 41);
  75. st[15] = rol64(st[ 4] ^ t[4], 27);
  76. st[ 4] = rol64(st[24] ^ t[4], 14);
  77. st[24] = rol64(st[21] ^ t[1], 2);
  78. st[21] = rol64(st[ 8] ^ t[3], 55);
  79. st[ 8] = rol64(st[16] ^ t[1], 45);
  80. st[16] = rol64(st[ 5] ^ t[0], 36);
  81. st[ 5] = rol64(st[ 3] ^ t[3], 28);
  82. st[ 3] = rol64(st[18] ^ t[3], 21);
  83. st[18] = rol64(st[17] ^ t[2], 15);
  84. st[17] = rol64(st[11] ^ t[1], 10);
  85. st[11] = rol64(st[ 7] ^ t[2], 6);
  86. st[ 7] = rol64(st[10] ^ t[0], 3);
  87. st[10] = rol64( tt ^ t[1], 1);
  88. /* Chi */
  89. bc[ 0] = ~st[ 1] & st[ 2];
  90. bc[ 1] = ~st[ 2] & st[ 3];
  91. bc[ 2] = ~st[ 3] & st[ 4];
  92. bc[ 3] = ~st[ 4] & st[ 0];
  93. bc[ 4] = ~st[ 0] & st[ 1];
  94. st[ 0] ^= bc[ 0];
  95. st[ 1] ^= bc[ 1];
  96. st[ 2] ^= bc[ 2];
  97. st[ 3] ^= bc[ 3];
  98. st[ 4] ^= bc[ 4];
  99. bc[ 0] = ~st[ 6] & st[ 7];
  100. bc[ 1] = ~st[ 7] & st[ 8];
  101. bc[ 2] = ~st[ 8] & st[ 9];
  102. bc[ 3] = ~st[ 9] & st[ 5];
  103. bc[ 4] = ~st[ 5] & st[ 6];
  104. st[ 5] ^= bc[ 0];
  105. st[ 6] ^= bc[ 1];
  106. st[ 7] ^= bc[ 2];
  107. st[ 8] ^= bc[ 3];
  108. st[ 9] ^= bc[ 4];
  109. bc[ 0] = ~st[11] & st[12];
  110. bc[ 1] = ~st[12] & st[13];
  111. bc[ 2] = ~st[13] & st[14];
  112. bc[ 3] = ~st[14] & st[10];
  113. bc[ 4] = ~st[10] & st[11];
  114. st[10] ^= bc[ 0];
  115. st[11] ^= bc[ 1];
  116. st[12] ^= bc[ 2];
  117. st[13] ^= bc[ 3];
  118. st[14] ^= bc[ 4];
  119. bc[ 0] = ~st[16] & st[17];
  120. bc[ 1] = ~st[17] & st[18];
  121. bc[ 2] = ~st[18] & st[19];
  122. bc[ 3] = ~st[19] & st[15];
  123. bc[ 4] = ~st[15] & st[16];
  124. st[15] ^= bc[ 0];
  125. st[16] ^= bc[ 1];
  126. st[17] ^= bc[ 2];
  127. st[18] ^= bc[ 3];
  128. st[19] ^= bc[ 4];
  129. bc[ 0] = ~st[21] & st[22];
  130. bc[ 1] = ~st[22] & st[23];
  131. bc[ 2] = ~st[23] & st[24];
  132. bc[ 3] = ~st[24] & st[20];
  133. bc[ 4] = ~st[20] & st[21];
  134. st[20] ^= bc[ 0];
  135. st[21] ^= bc[ 1];
  136. st[22] ^= bc[ 2];
  137. st[23] ^= bc[ 3];
  138. st[24] ^= bc[ 4];
  139. /* Iota */
  140. st[0] ^= sha3_keccakf_rndc[round];
  141. }
  142. /* Generic implementation of the Keccak-f[1600] permutation */
  143. static void sha3_keccakf_generic(struct sha3_state *state)
  144. {
  145. /*
  146. * Temporarily convert the state words from little-endian to native-
  147. * endian so that they can be operated on. Note that on little-endian
  148. * machines this conversion is a no-op and is optimized out.
  149. */
  150. for (int i = 0; i < ARRAY_SIZE(state->words); i++)
  151. state->native_words[i] = le64_to_cpu(state->words[i]);
  152. for (int round = 0; round < SHA3_KECCAK_ROUNDS; round++)
  153. sha3_keccakf_one_round_generic(state->native_words, round);
  154. for (int i = 0; i < ARRAY_SIZE(state->words); i++)
  155. state->words[i] = cpu_to_le64(state->native_words[i]);
  156. }
  157. /*
  158. * Generic implementation of absorbing the given nonzero number of full blocks
  159. * into the sponge function Keccak[r=8*block_size, c=1600-8*block_size].
  160. */
  161. static void __maybe_unused
  162. sha3_absorb_blocks_generic(struct sha3_state *state, const u8 *data,
  163. size_t nblocks, size_t block_size)
  164. {
  165. do {
  166. for (size_t i = 0; i < block_size; i += 8)
  167. state->words[i / 8] ^= get_unaligned((__le64 *)&data[i]);
  168. sha3_keccakf_generic(state);
  169. data += block_size;
  170. } while (--nblocks);
  171. }
  172. #ifdef CONFIG_CRYPTO_LIB_SHA3_ARCH
  173. #include "sha3.h" /* $(SRCARCH)/sha3.h */
  174. #else
  175. #define sha3_keccakf sha3_keccakf_generic
  176. #define sha3_absorb_blocks sha3_absorb_blocks_generic
  177. #endif
  178. void __sha3_update(struct __sha3_ctx *ctx, const u8 *in, size_t in_len)
  179. {
  180. const size_t block_size = ctx->block_size;
  181. size_t absorb_offset = ctx->absorb_offset;
  182. /* Warn if squeezing has already begun. */
  183. WARN_ON_ONCE(absorb_offset >= block_size);
  184. if (absorb_offset && absorb_offset + in_len >= block_size) {
  185. crypto_xor(&ctx->state.bytes[absorb_offset], in,
  186. block_size - absorb_offset);
  187. in += block_size - absorb_offset;
  188. in_len -= block_size - absorb_offset;
  189. sha3_keccakf(&ctx->state);
  190. absorb_offset = 0;
  191. }
  192. if (in_len >= block_size) {
  193. size_t nblocks = in_len / block_size;
  194. sha3_absorb_blocks(&ctx->state, in, nblocks, block_size);
  195. in += nblocks * block_size;
  196. in_len -= nblocks * block_size;
  197. }
  198. if (in_len) {
  199. crypto_xor(&ctx->state.bytes[absorb_offset], in, in_len);
  200. absorb_offset += in_len;
  201. }
  202. ctx->absorb_offset = absorb_offset;
  203. }
  204. EXPORT_SYMBOL_GPL(__sha3_update);
  205. void sha3_final(struct sha3_ctx *sha3_ctx, u8 *out)
  206. {
  207. struct __sha3_ctx *ctx = &sha3_ctx->ctx;
  208. ctx->state.bytes[ctx->absorb_offset] ^= 0x06;
  209. ctx->state.bytes[ctx->block_size - 1] ^= 0x80;
  210. sha3_keccakf(&ctx->state);
  211. memcpy(out, ctx->state.bytes, ctx->digest_size);
  212. sha3_zeroize_ctx(sha3_ctx);
  213. }
  214. EXPORT_SYMBOL_GPL(sha3_final);
  215. void shake_squeeze(struct shake_ctx *shake_ctx, u8 *out, size_t out_len)
  216. {
  217. struct __sha3_ctx *ctx = &shake_ctx->ctx;
  218. const size_t block_size = ctx->block_size;
  219. size_t squeeze_offset = ctx->squeeze_offset;
  220. if (ctx->absorb_offset < block_size) {
  221. /* First squeeze: */
  222. /* Add the domain separation suffix and padding. */
  223. ctx->state.bytes[ctx->absorb_offset] ^= 0x1f;
  224. ctx->state.bytes[block_size - 1] ^= 0x80;
  225. /* Indicate that squeezing has begun. */
  226. ctx->absorb_offset = block_size;
  227. /*
  228. * Indicate that no output is pending yet, i.e. sha3_keccakf()
  229. * will need to be called before the first copy.
  230. */
  231. squeeze_offset = block_size;
  232. }
  233. while (out_len) {
  234. if (squeeze_offset == block_size) {
  235. sha3_keccakf(&ctx->state);
  236. squeeze_offset = 0;
  237. }
  238. size_t copy = min(out_len, block_size - squeeze_offset);
  239. memcpy(out, &ctx->state.bytes[squeeze_offset], copy);
  240. out += copy;
  241. out_len -= copy;
  242. squeeze_offset += copy;
  243. }
  244. ctx->squeeze_offset = squeeze_offset;
  245. }
  246. EXPORT_SYMBOL_GPL(shake_squeeze);
  247. #ifndef sha3_224_arch
  248. static inline bool sha3_224_arch(const u8 *in, size_t in_len,
  249. u8 out[SHA3_224_DIGEST_SIZE])
  250. {
  251. return false;
  252. }
  253. #endif
  254. #ifndef sha3_256_arch
  255. static inline bool sha3_256_arch(const u8 *in, size_t in_len,
  256. u8 out[SHA3_256_DIGEST_SIZE])
  257. {
  258. return false;
  259. }
  260. #endif
  261. #ifndef sha3_384_arch
  262. static inline bool sha3_384_arch(const u8 *in, size_t in_len,
  263. u8 out[SHA3_384_DIGEST_SIZE])
  264. {
  265. return false;
  266. }
  267. #endif
  268. #ifndef sha3_512_arch
  269. static inline bool sha3_512_arch(const u8 *in, size_t in_len,
  270. u8 out[SHA3_512_DIGEST_SIZE])
  271. {
  272. return false;
  273. }
  274. #endif
  275. void sha3_224(const u8 *in, size_t in_len, u8 out[SHA3_224_DIGEST_SIZE])
  276. {
  277. struct sha3_ctx ctx;
  278. if (sha3_224_arch(in, in_len, out))
  279. return;
  280. sha3_224_init(&ctx);
  281. sha3_update(&ctx, in, in_len);
  282. sha3_final(&ctx, out);
  283. }
  284. EXPORT_SYMBOL_GPL(sha3_224);
  285. void sha3_256(const u8 *in, size_t in_len, u8 out[SHA3_256_DIGEST_SIZE])
  286. {
  287. struct sha3_ctx ctx;
  288. if (sha3_256_arch(in, in_len, out))
  289. return;
  290. sha3_256_init(&ctx);
  291. sha3_update(&ctx, in, in_len);
  292. sha3_final(&ctx, out);
  293. }
  294. EXPORT_SYMBOL_GPL(sha3_256);
  295. void sha3_384(const u8 *in, size_t in_len, u8 out[SHA3_384_DIGEST_SIZE])
  296. {
  297. struct sha3_ctx ctx;
  298. if (sha3_384_arch(in, in_len, out))
  299. return;
  300. sha3_384_init(&ctx);
  301. sha3_update(&ctx, in, in_len);
  302. sha3_final(&ctx, out);
  303. }
  304. EXPORT_SYMBOL_GPL(sha3_384);
  305. void sha3_512(const u8 *in, size_t in_len, u8 out[SHA3_512_DIGEST_SIZE])
  306. {
  307. struct sha3_ctx ctx;
  308. if (sha3_512_arch(in, in_len, out))
  309. return;
  310. sha3_512_init(&ctx);
  311. sha3_update(&ctx, in, in_len);
  312. sha3_final(&ctx, out);
  313. }
  314. EXPORT_SYMBOL_GPL(sha3_512);
  315. void shake128(const u8 *in, size_t in_len, u8 *out, size_t out_len)
  316. {
  317. struct shake_ctx ctx;
  318. shake128_init(&ctx);
  319. shake_update(&ctx, in, in_len);
  320. shake_squeeze(&ctx, out, out_len);
  321. shake_zeroize_ctx(&ctx);
  322. }
  323. EXPORT_SYMBOL_GPL(shake128);
  324. void shake256(const u8 *in, size_t in_len, u8 *out, size_t out_len)
  325. {
  326. struct shake_ctx ctx;
  327. shake256_init(&ctx);
  328. shake_update(&ctx, in, in_len);
  329. shake_squeeze(&ctx, out, out_len);
  330. shake_zeroize_ctx(&ctx);
  331. }
  332. EXPORT_SYMBOL_GPL(shake256);
  333. #if defined(sha3_mod_init_arch) || defined(CONFIG_CRYPTO_FIPS)
  334. static int __init sha3_mod_init(void)
  335. {
  336. #ifdef sha3_mod_init_arch
  337. sha3_mod_init_arch();
  338. #endif
  339. if (fips_enabled) {
  340. /*
  341. * FIPS cryptographic algorithm self-test. As per the FIPS
  342. * Implementation Guidance, testing any SHA-3 algorithm
  343. * satisfies the test requirement for all of them.
  344. */
  345. u8 hash[SHA3_256_DIGEST_SIZE];
  346. sha3_256(fips_test_data, sizeof(fips_test_data), hash);
  347. if (memcmp(fips_test_sha3_256_value, hash, sizeof(hash)) != 0)
  348. panic("sha3: FIPS self-test failed\n");
  349. }
  350. return 0;
  351. }
  352. subsys_initcall(sha3_mod_init);
  353. static void __exit sha3_mod_exit(void)
  354. {
  355. }
  356. module_exit(sha3_mod_exit);
  357. #endif
  358. MODULE_DESCRIPTION("SHA-3 library functions");
  359. MODULE_LICENSE("GPL");