ghash.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * GHASH routines supporting VMX instructions on the Power 8
  3. *
  4. * Copyright (C) 2015 International Business Machines Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 only.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
  20. */
  21. #include <linux/types.h>
  22. #include <linux/err.h>
  23. #include <linux/crypto.h>
  24. #include <linux/delay.h>
  25. #include <linux/hardirq.h>
  26. #include <asm/switch_to.h>
  27. #include <crypto/aes.h>
  28. #include <crypto/ghash.h>
  29. #include <crypto/scatterwalk.h>
  30. #include <crypto/internal/hash.h>
  31. #include <crypto/b128ops.h>
  32. #define IN_INTERRUPT in_interrupt()
  33. void gcm_init_p8(u128 htable[16], const u64 Xi[2]);
  34. void gcm_gmult_p8(u64 Xi[2], const u128 htable[16]);
  35. void gcm_ghash_p8(u64 Xi[2], const u128 htable[16],
  36. const u8 *in, size_t len);
  37. struct p8_ghash_ctx {
  38. u128 htable[16];
  39. struct crypto_shash *fallback;
  40. };
  41. struct p8_ghash_desc_ctx {
  42. u64 shash[2];
  43. u8 buffer[GHASH_DIGEST_SIZE];
  44. int bytes;
  45. struct shash_desc fallback_desc;
  46. };
  47. static int p8_ghash_init_tfm(struct crypto_tfm *tfm)
  48. {
  49. const char *alg = "ghash-generic";
  50. struct crypto_shash *fallback;
  51. struct crypto_shash *shash_tfm = __crypto_shash_cast(tfm);
  52. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm);
  53. fallback = crypto_alloc_shash(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
  54. if (IS_ERR(fallback)) {
  55. printk(KERN_ERR
  56. "Failed to allocate transformation for '%s': %ld\n",
  57. alg, PTR_ERR(fallback));
  58. return PTR_ERR(fallback);
  59. }
  60. printk(KERN_INFO "Using '%s' as fallback implementation.\n",
  61. crypto_tfm_alg_driver_name(crypto_shash_tfm(fallback)));
  62. crypto_shash_set_flags(fallback,
  63. crypto_shash_get_flags((struct crypto_shash
  64. *) tfm));
  65. /* Check if the descsize defined in the algorithm is still enough. */
  66. if (shash_tfm->descsize < sizeof(struct p8_ghash_desc_ctx)
  67. + crypto_shash_descsize(fallback)) {
  68. printk(KERN_ERR
  69. "Desc size of the fallback implementation (%s) does not match the expected value: %lu vs %u\n",
  70. alg,
  71. shash_tfm->descsize - sizeof(struct p8_ghash_desc_ctx),
  72. crypto_shash_descsize(fallback));
  73. return -EINVAL;
  74. }
  75. ctx->fallback = fallback;
  76. return 0;
  77. }
  78. static void p8_ghash_exit_tfm(struct crypto_tfm *tfm)
  79. {
  80. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm);
  81. if (ctx->fallback) {
  82. crypto_free_shash(ctx->fallback);
  83. ctx->fallback = NULL;
  84. }
  85. }
  86. static int p8_ghash_init(struct shash_desc *desc)
  87. {
  88. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
  89. struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  90. dctx->bytes = 0;
  91. memset(dctx->shash, 0, GHASH_DIGEST_SIZE);
  92. dctx->fallback_desc.tfm = ctx->fallback;
  93. dctx->fallback_desc.flags = desc->flags;
  94. return crypto_shash_init(&dctx->fallback_desc);
  95. }
  96. static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key,
  97. unsigned int keylen)
  98. {
  99. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(tfm));
  100. if (keylen != GHASH_BLOCK_SIZE)
  101. return -EINVAL;
  102. preempt_disable();
  103. pagefault_disable();
  104. enable_kernel_vsx();
  105. gcm_init_p8(ctx->htable, (const u64 *) key);
  106. disable_kernel_vsx();
  107. pagefault_enable();
  108. preempt_enable();
  109. return crypto_shash_setkey(ctx->fallback, key, keylen);
  110. }
  111. static int p8_ghash_update(struct shash_desc *desc,
  112. const u8 *src, unsigned int srclen)
  113. {
  114. unsigned int len;
  115. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
  116. struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  117. if (IN_INTERRUPT) {
  118. return crypto_shash_update(&dctx->fallback_desc, src,
  119. srclen);
  120. } else {
  121. if (dctx->bytes) {
  122. if (dctx->bytes + srclen < GHASH_DIGEST_SIZE) {
  123. memcpy(dctx->buffer + dctx->bytes, src,
  124. srclen);
  125. dctx->bytes += srclen;
  126. return 0;
  127. }
  128. memcpy(dctx->buffer + dctx->bytes, src,
  129. GHASH_DIGEST_SIZE - dctx->bytes);
  130. preempt_disable();
  131. pagefault_disable();
  132. enable_kernel_vsx();
  133. gcm_ghash_p8(dctx->shash, ctx->htable,
  134. dctx->buffer, GHASH_DIGEST_SIZE);
  135. disable_kernel_vsx();
  136. pagefault_enable();
  137. preempt_enable();
  138. src += GHASH_DIGEST_SIZE - dctx->bytes;
  139. srclen -= GHASH_DIGEST_SIZE - dctx->bytes;
  140. dctx->bytes = 0;
  141. }
  142. len = srclen & ~(GHASH_DIGEST_SIZE - 1);
  143. if (len) {
  144. preempt_disable();
  145. pagefault_disable();
  146. enable_kernel_vsx();
  147. gcm_ghash_p8(dctx->shash, ctx->htable, src, len);
  148. disable_kernel_vsx();
  149. pagefault_enable();
  150. preempt_enable();
  151. src += len;
  152. srclen -= len;
  153. }
  154. if (srclen) {
  155. memcpy(dctx->buffer, src, srclen);
  156. dctx->bytes = srclen;
  157. }
  158. return 0;
  159. }
  160. }
  161. static int p8_ghash_final(struct shash_desc *desc, u8 *out)
  162. {
  163. int i;
  164. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
  165. struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  166. if (IN_INTERRUPT) {
  167. return crypto_shash_final(&dctx->fallback_desc, out);
  168. } else {
  169. if (dctx->bytes) {
  170. for (i = dctx->bytes; i < GHASH_DIGEST_SIZE; i++)
  171. dctx->buffer[i] = 0;
  172. preempt_disable();
  173. pagefault_disable();
  174. enable_kernel_vsx();
  175. gcm_ghash_p8(dctx->shash, ctx->htable,
  176. dctx->buffer, GHASH_DIGEST_SIZE);
  177. disable_kernel_vsx();
  178. pagefault_enable();
  179. preempt_enable();
  180. dctx->bytes = 0;
  181. }
  182. memcpy(out, dctx->shash, GHASH_DIGEST_SIZE);
  183. return 0;
  184. }
  185. }
  186. struct shash_alg p8_ghash_alg = {
  187. .digestsize = GHASH_DIGEST_SIZE,
  188. .init = p8_ghash_init,
  189. .update = p8_ghash_update,
  190. .final = p8_ghash_final,
  191. .setkey = p8_ghash_setkey,
  192. .descsize = sizeof(struct p8_ghash_desc_ctx)
  193. + sizeof(struct ghash_desc_ctx),
  194. .base = {
  195. .cra_name = "ghash",
  196. .cra_driver_name = "p8_ghash",
  197. .cra_priority = 1000,
  198. .cra_flags = CRYPTO_ALG_TYPE_SHASH | CRYPTO_ALG_NEED_FALLBACK,
  199. .cra_blocksize = GHASH_BLOCK_SIZE,
  200. .cra_ctxsize = sizeof(struct p8_ghash_ctx),
  201. .cra_module = THIS_MODULE,
  202. .cra_init = p8_ghash_init_tfm,
  203. .cra_exit = p8_ghash_exit_tfm,
  204. },
  205. };