X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/ba6e6b64033b1f9de49feccb5c9cd438354481f7..0f00dc4c8eb47e67bc0f148c2dd109f73a451e0a:/symm/t/xtea-test.c diff --git a/symm/t/xtea-test.c b/symm/t/xtea-test.c new file mode 100644 index 00000000..49632b16 --- /dev/null +++ b/symm/t/xtea-test.c @@ -0,0 +1,60 @@ +#include +#include + +/* --- Needham and Wheeler's original code --- * + * + * Almost. I changed the types from long to unsigned long. + */ + +void tean(unsigned long * v, unsigned long * k, long N) { +unsigned long y=v[0], z=v[1], DELTA=0x9e3779b9 ; +if (N>0) { + /* coding */ + unsigned long limit=DELTA*N, sum=0 ; + while (sum!=limit) + y+= (z<<4 ^ z>>5) + z ^ sum + k[sum&3], + sum+=DELTA, + z+= (y<<4 ^ y>>5) + y ^ sum + k[sum>>11 &3] ; + } +else { + + /* decoding */ + unsigned long sum=DELTA*(-N) ; + while (sum) + z-= (y<<4 ^ y>>5) + y ^ sum + k[sum>>11 &3], + sum-=DELTA, + y-= (z<<4 ^ z>>5) + z ^ sum + k[sum&3]; + } +v[0]=y, v[1]=z ; +return ; +} + +int main(void) +{ + unsigned long k[4] = { 0x00112233, 0x44556677, 0x8899aabb, 0xccddeeff }; + unsigned long p[2] = { 0x01234567, 0x89abcdef }; + fibrand f; + + int i; + + printf(" %08lx%08lx%08lx%08lx %08lx%08lx ", + k[0], k[1], k[2], k[3], p[0], p[1]); + tean(p, k, 32); + printf("%08lx%08lx;\n", p[0], p[1]); + + fibrand_lcseed(&f, 0); + for (i = 1; i < 64; i++) { + k[0] = fibrand_step(&f); + k[1] = fibrand_step(&f); + k[2] = fibrand_step(&f); + k[3] = fibrand_step(&f); + p[0] = fibrand_step(&f); + p[1] = fibrand_step(&f); + printf(" %08lx%08lx%08lx%08lx %08lx%08lx ", + k[0], k[1], k[2], k[3], p[0], p[1]); + tean(p, k, 32); + printf("%08lx%08lx;\n", p[0], p[1]); + } + + return (0); +}