annotate aes-asm-ltc.c @ 908:3ca7113936c1 asm

aes and sha1 for arm
author Matt Johnston <matt@ucc.asn.au>
date Sun, 06 Oct 2013 21:49:15 +0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
908
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Tom St Denis, [email protected], http://libtomcrypt.com
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 /* AES implementation by Tom St Denis
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 *
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * Derived from the Public Domain source code by
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 ---
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * rijndael-alg-fst.c
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 *
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * @version 3.0 (December 2000)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 *
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * Optimised ANSI C code for the Rijndael cipher (now AES)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 *
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * @author Vincent Rijmen <[email protected]>
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 * @author Antoon Bosselaers <[email protected]>
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 * @author Paulo Barreto <[email protected]>
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 ---
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 /**
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 @file aes.c
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 Implementation of AES
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #include "options.h"
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #include "tomcrypt.h"
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #ifdef DROPBEAR_AES_ASM
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #define SETUP aes_asm_setup
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 #define ECB_ENC aes_asm_ecb_encrypt
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 #define ECB_DEC aes_asm_ecb_decrypt
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #define ECB_DONE aes_asm_done
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #define ECB_TEST aes_asm_test
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 #define ECB_KS aes_asm_keysize
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 /* Matches the AES key structure used by OpenSSL */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 struct aes_asm_key {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 ulong32 key[60];
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 int rounds;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 };
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 struct aes_asm_keypair {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 struct aes_asm_key enc;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 struct aes_asm_key dec;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 };
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 int private_AES_set_encrypt_key(const unsigned char* key,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 int keybits, struct aes_asm_key* key_state);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 int private_AES_set_decrypt_key(const unsigned char* key,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 int keybits, struct aes_asm_key* key_state);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 int AES_encrypt(const unsigned char* in,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 const unsigned char* out,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 struct aes_asm_key* key_state);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 int AES_decrypt(const unsigned char* in,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 const unsigned char* out,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 struct aes_asm_key* key_state);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 /**
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 Initialize the AES (Rijndael) block cipher
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 @param key The symmetric key you wish to pass
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 @param keylen The key length in bytes
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 @param num_rounds The number of rounds desired (0 for default)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 @param skey The key in as scheduled by this function.
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 @return CRYPT_OK if successful
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 struct aes_asm_keypair *keypair = NULL;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 LTC_ARGCHK(key != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 LTC_ARGCHK(skey != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 if (keylen != 16 && keylen != 24 && keylen != 32) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 return CRYPT_INVALID_KEYSIZE;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 if (num_rounds != 0)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 return CRYPT_INVALID_ROUNDS;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 skey->data = XMALLOC(sizeof(*keypair));
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 keypair = skey->data;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 private_AES_set_encrypt_key(key, keylen*8, &keypair->enc);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 private_AES_set_decrypt_key(key, keylen*8, &keypair->dec);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 return CRYPT_OK;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 /**
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 Encrypts a block of text with AES
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 @param pt The input plaintext (16 bytes)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 @param ct The output ciphertext (16 bytes)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 @param skey The key as scheduled
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 @return CRYPT_OK if successful
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 struct aes_asm_keypair *keypair = NULL;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 LTC_ARGCHK(pt != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 LTC_ARGCHK(ct != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 LTC_ARGCHK(skey != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 keypair = skey->data;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 AES_encrypt(pt, ct, &keypair->enc);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 return CRYPT_OK;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 /**
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 Decrypts a block of text with AES
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 @param ct The input ciphertext (16 bytes)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 @param pt The output plaintext (16 bytes)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 @param skey The key as scheduled
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 @return CRYPT_OK if successful
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 int ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 struct aes_asm_keypair *keypair = NULL;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 LTC_ARGCHK(pt != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 LTC_ARGCHK(ct != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 LTC_ARGCHK(skey != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 keypair = skey->data;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 AES_encrypt(pt, ct, &keypair->enc);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 return CRYPT_OK;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 #ifdef LTC_CLEAN_STACK
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 #error No clean stack support in ASM AES
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 #endif
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 /**
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 Performs a self-test of the AES block cipher
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 int ECB_TEST(void)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 #ifndef LTC_TEST
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 return CRYPT_NOP;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 #else
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 int err;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 static const struct {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 int keylen;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 unsigned char key[32], pt[16], ct[16];
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 } tests[] = {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 { 16,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 }, {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 24,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 }, {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 32,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 };
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 symmetric_key key;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 unsigned char tmp[2][16];
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 int i, y;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 zeromem(&key, sizeof(key));
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 return err;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 rijndael_ecb_encrypt(tests[i].pt, tmp[0], &key);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 rijndael_ecb_decrypt(tmp[0], tmp[1], &key);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 if (XMEMCMP(tmp[0], tests[i].ct, 16) || XMEMCMP(tmp[1], tests[i].pt, 16)) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 #if 0
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 printf("\n\nTest %d failed\n", i);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 if (XMEMCMP(tmp[0], tests[i].ct, 16)) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 printf("CT: ");
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 for (i = 0; i < 16; i++) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 printf("%02x ", tmp[0][i]);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 printf("\n");
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 } else {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 printf("PT: ");
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 for (i = 0; i < 16; i++) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 printf("%02x ", tmp[1][i]);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 printf("\n");
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 #endif
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 return CRYPT_FAIL_TESTVECTOR;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 for (y = 0; y < 16; y++) tmp[0][y] = 0;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 for (y = 0; y < 1000; y++) rijndael_ecb_encrypt(tmp[0], tmp[0], &key);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 for (y = 0; y < 1000; y++) rijndael_ecb_decrypt(tmp[0], tmp[0], &key);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 return CRYPT_OK;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 #endif
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 /** Terminate the context
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 @param skey The scheduled key
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 void ECB_DONE(symmetric_key *skey)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 XFREE(skey->data);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 /**
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 Gets suitable key size
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 @return CRYPT_OK if the input key size is acceptable.
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 int ECB_KS(int *keysize)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 LTC_ARGCHK(keysize != NULL);
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 if (*keysize < 16)
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 return CRYPT_INVALID_KEYSIZE;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 if (*keysize < 24) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 *keysize = 16;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 return CRYPT_OK;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 } else if (*keysize < 32) {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 *keysize = 24;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 return CRYPT_OK;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 } else {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 *keysize = 32;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 return CRYPT_OK;
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 }
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 const struct ltc_cipher_descriptor aes_asm_desc =
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 {
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 "aes_asm",
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 106,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 16, 32, 16, 10,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 };
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 #endif /* AES_ASM */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/aes/aes.c,v $ */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 /* $Revision: 1.14 $ */
3ca7113936c1 aes and sha1 for arm
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 /* $Date: 2006/11/08 23:01:06 $ */