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