sha1.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API.
  4. *
  5. * powerpc implementation of the SHA1 Secure Hash Algorithm.
  6. *
  7. * Derived from cryptoapi implementation, adapted for in-place
  8. * scatterlist interface.
  9. *
  10. * Derived from "crypto/sha1.c"
  11. * Copyright (c) Alan Smithee.
  12. * Copyright (c) Andrew McDonald <[email protected]>
  13. * Copyright (c) Jean-Francois Dive <[email protected]>
  14. */
  15. #include <crypto/internal/hash.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/mm.h>
  19. #include <linux/types.h>
  20. #include <crypto/sha1.h>
  21. #include <crypto/sha1_base.h>
  22. #include <asm/byteorder.h>
  23. void powerpc_sha_transform(u32 *state, const u8 *src);
  24. static int powerpc_sha1_update(struct shash_desc *desc, const u8 *data,
  25. unsigned int len)
  26. {
  27. struct sha1_state *sctx = shash_desc_ctx(desc);
  28. unsigned int partial, done;
  29. const u8 *src;
  30. partial = sctx->count & 0x3f;
  31. sctx->count += len;
  32. done = 0;
  33. src = data;
  34. if ((partial + len) > 63) {
  35. if (partial) {
  36. done = -partial;
  37. memcpy(sctx->buffer + partial, data, done + 64);
  38. src = sctx->buffer;
  39. }
  40. do {
  41. powerpc_sha_transform(sctx->state, src);
  42. done += 64;
  43. src = data + done;
  44. } while (done + 63 < len);
  45. partial = 0;
  46. }
  47. memcpy(sctx->buffer + partial, src, len - done);
  48. return 0;
  49. }
  50. /* Add padding and return the message digest. */
  51. static int powerpc_sha1_final(struct shash_desc *desc, u8 *out)
  52. {
  53. struct sha1_state *sctx = shash_desc_ctx(desc);
  54. __be32 *dst = (__be32 *)out;
  55. u32 i, index, padlen;
  56. __be64 bits;
  57. static const u8 padding[64] = { 0x80, };
  58. bits = cpu_to_be64(sctx->count << 3);
  59. /* Pad out to 56 mod 64 */
  60. index = sctx->count & 0x3f;
  61. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  62. powerpc_sha1_update(desc, padding, padlen);
  63. /* Append length */
  64. powerpc_sha1_update(desc, (const u8 *)&bits, sizeof(bits));
  65. /* Store state in digest */
  66. for (i = 0; i < 5; i++)
  67. dst[i] = cpu_to_be32(sctx->state[i]);
  68. /* Wipe context */
  69. memset(sctx, 0, sizeof *sctx);
  70. return 0;
  71. }
  72. static int powerpc_sha1_export(struct shash_desc *desc, void *out)
  73. {
  74. struct sha1_state *sctx = shash_desc_ctx(desc);
  75. memcpy(out, sctx, sizeof(*sctx));
  76. return 0;
  77. }
  78. static int powerpc_sha1_import(struct shash_desc *desc, const void *in)
  79. {
  80. struct sha1_state *sctx = shash_desc_ctx(desc);
  81. memcpy(sctx, in, sizeof(*sctx));
  82. return 0;
  83. }
  84. static struct shash_alg alg = {
  85. .digestsize = SHA1_DIGEST_SIZE,
  86. .init = sha1_base_init,
  87. .update = powerpc_sha1_update,
  88. .final = powerpc_sha1_final,
  89. .export = powerpc_sha1_export,
  90. .import = powerpc_sha1_import,
  91. .descsize = sizeof(struct sha1_state),
  92. .statesize = sizeof(struct sha1_state),
  93. .base = {
  94. .cra_name = "sha1",
  95. .cra_driver_name= "sha1-powerpc",
  96. .cra_blocksize = SHA1_BLOCK_SIZE,
  97. .cra_module = THIS_MODULE,
  98. }
  99. };
  100. static int __init sha1_powerpc_mod_init(void)
  101. {
  102. return crypto_register_shash(&alg);
  103. }
  104. static void __exit sha1_powerpc_mod_fini(void)
  105. {
  106. crypto_unregister_shash(&alg);
  107. }
  108. module_init(sha1_powerpc_mod_init);
  109. module_exit(sha1_powerpc_mod_fini);
  110. MODULE_LICENSE("GPL");
  111. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
  112. MODULE_ALIAS_CRYPTO("sha1");
  113. MODULE_ALIAS_CRYPTO("sha1-powerpc");