ghash-ce-glue.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Accelerated GHASH implementation with ARMv8 PMULL instructions.
  3. *
  4. * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <asm/unaligned.h>
  12. #include <crypto/internal/hash.h>
  13. #include <linux/cpufeature.h>
  14. #include <linux/crypto.h>
  15. #include <linux/module.h>
  16. MODULE_DESCRIPTION("GHASH secure hash using ARMv8 Crypto Extensions");
  17. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  18. MODULE_LICENSE("GPL v2");
  19. #define GHASH_BLOCK_SIZE 16
  20. #define GHASH_DIGEST_SIZE 16
  21. struct ghash_key {
  22. u64 a;
  23. u64 b;
  24. };
  25. struct ghash_desc_ctx {
  26. u64 digest[GHASH_DIGEST_SIZE/sizeof(u64)];
  27. u8 buf[GHASH_BLOCK_SIZE];
  28. u32 count;
  29. };
  30. asmlinkage void pmull_ghash_update(int blocks, u64 dg[], const char *src,
  31. struct ghash_key const *k, const char *head);
  32. static int ghash_init(struct shash_desc *desc)
  33. {
  34. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  35. *ctx = (struct ghash_desc_ctx){};
  36. return 0;
  37. }
  38. static int ghash_update(struct shash_desc *desc, const u8 *src,
  39. unsigned int len)
  40. {
  41. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  42. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  43. ctx->count += len;
  44. if ((partial + len) >= GHASH_BLOCK_SIZE) {
  45. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  46. int blocks;
  47. if (partial) {
  48. int p = GHASH_BLOCK_SIZE - partial;
  49. memcpy(ctx->buf + partial, src, p);
  50. src += p;
  51. len -= p;
  52. }
  53. blocks = len / GHASH_BLOCK_SIZE;
  54. len %= GHASH_BLOCK_SIZE;
  55. kernel_neon_begin_partial(8);
  56. pmull_ghash_update(blocks, ctx->digest, src, key,
  57. partial ? ctx->buf : NULL);
  58. kernel_neon_end();
  59. src += blocks * GHASH_BLOCK_SIZE;
  60. partial = 0;
  61. }
  62. if (len)
  63. memcpy(ctx->buf + partial, src, len);
  64. return 0;
  65. }
  66. static int ghash_final(struct shash_desc *desc, u8 *dst)
  67. {
  68. struct ghash_desc_ctx *ctx = shash_desc_ctx(desc);
  69. unsigned int partial = ctx->count % GHASH_BLOCK_SIZE;
  70. if (partial) {
  71. struct ghash_key *key = crypto_shash_ctx(desc->tfm);
  72. memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
  73. kernel_neon_begin_partial(8);
  74. pmull_ghash_update(1, ctx->digest, ctx->buf, key, NULL);
  75. kernel_neon_end();
  76. }
  77. put_unaligned_be64(ctx->digest[1], dst);
  78. put_unaligned_be64(ctx->digest[0], dst + 8);
  79. *ctx = (struct ghash_desc_ctx){};
  80. return 0;
  81. }
  82. static int ghash_setkey(struct crypto_shash *tfm,
  83. const u8 *inkey, unsigned int keylen)
  84. {
  85. struct ghash_key *key = crypto_shash_ctx(tfm);
  86. u64 a, b;
  87. if (keylen != GHASH_BLOCK_SIZE) {
  88. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  89. return -EINVAL;
  90. }
  91. /* perform multiplication by 'x' in GF(2^128) */
  92. b = get_unaligned_be64(inkey);
  93. a = get_unaligned_be64(inkey + 8);
  94. key->a = (a << 1) | (b >> 63);
  95. key->b = (b << 1) | (a >> 63);
  96. if (b >> 63)
  97. key->b ^= 0xc200000000000000UL;
  98. return 0;
  99. }
  100. static struct shash_alg ghash_alg = {
  101. .digestsize = GHASH_DIGEST_SIZE,
  102. .init = ghash_init,
  103. .update = ghash_update,
  104. .final = ghash_final,
  105. .setkey = ghash_setkey,
  106. .descsize = sizeof(struct ghash_desc_ctx),
  107. .base = {
  108. .cra_name = "ghash",
  109. .cra_driver_name = "ghash-ce",
  110. .cra_priority = 200,
  111. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  112. .cra_blocksize = GHASH_BLOCK_SIZE,
  113. .cra_ctxsize = sizeof(struct ghash_key),
  114. .cra_module = THIS_MODULE,
  115. },
  116. };
  117. static int __init ghash_ce_mod_init(void)
  118. {
  119. return crypto_register_shash(&ghash_alg);
  120. }
  121. static void __exit ghash_ce_mod_exit(void)
  122. {
  123. crypto_unregister_shash(&ghash_alg);
  124. }
  125. module_cpu_feature_match(PMULL, ghash_ce_mod_init);
  126. module_exit(ghash_ce_mod_exit);