]>
Commit | Line | Data |
---|---|---|
c08d0e64 MW |
1 | /* |
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 | ||
dbd872a1 | 12 | #include <asm/unaligned.h> |
c08d0e64 | 13 | #include <crypto/algapi.h> |
31d7247d | 14 | #include <crypto/chacha20.h> |
9ae433bc AB |
15 | #include <crypto/internal/skcipher.h> |
16 | #include <linux/module.h> | |
c08d0e64 | 17 | |
c08d0e64 MW |
18 | static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src, |
19 | unsigned int bytes) | |
20 | { | |
a5e9f557 EB |
21 | /* aligned to potentially speed up crypto_xor() */ |
22 | u8 stream[CHACHA20_BLOCK_SIZE] __aligned(sizeof(long)); | |
c08d0e64 MW |
23 | |
24 | if (dst != src) | |
25 | memcpy(dst, src, bytes); | |
26 | ||
27 | while (bytes >= CHACHA20_BLOCK_SIZE) { | |
28 | chacha20_block(state, stream); | |
a5e9f557 | 29 | crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE); |
c08d0e64 MW |
30 | bytes -= CHACHA20_BLOCK_SIZE; |
31 | dst += CHACHA20_BLOCK_SIZE; | |
32 | } | |
33 | if (bytes) { | |
34 | chacha20_block(state, stream); | |
a5e9f557 | 35 | crypto_xor(dst, stream, bytes); |
c08d0e64 MW |
36 | } |
37 | } | |
38 | ||
31d7247d | 39 | void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv) |
c08d0e64 | 40 | { |
ecf3220d EB |
41 | state[0] = 0x61707865; /* "expa" */ |
42 | state[1] = 0x3320646e; /* "nd 3" */ | |
43 | state[2] = 0x79622d32; /* "2-by" */ | |
44 | state[3] = 0x6b206574; /* "te k" */ | |
c08d0e64 MW |
45 | 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]; | |
dbd872a1 EB |
53 | 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); | |
c08d0e64 | 57 | } |
31d7247d | 58 | EXPORT_SYMBOL_GPL(crypto_chacha20_init); |
c08d0e64 | 59 | |
9ae433bc | 60 | int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, |
c08d0e64 MW |
61 | unsigned int keysize) |
62 | { | |
9ae433bc | 63 | struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm); |
c08d0e64 MW |
64 | int i; |
65 | ||
66 | if (keysize != CHACHA20_KEY_SIZE) | |
67 | return -EINVAL; | |
68 | ||
69 | for (i = 0; i < ARRAY_SIZE(ctx->key); i++) | |
dbd872a1 | 70 | ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32)); |
c08d0e64 MW |
71 | |
72 | return 0; | |
73 | } | |
31d7247d | 74 | EXPORT_SYMBOL_GPL(crypto_chacha20_setkey); |
c08d0e64 | 75 | |
9ae433bc | 76 | int crypto_chacha20_crypt(struct skcipher_request *req) |
c08d0e64 | 77 | { |
9ae433bc AB |
78 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
79 | struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm); | |
80 | struct skcipher_walk walk; | |
c08d0e64 MW |
81 | u32 state[16]; |
82 | int err; | |
83 | ||
9ae433bc | 84 | err = skcipher_walk_virt(&walk, req, true); |
c08d0e64 | 85 | |
9ae433bc | 86 | crypto_chacha20_init(state, ctx, walk.iv); |
c08d0e64 | 87 | |
9ae433bc | 88 | while (walk.nbytes > 0) { |
4de43726 AB |
89 | unsigned int nbytes = walk.nbytes; |
90 | ||
91 | if (nbytes < walk.total) | |
92 | nbytes = round_down(nbytes, walk.stride); | |
93 | ||
c08d0e64 | 94 | chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
4de43726 AB |
95 | nbytes); |
96 | err = skcipher_walk_done(&walk, walk.nbytes - nbytes); | |
c08d0e64 MW |
97 | } |
98 | ||
99 | return err; | |
100 | } | |
31d7247d | 101 | EXPORT_SYMBOL_GPL(crypto_chacha20_crypt); |
c08d0e64 | 102 | |
9ae433bc AB |
103 | static 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), | |
9ae433bc AB |
109 | .base.cra_module = THIS_MODULE, |
110 | ||
111 | .min_keysize = CHACHA20_KEY_SIZE, | |
112 | .max_keysize = CHACHA20_KEY_SIZE, | |
113 | .ivsize = CHACHA20_IV_SIZE, | |
114 | .chunksize = CHACHA20_BLOCK_SIZE, | |
115 | .setkey = crypto_chacha20_setkey, | |
116 | .encrypt = crypto_chacha20_crypt, | |
117 | .decrypt = crypto_chacha20_crypt, | |
c08d0e64 MW |
118 | }; |
119 | ||
120 | static int __init chacha20_generic_mod_init(void) | |
121 | { | |
9ae433bc | 122 | return crypto_register_skcipher(&alg); |
c08d0e64 MW |
123 | } |
124 | ||
125 | static void __exit chacha20_generic_mod_fini(void) | |
126 | { | |
9ae433bc | 127 | crypto_unregister_skcipher(&alg); |
c08d0e64 MW |
128 | } |
129 | ||
130 | module_init(chacha20_generic_mod_init); | |
131 | module_exit(chacha20_generic_mod_fini); | |
132 | ||
133 | MODULE_LICENSE("GPL"); | |
134 | MODULE_AUTHOR("Martin Willi <[email protected]>"); | |
135 | MODULE_DESCRIPTION("chacha20 cipher algorithm"); | |
136 | MODULE_ALIAS_CRYPTO("chacha20"); | |
137 | MODULE_ALIAS_CRYPTO("chacha20-generic"); |