blob: 5fc4f9e4ed2f6ac224696ffb922ff70f1656028e [file] [log] [blame]
Bence Szépkúti700ee442020-05-26 00:33:31 +02001/*
2 * Copyright (C) 2018-2019, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
18 */
19
Gilles Peskine3f775262019-02-13 18:42:06 +010020#include <errno.h>
21#include <stdint.h>
Gilles Peskine029b5d62018-07-16 23:13:37 +020022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "psa/crypto.h"
27
Darryl Green608e0912018-10-17 14:48:27 +010028/* This block is present to support Visual Studio builds prior to 2015 */
29#if defined(_MSC_VER) && _MSC_VER < 1900
30#include <stdarg.h>
31int snprintf( char *s, size_t n, const char *fmt, ... )
32{
33 int ret;
34 va_list argp;
35
36 /* Avoid calling the invalid parameter handler by checking ourselves */
37 if( s == NULL || n == 0 || fmt == NULL )
38 return( -1 );
39
40 va_start( argp, fmt );
41#if defined(_TRUNCATE) && !defined(__MINGW32__)
42 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
43#else
44 ret = _vsnprintf( s, n, fmt, argp );
45 if( ret < 0 || (size_t) ret == n )
46 {
47 s[n-1] = '\0';
48 ret = -1;
49 }
50#endif
51 va_end( argp );
52
53 return( ret );
54}
55#endif
56
Gilles Peskine029b5d62018-07-16 23:13:37 +020057/* There are different GET_HASH macros for different kinds of algorithms
58 * built from hashes, but the values are all constructed on the
59 * same model. */
60#define PSA_ALG_GET_HASH(alg) \
61 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
62
63static void append(char **buffer, size_t buffer_size,
64 size_t *required_size,
65 const char *string, size_t length)
66{
67 *required_size += length;
68 if (*required_size < buffer_size) {
69 memcpy(*buffer, string, length);
70 *buffer += length;
71 }
72}
73
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020074static void append_integer(char **buffer, size_t buffer_size,
75 size_t *required_size,
76 const char *format /*printf format for value*/,
77 unsigned long value)
78{
79 size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
80 if (n < buffer_size - *required_size) *buffer += n;
81 *required_size += n;
82}
83
Gilles Peskine029b5d62018-07-16 23:13:37 +020084/* The code of these function is automatically generated and included below. */
Paul Elliott8ff510a2020-06-02 17:19:28 +010085static const char *psa_ecc_family_name(psa_ecc_family_t curve);
Paul Elliott75e27032020-06-03 15:17:39 +010086static const char *psa_dh_family_name(psa_dh_family_t group);
Gilles Peskine029b5d62018-07-16 23:13:37 +020087static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
88
89static void append_with_curve(char **buffer, size_t buffer_size,
90 size_t *required_size,
91 const char *string, size_t length,
Paul Elliott8ff510a2020-06-02 17:19:28 +010092 psa_ecc_family_t curve)
Gilles Peskine029b5d62018-07-16 23:13:37 +020093{
Paul Elliott8ff510a2020-06-02 17:19:28 +010094 const char *family_name = psa_ecc_family_name(curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +020095 append(buffer, buffer_size, required_size, string, length);
96 append(buffer, buffer_size, required_size, "(", 1);
Paul Elliott8ff510a2020-06-02 17:19:28 +010097 if (family_name != NULL) {
Gilles Peskine029b5d62018-07-16 23:13:37 +020098 append(buffer, buffer_size, required_size,
Paul Elliott8ff510a2020-06-02 17:19:28 +010099 family_name, strlen(family_name));
Gilles Peskine029b5d62018-07-16 23:13:37 +0200100 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +0200101 append_integer(buffer, buffer_size, required_size,
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100102 "0x%02x", curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200103 }
104 append(buffer, buffer_size, required_size, ")", 1);
105}
106
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200107static void append_with_group(char **buffer, size_t buffer_size,
108 size_t *required_size,
109 const char *string, size_t length,
Paul Elliott75e27032020-06-03 15:17:39 +0100110 psa_dh_family_t group)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200111{
Paul Elliott75e27032020-06-03 15:17:39 +0100112 const char *group_name = psa_dh_family_name(group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200113 append(buffer, buffer_size, required_size, string, length);
114 append(buffer, buffer_size, required_size, "(", 1);
115 if (group_name != NULL) {
116 append(buffer, buffer_size, required_size,
117 group_name, strlen(group_name));
118 } else {
119 append_integer(buffer, buffer_size, required_size,
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100120 "0x%02x", group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200121 }
122 append(buffer, buffer_size, required_size, ")", 1);
123}
124
Gilles Peskine882e57e2019-04-12 00:12:07 +0200125typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
126
127static void append_with_alg(char **buffer, size_t buffer_size,
128 size_t *required_size,
129 psa_get_algorithm_name_func_ptr get_name,
130 psa_algorithm_t alg)
Gilles Peskine029b5d62018-07-16 23:13:37 +0200131{
Gilles Peskine882e57e2019-04-12 00:12:07 +0200132 const char *name = get_name(alg);
133 if (name != NULL) {
Gilles Peskine029b5d62018-07-16 23:13:37 +0200134 append(buffer, buffer_size, required_size,
Gilles Peskine882e57e2019-04-12 00:12:07 +0200135 name, strlen(name));
Gilles Peskine029b5d62018-07-16 23:13:37 +0200136 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +0200137 append_integer(buffer, buffer_size, required_size,
Gilles Peskine882e57e2019-04-12 00:12:07 +0200138 "0x%08lx", alg);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200139 }
Gilles Peskine029b5d62018-07-16 23:13:37 +0200140}
141
142#include "psa_constant_names_generated.c"
143
144static int psa_snprint_status(char *buffer, size_t buffer_size,
145 psa_status_t status)
146{
147 const char *name = psa_strerror(status);
148 if (name == NULL) {
149 return snprintf(buffer, buffer_size, "%ld", (long) status);
150 } else {
151 size_t length = strlen(name);
152 if (length < buffer_size) {
153 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +0100154 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200155 } else {
Darryl Green18246962018-10-17 15:01:45 +0100156 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200157 }
158 }
159}
160
161static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
Paul Elliott8ff510a2020-06-02 17:19:28 +0100162 psa_ecc_family_t curve)
Gilles Peskine029b5d62018-07-16 23:13:37 +0200163{
Paul Elliott8ff510a2020-06-02 17:19:28 +0100164 const char *name = psa_ecc_family_name(curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200165 if (name == NULL) {
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100166 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200167 } else {
168 size_t length = strlen(name);
169 if (length < buffer_size) {
170 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +0100171 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200172 } else {
Darryl Green18246962018-10-17 15:01:45 +0100173 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200174 }
175 }
176}
177
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200178static int psa_snprint_dh_group(char *buffer, size_t buffer_size,
Paul Elliott75e27032020-06-03 15:17:39 +0100179 psa_dh_family_t group)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200180{
Paul Elliott75e27032020-06-03 15:17:39 +0100181 const char *name = psa_dh_family_name(group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200182 if (name == NULL) {
Gilles Peskinef65ed6f2019-12-04 17:18:41 +0100183 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) group);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200184 } else {
185 size_t length = strlen(name);
186 if (length < buffer_size) {
187 memcpy(buffer, name, length + 1);
188 return (int) length;
189 } else {
190 return (int) buffer_size;
191 }
192 }
193}
194
Gilles Peskine029b5d62018-07-16 23:13:37 +0200195static void usage(const char *program_name)
196{
Gilles Peskine567840e2018-09-25 18:27:53 +0200197 printf("Usage: %s TYPE VALUE [VALUE...]\n",
Gilles Peskine029b5d62018-07-16 23:13:37 +0200198 program_name == NULL ? "psa_constant_names" : program_name);
199 printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
200 printf("Supported types (with = between aliases):\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200201 printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
Paul Elliott8ff510a2020-06-02 17:19:28 +0100202 printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_family_t)\n");
Paul Elliott75e27032020-06-03 15:17:39 +0100203 printf(" group=dh_group Diffie-Hellman group identifier (psa_dh_family_t)\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200204 printf(" type=key_type Key type (psa_key_type_t)\n");
Gilles Peskine029b5d62018-07-16 23:13:37 +0200205 printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
206 printf(" error=status Status code (psa_status_t)\n");
207}
208
Gilles Peskine567840e2018-09-25 18:27:53 +0200209typedef enum {
210 TYPE_STATUS,
Gilles Peskine3f775262019-02-13 18:42:06 +0100211} signed_value_type;
212
213int process_signed(signed_value_type type, long min, long max, char **argp)
214{
215 for (; *argp != NULL; argp++) {
216 char buffer[200];
217 char *end;
218 long value = strtol(*argp, &end, 0);
219 if (*end) {
220 printf("Non-numeric value: %s\n", *argp);
221 return EXIT_FAILURE;
222 }
223 if (value < min || (errno == ERANGE && value < 0)) {
224 printf("Value too small: %s\n", *argp);
225 return EXIT_FAILURE;
226 }
227 if (value > max || (errno == ERANGE && value > 0)) {
228 printf("Value too large: %s\n", *argp);
229 return EXIT_FAILURE;
230 }
231
232 switch (type) {
233 case TYPE_STATUS:
234 psa_snprint_status(buffer, sizeof(buffer),
235 (psa_status_t) value);
236 break;
237 }
238 puts(buffer);
239 }
240
241 return EXIT_SUCCESS;
242}
243
244typedef enum {
Gilles Peskine567840e2018-09-25 18:27:53 +0200245 TYPE_ALGORITHM,
246 TYPE_ECC_CURVE,
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200247 TYPE_DH_GROUP,
Gilles Peskine567840e2018-09-25 18:27:53 +0200248 TYPE_KEY_TYPE,
249 TYPE_KEY_USAGE,
Gilles Peskine1b879842019-02-13 18:40:50 +0100250} unsigned_value_type;
Gilles Peskine567840e2018-09-25 18:27:53 +0200251
Gilles Peskine1b879842019-02-13 18:40:50 +0100252int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
Gilles Peskine029b5d62018-07-16 23:13:37 +0200253{
Gilles Peskine1b879842019-02-13 18:40:50 +0100254 for (; *argp != NULL; argp++) {
Gilles Peskine567840e2018-09-25 18:27:53 +0200255 char buffer[200];
256 char *end;
Gilles Peskine1b879842019-02-13 18:40:50 +0100257 unsigned long value = strtoul(*argp, &end, 0);
Gilles Peskine567840e2018-09-25 18:27:53 +0200258 if (*end) {
Gilles Peskine1b879842019-02-13 18:40:50 +0100259 printf("Non-numeric value: %s\n", *argp);
Gilles Peskine567840e2018-09-25 18:27:53 +0200260 return EXIT_FAILURE;
261 }
Gilles Peskine3f775262019-02-13 18:42:06 +0100262 if (value > max || errno == ERANGE) {
Gilles Peskine1b879842019-02-13 18:40:50 +0100263 printf("Value out of range: %s\n", *argp);
Gilles Peskine265a1712018-10-31 14:52:28 +0100264 return EXIT_FAILURE;
265 }
Gilles Peskine567840e2018-09-25 18:27:53 +0200266
267 switch (type) {
Gilles Peskine567840e2018-09-25 18:27:53 +0200268 case TYPE_ALGORITHM:
269 psa_snprint_algorithm(buffer, sizeof(buffer),
270 (psa_algorithm_t) value);
271 break;
272 case TYPE_ECC_CURVE:
273 psa_snprint_ecc_curve(buffer, sizeof(buffer),
Paul Elliott8ff510a2020-06-02 17:19:28 +0100274 (psa_ecc_family_t) value);
Gilles Peskine567840e2018-09-25 18:27:53 +0200275 break;
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200276 case TYPE_DH_GROUP:
277 psa_snprint_dh_group(buffer, sizeof(buffer),
Paul Elliott75e27032020-06-03 15:17:39 +0100278 (psa_dh_family_t) value);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200279 break;
Gilles Peskine567840e2018-09-25 18:27:53 +0200280 case TYPE_KEY_TYPE:
281 psa_snprint_key_type(buffer, sizeof(buffer),
282 (psa_key_type_t) value);
283 break;
284 case TYPE_KEY_USAGE:
285 psa_snprint_key_usage(buffer, sizeof(buffer),
286 (psa_key_usage_t) value);
287 break;
288 }
289 puts(buffer);
290 }
291
Gilles Peskine029b5d62018-07-16 23:13:37 +0200292 return EXIT_SUCCESS;
293}
Gilles Peskine1b879842019-02-13 18:40:50 +0100294
295int main(int argc, char *argv[])
296{
297 if (argc <= 1 ||
298 !strcmp(argv[1], "help") ||
299 !strcmp(argv[1], "--help"))
300 {
301 usage(argv[0]);
302 return EXIT_FAILURE;
303 }
304
305 if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
Gilles Peskine3f775262019-02-13 18:42:06 +0100306 /* There's no way to obtain the actual range of a signed type,
307 * so hard-code it here: psa_status_t is int32_t. */
308 return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
309 argv + 2);
Gilles Peskine1b879842019-02-13 18:40:50 +0100310 } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
311 return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
312 argv + 2);
313 } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100314 return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_family_t) (-1),
Gilles Peskine1b879842019-02-13 18:40:50 +0100315 argv + 2);
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200316 } else if (!strcmp(argv[1], "group") || !strcmp(argv[1], "dh_group")) {
Paul Elliott75e27032020-06-03 15:17:39 +0100317 return process_unsigned(TYPE_DH_GROUP, (psa_dh_family_t) (-1),
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200318 argv + 2);
Gilles Peskine1b879842019-02-13 18:40:50 +0100319 } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
320 return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
321 argv + 2);
322 } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
323 return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
324 argv + 2);
325 } else {
326 printf("Unknown type: %s\n", argv[1]);
327 return EXIT_FAILURE;
328 }
329}