]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - crypto/cipher.c
net: ethernet: dwmac-sun8i: Use the correct function in exit path
[mirror_ubuntu-eoan-kernel.git] / crypto / cipher.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Cryptographic API.
4 *
5 * Cipher operations.
6 *
7 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
c774e93e 8 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
1da177e4 9 */
f1ddcaf3 10
6650c4de 11#include <crypto/algapi.h>
1da177e4
LT
12#include <linux/kernel.h>
13#include <linux/crypto.h>
14#include <linux/errno.h>
791b4d5f 15#include <linux/slab.h>
1da177e4 16#include <linux/string.h>
1da177e4 17#include "internal.h"
1da177e4 18
791b4d5f
HX
19static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
20 unsigned int keylen)
ca7c3938
SS
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);
06817176 36 memset(alignbuffer, 0, keylen);
ca7c3938
SS
37 kfree(buffer);
38 return ret;
39
40}
41
1da177e4
LT
42static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
43{
44 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
ca7c3938
SS
45 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
46
560c06ae 47 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
1da177e4
LT
48 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
49 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
50 return -EINVAL;
ca7c3938
SS
51 }
52
53 if ((unsigned long)key & alignmask)
54 return setkey_unaligned(tfm, key, keylen);
55
56 return cia->cia_setkey(tfm, key, keylen);
1da177e4
LT
57}
58
f28776a3
HX
59static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
60 const u8 *),
61 struct crypto_tfm *tfm,
62 u8 *dst, const u8 *src)
63{
64 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
65 unsigned int size = crypto_tfm_alg_blocksize(tfm);
6650c4de 66 u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
f28776a3
HX
67 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
68
69 memcpy(tmp, src, size);
70 fn(tfm, tmp, tmp);
71 memcpy(dst, tmp, size);
72}
73
74static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
75 u8 *dst, const u8 *src)
76{
77 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
78 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
79
80 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
81 cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
82 return;
83 }
84
85 cipher->cia_encrypt(tfm, dst, src);
86}
87
88static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
89 u8 *dst, const u8 *src)
90{
91 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
92 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
93
94 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
95 cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
96 return;
97 }
98
99 cipher->cia_decrypt(tfm, dst, src);
100}
101
1da177e4
LT
102int crypto_init_cipher_ops(struct crypto_tfm *tfm)
103{
1da177e4 104 struct cipher_tfm *ops = &tfm->crt_cipher;
f28776a3 105 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
1da177e4
LT
106
107 ops->cit_setkey = setkey;
f28776a3
HX
108 ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
109 cipher_encrypt_unaligned : cipher->cia_encrypt;
110 ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
111 cipher_decrypt_unaligned : cipher->cia_decrypt;
1da177e4 112
f1ddcaf3 113 return 0;
1da177e4 114}