1 /* random.c - part of the Libgcrypt test suite.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include "../src/gcrypt.h"
36 die (const char *format, ...)
40 va_start (arg_ptr, format);
41 vfprintf (stderr, format, arg_ptr);
48 print_hex (const char *text, const void *buf, size_t n)
50 const unsigned char *p = buf;
60 writen (int fd, const void *buf, size_t nbytes)
62 size_t nleft = nbytes;
67 nwritten = write (fd, buf, nleft);
76 buf = (const char*)buf + nwritten;
83 readn (int fd, void *buf, size_t buflen, size_t *ret_nread)
85 size_t nleft = buflen;
90 nread = read ( fd, buf, nleft );
101 buf = (char*)buf + nread;
104 *ret_nread = buflen - nleft;
110 /* Check that forking won't return the same random. */
118 char tmp1[16], tmp1c[16], tmp1p[16];
120 /* We better make sure that the RNG has been initialzied. */
121 gcry_randomize (tmp1, sizeof tmp1, GCRY_STRONG_RANDOM);
123 print_hex ("initial random: ", tmp1, sizeof tmp1);
126 die ("pipe failed: %s\n", strerror (errno));
129 if (pid == (pid_t)(-1))
130 die ("fork failed: %s\n", strerror (errno));
133 gcry_randomize (tmp1c, sizeof tmp1c, GCRY_STRONG_RANDOM);
134 if (writen (rp[1], tmp1c, sizeof tmp1c))
135 die ("write failed: %s\n", strerror (errno));
138 print_hex (" child random: ", tmp1c, sizeof tmp1c);
143 gcry_randomize (tmp1p, sizeof tmp1p, GCRY_STRONG_RANDOM);
145 print_hex (" parent random: ", tmp1p, sizeof tmp1p);
148 if (readn (rp[0], tmp1c, sizeof tmp1c, &nread))
149 die ("read failed: %s\n", strerror (errno));
150 if (nread != sizeof tmp1c)
151 die ("read too short\n");
153 while ( (i=waitpid (pid, &status, 0)) == -1 && errno == EINTR)
156 && WIFEXITED (status) && !WEXITSTATUS (status))
159 die ("child failed\n");
161 if (!memcmp (tmp1p, tmp1c, sizeof tmp1c))
162 die ("parent and child got the same random number\n");
167 /* Check that forking won't return the same nonce. */
169 check_nonce_forking (void)
175 char nonce1[10], nonce1c[10], nonce1p[10];
177 /* We won't get the same nonce back if we never initialized the
178 nonce subsystem, thus we get one nonce here and forget about
180 gcry_create_nonce (nonce1, sizeof nonce1);
182 print_hex ("initial nonce: ", nonce1, sizeof nonce1);
185 die ("pipe failed: %s\n", strerror (errno));
188 if (pid == (pid_t)(-1))
189 die ("fork failed: %s\n", strerror (errno));
192 gcry_create_nonce (nonce1c, sizeof nonce1c);
193 if (writen (rp[1], nonce1c, sizeof nonce1c))
194 die ("write failed: %s\n", strerror (errno));
197 print_hex (" child nonce: ", nonce1c, sizeof nonce1c);
202 gcry_create_nonce (nonce1p, sizeof nonce1p);
204 print_hex (" parent nonce: ", nonce1p, sizeof nonce1p);
207 if (readn (rp[0], nonce1c, sizeof nonce1c, &nread))
208 die ("read failed: %s\n", strerror (errno));
209 if (nread != sizeof nonce1c)
210 die ("read too short\n");
212 while ( (i=waitpid (pid, &status, 0)) == -1 && errno == EINTR)
215 && WIFEXITED (status) && !WEXITSTATUS (status))
218 die ("child failed\n");
220 if (!memcmp (nonce1p, nonce1c, sizeof nonce1c))
221 die ("parent and child got the same nonce\n");
230 main (int argc, char **argv)
234 if ((argc > 1) && (! strcmp (argv[1], "--verbose")))
236 else if ((argc > 1) && (! strcmp (argv[1], "--debug")))
239 signal (SIGPIPE, SIG_IGN);
241 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
242 if (!gcry_check_version (GCRYPT_VERSION))
243 die ("version mismatch\n");
245 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
247 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
250 check_nonce_forking ();