| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Cryptographic API. | 
 | 3 |  * | 
 | 4 |  * Cipher operations. | 
 | 5 |  * | 
 | 6 |  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> | 
| Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 7 |  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  */ | 
| Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 15 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> | 
 | 17 | #include <linux/crypto.h> | 
 | 18 | #include <linux/errno.h> | 
| Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 19 | #include <linux/scatterlist.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/string.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "internal.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
 | 23 | static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen) | 
 | 24 | { | 
 | 25 | 	struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher; | 
 | 26 | 	 | 
| Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 27 | 	tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Herbert Xu | 560c06a | 2006-08-13 14:16:39 +1000 | [diff] [blame] | 32 | 		return cia->cia_setkey(tfm, key, keylen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } | 
 | 34 |  | 
| Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 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) | 
 | 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 |  | 
 | 50 | static 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 |  | 
 | 64 | static 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | int crypto_init_cipher_ops(struct crypto_tfm *tfm) | 
 | 79 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | 	struct cipher_tfm *ops = &tfm->crt_cipher; | 
| Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 81 | 	struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 |  | 
 | 83 | 	ops->cit_setkey = setkey; | 
| Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 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; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 |  | 
| Herbert Xu | f1ddcaf | 2007-01-27 10:05:15 +1100 | [diff] [blame] | 89 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } | 
 | 91 |  | 
 | 92 | void crypto_exit_cipher_ops(struct crypto_tfm *tfm) | 
 | 93 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |