mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 18:41:59 2017 +0100
Revision:
1:9ebc941037d5
Parent:
0:cdf462088d13
Update to mbedtls 2.4.2, security fixes

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /*
markrad 0:cdf462088d13 2 * RFC 1521 base64 encoding/decoding
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_BASE64_C)
markrad 0:cdf462088d13 29
markrad 0:cdf462088d13 30 #include "mbedtls/base64.h"
markrad 0:cdf462088d13 31
markrad 0:cdf462088d13 32 #include <stdint.h>
markrad 0:cdf462088d13 33
markrad 0:cdf462088d13 34 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 35 #include <string.h>
markrad 0:cdf462088d13 36 #if defined(MBEDTLS_PLATFORM_C)
markrad 0:cdf462088d13 37 #include "mbedtls/platform.h"
markrad 0:cdf462088d13 38 #else
markrad 0:cdf462088d13 39 #include <stdio.h>
markrad 0:cdf462088d13 40 #define mbedtls_printf printf
markrad 0:cdf462088d13 41 #endif /* MBEDTLS_PLATFORM_C */
markrad 0:cdf462088d13 42 #endif /* MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 43
markrad 0:cdf462088d13 44 static const unsigned char base64_enc_map[64] =
markrad 0:cdf462088d13 45 {
markrad 0:cdf462088d13 46 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
markrad 0:cdf462088d13 47 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
markrad 0:cdf462088d13 48 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
markrad 0:cdf462088d13 49 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
markrad 0:cdf462088d13 50 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
markrad 0:cdf462088d13 51 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
markrad 0:cdf462088d13 52 '8', '9', '+', '/'
markrad 0:cdf462088d13 53 };
markrad 0:cdf462088d13 54
markrad 0:cdf462088d13 55 static const unsigned char base64_dec_map[128] =
markrad 0:cdf462088d13 56 {
markrad 0:cdf462088d13 57 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
markrad 0:cdf462088d13 58 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
markrad 0:cdf462088d13 59 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
markrad 0:cdf462088d13 60 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
markrad 0:cdf462088d13 61 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
markrad 0:cdf462088d13 62 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
markrad 0:cdf462088d13 63 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
markrad 0:cdf462088d13 64 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
markrad 0:cdf462088d13 65 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
markrad 0:cdf462088d13 66 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
markrad 0:cdf462088d13 67 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
markrad 0:cdf462088d13 68 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
markrad 0:cdf462088d13 69 49, 50, 51, 127, 127, 127, 127, 127
markrad 0:cdf462088d13 70 };
markrad 0:cdf462088d13 71
markrad 0:cdf462088d13 72 #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
markrad 0:cdf462088d13 73
markrad 0:cdf462088d13 74 /*
markrad 0:cdf462088d13 75 * Encode a buffer into base64 format
markrad 0:cdf462088d13 76 */
markrad 0:cdf462088d13 77 int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
markrad 0:cdf462088d13 78 const unsigned char *src, size_t slen )
markrad 0:cdf462088d13 79 {
markrad 0:cdf462088d13 80 size_t i, n;
markrad 0:cdf462088d13 81 int C1, C2, C3;
markrad 0:cdf462088d13 82 unsigned char *p;
markrad 0:cdf462088d13 83
markrad 0:cdf462088d13 84 if( slen == 0 )
markrad 0:cdf462088d13 85 {
markrad 0:cdf462088d13 86 *olen = 0;
markrad 0:cdf462088d13 87 return( 0 );
markrad 0:cdf462088d13 88 }
markrad 0:cdf462088d13 89
markrad 0:cdf462088d13 90 n = slen / 3 + ( slen % 3 != 0 );
markrad 0:cdf462088d13 91
markrad 0:cdf462088d13 92 if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
markrad 0:cdf462088d13 93 {
markrad 0:cdf462088d13 94 *olen = BASE64_SIZE_T_MAX;
markrad 0:cdf462088d13 95 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 96 }
markrad 0:cdf462088d13 97
markrad 0:cdf462088d13 98 n *= 4;
markrad 0:cdf462088d13 99
markrad 0:cdf462088d13 100 if( ( dlen < n + 1 ) || ( NULL == dst ) )
markrad 0:cdf462088d13 101 {
markrad 0:cdf462088d13 102 *olen = n + 1;
markrad 0:cdf462088d13 103 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 104 }
markrad 0:cdf462088d13 105
markrad 0:cdf462088d13 106 n = ( slen / 3 ) * 3;
markrad 0:cdf462088d13 107
markrad 0:cdf462088d13 108 for( i = 0, p = dst; i < n; i += 3 )
markrad 0:cdf462088d13 109 {
markrad 0:cdf462088d13 110 C1 = *src++;
markrad 0:cdf462088d13 111 C2 = *src++;
markrad 0:cdf462088d13 112 C3 = *src++;
markrad 0:cdf462088d13 113
markrad 0:cdf462088d13 114 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
markrad 0:cdf462088d13 115 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
markrad 0:cdf462088d13 116 *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
markrad 0:cdf462088d13 117 *p++ = base64_enc_map[C3 & 0x3F];
markrad 0:cdf462088d13 118 }
markrad 0:cdf462088d13 119
markrad 0:cdf462088d13 120 if( i < slen )
markrad 0:cdf462088d13 121 {
markrad 0:cdf462088d13 122 C1 = *src++;
markrad 0:cdf462088d13 123 C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
markrad 0:cdf462088d13 124
markrad 0:cdf462088d13 125 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
markrad 0:cdf462088d13 126 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
markrad 0:cdf462088d13 127
markrad 0:cdf462088d13 128 if( ( i + 1 ) < slen )
markrad 0:cdf462088d13 129 *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
markrad 0:cdf462088d13 130 else *p++ = '=';
markrad 0:cdf462088d13 131
markrad 0:cdf462088d13 132 *p++ = '=';
markrad 0:cdf462088d13 133 }
markrad 0:cdf462088d13 134
markrad 0:cdf462088d13 135 *olen = p - dst;
markrad 0:cdf462088d13 136 *p = 0;
markrad 0:cdf462088d13 137
markrad 0:cdf462088d13 138 return( 0 );
markrad 0:cdf462088d13 139 }
markrad 0:cdf462088d13 140
markrad 0:cdf462088d13 141 /*
markrad 0:cdf462088d13 142 * Decode a base64-formatted buffer
markrad 0:cdf462088d13 143 */
markrad 0:cdf462088d13 144 int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
markrad 0:cdf462088d13 145 const unsigned char *src, size_t slen )
markrad 0:cdf462088d13 146 {
markrad 0:cdf462088d13 147 size_t i, n;
markrad 0:cdf462088d13 148 uint32_t j, x;
markrad 0:cdf462088d13 149 unsigned char *p;
markrad 0:cdf462088d13 150
markrad 0:cdf462088d13 151 /* First pass: check for validity and get output length */
markrad 0:cdf462088d13 152 for( i = n = j = 0; i < slen; i++ )
markrad 0:cdf462088d13 153 {
markrad 0:cdf462088d13 154 /* Skip spaces before checking for EOL */
markrad 0:cdf462088d13 155 x = 0;
markrad 0:cdf462088d13 156 while( i < slen && src[i] == ' ' )
markrad 0:cdf462088d13 157 {
markrad 0:cdf462088d13 158 ++i;
markrad 0:cdf462088d13 159 ++x;
markrad 0:cdf462088d13 160 }
markrad 0:cdf462088d13 161
markrad 0:cdf462088d13 162 /* Spaces at end of buffer are OK */
markrad 0:cdf462088d13 163 if( i == slen )
markrad 0:cdf462088d13 164 break;
markrad 0:cdf462088d13 165
markrad 0:cdf462088d13 166 if( ( slen - i ) >= 2 &&
markrad 0:cdf462088d13 167 src[i] == '\r' && src[i + 1] == '\n' )
markrad 0:cdf462088d13 168 continue;
markrad 0:cdf462088d13 169
markrad 0:cdf462088d13 170 if( src[i] == '\n' )
markrad 0:cdf462088d13 171 continue;
markrad 0:cdf462088d13 172
markrad 0:cdf462088d13 173 /* Space inside a line is an error */
markrad 0:cdf462088d13 174 if( x != 0 )
markrad 0:cdf462088d13 175 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
markrad 0:cdf462088d13 176
markrad 0:cdf462088d13 177 if( src[i] == '=' && ++j > 2 )
markrad 0:cdf462088d13 178 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
markrad 0:cdf462088d13 179
markrad 0:cdf462088d13 180 if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
markrad 0:cdf462088d13 181 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
markrad 0:cdf462088d13 182
markrad 0:cdf462088d13 183 if( base64_dec_map[src[i]] < 64 && j != 0 )
markrad 0:cdf462088d13 184 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
markrad 0:cdf462088d13 185
markrad 0:cdf462088d13 186 n++;
markrad 0:cdf462088d13 187 }
markrad 0:cdf462088d13 188
markrad 0:cdf462088d13 189 if( n == 0 )
markrad 0:cdf462088d13 190 {
markrad 0:cdf462088d13 191 *olen = 0;
markrad 0:cdf462088d13 192 return( 0 );
markrad 0:cdf462088d13 193 }
markrad 0:cdf462088d13 194
Jasper Wallace 1:9ebc941037d5 195 /* The following expression is to calculate the following formula without
Jasper Wallace 1:9ebc941037d5 196 * risk of integer overflow in n:
Jasper Wallace 1:9ebc941037d5 197 * n = ( ( n * 6 ) + 7 ) >> 3;
Jasper Wallace 1:9ebc941037d5 198 */
Jasper Wallace 1:9ebc941037d5 199 n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
markrad 0:cdf462088d13 200 n -= j;
markrad 0:cdf462088d13 201
markrad 0:cdf462088d13 202 if( dst == NULL || dlen < n )
markrad 0:cdf462088d13 203 {
markrad 0:cdf462088d13 204 *olen = n;
markrad 0:cdf462088d13 205 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 206 }
markrad 0:cdf462088d13 207
markrad 0:cdf462088d13 208 for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
markrad 0:cdf462088d13 209 {
markrad 0:cdf462088d13 210 if( *src == '\r' || *src == '\n' || *src == ' ' )
markrad 0:cdf462088d13 211 continue;
markrad 0:cdf462088d13 212
markrad 0:cdf462088d13 213 j -= ( base64_dec_map[*src] == 64 );
markrad 0:cdf462088d13 214 x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );
markrad 0:cdf462088d13 215
markrad 0:cdf462088d13 216 if( ++n == 4 )
markrad 0:cdf462088d13 217 {
markrad 0:cdf462088d13 218 n = 0;
markrad 0:cdf462088d13 219 if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
markrad 0:cdf462088d13 220 if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
markrad 0:cdf462088d13 221 if( j > 2 ) *p++ = (unsigned char)( x );
markrad 0:cdf462088d13 222 }
markrad 0:cdf462088d13 223 }
markrad 0:cdf462088d13 224
markrad 0:cdf462088d13 225 *olen = p - dst;
markrad 0:cdf462088d13 226
markrad 0:cdf462088d13 227 return( 0 );
markrad 0:cdf462088d13 228 }
markrad 0:cdf462088d13 229
markrad 0:cdf462088d13 230 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 231
markrad 0:cdf462088d13 232 static const unsigned char base64_test_dec[64] =
markrad 0:cdf462088d13 233 {
markrad 0:cdf462088d13 234 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
markrad 0:cdf462088d13 235 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
markrad 0:cdf462088d13 236 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
markrad 0:cdf462088d13 237 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
markrad 0:cdf462088d13 238 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
markrad 0:cdf462088d13 239 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
markrad 0:cdf462088d13 240 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
markrad 0:cdf462088d13 241 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
markrad 0:cdf462088d13 242 };
markrad 0:cdf462088d13 243
markrad 0:cdf462088d13 244 static const unsigned char base64_test_enc[] =
markrad 0:cdf462088d13 245 "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
markrad 0:cdf462088d13 246 "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
markrad 0:cdf462088d13 247
markrad 0:cdf462088d13 248 /*
markrad 0:cdf462088d13 249 * Checkup routine
markrad 0:cdf462088d13 250 */
markrad 0:cdf462088d13 251 int mbedtls_base64_self_test( int verbose )
markrad 0:cdf462088d13 252 {
markrad 0:cdf462088d13 253 size_t len;
markrad 0:cdf462088d13 254 const unsigned char *src;
markrad 0:cdf462088d13 255 unsigned char buffer[128];
markrad 0:cdf462088d13 256
markrad 0:cdf462088d13 257 if( verbose != 0 )
markrad 0:cdf462088d13 258 mbedtls_printf( " Base64 encoding test: " );
markrad 0:cdf462088d13 259
markrad 0:cdf462088d13 260 src = base64_test_dec;
markrad 0:cdf462088d13 261
markrad 0:cdf462088d13 262 if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
markrad 0:cdf462088d13 263 memcmp( base64_test_enc, buffer, 88 ) != 0 )
markrad 0:cdf462088d13 264 {
markrad 0:cdf462088d13 265 if( verbose != 0 )
markrad 0:cdf462088d13 266 mbedtls_printf( "failed\n" );
markrad 0:cdf462088d13 267
markrad 0:cdf462088d13 268 return( 1 );
markrad 0:cdf462088d13 269 }
markrad 0:cdf462088d13 270
markrad 0:cdf462088d13 271 if( verbose != 0 )
markrad 0:cdf462088d13 272 mbedtls_printf( "passed\n Base64 decoding test: " );
markrad 0:cdf462088d13 273
markrad 0:cdf462088d13 274 src = base64_test_enc;
markrad 0:cdf462088d13 275
markrad 0:cdf462088d13 276 if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
markrad 0:cdf462088d13 277 memcmp( base64_test_dec, buffer, 64 ) != 0 )
markrad 0:cdf462088d13 278 {
markrad 0:cdf462088d13 279 if( verbose != 0 )
markrad 0:cdf462088d13 280 mbedtls_printf( "failed\n" );
markrad 0:cdf462088d13 281
markrad 0:cdf462088d13 282 return( 1 );
markrad 0:cdf462088d13 283 }
markrad 0:cdf462088d13 284
markrad 0:cdf462088d13 285 if( verbose != 0 )
markrad 0:cdf462088d13 286 mbedtls_printf( "passed\n\n" );
markrad 0:cdf462088d13 287
markrad 0:cdf462088d13 288 return( 0 );
markrad 0:cdf462088d13 289 }
markrad 0:cdf462088d13 290
markrad 0:cdf462088d13 291 #endif /* MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 292
markrad 0:cdf462088d13 293 #endif /* MBEDTLS_BASE64_C */