Skip to content

Commit cd2b0c0

Browse files
PruteanuVladxiaoxiang781216
authored andcommitted
testing/crypto: Add pbkdf2 test app
This adds support for testing PBKDF2 implementation. Test vectors for SHA1 are taken from RFC6070. SHA256 vectors were extrapolated using an online PBKDF2 generator which was checked against RFC6070. Signed-off-by: Vlad Pruteanu <pruteanuvlad1611@yahoo.com>
1 parent e564816 commit cd2b0c0

4 files changed

Lines changed: 301 additions & 0 deletions

File tree

testing/drivers/crypto/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,18 @@ if(CONFIG_TESTING_CRYPTO)
175175
rsa.c)
176176
endif()
177177

178+
if(CONFIG_TESTING_CRYPTO_PBKDF2)
179+
nuttx_add_application(
180+
NAME
181+
pbkdf2
182+
PRIORITY
183+
${CONFIG_TESTING_CRYPTO_PRIORITY}
184+
STACKSIZE
185+
${CONFIG_TESTING_CRYPTO_STACKSIZE}
186+
MODULE
187+
${CONFIG_TESTING_CRYPTO}
188+
SRCS
189+
pbkdf2.c)
190+
endif()
191+
178192
endif()

testing/drivers/crypto/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ config TESTING_CRYPTO_RSA
7474
bool "rsa crypto test"
7575
default n
7676

77+
config TESTING_CRYPTO_PBKDF2
78+
bool "pbkdf2 crypto test"
79+
default n
80+
7781
config TESTING_CRYPTO_PRIORITY
7882
int "crypto test task priority"
7983
default 100

testing/drivers/crypto/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ PROGNAME += rsa
7878
MAINSRC += rsa.c
7979
endif
8080

81+
ifeq ($(CONFIG_TESTING_CRYPTO_PBKDF2),y)
82+
PROGNAME += pbkdf2
83+
MAINSRC += pbkdf2.c
84+
endif
85+
8186
PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
8287
STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
8388
MODULE = $(CONFIG_TESTING_CRYPTO)

testing/drivers/crypto/pbkdf2.c

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
/****************************************************************************
2+
* apps/testing/drivers/crypto/pbkdf2.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
****************************************************************************/
21+
22+
/****************************************************************************
23+
* Included Files
24+
****************************************************************************/
25+
26+
#include <err.h>
27+
#include <stdio.h>
28+
#include <fcntl.h>
29+
#include <string.h>
30+
#include <unistd.h>
31+
#include <sys/ioctl.h>
32+
#include <sys/param.h>
33+
#include <crypto/cryptodev.h>
34+
35+
struct tb
36+
{
37+
FAR char *key;
38+
int keylen;
39+
FAR char *data;
40+
int datalen;
41+
int iterations;
42+
int dklen;
43+
}
44+
pbkdf2_testcases[] =
45+
{
46+
{
47+
"password",
48+
8,
49+
"salt",
50+
4,
51+
1,
52+
20,
53+
},
54+
{
55+
"password",
56+
8,
57+
"salt",
58+
4,
59+
2,
60+
20,
61+
},
62+
{
63+
"password",
64+
8,
65+
"salt",
66+
4,
67+
4096,
68+
20,
69+
},
70+
{
71+
"password",
72+
8,
73+
"salt",
74+
4,
75+
16777216,
76+
20,
77+
},
78+
{
79+
"passwordPASSWORDpassword",
80+
24,
81+
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
82+
36,
83+
4096,
84+
25,
85+
},
86+
{
87+
"pass\0word",
88+
9,
89+
"sa\0lt",
90+
5,
91+
4096,
92+
16,
93+
},
94+
};
95+
96+
FAR char *pbkdf2_sha1_result[] =
97+
{
98+
"\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06"
99+
"\x2f\xe0\x37\xa6",
100+
"\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0"
101+
"\xd8\xde\x89\x57",
102+
"\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0"
103+
"\x65\xa4\x29\xc1",
104+
"\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94\x5b\x3d\x6b\xa2\x15\x8c"
105+
"\x26\x34\xe9\x84",
106+
"\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8\xd8\x36\x62\xc0\xe4\x4a"
107+
"\x8b\x29\x1a\x96\x4c\xf2\xf0\x70\x38",
108+
"\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37\xd7\xf0\x34\x25\xe0\xc3",
109+
};
110+
111+
FAR char *pbkdf2_sha256_result[] =
112+
{
113+
"\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52\x56\xc4\xf8\x37"
114+
"\xa8\x65\x48\xc9",
115+
"\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9\x28\xf0\x6d\xd0"
116+
"\x2a\x30\x3f\x8e",
117+
"\xc5\xe4\x78\xd5\x92\x88\xc8\x41\xaa\x53\x0d\xb6\x84\x5c\x4c\x8d"
118+
"\x96\x28\x93\xa0",
119+
"\xcf\x81\xc6\x6f\xe8\xcf\xc0\x4d\x1f\x31\xec\xb6\x5d\xab\x40\x89"
120+
"\xf7\xf1\x79\xe8",
121+
"\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8\x11\x6e\x84\xcf"
122+
"\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c",
123+
"\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26\x65\x0a\x86\x87",
124+
};
125+
126+
int syspbkdf2(int mac, FAR const char *key, size_t keylen,
127+
FAR const char *s, size_t len, int iterations,
128+
size_t dklen, FAR char *out)
129+
{
130+
struct session_op session;
131+
struct crypt_op cryp;
132+
int cryptodev_fd = -1;
133+
int fd = -1;
134+
135+
if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0)
136+
{
137+
warn("/dev/crypto");
138+
goto err;
139+
}
140+
141+
if (ioctl(fd, CRIOGET, &cryptodev_fd) == -1)
142+
{
143+
warn("CRIOGET");
144+
goto err;
145+
}
146+
147+
memset(&session, 0, sizeof(session));
148+
session.cipher = 0;
149+
session.mac = mac;
150+
session.mackey = (caddr_t)key;
151+
session.mackeylen = keylen;
152+
if (ioctl(cryptodev_fd, CIOCGSESSION, &session) == -1)
153+
{
154+
warn("CIOCGSESSION");
155+
goto err;
156+
}
157+
memset(&cryp, 0, sizeof(cryp));
158+
cryp.ses = session.ses;
159+
cryp.flags = 0;
160+
cryp.src = (caddr_t)s;
161+
cryp.len = len;
162+
cryp.dst = 0;
163+
cryp.mac = (caddr_t) out;
164+
cryp.iv = 0;
165+
cryp.iterations = iterations;
166+
cryp.olen = dklen;
167+
168+
if (ioctl(cryptodev_fd, CIOCCRYPT, &cryp) == -1)
169+
{
170+
warn("CIOCCRYPT");
171+
goto err;
172+
}
173+
174+
close(cryptodev_fd);
175+
close(fd);
176+
177+
return 0;
178+
err:
179+
if (cryptodev_fd != -1)
180+
{
181+
close(cryptodev_fd);
182+
}
183+
184+
if (fd != -1)
185+
{
186+
close(fd);
187+
}
188+
189+
return 1;
190+
}
191+
192+
static int match(unsigned char *a, unsigned char *b, size_t len)
193+
{
194+
int i;
195+
196+
if (memcmp(a, b, len) == 0)
197+
return (0);
198+
199+
warnx("pbkdf2 mismatch\n");
200+
201+
for (i = 0; i < len; i++)
202+
{
203+
printf("%02x", a[i]);
204+
}
205+
206+
printf("\n");
207+
for (i = 0; i < len; i++)
208+
{
209+
printf("%02x", b[i]);
210+
}
211+
212+
printf("\n");
213+
214+
return (1);
215+
}
216+
217+
/****************************************************************************
218+
* Public Functions
219+
****************************************************************************/
220+
221+
int main(void)
222+
{
223+
char output[40];
224+
int ret = 0;
225+
for (int i = 0; i < 6; i++)
226+
{
227+
ret = syspbkdf2(CRYPTO_PBKDF2_HMAC_SHA1, pbkdf2_testcases[i].key,
228+
pbkdf2_testcases[i].keylen,
229+
pbkdf2_testcases[i].data,
230+
pbkdf2_testcases[i].datalen,
231+
pbkdf2_testcases[i].iterations,
232+
pbkdf2_testcases[i].dklen, output);
233+
if (ret)
234+
{
235+
printf("PBKDF2 SHA1 failed\n");
236+
}
237+
238+
ret += match((unsigned char *)pbkdf2_sha1_result[i],
239+
(unsigned char *)output,
240+
pbkdf2_testcases[i].dklen);
241+
if (ret)
242+
{
243+
printf("match PBKDF2 SHA1 failed\n");
244+
}
245+
else
246+
{
247+
printf("hmac PBKDF2 SHA1 success\n");
248+
}
249+
}
250+
251+
for (int i = 0; i < 6; i++)
252+
{
253+
ret = syspbkdf2(CRYPTO_PBKDF2_HMAC_SHA256, pbkdf2_testcases[i].key,
254+
pbkdf2_testcases[i].keylen,
255+
pbkdf2_testcases[i].data,
256+
pbkdf2_testcases[i].datalen,
257+
pbkdf2_testcases[i].iterations,
258+
pbkdf2_testcases[i].dklen, output);
259+
if (ret)
260+
{
261+
printf("PBKDF2 SHA256 failed\n");
262+
}
263+
264+
ret += match((unsigned char *)pbkdf2_sha256_result[i],
265+
(unsigned char *)output,
266+
pbkdf2_testcases[i].dklen);
267+
if (ret)
268+
{
269+
printf("match PBKDF2 SHA256 failed\n");
270+
}
271+
else
272+
{
273+
printf("hmac PBKDF2 SHA256 success\n");
274+
}
275+
}
276+
277+
return 0;
278+
}

0 commit comments

Comments
 (0)