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