sha256-spe-glue.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Glue code for SHA-256 implementation for SPE instructions (PPC)
  4. *
  5. * Based on generic implementation. The assembler module takes care
  6. * about the SPE registers so it can run from interrupt context.
  7. *
  8. * Copyright (c) 2015 Markus Stockhausen <[email protected]>
  9. */
  10. #include <crypto/internal/hash.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/types.h>
  15. #include <crypto/sha2.h>
  16. #include <crypto/sha256_base.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/switch_to.h>
  19. #include <linux/hardirq.h>
  20. /*
  21. * MAX_BYTES defines the number of bytes that are allowed to be processed
  22. * between preempt_disable() and preempt_enable(). SHA256 takes ~2,000
  23. * operations per 64 bytes. e500 cores can issue two arithmetic instructions
  24. * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).
  25. * Thus 1KB of input data will need an estimated maximum of 18,000 cycles.
  26. * Headroom for cache misses included. Even with the low end model clocked
  27. * at 667 MHz this equals to a critical time window of less than 27us.
  28. *
  29. */
  30. #define MAX_BYTES 1024
  31. extern void ppc_spe_sha256_transform(u32 *state, const u8 *src, u32 blocks);
  32. static void spe_begin(void)
  33. {
  34. /* We just start SPE operations and will save SPE registers later. */
  35. preempt_disable();
  36. enable_kernel_spe();
  37. }
  38. static void spe_end(void)
  39. {
  40. disable_kernel_spe();
  41. /* reenable preemption */
  42. preempt_enable();
  43. }
  44. static inline void ppc_sha256_clear_context(struct sha256_state *sctx)
  45. {
  46. int count = sizeof(struct sha256_state) >> 2;
  47. u32 *ptr = (u32 *)sctx;
  48. /* make sure we can clear the fast way */
  49. BUILD_BUG_ON(sizeof(struct sha256_state) % 4);
  50. do { *ptr++ = 0; } while (--count);
  51. }
  52. static int ppc_spe_sha256_update(struct shash_desc *desc, const u8 *data,
  53. unsigned int len)
  54. {
  55. struct sha256_state *sctx = shash_desc_ctx(desc);
  56. const unsigned int offset = sctx->count & 0x3f;
  57. const unsigned int avail = 64 - offset;
  58. unsigned int bytes;
  59. const u8 *src = data;
  60. if (avail > len) {
  61. sctx->count += len;
  62. memcpy((char *)sctx->buf + offset, src, len);
  63. return 0;
  64. }
  65. sctx->count += len;
  66. if (offset) {
  67. memcpy((char *)sctx->buf + offset, src, avail);
  68. spe_begin();
  69. ppc_spe_sha256_transform(sctx->state, (const u8 *)sctx->buf, 1);
  70. spe_end();
  71. len -= avail;
  72. src += avail;
  73. }
  74. while (len > 63) {
  75. /* cut input data into smaller blocks */
  76. bytes = (len > MAX_BYTES) ? MAX_BYTES : len;
  77. bytes = bytes & ~0x3f;
  78. spe_begin();
  79. ppc_spe_sha256_transform(sctx->state, src, bytes >> 6);
  80. spe_end();
  81. src += bytes;
  82. len -= bytes;
  83. }
  84. memcpy((char *)sctx->buf, src, len);
  85. return 0;
  86. }
  87. static int ppc_spe_sha256_final(struct shash_desc *desc, u8 *out)
  88. {
  89. struct sha256_state *sctx = shash_desc_ctx(desc);
  90. const unsigned int offset = sctx->count & 0x3f;
  91. char *p = (char *)sctx->buf + offset;
  92. int padlen;
  93. __be64 *pbits = (__be64 *)(((char *)&sctx->buf) + 56);
  94. __be32 *dst = (__be32 *)out;
  95. padlen = 55 - offset;
  96. *p++ = 0x80;
  97. spe_begin();
  98. if (padlen < 0) {
  99. memset(p, 0x00, padlen + sizeof (u64));
  100. ppc_spe_sha256_transform(sctx->state, sctx->buf, 1);
  101. p = (char *)sctx->buf;
  102. padlen = 56;
  103. }
  104. memset(p, 0, padlen);
  105. *pbits = cpu_to_be64(sctx->count << 3);
  106. ppc_spe_sha256_transform(sctx->state, sctx->buf, 1);
  107. spe_end();
  108. dst[0] = cpu_to_be32(sctx->state[0]);
  109. dst[1] = cpu_to_be32(sctx->state[1]);
  110. dst[2] = cpu_to_be32(sctx->state[2]);
  111. dst[3] = cpu_to_be32(sctx->state[3]);
  112. dst[4] = cpu_to_be32(sctx->state[4]);
  113. dst[5] = cpu_to_be32(sctx->state[5]);
  114. dst[6] = cpu_to_be32(sctx->state[6]);
  115. dst[7] = cpu_to_be32(sctx->state[7]);
  116. ppc_sha256_clear_context(sctx);
  117. return 0;
  118. }
  119. static int ppc_spe_sha224_final(struct shash_desc *desc, u8 *out)
  120. {
  121. __be32 D[SHA256_DIGEST_SIZE >> 2];
  122. __be32 *dst = (__be32 *)out;
  123. ppc_spe_sha256_final(desc, (u8 *)D);
  124. /* avoid bytewise memcpy */
  125. dst[0] = D[0];
  126. dst[1] = D[1];
  127. dst[2] = D[2];
  128. dst[3] = D[3];
  129. dst[4] = D[4];
  130. dst[5] = D[5];
  131. dst[6] = D[6];
  132. /* clear sensitive data */
  133. memzero_explicit(D, SHA256_DIGEST_SIZE);
  134. return 0;
  135. }
  136. static int ppc_spe_sha256_export(struct shash_desc *desc, void *out)
  137. {
  138. struct sha256_state *sctx = shash_desc_ctx(desc);
  139. memcpy(out, sctx, sizeof(*sctx));
  140. return 0;
  141. }
  142. static int ppc_spe_sha256_import(struct shash_desc *desc, const void *in)
  143. {
  144. struct sha256_state *sctx = shash_desc_ctx(desc);
  145. memcpy(sctx, in, sizeof(*sctx));
  146. return 0;
  147. }
  148. static struct shash_alg algs[2] = { {
  149. .digestsize = SHA256_DIGEST_SIZE,
  150. .init = sha256_base_init,
  151. .update = ppc_spe_sha256_update,
  152. .final = ppc_spe_sha256_final,
  153. .export = ppc_spe_sha256_export,
  154. .import = ppc_spe_sha256_import,
  155. .descsize = sizeof(struct sha256_state),
  156. .statesize = sizeof(struct sha256_state),
  157. .base = {
  158. .cra_name = "sha256",
  159. .cra_driver_name= "sha256-ppc-spe",
  160. .cra_priority = 300,
  161. .cra_blocksize = SHA256_BLOCK_SIZE,
  162. .cra_module = THIS_MODULE,
  163. }
  164. }, {
  165. .digestsize = SHA224_DIGEST_SIZE,
  166. .init = sha224_base_init,
  167. .update = ppc_spe_sha256_update,
  168. .final = ppc_spe_sha224_final,
  169. .export = ppc_spe_sha256_export,
  170. .import = ppc_spe_sha256_import,
  171. .descsize = sizeof(struct sha256_state),
  172. .statesize = sizeof(struct sha256_state),
  173. .base = {
  174. .cra_name = "sha224",
  175. .cra_driver_name= "sha224-ppc-spe",
  176. .cra_priority = 300,
  177. .cra_blocksize = SHA224_BLOCK_SIZE,
  178. .cra_module = THIS_MODULE,
  179. }
  180. } };
  181. static int __init ppc_spe_sha256_mod_init(void)
  182. {
  183. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  184. }
  185. static void __exit ppc_spe_sha256_mod_fini(void)
  186. {
  187. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  188. }
  189. module_init(ppc_spe_sha256_mod_init);
  190. module_exit(ppc_spe_sha256_mod_fini);
  191. MODULE_LICENSE("GPL");
  192. MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm, SPE optimized");
  193. MODULE_ALIAS_CRYPTO("sha224");
  194. MODULE_ALIAS_CRYPTO("sha224-ppc-spe");
  195. MODULE_ALIAS_CRYPTO("sha256");
  196. MODULE_ALIAS_CRYPTO("sha256-ppc-spe");