From 774588562ed79291c84f8c0796c9288c6cf28ac6 Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Sat, 15 Oct 2022 22:09:06 +0800 Subject: [PATCH] Add Linux RNG --- src/rand_unix.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/rand_unix.c diff --git a/src/rand_unix.c b/src/rand_unix.c new file mode 100644 index 00000000..a660dcdd --- /dev/null +++ b/src/rand_unix.c @@ -0,0 +1,40 @@ +/* + * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ + + + +#include +#include +#include +#include +#include // in Linux +//#include // in Apple +#include +#include + + +#define RAND_MAX_BUF_SIZE 256 // requirement of getentropy() + +int rand_bytes(uint8_t *buf, size_t len) +{ + if (!buf) { + error_print(); + return -1; + } + if (!len || len > RAND_MAX_BUF_SIZE) { + error_print(); + return -1; + } + if (getentropy(buf, len) != 0) { + error_print(); + return -1; + } + return 1; +} +