11#define TEST_NAME "ipcrypt"
22#include "cmptest.h"
33
4- #ifdef DEBUG_TEST
5- # define DPRINT (...) fprintf(stderr, __VA_ARGS__)
6- #else
7- # define DPRINT (...) \
8- do { \
9- } while (0)
10- #endif
11-
124static void
13- print_hex (const unsigned char * data , size_t len )
5+ dump_hex (const unsigned char * data , size_t len )
146{
15- size_t i ;
7+ char hex [ 129 ] ;
168
17- for (i = 0 ; i < len ; i ++ ) {
18- printf ("%02x" , data [i ]);
19- }
9+ sodium_bin2hex (hex , sizeof hex , data , len );
10+ printf ("%s\n" , hex );
2011}
2112
2213int
@@ -72,20 +63,17 @@ main(void)
7263
7364 printf ("\nTest 1: Format-preserving encryption\n" );
7465 printf ("Key: " );
75- print_hex (key , sizeof key );
76- printf ("\nInput: " );
77- print_hex (input , sizeof input );
78- printf ("\n" );
66+ dump_hex (key , sizeof key );
67+ printf ("Input: " );
68+ dump_hex (input , sizeof input );
7969
8070 crypto_ipcrypt_encrypt (output , input , key );
8171 printf ("Encrypted: " );
82- print_hex (output , sizeof output );
83- printf ("\n" );
72+ dump_hex (output , sizeof output );
8473
8574 crypto_ipcrypt_decrypt (decrypted , output , key );
8675 printf ("Decrypted: " );
87- print_hex (decrypted , sizeof decrypted );
88- printf ("\n" );
76+ dump_hex (decrypted , sizeof decrypted );
8977
9078 if (memcmp (input , decrypted , sizeof input ) != 0 ) {
9179 printf ("FAILED: Decrypted does not match input\n" );
@@ -106,18 +94,15 @@ main(void)
10694
10795 printf ("\nTest 2: Non-deterministic encryption (ND mode)\n" );
10896 printf ("Tweak: " );
109- print_hex (tweak_nd , sizeof tweak_nd );
110- printf ("\n" );
97+ dump_hex (tweak_nd , sizeof tweak_nd );
11198
11299 crypto_ipcrypt_nd_encrypt (nd_output , input , tweak_nd , key );
113100 printf ("ND Encrypted: " );
114- print_hex (nd_output , sizeof nd_output );
115- printf ("\n" );
101+ dump_hex (nd_output , sizeof nd_output );
116102
117103 crypto_ipcrypt_nd_decrypt (decrypted , nd_output , key );
118104 printf ("ND Decrypted: " );
119- print_hex (decrypted , sizeof decrypted );
120- printf ("\n" );
105+ dump_hex (decrypted , sizeof decrypted );
121106
122107 if (memcmp (input , decrypted , sizeof input ) != 0 ) {
123108 printf ("FAILED: ND decrypted does not match input\n" );
@@ -138,20 +123,17 @@ main(void)
138123
139124 printf ("\nTest 3: Non-deterministic encryption (NDX mode with 16-byte tweak)\n" );
140125 printf ("NDX Key: " );
141- print_hex (ndx_key , sizeof ndx_key );
142- printf ("\nNDX Tweak: " );
143- print_hex (tweak_ndx , sizeof tweak_ndx );
144- printf ("\n" );
126+ dump_hex (ndx_key , sizeof ndx_key );
127+ printf ("NDX Tweak: " );
128+ dump_hex (tweak_ndx , sizeof tweak_ndx );
145129
146130 crypto_ipcrypt_ndx_encrypt (ndx_output , input , tweak_ndx , ndx_key );
147131 printf ("NDX Encrypted: " );
148- print_hex (ndx_output , sizeof ndx_output );
149- printf ("\n" );
132+ dump_hex (ndx_output , sizeof ndx_output );
150133
151134 crypto_ipcrypt_ndx_decrypt (decrypted , ndx_output , ndx_key );
152135 printf ("NDX Decrypted: " );
153- print_hex (decrypted , sizeof decrypted );
154- printf ("\n" );
136+ dump_hex (decrypted , sizeof decrypted );
155137
156138 if (memcmp (input , decrypted , sizeof input ) != 0 ) {
157139 printf ("FAILED: NDX decrypted does not match input\n" );
@@ -179,10 +161,8 @@ main(void)
179161
180162 crypto_ipcrypt_encrypt (output , input , key );
181163 printf ("Input[%zu]: " , i );
182- print_hex (input , sizeof input );
183- printf (" -> " );
184- print_hex (output , sizeof output );
185- printf ("\n" );
164+ dump_hex (input , sizeof input );
165+ dump_hex (output , sizeof output );
186166 }
187167
188168 /* Test 6: Verify deterministic encryption */
0 commit comments