ghash-generic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * GHASH: hash function for GCM (Galois/Counter Mode).
  4. *
  5. * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
  6. * Copyright (c) 2009 Intel Corp.
  7. * Author: Huang Ying <ying.huang@intel.com>
  8. */
  9. /*
  10. * GHASH is a keyed hash function used in GCM authentication tag generation.
  11. *
  12. * The original GCM paper [1] presents GHASH as a function GHASH(H, A, C) which
  13. * takes a 16-byte hash key H, additional authenticated data A, and a ciphertext
  14. * C. It formats A and C into a single byte string X, interprets X as a
  15. * polynomial over GF(2^128), and evaluates this polynomial at the point H.
  16. *
  17. * However, the NIST standard for GCM [2] presents GHASH as GHASH(H, X) where X
  18. * is the already-formatted byte string containing both A and C.
  19. *
  20. * "ghash" in the Linux crypto API uses the 'X' (pre-formatted) convention,
  21. * since the API supports only a single data stream per hash. Thus, the
  22. * formatting of 'A' and 'C' is done in the "gcm" template, not in "ghash".
  23. *
  24. * The reason "ghash" is separate from "gcm" is to allow "gcm" to use an
  25. * accelerated "ghash" when a standalone accelerated "gcm(aes)" is unavailable.
  26. * It is generally inappropriate to use "ghash" for other purposes, since it is
  27. * an "ε-almost-XOR-universal hash function", not a cryptographic hash function.
  28. * It can only be used securely in crypto modes specially designed to use it.
  29. *
  30. * [1] The Galois/Counter Mode of Operation (GCM)
  31. * (http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.694.695&rep=rep1&type=pdf)
  32. * [2] Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
  33. * (https://csrc.nist.gov/publications/detail/sp/800-38d/final)
  34. */
  35. #include <crypto/gf128mul.h>
  36. #include <crypto/ghash.h>
  37. #include <crypto/internal/hash.h>
  38. #include <crypto/utils.h>
  39. #include <linux/err.h>
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/string.h>
  43. static int ghash_init(struct shash_desc *desc)
  44. {
  45. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  46. memset(dctx, 0, sizeof(*dctx));
  47. return 0;
  48. }
  49. static int ghash_setkey(struct crypto_shash *tfm,
  50. const u8 *key, unsigned int keylen)
  51. {
  52. struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
  53. be128 k;
  54. if (keylen != GHASH_BLOCK_SIZE)
  55. return -EINVAL;
  56. if (ctx->gf128)
  57. gf128mul_free_4k(ctx->gf128);
  58. BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
  59. memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
  60. ctx->gf128 = gf128mul_init_4k_lle(&k);
  61. memzero_explicit(&k, GHASH_BLOCK_SIZE);
  62. if (!ctx->gf128)
  63. return -ENOMEM;
  64. return 0;
  65. }
  66. static int ghash_update(struct shash_desc *desc,
  67. const u8 *src, unsigned int srclen)
  68. {
  69. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  70. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  71. u8 *dst = dctx->buffer;
  72. do {
  73. crypto_xor(dst, src, GHASH_BLOCK_SIZE);
  74. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  75. src += GHASH_BLOCK_SIZE;
  76. srclen -= GHASH_BLOCK_SIZE;
  77. } while (srclen >= GHASH_BLOCK_SIZE);
  78. return srclen;
  79. }
  80. static void ghash_flush(struct shash_desc *desc, const u8 *src,
  81. unsigned int len)
  82. {
  83. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  84. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  85. u8 *dst = dctx->buffer;
  86. if (len) {
  87. crypto_xor(dst, src, len);
  88. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  89. }
  90. }
  91. static int ghash_finup(struct shash_desc *desc, const u8 *src,
  92. unsigned int len, u8 *dst)
  93. {
  94. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  95. u8 *buf = dctx->buffer;
  96. ghash_flush(desc, src, len);
  97. memcpy(dst, buf, GHASH_BLOCK_SIZE);
  98. return 0;
  99. }
  100. static void ghash_exit_tfm(struct crypto_tfm *tfm)
  101. {
  102. struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
  103. if (ctx->gf128)
  104. gf128mul_free_4k(ctx->gf128);
  105. }
  106. static struct shash_alg ghash_alg = {
  107. .digestsize = GHASH_DIGEST_SIZE,
  108. .init = ghash_init,
  109. .update = ghash_update,
  110. .finup = ghash_finup,
  111. .setkey = ghash_setkey,
  112. .descsize = sizeof(struct ghash_desc_ctx),
  113. .base = {
  114. .cra_name = "ghash",
  115. .cra_driver_name = "ghash-generic",
  116. .cra_priority = 100,
  117. .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
  118. .cra_blocksize = GHASH_BLOCK_SIZE,
  119. .cra_ctxsize = sizeof(struct ghash_ctx),
  120. .cra_module = THIS_MODULE,
  121. .cra_exit = ghash_exit_tfm,
  122. },
  123. };
  124. static int __init ghash_mod_init(void)
  125. {
  126. return crypto_register_shash(&ghash_alg);
  127. }
  128. static void __exit ghash_mod_exit(void)
  129. {
  130. crypto_unregister_shash(&ghash_alg);
  131. }
  132. module_init(ghash_mod_init);
  133. module_exit(ghash_mod_exit);
  134. MODULE_LICENSE("GPL");
  135. MODULE_DESCRIPTION("GHASH hash function");
  136. MODULE_ALIAS_CRYPTO("ghash");
  137. MODULE_ALIAS_CRYPTO("ghash-generic");