blob: 88821dab589a14e47785b54da9ef5728e2f9f412 [file] [log] [blame]
Gilles Peskine029b5d62018-07-16 23:13:37 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "psa/crypto.h"
6
7/* There are different GET_HASH macros for different kinds of algorithms
8 * built from hashes, but the values are all constructed on the
9 * same model. */
10#define PSA_ALG_GET_HASH(alg) \
11 (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
12
13static void append(char **buffer, size_t buffer_size,
14 size_t *required_size,
15 const char *string, size_t length)
16{
17 *required_size += length;
18 if (*required_size < buffer_size) {
19 memcpy(*buffer, string, length);
20 *buffer += length;
21 }
22}
23
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020024static void append_integer(char **buffer, size_t buffer_size,
25 size_t *required_size,
26 const char *format /*printf format for value*/,
27 unsigned long value)
28{
29 size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
30 if (n < buffer_size - *required_size) *buffer += n;
31 *required_size += n;
32}
33
Gilles Peskine029b5d62018-07-16 23:13:37 +020034/* The code of these function is automatically generated and included below. */
35static const char *psa_ecc_curve_name(psa_ecc_curve_t curve);
36static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
37
38static void append_with_curve(char **buffer, size_t buffer_size,
39 size_t *required_size,
40 const char *string, size_t length,
41 psa_ecc_curve_t curve)
42{
43 const char *curve_name = psa_ecc_curve_name(curve);
44 append(buffer, buffer_size, required_size, string, length);
45 append(buffer, buffer_size, required_size, "(", 1);
46 if (curve_name != NULL) {
47 append(buffer, buffer_size, required_size,
48 curve_name, strlen(curve_name));
49 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020050 append_integer(buffer, buffer_size, required_size,
51 "0x%04x", curve);
Gilles Peskine029b5d62018-07-16 23:13:37 +020052 }
53 append(buffer, buffer_size, required_size, ")", 1);
54}
55
56static void append_with_hash(char **buffer, size_t buffer_size,
57 size_t *required_size,
58 const char *string, size_t length,
59 psa_algorithm_t hash_alg)
60{
61 const char *hash_name = psa_hash_algorithm_name(hash_alg);
62 append(buffer, buffer_size, required_size, string, length);
63 append(buffer, buffer_size, required_size, "(", 1);
64 if (hash_name != NULL) {
65 append(buffer, buffer_size, required_size,
66 hash_name, strlen(hash_name));
67 } else {
Gilles Peskine0deaf3d2018-08-20 15:06:39 +020068 append_integer(buffer, buffer_size, required_size,
69 "0x%08lx", hash_alg);
Gilles Peskine029b5d62018-07-16 23:13:37 +020070 }
71 append(buffer, buffer_size, required_size, ")", 1);
72}
73
74#include "psa_constant_names_generated.c"
75
76static int psa_snprint_status(char *buffer, size_t buffer_size,
77 psa_status_t status)
78{
79 const char *name = psa_strerror(status);
80 if (name == NULL) {
81 return snprintf(buffer, buffer_size, "%ld", (long) status);
82 } else {
83 size_t length = strlen(name);
84 if (length < buffer_size) {
85 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +010086 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +020087 } else {
Darryl Green18246962018-10-17 15:01:45 +010088 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +020089 }
90 }
91}
92
93static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
94 psa_ecc_curve_t curve)
95{
96 const char *name = psa_ecc_curve_name(curve);
97 if (name == NULL) {
98 return snprintf(buffer, buffer_size, "0x%04x", (unsigned) curve);
99 } else {
100 size_t length = strlen(name);
101 if (length < buffer_size) {
102 memcpy(buffer, name, length + 1);
Darryl Green18246962018-10-17 15:01:45 +0100103 return (int) length;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200104 } else {
Darryl Green18246962018-10-17 15:01:45 +0100105 return (int) buffer_size;
Gilles Peskine029b5d62018-07-16 23:13:37 +0200106 }
107 }
108}
109
110static void usage(const char *program_name)
111{
112 printf("Usage: %s TYPE VALUE\n",
113 program_name == NULL ? "psa_constant_names" : program_name);
114 printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
115 printf("Supported types (with = between aliases):\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200116 printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
Gilles Peskine029b5d62018-07-16 23:13:37 +0200117 printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_curve_t)\n");
Gilles Peskine38808fa2018-08-20 15:07:37 +0200118 printf(" type=key_type Key type (psa_key_type_t)\n");
Gilles Peskine029b5d62018-07-16 23:13:37 +0200119 printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
120 printf(" error=status Status code (psa_status_t)\n");
121}
122
123int main(int argc, char *argv[])
124{
125 char buffer[200];
126 unsigned long value;
127 char *end;
128
129 if (argc <= 1 ||
130 !strcmp(argv[1], "help") ||
131 !strcmp(argv[1], "--help"))
132 {
133 usage(argv[0]);
134 return EXIT_FAILURE;
135 }
136 if (argc != 3) {
137 usage(argv[0]);
138 return EXIT_FAILURE;
139 }
140 value = strtoul(argv[2], &end, 0);
141 if (*end) {
142 printf("Non-numeric value: %s\n", argv[2]);
143 return EXIT_FAILURE;
144 }
145
146 if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status"))
Darryl Green18246962018-10-17 15:01:45 +0100147 psa_snprint_status(buffer, sizeof(buffer), (psa_status_t) value);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200148 else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm"))
Darryl Green18246962018-10-17 15:01:45 +0100149 psa_snprint_algorithm(buffer, sizeof(buffer), (psa_algorithm_t) value);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200150 else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve"))
Darryl Green18246962018-10-17 15:01:45 +0100151 psa_snprint_ecc_curve(buffer, sizeof(buffer), (psa_ecc_curve_t) value);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200152 else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type"))
Darryl Green18246962018-10-17 15:01:45 +0100153 psa_snprint_key_type(buffer, sizeof(buffer), (psa_key_type_t) value);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200154 else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage"))
Darryl Green18246962018-10-17 15:01:45 +0100155 psa_snprint_key_usage(buffer, sizeof(buffer), (psa_key_usage_t) value);
Gilles Peskine029b5d62018-07-16 23:13:37 +0200156 else {
157 printf("Unknown type: %s\n", argv[1]);
158 return EXIT_FAILURE;
159 }
160
161 puts(buffer);
162 return EXIT_SUCCESS;
163}