1 // SPDX-License-Identifier: GPL-2.0-only
3 * GHASH: digest algorithm for GCM (Galois/Counter Mode).
5 * Copyright (c) 2007 Nokia Siemens Networks - Mikko Herranen <mh1@iki.fi>
6 * Copyright (c) 2009 Intel Corp.
7 * Author: Huang Ying <ying.huang@intel.com>
9 * The algorithm implementation is copied from gcm.c.
12 #include <crypto/algapi.h>
13 #include <crypto/gf128mul.h>
14 #include <crypto/ghash.h>
15 #include <crypto/internal/hash.h>
16 #include <linux/crypto.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 static int ghash_init(struct shash_desc
*desc
)
23 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
25 memset(dctx
, 0, sizeof(*dctx
));
30 static int ghash_setkey(struct crypto_shash
*tfm
,
31 const u8
*key
, unsigned int keylen
)
33 struct ghash_ctx
*ctx
= crypto_shash_ctx(tfm
);
36 if (keylen
!= GHASH_BLOCK_SIZE
) {
37 crypto_shash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
42 gf128mul_free_4k(ctx
->gf128
);
44 BUILD_BUG_ON(sizeof(k
) != GHASH_BLOCK_SIZE
);
45 memcpy(&k
, key
, GHASH_BLOCK_SIZE
); /* avoid violating alignment rules */
46 ctx
->gf128
= gf128mul_init_4k_lle(&k
);
47 memzero_explicit(&k
, GHASH_BLOCK_SIZE
);
55 static int ghash_update(struct shash_desc
*desc
,
56 const u8
*src
, unsigned int srclen
)
58 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
59 struct ghash_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
60 u8
*dst
= dctx
->buffer
;
63 int n
= min(srclen
, dctx
->bytes
);
64 u8
*pos
= dst
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
73 gf128mul_4k_lle((be128
*)dst
, ctx
->gf128
);
76 while (srclen
>= GHASH_BLOCK_SIZE
) {
77 crypto_xor(dst
, src
, GHASH_BLOCK_SIZE
);
78 gf128mul_4k_lle((be128
*)dst
, ctx
->gf128
);
79 src
+= GHASH_BLOCK_SIZE
;
80 srclen
-= GHASH_BLOCK_SIZE
;
84 dctx
->bytes
= GHASH_BLOCK_SIZE
- srclen
;
92 static void ghash_flush(struct ghash_ctx
*ctx
, struct ghash_desc_ctx
*dctx
)
94 u8
*dst
= dctx
->buffer
;
97 u8
*tmp
= dst
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
102 gf128mul_4k_lle((be128
*)dst
, ctx
->gf128
);
108 static int ghash_final(struct shash_desc
*desc
, u8
*dst
)
110 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
111 struct ghash_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
112 u8
*buf
= dctx
->buffer
;
114 ghash_flush(ctx
, dctx
);
115 memcpy(dst
, buf
, GHASH_BLOCK_SIZE
);
120 static void ghash_exit_tfm(struct crypto_tfm
*tfm
)
122 struct ghash_ctx
*ctx
= crypto_tfm_ctx(tfm
);
124 gf128mul_free_4k(ctx
->gf128
);
127 static struct shash_alg ghash_alg
= {
128 .digestsize
= GHASH_DIGEST_SIZE
,
130 .update
= ghash_update
,
131 .final
= ghash_final
,
132 .setkey
= ghash_setkey
,
133 .descsize
= sizeof(struct ghash_desc_ctx
),
136 .cra_driver_name
= "ghash-generic",
138 .cra_blocksize
= GHASH_BLOCK_SIZE
,
139 .cra_ctxsize
= sizeof(struct ghash_ctx
),
140 .cra_module
= THIS_MODULE
,
141 .cra_exit
= ghash_exit_tfm
,
145 static int __init
ghash_mod_init(void)
147 return crypto_register_shash(&ghash_alg
);
150 static void __exit
ghash_mod_exit(void)
152 crypto_unregister_shash(&ghash_alg
);
155 subsys_initcall(ghash_mod_init
);
156 module_exit(ghash_mod_exit
);
158 MODULE_LICENSE("GPL");
159 MODULE_DESCRIPTION("GHASH Message Digest Algorithm");
160 MODULE_ALIAS_CRYPTO("ghash");
161 MODULE_ALIAS_CRYPTO("ghash-generic");