blob: d422e14f62ed97a6754c785d5067c4dfeca0357d [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
24/* The code of these function is automatically generated and included below. */
25static const char *psa_ecc_curve_name(psa_ecc_curve_t curve);
26static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
27
28static void append_with_curve(char **buffer, size_t buffer_size,
29 size_t *required_size,
30 const char *string, size_t length,
31 psa_ecc_curve_t curve)
32{
33 const char *curve_name = psa_ecc_curve_name(curve);
34 append(buffer, buffer_size, required_size, string, length);
35 append(buffer, buffer_size, required_size, "(", 1);
36 if (curve_name != NULL) {
37 append(buffer, buffer_size, required_size,
38 curve_name, strlen(curve_name));
39 } else {
40 size_t n = snprintf(*buffer, buffer_size - *required_size,
41 "0x%04x", (unsigned) curve);
42 if (n < buffer_size - *required_size) *buffer += n;
43 *required_size += n;
44 }
45 append(buffer, buffer_size, required_size, ")", 1);
46}
47
48static void append_with_hash(char **buffer, size_t buffer_size,
49 size_t *required_size,
50 const char *string, size_t length,
51 psa_algorithm_t hash_alg)
52{
53 const char *hash_name = psa_hash_algorithm_name(hash_alg);
54 append(buffer, buffer_size, required_size, string, length);
55 append(buffer, buffer_size, required_size, "(", 1);
56 if (hash_name != NULL) {
57 append(buffer, buffer_size, required_size,
58 hash_name, strlen(hash_name));
59 } else {
60 size_t n = snprintf(*buffer, buffer_size - *required_size,
61 "0x%08lx", (unsigned long) hash_alg);
62 if (n < buffer_size - *required_size) *buffer += n;
63 *required_size += n;
64 }
65 append(buffer, buffer_size, required_size, ")", 1);
66}
67
68#include "psa_constant_names_generated.c"
69
70static int psa_snprint_status(char *buffer, size_t buffer_size,
71 psa_status_t status)
72{
73 const char *name = psa_strerror(status);
74 if (name == NULL) {
75 return snprintf(buffer, buffer_size, "%ld", (long) status);
76 } else {
77 size_t length = strlen(name);
78 if (length < buffer_size) {
79 memcpy(buffer, name, length + 1);
80 return length;
81 } else {
82 return buffer_size;
83 }
84 }
85}
86
87static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
88 psa_ecc_curve_t curve)
89{
90 const char *name = psa_ecc_curve_name(curve);
91 if (name == NULL) {
92 return snprintf(buffer, buffer_size, "0x%04x", (unsigned) curve);
93 } else {
94 size_t length = strlen(name);
95 if (length < buffer_size) {
96 memcpy(buffer, name, length + 1);
97 return length;
98 } else {
99 return buffer_size;
100 }
101 }
102}
103
104static void usage(const char *program_name)
105{
106 printf("Usage: %s TYPE VALUE\n",
107 program_name == NULL ? "psa_constant_names" : program_name);
108 printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
109 printf("Supported types (with = between aliases):\n");
110 printf(" alg=algorithm Status code (psa_algorithm_t)\n");
111 printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_curve_t)\n");
112 printf(" type=key_type Status code (psa_key_type_t)\n");
113 printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
114 printf(" error=status Status code (psa_status_t)\n");
115}
116
117int main(int argc, char *argv[])
118{
119 char buffer[200];
120 unsigned long value;
121 char *end;
122
123 if (argc <= 1 ||
124 !strcmp(argv[1], "help") ||
125 !strcmp(argv[1], "--help"))
126 {
127 usage(argv[0]);
128 return EXIT_FAILURE;
129 }
130 if (argc != 3) {
131 usage(argv[0]);
132 return EXIT_FAILURE;
133 }
134 value = strtoul(argv[2], &end, 0);
135 if (*end) {
136 printf("Non-numeric value: %s\n", argv[2]);
137 return EXIT_FAILURE;
138 }
139
140 if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status"))
141 psa_snprint_status(buffer, sizeof(buffer), value);
142 else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm"))
143 psa_snprint_algorithm(buffer, sizeof(buffer), value);
144 else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve"))
145 psa_snprint_ecc_curve(buffer, sizeof(buffer), value);
146 else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type"))
147 psa_snprint_key_type(buffer, sizeof(buffer), value);
148 else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage"))
149 psa_snprint_key_usage(buffer, sizeof(buffer), value);
150 else {
151 printf("Unknown type: %s\n", argv[1]);
152 return EXIT_FAILURE;
153 }
154
155 puts(buffer);
156 return EXIT_SUCCESS;
157}