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}
4850tb ;
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+
5064static 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+
100187static 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+
297481err :
298482 syscrc32_free (& crc32_ctx );
299- return 0 ;
483+ return ret ;
300484}
0 commit comments