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)
16 #include <crypto/algapi.h>
17 #include <linux/kernel.h>
18 #include <linux/crypto.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
24 static int setkey_unaligned(struct crypto_tfm
*tfm
, const u8
*key
,
27 struct cipher_alg
*cia
= &tfm
->__crt_alg
->cra_cipher
;
28 unsigned long alignmask
= crypto_tfm_alg_alignmask(tfm
);
30 u8
*buffer
, *alignbuffer
;
33 absize
= keylen
+ alignmask
;
34 buffer
= kmalloc(absize
, GFP_ATOMIC
);
38 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
39 memcpy(alignbuffer
, key
, keylen
);
40 ret
= cia
->cia_setkey(tfm
, alignbuffer
, keylen
);
41 memset(alignbuffer
, 0, keylen
);
47 static int setkey(struct crypto_tfm
*tfm
, const u8
*key
, unsigned int keylen
)
49 struct cipher_alg
*cia
= &tfm
->__crt_alg
->cra_cipher
;
50 unsigned long alignmask
= crypto_tfm_alg_alignmask(tfm
);
52 tfm
->crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
53 if (keylen
< cia
->cia_min_keysize
|| keylen
> cia
->cia_max_keysize
) {
54 tfm
->crt_flags
|= CRYPTO_TFM_RES_BAD_KEY_LEN
;
58 if ((unsigned long)key
& alignmask
)
59 return setkey_unaligned(tfm
, key
, keylen
);
61 return cia
->cia_setkey(tfm
, key
, keylen
);
64 static void cipher_crypt_unaligned(void (*fn
)(struct crypto_tfm
*, u8
*,
66 struct crypto_tfm
*tfm
,
67 u8
*dst
, const u8
*src
)
69 unsigned long alignmask
= crypto_tfm_alg_alignmask(tfm
);
70 unsigned int size
= crypto_tfm_alg_blocksize(tfm
);
71 u8 buffer
[MAX_CIPHER_BLOCKSIZE
+ MAX_CIPHER_ALIGNMASK
];
72 u8
*tmp
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
74 memcpy(tmp
, src
, size
);
76 memcpy(dst
, tmp
, size
);
79 static void cipher_encrypt_unaligned(struct crypto_tfm
*tfm
,
80 u8
*dst
, const u8
*src
)
82 unsigned long alignmask
= crypto_tfm_alg_alignmask(tfm
);
83 struct cipher_alg
*cipher
= &tfm
->__crt_alg
->cra_cipher
;
85 if (unlikely(((unsigned long)dst
| (unsigned long)src
) & alignmask
)) {
86 cipher_crypt_unaligned(cipher
->cia_encrypt
, tfm
, dst
, src
);
90 cipher
->cia_encrypt(tfm
, dst
, src
);
93 static void cipher_decrypt_unaligned(struct crypto_tfm
*tfm
,
94 u8
*dst
, const u8
*src
)
96 unsigned long alignmask
= crypto_tfm_alg_alignmask(tfm
);
97 struct cipher_alg
*cipher
= &tfm
->__crt_alg
->cra_cipher
;
99 if (unlikely(((unsigned long)dst
| (unsigned long)src
) & alignmask
)) {
100 cipher_crypt_unaligned(cipher
->cia_decrypt
, tfm
, dst
, src
);
104 cipher
->cia_decrypt(tfm
, dst
, src
);
107 int crypto_init_cipher_ops(struct crypto_tfm
*tfm
)
109 struct cipher_tfm
*ops
= &tfm
->crt_cipher
;
110 struct cipher_alg
*cipher
= &tfm
->__crt_alg
->cra_cipher
;
112 ops
->cit_setkey
= setkey
;
113 ops
->cit_encrypt_one
= crypto_tfm_alg_alignmask(tfm
) ?
114 cipher_encrypt_unaligned
: cipher
->cia_encrypt
;
115 ops
->cit_decrypt_one
= crypto_tfm_alg_alignmask(tfm
) ?
116 cipher_decrypt_unaligned
: cipher
->cia_decrypt
;