sha1-spe-glue.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Glue code for SHA-1 implementation for SPE instructions (PPC)
  4. *
  5. * Based on generic implementation.
  6. *
  7. * Copyright (c) 2015 Markus Stockhausen <[email protected]>
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/mm.h>
  13. #include <linux/types.h>
  14. #include <crypto/sha1.h>
  15. #include <crypto/sha1_base.h>
  16. #include <asm/byteorder.h>
  17. #include <asm/switch_to.h>
  18. #include <linux/hardirq.h>
  19. /*
  20. * MAX_BYTES defines the number of bytes that are allowed to be processed
  21. * between preempt_disable() and preempt_enable(). SHA1 takes ~1000
  22. * operations per 64 bytes. e500 cores can issue two arithmetic instructions
  23. * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).
  24. * Thus 2KB of input data will need an estimated maximum of 18,000 cycles.
  25. * Headroom for cache misses included. Even with the low end model clocked
  26. * at 667 MHz this equals to a critical time window of less than 27us.
  27. *
  28. */
  29. #define MAX_BYTES 2048
  30. extern void ppc_spe_sha1_transform(u32 *state, const u8 *src, u32 blocks);
  31. static void spe_begin(void)
  32. {
  33. /* We just start SPE operations and will save SPE registers later. */
  34. preempt_disable();
  35. enable_kernel_spe();
  36. }
  37. static void spe_end(void)
  38. {
  39. disable_kernel_spe();
  40. /* reenable preemption */
  41. preempt_enable();
  42. }
  43. static inline void ppc_sha1_clear_context(struct sha1_state *sctx)
  44. {
  45. int count = sizeof(struct sha1_state) >> 2;
  46. u32 *ptr = (u32 *)sctx;
  47. /* make sure we can clear the fast way */
  48. BUILD_BUG_ON(sizeof(struct sha1_state) % 4);
  49. do { *ptr++ = 0; } while (--count);
  50. }
  51. static int ppc_spe_sha1_update(struct shash_desc *desc, const u8 *data,
  52. unsigned int len)
  53. {
  54. struct sha1_state *sctx = shash_desc_ctx(desc);
  55. const unsigned int offset = sctx->count & 0x3f;
  56. const unsigned int avail = 64 - offset;
  57. unsigned int bytes;
  58. const u8 *src = data;
  59. if (avail > len) {
  60. sctx->count += len;
  61. memcpy((char *)sctx->buffer + offset, src, len);
  62. return 0;
  63. }
  64. sctx->count += len;
  65. if (offset) {
  66. memcpy((char *)sctx->buffer + offset, src, avail);
  67. spe_begin();
  68. ppc_spe_sha1_transform(sctx->state, (const u8 *)sctx->buffer, 1);
  69. spe_end();
  70. len -= avail;
  71. src += avail;
  72. }
  73. while (len > 63) {
  74. bytes = (len > MAX_BYTES) ? MAX_BYTES : len;
  75. bytes = bytes & ~0x3f;
  76. spe_begin();
  77. ppc_spe_sha1_transform(sctx->state, src, bytes >> 6);
  78. spe_end();
  79. src += bytes;
  80. len -= bytes;
  81. }
  82. memcpy((char *)sctx->buffer, src, len);
  83. return 0;
  84. }
  85. static int ppc_spe_sha1_final(struct shash_desc *desc, u8 *out)
  86. {
  87. struct sha1_state *sctx = shash_desc_ctx(desc);
  88. const unsigned int offset = sctx->count & 0x3f;
  89. char *p = (char *)sctx->buffer + offset;
  90. int padlen;
  91. __be64 *pbits = (__be64 *)(((char *)&sctx->buffer) + 56);
  92. __be32 *dst = (__be32 *)out;
  93. padlen = 55 - offset;
  94. *p++ = 0x80;
  95. spe_begin();
  96. if (padlen < 0) {
  97. memset(p, 0x00, padlen + sizeof (u64));
  98. ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
  99. p = (char *)sctx->buffer;
  100. padlen = 56;
  101. }
  102. memset(p, 0, padlen);
  103. *pbits = cpu_to_be64(sctx->count << 3);
  104. ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
  105. spe_end();
  106. dst[0] = cpu_to_be32(sctx->state[0]);
  107. dst[1] = cpu_to_be32(sctx->state[1]);
  108. dst[2] = cpu_to_be32(sctx->state[2]);
  109. dst[3] = cpu_to_be32(sctx->state[3]);
  110. dst[4] = cpu_to_be32(sctx->state[4]);
  111. ppc_sha1_clear_context(sctx);
  112. return 0;
  113. }
  114. static int ppc_spe_sha1_export(struct shash_desc *desc, void *out)
  115. {
  116. struct sha1_state *sctx = shash_desc_ctx(desc);
  117. memcpy(out, sctx, sizeof(*sctx));
  118. return 0;
  119. }
  120. static int ppc_spe_sha1_import(struct shash_desc *desc, const void *in)
  121. {
  122. struct sha1_state *sctx = shash_desc_ctx(desc);
  123. memcpy(sctx, in, sizeof(*sctx));
  124. return 0;
  125. }
  126. static struct shash_alg alg = {
  127. .digestsize = SHA1_DIGEST_SIZE,
  128. .init = sha1_base_init,
  129. .update = ppc_spe_sha1_update,
  130. .final = ppc_spe_sha1_final,
  131. .export = ppc_spe_sha1_export,
  132. .import = ppc_spe_sha1_import,
  133. .descsize = sizeof(struct sha1_state),
  134. .statesize = sizeof(struct sha1_state),
  135. .base = {
  136. .cra_name = "sha1",
  137. .cra_driver_name= "sha1-ppc-spe",
  138. .cra_priority = 300,
  139. .cra_blocksize = SHA1_BLOCK_SIZE,
  140. .cra_module = THIS_MODULE,
  141. }
  142. };
  143. static int __init ppc_spe_sha1_mod_init(void)
  144. {
  145. return crypto_register_shash(&alg);
  146. }
  147. static void __exit ppc_spe_sha1_mod_fini(void)
  148. {
  149. crypto_unregister_shash(&alg);
  150. }
  151. module_init(ppc_spe_sha1_mod_init);
  152. module_exit(ppc_spe_sha1_mod_fini);
  153. MODULE_LICENSE("GPL");
  154. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, SPE optimized");
  155. MODULE_ALIAS_CRYPTO("sha1");
  156. MODULE_ALIAS_CRYPTO("sha1-ppc-spe");