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}
4849tb ;
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+
5063static 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+
100186static 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+
297479err :
298480 syscrc32_free (& crc32_ctx );
299- return 0 ;
481+ return ret ;
300482}
0 commit comments