2 * ChaCha20 256-bit cipher algorithm, RFC7539, arm64 NEON functions
4 * Copyright (C) 2016 Linaro, Ltd. <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 * ChaCha20 256-bit cipher algorithm, RFC7539, SIMD glue code
13 * Copyright (C) 2015 Martin Willi
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
21 #include <crypto/algapi.h>
22 #include <crypto/chacha20.h>
23 #include <crypto/internal/skcipher.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
27 #include <asm/hwcap.h>
30 asmlinkage
void chacha20_block_xor_neon(u32
*state
, u8
*dst
, const u8
*src
);
31 asmlinkage
void chacha20_4block_xor_neon(u32
*state
, u8
*dst
, const u8
*src
);
33 static void chacha20_doneon(u32
*state
, u8
*dst
, const u8
*src
,
36 u8 buf
[CHACHA20_BLOCK_SIZE
];
38 while (bytes
>= CHACHA20_BLOCK_SIZE
* 4) {
39 chacha20_4block_xor_neon(state
, dst
, src
);
40 bytes
-= CHACHA20_BLOCK_SIZE
* 4;
41 src
+= CHACHA20_BLOCK_SIZE
* 4;
42 dst
+= CHACHA20_BLOCK_SIZE
* 4;
45 while (bytes
>= CHACHA20_BLOCK_SIZE
) {
46 chacha20_block_xor_neon(state
, dst
, src
);
47 bytes
-= CHACHA20_BLOCK_SIZE
;
48 src
+= CHACHA20_BLOCK_SIZE
;
49 dst
+= CHACHA20_BLOCK_SIZE
;
53 memcpy(buf
, src
, bytes
);
54 chacha20_block_xor_neon(state
, buf
, buf
);
55 memcpy(dst
, buf
, bytes
);
59 static int chacha20_neon(struct skcipher_request
*req
)
61 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
62 struct chacha20_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
63 struct skcipher_walk walk
;
67 if (req
->cryptlen
<= CHACHA20_BLOCK_SIZE
)
68 return crypto_chacha20_crypt(req
);
70 err
= skcipher_walk_virt(&walk
, req
, true);
72 crypto_chacha20_init(state
, ctx
, walk
.iv
);
75 while (walk
.nbytes
> 0) {
76 unsigned int nbytes
= walk
.nbytes
;
78 if (nbytes
< walk
.total
)
79 nbytes
= round_down(nbytes
, walk
.stride
);
81 chacha20_doneon(state
, walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
,
83 err
= skcipher_walk_done(&walk
, walk
.nbytes
- nbytes
);
90 static struct skcipher_alg alg
= {
91 .base
.cra_name
= "chacha20",
92 .base
.cra_driver_name
= "chacha20-neon",
93 .base
.cra_priority
= 300,
94 .base
.cra_blocksize
= 1,
95 .base
.cra_ctxsize
= sizeof(struct chacha20_ctx
),
96 .base
.cra_module
= THIS_MODULE
,
98 .min_keysize
= CHACHA20_KEY_SIZE
,
99 .max_keysize
= CHACHA20_KEY_SIZE
,
100 .ivsize
= CHACHA20_IV_SIZE
,
101 .chunksize
= CHACHA20_BLOCK_SIZE
,
102 .walksize
= 4 * CHACHA20_BLOCK_SIZE
,
103 .setkey
= crypto_chacha20_setkey
,
104 .encrypt
= chacha20_neon
,
105 .decrypt
= chacha20_neon
,
108 static int __init
chacha20_simd_mod_init(void)
110 if (!(elf_hwcap
& HWCAP_ASIMD
))
113 return crypto_register_skcipher(&alg
);
116 static void __exit
chacha20_simd_mod_fini(void)
118 crypto_unregister_skcipher(&alg
);
121 module_init(chacha20_simd_mod_init
);
122 module_exit(chacha20_simd_mod_fini
);
124 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
125 MODULE_LICENSE("GPL v2");
126 MODULE_ALIAS_CRYPTO("chacha20");