Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 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 | |
| 12 | #include <crypto/algapi.h> |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 13 | #include <crypto/chacha20.h> |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 14 | #include <crypto/internal/skcipher.h> |
| 15 | #include <linux/module.h> |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 16 | |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 17 | static inline u32 le32_to_cpuvp(const void *p) |
| 18 | { |
| 19 | return le32_to_cpup(p); |
| 20 | } |
| 21 | |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 22 | static 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 Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 42 | void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv) |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 43 | { |
Eric Biggers | ecf3220 | 2017-11-22 11:51:35 -0800 | [diff] [blame^] | 44 | state[0] = 0x61707865; /* "expa" */ |
| 45 | state[1] = 0x3320646e; /* "nd 3" */ |
| 46 | state[2] = 0x79622d32; /* "2-by" */ |
| 47 | state[3] = 0x6b206574; /* "te k" */ |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 48 | state[4] = ctx->key[0]; |
| 49 | state[5] = ctx->key[1]; |
| 50 | state[6] = ctx->key[2]; |
| 51 | state[7] = ctx->key[3]; |
| 52 | state[8] = ctx->key[4]; |
| 53 | state[9] = ctx->key[5]; |
| 54 | state[10] = ctx->key[6]; |
| 55 | state[11] = ctx->key[7]; |
| 56 | state[12] = le32_to_cpuvp(iv + 0); |
| 57 | state[13] = le32_to_cpuvp(iv + 4); |
| 58 | state[14] = le32_to_cpuvp(iv + 8); |
| 59 | state[15] = le32_to_cpuvp(iv + 12); |
| 60 | } |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 61 | EXPORT_SYMBOL_GPL(crypto_chacha20_init); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 62 | |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 63 | int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key, |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 64 | unsigned int keysize) |
| 65 | { |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 66 | struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 67 | int i; |
| 68 | |
| 69 | if (keysize != CHACHA20_KEY_SIZE) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | for (i = 0; i < ARRAY_SIZE(ctx->key); i++) |
| 73 | ctx->key[i] = le32_to_cpuvp(key + i * sizeof(u32)); |
| 74 | |
| 75 | return 0; |
| 76 | } |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 77 | EXPORT_SYMBOL_GPL(crypto_chacha20_setkey); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 78 | |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 79 | int crypto_chacha20_crypt(struct skcipher_request *req) |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 80 | { |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 81 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 82 | struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm); |
| 83 | struct skcipher_walk walk; |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 84 | u32 state[16]; |
| 85 | int err; |
| 86 | |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 87 | err = skcipher_walk_virt(&walk, req, true); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 88 | |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 89 | crypto_chacha20_init(state, ctx, walk.iv); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 90 | |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 91 | while (walk.nbytes > 0) { |
Ard Biesheuvel | 4de4372 | 2017-08-14 14:28:14 +0100 | [diff] [blame] | 92 | unsigned int nbytes = walk.nbytes; |
| 93 | |
| 94 | if (nbytes < walk.total) |
| 95 | nbytes = round_down(nbytes, walk.stride); |
| 96 | |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 97 | chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
Ard Biesheuvel | 4de4372 | 2017-08-14 14:28:14 +0100 | [diff] [blame] | 98 | nbytes); |
| 99 | err = skcipher_walk_done(&walk, walk.nbytes - nbytes); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return err; |
| 103 | } |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 104 | EXPORT_SYMBOL_GPL(crypto_chacha20_crypt); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 105 | |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 106 | static struct skcipher_alg alg = { |
| 107 | .base.cra_name = "chacha20", |
| 108 | .base.cra_driver_name = "chacha20-generic", |
| 109 | .base.cra_priority = 100, |
| 110 | .base.cra_blocksize = 1, |
| 111 | .base.cra_ctxsize = sizeof(struct chacha20_ctx), |
| 112 | .base.cra_alignmask = sizeof(u32) - 1, |
| 113 | .base.cra_module = THIS_MODULE, |
| 114 | |
| 115 | .min_keysize = CHACHA20_KEY_SIZE, |
| 116 | .max_keysize = CHACHA20_KEY_SIZE, |
| 117 | .ivsize = CHACHA20_IV_SIZE, |
| 118 | .chunksize = CHACHA20_BLOCK_SIZE, |
| 119 | .setkey = crypto_chacha20_setkey, |
| 120 | .encrypt = crypto_chacha20_crypt, |
| 121 | .decrypt = crypto_chacha20_crypt, |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static int __init chacha20_generic_mod_init(void) |
| 125 | { |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 126 | return crypto_register_skcipher(&alg); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static void __exit chacha20_generic_mod_fini(void) |
| 130 | { |
Ard Biesheuvel | 9ae433b | 2016-12-09 14:33:51 +0000 | [diff] [blame] | 131 | crypto_unregister_skcipher(&alg); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | module_init(chacha20_generic_mod_init); |
| 135 | module_exit(chacha20_generic_mod_fini); |
| 136 | |
| 137 | MODULE_LICENSE("GPL"); |
| 138 | MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); |
| 139 | MODULE_DESCRIPTION("chacha20 cipher algorithm"); |
| 140 | MODULE_ALIAS_CRYPTO("chacha20"); |
| 141 | MODULE_ALIAS_CRYPTO("chacha20-generic"); |