Imported Upstream version 3.3.5
[platform/upstream/gnutls.git] / tests / gc.c
1 /*
2  * Copyright (C) 2004-2012 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuTLS.
5  *
6  * GnuTLS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuTLS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <gnutls/gnutls.h>
28 #include <gnutls/crypto.h>
29 #include "utils.h"
30
31 #include "../lib/gnutls_int.h"
32 #include "../lib/gnutls_hash_int.h"
33 #include "../lib/x509/pbkdf2-sha1.h"
34 #include "../lib/debug.h"
35
36 static void tls_log_func(int level, const char *str)
37 {
38         fprintf(stderr, "|<%d>| %s", level, str);
39 }
40
41 void doit(void)
42 {
43         unsigned char digest[20];
44         int err;
45
46         /* XXX: We need this to fix secure memory. */
47         global_init();
48         gnutls_global_set_log_function(tls_log_func);
49         if (debug)
50                 gnutls_global_set_log_level(4711);
51
52         err =
53             gnutls_hmac_fast(GNUTLS_MAC_MD5, "keykeykey", 9, "abcdefgh", 8,
54                              digest);
55         if (err < 0)
56                 fail("gnutls_hmac_fast(MD5) failed: %d\n", err);
57         else {
58                 if (memcmp(digest, "\x3c\xb0\x9d\x83\x28\x01\xef\xc0"
59                            "\x7b\xb3\xaf\x42\x69\xe5\x93\x9a", 16) == 0) {
60                         if (debug)
61                                 success("gnutls_hmac_fast(MD5) OK\n");
62                 } else {
63                         hexprint(digest, 16);
64                         fail("gnutls_hmac_fast(MD5) failure\n");
65                 }
66         }
67
68         err =
69             gnutls_hmac_fast(GNUTLS_MAC_SHA1, "keykeykey", 9, "abcdefgh",
70                              8, digest);
71         if (err < 0)
72                 fail("gnutls_hmac_fast(SHA1) failed: %d\n", err);
73         else {
74                 if (memcmp(digest, "\x58\x93\x7a\x58\xfe\xea\x82\xf8"
75                            "\x0e\x64\x62\x01\x40\x2b\x2c\xed\x5d\x54\xc1\xfa",
76                            20) == 0) {
77                         if (debug)
78                                 success("gnutls_hmac_fast(SHA1) OK\n");
79                 } else {
80                         hexprint(digest, 20);
81                         fail("gnutls_hmac_fast(SHA1) failure\n");
82                 }
83         }
84
85         err =
86             _gnutls_pbkdf2_sha1("password", 8, (unsigned char *) "salt", 4,
87                                 4711, digest, 16);
88         if (err < 0)
89                 fail("_gnutls_pkcs5_pbkdf2_sha1() failed: %d\n", err);
90         else {
91                 if (memcmp(digest, "\x09\xb7\x85\x57\xdd\xf6\x07\x15"
92                            "\x1c\x52\x34\xde\xba\x5c\xdc\x59", 16) == 0) {
93                         if (debug)
94                                 success
95                                     ("_gnutls_pkcs5_pbkdf2_sha1() OK\n");
96                 } else {
97                         hexprint(digest, 16);
98                         fail("_gnutls_pkcs5_pbkdf2_sha1() failure\n");
99                 }
100         }
101
102         gnutls_global_deinit();
103 }