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. if (keylen != GHASH_BLOCK_SIZE) {
  33. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  34. return -EINVAL;
  35. }
  36. if (ctx->gf128)
  37. gf128mul_free_4k(ctx->gf128);
  38. ctx->gf128 = gf128mul_init_4k_lle((be128 *)key);
  39. if (!ctx->gf128)
  40. return -ENOMEM;
  41. return 0;
  42. }
  43. static int ghash_update(struct shash_desc *desc,
  44. const u8 *src, unsigned int srclen)
  45. {
  46. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  47. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  48. u8 *dst = dctx->buffer;
  49. if (!ctx->gf128)
  50. return -ENOKEY;
  51. if (dctx->bytes) {
  52. int n = min(srclen, dctx->bytes);
  53. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  54. dctx->bytes -= n;
  55. srclen -= n;
  56. while (n--)
  57. *pos++ ^= *src++;
  58. if (!dctx->bytes)
  59. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  60. }
  61. while (srclen >= GHASH_BLOCK_SIZE) {
  62. crypto_xor(dst, src, GHASH_BLOCK_SIZE);
  63. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  64. src += GHASH_BLOCK_SIZE;
  65. srclen -= GHASH_BLOCK_SIZE;
  66. }
  67. if (srclen) {
  68. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  69. while (srclen--)
  70. *dst++ ^= *src++;
  71. }
  72. return 0;
  73. }
  74. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  75. {
  76. u8 *dst = dctx->buffer;
  77. if (dctx->bytes) {
  78. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  79. while (dctx->bytes--)
  80. *tmp++ ^= 0;
  81. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  82. }
  83. dctx->bytes = 0;
  84. }
  85. static int ghash_final(struct shash_desc *desc, u8 *dst)
  86. {
  87. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  88. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  89. u8 *buf = dctx->buffer;
  90. if (!ctx->gf128)
  91. return -ENOKEY;
  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_flags = CRYPTO_ALG_TYPE_SHASH,
  114. .cra_blocksize = GHASH_BLOCK_SIZE,
  115. .cra_ctxsize = sizeof(struct ghash_ctx),
  116. .cra_module = THIS_MODULE,
  117. .cra_exit = ghash_exit_tfm,
  118. },
  119. };
  120. static int __init ghash_mod_init(void)
  121. {
  122. return crypto_register_shash(&ghash_alg);
  123. }
  124. static void __exit ghash_mod_exit(void)
  125. {
  126. crypto_unregister_shash(&ghash_alg);
  127. }
  128. module_init(ghash_mod_init);
  129. module_exit(ghash_mod_exit);
  130. MODULE_LICENSE("GPL");
  131. MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
  132. MODULE_ALIAS_CRYPTO("ghash");
  133. MODULE_ALIAS_CRYPTO("ghash-generic");