ghash-generic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * GHASH: digest algorithm for GCM (Galois/Counter Mode).
  3. *
  4. * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <[email protected]>
  5. * Copyright (c) 2009 Intel Corp.
  6. * Author: Huang Ying <[email protected]>
  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 (!ctx->gf128)
  54. return -ENOKEY;
  55. if (dctx->bytes) {
  56. int n = min(srclen, dctx->bytes);
  57. u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  58. dctx->bytes -= n;
  59. srclen -= n;
  60. while (n--)
  61. *pos++ ^= *src++;
  62. if (!dctx->bytes)
  63. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  64. }
  65. while (srclen >= GHASH_BLOCK_SIZE) {
  66. crypto_xor(dst, src, GHASH_BLOCK_SIZE);
  67. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  68. src += GHASH_BLOCK_SIZE;
  69. srclen -= GHASH_BLOCK_SIZE;
  70. }
  71. if (srclen) {
  72. dctx->bytes = GHASH_BLOCK_SIZE - srclen;
  73. while (srclen--)
  74. *dst++ ^= *src++;
  75. }
  76. return 0;
  77. }
  78. static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
  79. {
  80. u8 *dst = dctx->buffer;
  81. if (dctx->bytes) {
  82. u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
  83. while (dctx->bytes--)
  84. *tmp++ ^= 0;
  85. gf128mul_4k_lle((be128 *)dst, ctx->gf128);
  86. }
  87. dctx->bytes = 0;
  88. }
  89. static int ghash_final(struct shash_desc *desc, u8 *dst)
  90. {
  91. struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
  92. struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
  93. u8 *buf = dctx->buffer;
  94. if (!ctx->gf128)
  95. return -ENOKEY;
  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_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 Message Digest Algorithm");
  136. MODULE_ALIAS_CRYPTO("ghash");
  137. MODULE_ALIAS_CRYPTO("ghash-generic");