ghash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. crypto_shash_set_flags(fallback,
  61. crypto_shash_get_flags((struct crypto_shash
  62. *) tfm));
  63. /* Check if the descsize defined in the algorithm is still enough. */
  64. if (shash_tfm->descsize < sizeof(struct p8_ghash_desc_ctx)
  65. + crypto_shash_descsize(fallback)) {
  66. printk(KERN_ERR
  67. "Desc size of the fallback implementation (%s) does not match the expected value: %lu vs %u\n",
  68. alg,
  69. shash_tfm->descsize - sizeof(struct p8_ghash_desc_ctx),
  70. crypto_shash_descsize(fallback));
  71. return -EINVAL;
  72. }
  73. ctx->fallback = fallback;
  74. return 0;
  75. }
  76. static void p8_ghash_exit_tfm(struct crypto_tfm *tfm)
  77. {
  78. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm);
  79. if (ctx->fallback) {
  80. crypto_free_shash(ctx->fallback);
  81. ctx->fallback = NULL;
  82. }
  83. }
  84. static int p8_ghash_init(struct shash_desc *desc)
  85. {
  86. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
  87. struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  88. dctx->bytes = 0;
  89. memset(dctx->shash, 0, GHASH_DIGEST_SIZE);
  90. dctx->fallback_desc.tfm = ctx->fallback;
  91. dctx->fallback_desc.flags = desc->flags;
  92. return crypto_shash_init(&dctx->fallback_desc);
  93. }
  94. static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key,
  95. unsigned int keylen)
  96. {
  97. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(tfm));
  98. if (keylen != GHASH_BLOCK_SIZE)
  99. return -EINVAL;
  100. preempt_disable();
  101. pagefault_disable();
  102. enable_kernel_vsx();
  103. gcm_init_p8(ctx->htable, (const u64 *) key);
  104. disable_kernel_vsx();
  105. pagefault_enable();
  106. preempt_enable();
  107. return crypto_shash_setkey(ctx->fallback, key, keylen);
  108. }
  109. static int p8_ghash_update(struct shash_desc *desc,
  110. const u8 *src, unsigned int srclen)
  111. {
  112. unsigned int len;
  113. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
  114. struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  115. if (IN_INTERRUPT) {
  116. return crypto_shash_update(&dctx->fallback_desc, src,
  117. srclen);
  118. } else {
  119. if (dctx->bytes) {
  120. if (dctx->bytes + srclen < GHASH_DIGEST_SIZE) {
  121. memcpy(dctx->buffer + dctx->bytes, src,
  122. srclen);
  123. dctx->bytes += srclen;
  124. return 0;
  125. }
  126. memcpy(dctx->buffer + dctx->bytes, src,
  127. GHASH_DIGEST_SIZE - dctx->bytes);
  128. preempt_disable();
  129. pagefault_disable();
  130. enable_kernel_vsx();
  131. gcm_ghash_p8(dctx->shash, ctx->htable,
  132. dctx->buffer, GHASH_DIGEST_SIZE);
  133. disable_kernel_vsx();
  134. pagefault_enable();
  135. preempt_enable();
  136. src += GHASH_DIGEST_SIZE - dctx->bytes;
  137. srclen -= GHASH_DIGEST_SIZE - dctx->bytes;
  138. dctx->bytes = 0;
  139. }
  140. len = srclen & ~(GHASH_DIGEST_SIZE - 1);
  141. if (len) {
  142. preempt_disable();
  143. pagefault_disable();
  144. enable_kernel_vsx();
  145. gcm_ghash_p8(dctx->shash, ctx->htable, src, len);
  146. disable_kernel_vsx();
  147. pagefault_enable();
  148. preempt_enable();
  149. src += len;
  150. srclen -= len;
  151. }
  152. if (srclen) {
  153. memcpy(dctx->buffer, src, srclen);
  154. dctx->bytes = srclen;
  155. }
  156. return 0;
  157. }
  158. }
  159. static int p8_ghash_final(struct shash_desc *desc, u8 *out)
  160. {
  161. int i;
  162. struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
  163. struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  164. if (IN_INTERRUPT) {
  165. return crypto_shash_final(&dctx->fallback_desc, out);
  166. } else {
  167. if (dctx->bytes) {
  168. for (i = dctx->bytes; i < GHASH_DIGEST_SIZE; i++)
  169. dctx->buffer[i] = 0;
  170. preempt_disable();
  171. pagefault_disable();
  172. enable_kernel_vsx();
  173. gcm_ghash_p8(dctx->shash, ctx->htable,
  174. dctx->buffer, GHASH_DIGEST_SIZE);
  175. disable_kernel_vsx();
  176. pagefault_enable();
  177. preempt_enable();
  178. dctx->bytes = 0;
  179. }
  180. memcpy(out, dctx->shash, GHASH_DIGEST_SIZE);
  181. return 0;
  182. }
  183. }
  184. struct shash_alg p8_ghash_alg = {
  185. .digestsize = GHASH_DIGEST_SIZE,
  186. .init = p8_ghash_init,
  187. .update = p8_ghash_update,
  188. .final = p8_ghash_final,
  189. .setkey = p8_ghash_setkey,
  190. .descsize = sizeof(struct p8_ghash_desc_ctx)
  191. + sizeof(struct ghash_desc_ctx),
  192. .base = {
  193. .cra_name = "ghash",
  194. .cra_driver_name = "p8_ghash",
  195. .cra_priority = 1000,
  196. .cra_flags = CRYPTO_ALG_TYPE_SHASH | CRYPTO_ALG_NEED_FALLBACK,
  197. .cra_blocksize = GHASH_BLOCK_SIZE,
  198. .cra_ctxsize = sizeof(struct p8_ghash_ctx),
  199. .cra_module = THIS_MODULE,
  200. .cra_init = p8_ghash_init_tfm,
  201. .cra_exit = p8_ghash_exit_tfm,
  202. },
  203. };