blob: b5fea04ddc747561e2c32a3196903e763320b906 [file] [log] [blame]
Bence Szépkúti700ee442020-05-26 00:33:31 +02001/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02002 * Copyright The Mbed TLS Contributors
Bence Szépkúti86974652020-06-15 11:59:37 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Bence Szépkúti700ee442020-05-26 00:33:31 +020016 */
17
Gilles Peskine3f775262019-02-13 18:42:06 +010018#include <errno.h>
19#include <stdint.h>
Gilles Peskine029b5d62018-07-16 23:13:37 +020020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "psa/crypto.h"
25
Darryl Green608e0912018-10-17 14:48:27 +010026/* This block is present to support Visual Studio builds prior to 2015 */
27#if defined(_MSC_VER) && _MSC_VER < 1900
28#include <stdarg.h>
29int snprintf( char *s, size_t n, const char *fmt, ... )
30{
31 int ret;
32 va_list argp;
33
34 /* Avoid calling the invalid parameter handler by checking ourselves */
35 if( s == NULL || n == 0 || fmt == NULL )
36 return( -1 );
37
38 va_start( argp, fmt );
39#if defined(_TRUNCATE) && !defined(__MINGW32__)
40 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
41#else
42 ret = _vsnprintf( s, n, fmt, argp );
43 if( ret < 0 || (size_t) ret == n )
44 {
45 s[n-1] = '\0';
46 ret = -1;
47 }
48#endif
49 va_end( argp );
50
51 return( ret );
52}
53#endif
54
Gilles Peskine029b5d62018-07-16 23:13:37 +020055static void append(char **buffer, size_t buffer_size,
56 size_t *required_size,
57 const char *string, size_t length)
58{
59 *required_size += length;
60 if (*required_size < buffer_size) {
61 memcpy(*buffer, string, length);
62 *buffer += length;
63 }
64}
65
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020066static void append_integer(char **buffer, size_t buffer_size,
67 size_t *required_size,
68 const char *format /*printf format for value*/,
69 unsigned long value)
70{
71 size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
72 if (n < buffer_size - *required_size) *buffer += n;
73 *required_size += n;
74}
75
Gilles Peskine029b5d62018-07-16 23:13:37 +020076/* The code of these function is automatically generated and included below. */
Paul Elliott8ff510a2020-06-02 17:19:28 +010077static const char *psa_ecc_family_name(psa_ecc_family_t curve);
Paul Elliott75e27032020-06-03 15:17:39 +010078static const char *psa_dh_family_name(psa_dh_family_t group);
Gilles Peskine029b5d62018-07-16 23:13:37 +020079static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
80
81static void append_with_curve(char **buffer, size_t buffer_size,
82 size_t *required_size,
83 const char *string, size_t length,
Paul Elliott8ff510a2020-06-02 17:19:28 +010084 psa_ecc_family_t curve)
Gilles Peskine029b5d62018-07-16 23:13:37 +020085{
Paul Elliott8ff510a2020-06-02 17:19:28 +010086 const char *family_name = psa_ecc_family_name(curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +020087 append(buffer, buffer_size, required_size, string, length);
88 append(buffer, buffer_size, required_size, "(", 1);
Paul Elliott8ff510a2020-06-02 17:19:28 +010089 if (family_name != NULL) {
Gilles Peskine029b5d62018-07-16 23:13:37 +020090 append(buffer, buffer_size, required_size,
Paul Elliott8ff510a2020-06-02 17:19:28 +010091 family_name, strlen(family_name));
Gilles Peskine029b5d62018-07-16 23:13:37 +020092 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020093 append_integer(buffer, buffer_size, required_size,
Gilles Peskinef65ed6f2019-12-04 17:18:41 +010094 "0x%02x", curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +020095 }
96 append(buffer, buffer_size, required_size, ")", 1);
97}
98
Gilles Peskinedcaefae2019-05-16 12:55:35 +020099static void append_with_group(char **buffer, size_t buffer_size,
100 size_t *required_size,
101 const char *string, size_t length,
Paul Elliott75e27032020-06-03 15:17:39 +0100102 psa_dh_family_t group)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200103{
Paul Elliott75e27032020-06-03 15:17:39 +0100104 const char *group_name = psa_dh_family_name(group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200105 append(buffer, buffer_size, required_size, string, length);
106 append(buffer, buffer_size, required_size, "(", 1);
107 if (group_name != NULL) {
108 append(buffer, buffer_size, required_size,
109 group_name, strlen(group_name));
110 } else {
111 append_integer(buffer, buffer_size, required_size,
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100112 "0x%02x", group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200113 }
114 append(buffer, buffer_size, required_size, ")", 1);
115}
116
Gilles Peskine882e57e2019-04-12 00:12:07 +0200117typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
118
119static void append_with_alg(char **buffer, size_t buffer_size,
120 size_t *required_size,
121 psa_get_algorithm_name_func_ptr get_name,
122 psa_algorithm_t alg)
Gilles Peskine029b5d62018-07-16 23:13:37 +0200123{
Gilles Peskine882e57e2019-04-12 00:12:07 +0200124 const char *name = get_name(alg);
125 if (name != NULL) {
Gilles Peskine029b5d62018-07-16 23:13:37 +0200126 append(buffer, buffer_size, required_size,
Gilles Peskine882e57e2019-04-12 00:12:07 +0200127 name, strlen(name));
Gilles Peskine029b5d62018-07-16 23:13:37 +0200128 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +0200129 append_integer(buffer, buffer_size, required_size,
Gilles Peskine882e57e2019-04-12 00:12:07 +0200130 "0x%08lx", alg);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200131 }
Gilles Peskine029b5d62018-07-16 23:13:37 +0200132}
133
134#include "psa_constant_names_generated.c"
135
136static int psa_snprint_status(char *buffer, size_t buffer_size,
137 psa_status_t status)
138{
139 const char *name = psa_strerror(status);
140 if (name == NULL) {
141 return snprintf(buffer, buffer_size, "%ld", (long) status);
142 } else {
143 size_t length = strlen(name);
144 if (length < buffer_size) {
145 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +0100146 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200147 } else {
Darryl Green18246962018-10-17 15:01:45 +0100148 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200149 }
150 }
151}
152
153static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
Paul Elliott8ff510a2020-06-02 17:19:28 +0100154 psa_ecc_family_t curve)
Gilles Peskine029b5d62018-07-16 23:13:37 +0200155{
Paul Elliott8ff510a2020-06-02 17:19:28 +0100156 const char *name = psa_ecc_family_name(curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200157 if (name == NULL) {
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100158 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200159 } else {
160 size_t length = strlen(name);
161 if (length < buffer_size) {
162 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +0100163 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200164 } else {
Darryl Green18246962018-10-17 15:01:45 +0100165 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200166 }
167 }
168}
169
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200170static int psa_snprint_dh_group(char *buffer, size_t buffer_size,
Paul Elliott75e27032020-06-03 15:17:39 +0100171 psa_dh_family_t group)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200172{
Paul Elliott75e27032020-06-03 15:17:39 +0100173 const char *name = psa_dh_family_name(group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200174 if (name == NULL) {
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100175 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200176 } else {
177 size_t length = strlen(name);
178 if (length < buffer_size) {
179 memcpy(buffer, name, length + 1);
180 return (int) length;
181 } else {
182 return (int) buffer_size;
183 }
184 }
185}
186
Gilles Peskine029b5d62018-07-16 23:13:37 +0200187static void usage(const char *program_name)
188{
Gilles Peskine567840e2018-09-25 18:27:53 +0200189 printf("Usage: %s TYPE VALUE [VALUE...]\n",
Gilles Peskine029b5d62018-07-16 23:13:37 +0200190 program_name == NULL ? "psa_constant_names" : program_name);
191 printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
192 printf("Supported types (with = between aliases):\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200193 printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
Paul Elliott8ff510a2020-06-02 17:19:28 +0100194 printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_family_t)\n");
Paul Elliott75e27032020-06-03 15:17:39 +0100195 printf(" group=dh_group Diffie-Hellman group identifier (psa_dh_family_t)\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200196 printf(" type=key_type Key type (psa_key_type_t)\n");
Gilles Peskine029b5d62018-07-16 23:13:37 +0200197 printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
198 printf(" error=status Status code (psa_status_t)\n");
199}
200
Gilles Peskine567840e2018-09-25 18:27:53 +0200201typedef enum {
202 TYPE_STATUS,
Gilles Peskine3f775262019-02-13 18:42:06 +0100203} signed_value_type;
204
205int process_signed(signed_value_type type, long min, long max, char **argp)
206{
207 for (; *argp != NULL; argp++) {
208 char buffer[200];
209 char *end;
210 long value = strtol(*argp, &end, 0);
211 if (*end) {
212 printf("Non-numeric value: %s\n", *argp);
213 return EXIT_FAILURE;
214 }
215 if (value < min || (errno == ERANGE && value < 0)) {
216 printf("Value too small: %s\n", *argp);
217 return EXIT_FAILURE;
218 }
219 if (value > max || (errno == ERANGE && value > 0)) {
220 printf("Value too large: %s\n", *argp);
221 return EXIT_FAILURE;
222 }
223
224 switch (type) {
225 case TYPE_STATUS:
226 psa_snprint_status(buffer, sizeof(buffer),
227 (psa_status_t) value);
228 break;
229 }
230 puts(buffer);
231 }
232
233 return EXIT_SUCCESS;
234}
235
236typedef enum {
Gilles Peskine567840e2018-09-25 18:27:53 +0200237 TYPE_ALGORITHM,
238 TYPE_ECC_CURVE,
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200239 TYPE_DH_GROUP,
Gilles Peskine567840e2018-09-25 18:27:53 +0200240 TYPE_KEY_TYPE,
241 TYPE_KEY_USAGE,
Gilles Peskine1b879842019-02-13 18:40:50 +0100242} unsigned_value_type;
Gilles Peskine567840e2018-09-25 18:27:53 +0200243
Gilles Peskine1b879842019-02-13 18:40:50 +0100244int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
Gilles Peskine029b5d62018-07-16 23:13:37 +0200245{
Gilles Peskine1b879842019-02-13 18:40:50 +0100246 for (; *argp != NULL; argp++) {
Gilles Peskine567840e2018-09-25 18:27:53 +0200247 char buffer[200];
248 char *end;
Gilles Peskine1b879842019-02-13 18:40:50 +0100249 unsigned long value = strtoul(*argp, &end, 0);
Gilles Peskine567840e2018-09-25 18:27:53 +0200250 if (*end) {
Gilles Peskine1b879842019-02-13 18:40:50 +0100251 printf("Non-numeric value: %s\n", *argp);
Gilles Peskine567840e2018-09-25 18:27:53 +0200252 return EXIT_FAILURE;
253 }
Gilles Peskine3f775262019-02-13 18:42:06 +0100254 if (value > max || errno == ERANGE) {
Gilles Peskine1b879842019-02-13 18:40:50 +0100255 printf("Value out of range: %s\n", *argp);
Gilles Peskine265a1712018-10-31 14:52:28 +0100256 return EXIT_FAILURE;
257 }
Gilles Peskine567840e2018-09-25 18:27:53 +0200258
259 switch (type) {
Gilles Peskine567840e2018-09-25 18:27:53 +0200260 case TYPE_ALGORITHM:
261 psa_snprint_algorithm(buffer, sizeof(buffer),
262 (psa_algorithm_t) value);
263 break;
264 case TYPE_ECC_CURVE:
265 psa_snprint_ecc_curve(buffer, sizeof(buffer),
Paul Elliott8ff510a2020-06-02 17:19:28 +0100266 (psa_ecc_family_t) value);
Gilles Peskine567840e2018-09-25 18:27:53 +0200267 break;
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200268 case TYPE_DH_GROUP:
269 psa_snprint_dh_group(buffer, sizeof(buffer),
Paul Elliott75e27032020-06-03 15:17:39 +0100270 (psa_dh_family_t) value);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200271 break;
Gilles Peskine567840e2018-09-25 18:27:53 +0200272 case TYPE_KEY_TYPE:
273 psa_snprint_key_type(buffer, sizeof(buffer),
274 (psa_key_type_t) value);
275 break;
276 case TYPE_KEY_USAGE:
277 psa_snprint_key_usage(buffer, sizeof(buffer),
278 (psa_key_usage_t) value);
279 break;
280 }
281 puts(buffer);
282 }
283
Gilles Peskine029b5d62018-07-16 23:13:37 +0200284 return EXIT_SUCCESS;
285}
Gilles Peskine1b879842019-02-13 18:40:50 +0100286
287int main(int argc, char *argv[])
288{
289 if (argc <= 1 ||
290 !strcmp(argv[1], "help") ||
291 !strcmp(argv[1], "--help"))
292 {
293 usage(argv[0]);
294 return EXIT_FAILURE;
295 }
296
297 if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
Gilles Peskine3f775262019-02-13 18:42:06 +0100298 /* There's no way to obtain the actual range of a signed type,
299 * so hard-code it here: psa_status_t is int32_t. */
300 return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
301 argv + 2);
Gilles Peskine1b879842019-02-13 18:40:50 +0100302 } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
303 return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
304 argv + 2);
305 } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100306 return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_family_t) (-1),
Gilles Peskine1b879842019-02-13 18:40:50 +0100307 argv + 2);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200308 } else if (!strcmp(argv[1], "group") || !strcmp(argv[1], "dh_group")) {
Paul Elliott75e27032020-06-03 15:17:39 +0100309 return process_unsigned(TYPE_DH_GROUP, (psa_dh_family_t) (-1),
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200310 argv + 2);
Gilles Peskine1b879842019-02-13 18:40:50 +0100311 } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
312 return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
313 argv + 2);
314 } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
315 return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
316 argv + 2);
317 } else {
318 printf("Unknown type: %s\n", argv[1]);
319 return EXIT_FAILURE;
320 }
321}