sha512_glue.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Glue code for SHA512 hashing optimized for sparc64 crypto opcodes.
  3. *
  4. * This is based largely upon crypto/sha512_generic.c
  5. *
  6. * Copyright (c) Jean-Luc Cooke <[email protected]>
  7. * Copyright (c) Andrew McDonald <[email protected]>
  8. * Copyright (c) 2003 Kyle McMartin <[email protected]>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <crypto/internal/hash.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/types.h>
  16. #include <crypto/sha2.h>
  17. #include <crypto/sha512_base.h>
  18. #include <asm/pstate.h>
  19. #include <asm/elf.h>
  20. #include "opcodes.h"
  21. asmlinkage void sha512_sparc64_transform(u64 *digest, const char *data,
  22. unsigned int rounds);
  23. static void __sha512_sparc64_update(struct sha512_state *sctx, const u8 *data,
  24. unsigned int len, unsigned int partial)
  25. {
  26. unsigned int done = 0;
  27. if ((sctx->count[0] += len) < len)
  28. sctx->count[1]++;
  29. if (partial) {
  30. done = SHA512_BLOCK_SIZE - partial;
  31. memcpy(sctx->buf + partial, data, done);
  32. sha512_sparc64_transform(sctx->state, sctx->buf, 1);
  33. }
  34. if (len - done >= SHA512_BLOCK_SIZE) {
  35. const unsigned int rounds = (len - done) / SHA512_BLOCK_SIZE;
  36. sha512_sparc64_transform(sctx->state, data + done, rounds);
  37. done += rounds * SHA512_BLOCK_SIZE;
  38. }
  39. memcpy(sctx->buf, data + done, len - done);
  40. }
  41. static int sha512_sparc64_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct sha512_state *sctx = shash_desc_ctx(desc);
  45. unsigned int partial = sctx->count[0] % SHA512_BLOCK_SIZE;
  46. /* Handle the fast case right here */
  47. if (partial + len < SHA512_BLOCK_SIZE) {
  48. if ((sctx->count[0] += len) < len)
  49. sctx->count[1]++;
  50. memcpy(sctx->buf + partial, data, len);
  51. } else
  52. __sha512_sparc64_update(sctx, data, len, partial);
  53. return 0;
  54. }
  55. static int sha512_sparc64_final(struct shash_desc *desc, u8 *out)
  56. {
  57. struct sha512_state *sctx = shash_desc_ctx(desc);
  58. unsigned int i, index, padlen;
  59. __be64 *dst = (__be64 *)out;
  60. __be64 bits[2];
  61. static const u8 padding[SHA512_BLOCK_SIZE] = { 0x80, };
  62. /* Save number of bits */
  63. bits[1] = cpu_to_be64(sctx->count[0] << 3);
  64. bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61);
  65. /* Pad out to 112 mod 128 and append length */
  66. index = sctx->count[0] % SHA512_BLOCK_SIZE;
  67. padlen = (index < 112) ? (112 - index) : ((SHA512_BLOCK_SIZE+112) - index);
  68. /* We need to fill a whole block for __sha512_sparc64_update() */
  69. if (padlen <= 112) {
  70. if ((sctx->count[0] += padlen) < padlen)
  71. sctx->count[1]++;
  72. memcpy(sctx->buf + index, padding, padlen);
  73. } else {
  74. __sha512_sparc64_update(sctx, padding, padlen, index);
  75. }
  76. __sha512_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 112);
  77. /* Store state in digest */
  78. for (i = 0; i < 8; i++)
  79. dst[i] = cpu_to_be64(sctx->state[i]);
  80. /* Wipe context */
  81. memset(sctx, 0, sizeof(*sctx));
  82. return 0;
  83. }
  84. static int sha384_sparc64_final(struct shash_desc *desc, u8 *hash)
  85. {
  86. u8 D[64];
  87. sha512_sparc64_final(desc, D);
  88. memcpy(hash, D, 48);
  89. memzero_explicit(D, 64);
  90. return 0;
  91. }
  92. static struct shash_alg sha512 = {
  93. .digestsize = SHA512_DIGEST_SIZE,
  94. .init = sha512_base_init,
  95. .update = sha512_sparc64_update,
  96. .final = sha512_sparc64_final,
  97. .descsize = sizeof(struct sha512_state),
  98. .base = {
  99. .cra_name = "sha512",
  100. .cra_driver_name= "sha512-sparc64",
  101. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  102. .cra_blocksize = SHA512_BLOCK_SIZE,
  103. .cra_module = THIS_MODULE,
  104. }
  105. };
  106. static struct shash_alg sha384 = {
  107. .digestsize = SHA384_DIGEST_SIZE,
  108. .init = sha384_base_init,
  109. .update = sha512_sparc64_update,
  110. .final = sha384_sparc64_final,
  111. .descsize = sizeof(struct sha512_state),
  112. .base = {
  113. .cra_name = "sha384",
  114. .cra_driver_name= "sha384-sparc64",
  115. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  116. .cra_blocksize = SHA384_BLOCK_SIZE,
  117. .cra_module = THIS_MODULE,
  118. }
  119. };
  120. static bool __init sparc64_has_sha512_opcode(void)
  121. {
  122. unsigned long cfr;
  123. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  124. return false;
  125. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  126. if (!(cfr & CFR_SHA512))
  127. return false;
  128. return true;
  129. }
  130. static int __init sha512_sparc64_mod_init(void)
  131. {
  132. if (sparc64_has_sha512_opcode()) {
  133. int ret = crypto_register_shash(&sha384);
  134. if (ret < 0)
  135. return ret;
  136. ret = crypto_register_shash(&sha512);
  137. if (ret < 0) {
  138. crypto_unregister_shash(&sha384);
  139. return ret;
  140. }
  141. pr_info("Using sparc64 sha512 opcode optimized SHA-512/SHA-384 implementation\n");
  142. return 0;
  143. }
  144. pr_info("sparc64 sha512 opcode not available.\n");
  145. return -ENODEV;
  146. }
  147. static void __exit sha512_sparc64_mod_fini(void)
  148. {
  149. crypto_unregister_shash(&sha384);
  150. crypto_unregister_shash(&sha512);
  151. }
  152. module_init(sha512_sparc64_mod_init);
  153. module_exit(sha512_sparc64_mod_fini);
  154. MODULE_LICENSE("GPL");
  155. MODULE_DESCRIPTION("SHA-384 and SHA-512 Secure Hash Algorithm, sparc64 sha512 opcode accelerated");
  156. MODULE_ALIAS_CRYPTO("sha384");
  157. MODULE_ALIAS_CRYPTO("sha512");
  158. #include "crop_devid.c"