Make it possible to disable fuzz testing
[openssl.git] / test / p5_crpt2_test.c
1 /*
2  * Copyright 2013-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "../e_os.h"
14
15 #include <openssl/opensslconf.h>
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/conf.h>
19
20 typedef struct {
21     const char *pass;
22     int passlen;
23     const char *salt;
24     int saltlen;
25     int iter;
26 } testdata;
27
28 static testdata test_cases[] = {
29     {"password", 8, "salt", 4, 1},
30     {"password", 8, "salt", 4, 2},
31     {"password", 8, "salt", 4, 4096},
32     {"passwordPASSWORDpassword", 24,
33      "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, 4096},
34     {"pass\0word", 9, "sa\0lt", 5, 4096},
35     {NULL},
36 };
37
38 static const char *sha1_results[] = {
39     "0c60c80f961f0e71f3a9b524af6012062fe037a6",
40     "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957",
41     "4b007901b765489abead49d926f721d065a429c1",
42     "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038",
43     "56fa6aa75548099dcc37d7f03425e0c3",
44 };
45
46 static const char *sha256_results[] = {
47     "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b",
48     "ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43",
49     "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a",
50     "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c63551"
51         "8c7dac47e9",
52     "89b69d0516f829893c696226650a8687",
53 };
54
55 static const char *sha512_results[] = {
56     "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d47"
57         "0a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce",
58     "e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab"
59         "2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e",
60     "d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30"
61         "602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5",
62     "8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59"
63         "f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8",
64     "9d9e9c4cd21fe4be24d5b8244c759665",
65 };
66
67 static void hexdump(FILE *f, const char *title, const unsigned char *s, int l)
68 {
69     int i;
70     fprintf(f, "%s", title);
71     for (i = 0; i < l; i++) {
72         fprintf(f, "%02x", s[i]);
73     }
74     fprintf(f, "\n");
75 }
76
77 static void convert(unsigned char *dst, const unsigned char *src, int len)
78 {
79     int i;
80     for (i = 0; i < len; i++, dst++, src += 2) {
81         unsigned int n;
82         sscanf((char *)src, "%2x", &n);
83         *dst = (unsigned char)n;
84     }
85     *dst = 0;
86 }
87
88 static void
89 test_p5_pbkdf2(int i, char *digestname, testdata *test, const char *hex)
90 {
91     const EVP_MD *digest;
92     unsigned char *out;
93     unsigned char *expected;
94     int keylen, r;
95
96     digest = EVP_get_digestbyname(digestname);
97     if (digest == NULL) {
98         fprintf(stderr, "unknown digest %s\n", digestname);
99         EXIT(5);
100     }
101
102     if ((strlen(hex) % 2) != 0) {
103         fprintf(stderr, "odd hex digest %s %i\n", digestname, i);
104         EXIT(5);
105     }
106     keylen = strlen(hex) / 2;
107     expected = OPENSSL_malloc(keylen + 1);
108     out = OPENSSL_malloc(keylen + 1);
109     if ((expected == NULL) || (out == NULL)) {
110         fprintf(stderr, "malloc() failed\n");
111         EXIT(5);
112     }
113     convert(expected, (const unsigned char *)hex, keylen);
114
115     r = PKCS5_PBKDF2_HMAC(test->pass, test->passlen,
116                           (const unsigned char *)test->salt, test->saltlen,
117                           test->iter, digest, keylen, out);
118
119     if (r == 0) {
120         fprintf(stderr, "PKCS5_PBKDF2_HMAC(%s) failure test %i\n",
121                 digestname, i);
122         EXIT(3);
123     }
124     if (memcmp(expected, out, keylen) != 0) {
125         fprintf(stderr, "Wrong result for PKCS5_PBKDF2_HMAC(%s) test %i\n",
126                 digestname, i);
127         hexdump(stderr, "expected: ", expected, keylen);
128         hexdump(stderr, "result:   ", out, keylen);
129         EXIT(2);
130     }
131     OPENSSL_free(expected);
132     OPENSSL_free(out);
133 }
134
135 int main(int argc, char **argv)
136 {
137     int i;
138     testdata *test = test_cases;
139
140     CRYPTO_set_mem_debug(1);
141     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
142
143     OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN, NULL);
144
145     printf("PKCS5_PBKDF2_HMAC() tests ");
146     for (i = 0; test->pass != NULL; i++, test++) {
147         test_p5_pbkdf2(i, "sha1", test, sha1_results[i]);
148         test_p5_pbkdf2(i, "sha256", test, sha256_results[i]);
149         test_p5_pbkdf2(i, "sha512", test, sha512_results[i]);
150         printf(".");
151     }
152     printf(" done\n");
153
154 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
155     if (CRYPTO_mem_leaks_fp(stderr) <= 0)
156         return 1;
157 # endif
158     return 0;
159 }