sha256_glue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Glue code for SHA256 hashing optimized for sparc64 crypto opcodes.
  3. *
  4. * This is based largely upon crypto/sha256_generic.c
  5. *
  6. * Copyright (c) Jean-Luc Cooke <[email protected]>
  7. * Copyright (c) Andrew McDonald <[email protected]>
  8. * Copyright (c) 2002 James Morris <[email protected]>
  9. * SHA224 Support Copyright 2007 Intel Corporation <[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/sha2.h>
  18. #include <crypto/sha256_base.h>
  19. #include <asm/pstate.h>
  20. #include <asm/elf.h>
  21. #include "opcodes.h"
  22. asmlinkage void sha256_sparc64_transform(u32 *digest, const char *data,
  23. unsigned int rounds);
  24. static void __sha256_sparc64_update(struct sha256_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 = SHA256_BLOCK_SIZE - partial;
  31. memcpy(sctx->buf + partial, data, done);
  32. sha256_sparc64_transform(sctx->state, sctx->buf, 1);
  33. }
  34. if (len - done >= SHA256_BLOCK_SIZE) {
  35. const unsigned int rounds = (len - done) / SHA256_BLOCK_SIZE;
  36. sha256_sparc64_transform(sctx->state, data + done, rounds);
  37. done += rounds * SHA256_BLOCK_SIZE;
  38. }
  39. memcpy(sctx->buf, data + done, len - done);
  40. }
  41. static int sha256_sparc64_update(struct shash_desc *desc, const u8 *data,
  42. unsigned int len)
  43. {
  44. struct sha256_state *sctx = shash_desc_ctx(desc);
  45. unsigned int partial = sctx->count % SHA256_BLOCK_SIZE;
  46. /* Handle the fast case right here */
  47. if (partial + len < SHA256_BLOCK_SIZE) {
  48. sctx->count += len;
  49. memcpy(sctx->buf + partial, data, len);
  50. } else
  51. __sha256_sparc64_update(sctx, data, len, partial);
  52. return 0;
  53. }
  54. static int sha256_sparc64_final(struct shash_desc *desc, u8 *out)
  55. {
  56. struct sha256_state *sctx = shash_desc_ctx(desc);
  57. unsigned int i, index, padlen;
  58. __be32 *dst = (__be32 *)out;
  59. __be64 bits;
  60. static const u8 padding[SHA256_BLOCK_SIZE] = { 0x80, };
  61. bits = cpu_to_be64(sctx->count << 3);
  62. /* Pad out to 56 mod 64 and append length */
  63. index = sctx->count % SHA256_BLOCK_SIZE;
  64. padlen = (index < 56) ? (56 - index) : ((SHA256_BLOCK_SIZE+56) - index);
  65. /* We need to fill a whole block for __sha256_sparc64_update() */
  66. if (padlen <= 56) {
  67. sctx->count += padlen;
  68. memcpy(sctx->buf + index, padding, padlen);
  69. } else {
  70. __sha256_sparc64_update(sctx, padding, padlen, index);
  71. }
  72. __sha256_sparc64_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
  73. /* Store state in digest */
  74. for (i = 0; i < 8; i++)
  75. dst[i] = cpu_to_be32(sctx->state[i]);
  76. /* Wipe context */
  77. memset(sctx, 0, sizeof(*sctx));
  78. return 0;
  79. }
  80. static int sha224_sparc64_final(struct shash_desc *desc, u8 *hash)
  81. {
  82. u8 D[SHA256_DIGEST_SIZE];
  83. sha256_sparc64_final(desc, D);
  84. memcpy(hash, D, SHA224_DIGEST_SIZE);
  85. memzero_explicit(D, SHA256_DIGEST_SIZE);
  86. return 0;
  87. }
  88. static int sha256_sparc64_export(struct shash_desc *desc, void *out)
  89. {
  90. struct sha256_state *sctx = shash_desc_ctx(desc);
  91. memcpy(out, sctx, sizeof(*sctx));
  92. return 0;
  93. }
  94. static int sha256_sparc64_import(struct shash_desc *desc, const void *in)
  95. {
  96. struct sha256_state *sctx = shash_desc_ctx(desc);
  97. memcpy(sctx, in, sizeof(*sctx));
  98. return 0;
  99. }
  100. static struct shash_alg sha256_alg = {
  101. .digestsize = SHA256_DIGEST_SIZE,
  102. .init = sha256_base_init,
  103. .update = sha256_sparc64_update,
  104. .final = sha256_sparc64_final,
  105. .export = sha256_sparc64_export,
  106. .import = sha256_sparc64_import,
  107. .descsize = sizeof(struct sha256_state),
  108. .statesize = sizeof(struct sha256_state),
  109. .base = {
  110. .cra_name = "sha256",
  111. .cra_driver_name= "sha256-sparc64",
  112. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  113. .cra_blocksize = SHA256_BLOCK_SIZE,
  114. .cra_module = THIS_MODULE,
  115. }
  116. };
  117. static struct shash_alg sha224_alg = {
  118. .digestsize = SHA224_DIGEST_SIZE,
  119. .init = sha224_base_init,
  120. .update = sha256_sparc64_update,
  121. .final = sha224_sparc64_final,
  122. .descsize = sizeof(struct sha256_state),
  123. .base = {
  124. .cra_name = "sha224",
  125. .cra_driver_name= "sha224-sparc64",
  126. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  127. .cra_blocksize = SHA224_BLOCK_SIZE,
  128. .cra_module = THIS_MODULE,
  129. }
  130. };
  131. static bool __init sparc64_has_sha256_opcode(void)
  132. {
  133. unsigned long cfr;
  134. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  135. return false;
  136. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  137. if (!(cfr & CFR_SHA256))
  138. return false;
  139. return true;
  140. }
  141. static int __init sha256_sparc64_mod_init(void)
  142. {
  143. if (sparc64_has_sha256_opcode()) {
  144. int ret = crypto_register_shash(&sha224_alg);
  145. if (ret < 0)
  146. return ret;
  147. ret = crypto_register_shash(&sha256_alg);
  148. if (ret < 0) {
  149. crypto_unregister_shash(&sha224_alg);
  150. return ret;
  151. }
  152. pr_info("Using sparc64 sha256 opcode optimized SHA-256/SHA-224 implementation\n");
  153. return 0;
  154. }
  155. pr_info("sparc64 sha256 opcode not available.\n");
  156. return -ENODEV;
  157. }
  158. static void __exit sha256_sparc64_mod_fini(void)
  159. {
  160. crypto_unregister_shash(&sha224_alg);
  161. crypto_unregister_shash(&sha256_alg);
  162. }
  163. module_init(sha256_sparc64_mod_init);
  164. module_exit(sha256_sparc64_mod_fini);
  165. MODULE_LICENSE("GPL");
  166. MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm, sparc64 sha256 opcode accelerated");
  167. MODULE_ALIAS_CRYPTO("sha224");
  168. MODULE_ALIAS_CRYPTO("sha256");
  169. #include "crop_devid.c"