Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
electronichamsters
Date:
Mon Jul 10 04:00:25 2017 +0000
Revision:
5:f09f5ed830ca
Parent:
1:24750b9ad5ef
working gateway

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:24750b9ad5ef 1 /*
Christopher Haster 1:24750b9ad5ef 2 * Platform-specific and custom entropy polling functions
Christopher Haster 1:24750b9ad5ef 3 *
Christopher Haster 1:24750b9ad5ef 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Christopher Haster 1:24750b9ad5ef 5 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:24750b9ad5ef 6 *
Christopher Haster 1:24750b9ad5ef 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Christopher Haster 1:24750b9ad5ef 8 * not use this file except in compliance with the License.
Christopher Haster 1:24750b9ad5ef 9 * You may obtain a copy of the License at
Christopher Haster 1:24750b9ad5ef 10 *
Christopher Haster 1:24750b9ad5ef 11 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:24750b9ad5ef 12 *
Christopher Haster 1:24750b9ad5ef 13 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:24750b9ad5ef 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Christopher Haster 1:24750b9ad5ef 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:24750b9ad5ef 16 * See the License for the specific language governing permissions and
Christopher Haster 1:24750b9ad5ef 17 * limitations under the License.
Christopher Haster 1:24750b9ad5ef 18 *
Christopher Haster 1:24750b9ad5ef 19 * This file is part of mbed TLS (https://tls.mbed.org)
Christopher Haster 1:24750b9ad5ef 20 */
Christopher Haster 1:24750b9ad5ef 21
Christopher Haster 1:24750b9ad5ef 22 #if !defined(MBEDTLS_CONFIG_FILE)
Christopher Haster 1:24750b9ad5ef 23 #include "mbedtls/config.h"
Christopher Haster 1:24750b9ad5ef 24 #else
Christopher Haster 1:24750b9ad5ef 25 #include MBEDTLS_CONFIG_FILE
Christopher Haster 1:24750b9ad5ef 26 #endif
Christopher Haster 1:24750b9ad5ef 27
Christopher Haster 1:24750b9ad5ef 28 #if defined(MBEDTLS_ENTROPY_C)
Christopher Haster 1:24750b9ad5ef 29
Christopher Haster 1:24750b9ad5ef 30 #include "mbedtls/entropy.h"
Christopher Haster 1:24750b9ad5ef 31 #include "mbedtls/entropy_poll.h"
Christopher Haster 1:24750b9ad5ef 32
Christopher Haster 1:24750b9ad5ef 33 #if defined(MBEDTLS_TIMING_C)
Christopher Haster 1:24750b9ad5ef 34 #include <string.h>
Christopher Haster 1:24750b9ad5ef 35 #include "mbedtls/timing.h"
Christopher Haster 1:24750b9ad5ef 36 #endif
Christopher Haster 1:24750b9ad5ef 37 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 38 #include "mbedtls/havege.h"
Christopher Haster 1:24750b9ad5ef 39 #endif
Christopher Haster 1:24750b9ad5ef 40
Christopher Haster 1:24750b9ad5ef 41 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Christopher Haster 1:24750b9ad5ef 42 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Christopher Haster 1:24750b9ad5ef 43
Christopher Haster 1:24750b9ad5ef 44 #if !defined(_WIN32_WINNT)
Christopher Haster 1:24750b9ad5ef 45 #define _WIN32_WINNT 0x0400
Christopher Haster 1:24750b9ad5ef 46 #endif
Christopher Haster 1:24750b9ad5ef 47 #include <windows.h>
Christopher Haster 1:24750b9ad5ef 48 #include <wincrypt.h>
Christopher Haster 1:24750b9ad5ef 49
Christopher Haster 1:24750b9ad5ef 50 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Christopher Haster 1:24750b9ad5ef 51 size_t *olen )
Christopher Haster 1:24750b9ad5ef 52 {
Christopher Haster 1:24750b9ad5ef 53 HCRYPTPROV provider;
Christopher Haster 1:24750b9ad5ef 54 ((void) data);
Christopher Haster 1:24750b9ad5ef 55 *olen = 0;
Christopher Haster 1:24750b9ad5ef 56
Christopher Haster 1:24750b9ad5ef 57 if( CryptAcquireContext( &provider, NULL, NULL,
Christopher Haster 1:24750b9ad5ef 58 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
Christopher Haster 1:24750b9ad5ef 59 {
Christopher Haster 1:24750b9ad5ef 60 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Christopher Haster 1:24750b9ad5ef 61 }
Christopher Haster 1:24750b9ad5ef 62
Christopher Haster 1:24750b9ad5ef 63 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Christopher Haster 1:24750b9ad5ef 64 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Christopher Haster 1:24750b9ad5ef 65
Christopher Haster 1:24750b9ad5ef 66 CryptReleaseContext( provider, 0 );
Christopher Haster 1:24750b9ad5ef 67 *olen = len;
Christopher Haster 1:24750b9ad5ef 68
Christopher Haster 1:24750b9ad5ef 69 return( 0 );
Christopher Haster 1:24750b9ad5ef 70 }
Christopher Haster 1:24750b9ad5ef 71 #else /* _WIN32 && !EFIX64 && !EFI32 */
Christopher Haster 1:24750b9ad5ef 72
Christopher Haster 1:24750b9ad5ef 73 /*
Christopher Haster 1:24750b9ad5ef 74 * Test for Linux getrandom() support.
Christopher Haster 1:24750b9ad5ef 75 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
Christopher Haster 1:24750b9ad5ef 76 * available in GNU libc and compatible libc's (eg uClibc).
Christopher Haster 1:24750b9ad5ef 77 */
Christopher Haster 1:24750b9ad5ef 78 #if defined(__linux__) && defined(__GLIBC__)
Christopher Haster 1:24750b9ad5ef 79 #include <unistd.h>
Christopher Haster 1:24750b9ad5ef 80 #include <sys/syscall.h>
Christopher Haster 1:24750b9ad5ef 81 #if defined(SYS_getrandom)
Christopher Haster 1:24750b9ad5ef 82 #define HAVE_GETRANDOM
Christopher Haster 1:24750b9ad5ef 83
Christopher Haster 1:24750b9ad5ef 84 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
Christopher Haster 1:24750b9ad5ef 85 {
Christopher Haster 1:24750b9ad5ef 86 /* MemSan cannot understand that the syscall writes to the buffer */
Christopher Haster 1:24750b9ad5ef 87 #if defined(__has_feature)
Christopher Haster 1:24750b9ad5ef 88 #if __has_feature(memory_sanitizer)
Christopher Haster 1:24750b9ad5ef 89 memset( buf, 0, buflen );
Christopher Haster 1:24750b9ad5ef 90 #endif
Christopher Haster 1:24750b9ad5ef 91 #endif
Christopher Haster 1:24750b9ad5ef 92
Christopher Haster 1:24750b9ad5ef 93 return( syscall( SYS_getrandom, buf, buflen, flags ) );
Christopher Haster 1:24750b9ad5ef 94 }
Christopher Haster 1:24750b9ad5ef 95
Christopher Haster 1:24750b9ad5ef 96 #include <sys/utsname.h>
Christopher Haster 1:24750b9ad5ef 97 /* Check if version is at least 3.17.0 */
Christopher Haster 1:24750b9ad5ef 98 static int check_version_3_17_plus( void )
Christopher Haster 1:24750b9ad5ef 99 {
Christopher Haster 1:24750b9ad5ef 100 int minor;
Christopher Haster 1:24750b9ad5ef 101 struct utsname un;
Christopher Haster 1:24750b9ad5ef 102 const char *ver;
Christopher Haster 1:24750b9ad5ef 103
Christopher Haster 1:24750b9ad5ef 104 /* Get version information */
Christopher Haster 1:24750b9ad5ef 105 uname(&un);
Christopher Haster 1:24750b9ad5ef 106 ver = un.release;
Christopher Haster 1:24750b9ad5ef 107
Christopher Haster 1:24750b9ad5ef 108 /* Check major version; assume a single digit */
Christopher Haster 1:24750b9ad5ef 109 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
Christopher Haster 1:24750b9ad5ef 110 return( -1 );
Christopher Haster 1:24750b9ad5ef 111
Christopher Haster 1:24750b9ad5ef 112 if( ver[0] - '0' > 3 )
Christopher Haster 1:24750b9ad5ef 113 return( 0 );
Christopher Haster 1:24750b9ad5ef 114
Christopher Haster 1:24750b9ad5ef 115 /* Ok, so now we know major == 3, check minor.
Christopher Haster 1:24750b9ad5ef 116 * Assume 1 or 2 digits. */
Christopher Haster 1:24750b9ad5ef 117 if( ver[2] < '0' || ver[2] > '9' )
Christopher Haster 1:24750b9ad5ef 118 return( -1 );
Christopher Haster 1:24750b9ad5ef 119
Christopher Haster 1:24750b9ad5ef 120 minor = ver[2] - '0';
Christopher Haster 1:24750b9ad5ef 121
Christopher Haster 1:24750b9ad5ef 122 if( ver[3] >= '0' && ver[3] <= '9' )
Christopher Haster 1:24750b9ad5ef 123 minor = 10 * minor + ver[3] - '0';
Christopher Haster 1:24750b9ad5ef 124 else if( ver [3] != '.' )
Christopher Haster 1:24750b9ad5ef 125 return( -1 );
Christopher Haster 1:24750b9ad5ef 126
Christopher Haster 1:24750b9ad5ef 127 if( minor < 17 )
Christopher Haster 1:24750b9ad5ef 128 return( -1 );
Christopher Haster 1:24750b9ad5ef 129
Christopher Haster 1:24750b9ad5ef 130 return( 0 );
Christopher Haster 1:24750b9ad5ef 131 }
Christopher Haster 1:24750b9ad5ef 132 static int has_getrandom = -1;
Christopher Haster 1:24750b9ad5ef 133 #endif /* SYS_getrandom */
Christopher Haster 1:24750b9ad5ef 134 #endif /* __linux__ */
Christopher Haster 1:24750b9ad5ef 135
Christopher Haster 1:24750b9ad5ef 136 #include <stdio.h>
Christopher Haster 1:24750b9ad5ef 137
Christopher Haster 1:24750b9ad5ef 138 int mbedtls_platform_entropy_poll( void *data,
Christopher Haster 1:24750b9ad5ef 139 unsigned char *output, size_t len, size_t *olen )
Christopher Haster 1:24750b9ad5ef 140 {
Christopher Haster 1:24750b9ad5ef 141 FILE *file;
Christopher Haster 1:24750b9ad5ef 142 size_t read_len;
Christopher Haster 1:24750b9ad5ef 143 ((void) data);
Christopher Haster 1:24750b9ad5ef 144
Christopher Haster 1:24750b9ad5ef 145 #if defined(HAVE_GETRANDOM)
Christopher Haster 1:24750b9ad5ef 146 if( has_getrandom == -1 )
Christopher Haster 1:24750b9ad5ef 147 has_getrandom = ( check_version_3_17_plus() == 0 );
Christopher Haster 1:24750b9ad5ef 148
Christopher Haster 1:24750b9ad5ef 149 if( has_getrandom )
Christopher Haster 1:24750b9ad5ef 150 {
Christopher Haster 1:24750b9ad5ef 151 int ret;
Christopher Haster 1:24750b9ad5ef 152
Christopher Haster 1:24750b9ad5ef 153 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Christopher Haster 1:24750b9ad5ef 154 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Christopher Haster 1:24750b9ad5ef 155
Christopher Haster 1:24750b9ad5ef 156 *olen = ret;
Christopher Haster 1:24750b9ad5ef 157 return( 0 );
Christopher Haster 1:24750b9ad5ef 158 }
Christopher Haster 1:24750b9ad5ef 159 #endif /* HAVE_GETRANDOM */
Christopher Haster 1:24750b9ad5ef 160
Christopher Haster 1:24750b9ad5ef 161 *olen = 0;
Christopher Haster 1:24750b9ad5ef 162
Christopher Haster 1:24750b9ad5ef 163 file = fopen( "/dev/urandom", "rb" );
Christopher Haster 1:24750b9ad5ef 164 if( file == NULL )
Christopher Haster 1:24750b9ad5ef 165 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Christopher Haster 1:24750b9ad5ef 166
Christopher Haster 1:24750b9ad5ef 167 read_len = fread( output, 1, len, file );
Christopher Haster 1:24750b9ad5ef 168 if( read_len != len )
Christopher Haster 1:24750b9ad5ef 169 {
Christopher Haster 1:24750b9ad5ef 170 fclose( file );
Christopher Haster 1:24750b9ad5ef 171 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Christopher Haster 1:24750b9ad5ef 172 }
Christopher Haster 1:24750b9ad5ef 173
Christopher Haster 1:24750b9ad5ef 174 fclose( file );
Christopher Haster 1:24750b9ad5ef 175 *olen = len;
Christopher Haster 1:24750b9ad5ef 176
Christopher Haster 1:24750b9ad5ef 177 return( 0 );
Christopher Haster 1:24750b9ad5ef 178 }
Christopher Haster 1:24750b9ad5ef 179 #endif /* _WIN32 && !EFIX64 && !EFI32 */
Christopher Haster 1:24750b9ad5ef 180 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Christopher Haster 1:24750b9ad5ef 181
Christopher Haster 1:24750b9ad5ef 182 #if defined(MBEDTLS_TIMING_C)
Christopher Haster 1:24750b9ad5ef 183 int mbedtls_hardclock_poll( void *data,
Christopher Haster 1:24750b9ad5ef 184 unsigned char *output, size_t len, size_t *olen )
Christopher Haster 1:24750b9ad5ef 185 {
Christopher Haster 1:24750b9ad5ef 186 unsigned long timer = mbedtls_timing_hardclock();
Christopher Haster 1:24750b9ad5ef 187 ((void) data);
Christopher Haster 1:24750b9ad5ef 188 *olen = 0;
Christopher Haster 1:24750b9ad5ef 189
Christopher Haster 1:24750b9ad5ef 190 if( len < sizeof(unsigned long) )
Christopher Haster 1:24750b9ad5ef 191 return( 0 );
Christopher Haster 1:24750b9ad5ef 192
Christopher Haster 1:24750b9ad5ef 193 memcpy( output, &timer, sizeof(unsigned long) );
Christopher Haster 1:24750b9ad5ef 194 *olen = sizeof(unsigned long);
Christopher Haster 1:24750b9ad5ef 195
Christopher Haster 1:24750b9ad5ef 196 return( 0 );
Christopher Haster 1:24750b9ad5ef 197 }
Christopher Haster 1:24750b9ad5ef 198 #endif /* MBEDTLS_TIMING_C */
Christopher Haster 1:24750b9ad5ef 199
Christopher Haster 1:24750b9ad5ef 200 #if defined(MBEDTLS_HAVEGE_C)
Christopher Haster 1:24750b9ad5ef 201 int mbedtls_havege_poll( void *data,
Christopher Haster 1:24750b9ad5ef 202 unsigned char *output, size_t len, size_t *olen )
Christopher Haster 1:24750b9ad5ef 203 {
Christopher Haster 1:24750b9ad5ef 204 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Christopher Haster 1:24750b9ad5ef 205 *olen = 0;
Christopher Haster 1:24750b9ad5ef 206
Christopher Haster 1:24750b9ad5ef 207 if( mbedtls_havege_random( hs, output, len ) != 0 )
Christopher Haster 1:24750b9ad5ef 208 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Christopher Haster 1:24750b9ad5ef 209
Christopher Haster 1:24750b9ad5ef 210 *olen = len;
Christopher Haster 1:24750b9ad5ef 211
Christopher Haster 1:24750b9ad5ef 212 return( 0 );
Christopher Haster 1:24750b9ad5ef 213 }
Christopher Haster 1:24750b9ad5ef 214 #endif /* MBEDTLS_HAVEGE_C */
Christopher Haster 1:24750b9ad5ef 215
Christopher Haster 1:24750b9ad5ef 216 #endif /* MBEDTLS_ENTROPY_C */