mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-13 14:24:11 +08:00
d95312a3cc
Reimplement the library routines to perform chacha20poly1305 en/decryption on scatterlists, without [ab]using the [deprecated] blkcipher interface, which is rather heavyweight and does things we don't really need. Instead, we use the sg_miter API in a novel and clever way, to iterate over the scatterlist in-place (i.e., source == destination, which is the only way this library is expected to be used). That way, we don't have to iterate over two scatterlists in parallel. Another optimization is that, instead of relying on the blkcipher walker to present the input in suitable chunks, we recognize that ChaCha is a streamcipher, and so we can simply deal with partial blocks by keeping a block of cipherstream on the stack and use crypto_xor() to mix it with the in/output. Finally, we omit the scatterwalk_and_copy() call if the last element of the scatterlist covers the MAC as well (which is the common case), avoiding the need to walk the scatterlist and kmap() the page twice. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
49 lines
1.6 KiB
C
49 lines
1.6 KiB
C
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
|
|
/*
|
|
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
*/
|
|
|
|
#ifndef __CHACHA20POLY1305_H
|
|
#define __CHACHA20POLY1305_H
|
|
|
|
#include <linux/types.h>
|
|
#include <linux/scatterlist.h>
|
|
|
|
enum chacha20poly1305_lengths {
|
|
XCHACHA20POLY1305_NONCE_SIZE = 24,
|
|
CHACHA20POLY1305_KEY_SIZE = 32,
|
|
CHACHA20POLY1305_AUTHTAG_SIZE = 16
|
|
};
|
|
|
|
void chacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u64 nonce,
|
|
const u8 key[CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool __must_check
|
|
chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
|
|
const u8 *ad, const size_t ad_len, const u64 nonce,
|
|
const u8 key[CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
void xchacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u8 nonce[XCHACHA20POLY1305_NONCE_SIZE],
|
|
const u8 key[CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool __must_check xchacha20poly1305_decrypt(
|
|
u8 *dst, const u8 *src, const size_t src_len, const u8 *ad,
|
|
const size_t ad_len, const u8 nonce[XCHACHA20POLY1305_NONCE_SIZE],
|
|
const u8 key[CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool chacha20poly1305_encrypt_sg_inplace(struct scatterlist *src, size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u64 nonce,
|
|
const u8 key[CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
bool chacha20poly1305_decrypt_sg_inplace(struct scatterlist *src, size_t src_len,
|
|
const u8 *ad, const size_t ad_len,
|
|
const u64 nonce,
|
|
const u8 key[CHACHA20POLY1305_KEY_SIZE]);
|
|
|
|
#endif /* __CHACHA20POLY1305_H */
|