iso9001
Joined: 02 Dec 2003 Posts: 262
|
XXTEA / Correct Block TEA need help porting to CCSC |
Posted: Thu Mar 13, 2008 12:12 pm |
|
|
Hi,
I know there is are XTEA examples already here. But, I figured since the code was available I would switch to its successor Corrected Block TEA or XXTEA
The problem I'm having is I don't exactly understand what they are doing with 'n'. And why is has to be a signed int32
Code in C
Code: |
/* #define MX (z>>5^y<<2) + (y>>3^z<<4)^(sum^y) + (k[p&3^e]^z) */
#define MX ( (((z>>5)^(y<<2))+((y>>3)^(z<<4)))^((sum^y)+(k[(p&3)^e]^z)) )
long btea(long* v, long n, long* k) {
unsigned long z /* = v[n-1] */, y=v[0], sum=0, e, DELTA=0x9e3779b9;
long p, q ;
if (n > 1) { /* Coding Part */
z=v[n-1]; /* Moved z=v[n-1] to here, else [[Segmentation fault|segfaults]] in decode when n < 0 */
q = 6+52/n ;
while (q-- > 0) {
sum += DELTA;
e = sum >> 2&3 ;
for (p=0; p<n-1; p++) y = v[p+1], z = v[p] += MX;
y = v[0];
z = v[n-1] += MX;
}
return 0 ;
} else if (n < -1) { /* Decoding Part */
n = -n ;
q = 6+52/n ;
sum = q*DELTA ;
while (sum != 0) {
e = sum>>2 & 3;
for (p=n-1; p>0; p--) z = v[p-1], y = v[p] -= MX;
z = v[n-1];
y = v[0] -= MX;
sum -= DELTA;
}
return 0;
}
return 1;
}
|
Anyone care to explain so I can try and port this over ?
Thanks |
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: XXTEA / Correct Block TEA need help porting to CCSC |
Posted: Fri Mar 14, 2008 7:58 am |
|
|
It does not appear reasonable that n needs to be signed int32 for the algorithm to work. Since it mostly is used to index an array, you would think that it must be limited in range, especially for small microcontroller implementations. Can you provide a link to the original source for this code so we can see how this one function fits into the larger picture? |
|