]> pd.if.org Git - zpackage/blob - libtomcrypt/src/ciphers/xtea.c
remove ltc encauth eax
[zpackage] / libtomcrypt / src / ciphers / xtea.c
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
10 /**
11   @file xtea.c
12   Implementation of LTC_XTEA, Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_XTEA
17
18 const struct ltc_cipher_descriptor xtea_desc =
19 {
20     "xtea",
21     1,
22     16, 16, 8, 32,
23     &xtea_setup,
24     &xtea_ecb_encrypt,
25     &xtea_ecb_decrypt,
26     &xtea_test,
27     &xtea_done,
28     &xtea_keysize,
29     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
30 };
31
32 int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
33 {
34    ulong32 x, sum, K[4];
35
36    LTC_ARGCHK(key != NULL);
37    LTC_ARGCHK(skey != NULL);
38
39    /* check arguments */
40    if (keylen != 16) {
41       return CRYPT_INVALID_KEYSIZE;
42    }
43
44    if (num_rounds != 0 && num_rounds != 32) {
45       return CRYPT_INVALID_ROUNDS;
46    }
47
48    /* load key */
49    LOAD32H(K[0], key+0);
50    LOAD32H(K[1], key+4);
51    LOAD32H(K[2], key+8);
52    LOAD32H(K[3], key+12);
53
54    for (x = sum = 0; x < 32; x++) {
55        skey->xtea.A[x] = (sum + K[sum&3]) & 0xFFFFFFFFUL;
56        sum = (sum + 0x9E3779B9UL) & 0xFFFFFFFFUL;
57        skey->xtea.B[x] = (sum + K[(sum>>11)&3]) & 0xFFFFFFFFUL;
58    }
59
60 #ifdef LTC_CLEAN_STACK
61    zeromem(&K, sizeof(K));
62 #endif
63
64    return CRYPT_OK;
65 }
66
67 /**
68   Encrypts a block of text with LTC_XTEA
69   @param pt The input plaintext (8 bytes)
70   @param ct The output ciphertext (8 bytes)
71   @param skey The key as scheduled
72   @return CRYPT_OK if successful
73 */
74 int xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
75 {
76    ulong32 y, z;
77    int r;
78
79    LTC_ARGCHK(pt   != NULL);
80    LTC_ARGCHK(ct   != NULL);
81    LTC_ARGCHK(skey != NULL);
82
83    LOAD32H(y, &pt[0]);
84    LOAD32H(z, &pt[4]);
85    for (r = 0; r < 32; r += 4) {
86        y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL;
87        z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL;
88
89        y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+1])) & 0xFFFFFFFFUL;
90        z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+1])) & 0xFFFFFFFFUL;
91
92        y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+2])) & 0xFFFFFFFFUL;
93        z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+2])) & 0xFFFFFFFFUL;
94
95        y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+3])) & 0xFFFFFFFFUL;
96        z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+3])) & 0xFFFFFFFFUL;
97    }
98    STORE32H(y, &ct[0]);
99    STORE32H(z, &ct[4]);
100    return CRYPT_OK;
101 }
102
103 /**
104   Decrypts a block of text with LTC_XTEA
105   @param ct The input ciphertext (8 bytes)
106   @param pt The output plaintext (8 bytes)
107   @param skey The key as scheduled
108   @return CRYPT_OK if successful
109 */
110 int xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
111 {
112    ulong32 y, z;
113    int r;
114
115    LTC_ARGCHK(pt   != NULL);
116    LTC_ARGCHK(ct   != NULL);
117    LTC_ARGCHK(skey != NULL);
118
119    LOAD32H(y, &ct[0]);
120    LOAD32H(z, &ct[4]);
121    for (r = 31; r >= 0; r -= 4) {
122        z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r])) & 0xFFFFFFFFUL;
123        y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r])) & 0xFFFFFFFFUL;
124
125        z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-1])) & 0xFFFFFFFFUL;
126        y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-1])) & 0xFFFFFFFFUL;
127
128        z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-2])) & 0xFFFFFFFFUL;
129        y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-2])) & 0xFFFFFFFFUL;
130
131        z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-3])) & 0xFFFFFFFFUL;
132        y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-3])) & 0xFFFFFFFFUL;
133    }
134    STORE32H(y, &pt[0]);
135    STORE32H(z, &pt[4]);
136    return CRYPT_OK;
137 }
138
139 /**
140   Performs a self-test of the LTC_XTEA block cipher
141   @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
142 */
143 int xtea_test(void)
144 {
145  #ifndef LTC_TEST
146     return CRYPT_NOP;
147  #else
148     static const struct {
149         unsigned char key[16], pt[8], ct[8];
150     } tests[] = {
151        {
152          { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
154          { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
155          { 0xde, 0xe9, 0xd4, 0xd8, 0xf7, 0x13, 0x1e, 0xd9 }
156        }, {
157          { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
158            0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04 },
159          { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
160          { 0xa5, 0x97, 0xab, 0x41, 0x76, 0x01, 0x4d, 0x72 }
161        }, {
162          { 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04,
163            0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06 },
164          { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02 },
165          { 0xb1, 0xfd, 0x5d, 0xa9, 0xcc, 0x6d, 0xc9, 0xdc }
166        }, {
167          { 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f,
168            0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 },
169          { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 },
170          { 0x70, 0x4b, 0x31, 0x34, 0x47, 0x44, 0xdf, 0xab }
171        }, {
172          { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
173            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
174          { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
175          { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }
176        }, {
177          { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
178            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
179          { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
180          { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }
181        }, {
182          { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
183            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
184          { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
185          { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
186        }, {
187          { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
188            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
189          { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
190          { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }
191        }, {
192          { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
194          { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
195          { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }
196        }, {
197          { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
199          { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 },
200          { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
201        }
202     };
203    unsigned char tmp[2][8];
204    symmetric_key skey;
205    int i, err, y;
206    for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
207        zeromem(&skey, sizeof(skey));
208        if ((err = xtea_setup(tests[i].key, 16, 0, &skey)) != CRYPT_OK)  {
209           return err;
210        }
211        xtea_ecb_encrypt(tests[i].pt, tmp[0], &skey);
212        xtea_ecb_decrypt(tmp[0], tmp[1], &skey);
213
214        if (compare_testvector(tmp[0], 8, tests[i].ct, 8, "XTEA Encrypt", i) != 0 ||
215              compare_testvector(tmp[1], 8, tests[i].pt, 8, "XTEA Decrypt", i) != 0) {
216           return CRYPT_FAIL_TESTVECTOR;
217        }
218
219       /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
220       for (y = 0; y < 8; y++) tmp[0][y] = 0;
221       for (y = 0; y < 1000; y++) xtea_ecb_encrypt(tmp[0], tmp[0], &skey);
222       for (y = 0; y < 1000; y++) xtea_ecb_decrypt(tmp[0], tmp[0], &skey);
223       for (y = 0; y < 8; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
224    } /* for */
225
226    return CRYPT_OK;
227  #endif
228 }
229
230 /** Terminate the context
231    @param skey    The scheduled key
232 */
233 void xtea_done(symmetric_key *skey)
234 {
235   LTC_UNUSED_PARAM(skey);
236 }
237
238 /**
239   Gets suitable key size
240   @param keysize [in/out] The length of the recommended key (in bytes).  This function will store the suitable size back in this variable.
241   @return CRYPT_OK if the input key size is acceptable.
242 */
243 int xtea_keysize(int *keysize)
244 {
245    LTC_ARGCHK(keysize != NULL);
246    if (*keysize < 16) {
247       return CRYPT_INVALID_KEYSIZE;
248    }
249    *keysize = 16;
250    return CRYPT_OK;
251 }
252
253
254 #endif
255
256
257
258
259 /* ref:         $Format:%D$ */
260 /* git commit:  $Format:%H$ */
261 /* commit time: $Format:%ai$ */