ASCON Suite
ascon-trng-dev-random.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 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 #define _GNU_SOURCE
24 #include "ascon-trng.h"
26 
27 #if defined(ASCON_TRNG_DEV_RANDOM)
28 
29 #if defined(HAVE_CONFIG_H)
30 #include <config.h>
31 #endif
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <string.h>
39 #if defined(__linux__) || defined(HAVE_SYS_SYSCALL_H)
40 #include <sys/syscall.h>
41 #endif
42 #if defined(HAVE_SYS_RANDOM_H)
43 #include <sys/random.h>
44 #endif
45 
46 /* Use /dev/urandom if we don't have getrandom() or getentropy() */
47 #define RANDOM_DEVICE "/dev/urandom"
48 
49 /* Determine if we have some kind of getrandom() or getentropy() function */
50 #if defined(HAVE_GETRANDOM)
51 #define ascon_getrandom(buf, size) getrandom((buf), (size), 0)
52 #elif defined(HAVE_GETENTROPY)
53 #define ascon_getrandom(buf, size) getentropy((buf), (size))
54 #elif defined(SYS_getrandom)
55 #define ascon_getrandom(buf, size) syscall(SYS_getrandom, (buf), (size), 0)
56 #endif
57 
58 #if !defined(ascon_getrandom)
59 static int ascon_dev_random_open(void)
60 {
61  return open(RANDOM_DEVICE, O_RDONLY);
62 }
63 #else
64 #define ascon_dev_random_open() -1
65 #endif
66 
67 static int ascon_dev_random_read(int fd, unsigned char *out, size_t outlen)
68 {
69 #if defined(ascon_getrandom)
70  /* Keep looping until we get some data or a permanent error. */
71  (void)fd;
72  for (;;) {
73  int ret = ascon_getrandom(out, outlen);
74  if (ret >= 0) {
75  /* getentropy() returns 0 on success, getrandom() returns
76  * the number of bytes read on success */
77  return 1;
78  } else {
79  if (errno != EINTR && errno != EAGAIN) {
80  /* getrandom() or getentropy() is broken; this is a problem */
81  memset(out, 0, outlen);
82  break;
83  }
84  }
85  }
86  return 0;
87 #else
88  if (fd >= 0) {
89  /* Keep looping until we get some data or a permanent error. */
90  for (;;) {
91  int ret = read(fd, out, outlen);
92  if (ret == (int)outlen) {
93  return 1;
94  } else if (ret < 0) {
95  if (errno != EINTR && errno != EAGAIN)
96  break;
97  }
98  }
99  }
100  /* /dev/urandom is broken or not open; this is a problem */
101  memset(out, 0, outlen);
102  return 0;
103 #endif
104 }
105 
106 int ascon_trng_generate(unsigned char *out, size_t outlen)
107 {
108 #if defined(ascon_getrandom)
109  return ascon_dev_random_read(-1, out, outlen);
110 #else
111  int fd = ascon_dev_random_open();
112  int ok = ascon_dev_random_read(fd, out, outlen);
113  if (fd >= 0)
114  close(fd);
115  return ok;
116 #endif
117 }
118 
119 #endif /* ASCON_TRNG_DEV_RANDOM */
int ascon_trng_generate(unsigned char *out, size_t outlen)
Generates a buffer of bytes from the system TRNG source.
Access to the system's random number source.