sha1-spe-glue.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. disable_kernel_spe();
  45. /* reenable preemption */
  46. preempt_enable();
  47. }
  48. static inline void ppc_sha1_clear_context(struct sha1_state *sctx)
  49. {
  50. int count = sizeof(struct sha1_state) >> 2;
  51. u32 *ptr = (u32 *)sctx;
  52. /* make sure we can clear the fast way */
  53. BUILD_BUG_ON(sizeof(struct sha1_state) % 4);
  54. do { *ptr++ = 0; } while (--count);
  55. }
  56. static int ppc_spe_sha1_init(struct shash_desc *desc)
  57. {
  58. struct sha1_state *sctx = shash_desc_ctx(desc);
  59. sctx->state[0] = SHA1_H0;
  60. sctx->state[1] = SHA1_H1;
  61. sctx->state[2] = SHA1_H2;
  62. sctx->state[3] = SHA1_H3;
  63. sctx->state[4] = SHA1_H4;
  64. sctx->count = 0;
  65. return 0;
  66. }
  67. static int ppc_spe_sha1_update(struct shash_desc *desc, const u8 *data,
  68. unsigned int len)
  69. {
  70. struct sha1_state *sctx = shash_desc_ctx(desc);
  71. const unsigned int offset = sctx->count & 0x3f;
  72. const unsigned int avail = 64 - offset;
  73. unsigned int bytes;
  74. const u8 *src = data;
  75. if (avail > len) {
  76. sctx->count += len;
  77. memcpy((char *)sctx->buffer + offset, src, len);
  78. return 0;
  79. }
  80. sctx->count += len;
  81. if (offset) {
  82. memcpy((char *)sctx->buffer + offset, src, avail);
  83. spe_begin();
  84. ppc_spe_sha1_transform(sctx->state, (const u8 *)sctx->buffer, 1);
  85. spe_end();
  86. len -= avail;
  87. src += avail;
  88. }
  89. while (len > 63) {
  90. bytes = (len > MAX_BYTES) ? MAX_BYTES : len;
  91. bytes = bytes & ~0x3f;
  92. spe_begin();
  93. ppc_spe_sha1_transform(sctx->state, src, bytes >> 6);
  94. spe_end();
  95. src += bytes;
  96. len -= bytes;
  97. };
  98. memcpy((char *)sctx->buffer, src, len);
  99. return 0;
  100. }
  101. static int ppc_spe_sha1_final(struct shash_desc *desc, u8 *out)
  102. {
  103. struct sha1_state *sctx = shash_desc_ctx(desc);
  104. const unsigned int offset = sctx->count & 0x3f;
  105. char *p = (char *)sctx->buffer + offset;
  106. int padlen;
  107. __be64 *pbits = (__be64 *)(((char *)&sctx->buffer) + 56);
  108. __be32 *dst = (__be32 *)out;
  109. padlen = 55 - offset;
  110. *p++ = 0x80;
  111. spe_begin();
  112. if (padlen < 0) {
  113. memset(p, 0x00, padlen + sizeof (u64));
  114. ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
  115. p = (char *)sctx->buffer;
  116. padlen = 56;
  117. }
  118. memset(p, 0, padlen);
  119. *pbits = cpu_to_be64(sctx->count << 3);
  120. ppc_spe_sha1_transform(sctx->state, sctx->buffer, 1);
  121. spe_end();
  122. dst[0] = cpu_to_be32(sctx->state[0]);
  123. dst[1] = cpu_to_be32(sctx->state[1]);
  124. dst[2] = cpu_to_be32(sctx->state[2]);
  125. dst[3] = cpu_to_be32(sctx->state[3]);
  126. dst[4] = cpu_to_be32(sctx->state[4]);
  127. ppc_sha1_clear_context(sctx);
  128. return 0;
  129. }
  130. static int ppc_spe_sha1_export(struct shash_desc *desc, void *out)
  131. {
  132. struct sha1_state *sctx = shash_desc_ctx(desc);
  133. memcpy(out, sctx, sizeof(*sctx));
  134. return 0;
  135. }
  136. static int ppc_spe_sha1_import(struct shash_desc *desc, const void *in)
  137. {
  138. struct sha1_state *sctx = shash_desc_ctx(desc);
  139. memcpy(sctx, in, sizeof(*sctx));
  140. return 0;
  141. }
  142. static struct shash_alg alg = {
  143. .digestsize = SHA1_DIGEST_SIZE,
  144. .init = ppc_spe_sha1_init,
  145. .update = ppc_spe_sha1_update,
  146. .final = ppc_spe_sha1_final,
  147. .export = ppc_spe_sha1_export,
  148. .import = ppc_spe_sha1_import,
  149. .descsize = sizeof(struct sha1_state),
  150. .statesize = sizeof(struct sha1_state),
  151. .base = {
  152. .cra_name = "sha1",
  153. .cra_driver_name= "sha1-ppc-spe",
  154. .cra_priority = 300,
  155. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  156. .cra_blocksize = SHA1_BLOCK_SIZE,
  157. .cra_module = THIS_MODULE,
  158. }
  159. };
  160. static int __init ppc_spe_sha1_mod_init(void)
  161. {
  162. return crypto_register_shash(&alg);
  163. }
  164. static void __exit ppc_spe_sha1_mod_fini(void)
  165. {
  166. crypto_unregister_shash(&alg);
  167. }
  168. module_init(ppc_spe_sha1_mod_init);
  169. module_exit(ppc_spe_sha1_mod_fini);
  170. MODULE_LICENSE("GPL");
  171. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, SPE optimized");
  172. MODULE_ALIAS_CRYPTO("sha1");
  173. MODULE_ALIAS_CRYPTO("sha1-ppc-spe");