Tizen 2.0 Release
[external/libgnutls26.git] / tests / crypto_rng.c
1 /*
2  * Copyright (C) 2008, 2010 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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/>.
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28
29 #include "utils.h"
30
31 #include <gnutls/gnutls.h>
32 #include <gnutls/crypto.h>
33 #include "../lib/random.h"
34
35 static int
36 rng_init (void **ctx)
37 {
38   return 0;
39 }
40
41 static int
42 rng_rnd (void *ctx, int level, void *data, size_t datasize)
43 {
44   memset (data, 1, datasize);
45   return 0;
46 }
47
48 static void
49 rng_deinit (void *ctx)
50 {
51 }
52
53 void
54 doit (void)
55 {
56   int rc;
57   char buf1[32];
58   char buf2[32];
59   int failed = 0;
60   gnutls_crypto_rnd_st rng = { rng_init, rng_rnd, rng_deinit };
61
62
63   rc = gnutls_crypto_rnd_register (0, &rng);
64
65   gnutls_global_init ();
66
67   memset (buf2, 1, sizeof (buf2));
68
69   _gnutls_rnd (GNUTLS_RND_RANDOM, buf1, sizeof (buf1));
70
71   if (memcmp (buf1, buf2, sizeof (buf1)) != 0)
72     failed = 1;
73
74   gnutls_global_deinit ();
75
76   if (failed == 0)
77     {
78       success ("rng registered ok\n");
79     }
80   else
81     {
82       fail ("rng register test failed: %d\n", rc);
83     }
84 }