sha1.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto API support for SHA-1 and HMAC-SHA1
  4. *
  5. * Copyright (c) Alan Smithee.
  6. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  7. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  8. * Copyright 2025 Google LLC
  9. */
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/sha1.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. /*
  15. * Export and import functions. crypto_shash wants a particular format that
  16. * matches that used by some legacy drivers. It currently is the same as the
  17. * library SHA context, except the value in bytecount must be block-aligned and
  18. * the remainder must be stored in an extra u8 appended to the struct.
  19. */
  20. #define SHA1_SHASH_STATE_SIZE (sizeof(struct sha1_ctx) + 1)
  21. static_assert(sizeof(struct sha1_ctx) == sizeof(struct sha1_state));
  22. static_assert(offsetof(struct sha1_ctx, state) == offsetof(struct sha1_state, state));
  23. static_assert(offsetof(struct sha1_ctx, bytecount) == offsetof(struct sha1_state, count));
  24. static_assert(offsetof(struct sha1_ctx, buf) == offsetof(struct sha1_state, buffer));
  25. static int __crypto_sha1_export(const struct sha1_ctx *ctx0, void *out)
  26. {
  27. struct sha1_ctx ctx = *ctx0;
  28. unsigned int partial;
  29. u8 *p = out;
  30. partial = ctx.bytecount % SHA1_BLOCK_SIZE;
  31. ctx.bytecount -= partial;
  32. memcpy(p, &ctx, sizeof(ctx));
  33. p += sizeof(ctx);
  34. *p = partial;
  35. return 0;
  36. }
  37. static int __crypto_sha1_import(struct sha1_ctx *ctx, const void *in)
  38. {
  39. const u8 *p = in;
  40. memcpy(ctx, p, sizeof(*ctx));
  41. p += sizeof(*ctx);
  42. ctx->bytecount += *p;
  43. return 0;
  44. }
  45. static int __crypto_sha1_export_core(const struct sha1_ctx *ctx, void *out)
  46. {
  47. memcpy(out, ctx, offsetof(struct sha1_ctx, buf));
  48. return 0;
  49. }
  50. static int __crypto_sha1_import_core(struct sha1_ctx *ctx, const void *in)
  51. {
  52. memcpy(ctx, in, offsetof(struct sha1_ctx, buf));
  53. return 0;
  54. }
  55. const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
  56. 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
  57. 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
  58. 0xaf, 0xd8, 0x07, 0x09
  59. };
  60. EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
  61. #define SHA1_CTX(desc) ((struct sha1_ctx *)shash_desc_ctx(desc))
  62. static int crypto_sha1_init(struct shash_desc *desc)
  63. {
  64. sha1_init(SHA1_CTX(desc));
  65. return 0;
  66. }
  67. static int crypto_sha1_update(struct shash_desc *desc,
  68. const u8 *data, unsigned int len)
  69. {
  70. sha1_update(SHA1_CTX(desc), data, len);
  71. return 0;
  72. }
  73. static int crypto_sha1_final(struct shash_desc *desc, u8 *out)
  74. {
  75. sha1_final(SHA1_CTX(desc), out);
  76. return 0;
  77. }
  78. static int crypto_sha1_digest(struct shash_desc *desc,
  79. const u8 *data, unsigned int len, u8 *out)
  80. {
  81. sha1(data, len, out);
  82. return 0;
  83. }
  84. static int crypto_sha1_export(struct shash_desc *desc, void *out)
  85. {
  86. return __crypto_sha1_export(SHA1_CTX(desc), out);
  87. }
  88. static int crypto_sha1_import(struct shash_desc *desc, const void *in)
  89. {
  90. return __crypto_sha1_import(SHA1_CTX(desc), in);
  91. }
  92. static int crypto_sha1_export_core(struct shash_desc *desc, void *out)
  93. {
  94. return __crypto_sha1_export_core(SHA1_CTX(desc), out);
  95. }
  96. static int crypto_sha1_import_core(struct shash_desc *desc, const void *in)
  97. {
  98. return __crypto_sha1_import_core(SHA1_CTX(desc), in);
  99. }
  100. #define HMAC_SHA1_KEY(tfm) ((struct hmac_sha1_key *)crypto_shash_ctx(tfm))
  101. #define HMAC_SHA1_CTX(desc) ((struct hmac_sha1_ctx *)shash_desc_ctx(desc))
  102. static int crypto_hmac_sha1_setkey(struct crypto_shash *tfm,
  103. const u8 *raw_key, unsigned int keylen)
  104. {
  105. hmac_sha1_preparekey(HMAC_SHA1_KEY(tfm), raw_key, keylen);
  106. return 0;
  107. }
  108. static int crypto_hmac_sha1_init(struct shash_desc *desc)
  109. {
  110. hmac_sha1_init(HMAC_SHA1_CTX(desc), HMAC_SHA1_KEY(desc->tfm));
  111. return 0;
  112. }
  113. static int crypto_hmac_sha1_update(struct shash_desc *desc,
  114. const u8 *data, unsigned int len)
  115. {
  116. hmac_sha1_update(HMAC_SHA1_CTX(desc), data, len);
  117. return 0;
  118. }
  119. static int crypto_hmac_sha1_final(struct shash_desc *desc, u8 *out)
  120. {
  121. hmac_sha1_final(HMAC_SHA1_CTX(desc), out);
  122. return 0;
  123. }
  124. static int crypto_hmac_sha1_digest(struct shash_desc *desc,
  125. const u8 *data, unsigned int len, u8 *out)
  126. {
  127. hmac_sha1(HMAC_SHA1_KEY(desc->tfm), data, len, out);
  128. return 0;
  129. }
  130. static int crypto_hmac_sha1_export(struct shash_desc *desc, void *out)
  131. {
  132. return __crypto_sha1_export(&HMAC_SHA1_CTX(desc)->sha_ctx, out);
  133. }
  134. static int crypto_hmac_sha1_import(struct shash_desc *desc, const void *in)
  135. {
  136. struct hmac_sha1_ctx *ctx = HMAC_SHA1_CTX(desc);
  137. ctx->ostate = HMAC_SHA1_KEY(desc->tfm)->ostate;
  138. return __crypto_sha1_import(&ctx->sha_ctx, in);
  139. }
  140. static int crypto_hmac_sha1_export_core(struct shash_desc *desc, void *out)
  141. {
  142. return __crypto_sha1_export_core(&HMAC_SHA1_CTX(desc)->sha_ctx, out);
  143. }
  144. static int crypto_hmac_sha1_import_core(struct shash_desc *desc, const void *in)
  145. {
  146. struct hmac_sha1_ctx *ctx = HMAC_SHA1_CTX(desc);
  147. ctx->ostate = HMAC_SHA1_KEY(desc->tfm)->ostate;
  148. return __crypto_sha1_import_core(&ctx->sha_ctx, in);
  149. }
  150. static struct shash_alg algs[] = {
  151. {
  152. .base.cra_name = "sha1",
  153. .base.cra_driver_name = "sha1-lib",
  154. .base.cra_priority = 300,
  155. .base.cra_blocksize = SHA1_BLOCK_SIZE,
  156. .base.cra_module = THIS_MODULE,
  157. .digestsize = SHA1_DIGEST_SIZE,
  158. .init = crypto_sha1_init,
  159. .update = crypto_sha1_update,
  160. .final = crypto_sha1_final,
  161. .digest = crypto_sha1_digest,
  162. .export = crypto_sha1_export,
  163. .import = crypto_sha1_import,
  164. .export_core = crypto_sha1_export_core,
  165. .import_core = crypto_sha1_import_core,
  166. .descsize = sizeof(struct sha1_ctx),
  167. .statesize = SHA1_SHASH_STATE_SIZE,
  168. },
  169. {
  170. .base.cra_name = "hmac(sha1)",
  171. .base.cra_driver_name = "hmac-sha1-lib",
  172. .base.cra_priority = 300,
  173. .base.cra_blocksize = SHA1_BLOCK_SIZE,
  174. .base.cra_ctxsize = sizeof(struct hmac_sha1_key),
  175. .base.cra_module = THIS_MODULE,
  176. .digestsize = SHA1_DIGEST_SIZE,
  177. .setkey = crypto_hmac_sha1_setkey,
  178. .init = crypto_hmac_sha1_init,
  179. .update = crypto_hmac_sha1_update,
  180. .final = crypto_hmac_sha1_final,
  181. .digest = crypto_hmac_sha1_digest,
  182. .export = crypto_hmac_sha1_export,
  183. .import = crypto_hmac_sha1_import,
  184. .export_core = crypto_hmac_sha1_export_core,
  185. .import_core = crypto_hmac_sha1_import_core,
  186. .descsize = sizeof(struct hmac_sha1_ctx),
  187. .statesize = SHA1_SHASH_STATE_SIZE,
  188. },
  189. };
  190. static int __init crypto_sha1_mod_init(void)
  191. {
  192. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  193. }
  194. module_init(crypto_sha1_mod_init);
  195. static void __exit crypto_sha1_mod_exit(void)
  196. {
  197. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  198. }
  199. module_exit(crypto_sha1_mod_exit);
  200. MODULE_LICENSE("GPL");
  201. MODULE_DESCRIPTION("Crypto API support for SHA-1 and HMAC-SHA1");
  202. MODULE_ALIAS_CRYPTO("sha1");
  203. MODULE_ALIAS_CRYPTO("sha1-lib");
  204. MODULE_ALIAS_CRYPTO("hmac(sha1)");
  205. MODULE_ALIAS_CRYPTO("hmac-sha1-lib");