1 #define TEST_NAME "sodium_utils"
6 unsigned char buf_add
[1000];
7 unsigned char buf1
[1000];
8 unsigned char buf2
[1000];
9 unsigned char buf1_rev
[1000];
10 unsigned char buf2_rev
[1000];
12 unsigned char buf4
[4];
13 unsigned char nonce
[24];
21 randombytes_buf(buf1
, sizeof buf1
);
22 memcpy(buf2
, buf1
, sizeof buf2
);
23 printf("%d\n", sodium_memcmp(buf1
, buf2
, sizeof buf1
));
24 sodium_memzero(buf1
, 0U);
25 printf("%d\n", sodium_memcmp(buf1
, buf2
, sizeof buf1
));
26 sodium_memzero(buf1
, sizeof buf1
/ 2);
27 printf("%d\n", sodium_memcmp(buf1
, buf2
, sizeof buf1
));
28 printf("%d\n", sodium_memcmp(buf1
, buf2
, 0U));
29 sodium_memzero(buf2
, sizeof buf2
/ 2);
30 printf("%d\n", sodium_memcmp(buf1
, buf2
, sizeof buf1
));
32 sodium_bin2hex(buf3
, 33U, (const unsigned char *)"0123456789ABCDEF",
35 sodium_hex2bin(buf4
, sizeof buf4
, hex
, strlen(hex
), ": ", &bin_len
, &hex_end
);
36 printf("%lu:%02x%02x%02x%02x\n", (unsigned long)bin_len
, buf4
[0], buf4
[1],
38 printf("dt1: %ld\n", (long) (hex_end
- hex
));
41 sodium_hex2bin(buf4
, sizeof buf4
, hex
, strlen(hex
), ": ", &bin_len
, NULL
);
42 printf("%lu:%02x%02x%02x%02x\n", (unsigned long)bin_len
, buf4
[2], buf4
[3],
46 if (sodium_hex2bin(buf1
, 1U, hex
, 8U, NULL
, &bin_len
, &hex_end
) != -1) {
47 printf("sodium_hex2bin() overflow not detected\n");
49 printf("dt2: %ld\n", (long) (hex_end
- hex
));
52 if (sodium_hex2bin(buf1
, 4U, hex
, 12U, ":", &bin_len
, &hex_end
) != -1) {
53 printf("sodium_hex2bin() with an odd input length and a short output buffer\n");
55 printf("dt3: %ld\n", (long) (hex_end
- hex
));
58 if (sodium_hex2bin(buf1
, sizeof buf1
, hex
, 12U, ":", &bin_len
, &hex_end
) != 0) {
59 printf("sodium_hex2bin() with an odd input length\n");
61 printf("dt4: %ld\n", (long) (hex_end
- hex
));
64 if (sodium_hex2bin(buf1
, sizeof buf1
, hex
, 13U, ":", &bin_len
, &hex_end
) != 0) {
65 printf("sodium_hex2bin() with an odd input length\n");
67 printf("dt5: %ld\n", (long) (hex_end
- hex
));
69 memset(nonce
, 0, sizeof nonce
);
70 sodium_increment(nonce
, sizeof nonce
);
71 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
72 nonce
, sizeof nonce
));
73 memset(nonce
, 255, sizeof nonce
);
74 sodium_increment(nonce
, sizeof nonce
);
75 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
76 nonce
, sizeof nonce
));
78 sodium_increment(nonce
, sizeof nonce
);
79 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
80 nonce
, sizeof nonce
));
82 sodium_increment(nonce
, sizeof nonce
);
83 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
84 nonce
, sizeof nonce
));
87 sodium_increment(nonce
, sizeof nonce
);
88 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
89 nonce
, sizeof nonce
));
90 for (i
= 0U; i
< 1000U; i
++) {
91 bin_len
= (size_t) randombytes_uniform(sizeof buf1
);
92 randombytes_buf(buf1
, bin_len
);
93 randombytes_buf(buf2
, bin_len
);
94 for (j
= 0U; j
< bin_len
; j
++) {
95 buf1_rev
[bin_len
- 1 - j
] = buf1
[j
];
96 buf2_rev
[bin_len
- 1 - j
] = buf2
[j
];
98 if (memcmp(buf1_rev
, buf2_rev
, bin_len
) *
99 sodium_compare(buf1
, buf2
, bin_len
) < 0) {
100 printf("sodium_compare() failure with length=%u\n",
101 (unsigned int) bin_len
);
103 memcpy(buf1
, buf2
, bin_len
);
104 if (sodium_compare(buf1
, buf2
, bin_len
)) {
105 printf("sodium_compare() equality failure with length=%u\n",
106 (unsigned int) bin_len
);
109 memset(buf1
, 0, sizeof buf1
);
110 if (sodium_is_zero(buf1
, sizeof buf1
) != 1) {
111 printf("sodium_is_zero() failed\n");
113 for (i
= 0U; i
< sizeof buf1
; i
++) {
115 if (sodium_is_zero(buf1
, sizeof buf1
) != 0) {
116 printf("sodium_is_zero() failed\n");
120 bin_len
= randombytes_uniform(sizeof buf1
);
121 randombytes_buf(buf1
, bin_len
);
122 memcpy(buf2
, buf1
, bin_len
);
123 memset(buf_add
, 0, bin_len
);
124 j
= randombytes_uniform(10000);
125 for (i
= 0U; i
< j
; i
++) {
126 sodium_increment(buf1
, bin_len
);
127 sodium_increment(buf_add
, bin_len
);
129 sodium_add(buf2
, buf_add
, bin_len
);
130 if (sodium_compare(buf1
, buf2
, bin_len
) != 0) {
131 printf("sodium_add() failed\n");
133 bin_len
= randombytes_uniform(sizeof buf1
);
134 randombytes_buf(buf1
, bin_len
);
135 memcpy(buf2
, buf1
, bin_len
);
136 memset(buf_add
, 0xff, bin_len
);
137 sodium_increment(buf2
, bin_len
);
138 sodium_increment(buf2
, 0U);
139 sodium_add(buf2
, buf_add
, bin_len
);
140 sodium_add(buf2
, buf_add
, 0U);
141 if (sodium_compare(buf1
, buf2
, bin_len
) != 0) {
142 printf("sodium_add() failed\n");
145 assert(sizeof nonce
>= 24U);
146 memset(nonce
, 0xfe, 24U);
147 memset(nonce
, 0xff, 6U);
148 sodium_increment(nonce
, 8U);
149 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
150 nonce
, sizeof nonce
));
151 memset(nonce
, 0xfe, 24U);
152 memset(nonce
, 0xff, 10U);
153 sodium_increment(nonce
, 12U);
154 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
155 nonce
, sizeof nonce
));
156 memset(nonce
, 0xff, 22U);
157 sodium_increment(nonce
, 24U);
158 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
159 nonce
, sizeof nonce
));
162 assert(sizeof nonce
>= 24U);
163 memset(nonce
, 0xfe, 24U);
164 memset(nonce
, 0xff, 6U);
165 sodium_add(nonce
, nonce
, 7U);
166 sodium_add(nonce
, nonce
, 8U);
167 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
168 nonce
, sizeof nonce
));
169 memset(nonce
, 0xfe, 24U);
170 memset(nonce
, 0xff, 10U);
171 sodium_add(nonce
, nonce
, 11U);
172 sodium_add(nonce
, nonce
, 12U);
173 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
174 nonce
, sizeof nonce
));
175 memset(nonce
, 0xff, 22U);
176 sodium_add(nonce
, nonce
, 23U);
177 sodium_add(nonce
, nonce
, 24U);
178 printf("%s\n", sodium_bin2hex(nonce_hex
, sizeof nonce_hex
,
179 nonce
, sizeof nonce
));