blob: 1a48d3acaf031ad13b55475d0866f8948a4760b6 [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
Eric Biggers930b1cd2017-11-22 11:51:36 -080012#include <asm/unaligned.h>
Martin Willic08d0e62015-06-01 13:43:56 +020013#include <crypto/algapi.h>
14#include <linux/crypto.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
Martin Willi31d72472015-07-16 19:14:00 +020017#include <crypto/chacha20.h>
Martin Willic08d0e62015-06-01 13:43:56 +020018
Martin Willic08d0e62015-06-01 13:43:56 +020019static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
20 unsigned int bytes)
21{
Eric Biggers12b2fb032017-11-22 11:51:39 -080022 u32 stream[CHACHA20_BLOCK_WORDS];
Martin Willic08d0e62015-06-01 13:43:56 +020023
24 if (dst != src)
25 memcpy(dst, src, bytes);
26
27 while (bytes >= CHACHA20_BLOCK_SIZE) {
28 chacha20_block(state, stream);
Eric Biggers12b2fb032017-11-22 11:51:39 -080029 crypto_xor(dst, (const u8 *)stream, CHACHA20_BLOCK_SIZE);
Martin Willic08d0e62015-06-01 13:43:56 +020030 bytes -= CHACHA20_BLOCK_SIZE;
31 dst += CHACHA20_BLOCK_SIZE;
32 }
33 if (bytes) {
34 chacha20_block(state, stream);
Eric Biggers12b2fb032017-11-22 11:51:39 -080035 crypto_xor(dst, (const u8 *)stream, bytes);
Martin Willic08d0e62015-06-01 13:43:56 +020036 }
37}
38
Martin Willi31d72472015-07-16 19:14:00 +020039void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
Martin Willic08d0e62015-06-01 13:43:56 +020040{
Eric Biggers73ae7b82017-11-22 11:51:35 -080041 state[0] = 0x61707865; /* "expa" */
42 state[1] = 0x3320646e; /* "nd 3" */
43 state[2] = 0x79622d32; /* "2-by" */
44 state[3] = 0x6b206574; /* "te k" */
Martin Willic08d0e62015-06-01 13:43:56 +020045 state[4] = ctx->key[0];
46 state[5] = ctx->key[1];
47 state[6] = ctx->key[2];
48 state[7] = ctx->key[3];
49 state[8] = ctx->key[4];
50 state[9] = ctx->key[5];
51 state[10] = ctx->key[6];
52 state[11] = ctx->key[7];
Eric Biggers930b1cd2017-11-22 11:51:36 -080053 state[12] = get_unaligned_le32(iv + 0);
54 state[13] = get_unaligned_le32(iv + 4);
55 state[14] = get_unaligned_le32(iv + 8);
56 state[15] = get_unaligned_le32(iv + 12);
Martin Willic08d0e62015-06-01 13:43:56 +020057}
Martin Willi31d72472015-07-16 19:14:00 +020058EXPORT_SYMBOL_GPL(crypto_chacha20_init);
Martin Willic08d0e62015-06-01 13:43:56 +020059
Martin Willi31d72472015-07-16 19:14:00 +020060int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key,
Martin Willic08d0e62015-06-01 13:43:56 +020061 unsigned int keysize)
62{
63 struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm);
64 int i;
65
66 if (keysize != CHACHA20_KEY_SIZE)
67 return -EINVAL;
68
69 for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
Eric Biggers930b1cd2017-11-22 11:51:36 -080070 ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
Martin Willic08d0e62015-06-01 13:43:56 +020071
72 return 0;
73}
Martin Willi31d72472015-07-16 19:14:00 +020074EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
Martin Willic08d0e62015-06-01 13:43:56 +020075
Martin Willi31d72472015-07-16 19:14:00 +020076int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
Martin Willic08d0e62015-06-01 13:43:56 +020077 struct scatterlist *src, unsigned int nbytes)
78{
79 struct blkcipher_walk walk;
80 u32 state[16];
81 int err;
82
83 blkcipher_walk_init(&walk, dst, src, nbytes);
84 err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE);
85
Martin Willi31d72472015-07-16 19:14:00 +020086 crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv);
Martin Willic08d0e62015-06-01 13:43:56 +020087
88 while (walk.nbytes >= CHACHA20_BLOCK_SIZE) {
89 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
90 rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE));
91 err = blkcipher_walk_done(desc, &walk,
92 walk.nbytes % CHACHA20_BLOCK_SIZE);
93 }
94
95 if (walk.nbytes) {
96 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
97 walk.nbytes);
98 err = blkcipher_walk_done(desc, &walk, 0);
99 }
100
101 return err;
102}
Martin Willi31d72472015-07-16 19:14:00 +0200103EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
Martin Willic08d0e62015-06-01 13:43:56 +0200104
105static struct crypto_alg alg = {
106 .cra_name = "chacha20",
107 .cra_driver_name = "chacha20-generic",
108 .cra_priority = 100,
109 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
110 .cra_blocksize = 1,
111 .cra_type = &crypto_blkcipher_type,
112 .cra_ctxsize = sizeof(struct chacha20_ctx),
113 .cra_alignmask = sizeof(u32) - 1,
114 .cra_module = THIS_MODULE,
115 .cra_u = {
116 .blkcipher = {
117 .min_keysize = CHACHA20_KEY_SIZE,
118 .max_keysize = CHACHA20_KEY_SIZE,
Martin Willi31d72472015-07-16 19:14:00 +0200119 .ivsize = CHACHA20_IV_SIZE,
Martin Willic08d0e62015-06-01 13:43:56 +0200120 .geniv = "seqiv",
Martin Willi31d72472015-07-16 19:14:00 +0200121 .setkey = crypto_chacha20_setkey,
122 .encrypt = crypto_chacha20_crypt,
123 .decrypt = crypto_chacha20_crypt,
Martin Willic08d0e62015-06-01 13:43:56 +0200124 },
125 },
126};
127
128static int __init chacha20_generic_mod_init(void)
129{
130 return crypto_register_alg(&alg);
131}
132
133static void __exit chacha20_generic_mod_fini(void)
134{
135 crypto_unregister_alg(&alg);
136}
137
138module_init(chacha20_generic_mod_init);
139module_exit(chacha20_generic_mod_fini);
140
141MODULE_LICENSE("GPL");
142MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
143MODULE_DESCRIPTION("chacha20 cipher algorithm");
144MODULE_ALIAS_CRYPTO("chacha20");
145MODULE_ALIAS_CRYPTO("chacha20-generic");