1 /*
2 * Copyright The Mbed TLS Contributors
3 * 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.
16 */
17
18 #include <errno.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "psa/crypto.h"
25
26 /* This block is present to support Visual Studio builds prior to 2015 */
27 #if defined(_MSC_VER) && _MSC_VER < 1900
28 #include <stdarg.h>
snprintf(char * s,size_t n,const char * fmt,...)29 int 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
55 /* There are different GET_HASH macros for different kinds of algorithms
56 * built from hashes, but the values are all constructed on the
57 * same model. */
58 #define PSA_ALG_GET_HASH(alg) \
59 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
60
append(char ** buffer,size_t buffer_size,size_t * required_size,const char * string,size_t length)61 static void append(char **buffer, size_t buffer_size,
62 size_t *required_size,
63 const char *string, size_t length)
64 {
65 *required_size += length;
66 if (*required_size < buffer_size) {
67 memcpy(*buffer, string, length);
68 *buffer += length;
69 }
70 }
71
append_integer(char ** buffer,size_t buffer_size,size_t * required_size,const char * format,unsigned long value)72 static void append_integer(char **buffer, size_t buffer_size,
73 size_t *required_size,
74 const char *format /*printf format for value*/,
75 unsigned long value)
76 {
77 size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
78 if (n < buffer_size - *required_size) *buffer += n;
79 *required_size += n;
80 }
81
82 /* The code of these function is automatically generated and included below. */
83 static const char *psa_ecc_family_name(psa_ecc_family_t curve);
84 static const char *psa_dh_family_name(psa_dh_family_t group);
85 static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
86
append_with_curve(char ** buffer,size_t buffer_size,size_t * required_size,const char * string,size_t length,psa_ecc_family_t curve)87 static void append_with_curve(char **buffer, size_t buffer_size,
88 size_t *required_size,
89 const char *string, size_t length,
90 psa_ecc_family_t curve)
91 {
92 const char *family_name = psa_ecc_family_name(curve);
93 append(buffer, buffer_size, required_size, string, length);
94 append(buffer, buffer_size, required_size, "(", 1);
95 if (family_name != NULL) {
96 append(buffer, buffer_size, required_size,
97 family_name, strlen(family_name));
98 } else {
99 append_integer(buffer, buffer_size, required_size,
100 "0x%02x", curve);
101 }
102 append(buffer, buffer_size, required_size, ")", 1);
103 }
104
append_with_group(char ** buffer,size_t buffer_size,size_t * required_size,const char * string,size_t length,psa_dh_family_t group)105 static void append_with_group(char **buffer, size_t buffer_size,
106 size_t *required_size,
107 const char *string, size_t length,
108 psa_dh_family_t group)
109 {
110 const char *group_name = psa_dh_family_name(group);
111 append(buffer, buffer_size, required_size, string, length);
112 append(buffer, buffer_size, required_size, "(", 1);
113 if (group_name != NULL) {
114 append(buffer, buffer_size, required_size,
115 group_name, strlen(group_name));
116 } else {
117 append_integer(buffer, buffer_size, required_size,
118 "0x%02x", group);
119 }
120 append(buffer, buffer_size, required_size, ")", 1);
121 }
122
123 typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
124
append_with_alg(char ** buffer,size_t buffer_size,size_t * required_size,psa_get_algorithm_name_func_ptr get_name,psa_algorithm_t alg)125 static void append_with_alg(char **buffer, size_t buffer_size,
126 size_t *required_size,
127 psa_get_algorithm_name_func_ptr get_name,
128 psa_algorithm_t alg)
129 {
130 const char *name = get_name(alg);
131 if (name != NULL) {
132 append(buffer, buffer_size, required_size,
133 name, strlen(name));
134 } else {
135 append_integer(buffer, buffer_size, required_size,
136 "0x%08lx", alg);
137 }
138 }
139
140 #include "psa_constant_names_generated.c"
141
psa_snprint_status(char * buffer,size_t buffer_size,psa_status_t status)142 static int psa_snprint_status(char *buffer, size_t buffer_size,
143 psa_status_t status)
144 {
145 const char *name = psa_strerror(status);
146 if (name == NULL) {
147 return snprintf(buffer, buffer_size, "%ld", (long) status);
148 } else {
149 size_t length = strlen(name);
150 if (length < buffer_size) {
151 memcpy(buffer, name, length + 1);
152 return (int) length;
153 } else {
154 return (int) buffer_size;
155 }
156 }
157 }
158
psa_snprint_ecc_curve(char * buffer,size_t buffer_size,psa_ecc_family_t curve)159 static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
160 psa_ecc_family_t curve)
161 {
162 const char *name = psa_ecc_family_name(curve);
163 if (name == NULL) {
164 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) curve);
165 } else {
166 size_t length = strlen(name);
167 if (length < buffer_size) {
168 memcpy(buffer, name, length + 1);
169 return (int) length;
170 } else {
171 return (int) buffer_size;
172 }
173 }
174 }
175
psa_snprint_dh_group(char * buffer,size_t buffer_size,psa_dh_family_t group)176 static int psa_snprint_dh_group(char *buffer, size_t buffer_size,
177 psa_dh_family_t group)
178 {
179 const char *name = psa_dh_family_name(group);
180 if (name == NULL) {
181 return snprintf(buffer, buffer_size, "0x%02x", (unsigned) group);
182 } else {
183 size_t length = strlen(name);
184 if (length < buffer_size) {
185 memcpy(buffer, name, length + 1);
186 return (int) length;
187 } else {
188 return (int) buffer_size;
189 }
190 }
191 }
192
usage(const char * program_name)193 static void usage(const char *program_name)
194 {
195 printf("Usage: %s TYPE VALUE [VALUE...]\n",
196 program_name == NULL ? "psa_constant_names" : program_name);
197 printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
198 printf("Supported types (with = between aliases):\n");
199 printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
200 printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_family_t)\n");
201 printf(" group=dh_group Diffie-Hellman group identifier (psa_dh_family_t)\n");
202 printf(" type=key_type Key type (psa_key_type_t)\n");
203 printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
204 printf(" error=status Status code (psa_status_t)\n");
205 }
206
207 typedef enum {
208 TYPE_STATUS,
209 } signed_value_type;
210
process_signed(signed_value_type type,long min,long max,char ** argp)211 int process_signed(signed_value_type type, long min, long max, char **argp)
212 {
213 for (; *argp != NULL; argp++) {
214 char buffer[200];
215 char *end;
216 long value = strtol(*argp, &end, 0);
217 if (*end) {
218 printf("Non-numeric value: %s\n", *argp);
219 return EXIT_FAILURE;
220 }
221 if (value < min || (errno == ERANGE && value < 0)) {
222 printf("Value too small: %s\n", *argp);
223 return EXIT_FAILURE;
224 }
225 if (value > max || (errno == ERANGE && value > 0)) {
226 printf("Value too large: %s\n", *argp);
227 return EXIT_FAILURE;
228 }
229
230 switch (type) {
231 case TYPE_STATUS:
232 psa_snprint_status(buffer, sizeof(buffer),
233 (psa_status_t) value);
234 break;
235 }
236 puts(buffer);
237 }
238
239 return EXIT_SUCCESS;
240 }
241
242 typedef enum {
243 TYPE_ALGORITHM,
244 TYPE_ECC_CURVE,
245 TYPE_DH_GROUP,
246 TYPE_KEY_TYPE,
247 TYPE_KEY_USAGE,
248 } unsigned_value_type;
249
process_unsigned(unsigned_value_type type,unsigned long max,char ** argp)250 int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
251 {
252 for (; *argp != NULL; argp++) {
253 char buffer[200];
254 char *end;
255 unsigned long value = strtoul(*argp, &end, 0);
256 if (*end) {
257 printf("Non-numeric value: %s\n", *argp);
258 return EXIT_FAILURE;
259 }
260 if (value > max || errno == ERANGE) {
261 printf("Value out of range: %s\n", *argp);
262 return EXIT_FAILURE;
263 }
264
265 switch (type) {
266 case TYPE_ALGORITHM:
267 psa_snprint_algorithm(buffer, sizeof(buffer),
268 (psa_algorithm_t) value);
269 break;
270 case TYPE_ECC_CURVE:
271 psa_snprint_ecc_curve(buffer, sizeof(buffer),
272 (psa_ecc_family_t) value);
273 break;
274 case TYPE_DH_GROUP:
275 psa_snprint_dh_group(buffer, sizeof(buffer),
276 (psa_dh_family_t) value);
277 break;
278 case TYPE_KEY_TYPE:
279 psa_snprint_key_type(buffer, sizeof(buffer),
280 (psa_key_type_t) value);
281 break;
282 case TYPE_KEY_USAGE:
283 psa_snprint_key_usage(buffer, sizeof(buffer),
284 (psa_key_usage_t) value);
285 break;
286 }
287 puts(buffer);
288 }
289
290 return EXIT_SUCCESS;
291 }
292
main(int argc,char * argv[])293 int main(int argc, char *argv[])
294 {
295 if (argc <= 1 ||
296 !strcmp(argv[1], "help") ||
297 !strcmp(argv[1], "--help"))
298 {
299 usage(argv[0]);
300 return EXIT_FAILURE;
301 }
302
303 if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
304 /* There's no way to obtain the actual range of a signed type,
305 * so hard-code it here: psa_status_t is int32_t. */
306 return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
307 argv + 2);
308 } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
309 return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
310 argv + 2);
311 } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
312 return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_family_t) (-1),
313 argv + 2);
314 } else if (!strcmp(argv[1], "group") || !strcmp(argv[1], "dh_group")) {
315 return process_unsigned(TYPE_DH_GROUP, (psa_dh_family_t) (-1),
316 argv + 2);
317 } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
318 return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
319 argv + 2);
320 } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
321 return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
322 argv + 2);
323 } else {
324 printf("Unknown type: %s\n", argv[1]);
325 return EXIT_FAILURE;
326 }
327 }
328