xref: /reactos/dll/3rdparty/mbedtls/xtea.c (revision 94a413ae)
1 /*
2  *  An 32-bit implementation of the XTEA algorithm
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  *
46  *  This file is part of mbed TLS (https://tls.mbed.org)
47  */
48 
49 #if !defined(MBEDTLS_CONFIG_FILE)
50 #include "mbedtls/config.h"
51 #else
52 #include MBEDTLS_CONFIG_FILE
53 #endif
54 
55 #if defined(MBEDTLS_XTEA_C)
56 
57 #include "mbedtls/xtea.h"
58 
59 #include <string.h>
60 
61 #if defined(MBEDTLS_SELF_TEST)
62 #if defined(MBEDTLS_PLATFORM_C)
63 #include "mbedtls/platform.h"
64 #else
65 #include <stdio.h>
66 #define mbedtls_printf printf
67 #endif /* MBEDTLS_PLATFORM_C */
68 #endif /* MBEDTLS_SELF_TEST */
69 
70 #if !defined(MBEDTLS_XTEA_ALT)
71 
72 /* Implementation that should never be optimized out by the compiler */
73 static void mbedtls_zeroize( void *v, size_t n ) {
74     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
75 }
76 
77 /*
78  * 32-bit integer manipulation macros (big endian)
79  */
80 #ifndef GET_UINT32_BE
81 #define GET_UINT32_BE(n,b,i)                            \
82 {                                                       \
83     (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \
84         | ( (uint32_t) (b)[(i) + 1] << 16 )             \
85         | ( (uint32_t) (b)[(i) + 2] <<  8 )             \
86         | ( (uint32_t) (b)[(i) + 3]       );            \
87 }
88 #endif
89 
90 #ifndef PUT_UINT32_BE
91 #define PUT_UINT32_BE(n,b,i)                            \
92 {                                                       \
93     (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
94     (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
95     (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
96     (b)[(i) + 3] = (unsigned char) ( (n)       );       \
97 }
98 #endif
99 
100 void mbedtls_xtea_init( mbedtls_xtea_context *ctx )
101 {
102     memset( ctx, 0, sizeof( mbedtls_xtea_context ) );
103 }
104 
105 void mbedtls_xtea_free( mbedtls_xtea_context *ctx )
106 {
107     if( ctx == NULL )
108         return;
109 
110     mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) );
111 }
112 
113 /*
114  * XTEA key schedule
115  */
116 void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )
117 {
118     int i;
119 
120     memset( ctx, 0, sizeof(mbedtls_xtea_context) );
121 
122     for( i = 0; i < 4; i++ )
123     {
124         GET_UINT32_BE( ctx->k[i], key, i << 2 );
125     }
126 }
127 
128 /*
129  * XTEA encrypt function
130  */
131 int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,
132                     const unsigned char input[8], unsigned char output[8])
133 {
134     uint32_t *k, v0, v1, i;
135 
136     k = ctx->k;
137 
138     GET_UINT32_BE( v0, input, 0 );
139     GET_UINT32_BE( v1, input, 4 );
140 
141     if( mode == MBEDTLS_XTEA_ENCRYPT )
142     {
143         uint32_t sum = 0, delta = 0x9E3779B9;
144 
145         for( i = 0; i < 32; i++ )
146         {
147             v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
148             sum += delta;
149             v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
150         }
151     }
152     else /* MBEDTLS_XTEA_DECRYPT */
153     {
154         uint32_t delta = 0x9E3779B9, sum = delta * 32;
155 
156         for( i = 0; i < 32; i++ )
157         {
158             v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
159             sum -= delta;
160             v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
161         }
162     }
163 
164     PUT_UINT32_BE( v0, output, 0 );
165     PUT_UINT32_BE( v1, output, 4 );
166 
167     return( 0 );
168 }
169 
170 #if defined(MBEDTLS_CIPHER_MODE_CBC)
171 /*
172  * XTEA-CBC buffer encryption/decryption
173  */
174 int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,
175                     unsigned char iv[8], const unsigned char *input,
176                     unsigned char *output)
177 {
178     int i;
179     unsigned char temp[8];
180 
181     if( length % 8 )
182         return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );
183 
184     if( mode == MBEDTLS_XTEA_DECRYPT )
185     {
186         while( length > 0 )
187         {
188             memcpy( temp, input, 8 );
189             mbedtls_xtea_crypt_ecb( ctx, mode, input, output );
190 
191             for( i = 0; i < 8; i++ )
192                 output[i] = (unsigned char)( output[i] ^ iv[i] );
193 
194             memcpy( iv, temp, 8 );
195 
196             input  += 8;
197             output += 8;
198             length -= 8;
199         }
200     }
201     else
202     {
203         while( length > 0 )
204         {
205             for( i = 0; i < 8; i++ )
206                 output[i] = (unsigned char)( input[i] ^ iv[i] );
207 
208             mbedtls_xtea_crypt_ecb( ctx, mode, output, output );
209             memcpy( iv, output, 8 );
210 
211             input  += 8;
212             output += 8;
213             length -= 8;
214         }
215     }
216 
217     return( 0 );
218 }
219 #endif /* MBEDTLS_CIPHER_MODE_CBC */
220 #endif /* !MBEDTLS_XTEA_ALT */
221 
222 #if defined(MBEDTLS_SELF_TEST)
223 
224 /*
225  * XTEA tests vectors (non-official)
226  */
227 
228 static const unsigned char xtea_test_key[6][16] =
229 {
230    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
231      0x0c, 0x0d, 0x0e, 0x0f },
232    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
233      0x0c, 0x0d, 0x0e, 0x0f },
234    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
235      0x0c, 0x0d, 0x0e, 0x0f },
236    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237      0x00, 0x00, 0x00, 0x00 },
238    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239      0x00, 0x00, 0x00, 0x00 },
240    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241      0x00, 0x00, 0x00, 0x00 }
242 };
243 
244 static const unsigned char xtea_test_pt[6][8] =
245 {
246     { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
247     { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
248     { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
249     { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
250     { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
251     { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
252 };
253 
254 static const unsigned char xtea_test_ct[6][8] =
255 {
256     { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
257     { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
258     { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
259     { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
260     { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
261     { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
262 };
263 
264 /*
265  * Checkup routine
266  */
267 int mbedtls_xtea_self_test( int verbose )
268 {
269     int i, ret = 0;
270     unsigned char buf[8];
271     mbedtls_xtea_context ctx;
272 
273     mbedtls_xtea_init( &ctx );
274     for( i = 0; i < 6; i++ )
275     {
276         if( verbose != 0 )
277             mbedtls_printf( "  XTEA test #%d: ", i + 1 );
278 
279         memcpy( buf, xtea_test_pt[i], 8 );
280 
281         mbedtls_xtea_setup( &ctx, xtea_test_key[i] );
282         mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );
283 
284         if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )
285         {
286             if( verbose != 0 )
287                 mbedtls_printf( "failed\n" );
288 
289             ret = 1;
290             goto exit;
291         }
292 
293         if( verbose != 0 )
294             mbedtls_printf( "passed\n" );
295     }
296 
297     if( verbose != 0 )
298         mbedtls_printf( "\n" );
299 
300 exit:
301     mbedtls_xtea_free( &ctx );
302 
303     return( ret );
304 }
305 
306 #endif /* MBEDTLS_SELF_TEST */
307 
308 #endif /* MBEDTLS_XTEA_C */
309