sha1-spe-glue.c 5.0 KB

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