sha1.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <andrew@mcdonald.org.uk>
  13. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  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/sha.h>
  21. #include <asm/byteorder.h>
  22. void powerpc_sha_transform(u32 *state, const u8 *src);
  23. static int powerpc_sha1_init(struct shash_desc *desc)
  24. {
  25. struct sha1_state *sctx = shash_desc_ctx(desc);
  26. *sctx = (struct sha1_state){
  27. .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
  28. };
  29. return 0;
  30. }
  31. static int powerpc_sha1_update(struct shash_desc *desc, const u8 *data,
  32. unsigned int len)
  33. {
  34. struct sha1_state *sctx = shash_desc_ctx(desc);
  35. unsigned int partial, done;
  36. const u8 *src;
  37. partial = sctx->count & 0x3f;
  38. sctx->count += len;
  39. done = 0;
  40. src = data;
  41. if ((partial + len) > 63) {
  42. if (partial) {
  43. done = -partial;
  44. memcpy(sctx->buffer + partial, data, done + 64);
  45. src = sctx->buffer;
  46. }
  47. do {
  48. powerpc_sha_transform(sctx->state, src);
  49. done += 64;
  50. src = data + done;
  51. } while (done + 63 < len);
  52. partial = 0;
  53. }
  54. memcpy(sctx->buffer + partial, src, len - done);
  55. return 0;
  56. }
  57. /* Add padding and return the message digest. */
  58. static int powerpc_sha1_final(struct shash_desc *desc, u8 *out)
  59. {
  60. struct sha1_state *sctx = shash_desc_ctx(desc);
  61. __be32 *dst = (__be32 *)out;
  62. u32 i, index, padlen;
  63. __be64 bits;
  64. static const u8 padding[64] = { 0x80, };
  65. bits = cpu_to_be64(sctx->count << 3);
  66. /* Pad out to 56 mod 64 */
  67. index = sctx->count & 0x3f;
  68. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  69. powerpc_sha1_update(desc, padding, padlen);
  70. /* Append length */
  71. powerpc_sha1_update(desc, (const u8 *)&bits, sizeof(bits));
  72. /* Store state in digest */
  73. for (i = 0; i < 5; i++)
  74. dst[i] = cpu_to_be32(sctx->state[i]);
  75. /* Wipe context */
  76. memset(sctx, 0, sizeof *sctx);
  77. return 0;
  78. }
  79. static int powerpc_sha1_export(struct shash_desc *desc, void *out)
  80. {
  81. struct sha1_state *sctx = shash_desc_ctx(desc);
  82. memcpy(out, sctx, sizeof(*sctx));
  83. return 0;
  84. }
  85. static int powerpc_sha1_import(struct shash_desc *desc, const void *in)
  86. {
  87. struct sha1_state *sctx = shash_desc_ctx(desc);
  88. memcpy(sctx, in, sizeof(*sctx));
  89. return 0;
  90. }
  91. static struct shash_alg alg = {
  92. .digestsize = SHA1_DIGEST_SIZE,
  93. .init = powerpc_sha1_init,
  94. .update = powerpc_sha1_update,
  95. .final = powerpc_sha1_final,
  96. .export = powerpc_sha1_export,
  97. .import = powerpc_sha1_import,
  98. .descsize = sizeof(struct sha1_state),
  99. .statesize = sizeof(struct sha1_state),
  100. .base = {
  101. .cra_name = "sha1",
  102. .cra_driver_name= "sha1-powerpc",
  103. .cra_blocksize = SHA1_BLOCK_SIZE,
  104. .cra_module = THIS_MODULE,
  105. }
  106. };
  107. static int __init sha1_powerpc_mod_init(void)
  108. {
  109. return crypto_register_shash(&alg);
  110. }
  111. static void __exit sha1_powerpc_mod_fini(void)
  112. {
  113. crypto_unregister_shash(&alg);
  114. }
  115. module_init(sha1_powerpc_mod_init);
  116. module_exit(sha1_powerpc_mod_fini);
  117. MODULE_LICENSE("GPL");
  118. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
  119. MODULE_ALIAS_CRYPTO("sha1");
  120. MODULE_ALIAS_CRYPTO("sha1-powerpc");