NeoMutt  2024-04-16-36-g75b6fb
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 "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 57 of file random.c.

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

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

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

◆ Base32

const unsigned char Base32[] = "abcdefghijklmnopqrstuvwxyz234567"
static

Base 32 alphabet.

Definition at line 48 of file random.c.