2 * ChaCha20 256-bit cipher algorithm, RFC7539
4 * Copyright (C) 2015 Martin Willi
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.
12 #include <crypto/algapi.h>
13 #include <crypto/chacha20.h>
14 #include <crypto/internal/skcipher.h>
15 #include <linux/module.h>
17 static inline u32 le32_to_cpuvp(const void *p)
19 return le32_to_cpup(p);
22 static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
25 /* aligned to potentially speed up crypto_xor() */
26 u8 stream[CHACHA20_BLOCK_SIZE] __aligned(sizeof(long));
29 memcpy(dst, src, bytes);
31 while (bytes >= CHACHA20_BLOCK_SIZE) {
32 chacha20_block(state, stream);
33 crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE);
34 bytes -= CHACHA20_BLOCK_SIZE;
35 dst += CHACHA20_BLOCK_SIZE;
38 chacha20_block(state, stream);
39 crypto_xor(dst, stream, bytes);
43 void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
45 static const char constant[16] = "expand 32-byte k";
47 state[0] = le32_to_cpuvp(constant + 0);
48 state[1] = le32_to_cpuvp(constant + 4);
49 state[2] = le32_to_cpuvp(constant + 8);
50 state[3] = le32_to_cpuvp(constant + 12);
51 state[4] = ctx->key[0];
52 state[5] = ctx->key[1];
53 state[6] = ctx->key[2];
54 state[7] = ctx->key[3];
55 state[8] = ctx->key[4];
56 state[9] = ctx->key[5];
57 state[10] = ctx->key[6];
58 state[11] = ctx->key[7];
59 state[12] = le32_to_cpuvp(iv + 0);
60 state[13] = le32_to_cpuvp(iv + 4);
61 state[14] = le32_to_cpuvp(iv + 8);
62 state[15] = le32_to_cpuvp(iv + 12);
64 EXPORT_SYMBOL_GPL(crypto_chacha20_init);
66 int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
69 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
72 if (keysize != CHACHA20_KEY_SIZE)
75 for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
76 ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32));
80 EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
82 int crypto_chacha20_crypt(struct skcipher_request *req)
84 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
85 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
86 struct skcipher_walk walk;
90 err = skcipher_walk_virt(&walk, req, true);
92 crypto_chacha20_init(state, ctx, walk.iv);
94 while (walk.nbytes > 0) {
95 unsigned int nbytes = walk.nbytes;
97 if (nbytes < walk.total)
98 nbytes = round_down(nbytes, walk.stride);
100 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
102 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
107 EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
109 static struct skcipher_alg alg = {
110 .base.cra_name = "chacha20",
111 .base.cra_driver_name = "chacha20-generic",
112 .base.cra_priority = 100,
113 .base.cra_blocksize = 1,
114 .base.cra_ctxsize = sizeof(struct chacha20_ctx),
115 .base.cra_alignmask = sizeof(u32) - 1,
116 .base.cra_module = THIS_MODULE,
118 .min_keysize = CHACHA20_KEY_SIZE,
119 .max_keysize = CHACHA20_KEY_SIZE,
120 .ivsize = CHACHA20_IV_SIZE,
121 .chunksize = CHACHA20_BLOCK_SIZE,
122 .setkey = crypto_chacha20_setkey,
123 .encrypt = crypto_chacha20_crypt,
124 .decrypt = crypto_chacha20_crypt,
127 static int __init chacha20_generic_mod_init(void)
129 return crypto_register_skcipher(&alg);
132 static void __exit chacha20_generic_mod_fini(void)
134 crypto_unregister_skcipher(&alg);
137 module_init(chacha20_generic_mod_init);
138 module_exit(chacha20_generic_mod_fini);
140 MODULE_LICENSE("GPL");
141 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
142 MODULE_DESCRIPTION("chacha20 cipher algorithm");
143 MODULE_ALIAS_CRYPTO("chacha20");
144 MODULE_ALIAS_CRYPTO("chacha20-generic");