0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 #include <crypto/algapi.h>
0038 #include <crypto/gf128mul.h>
0039 #include <crypto/ghash.h>
0040 #include <crypto/internal/hash.h>
0041 #include <linux/crypto.h>
0042 #include <linux/init.h>
0043 #include <linux/kernel.h>
0044 #include <linux/module.h>
0045
0046 static int ghash_init(struct shash_desc *desc)
0047 {
0048 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
0049
0050 memset(dctx, 0, sizeof(*dctx));
0051
0052 return 0;
0053 }
0054
0055 static int ghash_setkey(struct crypto_shash *tfm,
0056 const u8 *key, unsigned int keylen)
0057 {
0058 struct ghash_ctx *ctx = crypto_shash_ctx(tfm);
0059 be128 k;
0060
0061 if (keylen != GHASH_BLOCK_SIZE)
0062 return -EINVAL;
0063
0064 if (ctx->gf128)
0065 gf128mul_free_4k(ctx->gf128);
0066
0067 BUILD_BUG_ON(sizeof(k) != GHASH_BLOCK_SIZE);
0068 memcpy(&k, key, GHASH_BLOCK_SIZE);
0069 ctx->gf128 = gf128mul_init_4k_lle(&k);
0070 memzero_explicit(&k, GHASH_BLOCK_SIZE);
0071
0072 if (!ctx->gf128)
0073 return -ENOMEM;
0074
0075 return 0;
0076 }
0077
0078 static int ghash_update(struct shash_desc *desc,
0079 const u8 *src, unsigned int srclen)
0080 {
0081 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
0082 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
0083 u8 *dst = dctx->buffer;
0084
0085 if (dctx->bytes) {
0086 int n = min(srclen, dctx->bytes);
0087 u8 *pos = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
0088
0089 dctx->bytes -= n;
0090 srclen -= n;
0091
0092 while (n--)
0093 *pos++ ^= *src++;
0094
0095 if (!dctx->bytes)
0096 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
0097 }
0098
0099 while (srclen >= GHASH_BLOCK_SIZE) {
0100 crypto_xor(dst, src, GHASH_BLOCK_SIZE);
0101 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
0102 src += GHASH_BLOCK_SIZE;
0103 srclen -= GHASH_BLOCK_SIZE;
0104 }
0105
0106 if (srclen) {
0107 dctx->bytes = GHASH_BLOCK_SIZE - srclen;
0108 while (srclen--)
0109 *dst++ ^= *src++;
0110 }
0111
0112 return 0;
0113 }
0114
0115 static void ghash_flush(struct ghash_ctx *ctx, struct ghash_desc_ctx *dctx)
0116 {
0117 u8 *dst = dctx->buffer;
0118
0119 if (dctx->bytes) {
0120 u8 *tmp = dst + (GHASH_BLOCK_SIZE - dctx->bytes);
0121
0122 while (dctx->bytes--)
0123 *tmp++ ^= 0;
0124
0125 gf128mul_4k_lle((be128 *)dst, ctx->gf128);
0126 }
0127
0128 dctx->bytes = 0;
0129 }
0130
0131 static int ghash_final(struct shash_desc *desc, u8 *dst)
0132 {
0133 struct ghash_desc_ctx *dctx = shash_desc_ctx(desc);
0134 struct ghash_ctx *ctx = crypto_shash_ctx(desc->tfm);
0135 u8 *buf = dctx->buffer;
0136
0137 ghash_flush(ctx, dctx);
0138 memcpy(dst, buf, GHASH_BLOCK_SIZE);
0139
0140 return 0;
0141 }
0142
0143 static void ghash_exit_tfm(struct crypto_tfm *tfm)
0144 {
0145 struct ghash_ctx *ctx = crypto_tfm_ctx(tfm);
0146 if (ctx->gf128)
0147 gf128mul_free_4k(ctx->gf128);
0148 }
0149
0150 static struct shash_alg ghash_alg = {
0151 .digestsize = GHASH_DIGEST_SIZE,
0152 .init = ghash_init,
0153 .update = ghash_update,
0154 .final = ghash_final,
0155 .setkey = ghash_setkey,
0156 .descsize = sizeof(struct ghash_desc_ctx),
0157 .base = {
0158 .cra_name = "ghash",
0159 .cra_driver_name = "ghash-generic",
0160 .cra_priority = 100,
0161 .cra_blocksize = GHASH_BLOCK_SIZE,
0162 .cra_ctxsize = sizeof(struct ghash_ctx),
0163 .cra_module = THIS_MODULE,
0164 .cra_exit = ghash_exit_tfm,
0165 },
0166 };
0167
0168 static int __init ghash_mod_init(void)
0169 {
0170 return crypto_register_shash(&ghash_alg);
0171 }
0172
0173 static void __exit ghash_mod_exit(void)
0174 {
0175 crypto_unregister_shash(&ghash_alg);
0176 }
0177
0178 subsys_initcall(ghash_mod_init);
0179 module_exit(ghash_mod_exit);
0180
0181 MODULE_LICENSE("GPL");
0182 MODULE_DESCRIPTION("GHASH hash function");
0183 MODULE_ALIAS_CRYPTO("ghash");
0184 MODULE_ALIAS_CRYPTO("ghash-generic");