blob: 56603afbf6bd0a200291c6b5475b0d6c41812485 [file] [log] [blame]
Martin Willic08d0e62015-06-01 13:43:56 +02001/*
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>
13#include <linux/crypto.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
Martin Willi31d72472015-07-16 19:14:00 +020016#include <crypto/chacha20.h>
Martin Willic08d0e62015-06-01 13:43:56 +020017
Martin Willic08d0e62015-06-01 13:43:56 +020018static inline u32 le32_to_cpuvp(const void *p)
19{
20 return le32_to_cpup(p);
21}
22
Martin Willic08d0e62015-06-01 13:43:56 +020023static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
24 unsigned int bytes)
25{
Eric Biggers70e97552018-09-11 20:05:10 -070026 /* aligned to potentially speed up crypto_xor() */
27 u8 stream[CHACHA20_BLOCK_SIZE] __aligned(sizeof(long));
Martin Willic08d0e62015-06-01 13:43:56 +020028
29 if (dst != src)
30 memcpy(dst, src, bytes);
31
32 while (bytes >= CHACHA20_BLOCK_SIZE) {
33 chacha20_block(state, stream);
Eric Biggers70e97552018-09-11 20:05:10 -070034 crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE);
Martin Willic08d0e62015-06-01 13:43:56 +020035 bytes -= CHACHA20_BLOCK_SIZE;
36 dst += CHACHA20_BLOCK_SIZE;
37 }
38 if (bytes) {
39 chacha20_block(state, stream);
Eric Biggers70e97552018-09-11 20:05:10 -070040 crypto_xor(dst, stream, bytes);
Martin Willic08d0e62015-06-01 13:43:56 +020041 }
42}
43
Martin Willi31d72472015-07-16 19:14:00 +020044void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
Martin Willic08d0e62015-06-01 13:43:56 +020045{
46 static const char constant[16] = "expand 32-byte k";
47
48 state[0] = le32_to_cpuvp(constant + 0);
49 state[1] = le32_to_cpuvp(constant + 4);
50 state[2] = le32_to_cpuvp(constant + 8);
51 state[3] = le32_to_cpuvp(constant + 12);
52 state[4] = ctx->key[0];
53 state[5] = ctx->key[1];
54 state[6] = ctx->key[2];
55 state[7] = ctx->key[3];
56 state[8] = ctx->key[4];
57 state[9] = ctx->key[5];
58 state[10] = ctx->key[6];
59 state[11] = ctx->key[7];
60 state[12] = le32_to_cpuvp(iv + 0);
61 state[13] = le32_to_cpuvp(iv + 4);
62 state[14] = le32_to_cpuvp(iv + 8);
63 state[15] = le32_to_cpuvp(iv + 12);
64}
Martin Willi31d72472015-07-16 19:14:00 +020065EXPORT_SYMBOL_GPL(crypto_chacha20_init);
Martin Willic08d0e62015-06-01 13:43:56 +020066
Martin Willi31d72472015-07-16 19:14:00 +020067int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
Martin Willic08d0e62015-06-01 13:43:56 +020068 unsigned int keysize)
69{
70 struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
71 int i;
72
73 if (keysize != CHACHA20_KEY_SIZE)
74 return -EINVAL;
75
76 for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
77 ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32));
78
79 return 0;
80}
Martin Willi31d72472015-07-16 19:14:00 +020081EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
Martin Willic08d0e62015-06-01 13:43:56 +020082
Martin Willi31d72472015-07-16 19:14:00 +020083int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
Martin Willic08d0e62015-06-01 13:43:56 +020084 struct scatterlist *src, unsigned int nbytes)
85{
86 struct blkcipher_walk walk;
87 u32 state[16];
88 int err;
89
90 blkcipher_walk_init(&walk, dst, src, nbytes);
91 err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
92
Martin Willi31d72472015-07-16 19:14:00 +020093 crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
Martin Willic08d0e62015-06-01 13:43:56 +020094
95 while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
96 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
97 rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
98 err = blkcipher_walk_done(desc, &walk,
99 walk.nbytes % CHACHA20_BLOCK_SIZE);
100 }
101
102 if (walk.nbytes) {
103 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
104 walk.nbytes);
105 err = blkcipher_walk_done(desc, &walk, 0);
106 }
107
108 return err;
109}
Martin Willi31d72472015-07-16 19:14:00 +0200110EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
Martin Willic08d0e62015-06-01 13:43:56 +0200111
112static struct crypto_alg alg = {
113 .cra_name = "chacha20",
114 .cra_driver_name = "chacha20-generic",
115 .cra_priority = 100,
116 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
117 .cra_blocksize = 1,
118 .cra_type = &crypto_blkcipher_type,
119 .cra_ctxsize = sizeof(struct chacha20_ctx),
120 .cra_alignmask = sizeof(u32) - 1,
121 .cra_module = THIS_MODULE,
122 .cra_u = {
123 .blkcipher = {
124 .min_keysize = CHACHA20_KEY_SIZE,
125 .max_keysize = CHACHA20_KEY_SIZE,
Martin Willi31d72472015-07-16 19:14:00 +0200126 .ivsize = CHACHA20_IV_SIZE,
Martin Willic08d0e62015-06-01 13:43:56 +0200127 .geniv = "seqiv",
Martin Willi31d72472015-07-16 19:14:00 +0200128 .setkey = crypto_chacha20_setkey,
129 .encrypt = crypto_chacha20_crypt,
130 .decrypt = crypto_chacha20_crypt,
Martin Willic08d0e62015-06-01 13:43:56 +0200131 },
132 },
133};
134
135static int __init chacha20_generic_mod_init(void)
136{
137 return crypto_register_alg(&alg);
138}
139
140static void __exit chacha20_generic_mod_fini(void)
141{
142 crypto_unregister_alg(&alg);
143}
144
145module_init(chacha20_generic_mod_init);
146module_exit(chacha20_generic_mod_fini);
147
148MODULE_LICENSE("GPL");
149MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
150MODULE_DESCRIPTION("chacha20 cipher algorithm");
151MODULE_ALIAS_CRYPTO("chacha20");
152MODULE_ALIAS_CRYPTO("chacha20-generic");