ghash-generic.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * GHASH: digest algorithm for GCM (Galois/Counter Mode).
  3. *
  4. * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
  5. * Copyright (c) 2009 Intel Corp.
  6. * Author: Huang Ying <ying.huang@intel.com>
  7. *
  8. * The algorithm implementation is copied from gcm.c.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <crypto/algapi.h>
  15. #include <crypto/gf128mul.h>
  16. #include <crypto/ghash.h>
  17. #include <crypto/internal/hash.h>
  18. #include <linux/crypto.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. static int ghash_init(struct shash_desc *desc)
  23. {
  24. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  25. memset(dctx, 0, sizeof(*dctx));
  26. return 0;
  27. }
  28. static int ghash_setkey(struct crypto_shash *tfm,
  29. const u8 *key, unsigned int keylen)
  30. {
  31. struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
  32. be128 k;
  33. if (keylen != GHASH_BLOCK_SIZE) {
  34. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  35. return -EINVAL;
  36. }
  37. if (ctx->gf128)
  38. gf128mul_free_4k(ctx->gf128);
  39. BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
  40. memcpy(&k, key, GHASH_BLOCK_SIZE); /* avoid violating alignment rules */
  41. ctx->gf128 = gf128mul_init_4k_lle(&k);
  42. memzero_explicit(&k, GHASH_BLOCK_SIZE);
  43. if (!ctx->gf128)
  44. return -ENOMEM;
  45. return 0;
  46. }
  47. static int ghash_update(struct shash_desc *desc,
  48. const u8 *src, unsigned int srclen)
  49. {
  50. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  51. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  52. u8 *dst = dctx->buffer;
  53. if (dctx->bytes) {
  54. int n = min(srclen, dctx->bytes);
  55. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  56. dctx->bytes -= n;
  57. srclen -= n;
  58. while (n--)
  59. *pos++ ^= *src++;
  60. if (!dctx->bytes)
  61. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  62. }
  63. while (srclen >= GHASH_BLOCK_SIZE) {
  64. crypto_xor(dst, src, GHASH_BLOCK_SIZE);
  65. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  66. src += GHASH_BLOCK_SIZE;
  67. srclen -= GHASH_BLOCK_SIZE;
  68. }
  69. if (srclen) {
  70. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  71. while (srclen--)
  72. *dst++ ^= *src++;
  73. }
  74. return 0;
  75. }
  76. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  77. {
  78. u8 *dst = dctx->buffer;
  79. if (dctx->bytes) {
  80. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  81. while (dctx->bytes--)
  82. *tmp++ ^= 0;
  83. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  84. }
  85. dctx->bytes = 0;
  86. }
  87. static int ghash_final(struct shash_desc *desc, u8 *dst)
  88. {
  89. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  90. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  91. u8 *buf = dctx->buffer;
  92. ghash_flush(ctx, dctx);
  93. memcpy(dst, buf, GHASH_BLOCK_SIZE);
  94. return 0;
  95. }
  96. static void ghash_exit_tfm(struct crypto_tfm *tfm)
  97. {
  98. struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
  99. if (ctx->gf128)
  100. gf128mul_free_4k(ctx->gf128);
  101. }
  102. static struct shash_alg ghash_alg = {
  103. .digestsize = GHASH_DIGEST_SIZE,
  104. .init = ghash_init,
  105. .update = ghash_update,
  106. .final = ghash_final,
  107. .setkey = ghash_setkey,
  108. .descsize = sizeof(struct ghash_desc_ctx),
  109. .base = {
  110. .cra_name = "ghash",
  111. .cra_driver_name = "ghash-generic",
  112. .cra_priority = 100,
  113. .cra_blocksize = GHASH_BLOCK_SIZE,
  114. .cra_ctxsize = sizeof(struct ghash_ctx),
  115. .cra_module = THIS_MODULE,
  116. .cra_exit = ghash_exit_tfm,
  117. },
  118. };
  119. static int __init ghash_mod_init(void)
  120. {
  121. return crypto_register_shash(&ghash_alg);
  122. }
  123. static void __exit ghash_mod_exit(void)
  124. {
  125. crypto_unregister_shash(&ghash_alg);
  126. }
  127. module_init(ghash_mod_init);
  128. module_exit(ghash_mod_exit);
  129. MODULE_LICENSE("GPL");
  130. MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
  131. MODULE_ALIAS_CRYPTO("ghash");
  132. MODULE_ALIAS_CRYPTO("ghash-generic");