blob: 5240b084a4f167a55f21e239d5eb24736885f10f [file] [log] [blame]
Gilles Peskine3f775262019-02-13 18:42:06 +01001#include <errno.h>
2#include <stdint.h>
Gilles Peskine029b5d62018-07-16 23:13:37 +02003#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#include "psa/crypto.h"
8
Darryl Green608e0912018-10-17 14:48:27 +01009/* This block is present to support Visual Studio builds prior to 2015 */
10#if defined(_MSC_VER) && _MSC_VER < 1900
11#include <stdarg.h>
12int snprintf( char *s, size_t n, const char *fmt, ... )
13{
14 int ret;
15 va_list argp;
16
17 /* Avoid calling the invalid parameter handler by checking ourselves */
18 if( s == NULL || n == 0 || fmt == NULL )
19 return( -1 );
20
21 va_start( argp, fmt );
22#if defined(_TRUNCATE) && !defined(__MINGW32__)
23 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
24#else
25 ret = _vsnprintf( s, n, fmt, argp );
26 if( ret < 0 || (size_t) ret == n )
27 {
28 s[n-1] = '\0';
29 ret = -1;
30 }
31#endif
32 va_end( argp );
33
34 return( ret );
35}
36#endif
37
Gilles Peskine029b5d62018-07-16 23:13:37 +020038/* There are different GET_HASH macros for different kinds of algorithms
39 * built from hashes, but the values are all constructed on the
40 * same model. */
41#define PSA_ALG_GET_HASH(alg) \
42 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
43
44static void append(char **buffer, size_t buffer_size,
45 size_t *required_size,
46 const char *string, size_t length)
47{
48 *required_size += length;
49 if (*required_size < buffer_size) {
50 memcpy(*buffer, string, length);
51 *buffer += length;
52 }
53}
54
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020055static void append_integer(char **buffer, size_t buffer_size,
56 size_t *required_size,
57 const char *format /*printf format for value*/,
58 unsigned long value)
59{
60 size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
61 if (n < buffer_size - *required_size) *buffer += n;
62 *required_size += n;
63}
64
Gilles Peskine029b5d62018-07-16 23:13:37 +020065/* The code of these function is automatically generated and included below. */
66static const char *psa_ecc_curve_name(psa_ecc_curve_t curve);
67static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
68
69static void append_with_curve(char **buffer, size_t buffer_size,
70 size_t *required_size,
71 const char *string, size_t length,
72 psa_ecc_curve_t curve)
73{
74 const char *curve_name = psa_ecc_curve_name(curve);
75 append(buffer, buffer_size, required_size, string, length);
76 append(buffer, buffer_size, required_size, "(", 1);
77 if (curve_name != NULL) {
78 append(buffer, buffer_size, required_size,
79 curve_name, strlen(curve_name));
80 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020081 append_integer(buffer, buffer_size, required_size,
82 "0x%04x", curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +020083 }
84 append(buffer, buffer_size, required_size, ")", 1);
85}
86
Gilles Peskine882e57e2019-04-12 00:12:07 +020087typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
88
89static void append_with_alg(char **buffer, size_t buffer_size,
90 size_t *required_size,
91 psa_get_algorithm_name_func_ptr get_name,
92 psa_algorithm_t alg)
Gilles Peskine029b5d62018-07-16 23:13:37 +020093{
Gilles Peskine882e57e2019-04-12 00:12:07 +020094 const char *name = get_name(alg);
95 if (name != NULL) {
Gilles Peskine029b5d62018-07-16 23:13:37 +020096 append(buffer, buffer_size, required_size,
Gilles Peskine882e57e2019-04-12 00:12:07 +020097 name, strlen(name));
Gilles Peskine029b5d62018-07-16 23:13:37 +020098 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020099 append_integer(buffer, buffer_size, required_size,
Gilles Peskine882e57e2019-04-12 00:12:07 +0200100 "0x%08lx", alg);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200101 }
Gilles Peskine029b5d62018-07-16 23:13:37 +0200102}
103
104#include "psa_constant_names_generated.c"
105
106static int psa_snprint_status(char *buffer, size_t buffer_size,
107 psa_status_t status)
108{
109 const char *name = psa_strerror(status);
110 if (name == NULL) {
111 return snprintf(buffer, buffer_size, "%ld", (long) status);
112 } else {
113 size_t length = strlen(name);
114 if (length < buffer_size) {
115 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +0100116 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200117 } else {
Darryl Green18246962018-10-17 15:01:45 +0100118 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200119 }
120 }
121}
122
123static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
124 psa_ecc_curve_t curve)
125{
126 const char *name = psa_ecc_curve_name(curve);
127 if (name == NULL) {
128 return snprintf(buffer, buffer_size, "0x%04x", (unsigned) curve);
129 } else {
130 size_t length = strlen(name);
131 if (length < buffer_size) {
132 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +0100133 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200134 } else {
Darryl Green18246962018-10-17 15:01:45 +0100135 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200136 }
137 }
138}
139
140static void usage(const char *program_name)
141{
Gilles Peskine567840e2018-09-25 18:27:53 +0200142 printf("Usage: %s TYPE VALUE [VALUE...]\n",
Gilles Peskine029b5d62018-07-16 23:13:37 +0200143 program_name == NULL ? "psa_constant_names" : program_name);
144 printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
145 printf("Supported types (with = between aliases):\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200146 printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
Gilles Peskine029b5d62018-07-16 23:13:37 +0200147 printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_curve_t)\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200148 printf(" type=key_type Key type (psa_key_type_t)\n");
Gilles Peskine029b5d62018-07-16 23:13:37 +0200149 printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
150 printf(" error=status Status code (psa_status_t)\n");
151}
152
Gilles Peskine567840e2018-09-25 18:27:53 +0200153typedef enum {
154 TYPE_STATUS,
Gilles Peskine3f775262019-02-13 18:42:06 +0100155} signed_value_type;
156
157int process_signed(signed_value_type type, long min, long max, char **argp)
158{
159 for (; *argp != NULL; argp++) {
160 char buffer[200];
161 char *end;
162 long value = strtol(*argp, &end, 0);
163 if (*end) {
164 printf("Non-numeric value: %s\n", *argp);
165 return EXIT_FAILURE;
166 }
167 if (value < min || (errno == ERANGE && value < 0)) {
168 printf("Value too small: %s\n", *argp);
169 return EXIT_FAILURE;
170 }
171 if (value > max || (errno == ERANGE && value > 0)) {
172 printf("Value too large: %s\n", *argp);
173 return EXIT_FAILURE;
174 }
175
176 switch (type) {
177 case TYPE_STATUS:
178 psa_snprint_status(buffer, sizeof(buffer),
179 (psa_status_t) value);
180 break;
181 }
182 puts(buffer);
183 }
184
185 return EXIT_SUCCESS;
186}
187
188typedef enum {
Gilles Peskine567840e2018-09-25 18:27:53 +0200189 TYPE_ALGORITHM,
190 TYPE_ECC_CURVE,
191 TYPE_KEY_TYPE,
192 TYPE_KEY_USAGE,
Gilles Peskine1b879842019-02-13 18:40:50 +0100193} unsigned_value_type;
Gilles Peskine567840e2018-09-25 18:27:53 +0200194
Gilles Peskine1b879842019-02-13 18:40:50 +0100195int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
Gilles Peskine029b5d62018-07-16 23:13:37 +0200196{
Gilles Peskine1b879842019-02-13 18:40:50 +0100197 for (; *argp != NULL; argp++) {
Gilles Peskine567840e2018-09-25 18:27:53 +0200198 char buffer[200];
199 char *end;
Gilles Peskine1b879842019-02-13 18:40:50 +0100200 unsigned long value = strtoul(*argp, &end, 0);
Gilles Peskine567840e2018-09-25 18:27:53 +0200201 if (*end) {
Gilles Peskine1b879842019-02-13 18:40:50 +0100202 printf("Non-numeric value: %s\n", *argp);
Gilles Peskine567840e2018-09-25 18:27:53 +0200203 return EXIT_FAILURE;
204 }
Gilles Peskine3f775262019-02-13 18:42:06 +0100205 if (value > max || errno == ERANGE) {
Gilles Peskine1b879842019-02-13 18:40:50 +0100206 printf("Value out of range: %s\n", *argp);
Gilles Peskine265a1712018-10-31 14:52:28 +0100207 return EXIT_FAILURE;
208 }
Gilles Peskine567840e2018-09-25 18:27:53 +0200209
210 switch (type) {
Gilles Peskine567840e2018-09-25 18:27:53 +0200211 case TYPE_ALGORITHM:
212 psa_snprint_algorithm(buffer, sizeof(buffer),
213 (psa_algorithm_t) value);
214 break;
215 case TYPE_ECC_CURVE:
216 psa_snprint_ecc_curve(buffer, sizeof(buffer),
217 (psa_ecc_curve_t) value);
218 break;
219 case TYPE_KEY_TYPE:
220 psa_snprint_key_type(buffer, sizeof(buffer),
221 (psa_key_type_t) value);
222 break;
223 case TYPE_KEY_USAGE:
224 psa_snprint_key_usage(buffer, sizeof(buffer),
225 (psa_key_usage_t) value);
226 break;
227 }
228 puts(buffer);
229 }
230
Gilles Peskine029b5d62018-07-16 23:13:37 +0200231 return EXIT_SUCCESS;
232}
Gilles Peskine1b879842019-02-13 18:40:50 +0100233
234int main(int argc, char *argv[])
235{
236 if (argc <= 1 ||
237 !strcmp(argv[1], "help") ||
238 !strcmp(argv[1], "--help"))
239 {
240 usage(argv[0]);
241 return EXIT_FAILURE;
242 }
243
244 if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
Gilles Peskine3f775262019-02-13 18:42:06 +0100245 /* There's no way to obtain the actual range of a signed type,
246 * so hard-code it here: psa_status_t is int32_t. */
247 return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
248 argv + 2);
Gilles Peskine1b879842019-02-13 18:40:50 +0100249 } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
250 return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
251 argv + 2);
252 } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
253 return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_curve_t) (-1),
254 argv + 2);
255 } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
256 return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
257 argv + 2);
258 } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
259 return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
260 argv + 2);
261 } else {
262 printf("Unknown type: %s\n", argv[1]);
263 return EXIT_FAILURE;
264 }
265}