Tizen 2.0 Release
[external/libgnutls26.git] / tests / pkcs12_s2k.c
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
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, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28
29 #include <utils.h>
30 #include "../../lib/gnutls_int.h"
31 #include "../../lib/x509/x509_int.h"
32 #include "../../lib/debug.h"
33
34 static void
35 tls_log_func (int level, const char *str)
36 {
37   fprintf (stderr, "|<%d>| %s", level, str);
38 }
39
40 static const char *salt[3] = { "salt1", "ltsa22", "balt33" };
41 static const char *pw[3] = { "secret1", "verysecret2", "veryverysecret3" };
42
43 static const char *values[] = {
44 /* 1.0 */
45   "85a3c676a66f0960f4807144a28c8d61a0001b81846f301a1ac164289879972f",
46 /* 1.2 */
47   "e659da7d5989733a3d268e0bf7752c35c116e5c75919449a98f6812f82a15b16",
48 /* 1.2 */
49   "878b8a88bf6166ce803b7498822205b1ac82870d3aec20807148779375a61f1e",
50 /* 2.0 */
51   "1c845be764371d633c7fd1056967a9940385e110e85b58f826d39ae8561a0019",
52 /* 2.1 */
53   "de8dd3ffd59b65d3d5f59a1f71d7add582741f7752a786c045953e727e4465c0",
54 /* 2.2 */
55 #ifndef PKCS12_BROKEN_KEYGEN
56   "9dd7f19e5e6aee5c5008b5deefd35889ab75193594ed49a605df4e93e7c2a155",
57 #else
58   "9dd7f19e5e6aee5c5008b5deefd35889ab7519356f13478ecdee593c5ed689b1",
59 #endif
60   /* 3.0 */
61   "1c165e5a291a1539f3dbcf82a3e6ed566eb9d50ad4b0b3b57b599b08f0531236",
62 /* 3.1 */
63   "5c9abee3cde31656eedfc131b7c2f8061032a3c705961ee2306a826c8b4b1a76",
64 /* 3.2 */
65   "a9c94e0acdaeaea54d1b1b681c3b64916396a352dea7ffe635fb2c11d8502e98"
66 };
67
68 /* Values derived from
69    http://www.drh-consultancy.demon.co.uk/test.txt */
70 static struct
71 {
72   int id;
73   const char *password;
74   const char *salt;
75   size_t iter;
76   size_t keylen;
77   const char *key;
78 } tv[] =
79 {
80   {
81   1, "smeg", "\x0A\x58\xCF\x64\x53\x0D\x82\x3F", 1, 24,
82       "8aaae6297b6cb04642ab5b077851284eb7128f1a2a7fbca3"},
83   {
84   2, "smeg", "\x0A\x58\xCF\x64\x53\x0D\x82\x3F", 1, 8, "79993dfe048d3b76"},
85   {
86   1, "smeg", "\x64\x2B\x99\xAB\x44\xFB\x4B\x1F", 1, 24,
87       "f3a95fec48d7711e985cfe67908c5ab79fa3d7c5caa5d966"},
88   {
89   2, "smeg", "\x64\x2B\x99\xAB\x44\xFB\x4B\x1F", 1, 8, "c0a38d64a79bea1d"},
90   {
91   3, "smeg", "\x3D\x83\xC0\xE4\x54\x6A\xC1\x40", 1, 20,
92       "8d967d88f6caa9d714800ab3d48051d63f73a312"},
93   {
94   1, "queeg", "\x05\xDE\xC9\x59\xAC\xFF\x72\xF7", 1000, 24,
95       "ed2034e36328830ff09df1e1a07dd357185dac0d4f9eb3d4"},
96   {
97   2, "queeg", "\x05\xDE\xC9\x59\xAC\xFF\x72\xF7", 1000, 8,
98       "11dedad7758d4860"},
99   {
100   1, "queeg", "\x16\x82\xC0\xFC\x5B\x3F\x7E\xC5", 1000, 24,
101       "483dd6e919d7de2e8e648ba8f862f3fbfbdc2bcb2c02957f"},
102   {
103   2, "queeg", "\x16\x82\xC0\xFC\x5B\x3F\x7E\xC5", 1000, 8,
104       "9d461d1b00355c50"},
105   {
106   3, "queeg", "\x26\x32\x16\xFC\xC2\xFA\xB3\x1C", 1000, 20,
107       "5ec4c7a80df652294c3925b6489a7ab857c83476"}
108 };
109
110 void
111 doit (void)
112 {
113   int rc, i, j, x;
114   char key[32];
115   char tmp[1024];
116
117   gnutls_global_init ();
118
119   gnutls_global_set_log_function (tls_log_func);
120   if (debug)
121     gnutls_global_set_log_level (99);
122
123   x = 0;
124   for (i = 1; i < 4; i++)
125     {
126       for (j = 0; j < 3; j++)
127         {
128           rc =
129             _gnutls_pkcs12_string_to_key (i, salt[j], strlen (salt[j]),
130                                           j + i + 15, pw[j], sizeof (key),
131                                           key);
132           if (rc < 0)
133             fail ("_gnutls_pkcs12_string_to_key failed[0]: %d\n", rc);
134
135           if (strcmp (_gnutls_bin2hex (key, sizeof (key),
136                                        tmp, sizeof (tmp), NULL),
137                       values[x]) != 0)
138             fail ("_gnutls_pkcs12_string_to_key failed[1]\n");
139
140           if (debug)
141             printf ("ij: %d.%d: %s\n", i, j,
142                     _gnutls_bin2hex (key, sizeof (key), tmp, sizeof (tmp),
143                                      NULL));
144           x++;
145         }
146     }
147   if (debug)
148     printf ("\n");
149
150   for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
151     {
152       rc = _gnutls_pkcs12_string_to_key (tv[i].id, tv[i].salt, 8,
153                                          tv[i].iter, tv[i].password,
154                                          tv[i].keylen, key);
155       if (rc < 0)
156         fail ("_gnutls_pkcs12_string_to_key failed[2]: %d\n", rc);
157
158       if (memcmp (_gnutls_bin2hex (key, tv[i].keylen,
159                                    tmp, sizeof (tmp), NULL),
160                   tv[i].key, tv[i].keylen) != 0)
161         fail ("_gnutls_pkcs12_string_to_key failed[3]\n");
162
163       if (debug)
164         printf ("tv[%d]: %s\n", i,
165                 _gnutls_bin2hex (key, tv[i].keylen, tmp, sizeof (tmp), NULL));
166     }
167   if (debug)
168     printf ("\n");
169
170   gnutls_global_deinit ();
171
172   if (debug)
173     success ("_gnutls_pkcs12_string_to_key ok\n");
174 }