comparison libtomcrypt/src/ciphers/aes/aes.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children eef377591301
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org 9 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */ 10 */
11 11
12 /* AES implementation by Tom St Denis 12 /* AES implementation by Tom St Denis
13 * 13 *
14 * Derived from the Public Domain source code by 14 * Derived from the Public Domain source code by
48 { 48 {
49 "rijndael", 49 "rijndael",
50 6, 50 6,
51 16, 32, 16, 10, 51 16, 32, 16, 10,
52 SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS, 52 SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
53 NULL, NULL, NULL, NULL, NULL, NULL, NULL 53 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
54 }; 54 };
55 #endif 55 #endif
56 56
57 const struct ltc_cipher_descriptor aes_desc = 57 const struct ltc_cipher_descriptor aes_desc =
58 { 58 {
59 "aes", 59 "aes",
60 6, 60 6,
61 16, 32, 16, 10, 61 16, 32, 16, 10,
62 SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS, 62 SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
63 NULL, NULL, NULL, NULL, NULL, NULL, NULL 63 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
64 }; 64 };
65 65
66 #else 66 #else
67 67
68 #define SETUP rijndael_enc_setup 68 #define SETUP rijndael_enc_setup
74 { 74 {
75 "rijndael", 75 "rijndael",
76 6, 76 6,
77 16, 32, 16, 10, 77 16, 32, 16, 10,
78 SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS, 78 SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS,
79 NULL, NULL, NULL, NULL, NULL, NULL, NULL 79 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
80 }; 80 };
81 81
82 const struct ltc_cipher_descriptor aes_enc_desc = 82 const struct ltc_cipher_descriptor aes_enc_desc =
83 { 83 {
84 "aes", 84 "aes",
85 6, 85 6,
86 16, 32, 16, 10, 86 16, 32, 16, 10,
87 SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS, 87 SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS,
88 NULL, NULL, NULL, NULL, NULL, NULL, NULL 88 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
89 }; 89 };
90 90
91 #endif 91 #endif
92 92
93 #include "aes_tab.c" 93 #include "aes_tab.c"
281 /** 281 /**
282 Encrypts a block of text with AES 282 Encrypts a block of text with AES
283 @param pt The input plaintext (16 bytes) 283 @param pt The input plaintext (16 bytes)
284 @param ct The output ciphertext (16 bytes) 284 @param ct The output ciphertext (16 bytes)
285 @param skey The key as scheduled 285 @param skey The key as scheduled
286 @return CRYPT_OK if successful
286 */ 287 */
287 #ifdef LTC_CLEAN_STACK 288 #ifdef LTC_CLEAN_STACK
288 static void _rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 289 static int _rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
289 #else 290 #else
290 void ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 291 int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
291 #endif 292 #endif
292 { 293 {
293 ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk; 294 ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk;
294 int Nr, r; 295 int Nr, r;
295 296
306 */ 307 */
307 LOAD32H(s0, pt ); s0 ^= rk[0]; 308 LOAD32H(s0, pt ); s0 ^= rk[0];
308 LOAD32H(s1, pt + 4); s1 ^= rk[1]; 309 LOAD32H(s1, pt + 4); s1 ^= rk[1];
309 LOAD32H(s2, pt + 8); s2 ^= rk[2]; 310 LOAD32H(s2, pt + 8); s2 ^= rk[2];
310 LOAD32H(s3, pt + 12); s3 ^= rk[3]; 311 LOAD32H(s3, pt + 12); s3 ^= rk[3];
311
312 312
313 #ifdef LTC_SMALL_CODE 313 #ifdef LTC_SMALL_CODE
314 314
315 for (r = 0; ; r++) { 315 for (r = 0; ; r++) {
316 rk += 4; 316 rk += 4;
440 (Te4_2[byte(t0, 2)]) ^ 440 (Te4_2[byte(t0, 2)]) ^
441 (Te4_1[byte(t1, 1)]) ^ 441 (Te4_1[byte(t1, 1)]) ^
442 (Te4_0[byte(t2, 0)]) ^ 442 (Te4_0[byte(t2, 0)]) ^
443 rk[3]; 443 rk[3];
444 STORE32H(s3, ct+12); 444 STORE32H(s3, ct+12);
445
446 return CRYPT_OK;
445 } 447 }
446 448
447 #ifdef LTC_CLEAN_STACK 449 #ifdef LTC_CLEAN_STACK
448 void ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 450 int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
449 { 451 {
450 _rijndael_ecb_encrypt(pt, ct, skey); 452 int err = _rijndael_ecb_encrypt(pt, ct, skey);
451 burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2); 453 burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
454 return err;
452 } 455 }
453 #endif 456 #endif
454 457
455 #ifndef ENCRYPT_ONLY 458 #ifndef ENCRYPT_ONLY
456 459
457 /** 460 /**
458 Decrypts a block of text with AES 461 Decrypts a block of text with AES
459 @param ct The input ciphertext (16 bytes) 462 @param ct The input ciphertext (16 bytes)
460 @param pt The output plaintext (16 bytes) 463 @param pt The output plaintext (16 bytes)
461 @param skey The key as scheduled 464 @param skey The key as scheduled
465 @return CRYPT_OK if successful
462 */ 466 */
463 #ifdef LTC_CLEAN_STACK 467 #ifdef LTC_CLEAN_STACK
464 static void _rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 468 static int _rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
465 #else 469 #else
466 void ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 470 int ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
467 #endif 471 #endif
468 { 472 {
469 ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk; 473 ulong32 s0, s1, s2, s3, t0, t1, t2, t3, *rk;
470 int Nr, r; 474 int Nr, r;
471 475
615 (Td4[byte(t2, 2)] & 0x00ff0000) ^ 619 (Td4[byte(t2, 2)] & 0x00ff0000) ^
616 (Td4[byte(t1, 1)] & 0x0000ff00) ^ 620 (Td4[byte(t1, 1)] & 0x0000ff00) ^
617 (Td4[byte(t0, 0)] & 0x000000ff) ^ 621 (Td4[byte(t0, 0)] & 0x000000ff) ^
618 rk[3]; 622 rk[3];
619 STORE32H(s3, pt+12); 623 STORE32H(s3, pt+12);
624
625 return CRYPT_OK;
620 } 626 }
621 627
622 628
623 #ifdef LTC_CLEAN_STACK 629 #ifdef LTC_CLEAN_STACK
624 void ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 630 int ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
625 { 631 {
626 _rijndael_ecb_decrypt(ct, pt, skey); 632 int err = _rijndael_ecb_decrypt(ct, pt, skey);
627 burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2); 633 burn_stack(sizeof(unsigned long)*8 + sizeof(unsigned long*) + sizeof(int)*2);
634 return err;
628 } 635 }
629 #endif 636 #endif
630 637
631 /** 638 /**
632 Performs a self-test of the AES block cipher 639 Performs a self-test of the AES block cipher
681 return err; 688 return err;
682 } 689 }
683 690
684 rijndael_ecb_encrypt(tests[i].pt, tmp[0], &key); 691 rijndael_ecb_encrypt(tests[i].pt, tmp[0], &key);
685 rijndael_ecb_decrypt(tmp[0], tmp[1], &key); 692 rijndael_ecb_decrypt(tmp[0], tmp[1], &key);
686 if (memcmp(tmp[0], tests[i].ct, 16) || memcmp(tmp[1], tests[i].pt, 16)) { 693 if (XMEMCMP(tmp[0], tests[i].ct, 16) || XMEMCMP(tmp[1], tests[i].pt, 16)) {
687 #if 0 694 #if 0
688 printf("\n\nTest %d failed\n", i); 695 printf("\n\nTest %d failed\n", i);
689 if (memcmp(tmp[0], tests[i].ct, 16)) { 696 if (XMEMCMP(tmp[0], tests[i].ct, 16)) {
690 printf("CT: "); 697 printf("CT: ");
691 for (i = 0; i < 16; i++) { 698 for (i = 0; i < 16; i++) {
692 printf("%02x ", tmp[0][i]); 699 printf("%02x ", tmp[0][i]);
693 } 700 }
694 printf("\n"); 701 printf("\n");
749 756
750 #endif 757 #endif
751 758
752 759
753 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/aes/aes.c,v $ */ 760 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/aes/aes.c,v $ */
754 /* $Revision: 1.8 $ */ 761 /* $Revision: 1.14 $ */
755 /* $Date: 2005/05/05 14:35:58 $ */ 762 /* $Date: 2006/11/08 23:01:06 $ */