aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/aes.c
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2005-10-30 05:25:15 -0500
committerDavid S. Miller <davem@sunset.davemloft.net>2006-01-09 17:15:34 -0500
commit06ace7a9bafeb9047352707eb79e8eaa0dfdf5f2 (patch)
treefa22bbc2e8ea5bee00b6aec353783144b6f8735a /crypto/aes.c
parent2df15fffc612b53b2c8e4ff3c981a82441bc00ae (diff)
[CRYPTO] Use standard byte order macros wherever possible
A lot of crypto code needs to read/write a 32-bit/64-bit words in a specific gender. Many of them open code them by reading/writing one byte at a time. This patch converts all the applicable usages over to use the standard byte order macros. This is based on a previous patch by Denis Vlasenko. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/aes.c')
-rw-r--r--crypto/aes.c60
1 files changed, 31 insertions, 29 deletions
diff --git a/crypto/aes.c b/crypto/aes.c
index 5df92888ef5a..35a11deef29b 100644
--- a/crypto/aes.c
+++ b/crypto/aes.c
@@ -73,9 +73,6 @@ byte(const u32 x, const unsigned n)
73 return x >> (n << 3); 73 return x >> (n << 3);
74} 74}
75 75
76#define u32_in(x) le32_to_cpu(*(const u32 *)(x))
77#define u32_out(to, from) (*(u32 *)(to) = cpu_to_le32(from))
78
79struct aes_ctx { 76struct aes_ctx {
80 int key_length; 77 int key_length;
81 u32 E[60]; 78 u32 E[60];
@@ -256,6 +253,7 @@ static int
256aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags) 253aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
257{ 254{
258 struct aes_ctx *ctx = ctx_arg; 255 struct aes_ctx *ctx = ctx_arg;
256 const __le32 *key = (const __le32 *)in_key;
259 u32 i, t, u, v, w; 257 u32 i, t, u, v, w;
260 258
261 if (key_len != 16 && key_len != 24 && key_len != 32) { 259 if (key_len != 16 && key_len != 24 && key_len != 32) {
@@ -265,10 +263,10 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
265 263
266 ctx->key_length = key_len; 264 ctx->key_length = key_len;
267 265
268 E_KEY[0] = u32_in (in_key); 266 E_KEY[0] = le32_to_cpu(key[0]);
269 E_KEY[1] = u32_in (in_key + 4); 267 E_KEY[1] = le32_to_cpu(key[1]);
270 E_KEY[2] = u32_in (in_key + 8); 268 E_KEY[2] = le32_to_cpu(key[2]);
271 E_KEY[3] = u32_in (in_key + 12); 269 E_KEY[3] = le32_to_cpu(key[3]);
272 270
273 switch (key_len) { 271 switch (key_len) {
274 case 16: 272 case 16:
@@ -278,17 +276,17 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
278 break; 276 break;
279 277
280 case 24: 278 case 24:
281 E_KEY[4] = u32_in (in_key + 16); 279 E_KEY[4] = le32_to_cpu(key[4]);
282 t = E_KEY[5] = u32_in (in_key + 20); 280 t = E_KEY[5] = le32_to_cpu(key[5]);
283 for (i = 0; i < 8; ++i) 281 for (i = 0; i < 8; ++i)
284 loop6 (i); 282 loop6 (i);
285 break; 283 break;
286 284
287 case 32: 285 case 32:
288 E_KEY[4] = u32_in (in_key + 16); 286 E_KEY[4] = le32_to_cpu(key[4]);
289 E_KEY[5] = u32_in (in_key + 20); 287 E_KEY[5] = le32_to_cpu(key[5]);
290 E_KEY[6] = u32_in (in_key + 24); 288 E_KEY[6] = le32_to_cpu(key[6]);
291 t = E_KEY[7] = u32_in (in_key + 28); 289 t = E_KEY[7] = le32_to_cpu(key[7]);
292 for (i = 0; i < 7; ++i) 290 for (i = 0; i < 7; ++i)
293 loop8 (i); 291 loop8 (i);
294 break; 292 break;
@@ -324,13 +322,15 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
324static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in) 322static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
325{ 323{
326 const struct aes_ctx *ctx = ctx_arg; 324 const struct aes_ctx *ctx = ctx_arg;
325 const __le32 *src = (const __le32 *)in;
326 __le32 *dst = (__le32 *)out;
327 u32 b0[4], b1[4]; 327 u32 b0[4], b1[4];
328 const u32 *kp = E_KEY + 4; 328 const u32 *kp = E_KEY + 4;
329 329
330 b0[0] = u32_in (in) ^ E_KEY[0]; 330 b0[0] = le32_to_cpu(src[0]) ^ E_KEY[0];
331 b0[1] = u32_in (in + 4) ^ E_KEY[1]; 331 b0[1] = le32_to_cpu(src[1]) ^ E_KEY[1];
332 b0[2] = u32_in (in + 8) ^ E_KEY[2]; 332 b0[2] = le32_to_cpu(src[2]) ^ E_KEY[2];
333 b0[3] = u32_in (in + 12) ^ E_KEY[3]; 333 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3];
334 334
335 if (ctx->key_length > 24) { 335 if (ctx->key_length > 24) {
336 f_nround (b1, b0, kp); 336 f_nround (b1, b0, kp);
@@ -353,10 +353,10 @@ static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
353 f_nround (b1, b0, kp); 353 f_nround (b1, b0, kp);
354 f_lround (b0, b1, kp); 354 f_lround (b0, b1, kp);
355 355
356 u32_out (out, b0[0]); 356 dst[0] = cpu_to_le32(b0[0]);
357 u32_out (out + 4, b0[1]); 357 dst[1] = cpu_to_le32(b0[1]);
358 u32_out (out + 8, b0[2]); 358 dst[2] = cpu_to_le32(b0[2]);
359 u32_out (out + 12, b0[3]); 359 dst[3] = cpu_to_le32(b0[3]);
360} 360}
361 361
362/* decrypt a block of text */ 362/* decrypt a block of text */
@@ -377,14 +377,16 @@ static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
377static void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in) 377static void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in)
378{ 378{
379 const struct aes_ctx *ctx = ctx_arg; 379 const struct aes_ctx *ctx = ctx_arg;
380 const __le32 *src = (const __le32 *)in;
381 __le32 *dst = (__le32 *)out;
380 u32 b0[4], b1[4]; 382 u32 b0[4], b1[4];
381 const int key_len = ctx->key_length; 383 const int key_len = ctx->key_length;
382 const u32 *kp = D_KEY + key_len + 20; 384 const u32 *kp = D_KEY + key_len + 20;
383 385
384 b0[0] = u32_in (in) ^ E_KEY[key_len + 24]; 386 b0[0] = le32_to_cpu(src[0]) ^ E_KEY[key_len + 24];
385 b0[1] = u32_in (in + 4) ^ E_KEY[key_len + 25]; 387 b0[1] = le32_to_cpu(src[1]) ^ E_KEY[key_len + 25];
386 b0[2] = u32_in (in + 8) ^ E_KEY[key_len + 26]; 388 b0[2] = le32_to_cpu(src[2]) ^ E_KEY[key_len + 26];
387 b0[3] = u32_in (in + 12) ^ E_KEY[key_len + 27]; 389 b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27];
388 390
389 if (key_len > 24) { 391 if (key_len > 24) {
390 i_nround (b1, b0, kp); 392 i_nround (b1, b0, kp);
@@ -407,10 +409,10 @@ static void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in)
407 i_nround (b1, b0, kp); 409 i_nround (b1, b0, kp);
408 i_lround (b0, b1, kp); 410 i_lround (b0, b1, kp);
409 411
410 u32_out (out, b0[0]); 412 dst[0] = cpu_to_le32(b0[0]);
411 u32_out (out + 4, b0[1]); 413 dst[1] = cpu_to_le32(b0[1]);
412 u32_out (out + 8, b0[2]); 414 dst[2] = cpu_to_le32(b0[2]);
413 u32_out (out + 12, b0[3]); 415 dst[3] = cpu_to_le32(b0[3]);
414} 416}
415 417
416 418