Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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