blob: 333aab2f027717fc96672ed74662d1c25d72b2ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * Cipher operations.
5 *
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
Herbert Xuc774e932005-07-06 13:51:31 -07007 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 Xuf1ddcaf2007-01-27 10:05:15 +110015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/crypto.h>
18#include <linux/errno.h>
Herbert Xuf1ddcaf2007-01-27 10:05:15 +110019#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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
Herbert Xu560c06a2006-08-13 14:16:39 +100027 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 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 Xu560c06a2006-08-13 14:16:39 +100032 return cia->cia_setkey(tfm, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Herbert Xuf28776a2006-08-13 20:58:18 +100035static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070078int crypto_init_cipher_ops(struct crypto_tfm *tfm)
79{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct cipher_tfm *ops = &tfm->crt_cipher;
Herbert Xuf28776a2006-08-13 20:58:18 +100081 struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 ops->cit_setkey = setkey;
Herbert Xuf28776a2006-08-13 20:58:18 +100084 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 Torvalds1da177e2005-04-16 15:20:36 -070088
Herbert Xuf1ddcaf2007-01-27 10:05:15 +110089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
93{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}