PolarSSL v1.3.8
entropy_poll.c
Go to the documentation of this file.
1 /*
2  * Platform-specific and custom entropy polling functions
3  *
4  * Copyright (C) 2006-2014, 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 #if !defined(POLARSSL_CONFIG_FILE)
27 #include "polarssl/config.h"
28 #else
29 #include POLARSSL_CONFIG_FILE
30 #endif
31 
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)
45 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
46 
47 #if !defined(_WIN32_WINNT)
48 #define _WIN32_WINNT 0x0400
49 #endif
50 #include <windows.h>
51 #include <wincrypt.h>
52 
53 int platform_entropy_poll( void *data, unsigned char *output, size_t len,
54  size_t *olen )
55 {
56  HCRYPTPROV provider;
57  ((void) data);
58  *olen = 0;
59 
60  if( CryptAcquireContext( &provider, NULL, NULL,
61  PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
62  {
64  }
65 
66  if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
68 
69  CryptReleaseContext( provider, 0 );
70  *olen = len;
71 
72  return( 0 );
73 }
74 #else /* _WIN32 && !EFIX64 && !EFI32 */
75 
76 #include <stdio.h>
77 
78 int 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 )
90 
91  ret = fread( output, 1, len, file );
92  if( ret != len )
93  {
94  fclose( file );
96  }
97 
98  fclose( file );
99  *olen = len;
100 
101  return( 0 );
102 }
103 #endif /* _WIN32 && !EFIX64 && !EFI32 */
104 #endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
105 
106 #if defined(POLARSSL_TIMING_C)
107 int 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 }
122 #endif /* POLARSSL_TIMING_C */
123 
124 #if defined(POLARSSL_HAVEGE_C)
125 int 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 )
133 
134  *olen = len;
135 
136  return( 0 );
137 }
138 #endif /* POLARSSL_HAVEGE_C */
139 
140 #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:41
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:56
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.