ghash-generic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 (!ctx->gf128)
  58. return -ENOKEY;
  59. if (dctx->bytes) {
  60. int n = min(srclen, dctx->bytes);
  61. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  62. dctx->bytes -= n;
  63. srclen -= n;
  64. while (n--)
  65. *pos++ ^= *src++;
  66. if (!dctx->bytes)
  67. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  68. }
  69. while (srclen >= GHASH_BLOCK_SIZE) {
  70. crypto_xor(dst, src, GHASH_BLOCK_SIZE);
  71. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  72. src += GHASH_BLOCK_SIZE;
  73. srclen -= GHASH_BLOCK_SIZE;
  74. }
  75. if (srclen) {
  76. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  77. while (srclen--)
  78. *dst++ ^= *src++;
  79. }
  80. return 0;
  81. }
  82. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  83. {
  84. u8 *dst = dctx->buffer;
  85. if (dctx->bytes) {
  86. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  87. while (dctx->bytes--)
  88. *tmp++ ^= 0;
  89. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  90. }
  91. dctx->bytes = 0;
  92. }
  93. static int ghash_final(struct shash_desc *desc, u8 *dst)
  94. {
  95. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  96. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  97. u8 *buf = dctx->buffer;
  98. if (!ctx->gf128)
  99. return -ENOKEY;
  100. ghash_flush(ctx, dctx);
  101. memcpy(dst, buf, GHASH_BLOCK_SIZE);
  102. return 0;
  103. }
  104. static void ghash_exit_tfm(struct crypto_tfm *tfm)
  105. {
  106. struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
  107. if (ctx->gf128)
  108. gf128mul_free_4k(ctx->gf128);
  109. }
  110. static struct shash_alg ghash_alg = {
  111. .digestsize = GHASH_DIGEST_SIZE,
  112. .init = ghash_init,
  113. .update = ghash_update,
  114. .final = ghash_final,
  115. .setkey = ghash_setkey,
  116. .descsize = sizeof(struct ghash_desc_ctx),
  117. .base = {
  118. .cra_name = "ghash",
  119. .cra_driver_name = "ghash-generic",
  120. .cra_priority = 100,
  121. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  122. .cra_blocksize = GHASH_BLOCK_SIZE,
  123. .cra_ctxsize = sizeof(struct ghash_ctx),
  124. .cra_module = THIS_MODULE,
  125. .cra_list = LIST_HEAD_INIT(ghash_alg.base.cra_list),
  126. .cra_exit = ghash_exit_tfm,
  127. },
  128. };
  129. static int __init ghash_mod_init(void)
  130. {
  131. return crypto_register_shash(&ghash_alg);
  132. }
  133. static void __exit ghash_mod_exit(void)
  134. {
  135. crypto_unregister_shash(&ghash_alg);
  136. }
  137. module_init(ghash_mod_init);
  138. module_exit(ghash_mod_exit);
  139. MODULE_LICENSE("GPL");
  140. MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
  141. MODULE_ALIAS("ghash");