5 * Implementation of XTEA for AVR
6 * include xtea.h in your C-Project to use this functions.
29 C = 28 /* der kleine Zaehler fuer zwischendurch */
33 ; xtea encrytion function
34 ; param1: 16-bit pointer to destination for encrypted block
36 ; param2: 16-bit pointer to the block (64-bit) which is to encrypt
38 ; param3: 16-bit pointer to the key (128-bit)
58 movw r26, r22 /* X points to block */
59 movw r30, r20 /* Z points to key */
70 movw r26, r24 /* X points to destination */
73 mov r0, Func1 /* r1 is cycle-counter */
88 brne 2b /* Accu == V1 << 4 */
98 brne 3b /* Func == V1 >> 5 */
107 adc Accu4, V14 /* Accu == ( (V1<<4)^(V1>>5) ) + V1 */
116 mov C, Sum1 /* calc key offset */
128 ldd Func4, Z+3 /* Func = key[sum & 3] */
138 eor Accu4, Func4 /* Accu = ((V1<<4 ^ V1>>5) + V1) ^ (sum + key[sum&3]) */
149 /* sum += delta */ /* delta == 0x9E3779B9 */
167 /* write block back */
195 ;####################################################################
197 /* #endif TWO_IN_ONE */
199 /* #ifdef TWO_IN_ONE */
200 /* now we use the same base-structure for enc- and decryption
201 to indicate operation mode we use the highest bit of param3 (16 bit pointer to key),
202 this is ok, since even the larges atmel today has "only" 8k of ram,
203 but you shouldn't use this feature while using external ram.
210 ; xtea decrytion function
211 ; param1: 16-bit pointer to destination for decrypted block
213 ; param2: 16-bit pointer to the block (64-bit) which is to derypt
215 ; param3: 16-bit pointer to the key (128-bit)
219 void xtea_dec(uint32_t* dest, uint32_t* v, uint32_t* k) {
220 uint32_t v0=v[0], v1=v[1], i;
221 uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
222 for(i=0; i<32; i++) {
223 v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
225 v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
227 dest[0]=v0; dest[1]=v1;
247 movw r26, r22 /* Z points to block */
248 movw r30, r20 /* X points to key */
257 movw r26, r24 /* Z points to destination */
260 mov r0, Sum1 /* r1 is cycle-counter */
261 ldi Sum1, 0x20 /* sum = 0xC6EF3720 */
276 brne 2b /* Accu == V0 << 4 */
286 brne 3b /* Func == V0 >> 5 */
295 adc Accu4, V04 /* Accu == ( (V0<<4)^(V0>>5) ) + V0 */
304 mov C, Sum1 /* calc key offset */
316 ldd Func4, Z+3 /* Func = key[sum & 3] */
326 eor Accu4, Func4 /* Accu = ((V0<<4 ^ V0>>5) + V0) ^ (sum + key[sum&3]) */
339 /* sum += delta */ /* delta == 0x9E3779B9 */
353 /* write block back */
381 ;####################################################################
384 /* now we use the same base-structure for enc- and decryption
385 to indicate operation mode we use the highest bit of param3 (16 bit pointer to key),
386 this is ok, since even the larges atmel today has "only" 8k of ram,
387 but you shouldn't use this feature while using external ram.
394 ; xtea decrytion function
395 ; param1: 16-bit pointer to destination for decrypted block
397 ; param2: 16-bit pointer to the block (64-bit) which is to derypt
399 ; param3: 16-bit pointer to the key (128-bit)
403 void xtea_dec(uint32_t* dest, uint32_t* v, uint32_t* k) {
404 uint32_t v0=v[0], v1=v[1], i;
405 uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
406 for(i=0; i<32; i++) {
407 v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
409 v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
411 dest[0]=v0; dest[1]=v1;
430 /* set T-bit if we are going to encrypt, clear otherwise */
432 andi r21, 0x7f /* fix r21:r22 to a real addr */
434 movw r26, r22 /* Z points to block */
435 movw r30, r20 /* X points to key */
444 movw r26, r24 /* Z points to destination */
447 mov r0, Sum1 /* r1 is cycle-counter */
448 ldi Sum1, 0x20 /* sum = 0xC6EF3720 */
463 brne 2b /* Accu == V0 << 4 */
473 brne 3b /* Func == V0 >> 5 */
482 adc Accu4, V04 /* Accu == ( (V0<<4)^(V0>>5) ) + V0 */
491 mov C, Sum1 /* calc key offset */
503 ldd Func4, Z+3 /* Func = key[sum & 3] */
513 eor Accu4, Func4 /* Accu = ((V0<<4 ^ V0>>5) + V0) ^ (sum + key[sum&3]) */
526 /* sum += delta */ /* delta == 0x9E3779B9 */
540 /* write block back */