NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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 "logging2.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.
 
void mutt_rand_base32 (char *buf, size_t buflen)
 Fill a buffer with a base32-encoded random string.
 
uint32_t mutt_rand32 (void)
 Create a 32-bit random number.
 
uint64_t mutt_rand64 (void)
 Create a 64-bit random number.
 

Variables

static FILE * FpRandom = NULL
 FILE pointer of the random source.
 
static const unsigned char Base32 [] = "abcdefghijklmnopqrstuvwxyz234567"
 Base 32 alphabet.
 

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 56 of file random.c.

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

105{
106 if (!buf || (buflen == 0))
107 return;
108
109 uint8_t *p = (uint8_t *) buf;
110
111 if (mutt_randbuf(p, buflen) < 0)
112 mutt_exit(1); // LCOV_EXCL_LINE
113 for (size_t pos = 0; pos < buflen; pos++)
114 p[pos] = Base32[p[pos] % 32];
115}
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:236
static const unsigned char Base32[]
Base 32 alphabet.
Definition: random.c:47
static int mutt_randbuf(void *buf, size_t buflen)
Fill a buffer with randomness.
Definition: random.c:56
+ 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 121 of file random.c.

122{
123 uint32_t num = 0;
124
125 if (mutt_randbuf(&num, sizeof(num)) < 0)
126 mutt_exit(1); // LCOV_EXCL_LINE
127 return num;
128}
+ 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 134 of file random.c.

135{
136 uint64_t num = 0;
137
138 if (mutt_randbuf(&num, sizeof(num)) < 0)
139 mutt_exit(1); // LCOV_EXCL_LINE
140 return num;
141}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ FpRandom

FILE* FpRandom = NULL
static

FILE pointer of the random source.

Definition at line 44 of file random.c.

◆ Base32

const unsigned char Base32[] = "abcdefghijklmnopqrstuvwxyz234567"
static

Base 32 alphabet.

Definition at line 47 of file random.c.