chacha20_generic.c 3.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * ChaCha20 256-bit cipher algorithm, RFC7539
 *
 * Copyright (C) 2015 Martin Willi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

12
#include <asm/unaligned.h>
13
#include <crypto/algapi.h>
14
#include <crypto/chacha20.h>
15 16
#include <crypto/internal/skcipher.h>
#include <linux/module.h>
17 18 19 20

static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
			     unsigned int bytes)
{
21
	u32 stream[CHACHA20_BLOCK_WORDS];
22 23 24 25 26 27

	if (dst != src)
		memcpy(dst, src, bytes);

	while (bytes >= CHACHA20_BLOCK_SIZE) {
		chacha20_block(state, stream);
28
		crypto_xor(dst, (const u8 *)stream, CHACHA20_BLOCK_SIZE);
29 30 31 32 33
		bytes -= CHACHA20_BLOCK_SIZE;
		dst += CHACHA20_BLOCK_SIZE;
	}
	if (bytes) {
		chacha20_block(state, stream);
34
		crypto_xor(dst, (const u8 *)stream, bytes);
35 36 37
	}
}

38
void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
39
{
40 41 42 43
	state[0]  = 0x61707865; /* "expa" */
	state[1]  = 0x3320646e; /* "nd 3" */
	state[2]  = 0x79622d32; /* "2-by" */
	state[3]  = 0x6b206574; /* "te k" */
44 45 46 47 48 49 50 51
	state[4]  = ctx->key[0];
	state[5]  = ctx->key[1];
	state[6]  = ctx->key[2];
	state[7]  = ctx->key[3];
	state[8]  = ctx->key[4];
	state[9]  = ctx->key[5];
	state[10] = ctx->key[6];
	state[11] = ctx->key[7];
52 53 54 55
	state[12] = get_unaligned_le32(iv +  0);
	state[13] = get_unaligned_le32(iv +  4);
	state[14] = get_unaligned_le32(iv +  8);
	state[15] = get_unaligned_le32(iv + 12);
56
}
57
EXPORT_SYMBOL_GPL(crypto_chacha20_init);
58

59
int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
60 61
			   unsigned int keysize)
{
62
	struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
63 64 65 66 67 68
	int i;

	if (keysize != CHACHA20_KEY_SIZE)
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
69
		ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
70 71 72

	return 0;
}
73
EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
74

75
int crypto_chacha20_crypt(struct skcipher_request *req)
76
{
77 78 79
	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
	struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
	struct skcipher_walk walk;
80 81 82
	u32 state[16];
	int err;

83
	err = skcipher_walk_virt(&walk, req, true);
84

85
	crypto_chacha20_init(state, ctx, walk.iv);
86

87
	while (walk.nbytes > 0) {
88 89 90 91 92
		unsigned int nbytes = walk.nbytes;

		if (nbytes < walk.total)
			nbytes = round_down(nbytes, walk.stride);

93
		chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
94 95
				 nbytes);
		err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
96 97 98 99
	}

	return err;
}
100
EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static struct skcipher_alg alg = {
	.base.cra_name		= "chacha20",
	.base.cra_driver_name	= "chacha20-generic",
	.base.cra_priority	= 100,
	.base.cra_blocksize	= 1,
	.base.cra_ctxsize	= sizeof(struct chacha20_ctx),
	.base.cra_module	= THIS_MODULE,

	.min_keysize		= CHACHA20_KEY_SIZE,
	.max_keysize		= CHACHA20_KEY_SIZE,
	.ivsize			= CHACHA20_IV_SIZE,
	.chunksize		= CHACHA20_BLOCK_SIZE,
	.setkey			= crypto_chacha20_setkey,
	.encrypt		= crypto_chacha20_crypt,
	.decrypt		= crypto_chacha20_crypt,
117 118 119 120
};

static int __init chacha20_generic_mod_init(void)
{
121
	return crypto_register_skcipher(&alg);
122 123 124 125
}

static void __exit chacha20_generic_mod_fini(void)
{
126
	crypto_unregister_skcipher(&alg);
127 128 129 130 131 132 133 134 135 136
}

module_init(chacha20_generic_mod_init);
module_exit(chacha20_generic_mod_fini);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
MODULE_DESCRIPTION("chacha20 cipher algorithm");
MODULE_ALIAS_CRYPTO("chacha20");
MODULE_ALIAS_CRYPTO("chacha20-generic");