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