Skip to content

Commit 6db8219

Browse files
ThePassionatexiaoxiang781216
authored andcommitted
testing/crypto: add crc32 testcases
Signed-off-by: makejian <[email protected]>
1 parent 524caec commit 6db8219

File tree

4 files changed

+321
-0
lines changed

4 files changed

+321
-0
lines changed

testing/crypto/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,18 @@ if(CONFIG_TESTING_CRYPTO)
103103
hash.c)
104104
endif()
105105

106+
if(CONFIG_TESTING_CRYPTO_CRC32)
107+
nuttx_add_application(
108+
NAME
109+
crc32
110+
PRIORITY
111+
${CONFIG_TESTING_CRYPTO_PRIORITY}
112+
STACKSIZE
113+
${CONFIG_TESTING_CRYPTO_STACKSIZE}
114+
MODULE
115+
${CONFIG_TESTING_CRYPTO}
116+
SRCS
117+
crc32.c)
118+
endif()
119+
106120
endif()

testing/crypto/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ config TESTING_CRYPTO_HASH
3030
bool "hash crypto test"
3131
default n
3232

33+
config TESTING_CRYPTO_CRC32
34+
bool "crc32 crypto test"
35+
default n
36+
3337
config TESTING_CRYPTO_PRIORITY
3438
int "crypto test task priority"
3539
default 100

testing/crypto/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ PROGNAME += hash
5151
MAINSRC += hash.c
5252
endif
5353

54+
ifeq ($(CONFIG_TESTING_CRYPTO_CRC32),y)
55+
PROGNAME += crc32
56+
MAINSRC += crc32.c
57+
endif
58+
5459
PRIORITY = $(CONFIG_TESTING_CRYPTO_PRIORITY)
5560
STACKSIZE = $(CONFIG_TESTING_CRYPTO_STACKSIZE)
5661
MODULE = $(CONFIG_TESTING_CRYPTO)

testing/crypto/crc32.c

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
/****************************************************************************
2+
* apps/testing/crypto/crc32.c
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
****************************************************************************/
16+
17+
/****************************************************************************
18+
* Included Files
19+
****************************************************************************/
20+
21+
#include <err.h>
22+
#include <stdint.h>
23+
#include <stdio.h>
24+
#include <fcntl.h>
25+
#include <stdlib.h>
26+
#include <unistd.h>
27+
#include <sys/ioctl.h>
28+
#include <sys/param.h>
29+
#include <crypto/cryptodev.h>
30+
31+
typedef struct crypto_context
32+
{
33+
int fd;
34+
int crypto_fd;
35+
struct session_op session;
36+
struct crypt_op cryp;
37+
}
38+
crypto_context;
39+
40+
typedef struct tb
41+
{
42+
FAR char *data;
43+
int datalen;
44+
uint32_t result;
45+
}
46+
tb;
47+
48+
static tb g_crc32_testcase[] =
49+
{
50+
/* testcase 1-7: Individual testing */
51+
52+
{
53+
"",
54+
0,
55+
0,
56+
},
57+
{
58+
"a",
59+
1,
60+
0xe8b7be43,
61+
},
62+
{
63+
"abc",
64+
3,
65+
0x352441c2,
66+
},
67+
{
68+
"message digest",
69+
14,
70+
0x20159d7f,
71+
},
72+
{
73+
"abcdefghijklmnopqrstuvwxyz",
74+
26,
75+
0x4c2750bd,
76+
},
77+
{
78+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
79+
62,
80+
0x1fc2e6d2,
81+
},
82+
{
83+
"123456789012345678901234567890123456789"
84+
"01234567890123456789012345678901234567890",
85+
80,
86+
0x7ca94a72,
87+
},
88+
89+
/* testcase 8: test case 7 is divided into 8 parts */
90+
91+
{
92+
"1234567890",
93+
10,
94+
0x7ca94a72,
95+
}
96+
};
97+
98+
static void syscrc32_free(FAR crypto_context *ctx)
99+
{
100+
if (ctx->crypto_fd != 0)
101+
{
102+
close(ctx->crypto_fd);
103+
ctx->crypto_fd = 0;
104+
}
105+
106+
if (ctx->fd != 0)
107+
{
108+
close(ctx->fd);
109+
ctx->fd = 0;
110+
}
111+
}
112+
113+
static int syscrc32_init(FAR crypto_context *ctx)
114+
{
115+
memset(ctx, 0, sizeof(crypto_context));
116+
if ((ctx->fd = open("/dev/crypto", O_RDWR, 0)) < 0)
117+
{
118+
warn("CRIOGET");
119+
syscrc32_free(ctx);
120+
return 1;
121+
}
122+
123+
if (ioctl(ctx->fd, CRIOGET, &ctx->crypto_fd) == -1)
124+
{
125+
warn("CRIOGET");
126+
syscrc32_free(ctx);
127+
return 1;
128+
}
129+
130+
return 0;
131+
}
132+
133+
static int syscrc32_start(FAR crypto_context *ctx, uint32_t *key)
134+
{
135+
ctx->session.mac = CRYPTO_CRC32;
136+
ctx->session.mackey = (caddr_t) key;
137+
ctx->session.mackeylen = sizeof(uint32_t);
138+
if (ioctl(ctx->crypto_fd, CIOCGSESSION, &ctx->session) == -1)
139+
{
140+
warn("CIOCGSESSION");
141+
syscrc32_free(ctx);
142+
return 1;
143+
}
144+
145+
ctx->cryp.ses = ctx->session.ses;
146+
return 0;
147+
}
148+
149+
static int syscrc32_update(FAR crypto_context *ctx, FAR const char *s,
150+
size_t len)
151+
{
152+
ctx->cryp.op = COP_ENCRYPT;
153+
ctx->cryp.flags |= COP_FLAG_UPDATE;
154+
ctx->cryp.src = (caddr_t) s;
155+
ctx->cryp.len = len;
156+
if (ioctl(ctx->crypto_fd, CIOCCRYPT, &ctx->cryp) == -1)
157+
{
158+
warn("CIOCCRYPT");
159+
return 1;
160+
}
161+
162+
return 0;
163+
}
164+
165+
static int syscrc32_finish(FAR crypto_context *ctx, FAR uint32_t *out)
166+
{
167+
ctx->cryp.flags = 0;
168+
ctx->cryp.mac = (caddr_t) out;
169+
170+
if (ioctl(ctx->crypto_fd, CIOCCRYPT, &ctx->cryp) == -1)
171+
{
172+
warn("CIOCCRYPT");
173+
return 1;
174+
}
175+
176+
if (ioctl(ctx->crypto_fd, CIOCFSESSION, &ctx->session.ses) == -1)
177+
{
178+
warn("CIOCFSESSION");
179+
return 1;
180+
};
181+
182+
return 0;
183+
}
184+
185+
static int match(const uint32_t a, const uint32_t b)
186+
{
187+
if (a == b)
188+
{
189+
return 0;
190+
}
191+
192+
warnx("crc32 mismatch");
193+
194+
printf("%02lx", a);
195+
printf("%02lx", b);
196+
printf("\n");
197+
198+
return 1;
199+
}
200+
201+
/****************************************************************************
202+
* Public Functions
203+
****************************************************************************/
204+
205+
int main(void)
206+
{
207+
crypto_context crc32_ctx;
208+
uint32_t output;
209+
uint32_t startval = 0;
210+
int ret = 0;
211+
int i;
212+
213+
ret = syscrc32_init(&crc32_ctx);
214+
if (ret != 0)
215+
{
216+
printf("syscrc32 init failed\n");
217+
}
218+
219+
/* testcase 1-7: test crc32 vector */
220+
221+
for (i = 0; i < sizeof(g_crc32_testcase) / sizeof(tb) - 1; i++)
222+
{
223+
ret = syscrc32_start(&crc32_ctx, &startval);
224+
if (ret != 0)
225+
{
226+
printf("syscrc32 start failed\n");
227+
goto err;
228+
}
229+
230+
ret = syscrc32_update(&crc32_ctx, g_crc32_testcase[i].data,
231+
g_crc32_testcase[i].datalen);
232+
if (ret)
233+
{
234+
printf("syscrc32 update failed\n");
235+
goto err;
236+
}
237+
238+
ret = syscrc32_finish(&crc32_ctx, &output);
239+
if (ret)
240+
{
241+
printf("syscrc32 finish failed\n");
242+
goto err;
243+
}
244+
245+
ret = match(g_crc32_testcase[i].result, output);
246+
if (ret)
247+
{
248+
printf("match crc32 test case %d failed\n", i + 1);
249+
goto err;
250+
}
251+
else
252+
{
253+
printf("crc32 test case %d success\n", i + 1);
254+
}
255+
}
256+
257+
/* testcase 8: test segmented computing capabilities in crc32 mode */
258+
259+
for (i = 0; i < 8; i++)
260+
{
261+
ret = syscrc32_start(&crc32_ctx, &startval);
262+
if (ret != 0)
263+
{
264+
printf("syscrc32 start failed\n");
265+
goto err;
266+
}
267+
268+
ret = syscrc32_update(&crc32_ctx, g_crc32_testcase[7].data,
269+
g_crc32_testcase[7].datalen);
270+
if (ret)
271+
{
272+
printf("syscrc32 update failed\n");
273+
goto err;
274+
}
275+
276+
ret = syscrc32_finish(&crc32_ctx, &startval);
277+
if (ret)
278+
{
279+
printf("syscrc32 finish failed\n");
280+
goto err;
281+
}
282+
}
283+
284+
ret = match(g_crc32_testcase[7].result, startval);
285+
if (ret)
286+
{
287+
printf("match crc32 testcase 8 failed\n");
288+
goto err;
289+
}
290+
else
291+
{
292+
printf("crc32 test case 8 success\n");
293+
}
294+
295+
err:
296+
syscrc32_free(&crc32_ctx);
297+
return 0;
298+
}

0 commit comments

Comments
 (0)