blob: fd78150deb1c14c6830df06b4511df2e4802d293 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Cryptographic API.
4 *
Olivier Deprez157378f2022-04-04 15:47:50 +02005 * Single-block cipher operations.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 *
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009 */
10
11#include <crypto/algapi.h>
12#include <linux/kernel.h>
13#include <linux/crypto.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
16#include <linux/string.h>
17#include "internal.h"
18
Olivier Deprez157378f2022-04-04 15:47:50 +020019static int setkey_unaligned(struct crypto_cipher *tfm, const u8 *key,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020 unsigned int keylen)
21{
Olivier Deprez157378f2022-04-04 15:47:50 +020022 struct cipher_alg *cia = crypto_cipher_alg(tfm);
23 unsigned long alignmask = crypto_cipher_alignmask(tfm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024 int ret;
25 u8 *buffer, *alignbuffer;
26 unsigned long absize;
27
28 absize = keylen + alignmask;
29 buffer = kmalloc(absize, GFP_ATOMIC);
30 if (!buffer)
31 return -ENOMEM;
32
33 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
34 memcpy(alignbuffer, key, keylen);
Olivier Deprez157378f2022-04-04 15:47:50 +020035 ret = cia->cia_setkey(crypto_cipher_tfm(tfm), alignbuffer, keylen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036 memset(alignbuffer, 0, keylen);
37 kfree(buffer);
38 return ret;
39
40}
41
Olivier Deprez157378f2022-04-04 15:47:50 +020042int crypto_cipher_setkey(struct crypto_cipher *tfm,
43 const u8 *key, unsigned int keylen)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044{
Olivier Deprez157378f2022-04-04 15:47:50 +020045 struct cipher_alg *cia = crypto_cipher_alg(tfm);
46 unsigned long alignmask = crypto_cipher_alignmask(tfm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047
Olivier Deprez157378f2022-04-04 15:47:50 +020048 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050
51 if ((unsigned long)key & alignmask)
52 return setkey_unaligned(tfm, key, keylen);
53
Olivier Deprez157378f2022-04-04 15:47:50 +020054 return cia->cia_setkey(crypto_cipher_tfm(tfm), key, keylen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055}
Olivier Deprez157378f2022-04-04 15:47:50 +020056EXPORT_SYMBOL_GPL(crypto_cipher_setkey);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057
Olivier Deprez157378f2022-04-04 15:47:50 +020058static inline void cipher_crypt_one(struct crypto_cipher *tfm,
59 u8 *dst, const u8 *src, bool enc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060{
Olivier Deprez157378f2022-04-04 15:47:50 +020061 unsigned long alignmask = crypto_cipher_alignmask(tfm);
62 struct cipher_alg *cia = crypto_cipher_alg(tfm);
63 void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
64 enc ? cia->cia_encrypt : cia->cia_decrypt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065
66 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
Olivier Deprez157378f2022-04-04 15:47:50 +020067 unsigned int bs = crypto_cipher_blocksize(tfm);
68 u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
69 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
70
71 memcpy(tmp, src, bs);
72 fn(crypto_cipher_tfm(tfm), tmp, tmp);
73 memcpy(dst, tmp, bs);
74 } else {
75 fn(crypto_cipher_tfm(tfm), dst, src);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077}
78
Olivier Deprez157378f2022-04-04 15:47:50 +020079void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
80 u8 *dst, const u8 *src)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081{
Olivier Deprez157378f2022-04-04 15:47:50 +020082 cipher_crypt_one(tfm, dst, src, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083}
Olivier Deprez157378f2022-04-04 15:47:50 +020084EXPORT_SYMBOL_GPL(crypto_cipher_encrypt_one);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085
Olivier Deprez157378f2022-04-04 15:47:50 +020086void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
87 u8 *dst, const u8 *src)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088{
Olivier Deprez157378f2022-04-04 15:47:50 +020089 cipher_crypt_one(tfm, dst, src, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090}
Olivier Deprez157378f2022-04-04 15:47:50 +020091EXPORT_SYMBOL_GPL(crypto_cipher_decrypt_one);