Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3)  * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5)  * Single-block cipher operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7)  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8)  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int setkey_unaligned(struct crypto_cipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) 			    unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) 	struct cipher_alg *cia = crypto_cipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 	unsigned long alignmask = crypto_cipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) 	u8 *buffer, *alignbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) 	unsigned long absize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 	absize = keylen + alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) 	buffer = kmalloc(absize, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) 	memcpy(alignbuffer, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 	ret = cia->cia_setkey(crypto_cipher_tfm(tfm), alignbuffer, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 	memset(alignbuffer, 0, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int crypto_cipher_setkey(struct crypto_cipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 			 const u8 *key, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 	struct cipher_alg *cia = crypto_cipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) 	unsigned long alignmask = crypto_cipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 	if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) 	if ((unsigned long)key & alignmask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) 		return setkey_unaligned(tfm, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) 	return cia->cia_setkey(crypto_cipher_tfm(tfm), key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) EXPORT_SYMBOL_NS_GPL(crypto_cipher_setkey, CRYPTO_INTERNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static inline void cipher_crypt_one(struct crypto_cipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) 				    u8 *dst, const u8 *src, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 	unsigned long alignmask = crypto_cipher_alignmask(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) 	struct cipher_alg *cia = crypto_cipher_alg(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) 	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) 		enc ? cia->cia_encrypt : cia->cia_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) 	if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) 		unsigned int bs = crypto_cipher_blocksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) 		u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) 		u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) 		memcpy(tmp, src, bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) 		fn(crypto_cipher_tfm(tfm), tmp, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) 		memcpy(dst, tmp, bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) 		fn(crypto_cipher_tfm(tfm), dst, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) 			       u8 *dst, const u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) 	cipher_crypt_one(tfm, dst, src, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) EXPORT_SYMBOL_NS_GPL(crypto_cipher_encrypt_one, CRYPTO_INTERNAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) 			       u8 *dst, const u8 *src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) 	cipher_crypt_one(tfm, dst, src, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) EXPORT_SYMBOL_NS_GPL(crypto_cipher_decrypt_one, CRYPTO_INTERNAL);