blob: 42316f3e260dce86760a9840f8699f00d9ce8aa2 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Paul Bakker9af723c2014-05-01 13:03:14 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6083fd22011-12-03 21:45:14 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000031
32#if defined(POLARSSL_ENTROPY_C)
33
34#include "polarssl/entropy.h"
35#include "polarssl/entropy_poll.h"
36
37#if defined(POLARSSL_TIMING_C)
38#include "polarssl/timing.h"
39#endif
40#if defined(POLARSSL_HAVEGE_C)
41#include "polarssl/havege.h"
42#endif
43
44#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010045#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000046
Paul Bakker6083fd22011-12-03 21:45:14 +000047#if !defined(_WIN32_WINNT)
48#define _WIN32_WINNT 0x0400
49#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000050#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000051#include <wincrypt.h>
52
53int platform_entropy_poll( void *data, unsigned char *output, size_t len,
54 size_t *olen )
55{
56 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000057 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000058 *olen = 0;
59
60 if( CryptAcquireContext( &provider, NULL, NULL,
61 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
62 {
63 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
64 }
65
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020066 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Paul Bakker6083fd22011-12-03 21:45:14 +000067 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
68
69 CryptReleaseContext( provider, 0 );
70 *olen = len;
71
72 return( 0 );
73}
Paul Bakker9af723c2014-05-01 13:03:14 +020074#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000075
76#include <stdio.h>
77
78int platform_entropy_poll( void *data,
79 unsigned char *output, size_t len, size_t *olen )
80{
81 FILE *file;
82 size_t ret;
83 ((void) data);
84
85 *olen = 0;
86
87 file = fopen( "/dev/urandom", "rb" );
88 if( file == NULL )
89 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker9af723c2014-05-01 13:03:14 +020090
Paul Bakker6083fd22011-12-03 21:45:14 +000091 ret = fread( output, 1, len, file );
92 if( ret != len )
93 {
94 fclose( file );
95 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
96 }
97
98 fclose( file );
99 *olen = len;
100
101 return( 0 );
102}
Paul Bakker9af723c2014-05-01 13:03:14 +0200103#endif /* _WIN32 && !EFIX64 && !EFI32 */
104#endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000105
106#if defined(POLARSSL_TIMING_C)
107int hardclock_poll( void *data,
108 unsigned char *output, size_t len, size_t *olen )
109{
110 unsigned long timer = hardclock();
111 ((void) data);
112 *olen = 0;
113
114 if( len < sizeof(unsigned long) )
115 return( 0 );
116
117 memcpy( output, &timer, sizeof(unsigned long) );
118 *olen = sizeof(unsigned long);
119
120 return( 0 );
121}
Paul Bakker9af723c2014-05-01 13:03:14 +0200122#endif /* POLARSSL_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000123
124#if defined(POLARSSL_HAVEGE_C)
125int havege_poll( void *data,
126 unsigned char *output, size_t len, size_t *olen )
127{
128 havege_state *hs = (havege_state *) data;
129 *olen = 0;
130
131 if( havege_random( hs, output, len ) != 0 )
132 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
133
134 *olen = len;
135
136 return( 0 );
137}
Paul Bakker9af723c2014-05-01 13:03:14 +0200138#endif /* POLARSSL_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000139
140#endif /* POLARSSL_ENTROPY_C */