aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Siewior <sebastian@breakpoint.cc>2008-04-01 08:58:51 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2008-04-20 22:19:34 -0400
commit5427663f498e19b441277de72ce7a685511f247c (patch)
treecaebca4d656c8028780bf1d692dd325704332225
parentc3715cb90f722b1cf5f6f073be02cc8a49659b90 (diff)
[CRYPTO] aes: Export generic setkey
The key expansion routine could be get little more generic, become a kernel doc entry and then get exported. Signed-off-by: Sebastian Siewior <sebastian@breakpoint.cc> Tested-by: Stefan Hellermann <stefan@the2masters.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/aes_generic.c56
-rw-r--r--include/crypto/aes.h8
2 files changed, 53 insertions, 11 deletions
diff --git a/crypto/aes_generic.c b/crypto/aes_generic.c
index cf30af74480f..136dc98d8a03 100644
--- a/crypto/aes_generic.c
+++ b/crypto/aes_generic.c
@@ -229,18 +229,29 @@ static void __init gen_tabs(void)
229 ctx->key_enc[8 * i + 15] = t; \ 229 ctx->key_enc[8 * i + 15] = t; \
230} while (0) 230} while (0)
231 231
232int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, 232/**
233 * crypto_aes_expand_key - Expands the AES key as described in FIPS-197
234 * @ctx: The location where the computed key will be stored.
235 * @in_key: The supplied key.
236 * @key_len: The length of the supplied key.
237 *
238 * Returns 0 on success. The function fails only if an invalid key size (or
239 * pointer) is supplied.
240 * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes
241 * key schedule plus a 16 bytes key which is used before the first round).
242 * The decryption key is prepared for the "Equivalent Inverse Cipher" as
243 * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is
244 * for the initial combination, the second slot for the first round and so on.
245 */
246int crypto_aes_expand_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
233 unsigned int key_len) 247 unsigned int key_len)
234{ 248{
235 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
236 const __le32 *key = (const __le32 *)in_key; 249 const __le32 *key = (const __le32 *)in_key;
237 u32 *flags = &tfm->crt_flags;
238 u32 i, t, u, v, w, j; 250 u32 i, t, u, v, w, j;
239 251
240 if (key_len % 8) { 252 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
241 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; 253 key_len != AES_KEYSIZE_256)
242 return -EINVAL; 254 return -EINVAL;
243 }
244 255
245 ctx->key_length = key_len; 256 ctx->key_length = key_len;
246 257
@@ -250,20 +261,20 @@ int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
250 ctx->key_dec[key_len + 27] = ctx->key_enc[3] = le32_to_cpu(key[3]); 261 ctx->key_dec[key_len + 27] = ctx->key_enc[3] = le32_to_cpu(key[3]);
251 262
252 switch (key_len) { 263 switch (key_len) {
253 case 16: 264 case AES_KEYSIZE_128:
254 t = ctx->key_enc[3]; 265 t = ctx->key_enc[3];
255 for (i = 0; i < 10; ++i) 266 for (i = 0; i < 10; ++i)
256 loop4(i); 267 loop4(i);
257 break; 268 break;
258 269
259 case 24: 270 case AES_KEYSIZE_192:
260 ctx->key_enc[4] = le32_to_cpu(key[4]); 271 ctx->key_enc[4] = le32_to_cpu(key[4]);
261 t = ctx->key_enc[5] = le32_to_cpu(key[5]); 272 t = ctx->key_enc[5] = le32_to_cpu(key[5]);
262 for (i = 0; i < 8; ++i) 273 for (i = 0; i < 8; ++i)
263 loop6(i); 274 loop6(i);
264 break; 275 break;
265 276
266 case 32: 277 case AES_KEYSIZE_256:
267 ctx->key_enc[4] = le32_to_cpu(key[4]); 278 ctx->key_enc[4] = le32_to_cpu(key[4]);
268 ctx->key_enc[5] = le32_to_cpu(key[5]); 279 ctx->key_enc[5] = le32_to_cpu(key[5]);
269 ctx->key_enc[6] = le32_to_cpu(key[6]); 280 ctx->key_enc[6] = le32_to_cpu(key[6]);
@@ -284,6 +295,33 @@ int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
284 } 295 }
285 return 0; 296 return 0;
286} 297}
298EXPORT_SYMBOL_GPL(crypto_aes_expand_key);
299
300/**
301 * crypto_aes_set_key - Set the AES key.
302 * @tfm: The %crypto_tfm that is used in the context.
303 * @in_key: The input key.
304 * @key_len: The size of the key.
305 *
306 * Returns 0 on success, on failure the %CRYPTO_TFM_RES_BAD_KEY_LEN flag in tfm
307 * is set. The function uses crypto_aes_expand_key() to expand the key.
308 * &crypto_aes_ctx _must_ be the private data embedded in @tfm which is
309 * retrieved with crypto_tfm_ctx().
310 */
311int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
312 unsigned int key_len)
313{
314 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
315 u32 *flags = &tfm->crt_flags;
316 int ret;
317
318 ret = crypto_aes_expand_key(ctx, in_key, key_len);
319 if (!ret)
320 return 0;
321
322 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
323 return -EINVAL;
324}
287EXPORT_SYMBOL_GPL(crypto_aes_set_key); 325EXPORT_SYMBOL_GPL(crypto_aes_set_key);
288 326
289/* encrypt a block of text */ 327/* encrypt a block of text */
diff --git a/include/crypto/aes.h b/include/crypto/aes.h
index d480b76715a8..40008d67ee3d 100644
--- a/include/crypto/aes.h
+++ b/include/crypto/aes.h
@@ -14,11 +14,13 @@
14#define AES_KEYSIZE_192 24 14#define AES_KEYSIZE_192 24
15#define AES_KEYSIZE_256 32 15#define AES_KEYSIZE_256 32
16#define AES_BLOCK_SIZE 16 16#define AES_BLOCK_SIZE 16
17#define AES_MAX_KEYLENGTH (15 * 16)
18#define AES_MAX_KEYLENGTH_U32 (AES_MAX_KEYLENGTH / sizeof(u32))
17 19
18struct crypto_aes_ctx { 20struct crypto_aes_ctx {
19 u32 key_length; 21 u32 key_length;
20 u32 key_enc[60]; 22 u32 key_enc[AES_MAX_KEYLENGTH_U32];
21 u32 key_dec[60]; 23 u32 key_dec[AES_MAX_KEYLENGTH_U32];
22}; 24};
23 25
24extern u32 crypto_ft_tab[4][256]; 26extern u32 crypto_ft_tab[4][256];
@@ -28,4 +30,6 @@ extern u32 crypto_il_tab[4][256];
28 30
29int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, 31int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
30 unsigned int key_len); 32 unsigned int key_len);
33int crypto_aes_expand_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
34 unsigned int key_len);
31#endif 35#endif