Imported Upstream version 3.0.30
[platform/upstream/gnutls.git] / tests / pkcs12_s2k.c
1 /*
2  * Copyright (C) 2007-2012 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 unsigned char *salt[3] = { (void*)"salt1", (void*)"ltsa22", (void*)"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 unsigned char *salt;
75   size_t iter;
76   size_t keylen;
77   const char *key;
78 } tv[] =
79 {
80   {
81   1, "smeg", (void*)"\x0A\x58\xCF\x64\x53\x0D\x82\x3F", 1, 24,
82       "8aaae6297b6cb04642ab5b077851284eb7128f1a2a7fbca3"},
83   {
84   2, "smeg", (void*)"\x0A\x58\xCF\x64\x53\x0D\x82\x3F", 1, 8, "79993dfe048d3b76"},
85   {
86   1, "smeg", (void*)"\x64\x2B\x99\xAB\x44\xFB\x4B\x1F", 1, 24,
87       "f3a95fec48d7711e985cfe67908c5ab79fa3d7c5caa5d966"},
88   {
89   2, "smeg", (void*)"\x64\x2B\x99\xAB\x44\xFB\x4B\x1F", 1, 8, "c0a38d64a79bea1d"},
90   {
91   3, "smeg", (void*)"\x3D\x83\xC0\xE4\x54\x6A\xC1\x40", 1, 20,
92       "8d967d88f6caa9d714800ab3d48051d63f73a312"},
93   {
94   1, "queeg", (void*)"\x05\xDE\xC9\x59\xAC\xFF\x72\xF7", 1000, 24,
95       "ed2034e36328830ff09df1e1a07dd357185dac0d4f9eb3d4"},
96   {
97   2, "queeg", (void*)"\x05\xDE\xC9\x59\xAC\xFF\x72\xF7", 1000, 8,
98       "11dedad7758d4860"},
99   {
100   1, "queeg", (void*)"\x16\x82\xC0\xFC\x5B\x3F\x7E\xC5", 1000, 24,
101       "483dd6e919d7de2e8e648ba8f862f3fbfbdc2bcb2c02957f"},
102   {
103   2, "queeg", (void*)"\x16\x82\xC0\xFC\x5B\x3F\x7E\xC5", 1000, 8,
104       "9d461d1b00355c50"},
105   {
106   3, "queeg", (void*)"\x26\x32\x16\xFC\xC2\xFA\xB3\x1C", 1000, 20,
107       "5ec4c7a80df652294c3925b6489a7ab857c83476"}
108 };
109
110 void
111 doit (void)
112 {
113   int rc;
114   unsigned int i, j, x;
115   unsigned char key[32];
116   char tmp[1024];
117
118   gnutls_global_init ();
119
120   gnutls_global_set_log_function (tls_log_func);
121   if (debug)
122     gnutls_global_set_log_level (99);
123
124   x = 0;
125   for (i = 1; i < 4; i++)
126     {
127       for (j = 0; j < 3; j++)
128         {
129           rc =
130             _gnutls_pkcs12_string_to_key (i, salt[j], strlen ((char*)salt[j]),
131                                           j + i + 15, pw[j], sizeof (key),
132                                           key);
133           if (rc < 0)
134             fail ("_gnutls_pkcs12_string_to_key failed[0]: %d\n", rc);
135
136           if (strcmp (_gnutls_bin2hex (key, sizeof (key),
137                                        tmp, sizeof (tmp), NULL),
138                       values[x]) != 0)
139             fail ("_gnutls_pkcs12_string_to_key failed[1]\n");
140
141           if (debug)
142             printf ("ij: %d.%d: %s\n", i, j,
143                     _gnutls_bin2hex (key, sizeof (key), tmp, sizeof (tmp),
144                                      NULL));
145           x++;
146         }
147     }
148   if (debug)
149     printf ("\n");
150
151   for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
152     {
153       rc = _gnutls_pkcs12_string_to_key (tv[i].id, tv[i].salt, 8,
154                                          tv[i].iter, tv[i].password,
155                                          tv[i].keylen, key);
156       if (rc < 0)
157         fail ("_gnutls_pkcs12_string_to_key failed[2]: %d\n", rc);
158
159       if (memcmp (_gnutls_bin2hex (key, tv[i].keylen,
160                                    tmp, sizeof (tmp), NULL),
161                   tv[i].key, tv[i].keylen) != 0)
162         fail ("_gnutls_pkcs12_string_to_key failed[3]\n");
163
164       if (debug)
165         printf ("tv[%d]: %s\n", i,
166                 _gnutls_bin2hex (key, tv[i].keylen, tmp, sizeof (tmp), NULL));
167     }
168   if (debug)
169     printf ("\n");
170
171   gnutls_global_deinit ();
172
173   if (debug)
174     success ("_gnutls_pkcs12_string_to_key ok\n");
175 }