Skip to content

Commit d983766

Browse files
committed
testing/crypto: cover reflect in/out in the crc tests
Add the reflected CRC parameter cases to the crc8 and crc16 tests, and extend the crc32 test with parameterized keys: the full parameter block {polynomial, initial, xorout, reflect_in, reflect_out} plus a three-word {polynomial, initial, xorout} CRC-32/MPEG-2 case. The legacy single-word crc32 key cases stay unchanged. Signed-off-by: makejian <makejian@xiaomi.com>
1 parent 989cc5f commit d983766

3 files changed

Lines changed: 254 additions & 7 deletions

File tree

‎testing/drivers/crypto/crc16.c‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,24 @@ static const tb g_crc16_testcases[] =
111111

112112
static const crc16_params g_crc16_testparams[] =
113113
{
114+
/* CRC-16/CCITT */
115+
116+
{
117+
0x1021, 0x0000, 0x0000, true, true,
118+
},
119+
114120
/* CRC-16/CCITT-FALSE */
115121

116122
{
117123
0x1021, 0xffff, 0x0000, false, false,
118124
},
119125

126+
/* CRC-16/IBM */
127+
128+
{
129+
0x8005, 0x0000, 0x0000, true, true,
130+
},
131+
120132
/* CRC-16/XMODEM */
121133

122134
{
@@ -126,13 +138,27 @@ static const crc16_params g_crc16_testparams[] =
126138

127139
static const uint16_t g_crc16_testresults[][8] =
128140
{
141+
/* CRC-16/CCITT */
142+
143+
{
144+
0x0000, 0x728f, 0x58e9, 0x7bd5,
145+
0x80b0, 0xd7d5, 0x4375, 0x4375,
146+
},
147+
129148
/* CRC-16/CCITT-FALSE */
130149

131150
{
132151
0xffff, 0x9d77, 0x514a, 0x32cc,
133152
0x53e2, 0xb46b, 0xdadf, 0xdadf,
134153
},
135154

155+
/* CRC-16/IBM */
156+
157+
{
158+
0x0000, 0xe8c1, 0x9738, 0x3b44,
159+
0x9c1d, 0x57b4, 0x5ec7, 0x5ec7,
160+
},
161+
136162
/* CRC-16/XMODEM */
137163

138164
{
@@ -324,7 +350,7 @@ int main(void)
324350

325351
setbuf(stdout, NULL);
326352

327-
for (i = 0; i < 2; i++)
353+
for (i = 0; i < 4; i++)
328354
{
329355
fails += syscrc16_case(&g_crc16_testparams[i],
330356
g_crc16_testresults[i], i + 1);

‎testing/drivers/crypto/crc32.c‎

Lines changed: 187 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
****************************************************************************/
2222

2323
#include <err.h>
24+
#include <stdbool.h>
2425
#include <stdint.h>
2526
#include <stdio.h>
2627
#include <fcntl.h>
@@ -47,6 +48,18 @@ typedef struct tb
4748
}
4849
tb;
4950

51+
/* Session key of the crc32 xform: the parameter block itself */
52+
53+
typedef struct crc32_params
54+
{
55+
uint32_t polynomial;
56+
uint32_t initial;
57+
uint32_t xorout;
58+
bool reflect_in;
59+
bool reflect_out;
60+
}
61+
crc32_params;
62+
5063
static tb g_crc32_testcase[] =
5164
{
5265
/* testcase 1-7: Individual testing */
@@ -97,6 +110,79 @@ static tb g_crc32_testcase[] =
97110
}
98111
};
99112

113+
/* Parameter cases: the session key carries the parameter block */
114+
115+
static const crc32_params g_crc32_testparams[] =
116+
{
117+
/* polynomial 0xedb88320, reflected input */
118+
119+
{
120+
0xedb88320, 0x00000000, 0x00000000, true, false,
121+
},
122+
123+
/* CRC-32/IEEE-802.3 */
124+
125+
{
126+
0x04c11db7, 0xffffffff, 0xffffffff, true, true,
127+
},
128+
129+
/* polynomial 0xf4acfb13, reflected input and output */
130+
131+
{
132+
0xf4acfb13, 0xffffffff, 0xffffffff, true, true,
133+
},
134+
135+
/* CRC-32C */
136+
137+
{
138+
0x1edc6f41, 0xffffffff, 0xffffffff, true, true,
139+
},
140+
};
141+
142+
static const uint32_t g_crc32_testresults[][8] =
143+
{
144+
/* polynomial 0xedb88320, reflected input */
145+
146+
{
147+
0x00000000, 0xb2364bc0, 0x75ce58c0, 0xbb741ae0,
148+
0xa24cfcc0, 0x32344b80, 0x121cfd80, 0x121cfd80,
149+
},
150+
151+
/* CRC-32/IEEE-802.3 */
152+
153+
{
154+
0x00000000, 0xe8b7be43, 0x352441c2, 0x20159d7f,
155+
0x4c2750bd, 0x1fc2e6d2, 0x7ca94a72, 0x7ca94a72,
156+
},
157+
158+
/* polynomial 0xf4acfb13, reflected input and output */
159+
160+
{
161+
0x00000000, 0xaa7b2b08, 0xebe963c8, 0x572300c2,
162+
0x9bed5706, 0xadd7278e, 0x226aba31, 0x226aba31,
163+
},
164+
165+
/* CRC-32C */
166+
167+
{
168+
0x00000000, 0xc1d04330, 0x364b3fb7, 0x02bd79d0,
169+
0x9ee6ef25, 0xa245d57d, 0x477a6781, 0x477a6781,
170+
},
171+
};
172+
173+
/* CRC-32/MPEG-2 via the three-word {polynomial, initial, xorout} key */
174+
175+
static const uint32_t g_crc32_mpeg2_key[3] =
176+
{
177+
0x04c11db7, 0xffffffff, 0x00000000
178+
};
179+
180+
static const uint32_t g_crc32_mpeg2_results[8] =
181+
{
182+
0xffffffff, 0xe66c6494, 0x9b73448c, 0x4036fca8,
183+
0x88406c69, 0x5f8bde77, 0xe9f78669, 0xe9f78669,
184+
};
185+
100186
static void syscrc32_free(FAR crypto_context *ctx)
101187
{
102188
if (ctx->crypto_fd != 0)
@@ -132,11 +218,12 @@ static int syscrc32_init(FAR crypto_context *ctx)
132218
return 0;
133219
}
134220

135-
static int syscrc32_start(FAR crypto_context *ctx, uint32_t *key)
221+
static int syscrc32_start(FAR crypto_context *ctx, FAR const void *key,
222+
int keylen)
136223
{
137224
ctx->session.mac = CRYPTO_CRC32;
138225
ctx->session.mackey = (caddr_t) key;
139-
ctx->session.mackeylen = sizeof(uint32_t);
226+
ctx->session.mackeylen = keylen;
140227
if (ioctl(ctx->crypto_fd, CIOCGSESSION, &ctx->session) == -1)
141228
{
142229
warn("CIOCGSESSION");
@@ -200,6 +287,86 @@ static int match(const uint32_t a, const uint32_t b)
200287
return 1;
201288
}
202289

290+
static int syscrc32_case(FAR const void *key, int keylen,
291+
FAR const uint32_t *result, int number)
292+
{
293+
crypto_context ctx;
294+
uint32_t output;
295+
int i;
296+
297+
if (syscrc32_init(&ctx) != 0)
298+
{
299+
printf("crc32 parameter case %d: init failed\n", number);
300+
return 1;
301+
}
302+
303+
/* testcase 1-7: test crc32 vector */
304+
305+
for (i = 0; i < (int)(sizeof(g_crc32_testcase) / sizeof(tb)) - 1; i++)
306+
{
307+
if (syscrc32_start(&ctx, key, keylen) != 0 ||
308+
syscrc32_update(&ctx, g_crc32_testcase[i].data,
309+
g_crc32_testcase[i].datalen) != 0 ||
310+
syscrc32_finish(&ctx, &output) != 0)
311+
{
312+
printf("crc32 parameter case %d: io failed\n", number);
313+
syscrc32_free(&ctx);
314+
return 1;
315+
}
316+
317+
if (output != result[i])
318+
{
319+
printf("crc32 parameter case %d test case %d failed: "
320+
"%08x != %08x\n", number, i + 1, output, result[i]);
321+
syscrc32_free(&ctx);
322+
return 1;
323+
}
324+
325+
printf("crc32 parameter case %d test case %d success\n",
326+
number, i + 1);
327+
}
328+
329+
/* testcase 8: test segmented computing capabilities in crc32 mode */
330+
331+
if (syscrc32_start(&ctx, key, keylen) != 0)
332+
{
333+
printf("crc32 parameter case %d: io failed\n", number);
334+
syscrc32_free(&ctx);
335+
return 1;
336+
}
337+
338+
for (i = 0; i < 8; i++)
339+
{
340+
if (syscrc32_update(&ctx, g_crc32_testcase[7].data,
341+
g_crc32_testcase[7].datalen) != 0)
342+
{
343+
printf("crc32 parameter case %d: io failed\n", number);
344+
syscrc32_free(&ctx);
345+
return 1;
346+
}
347+
}
348+
349+
if (syscrc32_finish(&ctx, &output) != 0)
350+
{
351+
printf("crc32 parameter case %d: io failed\n", number);
352+
syscrc32_free(&ctx);
353+
return 1;
354+
}
355+
356+
if (output != result[7])
357+
{
358+
printf("crc32 parameter case %d test case 8 failed: "
359+
"%08x != %08x\n", number, output, result[7]);
360+
syscrc32_free(&ctx);
361+
return 1;
362+
}
363+
364+
printf("crc32 parameter case %d test case 8 success\n", number);
365+
366+
syscrc32_free(&ctx);
367+
return 0;
368+
}
369+
203370
/****************************************************************************
204371
* Public Functions
205372
****************************************************************************/
@@ -212,6 +379,8 @@ int main(void)
212379
int ret = 0;
213380
int i;
214381

382+
setbuf(stdout, NULL);
383+
215384
ret = syscrc32_init(&crc32_ctx);
216385
if (ret != 0)
217386
{
@@ -222,7 +391,7 @@ int main(void)
222391

223392
for (i = 0; i < sizeof(g_crc32_testcase) / sizeof(tb) - 1; i++)
224393
{
225-
ret = syscrc32_start(&crc32_ctx, &startval);
394+
ret = syscrc32_start(&crc32_ctx, &startval, sizeof(uint32_t));
226395
if (ret != 0)
227396
{
228397
printf("syscrc32 start failed\n");
@@ -258,7 +427,7 @@ int main(void)
258427

259428
/* testcase 8: test segmented computing capabilities in crc32 mode */
260429

261-
ret = syscrc32_start(&crc32_ctx, &startval);
430+
ret = syscrc32_start(&crc32_ctx, &startval, sizeof(uint32_t));
262431
if (ret != 0)
263432
{
264433
printf("syscrc32 start failed\n");
@@ -294,7 +463,20 @@ int main(void)
294463
printf("crc32 test case 8 success\n");
295464
}
296465

466+
/* parameter cases: the session key carries the parameter block */
467+
468+
for (i = 0; i < 4; i++)
469+
{
470+
ret |= syscrc32_case(&g_crc32_testparams[i], sizeof(crc32_params),
471+
g_crc32_testresults[i], i + 1);
472+
}
473+
474+
/* CRC-32/MPEG-2 via the three-word {polynomial, initial, xorout} key */
475+
476+
ret |= syscrc32_case(g_crc32_mpeg2_key, sizeof(g_crc32_mpeg2_key),
477+
g_crc32_mpeg2_results, 5);
478+
297479
err:
298480
syscrc32_free(&crc32_ctx);
299-
return 0;
481+
return ret;
300482
}

‎testing/drivers/crypto/crc8.c‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ static const tb g_crc8_testcases[] =
111111

112112
static const crc8_params g_crc8_testparams[] =
113113
{
114+
/* polynomial 0xa6, reflected input */
115+
116+
{
117+
0xa6, 0x00, 0x00, true, false,
118+
},
119+
120+
/* polynomial 0x07, reflected input */
121+
122+
{
123+
0x07, 0x00, 0x00, true, false,
124+
},
125+
114126
/* CRC-8/SAE J1850 */
115127

116128
{
@@ -122,10 +134,30 @@ static const crc8_params g_crc8_testparams[] =
122134
{
123135
0x2f, 0xff, 0xff, false, false,
124136
},
137+
138+
/* CRC-8/ROHC */
139+
140+
{
141+
0x07, 0xff, 0x00, true, true,
142+
},
125143
};
126144

127145
static const uint8_t g_crc8_testresults[][8] =
128146
{
147+
/* polynomial 0xa6, reflected input */
148+
149+
{
150+
0x00, 0xa4, 0xa6, 0x86,
151+
0x68, 0x96, 0x50, 0x50,
152+
},
153+
154+
/* polynomial 0x07, reflected input */
155+
156+
{
157+
0x00, 0x9b, 0x0f, 0x0b,
158+
0xc6, 0x0e, 0x69, 0x69,
159+
},
160+
129161
/* CRC-8/SAE J1850 */
130162

131163
{
@@ -139,6 +171,13 @@ static const uint8_t g_crc8_testresults[][8] =
139171
0x00, 0x07, 0x41, 0xc1,
140172
0xb4, 0x20, 0x0c, 0x0c,
141173
},
174+
175+
/* CRC-8/ROHC */
176+
177+
{
178+
0xff, 0x16, 0x24, 0xdc,
179+
0x39, 0x91, 0x2b, 0x2b,
180+
},
142181
};
143182

144183
/****************************************************************************
@@ -324,7 +363,7 @@ int main(void)
324363

325364
setbuf(stdout, NULL);
326365

327-
for (i = 0; i < 2; i++)
366+
for (i = 0; i < 5; i++)
328367
{
329368
fails += syscrc8_case(&g_crc8_testparams[i],
330369
g_crc8_testresults[i], i + 1);

0 commit comments

Comments
 (0)