Blame view
crypto/cipher.c
2.51 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 |
/* * Cryptographic API. * |
e8cfed5e4 crypto: cipher - ... |
5 |
* Single-block cipher operations. |
1da177e4c Linux-2.6.12-rc2 |
6 7 |
* * 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 |
|
e8cfed5e4 crypto: cipher - ... |
19 |
static int setkey_unaligned(struct crypto_cipher *tfm, const u8 *key, |
791b4d5f7 [CRYPTO] api: Add... |
20 |
unsigned int keylen) |
ca7c39385 [CRYPTO] api: Han... |
21 |
{ |
e8cfed5e4 crypto: cipher - ... |
22 23 |
struct cipher_alg *cia = crypto_cipher_alg(tfm); unsigned long alignmask = crypto_cipher_alignmask(tfm); |
ca7c39385 [CRYPTO] api: Han... |
24 25 26 27 28 29 30 31 32 33 34 |
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); |
e8cfed5e4 crypto: cipher - ... |
35 |
ret = cia->cia_setkey(crypto_cipher_tfm(tfm), alignbuffer, keylen); |
068171767 [CRYPTO] api: fix... |
36 |
memset(alignbuffer, 0, keylen); |
ca7c39385 [CRYPTO] api: Han... |
37 38 39 40 |
kfree(buffer); return ret; } |
e8cfed5e4 crypto: cipher - ... |
41 42 |
int crypto_cipher_setkey(struct crypto_cipher *tfm, const u8 *key, unsigned int keylen) |
1da177e4c Linux-2.6.12-rc2 |
43 |
{ |
e8cfed5e4 crypto: cipher - ... |
44 45 |
struct cipher_alg *cia = crypto_cipher_alg(tfm); unsigned long alignmask = crypto_cipher_alignmask(tfm); |
ca7c39385 [CRYPTO] api: Han... |
46 |
|
674f368a9 crypto: remove CR... |
47 |
if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) |
1da177e4c Linux-2.6.12-rc2 |
48 |
return -EINVAL; |
ca7c39385 [CRYPTO] api: Han... |
49 50 51 |
if ((unsigned long)key & alignmask) return setkey_unaligned(tfm, key, keylen); |
e8cfed5e4 crypto: cipher - ... |
52 |
return cia->cia_setkey(crypto_cipher_tfm(tfm), key, keylen); |
1da177e4c Linux-2.6.12-rc2 |
53 |
} |
e8cfed5e4 crypto: cipher - ... |
54 |
EXPORT_SYMBOL_GPL(crypto_cipher_setkey); |
1da177e4c Linux-2.6.12-rc2 |
55 |
|
e8cfed5e4 crypto: cipher - ... |
56 57 |
static inline void cipher_crypt_one(struct crypto_cipher *tfm, u8 *dst, const u8 *src, bool enc) |
f28776a36 [CRYPTO] cipher: ... |
58 |
{ |
e8cfed5e4 crypto: cipher - ... |
59 60 61 62 |
unsigned long alignmask = crypto_cipher_alignmask(tfm); struct cipher_alg *cia = crypto_cipher_alg(tfm); void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = enc ? cia->cia_encrypt : cia->cia_decrypt; |
f28776a36 [CRYPTO] cipher: ... |
63 64 |
if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) { |
e8cfed5e4 crypto: cipher - ... |
65 66 67 68 69 70 71 72 73 |
unsigned int bs = crypto_cipher_blocksize(tfm); u8 buffer[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK]; u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); memcpy(tmp, src, bs); fn(crypto_cipher_tfm(tfm), tmp, tmp); memcpy(dst, tmp, bs); } else { fn(crypto_cipher_tfm(tfm), dst, src); |
f28776a36 [CRYPTO] cipher: ... |
74 |
} |
f28776a36 [CRYPTO] cipher: ... |
75 |
} |
e8cfed5e4 crypto: cipher - ... |
76 77 |
void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, u8 *dst, const u8 *src) |
f28776a36 [CRYPTO] cipher: ... |
78 |
{ |
e8cfed5e4 crypto: cipher - ... |
79 |
cipher_crypt_one(tfm, dst, src, true); |
f28776a36 [CRYPTO] cipher: ... |
80 |
} |
e8cfed5e4 crypto: cipher - ... |
81 |
EXPORT_SYMBOL_GPL(crypto_cipher_encrypt_one); |
f28776a36 [CRYPTO] cipher: ... |
82 |
|
e8cfed5e4 crypto: cipher - ... |
83 84 |
void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, u8 *dst, const u8 *src) |
1da177e4c Linux-2.6.12-rc2 |
85 |
{ |
e8cfed5e4 crypto: cipher - ... |
86 |
cipher_crypt_one(tfm, dst, src, false); |
1da177e4c Linux-2.6.12-rc2 |
87 |
} |
e8cfed5e4 crypto: cipher - ... |
88 |
EXPORT_SYMBOL_GPL(crypto_cipher_decrypt_one); |