chacha_generic.c (95d002e0a34cb0f238abb39987f9980f325d8332) chacha_generic.c (c4741b23059794bd99beef0f700103b0d983b3fd)
1/*
2 * ChaCha and XChaCha stream ciphers, including ChaCha20 (RFC7539)
3 *
4 * Copyright (C) 2015 Martin Willi
5 * Copyright (C) 2018 Google LLC
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by

--- 8 unchanged lines hidden (view full) ---

17#include <linux/module.h>
18
19static void chacha_docrypt(u32 *state, u8 *dst, const u8 *src,
20 unsigned int bytes, int nrounds)
21{
22 /* aligned to potentially speed up crypto_xor() */
23 u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
24
1/*
2 * ChaCha and XChaCha stream ciphers, including ChaCha20 (RFC7539)
3 *
4 * Copyright (C) 2015 Martin Willi
5 * Copyright (C) 2018 Google LLC
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by

--- 8 unchanged lines hidden (view full) ---

17#include <linux/module.h>
18
19static void chacha_docrypt(u32 *state, u8 *dst, const u8 *src,
20 unsigned int bytes, int nrounds)
21{
22 /* aligned to potentially speed up crypto_xor() */
23 u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long));
24
25 if (dst != src)
26 memcpy(dst, src, bytes);
27
28 while (bytes >= CHACHA_BLOCK_SIZE) {
29 chacha_block(state, stream, nrounds);
25 while (bytes >= CHACHA_BLOCK_SIZE) {
26 chacha_block(state, stream, nrounds);
30 crypto_xor(dst, stream, CHACHA_BLOCK_SIZE);
27 crypto_xor_cpy(dst, src, stream, CHACHA_BLOCK_SIZE);
31 bytes -= CHACHA_BLOCK_SIZE;
32 dst += CHACHA_BLOCK_SIZE;
28 bytes -= CHACHA_BLOCK_SIZE;
29 dst += CHACHA_BLOCK_SIZE;
30 src += CHACHA_BLOCK_SIZE;
33 }
34 if (bytes) {
35 chacha_block(state, stream, nrounds);
31 }
32 if (bytes) {
33 chacha_block(state, stream, nrounds);
36 crypto_xor(dst, stream, bytes);
34 crypto_xor_cpy(dst, src, stream, bytes);
37 }
38}
39
40static int chacha_stream_xor(struct skcipher_request *req,
41 struct chacha_ctx *ctx, u8 *iv)
42{
43 struct skcipher_walk walk;
44 u32 state[16];
45 int err;
46
47 err = skcipher_walk_virt(&walk, req, false);
48
49 crypto_chacha_init(state, ctx, iv);
50
51 while (walk.nbytes > 0) {
52 unsigned int nbytes = walk.nbytes;
53
54 if (nbytes < walk.total)
35 }
36}
37
38static int chacha_stream_xor(struct skcipher_request *req,
39 struct chacha_ctx *ctx, u8 *iv)
40{
41 struct skcipher_walk walk;
42 u32 state[16];
43 int err;
44
45 err = skcipher_walk_virt(&walk, req, false);
46
47 crypto_chacha_init(state, ctx, iv);
48
49 while (walk.nbytes > 0) {
50 unsigned int nbytes = walk.nbytes;
51
52 if (nbytes < walk.total)
55 nbytes = round_down(nbytes, walk.stride);
53 nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE);
56
57 chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
58 nbytes, ctx->nrounds);
59 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
60 }
61
62 return err;
63}

--- 134 unchanged lines hidden (view full) ---

198 return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
199}
200
201static void __exit chacha_generic_mod_fini(void)
202{
203 crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
204}
205
54
55 chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
56 nbytes, ctx->nrounds);
57 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
58 }
59
60 return err;
61}

--- 134 unchanged lines hidden (view full) ---

196 return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
197}
198
199static void __exit chacha_generic_mod_fini(void)
200{
201 crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
202}
203
206module_init(chacha_generic_mod_init);
204subsys_initcall(chacha_generic_mod_init);
207module_exit(chacha_generic_mod_fini);
208
209MODULE_LICENSE("GPL");
210MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
211MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)");
212MODULE_ALIAS_CRYPTO("chacha20");
213MODULE_ALIAS_CRYPTO("chacha20-generic");
214MODULE_ALIAS_CRYPTO("xchacha20");
215MODULE_ALIAS_CRYPTO("xchacha20-generic");
216MODULE_ALIAS_CRYPTO("xchacha12");
217MODULE_ALIAS_CRYPTO("xchacha12-generic");
205module_exit(chacha_generic_mod_fini);
206
207MODULE_LICENSE("GPL");
208MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
209MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)");
210MODULE_ALIAS_CRYPTO("chacha20");
211MODULE_ALIAS_CRYPTO("chacha20-generic");
212MODULE_ALIAS_CRYPTO("xchacha20");
213MODULE_ALIAS_CRYPTO("xchacha20-generic");
214MODULE_ALIAS_CRYPTO("xchacha12");
215MODULE_ALIAS_CRYPTO("xchacha12-generic");