2 * Accelerated GHASH implementation with Intel PCLMULQDQ-NI
3 * instructions. This file contains glue code.
5 * Copyright (c) 2009 Intel Corp.
6 * Author: Huang Ying <ying.huang@intel.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/crypto.h>
18 #include <crypto/algapi.h>
19 #include <crypto/cryptd.h>
20 #include <crypto/gf128mul.h>
21 #include <crypto/internal/hash.h>
23 #include <asm/cpu_device_id.h>
25 #define GHASH_BLOCK_SIZE 16
26 #define GHASH_DIGEST_SIZE 16
28 void clmul_ghash_mul(char *dst
, const be128
*shash
);
30 void clmul_ghash_update(char *dst
, const char *src
, unsigned int srclen
,
33 struct ghash_async_ctx
{
34 struct cryptd_ahash
*cryptd_tfm
;
41 struct ghash_desc_ctx
{
42 u8 buffer
[GHASH_BLOCK_SIZE
];
46 static int ghash_init(struct shash_desc
*desc
)
48 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
50 memset(dctx
, 0, sizeof(*dctx
));
55 static int ghash_setkey(struct crypto_shash
*tfm
,
56 const u8
*key
, unsigned int keylen
)
58 struct ghash_ctx
*ctx
= crypto_shash_ctx(tfm
);
59 be128
*x
= (be128
*)key
;
62 if (keylen
!= GHASH_BLOCK_SIZE
) {
63 crypto_shash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
67 /* perform multiplication by 'x' in GF(2^128) */
68 a
= be64_to_cpu(x
->a
);
69 b
= be64_to_cpu(x
->b
);
71 ctx
->shash
.a
= (__be64
)((b
<< 1) | (a
>> 63));
72 ctx
->shash
.b
= (__be64
)((a
<< 1) | (b
>> 63));
75 ctx
->shash
.b
^= cpu_to_be64(0xc2);
80 static int ghash_update(struct shash_desc
*desc
,
81 const u8
*src
, unsigned int srclen
)
83 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
84 struct ghash_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
85 u8
*dst
= dctx
->buffer
;
89 int n
= min(srclen
, dctx
->bytes
);
90 u8
*pos
= dst
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
99 clmul_ghash_mul(dst
, &ctx
->shash
);
102 clmul_ghash_update(dst
, src
, srclen
, &ctx
->shash
);
106 src
+= srclen
- (srclen
& 0xf);
108 dctx
->bytes
= GHASH_BLOCK_SIZE
- srclen
;
116 static void ghash_flush(struct ghash_ctx
*ctx
, struct ghash_desc_ctx
*dctx
)
118 u8
*dst
= dctx
->buffer
;
121 u8
*tmp
= dst
+ (GHASH_BLOCK_SIZE
- dctx
->bytes
);
123 while (dctx
->bytes
--)
127 clmul_ghash_mul(dst
, &ctx
->shash
);
134 static int ghash_final(struct shash_desc
*desc
, u8
*dst
)
136 struct ghash_desc_ctx
*dctx
= shash_desc_ctx(desc
);
137 struct ghash_ctx
*ctx
= crypto_shash_ctx(desc
->tfm
);
138 u8
*buf
= dctx
->buffer
;
140 ghash_flush(ctx
, dctx
);
141 memcpy(dst
, buf
, GHASH_BLOCK_SIZE
);
146 static struct shash_alg ghash_alg
= {
147 .digestsize
= GHASH_DIGEST_SIZE
,
149 .update
= ghash_update
,
150 .final
= ghash_final
,
151 .setkey
= ghash_setkey
,
152 .descsize
= sizeof(struct ghash_desc_ctx
),
154 .cra_name
= "__ghash",
155 .cra_driver_name
= "__ghash-pclmulqdqni",
157 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
158 .cra_blocksize
= GHASH_BLOCK_SIZE
,
159 .cra_ctxsize
= sizeof(struct ghash_ctx
),
160 .cra_module
= THIS_MODULE
,
164 static int ghash_async_init(struct ahash_request
*req
)
166 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
167 struct ghash_async_ctx
*ctx
= crypto_ahash_ctx(tfm
);
168 struct ahash_request
*cryptd_req
= ahash_request_ctx(req
);
169 struct cryptd_ahash
*cryptd_tfm
= ctx
->cryptd_tfm
;
171 if (!irq_fpu_usable()) {
172 memcpy(cryptd_req
, req
, sizeof(*req
));
173 ahash_request_set_tfm(cryptd_req
, &cryptd_tfm
->base
);
174 return crypto_ahash_init(cryptd_req
);
176 struct shash_desc
*desc
= cryptd_shash_desc(cryptd_req
);
177 struct crypto_shash
*child
= cryptd_ahash_child(cryptd_tfm
);
180 desc
->flags
= req
->base
.flags
;
181 return crypto_shash_init(desc
);
185 static int ghash_async_update(struct ahash_request
*req
)
187 struct ahash_request
*cryptd_req
= ahash_request_ctx(req
);
189 if (!irq_fpu_usable()) {
190 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
191 struct ghash_async_ctx
*ctx
= crypto_ahash_ctx(tfm
);
192 struct cryptd_ahash
*cryptd_tfm
= ctx
->cryptd_tfm
;
194 memcpy(cryptd_req
, req
, sizeof(*req
));
195 ahash_request_set_tfm(cryptd_req
, &cryptd_tfm
->base
);
196 return crypto_ahash_update(cryptd_req
);
198 struct shash_desc
*desc
= cryptd_shash_desc(cryptd_req
);
199 return shash_ahash_update(req
, desc
);
203 static int ghash_async_final(struct ahash_request
*req
)
205 struct ahash_request
*cryptd_req
= ahash_request_ctx(req
);
207 if (!irq_fpu_usable()) {
208 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
209 struct ghash_async_ctx
*ctx
= crypto_ahash_ctx(tfm
);
210 struct cryptd_ahash
*cryptd_tfm
= ctx
->cryptd_tfm
;
212 memcpy(cryptd_req
, req
, sizeof(*req
));
213 ahash_request_set_tfm(cryptd_req
, &cryptd_tfm
->base
);
214 return crypto_ahash_final(cryptd_req
);
216 struct shash_desc
*desc
= cryptd_shash_desc(cryptd_req
);
217 return crypto_shash_final(desc
, req
->result
);
221 static int ghash_async_digest(struct ahash_request
*req
)
223 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
224 struct ghash_async_ctx
*ctx
= crypto_ahash_ctx(tfm
);
225 struct ahash_request
*cryptd_req
= ahash_request_ctx(req
);
226 struct cryptd_ahash
*cryptd_tfm
= ctx
->cryptd_tfm
;
228 if (!irq_fpu_usable()) {
229 memcpy(cryptd_req
, req
, sizeof(*req
));
230 ahash_request_set_tfm(cryptd_req
, &cryptd_tfm
->base
);
231 return crypto_ahash_digest(cryptd_req
);
233 struct shash_desc
*desc
= cryptd_shash_desc(cryptd_req
);
234 struct crypto_shash
*child
= cryptd_ahash_child(cryptd_tfm
);
237 desc
->flags
= req
->base
.flags
;
238 return shash_ahash_digest(req
, desc
);
242 static int ghash_async_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
245 struct ghash_async_ctx
*ctx
= crypto_ahash_ctx(tfm
);
246 struct crypto_ahash
*child
= &ctx
->cryptd_tfm
->base
;
249 crypto_ahash_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
250 crypto_ahash_set_flags(child
, crypto_ahash_get_flags(tfm
)
251 & CRYPTO_TFM_REQ_MASK
);
252 err
= crypto_ahash_setkey(child
, key
, keylen
);
253 crypto_ahash_set_flags(tfm
, crypto_ahash_get_flags(child
)
254 & CRYPTO_TFM_RES_MASK
);
259 static int ghash_async_init_tfm(struct crypto_tfm
*tfm
)
261 struct cryptd_ahash
*cryptd_tfm
;
262 struct ghash_async_ctx
*ctx
= crypto_tfm_ctx(tfm
);
264 cryptd_tfm
= cryptd_alloc_ahash("__ghash-pclmulqdqni", 0, 0);
265 if (IS_ERR(cryptd_tfm
))
266 return PTR_ERR(cryptd_tfm
);
267 ctx
->cryptd_tfm
= cryptd_tfm
;
268 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
269 sizeof(struct ahash_request
) +
270 crypto_ahash_reqsize(&cryptd_tfm
->base
));
275 static void ghash_async_exit_tfm(struct crypto_tfm
*tfm
)
277 struct ghash_async_ctx
*ctx
= crypto_tfm_ctx(tfm
);
279 cryptd_free_ahash(ctx
->cryptd_tfm
);
282 static struct ahash_alg ghash_async_alg
= {
283 .init
= ghash_async_init
,
284 .update
= ghash_async_update
,
285 .final
= ghash_async_final
,
286 .setkey
= ghash_async_setkey
,
287 .digest
= ghash_async_digest
,
289 .digestsize
= GHASH_DIGEST_SIZE
,
292 .cra_driver_name
= "ghash-clmulni",
294 .cra_ctxsize
= sizeof(struct ghash_async_ctx
),
295 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
| CRYPTO_ALG_ASYNC
,
296 .cra_blocksize
= GHASH_BLOCK_SIZE
,
297 .cra_type
= &crypto_ahash_type
,
298 .cra_module
= THIS_MODULE
,
299 .cra_init
= ghash_async_init_tfm
,
300 .cra_exit
= ghash_async_exit_tfm
,
305 static const struct x86_cpu_id pcmul_cpu_id
[] = {
306 X86_FEATURE_MATCH(X86_FEATURE_PCLMULQDQ
), /* Pickle-Mickle-Duck */
309 MODULE_DEVICE_TABLE(x86cpu
, pcmul_cpu_id
);
311 static int __init
ghash_pclmulqdqni_mod_init(void)
315 if (!x86_match_cpu(pcmul_cpu_id
))
318 err
= crypto_register_shash(&ghash_alg
);
321 err
= crypto_register_ahash(&ghash_async_alg
);
328 crypto_unregister_shash(&ghash_alg
);
333 static void __exit
ghash_pclmulqdqni_mod_exit(void)
335 crypto_unregister_ahash(&ghash_async_alg
);
336 crypto_unregister_shash(&ghash_alg
);
339 module_init(ghash_pclmulqdqni_mod_init
);
340 module_exit(ghash_pclmulqdqni_mod_exit
);
342 MODULE_LICENSE("GPL");
343 MODULE_DESCRIPTION("GHASH Message Digest Algorithm, "
344 "acclerated by PCLMULQDQ-NI");
345 MODULE_ALIAS_CRYPTO("ghash");