1 // SPDX-License-Identifier: GPL-2.0-only
3 * Accelerated GHASH implementation with ARMv8 PMULL instructions.
5 * Copyright (C) 2014 - 2018 Linaro Ltd. <ard.biesheuvel@linaro.org>
10 #include <linux/unaligned.h>
11 #include <crypto/aes.h>
12 #include <crypto/gcm.h>
13 #include <crypto/algapi.h>
14 #include <crypto/b128ops.h>
15 #include <crypto/gf128mul.h>
16 #include <crypto/internal/aead.h>
17 #include <crypto/internal/hash.h>
18 #include <crypto/internal/simd.h>
19 #include <crypto/internal/skcipher.h>
20 #include <crypto/scatterwalk.h>
21 #include <linux/cpufeature.h>
22 #include <linux/crypto.h>
23 #include <linux/module.h>
25 MODULE_DESCRIPTION("GHASH and AES-GCM using ARMv8 Crypto Extensions");
26 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
27 MODULE_LICENSE("GPL v2");
28 MODULE_ALIAS_CRYPTO("ghash");
30 #define GHASH_BLOCK_SIZE 16
31 #define GHASH_DIGEST_SIZE 16
33 #define RFC4106_NONCE_SIZE 4
40 struct ghash_desc_ctx
{
41 u64 digest
[GHASH_DIGEST_SIZE
/sizeof(u64
)];
42 u8 buf
[GHASH_BLOCK_SIZE
];
47 struct crypto_aes_ctx aes_key
;
48 u8 nonce
[RFC4106_NONCE_SIZE
];
49 struct ghash_key ghash_key
;
52 asmlinkage
void pmull_ghash_update_p64(int blocks
, u64 dg
[], const char *src
,
53 u64
const h
[][2], const char *head
);
55 asmlinkage
void pmull_ghash_update_p8(int blocks
, u64 dg
[], const char *src
,
56 u64
const h
[][2], const char *head
);
58 asmlinkage
void pmull_gcm_encrypt(int bytes
, u8 dst
[], const u8 src
[],
59 u64
const h
[][2], u64 dg
[], u8 ctr
[],
60 u32
const rk
[], int rounds
, u8 tag
[]);
61 asmlinkage
int pmull_gcm_decrypt(int bytes
, u8 dst
[], const u8 src
[],
62 u64
const h
[][2], u64 dg
[], u8 ctr
[],
63 u32
const rk
[], int rounds
, const u8 l
[],
64 const u8 tag
[], u64 authsize
);
66 static int ghash_init(struct shash_desc
*desc
)
68 struct ghash_desc_ctx
*ctx
= shash_desc_ctx(desc
);
70 *ctx
= (struct ghash_desc_ctx
){};
74 static void ghash_do_update(int blocks
, u64 dg
[], const char *src
,
75 struct ghash_key
*key
, const char *head
)
77 be128 dst
= { cpu_to_be64(dg
[1]), cpu_to_be64(dg
[0]) };
87 src
+= GHASH_BLOCK_SIZE
;
90 crypto_xor((u8
*)&dst
, in
, GHASH_BLOCK_SIZE
);
91 gf128mul_lle(&dst
, &key
->k
);
94 dg
[0] = be64_to_cpu(dst
.b
);
95 dg
[1] = be64_to_cpu(dst
.a
);
98 static __always_inline
99 void ghash_do_simd_update(int blocks
, u64 dg
[], const char *src
,
100 struct ghash_key
*key
, const char *head
,
101 void (*simd_update
)(int blocks
, u64 dg
[],
106 if (likely(crypto_simd_usable())) {
108 simd_update(blocks
, dg
, src
, key
->h
, head
);
111 ghash_do_update(blocks
, dg
, src
, key
, head
);
115 /* avoid hogging the CPU for too long */
116 #define MAX_BLOCKS (SZ_64K / GHASH_BLOCK_SIZE)
118 static int ghash_update(struct shash_desc
*desc
, const u8
*src
,
121 struct ghash_desc_ctx
*ctx
= shash_desc_ctx(desc
);
122 unsigned int partial
= ctx
->count
% GHASH_BLOCK_SIZE
;
126 if ((partial
+ len
) >= GHASH_BLOCK_SIZE
) {
127 struct ghash_key
*key
= crypto_shash_ctx(desc
->tfm
);
131 int p
= GHASH_BLOCK_SIZE
- partial
;
133 memcpy(ctx
->buf
+ partial
, src
, p
);
138 blocks
= len
/ GHASH_BLOCK_SIZE
;
139 len
%= GHASH_BLOCK_SIZE
;
142 int chunk
= min(blocks
, MAX_BLOCKS
);
144 ghash_do_simd_update(chunk
, ctx
->digest
, src
, key
,
145 partial
? ctx
->buf
: NULL
,
146 pmull_ghash_update_p8
);
149 src
+= chunk
* GHASH_BLOCK_SIZE
;
151 } while (unlikely(blocks
> 0));
154 memcpy(ctx
->buf
+ partial
, src
, len
);
158 static int ghash_final(struct shash_desc
*desc
, u8
*dst
)
160 struct ghash_desc_ctx
*ctx
= shash_desc_ctx(desc
);
161 unsigned int partial
= ctx
->count
% GHASH_BLOCK_SIZE
;
164 struct ghash_key
*key
= crypto_shash_ctx(desc
->tfm
);
166 memset(ctx
->buf
+ partial
, 0, GHASH_BLOCK_SIZE
- partial
);
168 ghash_do_simd_update(1, ctx
->digest
, ctx
->buf
, key
, NULL
,
169 pmull_ghash_update_p8
);
171 put_unaligned_be64(ctx
->digest
[1], dst
);
172 put_unaligned_be64(ctx
->digest
[0], dst
+ 8);
174 memzero_explicit(ctx
, sizeof(*ctx
));
178 static void ghash_reflect(u64 h
[], const be128
*k
)
180 u64 carry
= be64_to_cpu(k
->a
) & BIT(63) ? 1 : 0;
182 h
[0] = (be64_to_cpu(k
->b
) << 1) | carry
;
183 h
[1] = (be64_to_cpu(k
->a
) << 1) | (be64_to_cpu(k
->b
) >> 63);
186 h
[1] ^= 0xc200000000000000UL
;
189 static int ghash_setkey(struct crypto_shash
*tfm
,
190 const u8
*inkey
, unsigned int keylen
)
192 struct ghash_key
*key
= crypto_shash_ctx(tfm
);
194 if (keylen
!= GHASH_BLOCK_SIZE
)
197 /* needed for the fallback */
198 memcpy(&key
->k
, inkey
, GHASH_BLOCK_SIZE
);
200 ghash_reflect(key
->h
[0], &key
->k
);
204 static struct shash_alg ghash_alg
= {
205 .base
.cra_name
= "ghash",
206 .base
.cra_driver_name
= "ghash-neon",
207 .base
.cra_priority
= 150,
208 .base
.cra_blocksize
= GHASH_BLOCK_SIZE
,
209 .base
.cra_ctxsize
= sizeof(struct ghash_key
) + sizeof(u64
[2]),
210 .base
.cra_module
= THIS_MODULE
,
212 .digestsize
= GHASH_DIGEST_SIZE
,
214 .update
= ghash_update
,
215 .final
= ghash_final
,
216 .setkey
= ghash_setkey
,
217 .descsize
= sizeof(struct ghash_desc_ctx
),
220 static int num_rounds(struct crypto_aes_ctx
*ctx
)
223 * # of rounds specified by AES:
224 * 128 bit key 10 rounds
225 * 192 bit key 12 rounds
226 * 256 bit key 14 rounds
227 * => n byte key => 6 + (n/4) rounds
229 return 6 + ctx
->key_length
/ 4;
232 static int gcm_aes_setkey(struct crypto_aead
*tfm
, const u8
*inkey
,
235 struct gcm_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
236 u8 key
[GHASH_BLOCK_SIZE
];
240 ret
= aes_expandkey(&ctx
->aes_key
, inkey
, keylen
);
244 aes_encrypt(&ctx
->aes_key
, key
, (u8
[AES_BLOCK_SIZE
]){});
246 /* needed for the fallback */
247 memcpy(&ctx
->ghash_key
.k
, key
, GHASH_BLOCK_SIZE
);
249 ghash_reflect(ctx
->ghash_key
.h
[0], &ctx
->ghash_key
.k
);
251 h
= ctx
->ghash_key
.k
;
252 gf128mul_lle(&h
, &ctx
->ghash_key
.k
);
253 ghash_reflect(ctx
->ghash_key
.h
[1], &h
);
255 gf128mul_lle(&h
, &ctx
->ghash_key
.k
);
256 ghash_reflect(ctx
->ghash_key
.h
[2], &h
);
258 gf128mul_lle(&h
, &ctx
->ghash_key
.k
);
259 ghash_reflect(ctx
->ghash_key
.h
[3], &h
);
264 static int gcm_aes_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
)
266 return crypto_gcm_check_authsize(authsize
);
269 static void gcm_update_mac(u64 dg
[], const u8
*src
, int count
, u8 buf
[],
270 int *buf_count
, struct gcm_aes_ctx
*ctx
)
272 if (*buf_count
> 0) {
273 int buf_added
= min(count
, GHASH_BLOCK_SIZE
- *buf_count
);
275 memcpy(&buf
[*buf_count
], src
, buf_added
);
277 *buf_count
+= buf_added
;
282 if (count
>= GHASH_BLOCK_SIZE
|| *buf_count
== GHASH_BLOCK_SIZE
) {
283 int blocks
= count
/ GHASH_BLOCK_SIZE
;
285 ghash_do_simd_update(blocks
, dg
, src
, &ctx
->ghash_key
,
286 *buf_count
? buf
: NULL
,
287 pmull_ghash_update_p64
);
289 src
+= blocks
* GHASH_BLOCK_SIZE
;
290 count
%= GHASH_BLOCK_SIZE
;
295 memcpy(buf
, src
, count
);
300 static void gcm_calculate_auth_mac(struct aead_request
*req
, u64 dg
[], u32 len
)
302 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
303 struct gcm_aes_ctx
*ctx
= crypto_aead_ctx(aead
);
304 u8 buf
[GHASH_BLOCK_SIZE
];
305 struct scatter_walk walk
;
308 scatterwalk_start(&walk
, req
->src
);
311 u32 n
= scatterwalk_clamp(&walk
, len
);
315 scatterwalk_start(&walk
, sg_next(walk
.sg
));
316 n
= scatterwalk_clamp(&walk
, len
);
318 p
= scatterwalk_map(&walk
);
320 gcm_update_mac(dg
, p
, n
, buf
, &buf_count
, ctx
);
323 scatterwalk_unmap(p
);
324 scatterwalk_advance(&walk
, n
);
325 scatterwalk_done(&walk
, 0, len
);
329 memset(&buf
[buf_count
], 0, GHASH_BLOCK_SIZE
- buf_count
);
330 ghash_do_simd_update(1, dg
, buf
, &ctx
->ghash_key
, NULL
,
331 pmull_ghash_update_p64
);
335 static int gcm_encrypt(struct aead_request
*req
, char *iv
, int assoclen
)
337 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
338 struct gcm_aes_ctx
*ctx
= crypto_aead_ctx(aead
);
339 int nrounds
= num_rounds(&ctx
->aes_key
);
340 struct skcipher_walk walk
;
341 u8 buf
[AES_BLOCK_SIZE
];
347 lengths
.a
= cpu_to_be64(assoclen
* 8);
348 lengths
.b
= cpu_to_be64(req
->cryptlen
* 8);
351 gcm_calculate_auth_mac(req
, dg
, assoclen
);
353 put_unaligned_be32(2, iv
+ GCM_AES_IV_SIZE
);
355 err
= skcipher_walk_aead_encrypt(&walk
, req
, false);
358 const u8
*src
= walk
.src
.virt
.addr
;
359 u8
*dst
= walk
.dst
.virt
.addr
;
360 int nbytes
= walk
.nbytes
;
362 tag
= (u8
*)&lengths
;
364 if (unlikely(nbytes
> 0 && nbytes
< AES_BLOCK_SIZE
)) {
365 src
= dst
= memcpy(buf
+ sizeof(buf
) - nbytes
,
367 } else if (nbytes
< walk
.total
) {
368 nbytes
&= ~(AES_BLOCK_SIZE
- 1);
373 pmull_gcm_encrypt(nbytes
, dst
, src
, ctx
->ghash_key
.h
,
374 dg
, iv
, ctx
->aes_key
.key_enc
, nrounds
,
378 if (unlikely(!nbytes
))
381 if (unlikely(nbytes
> 0 && nbytes
< AES_BLOCK_SIZE
))
382 memcpy(walk
.dst
.virt
.addr
,
383 buf
+ sizeof(buf
) - nbytes
, nbytes
);
385 err
= skcipher_walk_done(&walk
, walk
.nbytes
- nbytes
);
386 } while (walk
.nbytes
);
391 /* copy authtag to end of dst */
392 scatterwalk_map_and_copy(tag
, req
->dst
, req
->assoclen
+ req
->cryptlen
,
393 crypto_aead_authsize(aead
), 1);
398 static int gcm_decrypt(struct aead_request
*req
, char *iv
, int assoclen
)
400 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
401 struct gcm_aes_ctx
*ctx
= crypto_aead_ctx(aead
);
402 unsigned int authsize
= crypto_aead_authsize(aead
);
403 int nrounds
= num_rounds(&ctx
->aes_key
);
404 struct skcipher_walk walk
;
405 u8 otag
[AES_BLOCK_SIZE
];
406 u8 buf
[AES_BLOCK_SIZE
];
413 lengths
.a
= cpu_to_be64(assoclen
* 8);
414 lengths
.b
= cpu_to_be64((req
->cryptlen
- authsize
) * 8);
417 gcm_calculate_auth_mac(req
, dg
, assoclen
);
419 put_unaligned_be32(2, iv
+ GCM_AES_IV_SIZE
);
421 scatterwalk_map_and_copy(otag
, req
->src
,
422 req
->assoclen
+ req
->cryptlen
- authsize
,
425 err
= skcipher_walk_aead_decrypt(&walk
, req
, false);
428 const u8
*src
= walk
.src
.virt
.addr
;
429 u8
*dst
= walk
.dst
.virt
.addr
;
430 int nbytes
= walk
.nbytes
;
432 tag
= (u8
*)&lengths
;
434 if (unlikely(nbytes
> 0 && nbytes
< AES_BLOCK_SIZE
)) {
435 src
= dst
= memcpy(buf
+ sizeof(buf
) - nbytes
,
437 } else if (nbytes
< walk
.total
) {
438 nbytes
&= ~(AES_BLOCK_SIZE
- 1);
443 ret
= pmull_gcm_decrypt(nbytes
, dst
, src
, ctx
->ghash_key
.h
,
444 dg
, iv
, ctx
->aes_key
.key_enc
,
445 nrounds
, tag
, otag
, authsize
);
448 if (unlikely(!nbytes
))
451 if (unlikely(nbytes
> 0 && nbytes
< AES_BLOCK_SIZE
))
452 memcpy(walk
.dst
.virt
.addr
,
453 buf
+ sizeof(buf
) - nbytes
, nbytes
);
455 err
= skcipher_walk_done(&walk
, walk
.nbytes
- nbytes
);
456 } while (walk
.nbytes
);
461 return ret
? -EBADMSG
: 0;
464 static int gcm_aes_encrypt(struct aead_request
*req
)
466 u8 iv
[AES_BLOCK_SIZE
];
468 memcpy(iv
, req
->iv
, GCM_AES_IV_SIZE
);
469 return gcm_encrypt(req
, iv
, req
->assoclen
);
472 static int gcm_aes_decrypt(struct aead_request
*req
)
474 u8 iv
[AES_BLOCK_SIZE
];
476 memcpy(iv
, req
->iv
, GCM_AES_IV_SIZE
);
477 return gcm_decrypt(req
, iv
, req
->assoclen
);
480 static int rfc4106_setkey(struct crypto_aead
*tfm
, const u8
*inkey
,
483 struct gcm_aes_ctx
*ctx
= crypto_aead_ctx(tfm
);
486 keylen
-= RFC4106_NONCE_SIZE
;
487 err
= gcm_aes_setkey(tfm
, inkey
, keylen
);
491 memcpy(ctx
->nonce
, inkey
+ keylen
, RFC4106_NONCE_SIZE
);
495 static int rfc4106_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
)
497 return crypto_rfc4106_check_authsize(authsize
);
500 static int rfc4106_encrypt(struct aead_request
*req
)
502 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
503 struct gcm_aes_ctx
*ctx
= crypto_aead_ctx(aead
);
504 u8 iv
[AES_BLOCK_SIZE
];
506 memcpy(iv
, ctx
->nonce
, RFC4106_NONCE_SIZE
);
507 memcpy(iv
+ RFC4106_NONCE_SIZE
, req
->iv
, GCM_RFC4106_IV_SIZE
);
509 return crypto_ipsec_check_assoclen(req
->assoclen
) ?:
510 gcm_encrypt(req
, iv
, req
->assoclen
- GCM_RFC4106_IV_SIZE
);
513 static int rfc4106_decrypt(struct aead_request
*req
)
515 struct crypto_aead
*aead
= crypto_aead_reqtfm(req
);
516 struct gcm_aes_ctx
*ctx
= crypto_aead_ctx(aead
);
517 u8 iv
[AES_BLOCK_SIZE
];
519 memcpy(iv
, ctx
->nonce
, RFC4106_NONCE_SIZE
);
520 memcpy(iv
+ RFC4106_NONCE_SIZE
, req
->iv
, GCM_RFC4106_IV_SIZE
);
522 return crypto_ipsec_check_assoclen(req
->assoclen
) ?:
523 gcm_decrypt(req
, iv
, req
->assoclen
- GCM_RFC4106_IV_SIZE
);
526 static struct aead_alg gcm_aes_algs
[] = {{
527 .ivsize
= GCM_AES_IV_SIZE
,
528 .chunksize
= AES_BLOCK_SIZE
,
529 .maxauthsize
= AES_BLOCK_SIZE
,
530 .setkey
= gcm_aes_setkey
,
531 .setauthsize
= gcm_aes_setauthsize
,
532 .encrypt
= gcm_aes_encrypt
,
533 .decrypt
= gcm_aes_decrypt
,
535 .base
.cra_name
= "gcm(aes)",
536 .base
.cra_driver_name
= "gcm-aes-ce",
537 .base
.cra_priority
= 300,
538 .base
.cra_blocksize
= 1,
539 .base
.cra_ctxsize
= sizeof(struct gcm_aes_ctx
) +
541 .base
.cra_module
= THIS_MODULE
,
543 .ivsize
= GCM_RFC4106_IV_SIZE
,
544 .chunksize
= AES_BLOCK_SIZE
,
545 .maxauthsize
= AES_BLOCK_SIZE
,
546 .setkey
= rfc4106_setkey
,
547 .setauthsize
= rfc4106_setauthsize
,
548 .encrypt
= rfc4106_encrypt
,
549 .decrypt
= rfc4106_decrypt
,
551 .base
.cra_name
= "rfc4106(gcm(aes))",
552 .base
.cra_driver_name
= "rfc4106-gcm-aes-ce",
553 .base
.cra_priority
= 300,
554 .base
.cra_blocksize
= 1,
555 .base
.cra_ctxsize
= sizeof(struct gcm_aes_ctx
) +
557 .base
.cra_module
= THIS_MODULE
,
560 static int __init
ghash_ce_mod_init(void)
562 if (!cpu_have_named_feature(ASIMD
))
565 if (cpu_have_named_feature(PMULL
))
566 return crypto_register_aeads(gcm_aes_algs
,
567 ARRAY_SIZE(gcm_aes_algs
));
569 return crypto_register_shash(&ghash_alg
);
572 static void __exit
ghash_ce_mod_exit(void)
574 if (cpu_have_named_feature(PMULL
))
575 crypto_unregister_aeads(gcm_aes_algs
, ARRAY_SIZE(gcm_aes_algs
));
577 crypto_unregister_shash(&ghash_alg
);
580 static const struct cpu_feature __maybe_unused ghash_cpu_feature
[] = {
581 { cpu_feature(PMULL
) }, { }
583 MODULE_DEVICE_TABLE(cpu
, ghash_cpu_feature
);
585 module_init(ghash_ce_mod_init
);
586 module_exit(ghash_ce_mod_exit
);