Merge 4.14.63 into android-4.14-p
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / crypto / chacha20_generic.c
CommitLineData
c08d0e64
MW
1/*
2 * ChaCha20 256-bit cipher algorithm, RFC7539
3 *
4 * Copyright (C) 2015 Martin Willi
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <crypto/algapi.h>
31d7247d 13#include <crypto/chacha20.h>
9ae433bc
AB
14#include <crypto/internal/skcipher.h>
15#include <linux/module.h>
c08d0e64 16
c08d0e64
MW
17static inline u32 le32_to_cpuvp(const void *p)
18{
19 return le32_to_cpup(p);
20}
21
c08d0e64
MW
22static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
23 unsigned int bytes)
24{
25 u8 stream[CHACHA20_BLOCK_SIZE];
26
27 if (dst != src)
28 memcpy(dst, src, bytes);
29
30 while (bytes >= CHACHA20_BLOCK_SIZE) {
31 chacha20_block(state, stream);
32 crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE);
33 bytes -= CHACHA20_BLOCK_SIZE;
34 dst += CHACHA20_BLOCK_SIZE;
35 }
36 if (bytes) {
37 chacha20_block(state, stream);
38 crypto_xor(dst, stream, bytes);
39 }
40}
41
31d7247d 42void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
c08d0e64
MW
43{
44 static const char constant[16] = "expand 32-byte k";
45
46 state[0] = le32_to_cpuvp(constant + 0);
47 state[1] = le32_to_cpuvp(constant + 4);
48 state[2] = le32_to_cpuvp(constant + 8);
49 state[3] = le32_to_cpuvp(constant + 12);
50 state[4] = ctx->key[0];
51 state[5] = ctx->key[1];
52 state[6] = ctx->key[2];
53 state[7] = ctx->key[3];
54 state[8] = ctx->key[4];
55 state[9] = ctx->key[5];
56 state[10] = ctx->key[6];
57 state[11] = ctx->key[7];
58 state[12] = le32_to_cpuvp(iv + 0);
59 state[13] = le32_to_cpuvp(iv + 4);
60 state[14] = le32_to_cpuvp(iv + 8);
61 state[15] = le32_to_cpuvp(iv + 12);
62}
31d7247d 63EXPORT_SYMBOL_GPL(crypto_chacha20_init);
c08d0e64 64
9ae433bc 65int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
c08d0e64
MW
66 unsigned int keysize)
67{
9ae433bc 68 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
c08d0e64
MW
69 int i;
70
71 if (keysize != CHACHA20_KEY_SIZE)
72 return -EINVAL;
73
74 for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
75 ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32));
76
77 return 0;
78}
31d7247d 79EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
c08d0e64 80
9ae433bc 81int crypto_chacha20_crypt(struct skcipher_request *req)
c08d0e64 82{
9ae433bc
AB
83 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
84 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
85 struct skcipher_walk walk;
c08d0e64
MW
86 u32 state[16];
87 int err;
88
9ae433bc 89 err = skcipher_walk_virt(&walk, req, true);
c08d0e64 90
9ae433bc 91 crypto_chacha20_init(state, ctx, walk.iv);
c08d0e64 92
9ae433bc 93 while (walk.nbytes > 0) {
4de43726
AB
94 unsigned int nbytes = walk.nbytes;
95
96 if (nbytes < walk.total)
97 nbytes = round_down(nbytes, walk.stride);
98
c08d0e64 99 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
4de43726
AB
100 nbytes);
101 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
c08d0e64
MW
102 }
103
104 return err;
105}
31d7247d 106EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
c08d0e64 107
9ae433bc
AB
108static struct skcipher_alg alg = {
109 .base.cra_name = "chacha20",
110 .base.cra_driver_name = "chacha20-generic",
111 .base.cra_priority = 100,
112 .base.cra_blocksize = 1,
113 .base.cra_ctxsize = sizeof(struct chacha20_ctx),
114 .base.cra_alignmask = sizeof(u32) - 1,
115 .base.cra_module = THIS_MODULE,
116
117 .min_keysize = CHACHA20_KEY_SIZE,
118 .max_keysize = CHACHA20_KEY_SIZE,
119 .ivsize = CHACHA20_IV_SIZE,
120 .chunksize = CHACHA20_BLOCK_SIZE,
121 .setkey = crypto_chacha20_setkey,
122 .encrypt = crypto_chacha20_crypt,
123 .decrypt = crypto_chacha20_crypt,
c08d0e64
MW
124};
125
126static int __init chacha20_generic_mod_init(void)
127{
9ae433bc 128 return crypto_register_skcipher(&alg);
c08d0e64
MW
129}
130
131static void __exit chacha20_generic_mod_fini(void)
132{
9ae433bc 133 crypto_unregister_skcipher(&alg);
c08d0e64
MW
134}
135
136module_init(chacha20_generic_mod_init);
137module_exit(chacha20_generic_mod_fini);
138
139MODULE_LICENSE("GPL");
140MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
141MODULE_DESCRIPTION("chacha20 cipher algorithm");
142MODULE_ALIAS_CRYPTO("chacha20");
143MODULE_ALIAS_CRYPTO("chacha20-generic");