NeoMutt  2022-04-29-178-g3b62e6
Teaching an old dog new tricks
DOXYGEN
random.c File Reference

Random number/string functions. More...

#include "config.h"
#include <stddef.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "random.h"
#include "exit.h"
#include "logging.h"
#include "message.h"
+ Include dependency graph for random.c:

Go to the source code of this file.

Functions

static int mutt_randbuf (void *buf, size_t buflen)
 Fill a buffer with randomness. More...
 
void mutt_rand_base32 (char *buf, size_t buflen)
 Fill a buffer with a base32-encoded random string. More...
 
uint32_t mutt_rand32 (void)
 Create a 32-bit random number. More...
 
uint64_t mutt_rand64 (void)
 Create a 64-bit random number. More...
 

Variables

static FILE * fp_random = NULL
 
static const unsigned char base32 [] = "abcdefghijklmnopqrstuvwxyz234567"
 

Detailed Description

Random number/string functions.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file random.c.

Function Documentation

◆ mutt_randbuf()

static int mutt_randbuf ( void *  buf,
size_t  buflen 
)
static

Fill a buffer with randomness.

Parameters
bufBuffer for result
buflenSize of buffer
Return values
0Success
-1Error

Definition at line 54 of file random.c.

55{
56 if (buflen > 1048576)
57 {
58 mutt_error(_("mutt_randbuf buflen=%zu"), buflen);
59 return -1;
60 }
61
62#ifdef HAVE_GETRANDOM
63 ssize_t rc;
64 ssize_t count = 0;
65 do
66 {
67 // getrandom() can return less than requested if there's insufficient
68 // entropy or it's interrupted by a signal.
69 rc = getrandom((char *) buf + count, buflen - count, 0);
70 if (rc > 0)
71 count += rc;
72 } while (((rc >= 0) && (count < buflen)) || ((rc == -1) && (errno == EINTR)));
73 if (count == buflen)
74 return 0;
75#endif
76 /* let's try urandom in case we're on an old kernel, or the user has
77 * configured selinux, seccomp or something to not allow getrandom */
78 if (!fp_random)
79 {
80 fp_random = fopen("/dev/urandom", "rb");
81 if (!fp_random)
82 {
83 mutt_error(_("open /dev/urandom: %s"), strerror(errno));
84 return -1;
85 }
86 setbuf(fp_random, NULL);
87 }
88 if (fread(buf, 1, buflen, fp_random) != buflen)
89 {
90 mutt_error(_("read /dev/urandom: %s"), strerror(errno));
91 return -1;
92 }
93
94 return 0;
95}
#define mutt_error(...)
Definition: logging.h:87
#define _(a)
Definition: message.h:28
static FILE * fp_random
Definition: random.c:43
+ Here is the caller graph for this function:

◆ mutt_rand_base32()

void mutt_rand_base32 ( char *  buf,
size_t  buflen 
)

Fill a buffer with a base32-encoded random string.

Parameters
bufBuffer for result
buflenLength of buffer

Definition at line 102 of file random.c.

103{
104 uint8_t *p = (uint8_t *) buf;
105
106 if (mutt_randbuf(p, buflen) < 0)
107 mutt_exit(1);
108 for (size_t pos = 0; pos < buflen; pos++)
109 p[pos] = base32[p[pos] % 32];
110}
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:248
static int mutt_randbuf(void *buf, size_t buflen)
Fill a buffer with randomness.
Definition: random.c:54
static const unsigned char base32[]
Definition: random.c:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_rand32()

uint32_t mutt_rand32 ( void  )

Create a 32-bit random number.

Return values
numRandom number

Definition at line 116 of file random.c.

117{
118 uint32_t num = 0;
119
120 if (mutt_randbuf(&num, sizeof(num)) < 0)
121 mutt_exit(1);
122 return num;
123}
+ Here is the call graph for this function:

◆ mutt_rand64()

uint64_t mutt_rand64 ( void  )

Create a 64-bit random number.

Return values
numRandom number

Definition at line 129 of file random.c.

130{
131 uint64_t num = 0;
132
133 if (mutt_randbuf(&num, sizeof(num)) < 0)
134 mutt_exit(1);
135 return num;
136}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ fp_random

FILE* fp_random = NULL
static

Definition at line 43 of file random.c.

◆ base32

const unsigned char base32[] = "abcdefghijklmnopqrstuvwxyz234567"
static

Definition at line 45 of file random.c.