NeoMutt  2024-10-02-58-g7720cc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
random.c File Reference

Random number/string functions. More...

#include "config.h"
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "random.h"
#include "exit.h"
#include "file.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.
 
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 58 of file random.c.

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

107{
108 if (!buf || (buflen == 0))
109 return;
110
111 uint8_t *p = (uint8_t *) buf;
112
113 if (mutt_randbuf(p, buflen) < 0)
114 mutt_exit(1); // LCOV_EXCL_LINE
115 for (size_t pos = 0; pos < buflen; pos++)
116 p[pos] = Base32[p[pos] % 32];
117}
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition: main.c:269
static const unsigned char Base32[]
Base 32 alphabet.
Definition: random.c:49
static int mutt_randbuf(void *buf, size_t buflen)
Fill a buffer with randomness.
Definition: random.c:58
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_rand64()

uint64_t mutt_rand64 ( void  )

Create a 64-bit random number.

Return values
numRandom number

Definition at line 123 of file random.c.

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

◆ Base32

const unsigned char Base32[] = "abcdefghijklmnopqrstuvwxyz234567"
static

Base 32 alphabet.

Definition at line 49 of file random.c.