]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - package/libkcapi/0003-Unify-code-to-read-from-seed-sources.patch
libksba: add hash for license files
[coffee/buildroot.git] / package / libkcapi / 0003-Unify-code-to-read-from-seed-sources.patch
1 From 450dfb09ee72ffedea8f2a25fdce17295f01f62f Mon Sep 17 00:00:00 2001
2 From: Stephan Mueller <smueller@chronox.de>
3 Date: Tue, 8 Aug 2017 10:04:06 +0200
4 Subject: [PATCH] Unify code to read from seed sources
5
6 Remove the code duplication for FD reads and syscall reads.
7
8 This patch also fixes the use of __NR_getrandom resolution.
9
10 [Upstream commit: https://github.com/smuellerDD/libkcapi/commit/450dfb09ee72ffedea8f2a25fdce17295f01f62f]
11 Signed-off-by: Marcin Nowakowski <marcin.nowakowski@imgtec.com>
12
13 ---
14  apps/kcapi-rng.c | 71 +++++++++++++++++++++++++-------------------------------
15  1 file changed, 32 insertions(+), 39 deletions(-)
16
17 diff --git a/apps/kcapi-rng.c b/apps/kcapi-rng.c
18 index e15edf9..96da111 100644
19 --- a/apps/kcapi-rng.c
20 +++ b/apps/kcapi-rng.c
21 @@ -17,7 +17,9 @@
22   * DAMAGE.
23   */
24  
25 +#define _GNU_SOURCE
26  #include <unistd.h>
27 +#include <sys/syscall.h>
28  #include <errno.h>
29  #include <limits.h>
30  #include <stdint.h>
31 @@ -41,56 +43,48 @@
32  /* Minimum seed is 256 bits. */
33  #define KCAPI_RNG_MINSEEDSIZE 32
34  
35 -struct kcapi_handle *rng = NULL;
36 -unsigned int Verbosity = 0;
37 -char *rng_name = NULL;
38 +static struct kcapi_handle *rng = NULL;
39 +static unsigned int Verbosity = 0;
40 +static char *rng_name = NULL;
41  
42 -#ifndef HAVE_GETRANDOM
43 -static int read_complete(int fd, uint8_t *buf, uint32_t buflen)
44 +#if !defined(HAVE_GETRANDOM) && !defined(__NR_getrandom)
45 +static int random_fd = -1;
46 +static int open_random(void)
47  {
48 -       ssize_t ret;
49 +       random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
50 +       if (0 > random_fd)
51 +               return random_fd;
52  
53 -       do {
54 -               ret = read(fd, buf, buflen);
55 -               if (0 < ret) {
56 -                       buflen -= ret;
57 -                       buf += ret;
58 -               }
59 -       } while ((0 < ret || EINTR == errno || ERESTART == errno)
60 -                && buflen > 0);
61 -
62 -       if (buflen == 0)
63 -               return 0;
64 -       return 1;
65 +       return 0;
66  }
67  
68 -static int read_random(uint8_t *buf, uint32_t buflen)
69 +static void close_random(void)
70  {
71 -       int fd;
72 -       int ret = 0;
73 -
74 -       fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
75 -       if (0 > fd)
76 -               return fd;
77 -
78 -       ret = read_complete(fd, buf, buflen);
79 -       close(fd);
80 -       return ret;
81 +       close(random_fd);
82  }
83  #endif
84  
85  static int get_random(uint8_t *buf, uint32_t buflen)
86  {
87 +       ssize_t ret;
88 +
89         if (buflen > INT_MAX)
90                 return 1;
91  
92 +#if (!defined(HAVE_GETRANDOM) && !defined(__NR_getrandom))
93 +       ret = open_random();
94 +       if (ret)
95 +               return ret;
96 +#endif
97 +
98 +       do {
99  #ifdef HAVE_GETRANDOM
100 -       return getrandom(buf, buflen, 0);
101 +               ret = getrandom(buf, buflen, 0);
102 +#elif defined __NR_getrandom
103 +               ret = syscall(__NR_getrandom, buf, buflen, 0);
104  #else
105 -# ifdef __NR_getrandom
106 -       do {
107 -               int ret = syscall(__NR_getrandom, buf, buflen, 0);
108 -
109 +               ret = read(random_fd, buf, buflen);
110 +#endif
111                 if (0 < ret) {
112                         buflen -= ret;
113                         buf += ret;
114 @@ -98,14 +92,13 @@ static int get_random(uint8_t *buf, uint32_t buflen)
115         } while ((0 < ret || EINTR == errno || ERESTART == errno)
116                  && buflen > 0);
117  
118 +#if (!defined(HAVE_GETRANDOM) && !defined(__NR_getrandom))
119 +       close_random();
120 +#endif
121 +
122         if (buflen == 0)
123                 return 0;
124 -
125         return 1;
126 -# else
127 -       return read_random(buf, buflen);
128 -# endif
129 -#endif
130  }
131  
132  static void usage(void)