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.c54
1 files changed, 30 insertions, 24 deletions
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index 2cb0f56d..2aa0de3a 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,30 +115,6 @@ 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, "rmalloc: zero-byte request");
-
- if (n >= SIZE_MAX - 1)
- err_exit(EOVERFLOW, "rmalloc: overflow");
-
- if (if_err((s = rmalloc(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 *
rmalloc(size_t n)
{