]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - crypto/cipher.c
[CRYPTO] api: Wake up all waiters when larval completes
[mirror_ubuntu-bionic-kernel.git] / crypto / cipher.c
CommitLineData
1da177e4
LT
1/*
2 * Cryptographic API.
3 *
4 * Cipher operations.
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
c774e93e 7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
1da177e4
LT
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
f1ddcaf3 15
1da177e4
LT
16#include <linux/kernel.h>
17#include <linux/crypto.h>
18#include <linux/errno.h>
f1ddcaf3 19#include <linux/scatterlist.h>
1da177e4 20#include <linux/string.h>
1da177e4 21#include "internal.h"
1da177e4
LT
22
23static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
24{
25 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
26
560c06ae 27 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
1da177e4
LT
28 if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
29 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
30 return -EINVAL;
31 } else
560c06ae 32 return cia->cia_setkey(tfm, key, keylen);
1da177e4
LT
33}
34
f28776a3
HX
35static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
36 const u8 *),
37 struct crypto_tfm *tfm,
38 u8 *dst, const u8 *src)
39{
40 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
41 unsigned int size = crypto_tfm_alg_blocksize(tfm);
42 u8 buffer[size + alignmask];
43 u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
44
45 memcpy(tmp, src, size);
46 fn(tfm, tmp, tmp);
47 memcpy(dst, tmp, size);
48}
49
50static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
51 u8 *dst, const u8 *src)
52{
53 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
54 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
55
56 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
57 cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
58 return;
59 }
60
61 cipher->cia_encrypt(tfm, dst, src);
62}
63
64static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
65 u8 *dst, const u8 *src)
66{
67 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
68 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
69
70 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
71 cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
72 return;
73 }
74
75 cipher->cia_decrypt(tfm, dst, src);
76}
77
1da177e4
LT
78int crypto_init_cipher_ops(struct crypto_tfm *tfm)
79{
1da177e4 80 struct cipher_tfm *ops = &tfm->crt_cipher;
f28776a3 81 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
1da177e4
LT
82
83 ops->cit_setkey = setkey;
f28776a3
HX
84 ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
85 cipher_encrypt_unaligned : cipher->cia_encrypt;
86 ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
87 cipher_decrypt_unaligned : cipher->cia_decrypt;
1da177e4 88
f1ddcaf3 89 return 0;
1da177e4
LT
90}
91
92void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
93{
1da177e4 94}