blob: e451c3cb6a56e863e8da71170b3e676de1b0cb16 [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 Biggersdbd872a2017-11-22 11:51:36 -080012#include <asm/unaligned.h>
Martin Willic08d0e62015-06-01 13:43:56 +020013#include <crypto/algapi.h>
Martin Willi31d72472015-07-16 19:14:00 +020014#include <crypto/chacha20.h>
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000015#include <crypto/internal/skcipher.h>
16#include <linux/module.h>
Martin Willic08d0e62015-06-01 13:43:56 +020017
Martin Willic08d0e62015-06-01 13:43:56 +020018static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
19 unsigned int bytes)
20{
Eric Biggers9f480fa2017-11-22 11:51:39 -080021 u32 stream[CHACHA20_BLOCK_WORDS];
Martin Willic08d0e62015-06-01 13:43:56 +020022
23 if (dst != src)
24 memcpy(dst, src, bytes);
25
26 while (bytes >= CHACHA20_BLOCK_SIZE) {
27 chacha20_block(state, stream);
Eric Biggers9f480fa2017-11-22 11:51:39 -080028 crypto_xor(dst, (const u8 *)stream, CHACHA20_BLOCK_SIZE);
Martin Willic08d0e62015-06-01 13:43:56 +020029 bytes -= CHACHA20_BLOCK_SIZE;
30 dst += CHACHA20_BLOCK_SIZE;
31 }
32 if (bytes) {
33 chacha20_block(state, stream);
Eric Biggers9f480fa2017-11-22 11:51:39 -080034 crypto_xor(dst, (const u8 *)stream, bytes);
Martin Willic08d0e62015-06-01 13:43:56 +020035 }
36}
37
Martin Willi31d72472015-07-16 19:14:00 +020038void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
Martin Willic08d0e62015-06-01 13:43:56 +020039{
Eric Biggersecf32202017-11-22 11:51:35 -080040 state[0] = 0x61707865; /* "expa" */
41 state[1] = 0x3320646e; /* "nd 3" */
42 state[2] = 0x79622d32; /* "2-by" */
43 state[3] = 0x6b206574; /* "te k" */
Martin Willic08d0e62015-06-01 13:43:56 +020044 state[4] = ctx->key[0];
45 state[5] = ctx->key[1];
46 state[6] = ctx->key[2];
47 state[7] = ctx->key[3];
48 state[8] = ctx->key[4];
49 state[9] = ctx->key[5];
50 state[10] = ctx->key[6];
51 state[11] = ctx->key[7];
Eric Biggersdbd872a2017-11-22 11:51:36 -080052 state[12] = get_unaligned_le32(iv + 0);
53 state[13] = get_unaligned_le32(iv + 4);
54 state[14] = get_unaligned_le32(iv + 8);
55 state[15] = get_unaligned_le32(iv + 12);
Martin Willic08d0e62015-06-01 13:43:56 +020056}
Martin Willi31d72472015-07-16 19:14:00 +020057EXPORT_SYMBOL_GPL(crypto_chacha20_init);
Martin Willic08d0e62015-06-01 13:43:56 +020058
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000059int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
Martin Willic08d0e62015-06-01 13:43:56 +020060 unsigned int keysize)
61{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000062 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
Martin Willic08d0e62015-06-01 13:43:56 +020063 int i;
64
65 if (keysize != CHACHA20_KEY_SIZE)
66 return -EINVAL;
67
68 for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
Eric Biggersdbd872a2017-11-22 11:51:36 -080069 ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
Martin Willic08d0e62015-06-01 13:43:56 +020070
71 return 0;
72}
Martin Willi31d72472015-07-16 19:14:00 +020073EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
Martin Willic08d0e62015-06-01 13:43:56 +020074
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000075int crypto_chacha20_crypt(struct skcipher_request *req)
Martin Willic08d0e62015-06-01 13:43:56 +020076{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000077 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
78 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
79 struct skcipher_walk walk;
Martin Willic08d0e62015-06-01 13:43:56 +020080 u32 state[16];
81 int err;
82
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000083 err = skcipher_walk_virt(&walk, req, true);
Martin Willic08d0e62015-06-01 13:43:56 +020084
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000085 crypto_chacha20_init(state, ctx, walk.iv);
Martin Willic08d0e62015-06-01 13:43:56 +020086
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000087 while (walk.nbytes > 0) {
Ard Biesheuvel4de43722017-08-14 14:28:14 +010088 unsigned int nbytes = walk.nbytes;
89
90 if (nbytes < walk.total)
91 nbytes = round_down(nbytes, walk.stride);
92
Martin Willic08d0e62015-06-01 13:43:56 +020093 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
Ard Biesheuvel4de43722017-08-14 14:28:14 +010094 nbytes);
95 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
Martin Willic08d0e62015-06-01 13:43:56 +020096 }
97
98 return err;
99}
Martin Willi31d72472015-07-16 19:14:00 +0200100EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
Martin Willic08d0e62015-06-01 13:43:56 +0200101
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +0000102static struct skcipher_alg alg = {
103 .base.cra_name = "chacha20",
104 .base.cra_driver_name = "chacha20-generic",
105 .base.cra_priority = 100,
106 .base.cra_blocksize = 1,
107 .base.cra_ctxsize = sizeof(struct chacha20_ctx),
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +0000108 .base.cra_module = THIS_MODULE,
109
110 .min_keysize = CHACHA20_KEY_SIZE,
111 .max_keysize = CHACHA20_KEY_SIZE,
112 .ivsize = CHACHA20_IV_SIZE,
113 .chunksize = CHACHA20_BLOCK_SIZE,
114 .setkey = crypto_chacha20_setkey,
115 .encrypt = crypto_chacha20_crypt,
116 .decrypt = crypto_chacha20_crypt,
Martin Willic08d0e62015-06-01 13:43:56 +0200117};
118
119static int __init chacha20_generic_mod_init(void)
120{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +0000121 return crypto_register_skcipher(&alg);
Martin Willic08d0e62015-06-01 13:43:56 +0200122}
123
124static void __exit chacha20_generic_mod_fini(void)
125{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +0000126 crypto_unregister_skcipher(&alg);
Martin Willic08d0e62015-06-01 13:43:56 +0200127}
128
129module_init(chacha20_generic_mod_init);
130module_exit(chacha20_generic_mod_fini);
131
132MODULE_LICENSE("GPL");
133MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
134MODULE_DESCRIPTION("chacha20 cipher algorithm");
135MODULE_ALIAS_CRYPTO("chacha20");
136MODULE_ALIAS_CRYPTO("chacha20-generic");