]> pd.if.org Git - zpackage/blob - pkcs5/pkcs_5_test.c
remove stray debug fprintf
[zpackage] / pkcs5 / pkcs_5_test.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12   @file hkdf_test.c
13   PKCS #5 support, self-test, Steffen Jaeckel
14 */
15
16 #ifdef LTC_PKCS_5
17
18 /*
19     TEST CASES SOURCE:
20
21 Internet Engineering Task Force (IETF)                      S. Josefsson
22 Request for Comments: 6070                                        SJD AB
23 Category: Informational                                     January 2011
24 ISSN: 2070-1721
25 */
26
27 /**
28   PKCS #5 self-test
29   @return CRYPT_OK if successful, CRYPT_NOP if tests have been disabled.
30 */
31 int pkcs_5_test (void)
32 {
33  #ifndef LTC_TEST
34     return CRYPT_NOP;
35  #else
36
37     typedef struct {
38         const char* P;
39         unsigned long P_len;
40         const char* S;
41         unsigned long S_len;
42         int c;
43         unsigned long dkLen;
44         unsigned char DK[40];
45     } case_item;
46
47     static const case_item cases_5_2[] = {
48         {
49             "password",
50             8,
51             "salt",
52             4,
53             1,
54             20,
55             { 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
56               0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
57               0x2f, 0xe0, 0x37, 0xa6 }
58         },
59         {
60             "password",
61             8,
62             "salt",
63             4,
64             2,
65             20,
66             { 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
67               0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
68               0xd8, 0xde, 0x89, 0x57 }
69         },
70 #ifdef LTC_TEST_EXT
71         {
72             "password",
73             8,
74             "salt",
75             4,
76             4096,
77             20,
78             { 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
79               0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
80               0x65, 0xa4, 0x29, 0xc1 }
81         },
82         {
83             "password",
84             8,
85             "salt",
86             4,
87             16777216,
88             20,
89             { 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
90               0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
91               0x26, 0x34, 0xe9, 0x84 }
92         },
93         {
94             "passwordPASSWORDpassword",
95             25,
96             "saltSALTsaltSALTsaltSALTsaltSALTsalt",
97             36,
98             4096,
99             25,
100             { 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
101               0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
102               0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
103               0x38 }
104         },
105         {
106             "pass\0word",
107             9,
108             "sa\0lt",
109             5,
110             4096,
111             16,
112             { 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
113               0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 }
114         },
115 #endif /* LTC_TEST_EXT */
116     };
117
118     static const case_item cases_5_1[] = {
119         {
120             "password",
121             8,
122             "saltsalt", /* must be 8 octects */
123             8,          /* ignored by alg1 */
124             1,
125             20,
126             { 0xca, 0xb8, 0x6d, 0xd6, 0x26, 0x17, 0x10, 0x89, 0x1e, 0x8c,
127               0xb5, 0x6e, 0xe3, 0x62, 0x56, 0x91, 0xa7, 0x5d, 0xf3, 0x44 }
128         },
129     };
130
131     static const case_item cases_5_1o[] = {
132         {
133             "password",
134             8,
135             "saltsalt", /* must be 8 octects */
136             8,          /* ignored by alg1_openssl */
137             1,
138             20,
139             { 0xca, 0xb8, 0x6d, 0xd6, 0x26, 0x17, 0x10, 0x89, 0x1e, 0x8c,
140               0xb5, 0x6e, 0xe3, 0x62, 0x56, 0x91, 0xa7, 0x5d, 0xf3, 0x44 }
141
142         },
143         {
144             "password",
145             8,
146             "saltsalt", /* must be 8 octects */
147             8,          /* ignored by alg1_openssl */
148             1,
149             30,
150             { 0xca, 0xb8, 0x6d, 0xd6, 0x26, 0x17, 0x10, 0x89, 0x1e, 0x8c,
151               0xb5, 0x6e, 0xe3, 0x62, 0x56, 0x91, 0xa7, 0x5d, 0xf3, 0x44,
152               0xf0, 0xbf, 0xf4, 0xc1, 0x2c, 0xf3, 0x59, 0x6f, 0xc0, 0x0b }
153
154         }
155     };
156
157     unsigned char DK[40];
158     unsigned long dkLen;
159     int i, err;
160     int tested=0, failed=0;
161     int hash = find_hash("sha1");
162     if (hash == -1)
163     {
164 #ifdef LTC_TEST_DBG
165       printf("PKCS#5 test failed: 'sha1' hash not found\n");
166 #endif
167       return CRYPT_ERROR;
168     }
169
170     /* testing alg 2 */
171     for(i=0; i < (int)(sizeof(cases_5_2) / sizeof(cases_5_2[0])); i++) {
172         ++tested;
173         dkLen = cases_5_2[i].dkLen;
174         if((err = pkcs_5_alg2((unsigned char*)cases_5_2[i].P, cases_5_2[i].P_len,
175                               (unsigned char*)cases_5_2[i].S, cases_5_2[i].S_len,
176                               cases_5_2[i].c, hash,
177                               DK, &dkLen)) != CRYPT_OK) {
178 #ifdef LTC_TEST_DBG
179             printf("\npkcs_5_alg2() #%d: Failed/1 (%s)\n", i, error_to_string(err));
180 #endif
181             ++failed;
182         }
183         else if (compare_testvector(DK, dkLen, cases_5_2[i].DK, cases_5_2[i].dkLen, "PKCS#5_2", i)) {
184             ++failed;
185         }
186     }
187
188     /* testing alg 1 */
189     for(i=0; i < (int)(sizeof(cases_5_1) / sizeof(case_item)); i++, tested++) {
190         dkLen = cases_5_1[i].dkLen;
191         if((err = pkcs_5_alg1((unsigned char*)cases_5_1[i].P, cases_5_1[i].P_len,
192                               (unsigned char*)cases_5_1[i].S,
193                               cases_5_1[i].c, hash,
194                               DK, &dkLen)) != CRYPT_OK) {
195 #ifdef LTC_TEST_DBG
196             printf("\npkcs_5_alg1() #%d: Failed/1 (%s)\n", i, error_to_string(err));
197 #endif
198             ++failed;
199         }
200         else if (compare_testvector(DK, dkLen, cases_5_1[i].DK, cases_5_1[i].dkLen, "PKCS#5_1", i)) {
201             ++failed;
202         }
203     }
204
205     /* testing alg 1_openssl */
206     for(i = 0; i < (int)(sizeof(cases_5_1o) / sizeof(cases_5_1o[0])); i++, tested++) {
207         dkLen = cases_5_1o[i].dkLen;
208         if ((err = pkcs_5_alg1_openssl((unsigned char*)cases_5_1o[i].P, cases_5_1o[i].P_len,
209                                        (unsigned char*)cases_5_1o[i].S,
210                                        cases_5_1o[i].c, hash,
211                                        DK, &dkLen)) != CRYPT_OK) {
212 #ifdef LTC_TEST_DBG
213             printf("\npkcs_5_alg1_openssl() #%d: Failed/1 (%s)\n", i, error_to_string(err));
214 #endif
215             ++failed;
216         }
217         else if (compare_testvector(DK, dkLen, cases_5_1o[i].DK, cases_5_1o[i].dkLen, "PKCS#5_1o", i)) {
218             ++failed;
219         }
220     }
221
222     return (failed != 0) ? CRYPT_FAIL_TESTVECTOR : CRYPT_OK;
223  #endif
224 }
225
226 #endif
227
228
229 /* ref:         $Format:%D$ */
230 /* git commit:  $Format:%H$ */
231 /* commit time: $Format:%ai$ */