sha256-spe-glue.c 6.8 KB

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