tst-getrandom.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Tests for the getentropy, getrandom functions.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/random.h>
  20. /* Set to true if any errors are encountered. */
  21. static bool errors;
  22. /* Test getrandom with a single buffer length. NB: The passed-in
  23. buffer must have room for four extra bytes after the specified
  24. length, which are used to test that getrandom leaves those bytes
  25. unchanged. */
  26. static void
  27. test_length (char *buffer, size_t length, unsigned int flags)
  28. {
  29. memset (buffer, 0, length);
  30. strcpy (buffer + length, "123");
  31. ssize_t ret = getrandom (buffer, length, flags);
  32. if (ret < 0)
  33. {
  34. /* EAGAIN is an expected error with GRND_RANDOM and
  35. GRND_NONBLOCK. */
  36. if ((flags & GRND_RANDOM)
  37. && (flags & GRND_NONBLOCK)
  38. && errno == EAGAIN)
  39. return;
  40. printf ("error: getrandom (%zu, 0x%x): %m\n", length, flags);
  41. errors = true;
  42. return;
  43. }
  44. if (ret != length)
  45. {
  46. if (flags & GRND_RANDOM)
  47. {
  48. if (ret == 0 || ret > length)
  49. {
  50. printf ("error: getrandom (%zu, 0x%x) returned %zd\n",
  51. length, flags, ret);
  52. errors = true;
  53. }
  54. }
  55. else
  56. {
  57. printf ("error: getrandom (%zu, 0x%x) returned %zd\n",
  58. length, flags, ret);
  59. errors = true;
  60. }
  61. }
  62. if (length >= 7)
  63. {
  64. /* One spurious test failure in 2**56 is sufficiently
  65. unlikely. */
  66. int non_null = 0;
  67. for (int i = 0; i < length; ++i)
  68. non_null += buffer[i] != 0;
  69. if (non_null == 0)
  70. {
  71. printf ("error: getrandom (%zu, 0x%x) returned all-zero bytes\n",
  72. length, flags);
  73. errors = true;
  74. }
  75. }
  76. if (memcmp (buffer + length, "123", 4) != 0)
  77. {
  78. printf ("error: getrandom (%zu, 0x%x) wrote spurious bytes\n",
  79. length, flags);
  80. errors = true;
  81. }
  82. }
  83. /* Call getrandom repeatedly to fill the buffer. */
  84. static bool
  85. getrandom_full (char *buffer, size_t length, unsigned int flags)
  86. {
  87. char *end = buffer + length;
  88. while (buffer < end)
  89. {
  90. ssize_t ret = getrandom (buffer, end - buffer, flags);
  91. if (ret < 0)
  92. {
  93. printf ("error: getrandom (%zu, 0x%x): %m\n", length, flags);
  94. errors = true;
  95. return false;
  96. }
  97. buffer += ret;
  98. }
  99. return true;
  100. }
  101. static void
  102. test_flags (unsigned int flags)
  103. {
  104. /* Test various lengths, but only for !GRND_RANDOM, to conserve
  105. entropy. */
  106. {
  107. enum { max_length = 300 };
  108. char buffer[max_length + 4];
  109. if (flags & GRND_RANDOM)
  110. test_length (buffer, 0, flags);
  111. else
  112. {
  113. for (int length = 0; length <= 9; ++length)
  114. test_length (buffer, length, flags);
  115. test_length (buffer, 16, flags);
  116. test_length (buffer, max_length, flags);
  117. }
  118. }
  119. /* Test that getrandom returns different data. */
  120. if (!(flags & GRND_NONBLOCK))
  121. {
  122. char buffer1[8];
  123. memset (buffer1, 0, sizeof (buffer1));
  124. char buffer2[8];
  125. memset (buffer2, 0, sizeof (buffer2));
  126. if (getrandom_full (buffer1, sizeof (buffer1), flags)
  127. && getrandom_full (buffer2, sizeof (buffer2), flags))
  128. {
  129. /* The probability that these two 8-byte buffers are equal
  130. is very small (assuming that two subsequent calls to
  131. getrandom result are independent, uniformly distributed
  132. random variables). */
  133. if (memcmp (buffer1, buffer2, sizeof (buffer1)) == 0)
  134. {
  135. printf ("error: getrandom returns constant value\n");
  136. errors = true;
  137. }
  138. }
  139. }
  140. }
  141. static void
  142. test_getentropy (void)
  143. {
  144. char buf[16];
  145. memset (buf, '@', sizeof (buf));
  146. if (getentropy (buf, 0) != 0)
  147. {
  148. printf ("error: getentropy zero length: %m\n");
  149. errors = true;
  150. return;
  151. }
  152. for (size_t i = 0; i < sizeof (buf); ++i)
  153. if (buf[i] != '@')
  154. {
  155. printf ("error: getentropy modified zero-length buffer\n");
  156. errors = true;
  157. return;
  158. }
  159. if (getentropy (buf, sizeof (buf)) != 0)
  160. {
  161. printf ("error: getentropy buf: %m\n");
  162. errors = true;
  163. return;
  164. }
  165. char buf2[256];
  166. _Static_assert (sizeof (buf) < sizeof (buf2), "buf and buf2 compatible");
  167. memset (buf2, '@', sizeof (buf2));
  168. if (getentropy (buf2, sizeof (buf)) != 0)
  169. {
  170. printf ("error: getentropy buf2: %m\n");
  171. errors = true;
  172. return;
  173. }
  174. /* The probability that these two buffers are equal is very
  175. small. */
  176. if (memcmp (buf, buf2, sizeof (buf) == 0))
  177. {
  178. printf ("error: getentropy appears to return constant bytes\n");
  179. errors = true;
  180. return;
  181. }
  182. for (size_t i = sizeof (buf); i < sizeof (buf2); ++i)
  183. if (buf2[i] != '@')
  184. {
  185. printf ("error: getentropy wrote beyond the end of the buffer\n");
  186. errors = true;
  187. return;
  188. }
  189. char buf3[257];
  190. if (getentropy (buf3, sizeof (buf3)) == 0)
  191. {
  192. printf ("error: getentropy successful for 257 byte buffer\n");
  193. errors = true;
  194. return;
  195. }
  196. if (errno != EIO)
  197. {
  198. printf ("error: getentropy wrong error for 257 byte buffer: %m\n");
  199. errors = true;
  200. return;
  201. }
  202. }
  203. static int
  204. do_test (void)
  205. {
  206. /* Check if getrandom is not supported by this system. */
  207. if (getrandom (NULL, 0, 0) == -1 && errno == ENOSYS)
  208. return 77;
  209. for (int use_random = 0; use_random < 2; ++use_random)
  210. for (int use_nonblock = 0; use_nonblock < 2; ++use_nonblock)
  211. {
  212. unsigned int flags = 0;
  213. if (use_random)
  214. flags |= GRND_RANDOM;
  215. if (use_nonblock)
  216. flags |= GRND_NONBLOCK;
  217. test_flags (flags);
  218. }
  219. test_getentropy ();
  220. return errors;
  221. }
  222. #include <support/test-driver.c>