polyval-generic.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * POLYVAL: hash function for HCTR2.
  4. *
  5. * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <[email protected]>
  6. * Copyright (c) 2009 Intel Corp.
  7. * Author: Huang Ying <[email protected]>
  8. * Copyright 2021 Google LLC
  9. */
  10. /*
  11. * Code based on crypto/ghash-generic.c
  12. *
  13. * POLYVAL is a keyed hash function similar to GHASH. POLYVAL uses a different
  14. * modulus for finite field multiplication which makes hardware accelerated
  15. * implementations on little-endian machines faster. POLYVAL is used in the
  16. * kernel to implement HCTR2, but was originally specified for AES-GCM-SIV
  17. * (RFC 8452).
  18. *
  19. * For more information see:
  20. * Length-preserving encryption with HCTR2:
  21. * https://eprint.iacr.org/2021/1441.pdf
  22. * AES-GCM-SIV: Nonce Misuse-Resistant Authenticated Encryption:
  23. * https://datatracker.ietf.org/doc/html/rfc8452
  24. *
  25. * Like GHASH, POLYVAL is not a cryptographic hash function and should
  26. * not be used outside of crypto modes explicitly designed to use POLYVAL.
  27. *
  28. * This implementation uses a convenient trick involving the GHASH and POLYVAL
  29. * fields. This trick allows multiplication in the POLYVAL field to be
  30. * implemented by using multiplication in the GHASH field as a subroutine. An
  31. * element of the POLYVAL field can be converted to an element of the GHASH
  32. * field by computing x*REVERSE(a), where REVERSE reverses the byte-ordering of
  33. * a. Similarly, an element of the GHASH field can be converted back to the
  34. * POLYVAL field by computing REVERSE(x^{-1}*a). For more information, see:
  35. * https://datatracker.ietf.org/doc/html/rfc8452#appendix-A
  36. *
  37. * By using this trick, we do not need to implement the POLYVAL field for the
  38. * generic implementation.
  39. *
  40. * Warning: this generic implementation is not intended to be used in practice
  41. * and is not constant time. For practical use, a hardware accelerated
  42. * implementation of POLYVAL should be used instead.
  43. *
  44. */
  45. #include <asm/unaligned.h>
  46. #include <crypto/algapi.h>
  47. #include <crypto/gf128mul.h>
  48. #include <crypto/polyval.h>
  49. #include <crypto/internal/hash.h>
  50. #include <linux/crypto.h>
  51. #include <linux/init.h>
  52. #include <linux/kernel.h>
  53. #include <linux/module.h>
  54. struct polyval_tfm_ctx {
  55. struct gf128mul_4k *gf128;
  56. };
  57. struct polyval_desc_ctx {
  58. union {
  59. u8 buffer[POLYVAL_BLOCK_SIZE];
  60. be128 buffer128;
  61. };
  62. u32 bytes;
  63. };
  64. static void copy_and_reverse(u8 dst[POLYVAL_BLOCK_SIZE],
  65. const u8 src[POLYVAL_BLOCK_SIZE])
  66. {
  67. u64 a = get_unaligned((const u64 *)&src[0]);
  68. u64 b = get_unaligned((const u64 *)&src[8]);
  69. put_unaligned(swab64(a), (u64 *)&dst[8]);
  70. put_unaligned(swab64(b), (u64 *)&dst[0]);
  71. }
  72. /*
  73. * Performs multiplication in the POLYVAL field using the GHASH field as a
  74. * subroutine. This function is used as a fallback for hardware accelerated
  75. * implementations when simd registers are unavailable.
  76. *
  77. * Note: This function is not used for polyval-generic, instead we use the 4k
  78. * lookup table implementation for finite field multiplication.
  79. */
  80. void polyval_mul_non4k(u8 *op1, const u8 *op2)
  81. {
  82. be128 a, b;
  83. // Assume one argument is in Montgomery form and one is not.
  84. copy_and_reverse((u8 *)&a, op1);
  85. copy_and_reverse((u8 *)&b, op2);
  86. gf128mul_x_lle(&a, &a);
  87. gf128mul_lle(&a, &b);
  88. copy_and_reverse(op1, (u8 *)&a);
  89. }
  90. EXPORT_SYMBOL_GPL(polyval_mul_non4k);
  91. /*
  92. * Perform a POLYVAL update using non4k multiplication. This function is used
  93. * as a fallback for hardware accelerated implementations when simd registers
  94. * are unavailable.
  95. *
  96. * Note: This function is not used for polyval-generic, instead we use the 4k
  97. * lookup table implementation of finite field multiplication.
  98. */
  99. void polyval_update_non4k(const u8 *key, const u8 *in,
  100. size_t nblocks, u8 *accumulator)
  101. {
  102. while (nblocks--) {
  103. crypto_xor(accumulator, in, POLYVAL_BLOCK_SIZE);
  104. polyval_mul_non4k(accumulator, key);
  105. in += POLYVAL_BLOCK_SIZE;
  106. }
  107. }
  108. EXPORT_SYMBOL_GPL(polyval_update_non4k);
  109. static int polyval_setkey(struct crypto_shash *tfm,
  110. const u8 *key, unsigned int keylen)
  111. {
  112. struct polyval_tfm_ctx *ctx = crypto_shash_ctx(tfm);
  113. be128 k;
  114. if (keylen != POLYVAL_BLOCK_SIZE)
  115. return -EINVAL;
  116. gf128mul_free_4k(ctx->gf128);
  117. BUILD_BUG_ON(sizeof(k) != POLYVAL_BLOCK_SIZE);
  118. copy_and_reverse((u8 *)&k, key);
  119. gf128mul_x_lle(&k, &k);
  120. ctx->gf128 = gf128mul_init_4k_lle(&k);
  121. memzero_explicit(&k, POLYVAL_BLOCK_SIZE);
  122. if (!ctx->gf128)
  123. return -ENOMEM;
  124. return 0;
  125. }
  126. static int polyval_init(struct shash_desc *desc)
  127. {
  128. struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
  129. memset(dctx, 0, sizeof(*dctx));
  130. return 0;
  131. }
  132. static int polyval_update(struct shash_desc *desc,
  133. const u8 *src, unsigned int srclen)
  134. {
  135. struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
  136. const struct polyval_tfm_ctx *ctx = crypto_shash_ctx(desc->tfm);
  137. u8 *pos;
  138. u8 tmp[POLYVAL_BLOCK_SIZE];
  139. int n;
  140. if (dctx->bytes) {
  141. n = min(srclen, dctx->bytes);
  142. pos = dctx->buffer + dctx->bytes - 1;
  143. dctx->bytes -= n;
  144. srclen -= n;
  145. while (n--)
  146. *pos-- ^= *src++;
  147. if (!dctx->bytes)
  148. gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
  149. }
  150. while (srclen >= POLYVAL_BLOCK_SIZE) {
  151. copy_and_reverse(tmp, src);
  152. crypto_xor(dctx->buffer, tmp, POLYVAL_BLOCK_SIZE);
  153. gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
  154. src += POLYVAL_BLOCK_SIZE;
  155. srclen -= POLYVAL_BLOCK_SIZE;
  156. }
  157. if (srclen) {
  158. dctx->bytes = POLYVAL_BLOCK_SIZE - srclen;
  159. pos = dctx->buffer + POLYVAL_BLOCK_SIZE - 1;
  160. while (srclen--)
  161. *pos-- ^= *src++;
  162. }
  163. return 0;
  164. }
  165. static int polyval_final(struct shash_desc *desc, u8 *dst)
  166. {
  167. struct polyval_desc_ctx *dctx = shash_desc_ctx(desc);
  168. const struct polyval_tfm_ctx *ctx = crypto_shash_ctx(desc->tfm);
  169. if (dctx->bytes)
  170. gf128mul_4k_lle(&dctx->buffer128, ctx->gf128);
  171. copy_and_reverse(dst, dctx->buffer);
  172. return 0;
  173. }
  174. static void polyval_exit_tfm(struct crypto_tfm *tfm)
  175. {
  176. struct polyval_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  177. gf128mul_free_4k(ctx->gf128);
  178. }
  179. static struct shash_alg polyval_alg = {
  180. .digestsize = POLYVAL_DIGEST_SIZE,
  181. .init = polyval_init,
  182. .update = polyval_update,
  183. .final = polyval_final,
  184. .setkey = polyval_setkey,
  185. .descsize = sizeof(struct polyval_desc_ctx),
  186. .base = {
  187. .cra_name = "polyval",
  188. .cra_driver_name = "polyval-generic",
  189. .cra_priority = 100,
  190. .cra_blocksize = POLYVAL_BLOCK_SIZE,
  191. .cra_ctxsize = sizeof(struct polyval_tfm_ctx),
  192. .cra_module = THIS_MODULE,
  193. .cra_exit = polyval_exit_tfm,
  194. },
  195. };
  196. static int __init polyval_mod_init(void)
  197. {
  198. return crypto_register_shash(&polyval_alg);
  199. }
  200. static void __exit polyval_mod_exit(void)
  201. {
  202. crypto_unregister_shash(&polyval_alg);
  203. }
  204. subsys_initcall(polyval_mod_init);
  205. module_exit(polyval_mod_exit);
  206. MODULE_LICENSE("GPL");
  207. MODULE_DESCRIPTION("POLYVAL hash function");
  208. MODULE_ALIAS_CRYPTO("polyval");
  209. MODULE_ALIAS_CRYPTO("polyval-generic");