Eric Biggers | b8181f3 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 1 | /* |
Eric Biggers | bb31ed5 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 2 | * ChaCha and XChaCha stream ciphers, including ChaCha20 (RFC7539) |
Eric Biggers | b8181f3 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2015 Martin Willi |
| 5 | * Copyright (C) 2018 Google LLC |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <asm/unaligned.h> |
| 14 | #include <crypto/algapi.h> |
| 15 | #include <linux/crypto.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <crypto/chacha.h> |
| 19 | |
| 20 | static void chacha_docrypt(u32 *state, u8 *dst, const u8 *src, |
| 21 | unsigned int bytes, int nrounds) |
| 22 | { |
| 23 | /* aligned to potentially speed up crypto_xor() */ |
| 24 | u8 stream[CHACHA_BLOCK_SIZE] __aligned(sizeof(long)); |
| 25 | |
| 26 | if (dst != src) |
| 27 | memcpy(dst, src, bytes); |
| 28 | |
| 29 | while (bytes >= CHACHA_BLOCK_SIZE) { |
| 30 | chacha_block(state, stream, nrounds); |
| 31 | crypto_xor(dst, stream, CHACHA_BLOCK_SIZE); |
| 32 | bytes -= CHACHA_BLOCK_SIZE; |
| 33 | dst += CHACHA_BLOCK_SIZE; |
| 34 | } |
| 35 | if (bytes) { |
| 36 | chacha_block(state, stream, nrounds); |
| 37 | crypto_xor(dst, stream, bytes); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | static int chacha_stream_xor(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 42 | struct scatterlist *src, unsigned int nbytes, |
| 43 | struct chacha_ctx *ctx, u8 *iv) |
| 44 | { |
| 45 | struct blkcipher_walk walk; |
| 46 | u32 state[16]; |
| 47 | int err; |
| 48 | |
| 49 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 50 | err = blkcipher_walk_virt_block(desc, &walk, CHACHA_BLOCK_SIZE); |
| 51 | |
| 52 | crypto_chacha_init(state, ctx, iv); |
| 53 | |
| 54 | while (walk.nbytes >= CHACHA_BLOCK_SIZE) { |
| 55 | chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
| 56 | rounddown(walk.nbytes, CHACHA_BLOCK_SIZE), |
| 57 | ctx->nrounds); |
| 58 | err = blkcipher_walk_done(desc, &walk, |
| 59 | walk.nbytes % CHACHA_BLOCK_SIZE); |
| 60 | } |
| 61 | |
| 62 | if (walk.nbytes) { |
| 63 | chacha_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
| 64 | walk.nbytes, ctx->nrounds); |
| 65 | err = blkcipher_walk_done(desc, &walk, 0); |
| 66 | } |
| 67 | |
| 68 | return err; |
| 69 | } |
| 70 | |
| 71 | void crypto_chacha_init(u32 *state, struct chacha_ctx *ctx, u8 *iv) |
| 72 | { |
| 73 | state[0] = 0x61707865; /* "expa" */ |
| 74 | state[1] = 0x3320646e; /* "nd 3" */ |
| 75 | state[2] = 0x79622d32; /* "2-by" */ |
| 76 | state[3] = 0x6b206574; /* "te k" */ |
| 77 | state[4] = ctx->key[0]; |
| 78 | state[5] = ctx->key[1]; |
| 79 | state[6] = ctx->key[2]; |
| 80 | state[7] = ctx->key[3]; |
| 81 | state[8] = ctx->key[4]; |
| 82 | state[9] = ctx->key[5]; |
| 83 | state[10] = ctx->key[6]; |
| 84 | state[11] = ctx->key[7]; |
| 85 | state[12] = get_unaligned_le32(iv + 0); |
| 86 | state[13] = get_unaligned_le32(iv + 4); |
| 87 | state[14] = get_unaligned_le32(iv + 8); |
| 88 | state[15] = get_unaligned_le32(iv + 12); |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(crypto_chacha_init); |
| 91 | |
| 92 | static int chacha_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 93 | unsigned int keysize, int nrounds) |
| 94 | { |
| 95 | struct chacha_ctx *ctx = crypto_tfm_ctx(tfm); |
| 96 | int i; |
| 97 | |
| 98 | if (keysize != CHACHA_KEY_SIZE) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | for (i = 0; i < ARRAY_SIZE(ctx->key); i++) |
| 102 | ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32)); |
| 103 | |
| 104 | ctx->nrounds = nrounds; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 109 | unsigned int keysize) |
| 110 | { |
| 111 | return chacha_setkey(tfm, key, keysize, 20); |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(crypto_chacha20_setkey); |
| 114 | |
Eric Biggers | bb31ed5 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 115 | int crypto_chacha12_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 116 | unsigned int keysize) |
| 117 | { |
| 118 | return chacha_setkey(tfm, key, keysize, 12); |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(crypto_chacha12_setkey); |
| 121 | |
Eric Biggers | b8181f3 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 122 | int crypto_chacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 123 | struct scatterlist *src, unsigned int nbytes) |
| 124 | { |
| 125 | struct chacha_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
| 126 | u8 *iv = desc->info; |
| 127 | |
| 128 | return chacha_stream_xor(desc, dst, src, nbytes, ctx, iv); |
| 129 | } |
| 130 | EXPORT_SYMBOL_GPL(crypto_chacha_crypt); |
| 131 | |
| 132 | int crypto_xchacha_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, |
| 133 | struct scatterlist *src, unsigned int nbytes) |
| 134 | { |
| 135 | struct chacha_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); |
| 136 | u8 *iv = desc->info; |
| 137 | struct chacha_ctx subctx; |
| 138 | u32 state[16]; |
| 139 | u8 real_iv[16]; |
| 140 | |
| 141 | /* Compute the subkey given the original key and first 128 nonce bits */ |
| 142 | crypto_chacha_init(state, ctx, iv); |
| 143 | hchacha_block(state, subctx.key, ctx->nrounds); |
| 144 | subctx.nrounds = ctx->nrounds; |
| 145 | |
| 146 | /* Build the real IV */ |
| 147 | memcpy(&real_iv[0], iv + 24, 8); /* stream position */ |
| 148 | memcpy(&real_iv[8], iv + 16, 8); /* remaining 64 nonce bits */ |
| 149 | |
| 150 | /* Generate the stream and XOR it with the data */ |
| 151 | return chacha_stream_xor(desc, dst, src, nbytes, &subctx, real_iv); |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(crypto_xchacha_crypt); |
| 154 | |
| 155 | static struct crypto_alg algs[] = { |
| 156 | { |
| 157 | .cra_name = "chacha20", |
| 158 | .cra_driver_name = "chacha20-generic", |
| 159 | .cra_priority = 100, |
| 160 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 161 | .cra_blocksize = 1, |
| 162 | .cra_type = &crypto_blkcipher_type, |
| 163 | .cra_ctxsize = sizeof(struct chacha_ctx), |
| 164 | .cra_alignmask = sizeof(u32) - 1, |
| 165 | .cra_module = THIS_MODULE, |
| 166 | .cra_u = { |
| 167 | .blkcipher = { |
| 168 | .min_keysize = CHACHA_KEY_SIZE, |
| 169 | .max_keysize = CHACHA_KEY_SIZE, |
| 170 | .ivsize = CHACHA_IV_SIZE, |
| 171 | .geniv = "seqiv", |
| 172 | .setkey = crypto_chacha20_setkey, |
| 173 | .encrypt = crypto_chacha_crypt, |
| 174 | .decrypt = crypto_chacha_crypt, |
| 175 | }, |
| 176 | }, |
| 177 | }, { |
| 178 | .cra_name = "xchacha20", |
| 179 | .cra_driver_name = "xchacha20-generic", |
| 180 | .cra_priority = 100, |
| 181 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 182 | .cra_blocksize = 1, |
| 183 | .cra_type = &crypto_blkcipher_type, |
| 184 | .cra_ctxsize = sizeof(struct chacha_ctx), |
| 185 | .cra_alignmask = sizeof(u32) - 1, |
| 186 | .cra_module = THIS_MODULE, |
| 187 | .cra_u = { |
| 188 | .blkcipher = { |
| 189 | .min_keysize = CHACHA_KEY_SIZE, |
| 190 | .max_keysize = CHACHA_KEY_SIZE, |
| 191 | .ivsize = XCHACHA_IV_SIZE, |
| 192 | .geniv = "seqiv", |
| 193 | .setkey = crypto_chacha20_setkey, |
| 194 | .encrypt = crypto_xchacha_crypt, |
| 195 | .decrypt = crypto_xchacha_crypt, |
| 196 | }, |
| 197 | }, |
Eric Biggers | bb31ed5 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 198 | }, { |
| 199 | .cra_name = "xchacha12", |
| 200 | .cra_driver_name = "xchacha12-generic", |
| 201 | .cra_priority = 100, |
| 202 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 203 | .cra_blocksize = 1, |
| 204 | .cra_type = &crypto_blkcipher_type, |
| 205 | .cra_ctxsize = sizeof(struct chacha_ctx), |
| 206 | .cra_alignmask = sizeof(u32) - 1, |
| 207 | .cra_module = THIS_MODULE, |
| 208 | .cra_u = { |
| 209 | .blkcipher = { |
| 210 | .min_keysize = CHACHA_KEY_SIZE, |
| 211 | .max_keysize = CHACHA_KEY_SIZE, |
| 212 | .ivsize = XCHACHA_IV_SIZE, |
| 213 | .geniv = "seqiv", |
| 214 | .setkey = crypto_chacha12_setkey, |
| 215 | .encrypt = crypto_xchacha_crypt, |
| 216 | .decrypt = crypto_xchacha_crypt, |
| 217 | }, |
| 218 | }, |
Eric Biggers | b8181f3 | 2018-11-16 17:26:21 -0800 | [diff] [blame] | 219 | }, |
| 220 | }; |
| 221 | |
| 222 | static int __init chacha_generic_mod_init(void) |
| 223 | { |
| 224 | return crypto_register_algs(algs, ARRAY_SIZE(algs)); |
| 225 | } |
| 226 | |
| 227 | static void __exit chacha_generic_mod_fini(void) |
| 228 | { |
| 229 | crypto_unregister_algs(algs, ARRAY_SIZE(algs)); |
| 230 | } |
| 231 | |
| 232 | module_init(chacha_generic_mod_init); |
| 233 | module_exit(chacha_generic_mod_fini); |
| 234 | |
| 235 | MODULE_LICENSE("GPL"); |
| 236 | MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); |
| 237 | MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)"); |
| 238 | MODULE_ALIAS_CRYPTO("chacha20"); |
| 239 | MODULE_ALIAS_CRYPTO("chacha20-generic"); |
| 240 | MODULE_ALIAS_CRYPTO("xchacha20"); |
| 241 | MODULE_ALIAS_CRYPTO("xchacha20-generic"); |
Eric Biggers | bb31ed5 | 2018-11-16 17:26:22 -0800 | [diff] [blame] | 242 | MODULE_ALIAS_CRYPTO("xchacha12"); |
| 243 | MODULE_ALIAS_CRYPTO("xchacha12-generic"); |