18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic API.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Single-block cipher operations.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
88c2ecf20Sopenharmony_ci * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/crypto.h>
148c2ecf20Sopenharmony_ci#include <linux/errno.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/string.h>
178c2ecf20Sopenharmony_ci#include "internal.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int setkey_unaligned(struct crypto_cipher *tfm, const u8 *key,
208c2ecf20Sopenharmony_ci			    unsigned int keylen)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct cipher_alg *cia = crypto_cipher_alg(tfm);
238c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_cipher_alignmask(tfm);
248c2ecf20Sopenharmony_ci	int ret;
258c2ecf20Sopenharmony_ci	u8 *buffer, *alignbuffer;
268c2ecf20Sopenharmony_ci	unsigned long absize;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	absize = keylen + alignmask;
298c2ecf20Sopenharmony_ci	buffer = kmalloc(absize, GFP_ATOMIC);
308c2ecf20Sopenharmony_ci	if (!buffer)
318c2ecf20Sopenharmony_ci		return -ENOMEM;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
348c2ecf20Sopenharmony_ci	memcpy(alignbuffer, key, keylen);
358c2ecf20Sopenharmony_ci	ret = cia->cia_setkey(crypto_cipher_tfm(tfm), alignbuffer, keylen);
368c2ecf20Sopenharmony_ci	kfree_sensitive(buffer);
378c2ecf20Sopenharmony_ci	return ret;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciint crypto_cipher_setkey(struct crypto_cipher *tfm,
428c2ecf20Sopenharmony_ci			 const u8 *key, unsigned int keylen)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct cipher_alg *cia = crypto_cipher_alg(tfm);
458c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_cipher_alignmask(tfm);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize)
488c2ecf20Sopenharmony_ci		return -EINVAL;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if ((unsigned long)key & alignmask)
518c2ecf20Sopenharmony_ci		return setkey_unaligned(tfm, key, keylen);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return cia->cia_setkey(crypto_cipher_tfm(tfm), key, keylen);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_cipher_setkey);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic inline void cipher_crypt_one(struct crypto_cipher *tfm,
588c2ecf20Sopenharmony_ci				    u8 *dst, const u8 *src, bool enc)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_cipher_alignmask(tfm);
618c2ecf20Sopenharmony_ci	struct cipher_alg *cia = crypto_cipher_alg(tfm);
628c2ecf20Sopenharmony_ci	void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
638c2ecf20Sopenharmony_ci		enc ? cia->cia_encrypt : cia->cia_decrypt;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
668c2ecf20Sopenharmony_ci		unsigned int bs = crypto_cipher_blocksize(tfm);
678c2ecf20Sopenharmony_ci		u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
688c2ecf20Sopenharmony_ci		u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci		memcpy(tmp, src, bs);
718c2ecf20Sopenharmony_ci		fn(crypto_cipher_tfm(tfm), tmp, tmp);
728c2ecf20Sopenharmony_ci		memcpy(dst, tmp, bs);
738c2ecf20Sopenharmony_ci	} else {
748c2ecf20Sopenharmony_ci		fn(crypto_cipher_tfm(tfm), dst, src);
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_civoid crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
798c2ecf20Sopenharmony_ci			       u8 *dst, const u8 *src)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	cipher_crypt_one(tfm, dst, src, true);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_cipher_encrypt_one);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_civoid crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
868c2ecf20Sopenharmony_ci			       u8 *dst, const u8 *src)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	cipher_crypt_one(tfm, dst, src, false);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(crypto_cipher_decrypt_one);
91