Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * An 32-bit implementation of the XTEA algorithm |
| 3 | * |
Paul Bakker | 785a9ee | 2009-01-25 14:15:10 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 5 | * |
| 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 ) \ |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame^] | 36 | | ( (unsigned long) (b)[(i) + 1] << 16 ) \ |
| 37 | | ( (unsigned long) (b)[(i) + 2] << 8 ) \ |
| 38 | | ( (unsigned long) (b)[(i) + 3] ); \ |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 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 | */ |
| 55 | void 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 | */ |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame^] | 70 | void xtea_crypt_ecb( xtea_context *ctx, int mode, unsigned char input[8], |
| 71 | unsigned char output[8]) |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 72 | { |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame^] | 73 | uint32_t *k, v0, v1, i; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 74 | |
| 75 | k = ctx->k; |
| 76 | |
| 77 | GET_ULONG_BE( v0, input, 0 ); |
| 78 | GET_ULONG_BE( v1, input, 4 ); |
| 79 | |
| 80 | if( mode == XTEA_ENCRYPT ) |
| 81 | { |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame^] | 82 | uint32_t sum = 0, delta = 0x9E3779B9; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 83 | |
| 84 | for( i = 0; i < 32; i++ ) |
| 85 | { |
| 86 | v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
| 87 | sum += delta; |
| 88 | v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); |
| 89 | } |
| 90 | } |
| 91 | else /* XTEA_DECRYPT */ |
| 92 | { |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame^] | 93 | uint32_t delta = 0x9E3779B9, sum = delta * 32; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 94 | |
| 95 | for( i = 0; i < 32; i++ ) |
| 96 | { |
| 97 | v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); |
| 98 | sum -= delta; |
| 99 | v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | PUT_ULONG_BE( v0, output, 0 ); |
| 104 | PUT_ULONG_BE( v1, output, 4 ); |
| 105 | } |
| 106 | |
| 107 | #if defined(POLARSSL_SELF_TEST) |
| 108 | |
| 109 | #include <string.h> |
| 110 | #include <stdio.h> |
| 111 | |
| 112 | /* |
| 113 | * XTEA tests vectors (non-official) |
| 114 | */ |
| 115 | |
| 116 | static const unsigned char xtea_test_key[6][16] = |
| 117 | { |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame^] | 118 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 119 | 0x0c, 0x0d, 0x0e, 0x0f }, |
| 120 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 121 | 0x0c, 0x0d, 0x0e, 0x0f }, |
| 122 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 123 | 0x0c, 0x0d, 0x0e, 0x0f }, |
| 124 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 125 | 0x00, 0x00, 0x00, 0x00 }, |
| 126 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 127 | 0x00, 0x00, 0x00, 0x00 }, |
| 128 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 129 | 0x00, 0x00, 0x00, 0x00 } |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static const unsigned char xtea_test_pt[6][8] = |
| 133 | { |
| 134 | { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, |
| 135 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
| 136 | { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, |
| 137 | { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, |
| 138 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
| 139 | { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } |
| 140 | }; |
| 141 | |
| 142 | static const unsigned char xtea_test_ct[6][8] = |
| 143 | { |
| 144 | { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, |
| 145 | { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, |
| 146 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
| 147 | { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, |
| 148 | { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, |
| 149 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } |
| 150 | }; |
| 151 | |
| 152 | /* |
| 153 | * Checkup routine |
| 154 | */ |
| 155 | int xtea_self_test( int verbose ) |
| 156 | { |
| 157 | int i; |
| 158 | unsigned char buf[8]; |
| 159 | xtea_context ctx; |
| 160 | |
| 161 | for( i = 0; i < 6; i++ ) |
| 162 | { |
| 163 | if( verbose != 0 ) |
| 164 | printf( " XTEA test #%d: ", i + 1 ); |
| 165 | |
| 166 | memcpy( buf, xtea_test_pt[i], 8 ); |
| 167 | |
| 168 | xtea_setup( &ctx, (unsigned char *) xtea_test_key[i] ); |
| 169 | xtea_crypt_ecb( &ctx, XTEA_ENCRYPT, buf, buf ); |
| 170 | |
| 171 | if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 ) |
| 172 | { |
| 173 | if( verbose != 0 ) |
| 174 | printf( "failed\n" ); |
| 175 | |
| 176 | return( 1 ); |
| 177 | } |
| 178 | |
| 179 | if( verbose != 0 ) |
| 180 | printf( "passed\n" ); |
| 181 | } |
| 182 | |
| 183 | if( verbose != 0 ) |
| 184 | printf( "\n" ); |
| 185 | |
| 186 | return( 0 ); |
| 187 | } |
| 188 | |
| 189 | #endif |
| 190 | |
| 191 | #endif |