1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Single-block cipher operations.
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
11 #include <crypto/algapi.h>
12 #include <crypto/internal/cipher.h>
13 #include <linux/kernel.h>
14 #include <linux/crypto.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
20 static int setkey_unaligned(struct crypto_cipher *tfm, const u8 *key,
23 struct cipher_alg *cia = crypto_cipher_alg(tfm);
24 unsigned long alignmask = crypto_cipher_alignmask(tfm);
26 u8 *buffer, *alignbuffer;
29 absize = keylen + alignmask;
30 buffer = kmalloc(absize, GFP_ATOMIC);
34 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
35 memcpy(alignbuffer, key, keylen);
36 ret = cia->cia_setkey(crypto_cipher_tfm(tfm), alignbuffer, keylen);
37 memset(alignbuffer, 0, keylen);
43 int crypto_cipher_setkey(struct crypto_cipher *tfm,
44 const u8 *key, unsigned int keylen)
46 struct cipher_alg *cia = crypto_cipher_alg(tfm);
47 unsigned long alignmask = crypto_cipher_alignmask(tfm);
49 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize)
52 if ((unsigned long)key & alignmask)
53 return setkey_unaligned(tfm, key, keylen);
55 return cia->cia_setkey(crypto_cipher_tfm(tfm), key, keylen);
57 EXPORT_SYMBOL_NS_GPL(crypto_cipher_setkey, CRYPTO_INTERNAL);
59 static inline void cipher_crypt_one(struct crypto_cipher *tfm,
60 u8 *dst, const u8 *src, bool enc)
62 unsigned long alignmask = crypto_cipher_alignmask(tfm);
63 struct cipher_alg *cia = crypto_cipher_alg(tfm);
64 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
65 enc ? cia->cia_encrypt : cia->cia_decrypt;
67 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
68 unsigned int bs = crypto_cipher_blocksize(tfm);
69 u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
70 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
73 fn(crypto_cipher_tfm(tfm), tmp, tmp);
76 fn(crypto_cipher_tfm(tfm), dst, src);
80 void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
81 u8 *dst, const u8 *src)
83 cipher_crypt_one(tfm, dst, src, true);
85 EXPORT_SYMBOL_NS_GPL(crypto_cipher_encrypt_one, CRYPTO_INTERNAL);
87 void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
88 u8 *dst, const u8 *src)
90 cipher_crypt_one(tfm, dst, src, false);
92 EXPORT_SYMBOL_NS_GPL(crypto_cipher_decrypt_one, CRYPTO_INTERNAL);