sha1.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SHA-1 and HMAC-SHA1 library functions
  4. */
  5. #include <crypto/hmac.h>
  6. #include <crypto/sha1.h>
  7. #include <linux/bitops.h>
  8. #include <linux/export.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <linux/unaligned.h>
  13. #include <linux/wordpart.h>
  14. #include "fips.h"
  15. static const struct sha1_block_state sha1_iv = {
  16. .h = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
  17. };
  18. /*
  19. * If you have 32 registers or more, the compiler can (and should)
  20. * try to change the array[] accesses into registers. However, on
  21. * machines with less than ~25 registers, that won't really work,
  22. * and at least gcc will make an unholy mess of it.
  23. *
  24. * So to avoid that mess which just slows things down, we force
  25. * the stores to memory to actually happen (we might be better off
  26. * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
  27. * suggested by Artur Skawina - that will also make gcc unable to
  28. * try to do the silly "optimize away loads" part because it won't
  29. * see what the value will be).
  30. *
  31. * Ben Herrenschmidt reports that on PPC, the C version comes close
  32. * to the optimized asm with this (ie on PPC you don't want that
  33. * 'volatile', since there are lots of registers).
  34. *
  35. * On ARM we get the best code generation by forcing a full memory barrier
  36. * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
  37. * the stack frame size simply explode and performance goes down the drain.
  38. */
  39. #ifdef CONFIG_X86
  40. #define setW(x, val) (*(volatile __u32 *)&W(x) = (val))
  41. #elif defined(CONFIG_ARM)
  42. #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
  43. #else
  44. #define setW(x, val) (W(x) = (val))
  45. #endif
  46. /* This "rolls" over the 512-bit array */
  47. #define W(x) (workspace[(x)&15])
  48. /*
  49. * Where do we get the source from? The first 16 iterations get it from
  50. * the input data, the next mix it from the 512-bit array.
  51. */
  52. #define SHA_SRC(t) get_unaligned_be32((__u32 *)data + t)
  53. #define SHA_MIX(t) rol32(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
  54. #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
  55. __u32 TEMP = input(t); setW(t, TEMP); \
  56. E += TEMP + rol32(A,5) + (fn) + (constant); \
  57. B = ror32(B, 2); \
  58. TEMP = E; E = D; D = C; C = B; B = A; A = TEMP; } while (0)
  59. #define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
  60. #define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
  61. #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
  62. #define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
  63. #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
  64. #define SHA1_WORKSPACE_WORDS 16
  65. static void sha1_block_generic(struct sha1_block_state *state,
  66. const u8 data[SHA1_BLOCK_SIZE],
  67. u32 workspace[SHA1_WORKSPACE_WORDS])
  68. {
  69. __u32 A, B, C, D, E;
  70. unsigned int i = 0;
  71. A = state->h[0];
  72. B = state->h[1];
  73. C = state->h[2];
  74. D = state->h[3];
  75. E = state->h[4];
  76. /* Round 1 - iterations 0-16 take their input from 'data' */
  77. for (; i < 16; ++i)
  78. T_0_15(i, A, B, C, D, E);
  79. /* Round 1 - tail. Input from 512-bit mixing array */
  80. for (; i < 20; ++i)
  81. T_16_19(i, A, B, C, D, E);
  82. /* Round 2 */
  83. for (; i < 40; ++i)
  84. T_20_39(i, A, B, C, D, E);
  85. /* Round 3 */
  86. for (; i < 60; ++i)
  87. T_40_59(i, A, B, C, D, E);
  88. /* Round 4 */
  89. for (; i < 80; ++i)
  90. T_60_79(i, A, B, C, D, E);
  91. state->h[0] += A;
  92. state->h[1] += B;
  93. state->h[2] += C;
  94. state->h[3] += D;
  95. state->h[4] += E;
  96. }
  97. static void __maybe_unused sha1_blocks_generic(struct sha1_block_state *state,
  98. const u8 *data, size_t nblocks)
  99. {
  100. u32 workspace[SHA1_WORKSPACE_WORDS];
  101. do {
  102. sha1_block_generic(state, data, workspace);
  103. data += SHA1_BLOCK_SIZE;
  104. } while (--nblocks);
  105. memzero_explicit(workspace, sizeof(workspace));
  106. }
  107. #ifdef CONFIG_CRYPTO_LIB_SHA1_ARCH
  108. #include "sha1.h" /* $(SRCARCH)/sha1.h */
  109. #else
  110. #define sha1_blocks sha1_blocks_generic
  111. #endif
  112. void sha1_init(struct sha1_ctx *ctx)
  113. {
  114. ctx->state = sha1_iv;
  115. ctx->bytecount = 0;
  116. }
  117. EXPORT_SYMBOL_GPL(sha1_init);
  118. void sha1_update(struct sha1_ctx *ctx, const u8 *data, size_t len)
  119. {
  120. size_t partial = ctx->bytecount % SHA1_BLOCK_SIZE;
  121. ctx->bytecount += len;
  122. if (partial + len >= SHA1_BLOCK_SIZE) {
  123. size_t nblocks;
  124. if (partial) {
  125. size_t l = SHA1_BLOCK_SIZE - partial;
  126. memcpy(&ctx->buf[partial], data, l);
  127. data += l;
  128. len -= l;
  129. sha1_blocks(&ctx->state, ctx->buf, 1);
  130. }
  131. nblocks = len / SHA1_BLOCK_SIZE;
  132. len %= SHA1_BLOCK_SIZE;
  133. if (nblocks) {
  134. sha1_blocks(&ctx->state, data, nblocks);
  135. data += nblocks * SHA1_BLOCK_SIZE;
  136. }
  137. partial = 0;
  138. }
  139. if (len)
  140. memcpy(&ctx->buf[partial], data, len);
  141. }
  142. EXPORT_SYMBOL_GPL(sha1_update);
  143. static void __sha1_final(struct sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
  144. {
  145. u64 bitcount = ctx->bytecount << 3;
  146. size_t partial = ctx->bytecount % SHA1_BLOCK_SIZE;
  147. ctx->buf[partial++] = 0x80;
  148. if (partial > SHA1_BLOCK_SIZE - 8) {
  149. memset(&ctx->buf[partial], 0, SHA1_BLOCK_SIZE - partial);
  150. sha1_blocks(&ctx->state, ctx->buf, 1);
  151. partial = 0;
  152. }
  153. memset(&ctx->buf[partial], 0, SHA1_BLOCK_SIZE - 8 - partial);
  154. *(__be64 *)&ctx->buf[SHA1_BLOCK_SIZE - 8] = cpu_to_be64(bitcount);
  155. sha1_blocks(&ctx->state, ctx->buf, 1);
  156. for (size_t i = 0; i < SHA1_DIGEST_SIZE; i += 4)
  157. put_unaligned_be32(ctx->state.h[i / 4], out + i);
  158. }
  159. void sha1_final(struct sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
  160. {
  161. __sha1_final(ctx, out);
  162. memzero_explicit(ctx, sizeof(*ctx));
  163. }
  164. EXPORT_SYMBOL_GPL(sha1_final);
  165. void sha1(const u8 *data, size_t len, u8 out[SHA1_DIGEST_SIZE])
  166. {
  167. struct sha1_ctx ctx;
  168. sha1_init(&ctx);
  169. sha1_update(&ctx, data, len);
  170. sha1_final(&ctx, out);
  171. }
  172. EXPORT_SYMBOL_GPL(sha1);
  173. static void __hmac_sha1_preparekey(struct sha1_block_state *istate,
  174. struct sha1_block_state *ostate,
  175. const u8 *raw_key, size_t raw_key_len)
  176. {
  177. union {
  178. u8 b[SHA1_BLOCK_SIZE];
  179. unsigned long w[SHA1_BLOCK_SIZE / sizeof(unsigned long)];
  180. } derived_key = { 0 };
  181. if (unlikely(raw_key_len > SHA1_BLOCK_SIZE))
  182. sha1(raw_key, raw_key_len, derived_key.b);
  183. else
  184. memcpy(derived_key.b, raw_key, raw_key_len);
  185. for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
  186. derived_key.w[i] ^= REPEAT_BYTE(HMAC_IPAD_VALUE);
  187. *istate = sha1_iv;
  188. sha1_blocks(istate, derived_key.b, 1);
  189. for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
  190. derived_key.w[i] ^= REPEAT_BYTE(HMAC_OPAD_VALUE ^
  191. HMAC_IPAD_VALUE);
  192. *ostate = sha1_iv;
  193. sha1_blocks(ostate, derived_key.b, 1);
  194. memzero_explicit(&derived_key, sizeof(derived_key));
  195. }
  196. void hmac_sha1_preparekey(struct hmac_sha1_key *key,
  197. const u8 *raw_key, size_t raw_key_len)
  198. {
  199. __hmac_sha1_preparekey(&key->istate, &key->ostate,
  200. raw_key, raw_key_len);
  201. }
  202. EXPORT_SYMBOL_GPL(hmac_sha1_preparekey);
  203. void hmac_sha1_init(struct hmac_sha1_ctx *ctx, const struct hmac_sha1_key *key)
  204. {
  205. ctx->sha_ctx.state = key->istate;
  206. ctx->sha_ctx.bytecount = SHA1_BLOCK_SIZE;
  207. ctx->ostate = key->ostate;
  208. }
  209. EXPORT_SYMBOL_GPL(hmac_sha1_init);
  210. void hmac_sha1_init_usingrawkey(struct hmac_sha1_ctx *ctx,
  211. const u8 *raw_key, size_t raw_key_len)
  212. {
  213. __hmac_sha1_preparekey(&ctx->sha_ctx.state, &ctx->ostate,
  214. raw_key, raw_key_len);
  215. ctx->sha_ctx.bytecount = SHA1_BLOCK_SIZE;
  216. }
  217. EXPORT_SYMBOL_GPL(hmac_sha1_init_usingrawkey);
  218. void hmac_sha1_final(struct hmac_sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
  219. {
  220. /* Generate the padded input for the outer hash in ctx->sha_ctx.buf. */
  221. __sha1_final(&ctx->sha_ctx, ctx->sha_ctx.buf);
  222. memset(&ctx->sha_ctx.buf[SHA1_DIGEST_SIZE], 0,
  223. SHA1_BLOCK_SIZE - SHA1_DIGEST_SIZE);
  224. ctx->sha_ctx.buf[SHA1_DIGEST_SIZE] = 0x80;
  225. *(__be32 *)&ctx->sha_ctx.buf[SHA1_BLOCK_SIZE - 4] =
  226. cpu_to_be32(8 * (SHA1_BLOCK_SIZE + SHA1_DIGEST_SIZE));
  227. /* Compute the outer hash, which gives the HMAC value. */
  228. sha1_blocks(&ctx->ostate, ctx->sha_ctx.buf, 1);
  229. for (size_t i = 0; i < SHA1_DIGEST_SIZE; i += 4)
  230. put_unaligned_be32(ctx->ostate.h[i / 4], out + i);
  231. memzero_explicit(ctx, sizeof(*ctx));
  232. }
  233. EXPORT_SYMBOL_GPL(hmac_sha1_final);
  234. void hmac_sha1(const struct hmac_sha1_key *key,
  235. const u8 *data, size_t data_len, u8 out[SHA1_DIGEST_SIZE])
  236. {
  237. struct hmac_sha1_ctx ctx;
  238. hmac_sha1_init(&ctx, key);
  239. hmac_sha1_update(&ctx, data, data_len);
  240. hmac_sha1_final(&ctx, out);
  241. }
  242. EXPORT_SYMBOL_GPL(hmac_sha1);
  243. void hmac_sha1_usingrawkey(const u8 *raw_key, size_t raw_key_len,
  244. const u8 *data, size_t data_len,
  245. u8 out[SHA1_DIGEST_SIZE])
  246. {
  247. struct hmac_sha1_ctx ctx;
  248. hmac_sha1_init_usingrawkey(&ctx, raw_key, raw_key_len);
  249. hmac_sha1_update(&ctx, data, data_len);
  250. hmac_sha1_final(&ctx, out);
  251. }
  252. EXPORT_SYMBOL_GPL(hmac_sha1_usingrawkey);
  253. #if defined(sha1_mod_init_arch) || defined(CONFIG_CRYPTO_FIPS)
  254. static int __init sha1_mod_init(void)
  255. {
  256. #ifdef sha1_mod_init_arch
  257. sha1_mod_init_arch();
  258. #endif
  259. if (fips_enabled) {
  260. /*
  261. * FIPS cryptographic algorithm self-test. As per the FIPS
  262. * Implementation Guidance, testing HMAC-SHA1 satisfies the test
  263. * requirement for SHA-1 too.
  264. */
  265. u8 mac[SHA1_DIGEST_SIZE];
  266. hmac_sha1_usingrawkey(fips_test_key, sizeof(fips_test_key),
  267. fips_test_data, sizeof(fips_test_data),
  268. mac);
  269. if (memcmp(fips_test_hmac_sha1_value, mac, sizeof(mac)) != 0)
  270. panic("sha1: FIPS self-test failed\n");
  271. }
  272. return 0;
  273. }
  274. subsys_initcall(sha1_mod_init);
  275. static void __exit sha1_mod_exit(void)
  276. {
  277. }
  278. module_exit(sha1_mod_exit);
  279. #endif
  280. MODULE_DESCRIPTION("SHA-1 and HMAC-SHA1 library functions");
  281. MODULE_LICENSE("GPL");