summaryrefslogtreecommitdiff
path: root/tests/suite/rng.c
blob: 019bd8730c7a69a0979ca660bdaf3d285f5373fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (C) 2014 Nikos Mavrogiannopouls
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS 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 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with GnuTLS.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>

int main(int argc, char **argv)
{
	unsigned char buf[64];
	unsigned level, nbytes;
	FILE *fp;
	unsigned i;

	gnutls_global_init();

	if (argc != 4) {
		fprintf(stderr, "args %d\nusage: %s [nonce|key] [nbytes] [outfile]\n", argc, argv[0]);
		exit(1);
	}

	if (strcasecmp(argv[1], "nonce") == 0) {
		level = GNUTLS_RND_NONCE;
	} else if (strcasecmp(argv[1], "key") == 0) {
		level = GNUTLS_RND_KEY;
	} else {
		fprintf(stderr, "don't know %s\n", argv[1]);
		fprintf(stderr, "usage: %s [nonce|key] [nbytes] [outfile]\n", argv[0]);
		exit(1);
	}

	nbytes = atoi(argv[2]);

	fp = fopen(argv[3], "w");
	if (fp == NULL) {
		fprintf(stderr, "Cannot open %s\n", argv[3]);
		exit(1);
	}

	for (i = 0; i < nbytes; i+=sizeof(buf)) {
		if (gnutls_rnd(level, buf, sizeof(buf)) < 0)
			exit(2);

		fwrite(buf, 1, sizeof(buf), fp);
	}
	fclose(fp);

	gnutls_global_deinit();
	exit(0);
}