8d6a67eaf3a33f7ab3b06d1f9b373397428cf6eb
[BearSSL] / tools / names.c
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "brssl.h"
26 #include "bearssl.h"
27
28 /* see brssl.h */
29 const protocol_version protocol_versions[] = {
30 { "tls10", BR_TLS10, "TLS 1.0" },
31 { "tls11", BR_TLS11, "TLS 1.1" },
32 { "tls12", BR_TLS12, "TLS 1.2" },
33 { NULL, 0, NULL }
34 };
35
36 /* see brssl.h */
37 const hash_function hash_functions[] = {
38 { "md5", &br_md5_vtable, "MD5" },
39 { "sha1", &br_sha1_vtable, "SHA-1" },
40 { "sha224", &br_sha224_vtable, "SHA-224" },
41 { "sha256", &br_sha256_vtable, "SHA-256" },
42 { "sha384", &br_sha384_vtable, "SHA-384" },
43 { "sha512", &br_sha512_vtable, "SHA-512" },
44 { NULL, 0, NULL }
45 };
46
47 /* see brssl.h */
48 const cipher_suite cipher_suites[] = {
49 {
50 "ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
51 BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
52 REQ_ECDHE_ECDSA | REQ_CHAPOL | REQ_SHA256 | REQ_TLS12,
53 "ECDHE with ECDSA, ChaCha20+Poly1305 encryption (TLS 1.2+)"
54 },
55 {
56 "ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
57 BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
58 REQ_ECDHE_RSA | REQ_CHAPOL | REQ_SHA256 | REQ_TLS12,
59 "ECDHE with RSA, ChaCha20+Poly1305 encryption (TLS 1.2+)"
60 },
61 {
62 "ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
63 BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
64 REQ_ECDHE_ECDSA | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
65 "ECDHE with ECDSA, AES-128/GCM encryption (TLS 1.2+)"
66 },
67 {
68 "ECDHE_RSA_WITH_AES_128_GCM_SHA256",
69 BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
70 REQ_ECDHE_RSA | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
71 "ECDHE with RSA, AES-128/GCM encryption (TLS 1.2+)"
72 },
73 {
74 "ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
75 BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
76 REQ_ECDHE_ECDSA | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
77 "ECDHE with ECDSA, AES-256/GCM encryption (TLS 1.2+)"
78 },
79 {
80 "ECDHE_RSA_WITH_AES_256_GCM_SHA384",
81 BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
82 REQ_ECDHE_RSA | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
83 "ECDHE with RSA, AES-256/GCM encryption (TLS 1.2+)"
84 },
85 {
86 "ECDHE_ECDSA_WITH_AES_128_CCM",
87 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM,
88 REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
89 "ECDHE with ECDSA, AES-128/CCM encryption (TLS 1.2+)"
90 },
91 {
92 "ECDHE_ECDSA_WITH_AES_256_CCM",
93 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM,
94 REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
95 "ECDHE with ECDSA, AES-256/CCM encryption (TLS 1.2+)"
96 },
97 {
98 "ECDHE_ECDSA_WITH_AES_128_CCM_8",
99 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8,
100 REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
101 "ECDHE with ECDSA, AES-128/CCM_8 encryption (TLS 1.2+)"
102 },
103 {
104 "ECDHE_ECDSA_WITH_AES_256_CCM_8",
105 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8,
106 REQ_ECDHE_ECDSA | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
107 "ECDHE with ECDSA, AES-256/CCM_8 encryption (TLS 1.2+)"
108 },
109 {
110 "ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
111 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
112 REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
113 "ECDHE with ECDSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
114 },
115 {
116 "ECDHE_RSA_WITH_AES_128_CBC_SHA256",
117 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
118 REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
119 "ECDHE with RSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
120 },
121 {
122 "ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
123 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
124 REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
125 "ECDHE with ECDSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
126 },
127 {
128 "ECDHE_RSA_WITH_AES_256_CBC_SHA384",
129 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
130 REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
131 "ECDHE with RSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
132 },
133 {
134 "ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
135 BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
136 REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA1,
137 "ECDHE with ECDSA, AES-128/CBC + SHA-1"
138 },
139 {
140 "ECDHE_RSA_WITH_AES_128_CBC_SHA",
141 BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
142 REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA1,
143 "ECDHE with RSA, AES-128/CBC + SHA-1"
144 },
145 {
146 "ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
147 BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
148 REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA1,
149 "ECDHE with ECDSA, AES-256/CBC + SHA-1"
150 },
151 {
152 "ECDHE_RSA_WITH_AES_256_CBC_SHA",
153 BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
154 REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA1,
155 "ECDHE with RSA, AES-256/CBC + SHA-1"
156 },
157 {
158 "ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
159 BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
160 REQ_ECDH | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
161 "ECDH key exchange (EC cert), AES-128/GCM (TLS 1.2+)"
162 },
163 {
164 "ECDH_RSA_WITH_AES_128_GCM_SHA256",
165 BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
166 REQ_ECDH | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
167 "ECDH key exchange (RSA cert), AES-128/GCM (TLS 1.2+)"
168 },
169 {
170 "ECDH_ECDSA_WITH_AES_256_GCM_SHA384",
171 BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
172 REQ_ECDH | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
173 "ECDH key exchange (EC cert), AES-256/GCM (TLS 1.2+)"
174 },
175 {
176 "ECDH_RSA_WITH_AES_256_GCM_SHA384",
177 BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
178 REQ_ECDH | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
179 "ECDH key exchange (RSA cert), AES-256/GCM (TLS 1.2+)"
180 },
181 {
182 "ECDH_ECDSA_WITH_AES_128_CBC_SHA256",
183 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
184 REQ_ECDH | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
185 "ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
186 },
187 {
188 "ECDH_RSA_WITH_AES_128_CBC_SHA256",
189 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
190 REQ_ECDH | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
191 "ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
192 },
193 {
194 "ECDH_ECDSA_WITH_AES_256_CBC_SHA384",
195 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
196 REQ_ECDH | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
197 "ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
198 },
199 {
200 "ECDH_RSA_WITH_AES_256_CBC_SHA384",
201 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
202 REQ_ECDH | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
203 "ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
204 },
205 {
206 "ECDH_ECDSA_WITH_AES_128_CBC_SHA",
207 BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
208 REQ_ECDH | REQ_AESCBC | REQ_SHA1,
209 "ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-1"
210 },
211 {
212 "ECDH_RSA_WITH_AES_128_CBC_SHA",
213 BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
214 REQ_ECDH | REQ_AESCBC | REQ_SHA1,
215 "ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-1"
216 },
217 {
218 "ECDH_ECDSA_WITH_AES_256_CBC_SHA",
219 BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
220 REQ_ECDH | REQ_AESCBC | REQ_SHA1,
221 "ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-1"
222 },
223 {
224 "ECDH_RSA_WITH_AES_256_CBC_SHA",
225 BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
226 REQ_ECDH | REQ_AESCBC | REQ_SHA1,
227 "ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-1"
228 },
229 {
230 "RSA_WITH_AES_128_GCM_SHA256",
231 BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
232 REQ_RSAKEYX | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
233 "RSA key exchange, AES-128/GCM encryption (TLS 1.2+)"
234 },
235 {
236 "RSA_WITH_AES_256_GCM_SHA384",
237 BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
238 REQ_RSAKEYX | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
239 "RSA key exchange, AES-256/GCM encryption (TLS 1.2+)"
240 },
241 {
242 "RSA_WITH_AES_128_CCM",
243 BR_TLS_RSA_WITH_AES_128_CCM,
244 REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
245 "RSA key exchange, AES-128/CCM encryption (TLS 1.2+)"
246 },
247 {
248 "RSA_WITH_AES_256_CCM",
249 BR_TLS_RSA_WITH_AES_256_CCM,
250 REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
251 "RSA key exchange, AES-256/CCM encryption (TLS 1.2+)"
252 },
253 {
254 "RSA_WITH_AES_128_CCM_8",
255 BR_TLS_RSA_WITH_AES_128_CCM_8,
256 REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
257 "RSA key exchange, AES-128/CCM_8 encryption (TLS 1.2+)"
258 },
259 {
260 "RSA_WITH_AES_256_CCM_8",
261 BR_TLS_RSA_WITH_AES_256_CCM_8,
262 REQ_RSAKEYX | REQ_AESCCM | REQ_SHA256 | REQ_TLS12,
263 "RSA key exchange, AES-256/CCM_8 encryption (TLS 1.2+)"
264 },
265 {
266 "RSA_WITH_AES_128_CBC_SHA256",
267 BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
268 REQ_RSAKEYX | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
269 "RSA key exchange, AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
270 },
271 {
272 "RSA_WITH_AES_256_CBC_SHA256",
273 BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
274 REQ_RSAKEYX | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
275 "RSA key exchange, AES-256/CBC + HMAC/SHA-256 (TLS 1.2+)"
276 },
277 {
278 "RSA_WITH_AES_128_CBC_SHA",
279 BR_TLS_RSA_WITH_AES_128_CBC_SHA,
280 REQ_RSAKEYX | REQ_AESCBC | REQ_SHA1,
281 "RSA key exchange, AES-128/CBC + HMAC/SHA-1"
282 },
283 {
284 "RSA_WITH_AES_256_CBC_SHA",
285 BR_TLS_RSA_WITH_AES_256_CBC_SHA,
286 REQ_RSAKEYX | REQ_AESCBC | REQ_SHA1,
287 "RSA key exchange, AES-256/CBC + HMAC/SHA-1"
288 },
289 {
290 "ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
291 BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
292 REQ_ECDHE_ECDSA | REQ_3DESCBC | REQ_SHA1,
293 "ECDHE with ECDSA, 3DES/CBC + SHA-1"
294 },
295 {
296 "ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
297 BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
298 REQ_ECDHE_RSA | REQ_3DESCBC | REQ_SHA1,
299 "ECDHE with RSA, 3DES/CBC + SHA-1"
300 },
301 {
302 "ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",
303 BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
304 REQ_ECDH | REQ_3DESCBC | REQ_SHA1,
305 "ECDH key exchange (EC cert), 3DES/CBC + HMAC/SHA-1"
306 },
307 {
308 "ECDH_RSA_WITH_3DES_EDE_CBC_SHA",
309 BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
310 REQ_ECDH | REQ_3DESCBC | REQ_SHA1,
311 "ECDH key exchange (RSA cert), 3DES/CBC + HMAC/SHA-1"
312 },
313 {
314 "RSA_WITH_3DES_EDE_CBC_SHA",
315 BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
316 REQ_RSAKEYX | REQ_3DESCBC | REQ_SHA1,
317 "RSA key exchange, 3DES/CBC + HMAC/SHA-1"
318 },
319 { NULL, 0, 0, NULL }
320 };
321
322 static const struct {
323 int id;
324 const char *name;
325 const char *sid[4];
326 } curves[] = {
327 { BR_EC_sect163k1,
328 "sect163k1",
329 { "sect163k1", "K-163", NULL, NULL } },
330 { BR_EC_sect163r1,
331 "sect163r1",
332 { "sect163r1", NULL, NULL, NULL } },
333 { BR_EC_sect163r2,
334 "sect163r2",
335 { "sect163r2", "B-163", NULL, NULL } },
336 { BR_EC_sect193r1,
337 "sect193r1",
338 { "sect193r1", NULL, NULL, NULL } },
339 { BR_EC_sect193r2,
340 "sect193r2",
341 { "sect193r2", NULL, NULL, NULL } },
342 { BR_EC_sect233k1,
343 "sect233k1",
344 { "sect233k1", "K-233", NULL, NULL } },
345 { BR_EC_sect233r1,
346 "sect233r1",
347 { "sect233r1", "B-233", NULL, NULL } },
348 { BR_EC_sect239k1,
349 "sect239k1",
350 { "sect239k1", NULL, NULL, NULL } },
351 { BR_EC_sect283k1,
352 "sect283k1",
353 { "sect283k1", "K-283", NULL, NULL } },
354 { BR_EC_sect283r1,
355 "sect283r1",
356 { "sect283r1", "B-283", NULL, NULL } },
357 { BR_EC_sect409k1,
358 "sect409k1",
359 { "sect409k1", "K-409", NULL, NULL } },
360 { BR_EC_sect409r1,
361 "sect409r1",
362 { "sect409r1", "B-409", NULL, NULL } },
363 { BR_EC_sect571k1,
364 "sect571k1",
365 { "sect571k1", "K-571", NULL, NULL } },
366 { BR_EC_sect571r1,
367 "sect571r1",
368 { "sect571r1", "B-571", NULL, NULL } },
369 { BR_EC_secp160k1,
370 "secp160k1",
371 { "secp160k1", NULL, NULL, NULL } },
372 { BR_EC_secp160r1,
373 "secp160r1",
374 { "secp160r1", NULL, NULL, NULL } },
375 { BR_EC_secp160r2,
376 "secp160r2",
377 { "secp160r2", NULL, NULL, NULL } },
378 { BR_EC_secp192k1,
379 "secp192k1",
380 { "secp192k1", NULL, NULL, NULL } },
381 { BR_EC_secp192r1,
382 "secp192r1",
383 { "secp192r1", "P-192", NULL, NULL } },
384 { BR_EC_secp224k1,
385 "secp224k1",
386 { "secp224k1", NULL, NULL, NULL } },
387 { BR_EC_secp224r1,
388 "secp224r1",
389 { "secp224r1", "P-224", NULL, NULL } },
390 { BR_EC_secp256k1,
391 "secp256k1",
392 { "secp256k1", NULL, NULL, NULL } },
393 { BR_EC_secp256r1,
394 "secp256r1 (P-256)",
395 { "secp256r1", "P-256", "prime256v1", NULL } },
396 { BR_EC_secp384r1,
397 "secp384r1 (P-384)",
398 { "secp384r1", "P-384", NULL, NULL } },
399 { BR_EC_secp521r1,
400 "secp521r1 (P-521)",
401 { "secp521r1", "P-521", NULL, NULL } },
402 { BR_EC_brainpoolP256r1,
403 "brainpoolP256r1",
404 { "brainpoolP256r1", NULL, NULL, NULL } },
405 { BR_EC_brainpoolP384r1,
406 "brainpoolP384r1",
407 { "brainpoolP384r1", NULL, NULL, NULL } },
408 { BR_EC_brainpoolP512r1,
409 "brainpoolP512r1",
410 { "brainpoolP512r1", NULL, NULL, NULL } },
411 { BR_EC_curve25519,
412 "Curve25519",
413 { "curve25519", "c25519", NULL, NULL } },
414 { BR_EC_curve448,
415 "Curve448",
416 { "curve448", "c448", NULL, NULL } },
417 { 0, 0, { 0, 0, 0, 0 } }
418 };
419
420 static const struct {
421 const char *long_name;
422 const char *short_name;
423 const void *impl;
424 } algo_names[] = {
425 /* Block ciphers */
426 { "aes_big_cbcenc", "big", &br_aes_big_cbcenc_vtable },
427 { "aes_big_cbcdec", "big", &br_aes_big_cbcdec_vtable },
428 { "aes_big_ctr", "big", &br_aes_big_ctr_vtable },
429 { "aes_big_ctrcbc", "big", &br_aes_big_ctrcbc_vtable },
430 { "aes_small_cbcenc", "small", &br_aes_small_cbcenc_vtable },
431 { "aes_small_cbcdec", "small", &br_aes_small_cbcdec_vtable },
432 { "aes_small_ctr", "small", &br_aes_small_ctr_vtable },
433 { "aes_small_ctrcbc", "small", &br_aes_small_ctrcbc_vtable },
434 { "aes_ct_cbcenc", "ct", &br_aes_ct_cbcenc_vtable },
435 { "aes_ct_cbcdec", "ct", &br_aes_ct_cbcdec_vtable },
436 { "aes_ct_ctr", "ct", &br_aes_ct_ctr_vtable },
437 { "aes_ct_ctrcbc", "ct", &br_aes_ct_ctrcbc_vtable },
438 { "aes_ct64_cbcenc", "ct64", &br_aes_ct64_cbcenc_vtable },
439 { "aes_ct64_cbcdec", "ct64", &br_aes_ct64_cbcdec_vtable },
440 { "aes_ct64_ctr", "ct64", &br_aes_ct64_ctr_vtable },
441 { "aes_ct64_ctrcbc", "ct64", &br_aes_ct64_ctrcbc_vtable },
442
443 { "des_tab_cbcenc", "tab", &br_des_tab_cbcenc_vtable },
444 { "des_tab_cbcdec", "tab", &br_des_tab_cbcdec_vtable },
445 { "des_ct_cbcenc", "ct", &br_des_ct_cbcenc_vtable },
446 { "des_ct_cbcdec", "ct", &br_des_ct_cbcdec_vtable },
447
448 { "chacha20_ct", "ct", &br_chacha20_ct_run },
449
450 { "ghash_ctmul", "ctmul", &br_ghash_ctmul },
451 { "ghash_ctmul32", "ctmul32", &br_ghash_ctmul32 },
452 { "ghash_ctmul64", "ctmul64", &br_ghash_ctmul64 },
453
454 { "poly1305_ctmul", "ctmul", &br_poly1305_ctmul_run },
455 { "poly1305_ctmul32", "ctmul32", &br_poly1305_ctmul32_run },
456
457 { "ec_all_m15", "all_m15", &br_ec_all_m15 },
458 { "ec_all_m31", "all_m31", &br_ec_all_m31 },
459 { "ec_c25519_i15", "c25519_i15", &br_ec_c25519_i15 },
460 { "ec_c25519_i31", "c25519_i31", &br_ec_c25519_i31 },
461 { "ec_c25519_m15", "c25519_m15", &br_ec_c25519_m15 },
462 { "ec_c25519_m31", "c25519_m31", &br_ec_c25519_m31 },
463 { "ec_p256_m15", "p256_m15", &br_ec_p256_m15 },
464 { "ec_p256_m31", "p256_m31", &br_ec_p256_m31 },
465 { "ec_prime_i15", "prime_i15", &br_ec_prime_i15 },
466 { "ec_prime_i31", "prime_i31", &br_ec_prime_i31 },
467
468 { "ecdsa_i15_sign_asn1", "i15_asn1", &br_ecdsa_i15_sign_asn1 },
469 { "ecdsa_i15_sign_raw", "i15_raw", &br_ecdsa_i15_sign_raw },
470 { "ecdsa_i31_sign_asn1", "i31_asn1", &br_ecdsa_i31_sign_asn1 },
471 { "ecdsa_i31_sign_raw", "i31_raw", &br_ecdsa_i31_sign_raw },
472 { "ecdsa_i15_vrfy_asn1", "i15_asn1", &br_ecdsa_i15_vrfy_asn1 },
473 { "ecdsa_i15_vrfy_raw", "i15_raw", &br_ecdsa_i15_vrfy_raw },
474 { "ecdsa_i31_vrfy_asn1", "i31_asn1", &br_ecdsa_i31_vrfy_asn1 },
475 { "ecdsa_i31_vrfy_raw", "i31_raw", &br_ecdsa_i31_vrfy_raw },
476
477 { "rsa_i15_pkcs1_sign", "i15", &br_rsa_i15_pkcs1_sign },
478 { "rsa_i31_pkcs1_sign", "i31", &br_rsa_i31_pkcs1_sign },
479 { "rsa_i32_pkcs1_sign", "i32", &br_rsa_i32_pkcs1_sign },
480 { "rsa_i15_pkcs1_vrfy", "i15", &br_rsa_i15_pkcs1_vrfy },
481 { "rsa_i31_pkcs1_vrfy", "i31", &br_rsa_i31_pkcs1_vrfy },
482 { "rsa_i32_pkcs1_vrfy", "i32", &br_rsa_i32_pkcs1_vrfy },
483
484 { 0, 0, 0 }
485 };
486
487 static const struct {
488 const char *long_name;
489 const char *short_name;
490 const void *(*get)(void);
491 } algo_names_dyn[] = {
492 { "aes_pwr8_cbcenc", "pwr8",
493 (const void *(*)(void))&br_aes_pwr8_cbcenc_get_vtable },
494 { "aes_pwr8_cbcdec", "pwr8",
495 (const void *(*)(void))&br_aes_pwr8_cbcdec_get_vtable },
496 { "aes_pwr8_ctr", "pwr8",
497 (const void *(*)(void))&br_aes_pwr8_ctr_get_vtable },
498 { "aes_pwr8_ctrcbc", "pwr8",
499 (const void *(*)(void))&br_aes_pwr8_ctrcbc_get_vtable },
500 { "aes_x86ni_cbcenc", "x86ni",
501 (const void *(*)(void))&br_aes_x86ni_cbcenc_get_vtable },
502 { "aes_x86ni_cbcdec", "x86ni",
503 (const void *(*)(void))&br_aes_x86ni_cbcdec_get_vtable },
504 { "aes_x86ni_ctr", "x86ni",
505 (const void *(*)(void))&br_aes_x86ni_ctr_get_vtable },
506 { "aes_x86ni_ctrcbc", "x86ni",
507 (const void *(*)(void))&br_aes_x86ni_ctrcbc_get_vtable },
508 { "chacha20_sse2", "sse2",
509 (const void *(*)(void))&br_chacha20_sse2_get },
510 { "ghash_pclmul", "pclmul",
511 (const void *(*)(void))&br_ghash_pclmul_get },
512 { "ghash_pwr8", "pwr8",
513 (const void *(*)(void))&br_ghash_pwr8_get },
514 { "poly1305_ctmulq", "ctmulq",
515 (const void *(*)(void))&br_poly1305_ctmulq_get },
516 { "rsa_i62_pkcs1_sign", "i62",
517 (const void *(*)(void))&br_rsa_i62_pkcs1_sign_get },
518 { "rsa_i62_pkcs1_vrfy", "i62",
519 (const void *(*)(void))&br_rsa_i62_pkcs1_vrfy_get },
520 { "ec_c25519_m62", "m62",
521 (const void *(*)(void))&br_ec_c25519_m62_get },
522 { 0, 0, 0, }
523 };
524
525 /* see brssl.h */
526 const char *
527 get_algo_name(const void *impl, int long_name)
528 {
529 size_t u;
530
531 for (u = 0; algo_names[u].long_name; u ++) {
532 if (impl == algo_names[u].impl) {
533 return long_name
534 ? algo_names[u].long_name
535 : algo_names[u].short_name;
536 }
537 }
538 for (u = 0; algo_names_dyn[u].long_name; u ++) {
539 if (impl == algo_names_dyn[u].get()) {
540 return long_name
541 ? algo_names_dyn[u].long_name
542 : algo_names_dyn[u].short_name;
543 }
544 }
545 return "UNKNOWN";
546 }
547
548 /* see brssl.h */
549 const char *
550 get_curve_name(int id)
551 {
552 size_t u;
553
554 for (u = 0; curves[u].name; u ++) {
555 if (curves[u].id == id) {
556 return curves[u].name;
557 }
558 }
559 return NULL;
560 }
561
562 /* see brssl.h */
563 int
564 get_curve_name_ext(int id, char *dst, size_t len)
565 {
566 const char *name;
567 char tmp[30];
568 size_t n;
569
570 name = get_curve_name(id);
571 if (name == NULL) {
572 sprintf(tmp, "unknown (%d)", id);
573 name = tmp;
574 }
575 n = 1 + strlen(name);
576 if (n > len) {
577 if (len > 0) {
578 dst[0] = 0;
579 }
580 return -1;
581 }
582 memcpy(dst, name, n);
583 return 0;
584 }
585
586 /* see brssl.h */
587 const char *
588 get_suite_name(unsigned suite)
589 {
590 size_t u;
591
592 for (u = 0; cipher_suites[u].name; u ++) {
593 if (cipher_suites[u].suite == suite) {
594 return cipher_suites[u].name;
595 }
596 }
597 return NULL;
598 }
599
600 /* see brssl.h */
601 int
602 get_suite_name_ext(unsigned suite, char *dst, size_t len)
603 {
604 const char *name;
605 char tmp[30];
606 size_t n;
607
608 name = get_suite_name(suite);
609 if (name == NULL) {
610 sprintf(tmp, "unknown (0x%04X)", suite);
611 name = tmp;
612 }
613 n = 1 + strlen(name);
614 if (n > len) {
615 if (len > 0) {
616 dst[0] = 0;
617 }
618 return -1;
619 }
620 memcpy(dst, name, n);
621 return 0;
622 }
623
624 /* see brssl.h */
625 int
626 uses_ecdhe(unsigned suite)
627 {
628 size_t u;
629
630 for (u = 0; cipher_suites[u].name; u ++) {
631 if (cipher_suites[u].suite == suite) {
632 return (cipher_suites[u].req
633 & (REQ_ECDHE_RSA | REQ_ECDHE_ECDSA)) != 0;
634 }
635 }
636 return 0;
637 }
638
639 /* see brssl.h */
640 void
641 list_names(void)
642 {
643 size_t u;
644
645 printf("Protocol versions:\n");
646 for (u = 0; protocol_versions[u].name; u ++) {
647 printf(" %-8s %s\n",
648 protocol_versions[u].name,
649 protocol_versions[u].comment);
650 }
651 printf("Hash functions:\n");
652 for (u = 0; hash_functions[u].name; u ++) {
653 printf(" %-8s %s\n",
654 hash_functions[u].name,
655 hash_functions[u].comment);
656 }
657 printf("Cipher suites:\n");
658 for (u = 0; cipher_suites[u].name; u ++) {
659 printf(" %s\n %s\n",
660 cipher_suites[u].name,
661 cipher_suites[u].comment);
662 }
663 }
664
665 /* see brssl.h */
666 void
667 list_curves(void)
668 {
669 size_t u;
670 for (u = 0; curves[u].name; u ++) {
671 size_t v;
672
673 for (v = 0; curves[u].sid[v]; v ++) {
674 if (v == 0) {
675 printf(" ");
676 } else if (v == 1) {
677 printf(" (");
678 } else {
679 printf(", ");
680 }
681 printf("%s", curves[u].sid[v]);
682 }
683 if (v > 1) {
684 printf(")");
685 }
686 printf("\n");
687 }
688 }
689
690 static int
691 is_ign(int c)
692 {
693 if (c == 0) {
694 return 0;
695 }
696 if (c <= 32 || c == '-' || c == '_' || c == '.'
697 || c == '/' || c == '+' || c == ':')
698 {
699 return 1;
700 }
701 return 0;
702 }
703
704 /*
705 * Get next non-ignored character, normalised:
706 * ASCII letters are converted to lowercase
707 * control characters, space, '-', '_', '.', '/', '+' and ':' are ignored
708 * A terminating zero is returned as 0.
709 */
710 static int
711 next_char(const char **ps, const char *limit)
712 {
713 for (;;) {
714 int c;
715
716 if (*ps == limit) {
717 return 0;
718 }
719 c = *(*ps) ++;
720 if (c == 0) {
721 return 0;
722 }
723 if (c >= 'A' && c <= 'Z') {
724 c += 'a' - 'A';
725 }
726 if (!is_ign(c)) {
727 return c;
728 }
729 }
730 }
731
732 /*
733 * Partial string equality comparison, with normalisation.
734 */
735 static int
736 eqstr_chunk(const char *s1, size_t s1_len, const char *s2, size_t s2_len)
737 {
738 const char *lim1, *lim2;
739
740 lim1 = s1 + s1_len;
741 lim2 = s2 + s2_len;
742 for (;;) {
743 int c1, c2;
744
745 c1 = next_char(&s1, lim1);
746 c2 = next_char(&s2, lim2);
747 if (c1 != c2) {
748 return 0;
749 }
750 if (c1 == 0) {
751 return 1;
752 }
753 }
754 }
755
756 /* see brssl.h */
757 int
758 eqstr(const char *s1, const char *s2)
759 {
760 return eqstr_chunk(s1, strlen(s1), s2, strlen(s2));
761 }
762
763 static int
764 hexval(int c)
765 {
766 if (c >= '0' && c <= '9') {
767 return c - '0';
768 } else if (c >= 'A' && c <= 'F') {
769 return c - 'A' + 10;
770 } else if (c >= 'a' && c <= 'f') {
771 return c - 'a' + 10;
772 } else {
773 return -1;
774 }
775 }
776
777 /* see brssl.h */
778 size_t
779 parse_size(const char *s)
780 {
781 int radix;
782 size_t acc;
783 const char *t;
784
785 t = s;
786 if (t[0] == '0' && (t[1] == 'x' || t[1] == 'X')) {
787 radix = 16;
788 t += 2;
789 } else {
790 radix = 10;
791 }
792 acc = 0;
793 for (;;) {
794 int c, d;
795 size_t z;
796
797 c = *t ++;
798 if (c == 0) {
799 return acc;
800 }
801 d = hexval(c);
802 if (d < 0 || d >= radix) {
803 fprintf(stderr, "ERROR: not a valid digit: '%c'\n", c);
804 return (size_t)-1;
805 }
806 z = acc * (size_t)radix + (size_t)d;
807 if (z < (size_t)d || (z / (size_t)radix) != acc
808 || z == (size_t)-1)
809 {
810 fprintf(stderr, "ERROR: value too large: %s\n", s);
811 return (size_t)-1;
812 }
813 acc = z;
814 }
815 }
816
817 /*
818 * Comma-separated list enumeration. This returns a pointer to the first
819 * word in the string, skipping leading ignored characters. '*len' is
820 * set to the word length (not counting trailing ignored characters).
821 * '*str' is updated to point to immediately after the next comma, or to
822 * the terminating zero, whichever comes first.
823 *
824 * Empty words are skipped. If there is no next non-empty word, then this
825 * function returns NULL and sets *len to 0.
826 */
827 static const char *
828 next_word(const char **str, size_t *len)
829 {
830 int c;
831 const char *begin;
832 size_t u;
833
834 /*
835 * Find next non-ignored character which is not a comma.
836 */
837 for (;;) {
838 c = **str;
839 if (c == 0) {
840 *len = 0;
841 return NULL;
842 }
843 if (!is_ign(c) && c != ',') {
844 break;
845 }
846 (*str) ++;
847 }
848
849 /*
850 * Find next comma or terminator.
851 */
852 begin = *str;
853 for (;;) {
854 c = *(*str);
855 if (c == 0 || c == ',') {
856 break;
857 }
858 (*str) ++;
859 }
860
861 /*
862 * Remove trailing ignored characters.
863 */
864 u = (size_t)(*str - begin);
865 while (u > 0 && is_ign(begin[u - 1])) {
866 u --;
867 }
868 if (c == ',') {
869 (*str) ++;
870 }
871 *len = u;
872 return begin;
873 }
874
875 /* see brssl.h */
876 unsigned
877 parse_version(const char *name, size_t len)
878 {
879 size_t u;
880
881 for (u = 0;; u ++) {
882 const char *ref;
883
884 ref = protocol_versions[u].name;
885 if (ref == NULL) {
886 fprintf(stderr, "ERROR: unrecognised protocol"
887 " version name: '%s'\n", name);
888 return 0;
889 }
890 if (eqstr_chunk(ref, strlen(ref), name, len)) {
891 return protocol_versions[u].version;
892 }
893 }
894 }
895
896 /* see brssl.h */
897 unsigned
898 parse_hash_functions(const char *arg)
899 {
900 unsigned r;
901
902 r = 0;
903 for (;;) {
904 const char *name;
905 size_t len;
906 size_t u;
907
908 name = next_word(&arg, &len);
909 if (name == NULL) {
910 break;
911 }
912 for (u = 0;; u ++) {
913 const char *ref;
914
915 ref = hash_functions[u].name;
916 if (ref == 0) {
917 fprintf(stderr, "ERROR: unrecognised"
918 " hash function name: '");
919 fwrite(name, 1, len, stderr);
920 fprintf(stderr, "'\n");
921 return 0;
922 }
923 if (eqstr_chunk(ref, strlen(ref), name, len)) {
924 int id;
925
926 id = (hash_functions[u].hclass->desc
927 >> BR_HASHDESC_ID_OFF)
928 & BR_HASHDESC_ID_MASK;
929 r |= (unsigned)1 << id;
930 break;
931 }
932 }
933 }
934 if (r == 0) {
935 fprintf(stderr, "ERROR: no hash function name provided\n");
936 }
937 return r;
938 }
939
940 /* see brssl.h */
941 cipher_suite *
942 parse_suites(const char *arg, size_t *num)
943 {
944 VECTOR(cipher_suite) suites = VEC_INIT;
945 cipher_suite *r;
946
947 for (;;) {
948 const char *name;
949 size_t u, len;
950
951 name = next_word(&arg, &len);
952 if (name == NULL) {
953 break;
954 }
955 for (u = 0;; u ++) {
956 const char *ref;
957
958 ref = cipher_suites[u].name;
959 if (ref == NULL) {
960 fprintf(stderr, "ERROR: unrecognised"
961 " cipher suite '");
962 fwrite(name, 1, len, stderr);
963 fprintf(stderr, "'\n");
964 return 0;
965 }
966 if (eqstr_chunk(ref, strlen(ref), name, len)) {
967 VEC_ADD(suites, cipher_suites[u]);
968 break;
969 }
970 }
971 }
972 if (VEC_LEN(suites) == 0) {
973 fprintf(stderr, "ERROR: no cipher suite provided\n");
974 }
975 r = VEC_TOARRAY(suites);
976 *num = VEC_LEN(suites);
977 VEC_CLEAR(suites);
978 return r;
979 }
980
981 /* see brssl.h */
982 const char *
983 ec_curve_name(int curve)
984 {
985 switch (curve) {
986 case BR_EC_sect163k1: return "sect163k1";
987 case BR_EC_sect163r1: return "sect163r1";
988 case BR_EC_sect163r2: return "sect163r2";
989 case BR_EC_sect193r1: return "sect193r1";
990 case BR_EC_sect193r2: return "sect193r2";
991 case BR_EC_sect233k1: return "sect233k1";
992 case BR_EC_sect233r1: return "sect233r1";
993 case BR_EC_sect239k1: return "sect239k1";
994 case BR_EC_sect283k1: return "sect283k1";
995 case BR_EC_sect283r1: return "sect283r1";
996 case BR_EC_sect409k1: return "sect409k1";
997 case BR_EC_sect409r1: return "sect409r1";
998 case BR_EC_sect571k1: return "sect571k1";
999 case BR_EC_sect571r1: return "sect571r1";
1000 case BR_EC_secp160k1: return "secp160k1";
1001 case BR_EC_secp160r1: return "secp160r1";
1002 case BR_EC_secp160r2: return "secp160r2";
1003 case BR_EC_secp192k1: return "secp192k1";
1004 case BR_EC_secp192r1: return "secp192r1";
1005 case BR_EC_secp224k1: return "secp224k1";
1006 case BR_EC_secp224r1: return "secp224r1";
1007 case BR_EC_secp256k1: return "secp256k1";
1008 case BR_EC_secp256r1: return "secp256r1";
1009 case BR_EC_secp384r1: return "secp384r1";
1010 case BR_EC_secp521r1: return "secp521r1";
1011 case BR_EC_brainpoolP256r1: return "brainpoolP256r1";
1012 case BR_EC_brainpoolP384r1: return "brainpoolP384r1";
1013 case BR_EC_brainpoolP512r1: return "brainpoolP512r1";
1014 default:
1015 return "unknown";
1016 }
1017 }
1018
1019 /* see brssl.h */
1020 int
1021 get_curve_by_name(const char *str)
1022 {
1023 size_t u, v;
1024
1025 for (u = 0; curves[u].name; u ++) {
1026 for (v = 0; curves[u].sid[v]; v ++) {
1027 if (eqstr(curves[u].sid[v], str)) {
1028 return curves[u].id;
1029 }
1030 }
1031 }
1032 return -1;
1033 }
1034
1035 /* see brssl.h */
1036 const char *
1037 hash_function_name(int id)
1038 {
1039 switch (id) {
1040 case br_md5sha1_ID: return "MD5+SHA-1";
1041 case br_md5_ID: return "MD5";
1042 case br_sha1_ID: return "SHA-1";
1043 case br_sha224_ID: return "SHA-224";
1044 case br_sha256_ID: return "SHA-256";
1045 case br_sha384_ID: return "SHA-384";
1046 case br_sha512_ID: return "SHA-512";
1047 default:
1048 return "unknown";
1049 }
1050 }