Noise-C
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
rand_os.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 /* This module gets random entropy from the operating system. The code
24  here is very platform and OS dependent and will probably need work to
25  port it to new platforms. */
26 
27 #include "internal.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #if defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN32__)
32 #include <windows.h>
33 #include <wincrypt.h>
34 #else
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #endif
39 
51 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__APPLE__)
52 #define RANDOM_DEVICE "/dev/urandom"
53 #endif
54 #if defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN32__)
55 #define RANDOM_WIN32 1
56 #endif
57 
68 void noise_rand_bytes(void *bytes, size_t size)
69 {
70 #if defined(RANDOM_DEVICE)
71  int fd = open(RANDOM_DEVICE, O_RDONLY);
72  if (fd >= 0) {
73  for (;;) {
74  int len = read(fd, bytes, size);
75  if (len == (int)size) {
76  /* We have the bytes we wanted */
77  close(fd);
78  return;
79  } else if (len >= 0) {
80  /* Short read - this shouldn't happen. Treat it as "no data" */
81  break;
82  } else if (errno != EINTR) {
83  /* Some other error than "interrupted due to signal" */
84  perror(RANDOM_DEVICE);
85  break;
86  }
87  }
88  close(fd);
89  } else {
90  perror(RANDOM_DEVICE);
91  }
92 #elif defined(RANDOM_WIN32)
93  /* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx */
94  HCRYPTPROV provider = 0;
95  memset(bytes, 0, size);
96  if (CryptAcquireContextW(&provider, 0, 0, PROV_RSA_FULL,
97  CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
98  CryptGenRandom(provider, size, bytes);
99  CryptReleaseContext(provider, 0);
100  return;
101  }
102 #endif
103  fprintf(stderr, "Do not know how to generate random numbers! Abort!\n");
104  exit(1);
105 }
106 
107 #ifdef ED25519_CUSTOMRANDOM
108 
109 /* We are building against ed25519-donna, which needs a random function */
110 
111 void ed25519_randombytes_unsafe(void *p, size_t len)
112 {
113  noise_rand_bytes(p, len);
114 }
115 
116 #endif
void noise_rand_bytes(void *bytes, size_t size)
Gets cryptographically-strong random bytes from the operating system.
Definition: rand_os.c:68
Internal definitions for the library.