ghash-generic.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/internal/hash.h>
  17. #include <linux/crypto.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #define GHASH_BLOCK_SIZE 16
  22. #define GHASH_DIGEST_SIZE 16
  23. struct ghash_ctx {
  24. struct gf128mul_4k *gf128;
  25. };
  26. struct ghash_desc_ctx {
  27. u8 buffer[GHASH_BLOCK_SIZE];
  28. u32 bytes;
  29. };
  30. static int ghash_init(struct shash_desc *desc)
  31. {
  32. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  33. memset(dctx, 0, sizeof(*dctx));
  34. return 0;
  35. }
  36. static int ghash_setkey(struct crypto_shash *tfm,
  37. const u8 *key, unsigned int keylen)
  38. {
  39. struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
  40. if (keylen != GHASH_BLOCK_SIZE) {
  41. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  42. return -EINVAL;
  43. }
  44. if (ctx->gf128)
  45. gf128mul_free_4k(ctx->gf128);
  46. ctx->gf128 = gf128mul_init_4k_lle((be128 *)key);
  47. if (!ctx->gf128)
  48. return -ENOMEM;
  49. return 0;
  50. }
  51. static int ghash_update(struct shash_desc *desc,
  52. const u8 *src, unsigned int srclen)
  53. {
  54. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  55. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  56. u8 *dst = dctx->buffer;
  57. if (dctx->bytes) {
  58. int n = min(srclen, dctx->bytes);
  59. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  60. dctx->bytes -= n;
  61. srclen -= n;
  62. while (n--)
  63. *pos++ ^= *src++;
  64. if (!dctx->bytes)
  65. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  66. }
  67. while (srclen >= GHASH_BLOCK_SIZE) {
  68. crypto_xor(dst, src, GHASH_BLOCK_SIZE);
  69. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  70. src += GHASH_BLOCK_SIZE;
  71. srclen -= GHASH_BLOCK_SIZE;
  72. }
  73. if (srclen) {
  74. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  75. while (srclen--)
  76. *dst++ ^= *src++;
  77. }
  78. return 0;
  79. }
  80. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  81. {
  82. u8 *dst = dctx->buffer;
  83. if (dctx->bytes) {
  84. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  85. while (dctx->bytes--)
  86. *tmp++ ^= 0;
  87. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  88. }
  89. dctx->bytes = 0;
  90. }
  91. static int ghash_final(struct shash_desc *desc, u8 *dst)
  92. {
  93. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  94. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  95. u8 *buf = dctx->buffer;
  96. ghash_flush(ctx, dctx);
  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. .final = ghash_final,
  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_ALG_TYPE_SHASH,
  118. .cra_blocksize = GHASH_BLOCK_SIZE,
  119. .cra_ctxsize = sizeof(struct ghash_ctx),
  120. .cra_module = THIS_MODULE,
  121. .cra_list = LIST_HEAD_INIT(ghash_alg.base.cra_list),
  122. .cra_exit = ghash_exit_tfm,
  123. },
  124. };
  125. static int __init ghash_mod_init(void)
  126. {
  127. return crypto_register_shash(&ghash_alg);
  128. }
  129. static void __exit ghash_mod_exit(void)
  130. {
  131. crypto_unregister_shash(&ghash_alg);
  132. }
  133. module_init(ghash_mod_init);
  134. module_exit(ghash_mod_exit);
  135. MODULE_LICENSE("GPL");
  136. MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
  137. MODULE_ALIAS("ghash");