[LEDE-DEV] [PATCH ubox] getrandom: add helper for getrandom() syscall
Etienne Champetier
champetier.etienne at gmail.com
Thu Jun 9 08:02:16 PDT 2016
Hi,
Everything is fine except your computer clock ;)
ubox commit:
author Etienne CHAMPETIER <champetier.etienne at gmail.com>
Thu, 9 Jun 2016 16:03:00 +0300 (13:03 +0000)
committer John Crispin <john at phrozen.org>
Wed, 8 Jun 2016 01:19:24 +0300 (00:19 +0200)
staging commit:
author John Crispin <john at phrozen.org>
Wed, 8 Jun 2016 01:22:16 +0300 (00:22 +0200)
committer John Crispin <john at phrozen.org>
Wed, 8 Jun 2016 01:56:48 +0300
Thanks
Etienne
2016-06-09 17:17 GMT+03:00 John Crispin <john at phrozen.org>:
> Hi Etienne,
>
> pushed the patch to ubox.git and updated the ubox package inside my
> staging tree. should the tool be installed to /usr/bin ? if not let me
> know and i will fix the staged commit.
>
> John
>
> On 09/06/2016 15:03, Etienne CHAMPETIER wrote:
>> getrandom() was introduced in version 3.17 of the Linux kernel.
>> By default getrandom() block until /dev/urandom pool has been initialized
>> and then read from it.
>> Read buffer is 256 bytes so getrandom() calls always succeed.
>>
>> First usage will be to save a seed for /dev/urandom.
>>
>> Signed-off-by: Etienne CHAMPETIER <champetier.etienne at gmail.com>
>> ---
>> CMakeLists.txt | 5 +++++
>> getrandom.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 63 insertions(+)
>> create mode 100644 getrandom.c
>>
>> diff --git a/CMakeLists.txt b/CMakeLists.txt
>> index 834b5b6..73e8753 100644
>> --- a/CMakeLists.txt
>> +++ b/CMakeLists.txt
>> @@ -16,6 +16,11 @@ IF(DEBUG)
>> ADD_DEFINITIONS(-DDEBUG -g3)
>> ENDIF()
>>
>> +ADD_EXECUTABLE(getrandom getrandom.c)
>> +INSTALL(TARGETS getrandom
>> + RUNTIME DESTINATION bin
>> +)
>> +
>> ADD_EXECUTABLE(kmodloader kmodloader.c)
>> TARGET_LINK_LIBRARIES(kmodloader ubox)
>>
>> diff --git a/getrandom.c b/getrandom.c
>> new file mode 100644
>> index 0000000..9671202
>> --- /dev/null
>> +++ b/getrandom.c
>> @@ -0,0 +1,58 @@
>> +/*
>> + * Copyright (C) 2016 Etienne Champetier <champetier.etienne at gmail.com>
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License as published by
>> + * the Free Software Foundation; either version 2 of the License, or
>> + * (at your option) any later version.
>> + *
>> + * 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.
>> + */
>> +#define _GNU_SOURCE
>> +#include <errno.h>
>> +#include <linux/random.h>
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +#include <string.h>
>> +#include <sys/syscall.h>
>> +#include <unistd.h>
>> +
>> +#define ERROR_EXIT(fmt, ...) do { \
>> + fprintf(stderr, fmt, ## __VA_ARGS__); \
>> + return EXIT_FAILURE; \
>> + } while (0)
>> +
>> +static int usage(char *name)
>> +{
>> + fprintf(stderr, "Usage: %s <nb>\n", name);
>> + fprintf(stderr, " => return <nb> bytes from getrandom()\n");
>> + return EXIT_FAILURE;
>> +}
>> +
>> +int main(int argc, char *argv[])
>> +{
>> + if (argc != 2)
>> + return usage(argv[0]);
>> +
>> + if (isatty(STDOUT_FILENO))
>> + ERROR_EXIT("Not outputting random to a tty\n");
>> +
>> + int nbtot = atoi(argv[1]);
>> + if (nbtot < 1)
>> + ERROR_EXIT("Invalid <nb> param (must be > 0)\n");
>> +
>> + char buf[256];
>> + int len = sizeof(buf);
>> + while (nbtot > 0) {
>> + if (nbtot <= sizeof(buf))
>> + len = nbtot;
>> + if (syscall(SYS_getrandom, buf, len, 0) == -1)
>> + ERROR_EXIT("getrandom() failed: %s\n", strerror(errno));
>> + if (write(STDOUT_FILENO, buf, len) != len)
>> + ERROR_EXIT("write() failed: %s\n", strerror(errno));
>> + nbtot -= sizeof(buf);
>> + }
>> +}
>>
>
> _______________________________________________
> Lede-dev mailing list
> Lede-dev at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/lede-dev
More information about the Lede-dev
mailing list