From 498c2a1ff5fdca66c4600c00deaeeefcfa2b3ad9 Mon Sep 17 00:00:00 2001
From: Gilles Peskine <Gilles.Peskine@arm.com>
Date: Mon, 20 Aug 2018 15:07:20 +0200
Subject: [PATCH] psa_constant_names: support truncated MAC and AEAD

---
 scripts/generate_psa_constants.py | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/scripts/generate_psa_constants.py b/scripts/generate_psa_constants.py
index 0e80f40c5..85bfe3ae9 100755
--- a/scripts/generate_psa_constants.py
+++ b/scripts/generate_psa_constants.py
@@ -51,7 +51,24 @@ static int psa_snprint_algorithm(char *buffer, size_t buffer_size,
                                  psa_algorithm_t alg)
 {
     size_t required_size = 0;
-    switch (alg) {
+    psa_algorithm_t core_alg = alg;
+    unsigned long length_modifier = 0;
+    if (PSA_ALG_IS_MAC(alg)) {
+        core_alg = PSA_ALG_TRUNCATED_MAC(alg, 0);
+        if (core_alg != alg) {
+            append(&buffer, buffer_size, &required_size,
+                   "PSA_ALG_TRUNCATED_MAC(", 22);
+            length_modifier = PSA_MAC_TRUNCATED_LENGTH(alg);
+        }
+    } else if (PSA_ALG_IS_AEAD(alg)) {
+        core_alg = PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg);
+        if (core_alg != alg) {
+            append(&buffer, buffer_size, &required_size,
+                   "PSA_ALG_AEAD_WITH_TAG_LENGTH(", 29);
+            length_modifier = PSA_AEAD_TAG_LENGTH(alg);
+        }
+    }
+    switch (core_alg) {
     %(algorithm_cases)s
     default:
         %(algorithm_code)s{
@@ -60,6 +77,12 @@ static int psa_snprint_algorithm(char *buffer, size_t buffer_size,
         }
         break;
     }
+    if (core_alg != alg) {
+        append(&buffer, buffer_size, &required_size, ", ", 2);
+        append_integer(&buffer, buffer_size, &required_size,
+                       "%%lu", length_modifier);
+        append(&buffer, buffer_size, &required_size, ")", 1);
+    }
     buffer[0] = 0;
     return required_size;
 }
@@ -99,10 +122,10 @@ key_type_from_curve_template = '''if (%(tester)s(type)) {
                               PSA_KEY_TYPE_GET_CURVE(type));
         } else '''
 
-algorithm_from_hash_template = '''if (%(tester)s(alg)) {
+algorithm_from_hash_template = '''if (%(tester)s(core_alg)) {
             append_with_hash(&buffer, buffer_size, &required_size,
                              "%(builder)s", %(builder_length)s,
-                             PSA_ALG_GET_HASH(alg));
+                             PSA_ALG_GET_HASH(core_alg));
         } else '''
 
 bit_test_template = '''\