1*d150a20eSmatthew /* $OpenBSD: pkcs5_pbkdf2_test.c,v 1.1 2012/09/06 20:49:59 matthew Exp $ */
2*d150a20eSmatthew /*-
3*d150a20eSmatthew * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
4*d150a20eSmatthew *
5*d150a20eSmatthew * Permission to use, copy, modify, and distribute this software for any
6*d150a20eSmatthew * purpose with or without fee is hereby granted, provided that the above
7*d150a20eSmatthew * copyright notice and this permission notice appear in all copies.
8*d150a20eSmatthew *
9*d150a20eSmatthew * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*d150a20eSmatthew * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*d150a20eSmatthew * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*d150a20eSmatthew * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*d150a20eSmatthew * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*d150a20eSmatthew * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*d150a20eSmatthew * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*d150a20eSmatthew */
17*d150a20eSmatthew
18*d150a20eSmatthew #include <sys/types.h>
19*d150a20eSmatthew #include <err.h>
20*d150a20eSmatthew #include <stdio.h>
21*d150a20eSmatthew #include <string.h>
22*d150a20eSmatthew #include <util.h>
23*d150a20eSmatthew
24*d150a20eSmatthew struct test_vector {
25*d150a20eSmatthew u_int rounds;
26*d150a20eSmatthew const char *pass;
27*d150a20eSmatthew const char *salt;
28*d150a20eSmatthew const char expected[32];
29*d150a20eSmatthew };
30*d150a20eSmatthew
31*d150a20eSmatthew /*
32*d150a20eSmatthew * Test vectors from RFC 3962
33*d150a20eSmatthew */
34*d150a20eSmatthew struct test_vector test_vectors[] = {
35*d150a20eSmatthew {
36*d150a20eSmatthew 1,
37*d150a20eSmatthew "password",
38*d150a20eSmatthew "ATHENA.MIT.EDUraeburn",
39*d150a20eSmatthew {
40*d150a20eSmatthew 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01,
41*d150a20eSmatthew 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15,
42*d150a20eSmatthew 0x0a, 0xd1, 0xf7, 0xa0, 0x4b, 0xb9, 0xf3, 0xa3,
43*d150a20eSmatthew 0x33, 0xec, 0xc0, 0xe2, 0xe1, 0xf7, 0x08, 0x37
44*d150a20eSmatthew },
45*d150a20eSmatthew }, {
46*d150a20eSmatthew 2,
47*d150a20eSmatthew "password",
48*d150a20eSmatthew "ATHENA.MIT.EDUraeburn",
49*d150a20eSmatthew {
50*d150a20eSmatthew 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e,
51*d150a20eSmatthew 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d,
52*d150a20eSmatthew 0xa0, 0x53, 0x78, 0xb9, 0x32, 0x44, 0xec, 0x8f,
53*d150a20eSmatthew 0x48, 0xa9, 0x9e, 0x61, 0xad, 0x79, 0x9d, 0x86
54*d150a20eSmatthew },
55*d150a20eSmatthew }, {
56*d150a20eSmatthew 1200,
57*d150a20eSmatthew "password",
58*d150a20eSmatthew "ATHENA.MIT.EDUraeburn",
59*d150a20eSmatthew {
60*d150a20eSmatthew 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e,
61*d150a20eSmatthew 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b,
62*d150a20eSmatthew 0xa7, 0xe5, 0x2d, 0xdb, 0xc5, 0xe5, 0x14, 0x2f,
63*d150a20eSmatthew 0x70, 0x8a, 0x31, 0xe2, 0xe6, 0x2b, 0x1e, 0x13
64*d150a20eSmatthew },
65*d150a20eSmatthew }, {
66*d150a20eSmatthew 5,
67*d150a20eSmatthew "password",
68*d150a20eSmatthew "\0224VxxV4\022", /* 0x1234567878563412 */
69*d150a20eSmatthew {
70*d150a20eSmatthew 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6,
71*d150a20eSmatthew 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49,
72*d150a20eSmatthew 0x3f, 0x98, 0xd2, 0x03, 0xe6, 0xbe, 0x49, 0xa6,
73*d150a20eSmatthew 0xad, 0xf4, 0xfa, 0x57, 0x4b, 0x6e, 0x64, 0xee
74*d150a20eSmatthew },
75*d150a20eSmatthew }, {
76*d150a20eSmatthew 1200,
77*d150a20eSmatthew "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
78*d150a20eSmatthew "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
79*d150a20eSmatthew "pass phrase equals block size",
80*d150a20eSmatthew {
81*d150a20eSmatthew 0x13, 0x9c, 0x30, 0xc0, 0x96, 0x6b, 0xc3, 0x2b,
82*d150a20eSmatthew 0xa5, 0x5f, 0xdb, 0xf2, 0x12, 0x53, 0x0a, 0xc9,
83*d150a20eSmatthew 0xc5, 0xec, 0x59, 0xf1, 0xa4, 0x52, 0xf5, 0xcc,
84*d150a20eSmatthew 0x9a, 0xd9, 0x40, 0xfe, 0xa0, 0x59, 0x8e, 0xd1
85*d150a20eSmatthew },
86*d150a20eSmatthew }, {
87*d150a20eSmatthew 1200,
88*d150a20eSmatthew "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
89*d150a20eSmatthew "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
90*d150a20eSmatthew "pass phrase exceeds block size",
91*d150a20eSmatthew {
92*d150a20eSmatthew 0x9c, 0xca, 0xd6, 0xd4, 0x68, 0x77, 0x0c, 0xd5,
93*d150a20eSmatthew 0x1b, 0x10, 0xe6, 0xa6, 0x87, 0x21, 0xbe, 0x61,
94*d150a20eSmatthew 0x1a, 0x8b, 0x4d, 0x28, 0x26, 0x01, 0xdb, 0x3b,
95*d150a20eSmatthew 0x36, 0xbe, 0x92, 0x46, 0x91, 0x5e, 0xc8, 0x2a
96*d150a20eSmatthew },
97*d150a20eSmatthew }, {
98*d150a20eSmatthew 50,
99*d150a20eSmatthew "\360\235\204\236", /* g-clef (0xf09d849e) */
100*d150a20eSmatthew "EXAMPLE.COMpianist",
101*d150a20eSmatthew {
102*d150a20eSmatthew 0x6b, 0x9c, 0xf2, 0x6d, 0x45, 0x45, 0x5a, 0x43,
103*d150a20eSmatthew 0xa5, 0xb8, 0xbb, 0x27, 0x6a, 0x40, 0x3b, 0x39,
104*d150a20eSmatthew 0xe7, 0xfe, 0x37, 0xa0, 0xc4, 0x1e, 0x02, 0xc2,
105*d150a20eSmatthew 0x81, 0xff, 0x30, 0x69, 0xe1, 0xe9, 0x4f, 0x52
106*d150a20eSmatthew },
107*d150a20eSmatthew }
108*d150a20eSmatthew };
109*d150a20eSmatthew #define NVECS (sizeof(test_vectors) / sizeof(*test_vectors))
110*d150a20eSmatthew
111*d150a20eSmatthew static void
printhex(const char * s,const u_int8_t * buf,size_t len)112*d150a20eSmatthew printhex(const char *s, const u_int8_t *buf, size_t len)
113*d150a20eSmatthew {
114*d150a20eSmatthew size_t i;
115*d150a20eSmatthew
116*d150a20eSmatthew printf("%s: ", s);
117*d150a20eSmatthew for (i = 0; i < len; i++)
118*d150a20eSmatthew printf("%02x", buf[i]);
119*d150a20eSmatthew printf("\n");
120*d150a20eSmatthew fflush(stdout);
121*d150a20eSmatthew }
122*d150a20eSmatthew
123*d150a20eSmatthew int
main(int argc,char ** argv)124*d150a20eSmatthew main(int argc, char **argv)
125*d150a20eSmatthew {
126*d150a20eSmatthew u_int i, j;
127*d150a20eSmatthew u_char result[32];
128*d150a20eSmatthew struct test_vector *vec;
129*d150a20eSmatthew
130*d150a20eSmatthew for (i = 0; i < NVECS; i++) {
131*d150a20eSmatthew vec = &test_vectors[i];
132*d150a20eSmatthew for (j = 2; j <= sizeof(result); j += 3) {
133*d150a20eSmatthew if (pkcs5_pbkdf2(vec->pass, strlen(vec->pass),
134*d150a20eSmatthew vec->salt, strlen(vec->salt),
135*d150a20eSmatthew result, j, vec->rounds) != 0)
136*d150a20eSmatthew errx(1, "pbkdf2 vector %u failed", i);
137*d150a20eSmatthew if (memcmp(result, vec->expected, j) != 0) {
138*d150a20eSmatthew printhex(" got", result, j);
139*d150a20eSmatthew printhex("want", vec->expected, j);
140*d150a20eSmatthew return 1;
141*d150a20eSmatthew }
142*d150a20eSmatthew }
143*d150a20eSmatthew }
144*d150a20eSmatthew return 0;
145*d150a20eSmatthew }
146