Skip to content

Commit 247842c

Browse files
authored
Merge pull request #56 from gilles-peskine-arm/psa_constant_names-status_is_signed
psa_constant_names: status is signed
2 parents 2d7e5fe + c4cd2ad commit 247842c

File tree

2 files changed

+84
-44
lines changed

2 files changed

+84
-44
lines changed

programs/psa/psa_constant_names.c

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <errno.h>
2+
#include <stdint.h>
13
#include <stdio.h>
24
#include <stdlib.h>
35
#include <string.h>
@@ -151,56 +153,24 @@ static void usage(const char *program_name)
151153

152154
typedef enum {
153155
TYPE_STATUS,
154-
TYPE_ALGORITHM,
155-
TYPE_ECC_CURVE,
156-
TYPE_KEY_TYPE,
157-
TYPE_KEY_USAGE,
158-
} value_type;
156+
} signed_value_type;
159157

160-
int main(int argc, char *argv[])
158+
int process_signed(signed_value_type type, long min, long max, char **argp)
161159
{
162-
value_type type;
163-
unsigned long max;
164-
int i;
165-
166-
if (argc <= 1 ||
167-
!strcmp(argv[1], "help") ||
168-
!strcmp(argv[1], "--help"))
169-
{
170-
usage(argv[0]);
171-
return EXIT_FAILURE;
172-
}
173-
174-
if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
175-
type = TYPE_STATUS;
176-
max = 0x7fffffff; /* hard-coded because psa_status_t is signed */
177-
} else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
178-
type = TYPE_ALGORITHM;
179-
max = (psa_algorithm_t)( -1 );
180-
} else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
181-
type = TYPE_ECC_CURVE;
182-
max = (psa_ecc_curve_t)( -1 );
183-
} else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
184-
type = TYPE_KEY_TYPE;
185-
max = (psa_key_type_t)( -1 );
186-
} else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
187-
type = TYPE_KEY_USAGE;
188-
max = (psa_key_usage_t)( -1 );
189-
} else {
190-
printf("Unknown type: %s\n", argv[1]);
191-
return EXIT_FAILURE;
192-
}
193-
194-
for (i = 2; i < argc; i++) {
160+
for (; *argp != NULL; argp++) {
195161
char buffer[200];
196162
char *end;
197-
unsigned long value = strtoul(argv[i], &end, 0);
163+
long value = strtol(*argp, &end, 0);
198164
if (*end) {
199-
printf("Non-numeric value: %s\n", argv[i]);
165+
printf("Non-numeric value: %s\n", *argp);
166+
return EXIT_FAILURE;
167+
}
168+
if (value < min || (errno == ERANGE && value < 0)) {
169+
printf("Value too small: %s\n", *argp);
200170
return EXIT_FAILURE;
201171
}
202-
if (value > max) {
203-
printf("Value out of range: %s\n", argv[i]);
172+
if (value > max || (errno == ERANGE && value > 0)) {
173+
printf("Value too large: %s\n", *argp);
204174
return EXIT_FAILURE;
205175
}
206176

@@ -209,6 +179,36 @@ int main(int argc, char *argv[])
209179
psa_snprint_status(buffer, sizeof(buffer),
210180
(psa_status_t) value);
211181
break;
182+
}
183+
puts(buffer);
184+
}
185+
186+
return EXIT_SUCCESS;
187+
}
188+
189+
typedef enum {
190+
TYPE_ALGORITHM,
191+
TYPE_ECC_CURVE,
192+
TYPE_KEY_TYPE,
193+
TYPE_KEY_USAGE,
194+
} unsigned_value_type;
195+
196+
int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
197+
{
198+
for (; *argp != NULL; argp++) {
199+
char buffer[200];
200+
char *end;
201+
unsigned long value = strtoul(*argp, &end, 0);
202+
if (*end) {
203+
printf("Non-numeric value: %s\n", *argp);
204+
return EXIT_FAILURE;
205+
}
206+
if (value > max || errno == ERANGE) {
207+
printf("Value out of range: %s\n", *argp);
208+
return EXIT_FAILURE;
209+
}
210+
211+
switch (type) {
212212
case TYPE_ALGORITHM:
213213
psa_snprint_algorithm(buffer, sizeof(buffer),
214214
(psa_algorithm_t) value);
@@ -231,3 +231,36 @@ int main(int argc, char *argv[])
231231

232232
return EXIT_SUCCESS;
233233
}
234+
235+
int main(int argc, char *argv[])
236+
{
237+
if (argc <= 1 ||
238+
!strcmp(argv[1], "help") ||
239+
!strcmp(argv[1], "--help"))
240+
{
241+
usage(argv[0]);
242+
return EXIT_FAILURE;
243+
}
244+
245+
if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
246+
/* There's no way to obtain the actual range of a signed type,
247+
* so hard-code it here: psa_status_t is int32_t. */
248+
return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
249+
argv + 2);
250+
} else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
251+
return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
252+
argv + 2);
253+
} else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
254+
return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_curve_t) (-1),
255+
argv + 2);
256+
} else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
257+
return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
258+
argv + 2);
259+
} else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
260+
return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
261+
argv + 2);
262+
} else {
263+
printf("Unknown type: %s\n", argv[1]);
264+
return EXIT_FAILURE;
265+
}
266+
}

tests/scripts/test_psa_constant_names.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ def remove_file_if_exists(filename):
211211

212212
def run_c(options, type, names):
213213
'''Generate and run a program to print out numerical values for names.'''
214+
if type == 'status':
215+
cast_to = 'long'
216+
printf_format = '%ld'
217+
else:
218+
cast_to = 'unsigned long'
219+
printf_format = '0x%08lx'
214220
c_name = None
215221
exe_name = None
216222
try:
@@ -230,7 +236,8 @@ def run_c(options, type, names):
230236
{
231237
''')
232238
for name in names:
233-
c_file.write(' printf("0x%08x\\n", {});\n'.format(name))
239+
c_file.write(' printf("{}\\n", ({}) {});\n'
240+
.format(printf_format, cast_to, name))
234241
c_file.write(''' return 0;
235242
}
236243
''')

0 commit comments

Comments
 (0)