From 1b879843d11e8390f96b3a941ce1f71e1bf58a62 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 13 Feb 2019 18:40:50 +0100 Subject: [PATCH 1/3] psa_constant_names: factor unsigned support into its own function This is in preparation for adding support for signed types (namely, psa_status_t). --- programs/psa/psa_constant_names.c | 76 +++++++++++++++---------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/programs/psa/psa_constant_names.c b/programs/psa/psa_constant_names.c index cc98a9535..11dc3c120 100644 --- a/programs/psa/psa_constant_names.c +++ b/programs/psa/psa_constant_names.c @@ -155,52 +155,20 @@ typedef enum { TYPE_ECC_CURVE, TYPE_KEY_TYPE, TYPE_KEY_USAGE, -} value_type; +} unsigned_value_type; -int main(int argc, char *argv[]) +int process_unsigned(unsigned_value_type type, unsigned long max, char **argp) { - value_type type; - unsigned long max; - int i; - - if (argc <= 1 || - !strcmp(argv[1], "help") || - !strcmp(argv[1], "--help")) - { - usage(argv[0]); - return EXIT_FAILURE; - } - - if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) { - type = TYPE_STATUS; - max = 0x7fffffff; /* hard-coded because psa_status_t is signed */ - } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) { - type = TYPE_ALGORITHM; - max = (psa_algorithm_t)( -1 ); - } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) { - type = TYPE_ECC_CURVE; - max = (psa_ecc_curve_t)( -1 ); - } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) { - type = TYPE_KEY_TYPE; - max = (psa_key_type_t)( -1 ); - } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) { - type = TYPE_KEY_USAGE; - max = (psa_key_usage_t)( -1 ); - } else { - printf("Unknown type: %s\n", argv[1]); - return EXIT_FAILURE; - } - - for (i = 2; i < argc; i++) { + for (; *argp != NULL; argp++) { char buffer[200]; char *end; - unsigned long value = strtoul(argv[i], &end, 0); + unsigned long value = strtoul(*argp, &end, 0); if (*end) { - printf("Non-numeric value: %s\n", argv[i]); + printf("Non-numeric value: %s\n", *argp); return EXIT_FAILURE; } if (value > max) { - printf("Value out of range: %s\n", argv[i]); + printf("Value out of range: %s\n", *argp); return EXIT_FAILURE; } @@ -231,3 +199,35 @@ int main(int argc, char *argv[]) return EXIT_SUCCESS; } + +int main(int argc, char *argv[]) +{ + if (argc <= 1 || + !strcmp(argv[1], "help") || + !strcmp(argv[1], "--help")) + { + usage(argv[0]); + return EXIT_FAILURE; + } + + if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) { + /* Wrong! psa_status_t is signed. */ + return process_unsigned(TYPE_ALGORITHM, 0xffffffff, + argv + 2); + } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) { + return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1), + argv + 2); + } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) { + return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_curve_t) (-1), + argv + 2); + } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) { + return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1), + argv + 2); + } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) { + return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1), + argv + 2); + } else { + printf("Unknown type: %s\n", argv[1]); + return EXIT_FAILURE; + } +} From 3f775264d30edee151febba796845add1df1cd29 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 13 Feb 2019 18:42:06 +0100 Subject: [PATCH 2/3] psa_constant_names: adding support for signed types psa_constant_names now works correctly with signed values, such as psa_status_t may have. --- programs/psa/psa_constant_names.c | 49 ++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/programs/psa/psa_constant_names.c b/programs/psa/psa_constant_names.c index 11dc3c120..551410021 100644 --- a/programs/psa/psa_constant_names.c +++ b/programs/psa/psa_constant_names.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -151,6 +153,40 @@ static void usage(const char *program_name) typedef enum { TYPE_STATUS, +} signed_value_type; + +int process_signed(signed_value_type type, long min, long max, char **argp) +{ + for (; *argp != NULL; argp++) { + char buffer[200]; + char *end; + long value = strtol(*argp, &end, 0); + if (*end) { + printf("Non-numeric value: %s\n", *argp); + return EXIT_FAILURE; + } + if (value < min || (errno == ERANGE && value < 0)) { + printf("Value too small: %s\n", *argp); + return EXIT_FAILURE; + } + if (value > max || (errno == ERANGE && value > 0)) { + printf("Value too large: %s\n", *argp); + return EXIT_FAILURE; + } + + switch (type) { + case TYPE_STATUS: + psa_snprint_status(buffer, sizeof(buffer), + (psa_status_t) value); + break; + } + puts(buffer); + } + + return EXIT_SUCCESS; +} + +typedef enum { TYPE_ALGORITHM, TYPE_ECC_CURVE, TYPE_KEY_TYPE, @@ -167,16 +203,12 @@ int process_unsigned(unsigned_value_type type, unsigned long max, char **argp) printf("Non-numeric value: %s\n", *argp); return EXIT_FAILURE; } - if (value > max) { + if (value > max || errno == ERANGE) { printf("Value out of range: %s\n", *argp); return EXIT_FAILURE; } switch (type) { - case TYPE_STATUS: - psa_snprint_status(buffer, sizeof(buffer), - (psa_status_t) value); - break; case TYPE_ALGORITHM: psa_snprint_algorithm(buffer, sizeof(buffer), (psa_algorithm_t) value); @@ -211,9 +243,10 @@ int main(int argc, char *argv[]) } if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) { - /* Wrong! psa_status_t is signed. */ - return process_unsigned(TYPE_ALGORITHM, 0xffffffff, - argv + 2); + /* There's no way to obtain the actual range of a signed type, + * so hard-code it here: psa_status_t is int32_t. */ + return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX, + argv + 2); } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) { return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1), argv + 2); From c4cd2adae8df60d1992f5d4c9397af5ff35e0914 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 13 Feb 2019 18:42:53 +0100 Subject: [PATCH 3/3] test_psa_constant_names: fix uses of C integer types Some of the types may in principle be wider than `unsigned`, so use `unsigned long` in printf. Add support for signed types: a status is a signed value, and preferentially printed in decimal. --- tests/scripts/test_psa_constant_names.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/scripts/test_psa_constant_names.py b/tests/scripts/test_psa_constant_names.py index 000dedc20..d22652e8a 100755 --- a/tests/scripts/test_psa_constant_names.py +++ b/tests/scripts/test_psa_constant_names.py @@ -211,6 +211,12 @@ def remove_file_if_exists(filename): def run_c(options, type, names): '''Generate and run a program to print out numerical values for names.''' + if type == 'status': + cast_to = 'long' + printf_format = '%ld' + else: + cast_to = 'unsigned long' + printf_format = '0x%08lx' c_name = None exe_name = None try: @@ -230,7 +236,8 @@ int main(void) { ''') for name in names: - c_file.write(' printf("0x%08x\\n", {});\n'.format(name)) + c_file.write(' printf("{}\\n", ({}) {});\n' + .format(printf_format, cast_to, name)) c_file.write(''' return 0; } ''')