summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/rand.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/libreboot-utils/lib/rand.c')
-rw-r--r--util/libreboot-utils/lib/rand.c59
1 files changed, 33 insertions, 26 deletions
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index 9edc8a5b..9da8d9eb 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -72,6 +72,36 @@
* or your program dies.
*/
+/* random string generator, with
+ * rejection sampling. NOTE: only
+ * uses ASCII-safe characters, for
+ * printing on a unix terminal
+ *
+ * you still shouldn't use this for
+ * password generation; open diceware
+ * passphrases are better for that
+ *
+ * NOTE: the generated strings must
+ * ALSO be safe for file/directory names
+ * on unix-like os e.g. linux/bsd
+ */
+char *
+rchars(size_t n) /* emulates spkmodem-decode */
+{
+ static char ch[] =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+ char *s = NULL;
+ size_t i;
+
+ smalloc(&s, n + 1);
+ for (i = 0; i < n; i++)
+ s[i] = ch[rsize(sizeof(ch) - 1)];
+
+ *(s + n) = '\0';
+ return s;
+}
+
size_t
rsize(size_t n)
{
@@ -85,32 +115,8 @@ rsize(size_t n)
return rval % n;
}
-char *
-mkrstr(size_t n) /* emulates spkmodem-decode */
-{
- char *s;
- size_t i;
-
- if (n == 0)
- err_exit(EPERM, "mkrbuf: zero-byte request");
-
- if (n >= SIZE_MAX - 1)
- err_exit(EOVERFLOW, "mkrbuf: overflow");
-
- if (if_err((s = mkrbuf(n + 1)) == NULL, EFAULT))
- err_exit(EFAULT, "mkrstr: null");
-
- for (i = 0; i < n; i++)
- while(*(s + i) == '\0')
- rset(s + i, 1);
-
- *(s + n) = '\0';
-
- return s;
-}
-
void *
-mkrbuf(size_t n)
+rmalloc(size_t n)
{
void *buf = NULL;
rset(vmalloc(&buf, n), n);
@@ -144,7 +150,8 @@ rset(void *buf, size_t n)
int fd = -1;
open_on_eintr("/dev/urandom", &fd, O_RDONLY, 0400, NULL);
retry_rand:
- if ((rc = read(fd, (unsigned char *)buf + off, n - off)) < 0) {
+ if ((rc = read_on_eintr(fd,
+ (unsigned char *)buf + off, n - off)) < 0) {
#elif defined(__linux__)
retry_rand:
if ((rc = (ssize_t)syscall(SYS_getrandom,