i2c-s3c2410: Fix bug in releasing driver
[linux-2.6/s3c2410-cpufreq.git] / crypto / cipher.c
blob333aab2f027717fc96672ed74662d1c25d72b2ce
1 /*
2 * Cryptographic API.
4 * Cipher operations.
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
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.
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"
23 static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
25 struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
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);
35 static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
36 const u8 *),
37 struct crypto_tfm *tfm,
38 u8 *dst, const u8 *src)
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);
45 memcpy(tmp, src, size);
46 fn(tfm, tmp, tmp);
47 memcpy(dst, tmp, size);
50 static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
51 u8 *dst, const u8 *src)
53 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
54 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
56 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
57 cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
58 return;
61 cipher->cia_encrypt(tfm, dst, src);
64 static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
65 u8 *dst, const u8 *src)
67 unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
68 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
70 if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
71 cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
72 return;
75 cipher->cia_decrypt(tfm, dst, src);
78 int crypto_init_cipher_ops(struct crypto_tfm *tfm)
80 struct cipher_tfm *ops = &tfm->crt_cipher;
81 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
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;
89 return 0;
92 void crypto_exit_cipher_ops(struct crypto_tfm *tfm)