blob: 18bf9c425ae28162ecbeaf8c40e92f9f4d16828f [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Cryptographic API.
4 *
5 * Cipher operations.
6 *
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
9 */
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
19static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
20 unsigned int keylen)
21{
22 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
23 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
24 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);
35 ret = cia->cia_setkey(tfm, alignbuffer, keylen);
36 kzfree(buffer);
37 return ret;
38
39}
40
41static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
42{
43 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
44 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
45
46 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
47 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
48 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
49 return -EINVAL;
50 }
51
52 if ((unsigned long)key & alignmask)
53 return setkey_unaligned(tfm, key, keylen);
54
55 return cia->cia_setkey(tfm, key, keylen);
56}
57
58static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
59 const u8 *),
60 struct crypto_tfm *tfm,
61 u8 *dst, const u8 *src)
62{
63 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
64 unsigned int size = crypto_tfm_alg_blocksize(tfm);
65 u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
66 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
67
68 memcpy(tmp, src, size);
69 fn(tfm, tmp, tmp);
70 memcpy(dst, tmp, size);
71}
72
73static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
74 u8 *dst, const u8 *src)
75{
76 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
77 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
78
79 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
80 cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
81 return;
82 }
83
84 cipher->cia_encrypt(tfm, dst, src);
85}
86
87static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
88 u8 *dst, const u8 *src)
89{
90 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
91 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
92
93 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
94 cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
95 return;
96 }
97
98 cipher->cia_decrypt(tfm, dst, src);
99}
100
101int crypto_init_cipher_ops(struct crypto_tfm *tfm)
102{
103 struct cipher_tfm *ops = &tfm->crt_cipher;
104 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
105
106 ops->cit_setkey = setkey;
107 ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
108 cipher_encrypt_unaligned : cipher->cia_encrypt;
109 ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
110 cipher_decrypt_unaligned : cipher->cia_decrypt;
111
112 return 0;
113}