Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Platform-specific and custom entropy polling functions |
| 3 | * |
Manuel Pégourié-Gonnard | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame^] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #include "polarssl/config.h" |
| 24 | |
| 25 | #if defined(POLARSSL_ENTROPY_C) |
| 26 | |
| 27 | #include "polarssl/entropy.h" |
| 28 | #include "polarssl/entropy_poll.h" |
| 29 | |
| 30 | #if defined(POLARSSL_TIMING_C) |
| 31 | #include "polarssl/timing.h" |
| 32 | #endif |
| 33 | #if defined(POLARSSL_HAVEGE_C) |
| 34 | #include "polarssl/havege.h" |
| 35 | #endif |
| 36 | |
| 37 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
| 38 | #if defined(_WIN32) |
| 39 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 40 | #if !defined(_WIN32_WINNT) |
| 41 | #define _WIN32_WINNT 0x0400 |
| 42 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 43 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 44 | #include <wincrypt.h> |
| 45 | |
| 46 | int platform_entropy_poll( void *data, unsigned char *output, size_t len, |
| 47 | size_t *olen ) |
| 48 | { |
| 49 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 50 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 51 | *olen = 0; |
| 52 | |
| 53 | if( CryptAcquireContext( &provider, NULL, NULL, |
| 54 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) |
| 55 | { |
| 56 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 57 | } |
| 58 | |
| 59 | if( CryptGenRandom( provider, len, output ) == FALSE ) |
| 60 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 61 | |
| 62 | CryptReleaseContext( provider, 0 ); |
| 63 | *olen = len; |
| 64 | |
| 65 | return( 0 ); |
| 66 | } |
| 67 | #else |
| 68 | |
| 69 | #include <stdio.h> |
| 70 | |
| 71 | int platform_entropy_poll( void *data, |
| 72 | unsigned char *output, size_t len, size_t *olen ) |
| 73 | { |
| 74 | FILE *file; |
| 75 | size_t ret; |
| 76 | ((void) data); |
| 77 | |
| 78 | *olen = 0; |
| 79 | |
| 80 | file = fopen( "/dev/urandom", "rb" ); |
| 81 | if( file == NULL ) |
| 82 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 83 | |
| 84 | ret = fread( output, 1, len, file ); |
| 85 | if( ret != len ) |
| 86 | { |
| 87 | fclose( file ); |
| 88 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 89 | } |
| 90 | |
| 91 | fclose( file ); |
| 92 | *olen = len; |
| 93 | |
| 94 | return( 0 ); |
| 95 | } |
| 96 | #endif |
| 97 | #endif |
| 98 | |
| 99 | #if defined(POLARSSL_TIMING_C) |
| 100 | int hardclock_poll( void *data, |
| 101 | unsigned char *output, size_t len, size_t *olen ) |
| 102 | { |
| 103 | unsigned long timer = hardclock(); |
| 104 | ((void) data); |
| 105 | *olen = 0; |
| 106 | |
| 107 | if( len < sizeof(unsigned long) ) |
| 108 | return( 0 ); |
| 109 | |
| 110 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 111 | *olen = sizeof(unsigned long); |
| 112 | |
| 113 | return( 0 ); |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | #if defined(POLARSSL_HAVEGE_C) |
| 118 | int havege_poll( void *data, |
| 119 | unsigned char *output, size_t len, size_t *olen ) |
| 120 | { |
| 121 | havege_state *hs = (havege_state *) data; |
| 122 | *olen = 0; |
| 123 | |
| 124 | if( havege_random( hs, output, len ) != 0 ) |
| 125 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 126 | |
| 127 | *olen = len; |
| 128 | |
| 129 | return( 0 ); |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | #endif /* POLARSSL_ENTROPY_C */ |