blob: 17be637ad2cae4f54565eb9256373ca1a53a825a [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
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 Bakker6083fd22011-12-03 21:45:14 +000040#if !defined(_WIN32_WINNT)
41#define _WIN32_WINNT 0x0400
42#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000043#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000044#include <wincrypt.h>
45
46int platform_entropy_poll( void *data, unsigned char *output, size_t len,
47 size_t *olen )
48{
49 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000050 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000051 *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
71int 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)
100int 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)
118int 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 */