blob: e73c719781e6ab28ad13df4e1f6f12e5f201ee0a [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker6083fd22011-12-03 21:45:14 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000027
Gilles Peskine8ba12292018-06-01 14:38:45 +020028#include <string.h>
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/entropy.h"
33#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010043#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000044
Paul Bakker6083fd22011-12-03 21:45:14 +000045#if !defined(_WIN32_WINNT)
46#define _WIN32_WINNT 0x0400
47#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000048#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000049#include <wincrypt.h>
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000052 size_t *olen )
53{
54 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000055 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000056 *olen = 0;
57
58 if( CryptAcquireContext( &provider, NULL, NULL,
59 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
60 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000062 }
63
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020064 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000066
67 CryptReleaseContext( provider, 0 );
68 *olen = len;
69
70 return( 0 );
71}
Paul Bakker9af723c2014-05-01 13:03:14 +020072#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000073
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010074/*
75 * Test for Linux getrandom() support.
76 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
77 * available in GNU libc and compatible libc's (eg uClibc).
78 */
79#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010080#include <unistd.h>
81#include <sys/syscall.h>
82#if defined(SYS_getrandom)
83#define HAVE_GETRANDOM
Hanno Becker9a7fcd62018-10-18 11:49:25 +010084#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020085
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010086static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
87{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020088 /* MemSan cannot understand that the syscall writes to the buffer */
89#if defined(__has_feature)
90#if __has_feature(memory_sanitizer)
91 memset( buf, 0, buflen );
92#endif
93#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010094 return( syscall( SYS_getrandom, buf, buflen, flags ) );
95}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020096#endif /* SYS_getrandom */
97#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010098
Paul Bakker6083fd22011-12-03 21:45:14 +000099#include <stdio.h>
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000102 unsigned char *output, size_t len, size_t *olen )
103{
104 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200105 size_t read_len;
Hanno Becker9a7fcd62018-10-18 11:49:25 +0100106 int ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000107 ((void) data);
108
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200109#if defined(HAVE_GETRANDOM)
Hanno Becker9a7fcd62018-10-18 11:49:25 +0100110 ret = getrandom_wrapper( output, len, 0 );
111 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200112 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200113 *olen = ret;
114 return( 0 );
115 }
Hanno Becker9a7fcd62018-10-18 11:49:25 +0100116 else if( errno != ENOSYS )
117 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
118 /* Fall through if the system call isn't known. */
119#else
Hanno Becker766ca322018-11-05 11:43:07 +0000120 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200121#endif /* HAVE_GETRANDOM */
122
Paul Bakker6083fd22011-12-03 21:45:14 +0000123 *olen = 0;
124
125 file = fopen( "/dev/urandom", "rb" );
126 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200128
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200129 read_len = fread( output, 1, len, file );
130 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000131 {
132 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000134 }
135
136 fclose( file );
137 *olen = len;
138
139 return( 0 );
140}
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_TIMING_C)
145int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000146 unsigned char *output, size_t len, size_t *olen )
147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000149 ((void) data);
150 *olen = 0;
151
152 if( len < sizeof(unsigned long) )
153 return( 0 );
154
155 memcpy( output, &timer, sizeof(unsigned long) );
156 *olen = sizeof(unsigned long);
157
158 return( 0 );
159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_HAVEGE_C)
163int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000164 unsigned char *output, size_t len, size_t *olen )
165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000167 *olen = 0;
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 if( mbedtls_havege_random( hs, output, len ) != 0 )
170 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000171
172 *olen = len;
173
174 return( 0 );
175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_ENTROPY_C */