Skip to content

Commit ac60978

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 77797a0 commit ac60978

3 files changed

Lines changed: 256 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: 189 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
****************************************************************************/
2222

2323
#include <err.h>
24+
#include <stdbool.h>
2425
#include <stdint.h>
26+
#include <inttypes.h>
2527
#include <stdio.h>
2628
#include <fcntl.h>
2729
#include <stdlib.h>
@@ -47,6 +49,18 @@ typedef struct tb
4749
}
4850
tb;
4951

52+
/* Session key of the crc32 xform: the parameter block itself */
53+
54+
typedef struct crc32_params
55+
{
56+
uint32_t polynomial;
57+
uint32_t initial;
58+
uint32_t xorout;
59+
bool reflect_in;
60+
bool reflect_out;
61+
}
62+
crc32_params;
63+
5064
static tb g_crc32_testcase[] =
5165
{
5266
/* testcase 1-7: Individual testing */
@@ -97,6 +111,79 @@ static tb g_crc32_testcase[] =
97111
}
98112
};
99113

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

135-
static int syscrc32_start(FAR crypto_context *ctx, uint32_t *key)
222+
static int syscrc32_start(FAR crypto_context *ctx, FAR const void *key,
223+
int keylen)
136224
{
137225
ctx->session.mac = CRYPTO_CRC32;
138226
ctx->session.mackey = (caddr_t) key;
139-
ctx->session.mackeylen = sizeof(uint32_t);
227+
ctx->session.mackeylen = keylen;
140228
if (ioctl(ctx->crypto_fd, CIOCGSESSION, &ctx->session) == -1)
141229
{
142230
warn("CIOCGSESSION");
@@ -200,6 +288,87 @@ static int match(const uint32_t a, const uint32_t b)
200288
return 1;
201289
}
202290

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

384+
setbuf(stdout, NULL);
385+
215386
ret = syscrc32_init(&crc32_ctx);
216387
if (ret != 0)
217388
{
@@ -222,7 +393,7 @@ int main(void)
222393

223394
for (i = 0; i < sizeof(g_crc32_testcase) / sizeof(tb) - 1; i++)
224395
{
225-
ret = syscrc32_start(&crc32_ctx, &startval);
396+
ret = syscrc32_start(&crc32_ctx, &startval, sizeof(uint32_t));
226397
if (ret != 0)
227398
{
228399
printf("syscrc32 start failed\n");
@@ -258,7 +429,7 @@ int main(void)
258429

259430
/* testcase 8: test segmented computing capabilities in crc32 mode */
260431

261-
ret = syscrc32_start(&crc32_ctx, &startval);
432+
ret = syscrc32_start(&crc32_ctx, &startval, sizeof(uint32_t));
262433
if (ret != 0)
263434
{
264435
printf("syscrc32 start failed\n");
@@ -294,7 +465,20 @@ int main(void)
294465
printf("crc32 test case 8 success\n");
295466
}
296467

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

‎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)