xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/test_pkcs5.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: test_pkcs5.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2006 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include <config.h>
37b9d004c6Schristos #include <krb5/roken.h>
38ca1c9b0cSelric #include <err.h>
39ca1c9b0cSelric 
40ca1c9b0cSelric #include <evp.h>
41ca1c9b0cSelric 
42ca1c9b0cSelric struct tests {
43ca1c9b0cSelric     const char *password;
44ca1c9b0cSelric     const char *salt;
45ca1c9b0cSelric     int iterations;
46ca1c9b0cSelric     const void *pbkdf2_128;
47ca1c9b0cSelric     const void *pbkdf2_256;
48ca1c9b0cSelric };
49ca1c9b0cSelric 
50ca1c9b0cSelric 
51ca1c9b0cSelric const struct tests pkcs5_tests[] = {
52ca1c9b0cSelric     { "password",
53ca1c9b0cSelric       "ATHENA.MIT.EDUraeburn",
54ca1c9b0cSelric       1,
55ca1c9b0cSelric       "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01\x56\x5a\x11\x22\xb2\x56\x35\x15",
56ca1c9b0cSelric       "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01\x56\x5a\x11\x22\xb2\x56\x35\x15"
57ca1c9b0cSelric       "\x0a\xd1\xf7\xa0\x4b\xb9\xf3\xa3\x33\xec\xc0\xe2\xe1\xf7\x08\x37"
58ca1c9b0cSelric     },
59ca1c9b0cSelric     { "password",
60ca1c9b0cSelric       "ATHENA.MIT.EDUraeburn",
61ca1c9b0cSelric       2,
62ca1c9b0cSelric       "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d",
63ca1c9b0cSelric       "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d"
64ca1c9b0cSelric       "\xa0\x53\x78\xb9\x32\x44\xec\x8f\x48\xa9\x9e\x61\xad\x79\x9d\x86"
65ca1c9b0cSelric     },
66ca1c9b0cSelric     { "password",
67ca1c9b0cSelric       "ATHENA.MIT.EDUraeburn",
68ca1c9b0cSelric       1200,
69ca1c9b0cSelric       "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b",
70ca1c9b0cSelric       "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"
71ca1c9b0cSelric       "\xa7\xe5\x2d\xdb\xc5\xe5\x14\x2f\x70\x8a\x31\xe2\xe6\x2b\x1e\x13"
72ca1c9b0cSelric     },
73ca1c9b0cSelric     {
74ca1c9b0cSelric 	"password", "\x12\x34\x56\x78\x78\x56\x34\x12",
75ca1c9b0cSelric 	5,
76ca1c9b0cSelric 	"\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49",
77ca1c9b0cSelric 	"\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"
78ca1c9b0cSelric 	"\x3f\x98\xd2\x03\xe6\xbe\x49\xa6\xad\xf4\xfa\x57\x4b\x6e\x64\xee"
79ca1c9b0cSelric     },
80ca1c9b0cSelric     {
81ca1c9b0cSelric 	"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
82ca1c9b0cSelric 	"pass phrase equals block size",
83ca1c9b0cSelric 	1200,
84ca1c9b0cSelric 	"\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9",
85ca1c9b0cSelric 	"\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"
86ca1c9b0cSelric 	"\xc5\xec\x59\xf1\xa4\x52\xf5\xcc\x9a\xd9\x40\xfe\xa0\x59\x8e\xd1"
87ca1c9b0cSelric     },
88ca1c9b0cSelric     {
89ca1c9b0cSelric 	"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
90ca1c9b0cSelric 	"pass phrase exceeds block size",
91ca1c9b0cSelric 	1200,
92ca1c9b0cSelric 	"\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61",
93ca1c9b0cSelric 	"\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61"
94ca1c9b0cSelric 	"\x1a\x8b\x4d\x28\x26\x01\xdb\x3b\x36\xbe\x92\x46\x91\x5e\xc8\x2a"
95ca1c9b0cSelric     },
96ca1c9b0cSelric     {
97ca1c9b0cSelric 	"\xf0\x9d\x84\x9e" /* g-clef */,
98ca1c9b0cSelric 	"EXAMPLE.COMpianist",
99ca1c9b0cSelric 	50,
100ca1c9b0cSelric 	"\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39",
101ca1c9b0cSelric 	"\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"
102ca1c9b0cSelric 	"\xe7\xfe\x37\xa0\xc4\x1e\x02\xc2\x81\xff\x30\x69\xe1\xe9\x4f\x52"
103ca1c9b0cSelric     }
104ca1c9b0cSelric };
105ca1c9b0cSelric 
106ca1c9b0cSelric static int
test_pkcs5_pbe2(const struct tests * t)107ca1c9b0cSelric test_pkcs5_pbe2(const struct tests *t)
108ca1c9b0cSelric {
109ca1c9b0cSelric     unsigned char key[32];
110ca1c9b0cSelric     int ret, error = 0;
111ca1c9b0cSelric 
112b9d004c6Schristos     ret = PKCS5_PBKDF2_HMAC(t->password, strlen(t->password),
113ca1c9b0cSelric 			    t->salt, strlen(t->salt),
114b9d004c6Schristos 			    t->iterations, EVP_sha1(),
115ca1c9b0cSelric 			    16, key);
116ca1c9b0cSelric     if (ret != 1)
117b9d004c6Schristos 	errx(1, "PKCS5_PBKDF2_HMAC: %d", ret);
118ca1c9b0cSelric 
119ca1c9b0cSelric     if (memcmp(t->pbkdf2_128, key, 16) != 0) {
120ca1c9b0cSelric 	printf("incorrect 128 key\n");
121ca1c9b0cSelric 	error++;
122ca1c9b0cSelric     }
123ca1c9b0cSelric 
124b9d004c6Schristos     ret = PKCS5_PBKDF2_HMAC(t->password, strlen(t->password),
125ca1c9b0cSelric 			    t->salt, strlen(t->salt),
126b9d004c6Schristos 			    t->iterations, EVP_sha1(),
127ca1c9b0cSelric 			    32, key);
128ca1c9b0cSelric     if (ret != 1)
129b9d004c6Schristos 	errx(1, "PKCS5_PBKDF2_HMAC: %d", ret);
130ca1c9b0cSelric 
131ca1c9b0cSelric     if (memcmp(t->pbkdf2_256, key, 32) != 0) {
132ca1c9b0cSelric 	printf("incorrect 256 key\n");
133ca1c9b0cSelric 	error++;
134ca1c9b0cSelric     }
135ca1c9b0cSelric 
136ca1c9b0cSelric     return error;
137ca1c9b0cSelric }
138ca1c9b0cSelric 
139ca1c9b0cSelric int
main(int argc,char ** argv)140ca1c9b0cSelric main(int argc, char **argv)
141ca1c9b0cSelric {
142ca1c9b0cSelric     int ret = 0;
143ca1c9b0cSelric     int i;
144ca1c9b0cSelric 
145ca1c9b0cSelric     for (i = 0; i < sizeof(pkcs5_tests)/sizeof(pkcs5_tests[0]); i++)
146ca1c9b0cSelric 	ret += test_pkcs5_pbe2(&pkcs5_tests[i]);
147ca1c9b0cSelric 
148ca1c9b0cSelric     return ret;
149ca1c9b0cSelric }
150