2016-10-04 06:22:17 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/byteorder.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
|
|
|
#include <linux/crypto.h>
|
|
|
|
#include <crypto/algapi.h>
|
|
|
|
#include <crypto/chacha20.h>
|
2017-05-05 12:49:48 +03:00
|
|
|
#include <crypto/skcipher.h>
|
2016-10-04 06:22:17 +03:00
|
|
|
|
|
|
|
#include <sodium/crypto_stream_chacha20.h>
|
|
|
|
|
|
|
|
struct chacha20_ctx {
|
|
|
|
u32 key[8];
|
|
|
|
};
|
|
|
|
|
2017-05-05 12:49:48 +03:00
|
|
|
static int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2016-10-04 06:22:17 +03:00
|
|
|
unsigned int keysize)
|
|
|
|
{
|
2017-05-05 12:49:48 +03:00
|
|
|
struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2016-10-04 06:22:17 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (keysize != CHACHA20_KEY_SIZE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
|
|
|
|
ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-05 12:49:48 +03:00
|
|
|
static int crypto_chacha20_crypt(struct skcipher_request *req)
|
2016-10-04 06:22:17 +03:00
|
|
|
{
|
2017-05-05 12:49:48 +03:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct scatterlist *sg = req->src;
|
|
|
|
unsigned nbytes = req->cryptlen;
|
2016-10-04 06:22:17 +03:00
|
|
|
u32 iv[4];
|
|
|
|
int ret;
|
|
|
|
|
2017-05-05 12:49:48 +03:00
|
|
|
BUG_ON(req->src != req->dst);
|
2016-10-04 06:22:17 +03:00
|
|
|
|
2017-05-05 12:49:48 +03:00
|
|
|
memcpy(iv, req->iv, sizeof(iv));
|
2016-10-04 06:22:17 +03:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ret = crypto_stream_chacha20_xor_ic(sg_virt(sg),
|
|
|
|
sg_virt(sg),
|
|
|
|
sg->length,
|
|
|
|
(void *) &iv[2],
|
|
|
|
iv[0] | ((u64) iv[1] << 32),
|
|
|
|
(void *) ctx->key);
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
|
|
|
nbytes -= sg->length;
|
|
|
|
|
|
|
|
if (sg_is_last(sg))
|
|
|
|
break;
|
|
|
|
|
|
|
|
BUG_ON(sg->length % CHACHA20_BLOCK_SIZE);
|
|
|
|
iv[0] += sg->length / CHACHA20_BLOCK_SIZE;
|
|
|
|
sg = sg_next(sg);
|
|
|
|
};
|
|
|
|
|
|
|
|
BUG_ON(nbytes);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-05 12:49:48 +03:00
|
|
|
static struct skcipher_alg alg = {
|
|
|
|
.base.cra_name = "chacha20",
|
|
|
|
.base.cra_ctxsize = sizeof(struct chacha20_ctx),
|
|
|
|
|
|
|
|
.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,
|
2016-10-04 06:22:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
__attribute__((constructor(110)))
|
|
|
|
static int chacha20_generic_mod_init(void)
|
|
|
|
{
|
2017-05-05 12:49:48 +03:00
|
|
|
return crypto_register_alg(&alg.base);
|
2016-10-04 06:22:17 +03:00
|
|
|
}
|