blob: 8b3c04d625c3bb0ee667a8a61a0c1e4499b0b486 [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>
Martin Willi31d72472015-07-16 19:14:00 +020013#include <crypto/chacha20.h>
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000014#include <crypto/internal/skcipher.h>
15#include <linux/module.h>
Martin Willic08d0e62015-06-01 13:43:56 +020016
Martin Willic08d0e62015-06-01 13:43:56 +020017static inline u32 le32_to_cpuvp(const void *p)
18{
19 return le32_to_cpup(p);
20}
21
Martin Willic08d0e62015-06-01 13:43:56 +020022static 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
Martin Willi31d72472015-07-16 19:14:00 +020042void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
Martin Willic08d0e62015-06-01 13:43:56 +020043{
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}
Martin Willi31d72472015-07-16 19:14:00 +020063EXPORT_SYMBOL_GPL(crypto_chacha20_init);
Martin Willic08d0e62015-06-01 13:43:56 +020064
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000065int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
Martin Willic08d0e62015-06-01 13:43:56 +020066 unsigned int keysize)
67{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000068 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
Martin Willic08d0e62015-06-01 13:43:56 +020069 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}
Martin Willi31d72472015-07-16 19:14:00 +020079EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
Martin Willic08d0e62015-06-01 13:43:56 +020080
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000081int crypto_chacha20_crypt(struct skcipher_request *req)
Martin Willic08d0e62015-06-01 13:43:56 +020082{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000083 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
84 struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
85 struct skcipher_walk walk;
Martin Willic08d0e62015-06-01 13:43:56 +020086 u32 state[16];
87 int err;
88
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000089 err = skcipher_walk_virt(&walk, req, true);
Martin Willic08d0e62015-06-01 13:43:56 +020090
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000091 crypto_chacha20_init(state, ctx, walk.iv);
Martin Willic08d0e62015-06-01 13:43:56 +020092
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000093 while (walk.nbytes > 0) {
Martin Willic08d0e62015-06-01 13:43:56 +020094 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
95 walk.nbytes);
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +000096 err = skcipher_walk_done(&walk, 0);
Martin Willic08d0e62015-06-01 13:43:56 +020097 }
98
99 return err;
100}
Martin Willi31d72472015-07-16 19:14:00 +0200101EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
Martin Willic08d0e62015-06-01 13:43:56 +0200102
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +0000103static struct skcipher_alg alg = {
104 .base.cra_name = "chacha20",
105 .base.cra_driver_name = "chacha20-generic",
106 .base.cra_priority = 100,
107 .base.cra_blocksize = 1,
108 .base.cra_ctxsize = sizeof(struct chacha20_ctx),
109 .base.cra_alignmask = sizeof(u32) - 1,
110 .base.cra_module = THIS_MODULE,
111
112 .min_keysize = CHACHA20_KEY_SIZE,
113 .max_keysize = CHACHA20_KEY_SIZE,
114 .ivsize = CHACHA20_IV_SIZE,
115 .chunksize = CHACHA20_BLOCK_SIZE,
116 .setkey = crypto_chacha20_setkey,
117 .encrypt = crypto_chacha20_crypt,
118 .decrypt = crypto_chacha20_crypt,
Martin Willic08d0e62015-06-01 13:43:56 +0200119};
120
121static int __init chacha20_generic_mod_init(void)
122{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +0000123 return crypto_register_skcipher(&alg);
Martin Willic08d0e62015-06-01 13:43:56 +0200124}
125
126static void __exit chacha20_generic_mod_fini(void)
127{
Ard Biesheuvel9ae433b2016-12-09 14:33:51 +0000128 crypto_unregister_skcipher(&alg);
Martin Willic08d0e62015-06-01 13:43:56 +0200129}
130
131module_init(chacha20_generic_mod_init);
132module_exit(chacha20_generic_mod_fini);
133
134MODULE_LICENSE("GPL");
135MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
136MODULE_DESCRIPTION("chacha20 cipher algorithm");
137MODULE_ALIAS_CRYPTO("chacha20");
138MODULE_ALIAS_CRYPTO("chacha20-generic");