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 | |
Eric Biggers | 930b1cd | 2017-11-22 11:51:36 -0800 | [diff] [blame] | 12 | #include <asm/unaligned.h> |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 13 | #include <crypto/algapi.h> |
| 14 | #include <linux/crypto.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 17 | #include <crypto/chacha20.h> |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 18 | |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 19 | static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src, |
| 20 | unsigned int bytes) |
| 21 | { |
Eric Biggers | ede5c83 | 2018-09-11 20:05:10 -0700 | [diff] [blame^] | 22 | /* aligned to potentially speed up crypto_xor() */ |
| 23 | u8 stream[CHACHA20_BLOCK_SIZE] __aligned(sizeof(long)); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 24 | |
| 25 | if (dst != src) |
| 26 | memcpy(dst, src, bytes); |
| 27 | |
| 28 | while (bytes >= CHACHA20_BLOCK_SIZE) { |
| 29 | chacha20_block(state, stream); |
Eric Biggers | ede5c83 | 2018-09-11 20:05:10 -0700 | [diff] [blame^] | 30 | crypto_xor(dst, stream, CHACHA20_BLOCK_SIZE); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 31 | bytes -= CHACHA20_BLOCK_SIZE; |
| 32 | dst += CHACHA20_BLOCK_SIZE; |
| 33 | } |
| 34 | if (bytes) { |
| 35 | chacha20_block(state, stream); |
Eric Biggers | ede5c83 | 2018-09-11 20:05:10 -0700 | [diff] [blame^] | 36 | crypto_xor(dst, stream, bytes); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 40 | void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv) |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 41 | { |
Eric Biggers | 73ae7b8 | 2017-11-22 11:51:35 -0800 | [diff] [blame] | 42 | state[0] = 0x61707865; /* "expa" */ |
| 43 | state[1] = 0x3320646e; /* "nd 3" */ |
| 44 | state[2] = 0x79622d32; /* "2-by" */ |
| 45 | state[3] = 0x6b206574; /* "te k" */ |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 46 | state[4] = ctx->key[0]; |
| 47 | state[5] = ctx->key[1]; |
| 48 | state[6] = ctx->key[2]; |
| 49 | state[7] = ctx->key[3]; |
| 50 | state[8] = ctx->key[4]; |
| 51 | state[9] = ctx->key[5]; |
| 52 | state[10] = ctx->key[6]; |
| 53 | state[11] = ctx->key[7]; |
Eric Biggers | 930b1cd | 2017-11-22 11:51:36 -0800 | [diff] [blame] | 54 | state[12] = get_unaligned_le32(iv + 0); |
| 55 | state[13] = get_unaligned_le32(iv + 4); |
| 56 | state[14] = get_unaligned_le32(iv + 8); |
| 57 | state[15] = get_unaligned_le32(iv + 12); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 58 | } |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 59 | EXPORT_SYMBOL_GPL(crypto_chacha20_init); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 60 | |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 61 | int crypto_chacha20_setkey(struct crypto_tfm *tfm, const u8 *key, |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 62 | unsigned int keysize) |
| 63 | { |
| 64 | struct chacha20_ctx *ctx = crypto_tfm_ctx(tfm); |
| 65 | int i; |
| 66 | |
| 67 | if (keysize != CHACHA20_KEY_SIZE) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | for (i = 0; i < ARRAY_SIZE(ctx->key); i++) |
Eric Biggers | 930b1cd | 2017-11-22 11:51:36 -0800 | [diff] [blame] | 71 | ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32)); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 75 | EXPORT_SYMBOL_GPL(crypto_chacha20_setkey); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 76 | |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 77 | int crypto_chacha20_crypt(struct blkcipher_desc *desc, struct scatterlist *dst, |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 78 | struct scatterlist *src, unsigned int nbytes) |
| 79 | { |
| 80 | struct blkcipher_walk walk; |
| 81 | u32 state[16]; |
| 82 | int err; |
| 83 | |
| 84 | blkcipher_walk_init(&walk, dst, src, nbytes); |
| 85 | err = blkcipher_walk_virt_block(desc, &walk, CHACHA20_BLOCK_SIZE); |
| 86 | |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 87 | crypto_chacha20_init(state, crypto_blkcipher_ctx(desc->tfm), walk.iv); |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 88 | |
| 89 | while (walk.nbytes >= CHACHA20_BLOCK_SIZE) { |
| 90 | chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
| 91 | rounddown(walk.nbytes, CHACHA20_BLOCK_SIZE)); |
| 92 | err = blkcipher_walk_done(desc, &walk, |
| 93 | walk.nbytes % CHACHA20_BLOCK_SIZE); |
| 94 | } |
| 95 | |
| 96 | if (walk.nbytes) { |
| 97 | chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr, |
| 98 | walk.nbytes); |
| 99 | err = blkcipher_walk_done(desc, &walk, 0); |
| 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 | |
| 106 | static struct crypto_alg alg = { |
| 107 | .cra_name = "chacha20", |
| 108 | .cra_driver_name = "chacha20-generic", |
| 109 | .cra_priority = 100, |
| 110 | .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 111 | .cra_blocksize = 1, |
| 112 | .cra_type = &crypto_blkcipher_type, |
| 113 | .cra_ctxsize = sizeof(struct chacha20_ctx), |
| 114 | .cra_alignmask = sizeof(u32) - 1, |
| 115 | .cra_module = THIS_MODULE, |
| 116 | .cra_u = { |
| 117 | .blkcipher = { |
| 118 | .min_keysize = CHACHA20_KEY_SIZE, |
| 119 | .max_keysize = CHACHA20_KEY_SIZE, |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 120 | .ivsize = CHACHA20_IV_SIZE, |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 121 | .geniv = "seqiv", |
Martin Willi | 31d7247 | 2015-07-16 19:14:00 +0200 | [diff] [blame] | 122 | .setkey = crypto_chacha20_setkey, |
| 123 | .encrypt = crypto_chacha20_crypt, |
| 124 | .decrypt = crypto_chacha20_crypt, |
Martin Willi | c08d0e6 | 2015-06-01 13:43:56 +0200 | [diff] [blame] | 125 | }, |
| 126 | }, |
| 127 | }; |
| 128 | |
| 129 | static int __init chacha20_generic_mod_init(void) |
| 130 | { |
| 131 | return crypto_register_alg(&alg); |
| 132 | } |
| 133 | |
| 134 | static void __exit chacha20_generic_mod_fini(void) |
| 135 | { |
| 136 | crypto_unregister_alg(&alg); |
| 137 | } |
| 138 | |
| 139 | module_init(chacha20_generic_mod_init); |
| 140 | module_exit(chacha20_generic_mod_fini); |
| 141 | |
| 142 | MODULE_LICENSE("GPL"); |
| 143 | MODULE_AUTHOR("Martin Willi <martin@strongswan.org>"); |
| 144 | MODULE_DESCRIPTION("chacha20 cipher algorithm"); |
| 145 | MODULE_ALIAS_CRYPTO("chacha20"); |
| 146 | MODULE_ALIAS_CRYPTO("chacha20-generic"); |