blob: 9fc4acda23a3594eb051ddfcc3b638afa2444585 [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
2 * An 32-bit implementation of the XTEA algorithm
3 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00004 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakker7a7c78f2009-01-04 18:15:48 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "polarssl/config.h"
22
23#if defined(POLARSSL_XTEA_C)
24
25#include "polarssl/xtea.h"
26
27#include <string.h>
28
29/*
30 * 32-bit integer manipulation macros (big endian)
31 */
32#ifndef GET_ULONG_BE
33#define GET_ULONG_BE(n,b,i) \
34{ \
35 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
36 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
37 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
38 | ( (unsigned long) (b)[(i) + 3] ); \
39}
40#endif
41
42#ifndef PUT_ULONG_BE
43#define PUT_ULONG_BE(n,b,i) \
44{ \
45 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
46 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
47 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
48 (b)[(i) + 3] = (unsigned char) ( (n) ); \
49}
50#endif
51
52/*
53 * XTEA key schedule
54 */
55void xtea_setup( xtea_context *ctx, unsigned char key[16] )
56{
57 int i;
58
59 memset(ctx, 0, sizeof(xtea_context));
60
61 for( i = 0; i < 4; i++ )
62 {
63 GET_ULONG_BE( ctx->k[i], key, i << 2 );
64 }
65}
66
67/*
68 * XTEA encrypt function
69 */
70void xtea_crypt_ecb( xtea_context *ctx, int mode, unsigned char input[8], unsigned char output[8])
71{
72 unsigned long *k, v0, v1, i;
73
74 k = ctx->k;
75
76 GET_ULONG_BE( v0, input, 0 );
77 GET_ULONG_BE( v1, input, 4 );
78
79 if( mode == XTEA_ENCRYPT )
80 {
81 unsigned long sum = 0, delta = 0x9E3779B9;
82
83 for( i = 0; i < 32; i++ )
84 {
85 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
86 sum += delta;
87 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
88 }
89 }
90 else /* XTEA_DECRYPT */
91 {
92 unsigned long delta = 0x9E3779B9, sum = delta * 32;
93
94 for( i = 0; i < 32; i++ )
95 {
96 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
97 sum -= delta;
98 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
99 }
100 }
101
102 PUT_ULONG_BE( v0, output, 0 );
103 PUT_ULONG_BE( v1, output, 4 );
104}
105
106#if defined(POLARSSL_SELF_TEST)
107
108#include <string.h>
109#include <stdio.h>
110
111/*
112 * XTEA tests vectors (non-official)
113 */
114
115static const unsigned char xtea_test_key[6][16] =
116{
117 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
118 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
119 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
120 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
121 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
122 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
123};
124
125static const unsigned char xtea_test_pt[6][8] =
126{
127 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
128 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
129 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
130 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
131 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
132 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
133};
134
135static const unsigned char xtea_test_ct[6][8] =
136{
137 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
138 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
139 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
140 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
141 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
142 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
143};
144
145/*
146 * Checkup routine
147 */
148int xtea_self_test( int verbose )
149{
150 int i;
151 unsigned char buf[8];
152 xtea_context ctx;
153
154 for( i = 0; i < 6; i++ )
155 {
156 if( verbose != 0 )
157 printf( " XTEA test #%d: ", i + 1 );
158
159 memcpy( buf, xtea_test_pt[i], 8 );
160
161 xtea_setup( &ctx, (unsigned char *) xtea_test_key[i] );
162 xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf );
163
164 if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
165 {
166 if( verbose != 0 )
167 printf( "failed\n" );
168
169 return( 1 );
170 }
171
172 if( verbose != 0 )
173 printf( "passed\n" );
174 }
175
176 if( verbose != 0 )
177 printf( "\n" );
178
179 return( 0 );
180}
181
182#endif
183
184#endif