2 * Copyright (C) 2008-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * GnuTLS is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GnuTLS. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
36 #include <gnutls/gnutls.h>
37 #include <gnutls/crypto.h>
40 static void dump(const char *name, unsigned char *buf, int buf_size)
44 for (i = 0; i < buf_size; i++)
45 printf("%.2x:", buf[i]);
49 #define FILENAME "./rng-test"
53 unsigned char buf1[64];
54 unsigned char buf2[64];
62 for (i = GNUTLS_RND_NONCE; i <= GNUTLS_RND_KEY; i++) {
65 fp = fopen(FILENAME, "w");
67 fail("cannot open file");
69 gnutls_rnd(i, buf1, sizeof(buf1));
71 dump("buf1", buf1, sizeof(buf1));
73 fwrite(buf1, 1, sizeof(buf1), fp);
75 gnutls_global_deinit();
79 gnutls_rnd(i, buf2, sizeof(buf2));
81 dump("buf2", buf2, sizeof(buf2));
82 waitpid(pid, NULL, 0);
84 fp = fopen(FILENAME, "r");
86 fail("cannot open file");
88 ret = fread(buf1, 1, sizeof(buf1), fp);
93 if (ret != sizeof(buf1)) {
94 fail("error testing the random generator (%u).\n", i);
98 if (memcmp(buf1, buf2, sizeof(buf1)) == 0) {
99 fail("error in the random generator (%u). Produces same valus after fork()\n", i);
103 success("success\n");
107 for (i = 0; i <= 65539; i++) {
108 ret = gnutls_rnd(GNUTLS_RND_NONCE, buf1, sizeof(buf1));
110 fail("Error iterating RNG-nonce more than %u times\n", i);
115 for (i = 0; i <= 65539; i++) {
116 ret = gnutls_rnd(GNUTLS_RND_RANDOM, buf1, sizeof(buf1));
118 fail("Error iterating RNG-random more than %u times\n", i);
123 gnutls_global_deinit();