Merge pull request #51 from Patater/update-dev-tls-dev-crypto-merge

Merge mbedtls/development-psa, mbedtls/development into development
diff --git a/programs/psa/psa_constant_names.c b/programs/psa/psa_constant_names.c
index cc98a95..5514100 100644
--- a/programs/psa/psa_constant_names.c
+++ b/programs/psa/psa_constant_names.c
@@ -1,3 +1,5 @@
+#include <errno.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -151,56 +153,24 @@
 
 typedef enum {
     TYPE_STATUS,
-    TYPE_ALGORITHM,
-    TYPE_ECC_CURVE,
-    TYPE_KEY_TYPE,
-    TYPE_KEY_USAGE,
-} value_type;
+} signed_value_type;
 
-int main(int argc, char *argv[])
+int process_signed(signed_value_type type, long min, 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);
+        long value = strtol(*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]);
+        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;
         }
 
@@ -209,6 +179,36 @@
                 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,
+    TYPE_KEY_USAGE,
+} unsigned_value_type;
+
+int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
+{
+    for (; *argp != NULL; argp++) {
+        char buffer[200];
+        char *end;
+        unsigned long value = strtoul(*argp, &end, 0);
+        if (*end) {
+            printf("Non-numeric value: %s\n", *argp);
+            return EXIT_FAILURE;
+        }
+        if (value > max || errno == ERANGE) {
+            printf("Value out of range: %s\n", *argp);
+            return EXIT_FAILURE;
+        }
+
+        switch (type) {
             case TYPE_ALGORITHM:
                 psa_snprint_algorithm(buffer, sizeof(buffer),
                                       (psa_algorithm_t) value);
@@ -231,3 +231,36 @@
 
     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")) {
+        /* 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);
+    } 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;
+    }
+}
diff --git a/tests/scripts/test_psa_constant_names.py b/tests/scripts/test_psa_constant_names.py
index 000dedc..d22652e 100755
--- a/tests/scripts/test_psa_constant_names.py
+++ b/tests/scripts/test_psa_constant_names.py
@@ -211,6 +211,12 @@
 
 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 @@
 {
 ''')
         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;
 }
 ''')
diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function
index 4a3044a..929d1b2 100644
--- a/tests/suites/test_suite_psa_crypto.function
+++ b/tests/suites/test_suite_psa_crypto.function
@@ -1441,15 +1441,15 @@
 
     memset( &zero, 0, sizeof( zero ) );
 
-    /* Although not technically guaranteed by the C standard nor the PSA Crypto
-     * specification, we test that all valid ways of initializing the object
-     * have the same bit pattern. This is a stronger requirement that may not
-     * be valid on all platforms or PSA Crypto implementations, but implies the
-     * weaker actual requirement is met: that a freshly initialized object, no
-     * matter how it was initialized, acts the same as any other valid
-     * initialization. */
-    TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
-    TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
+    /* A default key policy should not permit any usage. */
+    TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
+    TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
+    TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
+
+    /* A default key policy should not permit any algorithm. */
+    TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
+    TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
+    TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
 }
 /* END_CASE */
 
@@ -1960,15 +1960,10 @@
 
     memset( &zero, 0, sizeof( zero ) );
 
-    /* Although not technically guaranteed by the C standard nor the PSA Crypto
-     * specification, we test that all valid ways of initializing the object
-     * have the same bit pattern. This is a stronger requirement that may not
-     * be valid on all platforms or PSA Crypto implementations, but implies the
-     * weaker actual requirement is met: that a freshly initialized object, no
-     * matter how it was initialized, acts the same as any other valid
-     * initialization. */
-    TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
-    TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
+    /* A default hash operation should be abortable without error. */
+    PSA_ASSERT( psa_hash_abort( &func ) );
+    PSA_ASSERT( psa_hash_abort( &init ) );
+    PSA_ASSERT( psa_hash_abort( &zero ) );
 }
 /* END_CASE */
 
@@ -2183,15 +2178,10 @@
 
     memset( &zero, 0, sizeof( zero ) );
 
-    /* Although not technically guaranteed by the C standard nor the PSA Crypto
-     * specification, we test that all valid ways of initializing the object
-     * have the same bit pattern. This is a stronger requirement that may not
-     * be valid on all platforms or PSA Crypto implementations, but implies the
-     * weaker actual requirement is met: that a freshly initialized object, no
-     * matter how it was initialized, acts the same as any other valid
-     * initialization. */
-    TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
-    TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
+    /* A default MAC operation should be abortable without error. */
+    PSA_ASSERT( psa_mac_abort( &func ) );
+    PSA_ASSERT( psa_mac_abort( &init ) );
+    PSA_ASSERT( psa_mac_abort( &zero ) );
 }
 /* END_CASE */
 
@@ -2338,15 +2328,10 @@
 
     memset( &zero, 0, sizeof( zero ) );
 
-    /* Although not technically guaranteed by the C standard nor the PSA Crypto
-     * specification, we test that all valid ways of initializing the object
-     * have the same bit pattern. This is a stronger requirement that may not
-     * be valid on all platforms or PSA Crypto implementations, but implies the
-     * weaker actual requirement is met: that a freshly initialized object, no
-     * matter how it was initialized, acts the same as any other valid
-     * initialization. */
-    TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
-    TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
+    /* A default cipher operation should be abortable without error. */
+    PSA_ASSERT( psa_cipher_abort( &func ) );
+    PSA_ASSERT( psa_cipher_abort( &init ) );
+    PSA_ASSERT( psa_cipher_abort( &zero ) );
 }
 /* END_CASE */
 
@@ -3527,21 +3512,25 @@
      * Clang 5 complains when `-Wmissing-field-initializers` is used, even
      * though it's OK by the C standard. We could test for this, but we'd need
      * to supress the Clang warning for the test. */
+    size_t capacity;
     psa_crypto_generator_t func = psa_crypto_generator_init( );
     psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
     psa_crypto_generator_t zero;
 
     memset( &zero, 0, sizeof( zero ) );
 
-    /* Although not technically guaranteed by the C standard nor the PSA Crypto
-     * specification, we test that all valid ways of initializing the object
-     * have the same bit pattern. This is a stronger requirement that may not
-     * be valid on all platforms or PSA Crypto implementations, but implies the
-     * weaker actual requirement is met: that a freshly initialized object, no
-     * matter how it was initialized, acts the same as any other valid
-     * initialization. */
-    TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
-    TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
+    /* A default generator should have no capacity. */
+    PSA_ASSERT( psa_get_generator_capacity( &func, &capacity ) );
+    TEST_EQUAL( capacity, 0 );
+    PSA_ASSERT( psa_get_generator_capacity( &init, &capacity ) );
+    TEST_EQUAL( capacity, 0 );
+    PSA_ASSERT( psa_get_generator_capacity( &zero, &capacity ) );
+    TEST_EQUAL( capacity, 0 );
+
+    /* A default generator should be abortable without error. */
+    PSA_ASSERT( psa_generator_abort(&func) );
+    PSA_ASSERT( psa_generator_abort(&init) );
+    PSA_ASSERT( psa_generator_abort(&zero) );
 }
 /* END_CASE */