Blame view
crypto/cipher.c
3.1 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 6 7 |
/* * Cryptographic API. * * Cipher operations. * * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
c774e93e2 [CRYPTO] Add plum... |
8 |
* Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4c Linux-2.6.12-rc2 |
9 |
*/ |
f1ddcaf33 [CRYPTO] api: Rem... |
10 |
|
6650c4de6 crypto: remove se... |
11 |
#include <crypto/algapi.h> |
1da177e4c Linux-2.6.12-rc2 |
12 13 14 |
#include <linux/kernel.h> #include <linux/crypto.h> #include <linux/errno.h> |
791b4d5f7 [CRYPTO] api: Add... |
15 |
#include <linux/slab.h> |
1da177e4c Linux-2.6.12-rc2 |
16 |
#include <linux/string.h> |
1da177e4c Linux-2.6.12-rc2 |
17 |
#include "internal.h" |
1da177e4c Linux-2.6.12-rc2 |
18 |
|
791b4d5f7 [CRYPTO] api: Add... |
19 20 |
static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) |
ca7c39385 [CRYPTO] api: Han... |
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
{ struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); int ret; u8 *buffer, *alignbuffer; unsigned long absize; absize = keylen + alignmask; buffer = kmalloc(absize, GFP_ATOMIC); if (!buffer) return -ENOMEM; alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); memcpy(alignbuffer, key, keylen); ret = cia->cia_setkey(tfm, alignbuffer, keylen); |
068171767 [CRYPTO] api: fix... |
36 |
memset(alignbuffer, 0, keylen); |
ca7c39385 [CRYPTO] api: Han... |
37 38 39 40 |
kfree(buffer); return ret; } |
1da177e4c Linux-2.6.12-rc2 |
41 42 43 |
static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) { struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; |
ca7c39385 [CRYPTO] api: Han... |
44 |
unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); |
560c06ae1 [CRYPTO] api: Get... |
45 |
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; |
1da177e4c Linux-2.6.12-rc2 |
46 47 48 |
if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) { tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; |
ca7c39385 [CRYPTO] api: Han... |
49 50 51 52 53 54 |
} if ((unsigned long)key & alignmask) return setkey_unaligned(tfm, key, keylen); return cia->cia_setkey(tfm, key, keylen); |
1da177e4c Linux-2.6.12-rc2 |
55 |
} |
f28776a36 [CRYPTO] cipher: ... |
56 57 58 59 60 61 62 |
static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *, const u8 *), struct crypto_tfm *tfm, u8 *dst, const u8 *src) { unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); unsigned int size = crypto_tfm_alg_blocksize(tfm); |
6650c4de6 crypto: remove se... |
63 |
u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK]; |
f28776a36 [CRYPTO] cipher: ... |
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); memcpy(tmp, src, size); fn(tfm, tmp, tmp); memcpy(dst, tmp, size); } static void cipher_encrypt_unaligned(struct crypto_tfm *tfm, u8 *dst, const u8 *src) { unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) { cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src); return; } cipher->cia_encrypt(tfm, dst, src); } static void cipher_decrypt_unaligned(struct crypto_tfm *tfm, u8 *dst, const u8 *src) { unsigned long alignmask = crypto_tfm_alg_alignmask(tfm); struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) { cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src); return; } cipher->cia_decrypt(tfm, dst, src); } |
1da177e4c Linux-2.6.12-rc2 |
98 99 |
int crypto_init_cipher_ops(struct crypto_tfm *tfm) { |
1da177e4c Linux-2.6.12-rc2 |
100 |
struct cipher_tfm *ops = &tfm->crt_cipher; |
f28776a36 [CRYPTO] cipher: ... |
101 |
struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; |
1da177e4c Linux-2.6.12-rc2 |
102 103 |
ops->cit_setkey = setkey; |
f28776a36 [CRYPTO] cipher: ... |
104 105 106 107 |
ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ? cipher_encrypt_unaligned : cipher->cia_encrypt; ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ? cipher_decrypt_unaligned : cipher->cia_decrypt; |
1da177e4c Linux-2.6.12-rc2 |
108 |
|
f1ddcaf33 [CRYPTO] api: Rem... |
109 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
110 |
} |