chiark / gitweb /
Rearrange the file tree.
[catacomb] / symm / t / xtea-test.c
1 #include <stdio.h>
2 #include <catacomb/fibrand.h>
3
4 /* --- Needham and Wheeler's original code --- *
5  *
6  * Almost.  I changed the types from long to unsigned long.
7  */
8
9 void tean(unsigned long * v, unsigned long * k, long N) {
10 unsigned long y=v[0], z=v[1], DELTA=0x9e3779b9 ;
11 if (N>0) {
12        /* coding */
13    unsigned long limit=DELTA*N, sum=0 ;
14    while (sum!=limit)
15      y+= (z<<4 ^ z>>5) + z ^ sum + k[sum&3],
16      sum+=DELTA,
17      z+= (y<<4 ^ y>>5) + y ^ sum + k[sum>>11 &3] ;
18          }
19 else {
20
21           /* decoding */
22    unsigned long sum=DELTA*(-N) ;
23    while (sum)
24      z-= (y<<4 ^ y>>5) + y ^ sum + k[sum>>11 &3],
25      sum-=DELTA,
26      y-= (z<<4 ^ z>>5) + z ^ sum + k[sum&3];
27       }
28 v[0]=y, v[1]=z ;
29 return ;
30 }
31
32 int main(void)
33 {
34   unsigned long k[4] = { 0x00112233, 0x44556677, 0x8899aabb, 0xccddeeff };
35   unsigned long p[2] = { 0x01234567, 0x89abcdef };
36   fibrand f;
37
38   int i;
39
40   printf("  %08lx%08lx%08lx%08lx %08lx%08lx ",
41          k[0], k[1], k[2], k[3], p[0], p[1]);
42   tean(p, k, 32);
43   printf("%08lx%08lx;\n", p[0], p[1]);
44
45   fibrand_lcseed(&f, 0);
46   for (i = 1; i < 64; i++) {
47     k[0] = fibrand_step(&f);
48     k[1] = fibrand_step(&f);
49     k[2] = fibrand_step(&f);
50     k[3] = fibrand_step(&f);
51     p[0] = fibrand_step(&f);
52     p[1] = fibrand_step(&f);
53     printf("  %08lx%08lx%08lx%08lx %08lx%08lx ",
54            k[0], k[1], k[2], k[3], p[0], p[1]);
55     tean(p, k, 32);
56     printf("%08lx%08lx;\n", p[0], p[1]);
57   }
58
59   return (0);
60 }