NeoMutt  2021-02-05-666-ge300cd
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 <sys/types.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 55 of file random.c.

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

104 {
105  uint8_t *p = (uint8_t *) buf;
106 
107  if (mutt_randbuf(p, buflen) < 0)
108  mutt_exit(1);
109  for (size_t pos = 0; pos < buflen; pos++)
110  p[pos] = base32[p[pos] % 32];
111 }
static const unsigned char base32[]
Definition: random.c:46
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:279
static int mutt_randbuf(void *buf, size_t buflen)
Fill a buffer with randomness.
Definition: random.c:55
+ 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 117 of file random.c.

118 {
119  uint32_t num = 0;
120 
121  if (mutt_randbuf(&num, sizeof(num)) < 0)
122  mutt_exit(1);
123  return num;
124 }
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:279
static int mutt_randbuf(void *buf, size_t buflen)
Fill a buffer with randomness.
Definition: random.c:55
+ 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 130 of file random.c.

131 {
132  uint64_t num = 0;
133 
134  if (mutt_randbuf(&num, sizeof(num)) < 0)
135  mutt_exit(1);
136  return num;
137 }
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:279
static int mutt_randbuf(void *buf, size_t buflen)
Fill a buffer with randomness.
Definition: random.c:55
+ 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 44 of file random.c.

◆ base32

const unsigned char base32[] = "abcdefghijklmnopqrstuvwxyz234567"
static

Definition at line 46 of file random.c.