PolarSSL v1.2.10
entropy_poll.c
Go to the documentation of this file.
1 /*
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 #if !defined(_WIN32_WINNT)
44 #define _WIN32_WINNT 0x0400
45 #endif
46 #include <windows.h>
47 #include <wincrypt.h>
48 
49 int platform_entropy_poll( void *data, unsigned char *output, size_t len,
50  size_t *olen )
51 {
52  HCRYPTPROV provider;
53  ((void) data);
54  *olen = 0;
55 
56  if( CryptAcquireContext( &provider, NULL, NULL,
57  PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
58  {
60  }
61 
62  if( CryptGenRandom( provider, len, output ) == FALSE )
64 
65  CryptReleaseContext( provider, 0 );
66  *olen = len;
67 
68  return( 0 );
69 }
70 #else
71 
72 #include <stdio.h>
73 
74 int platform_entropy_poll( void *data,
75  unsigned char *output, size_t len, size_t *olen )
76 {
77  FILE *file;
78  size_t ret;
79  ((void) data);
80 
81  *olen = 0;
82 
83  file = fopen( "/dev/urandom", "rb" );
84  if( file == NULL )
86 
87  ret = fread( output, 1, len, file );
88  if( ret != len )
89  {
90  fclose( file );
92  }
93 
94  fclose( file );
95  *olen = len;
96 
97  return( 0 );
98 }
99 #endif
100 #endif
101 
102 #if defined(POLARSSL_TIMING_C)
103 int hardclock_poll( void *data,
104  unsigned char *output, size_t len, size_t *olen )
105 {
106  unsigned long timer = hardclock();
107  ((void) data);
108  *olen = 0;
109 
110  if( len < sizeof(unsigned long) )
111  return( 0 );
112 
113  memcpy( output, &timer, sizeof(unsigned long) );
114  *olen = sizeof(unsigned long);
115 
116  return( 0 );
117 }
118 #endif
119 
120 #if defined(POLARSSL_HAVEGE_C)
121 int havege_poll( void *data,
122  unsigned char *output, size_t len, size_t *olen )
123 {
124  havege_state *hs = (havege_state *) data;
125  *olen = 0;
126 
127  if( havege_random( hs, output, len ) != 0 )
129 
130  *olen = len;
131 
132  return( 0 );
133 }
134 #endif
135 
136 #endif /* POLARSSL_ENTROPY_C */
Configuration options (set of defines)
unsigned long hardclock(void)
Return the CPU cycle counter value.
Platform-specific and custom entropy polling functions.
Entropy accumulator implementation.
HAVEGE state structure.
Definition: havege.h:37
HAVEGE: HArdware Volatile Entropy Gathering and Expansion.
int platform_entropy_poll(void *data, unsigned char *output, size_t len, size_t *olen)
Platform-specific entropy poll callback.
int havege_random(void *p_rng, unsigned char *output, size_t len)
HAVEGE rand function.
#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED
Critical entropy source failure.
Definition: entropy.h:39
int hardclock_poll(void *data, unsigned char *output, size_t len, size_t *olen)
hardclock-based entropy poll callback
Portable interface to the CPU cycle counter.