mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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