xref: /freebsd/sys/contrib/libsodium/test/default/codecs.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 #define TEST_NAME "codecs"
2 #include "cmptest.h"
3 
4 int
5 main(void)
6 {
7     unsigned char  buf1[1000];
8     char           buf3[33];
9     unsigned char  buf4[4];
10     const char    *b64;
11     char          *b64_;
12     const char    *b64_end;
13     unsigned char *bin;
14     unsigned char *bin_padded;
15     const char    *hex;
16     const char    *hex_end;
17     size_t         b64_len;
18     size_t         bin_len, bin_len2;
19     unsigned int   i;
20 
21     printf("%s\n",
22            sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
23                           16U));
24     hex = "Cafe : 6942";
25     sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
26                    &hex_end);
27     printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
28            buf4[0], buf4[1], buf4[2], buf4[3]);
29     printf("dt1: %ld\n", (long) (hex_end - hex));
30 
31     hex = "Cafe : 6942";
32     sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
33     printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
34            buf4[0], buf4[1], buf4[2], buf4[3]);
35 
36     hex = "deadbeef";
37     if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
38         printf("sodium_hex2bin() overflow not detected\n");
39     }
40     printf("dt2: %ld\n", (long) (hex_end - hex));
41 
42     hex = "de:ad:be:eff";
43     if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) {
44         printf(
45             "sodium_hex2bin() with an odd input length and a short output "
46             "buffer\n");
47     }
48     printf("dt3: %ld\n", (long) (hex_end - hex));
49 
50     hex = "de:ad:be:eff";
51     if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
52                        &bin_len, &hex_end) != -1) {
53         printf("sodium_hex2bin() with an odd input length\n");
54     }
55     printf("dt4: %ld\n", (long) (hex_end - hex));
56 
57     hex = "de:ad:be:eff";
58     if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":",
59                        &bin_len, &hex_end) != -1) {
60         printf("sodium_hex2bin() with an odd input length (2)\n");
61     }
62     printf("dt5: %ld\n", (long) (hex_end - hex));
63 
64     hex = "de:ad:be:eff";
65     if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
66                        &bin_len, NULL) != -1) {
67         printf("sodium_hex2bin() with an odd input length and no end pointer\n");
68     }
69 
70     hex = "de:ad:be:ef*";
71     if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
72                        &bin_len, &hex_end) != 0) {
73         printf("sodium_hex2bin() with an extra character and an end pointer\n");
74     }
75     printf("dt6: %ld\n", (long) (hex_end - hex));
76 
77     hex = "de:ad:be:ef*";
78     if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
79                        &bin_len, NULL) != -1) {
80         printf("sodium_hex2bin() with an extra character and no end pointer\n");
81     }
82 
83     printf("%s\n",
84            sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
85                              21U, sodium_base64_VARIANT_ORIGINAL));
86     printf("%s\n",
87            sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
88                              22U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
89     printf("%s\n",
90            sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
91                              21U, sodium_base64_VARIANT_URLSAFE));
92     printf("%s\n",
93            sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
94                              22U, sodium_base64_VARIANT_URLSAFE_NO_PADDING));
95     printf("%s\n",
96            sodium_bin2base64(buf3, 1U, NULL,
97                              0U, sodium_base64_VARIANT_ORIGINAL));
98     printf("%s\n",
99            sodium_bin2base64(buf3, 5U, (const unsigned char *) "a",
100                              1U, sodium_base64_VARIANT_ORIGINAL));
101     printf("%s\n",
102            sodium_bin2base64(buf3, 5U, (const unsigned char *) "ab",
103                              2U, sodium_base64_VARIANT_ORIGINAL));
104     printf("%s\n",
105            sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
106                              3U, sodium_base64_VARIANT_ORIGINAL));
107     printf("%s\n",
108            sodium_bin2base64(buf3, 1U, NULL,
109                              0U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
110     printf("%s\n",
111            sodium_bin2base64(buf3, 3U, (const unsigned char *) "a",
112                              1U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
113     printf("%s\n",
114            sodium_bin2base64(buf3, 4U, (const unsigned char *) "ab",
115                              2U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
116     printf("%s\n",
117            sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
118                              3U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
119 
120     b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
121     memset(buf4, '*', sizeof buf4);
122     assert(sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
123                              &b64_end, sodium_base64_VARIANT_ORIGINAL) == -1);
124     buf4[bin_len] = 0;
125     printf("[%s]\n", (const char *) buf4);
126     printf("[%s]\n", b64_end);
127 
128     memset(buf1, '*', sizeof buf1);
129     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), "\n\r ", &bin_len,
130                              &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
131     buf1[bin_len] = 0;
132     printf("[%s]\n", (const char *) buf1);
133     assert(*b64_end == 0);
134 
135     memset(buf1, '*', sizeof buf1);
136     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, &bin_len,
137                              &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
138     buf1[bin_len] = 0;
139     printf("[%s]\n", (const char *) buf1);
140     printf("[%s]\n", b64_end);
141 
142     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
143                              &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
144     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
145                              &b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
146     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
147                              &b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
148     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
149                              &b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
150     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
151                              &b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
152 
153     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
154                              NULL, sodium_base64_VARIANT_ORIGINAL) == -1);
155     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
156                              NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
157     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
158                              NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
159     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
160                              NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
161     assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
162                              NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
163 
164     assert(sodium_base642bin(NULL, (size_t) 10U, "a=", (size_t) 2U, NULL, NULL, NULL,
165                              sodium_base64_VARIANT_URLSAFE) == -1);
166     assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, NULL, NULL, NULL,
167                              sodium_base64_VARIANT_URLSAFE) == -1);
168     assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "~", NULL, NULL,
169                              sodium_base64_VARIANT_URLSAFE) == -1);
170     assert(sodium_base642bin(NULL, (size_t) 10U, "a*", (size_t) 2U, "*", NULL, NULL,
171                              sodium_base64_VARIANT_URLSAFE) == -1);
172     assert(sodium_base642bin(NULL, (size_t) 10U, "a==", (size_t) 3U, NULL, NULL, NULL,
173                              sodium_base64_VARIANT_URLSAFE) == -1);
174     assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, NULL, NULL, NULL,
175                              sodium_base64_VARIANT_URLSAFE) == -1);
176     assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "~", NULL, NULL,
177                              sodium_base64_VARIANT_URLSAFE) == -1);
178     assert(sodium_base642bin(NULL, (size_t) 10U, "a=*", (size_t) 3U, "*", NULL, NULL,
179                              sodium_base64_VARIANT_URLSAFE) == -1);
180 
181     assert(sodium_base642bin(buf1, sizeof buf1, "O1R", (size_t) 3U, NULL, NULL, NULL,
182                              sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
183     assert(sodium_base642bin(buf1, sizeof buf1, "O1Q", (size_t) 3U, NULL, NULL, NULL,
184                              sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
185     assert(sodium_base642bin(buf1, sizeof buf1, "O1", (size_t) 2U, NULL, NULL, NULL,
186                              sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
187     assert(sodium_base642bin(buf1, sizeof buf1, "Ow", (size_t) 2U, NULL, NULL, NULL,
188                              sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
189     assert(sodium_base642bin(buf1, sizeof buf1, "O", (size_t) 1U, NULL, NULL, NULL,
190                              sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
191 
192     assert(sodium_base642bin(buf1, sizeof buf1, "kaw", (size_t) 3U, NULL, NULL, NULL,
193                              sodium_base64_VARIANT_ORIGINAL) == -1);
194     assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "@", NULL, NULL,
195                              sodium_base64_VARIANT_ORIGINAL) == -1);
196     assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "*", NULL, NULL,
197                              sodium_base64_VARIANT_ORIGINAL) == -1);
198     assert(sodium_base642bin(buf1, sizeof buf1, "kaw=**", (size_t) 6U, "*", NULL, NULL,
199                              sodium_base64_VARIANT_ORIGINAL) == 0);
200     assert(sodium_base642bin(buf1, sizeof buf1, "kaw*=*", (size_t) 6U, "~*", NULL, NULL,
201                              sodium_base64_VARIANT_ORIGINAL) == 0);
202     assert(sodium_base642bin(buf1, sizeof buf1, "ka*w*=*", (size_t) 7U, "*~", NULL, NULL,
203                              sodium_base64_VARIANT_ORIGINAL) == 0);
204 
205     for (i = 0; i < 1000; i++) {
206         assert(sizeof buf1 >= 100);
207         bin_len = (size_t) randombytes_uniform(100);
208         bin = (unsigned char *) sodium_malloc(bin_len);
209         b64_len = (bin_len + 2U) / 3U * 4U + 1U;
210         assert(b64_len == sodium_base64_encoded_len(bin_len, sodium_base64_VARIANT_URLSAFE));
211         b64_ = (char *) sodium_malloc(b64_len);
212         randombytes_buf(bin, bin_len);
213         memcpy(buf1, bin, bin_len);
214         b64 = sodium_bin2base64(b64_, b64_len, bin, bin_len,
215                                 sodium_base64_VARIANT_URLSAFE);
216         assert(b64 != NULL);
217         assert(sodium_base642bin(bin, bin_len + 10, b64, b64_len,
218                                  NULL, NULL, &b64_end,
219                                  sodium_base64_VARIANT_URLSAFE) == 0);
220         assert(b64_end == &b64[b64_len - 1]);
221         assert(memcmp(bin, buf1, bin_len) == 0);
222         sodium_free(bin);
223         sodium_free(b64_);
224     }
225     return 0;
226 }
227