mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
0:cdf462088d13
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

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