aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDr. David Alan Gilbert <dave@treblig.org>2017-02-12 15:59:16 +0000
committerDr. David Alan Gilbert <dave@treblig.org>2018-02-25 02:08:49 +0000
commit01705b7b797a969be0708c66654e32842c034df3 (patch)
tree16b6c6ca7d87d738f6e985db9b058c70eca9711e
parent86f665ed9dfc3503ff3d6c855ffade60737354d1 (diff)
downloadpurple-matrix-01705b7b797a969be0708c66654e32842c034df3.tar.gz
e2e: get_random
Allocate a blob of memory and fill it with random data. (There must be some existing portable function for this?!) Signed-off-by: Dr. David Alan Gilbert <dave@treblig.org>
-rw-r--r--matrix-e2e.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/matrix-e2e.c b/matrix-e2e.c
index dbd7dd1..2e3acd9 100644
--- a/matrix-e2e.c
+++ b/matrix-e2e.c
@@ -22,6 +22,7 @@
#include "matrix-api.h"
#include "matrix-e2e.h"
#include "matrix-json.h"
+#include "debug.h"
/* json-glib */
#include <json-glib/json-glib.h>
@@ -50,6 +51,25 @@ static void clear_mem(volatile char *data, size_t len)
#endif
}
+/* Returns a pointer to a freshly allocated buffer of 'n' bytes of random data.
+ * If it fails it returns NULL.
+ * TODO: There must be some portable function we can call to do this.
+ */
+static void *get_random(size_t n)
+{
+ FILE *urandom = fopen("/dev/urandom", "rb");
+ if (!urandom) {
+ return NULL;
+ }
+ void *buffer = g_malloc(n);
+ if (fread(buffer, 1, n, urandom) != n) {
+ g_free(buffer);
+ buffer = NULL;
+ }
+ fclose(urandom);
+ return buffer;
+}
+
/* Sign the JsonObject with olm_account_sign and add it to the object
* as a 'signatures' member of the top level object.
* 0 on success