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