nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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