Skip to content

Commit caf84c5

Browse files
committed
Cleanup ipcrypt tests
1 parent a3a01ef commit caf84c5

File tree

2 files changed

+27
-43
lines changed

2 files changed

+27
-43
lines changed

test/default/ipcrypt.c

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
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-
124
static 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

2213
int
@@ -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 */

test/default/ipcrypt.exp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ Random key generated (skipped in output)
3232
Random NDX key generated (skipped in output)
3333

3434
Test 5: Different inputs produce different outputs
35-
Input[0]: 00000000000000000000ffff00000000 -> 05406dbec71c4163c3033a3a76b9ebad
36-
Input[1]: 00000000000000000000ffff00000001 -> 1672fc1d4626d0db668088eb5b54a40e
37-
Input[2]: 00000000000000000000ffff00000002 -> 748c9d664ca12e3669ae344a280202c8
38-
Input[3]: 00000000000000000000ffff00000003 -> 524ca1315033ea4509bbaabf93c3ec80
35+
Input[0]: 00000000000000000000ffff00000000
36+
05406dbec71c4163c3033a3a76b9ebad
37+
Input[1]: 00000000000000000000ffff00000001
38+
1672fc1d4626d0db668088eb5b54a40e
39+
Input[2]: 00000000000000000000ffff00000002
40+
748c9d664ca12e3669ae344a280202c8
41+
Input[3]: 00000000000000000000ffff00000003
42+
524ca1315033ea4509bbaabf93c3ec80
3943

4044
Test 6: Verify deterministic encryption
4145
OK: Deterministic encryption verified

0 commit comments

Comments
 (0)