Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * Cryptographic API.
3 *
4 * Cipher operations.
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
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 */
15
16#include <linux/kernel.h>
17#include <linux/crypto.h>
18#include <linux/errno.h>
19#include <linux/scatterlist.h>
20#include <linux/string.h>
21#include "internal.h"
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
27 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
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
32 return cia->cia_setkey(tfm, key, keylen);
33}
34
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
78int crypto_init_cipher_ops(struct crypto_tfm *tfm)
79{
80 struct cipher_tfm *ops = &tfm->crt_cipher;
81 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
82
83 ops->cit_setkey = setkey;
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;
88
89 return 0;
90}
91
92void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
93{
94}