sha1_glue.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Glue code for SHA1 hashing optimized for sparc64 crypto opcodes.
  3. *
  4. * This is based largely upon arch/x86/crypto/sha1_ssse3_glue.c
  5. *
  6. * Copyright (c) Alan Smithee.
  7. * Copyright (c) Andrew McDonald <[email protected]>
  8. * Copyright (c) Jean-Francois Dive <[email protected]>
  9. * Copyright (c) Mathias Krause <[email protected]>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <crypto/internal/hash.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/mm.h>
  16. #include <linux/types.h>
  17. #include <crypto/sha1.h>
  18. #include <crypto/sha1_base.h>
  19. #include <asm/pstate.h>
  20. #include <asm/elf.h>
  21. #include "opcodes.h"
  22. asmlinkage void sha1_sparc64_transform(u32 *digest, const char *data,
  23. unsigned int rounds);
  24. static void __sha1_sparc64_update(struct sha1_state *sctx, const u8 *data,
  25. unsigned int len, unsigned int partial)
  26. {
  27. unsigned int done = 0;
  28. sctx->count += len;
  29. if (partial) {
  30. done = SHA1_BLOCK_SIZE - partial;
  31. memcpy(sctx->buffer + partial, data, done);
  32. sha1_sparc64_transform(sctx->state, sctx->buffer, 1);
  33. }
  34. if (len - done >= SHA1_BLOCK_SIZE) {
  35. const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
  36. sha1_sparc64_transform(sctx->state, data + done, rounds);
  37. done += rounds * SHA1_BLOCK_SIZE;
  38. }
  39. memcpy(sctx->buffer, data + done, len - done);
  40. }
  41. static int sha1_sparc64_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct sha1_state *sctx = shash_desc_ctx(desc);
  45. unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
  46. /* Handle the fast case right here */
  47. if (partial + len < SHA1_BLOCK_SIZE) {
  48. sctx->count += len;
  49. memcpy(sctx->buffer + partial, data, len);
  50. } else
  51. __sha1_sparc64_update(sctx, data, len, partial);
  52. return 0;
  53. }
  54. /* Add padding and return the message digest. */
  55. static int sha1_sparc64_final(struct shash_desc *desc, u8 *out)
  56. {
  57. struct sha1_state *sctx = shash_desc_ctx(desc);
  58. unsigned int i, index, padlen;
  59. __be32 *dst = (__be32 *)out;
  60. __be64 bits;
  61. static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
  62. bits = cpu_to_be64(sctx->count << 3);
  63. /* Pad out to 56 mod 64 and append length */
  64. index = sctx->count % SHA1_BLOCK_SIZE;
  65. padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
  66. /* We need to fill a whole block for __sha1_sparc64_update() */
  67. if (padlen <= 56) {
  68. sctx->count += padlen;
  69. memcpy(sctx->buffer + index, padding, padlen);
  70. } else {
  71. __sha1_sparc64_update(sctx, padding, padlen, index);
  72. }
  73. __sha1_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
  74. /* Store state in digest */
  75. for (i = 0; i < 5; i++)
  76. dst[i] = cpu_to_be32(sctx->state[i]);
  77. /* Wipe context */
  78. memset(sctx, 0, sizeof(*sctx));
  79. return 0;
  80. }
  81. static int sha1_sparc64_export(struct shash_desc *desc, void *out)
  82. {
  83. struct sha1_state *sctx = shash_desc_ctx(desc);
  84. memcpy(out, sctx, sizeof(*sctx));
  85. return 0;
  86. }
  87. static int sha1_sparc64_import(struct shash_desc *desc, const void *in)
  88. {
  89. struct sha1_state *sctx = shash_desc_ctx(desc);
  90. memcpy(sctx, in, sizeof(*sctx));
  91. return 0;
  92. }
  93. static struct shash_alg alg = {
  94. .digestsize = SHA1_DIGEST_SIZE,
  95. .init = sha1_base_init,
  96. .update = sha1_sparc64_update,
  97. .final = sha1_sparc64_final,
  98. .export = sha1_sparc64_export,
  99. .import = sha1_sparc64_import,
  100. .descsize = sizeof(struct sha1_state),
  101. .statesize = sizeof(struct sha1_state),
  102. .base = {
  103. .cra_name = "sha1",
  104. .cra_driver_name= "sha1-sparc64",
  105. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  106. .cra_blocksize = SHA1_BLOCK_SIZE,
  107. .cra_module = THIS_MODULE,
  108. }
  109. };
  110. static bool __init sparc64_has_sha1_opcode(void)
  111. {
  112. unsigned long cfr;
  113. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  114. return false;
  115. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  116. if (!(cfr & CFR_SHA1))
  117. return false;
  118. return true;
  119. }
  120. static int __init sha1_sparc64_mod_init(void)
  121. {
  122. if (sparc64_has_sha1_opcode()) {
  123. pr_info("Using sparc64 sha1 opcode optimized SHA-1 implementation\n");
  124. return crypto_register_shash(&alg);
  125. }
  126. pr_info("sparc64 sha1 opcode not available.\n");
  127. return -ENODEV;
  128. }
  129. static void __exit sha1_sparc64_mod_fini(void)
  130. {
  131. crypto_unregister_shash(&alg);
  132. }
  133. module_init(sha1_sparc64_mod_init);
  134. module_exit(sha1_sparc64_mod_fini);
  135. MODULE_LICENSE("GPL");
  136. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, sparc64 sha1 opcode accelerated");
  137. MODULE_ALIAS_CRYPTO("sha1");
  138. #include "crop_devid.c"