blob: 270eb86923ab4c1ce5b42fcc0b73039174871f08 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
4 * Copyright (C) 2006-2011, Brainspark B.V.
5 *
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
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_ENTROPY_C)
29
30#include "polarssl/entropy.h"
31#include "polarssl/entropy_poll.h"
32
33#if defined(POLARSSL_TIMING_C)
34#include "polarssl/timing.h"
35#endif
36#if defined(POLARSSL_HAVEGE_C)
37#include "polarssl/havege.h"
38#endif
39
40#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
41#if defined(_WIN32)
42
43#include <windows.h>
44#if !defined(_WIN32_WINNT)
45#define _WIN32_WINNT 0x0400
46#endif
47#include <wincrypt.h>
48
49int platform_entropy_poll( void *data, unsigned char *output, size_t len,
50 size_t *olen )
51{
52 HCRYPTPROV provider;
53 *olen = 0;
54
55 if( CryptAcquireContext( &provider, NULL, NULL,
56 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
57 {
58 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
59 }
60
61 if( CryptGenRandom( provider, len, output ) == FALSE )
62 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
63
64 CryptReleaseContext( provider, 0 );
65 *olen = len;
66
67 return( 0 );
68}
69#else
70
71#include <stdio.h>
72
73int platform_entropy_poll( void *data,
74 unsigned char *output, size_t len, size_t *olen )
75{
76 FILE *file;
77 size_t ret;
78 ((void) data);
79
80 *olen = 0;
81
82 file = fopen( "/dev/urandom", "rb" );
83 if( file == NULL )
84 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
85
86 ret = fread( output, 1, len, file );
87 if( ret != len )
88 {
89 fclose( file );
90 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
91 }
92
93 fclose( file );
94 *olen = len;
95
96 return( 0 );
97}
98#endif
99#endif
100
101#if defined(POLARSSL_TIMING_C)
102int hardclock_poll( void *data,
103 unsigned char *output, size_t len, size_t *olen )
104{
105 unsigned long timer = hardclock();
106 ((void) data);
107 *olen = 0;
108
109 if( len < sizeof(unsigned long) )
110 return( 0 );
111
112 memcpy( output, &timer, sizeof(unsigned long) );
113 *olen = sizeof(unsigned long);
114
115 return( 0 );
116}
117#endif
118
119#if defined(POLARSSL_HAVEGE_C)
120int havege_poll( void *data,
121 unsigned char *output, size_t len, size_t *olen )
122{
123 havege_state *hs = (havege_state *) data;
124 *olen = 0;
125
126 if( havege_random( hs, output, len ) != 0 )
127 return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
128
129 *olen = len;
130
131 return( 0 );
132}
133#endif
134
135#endif /* POLARSSL_ENTROPY_C */