Skip to content

Commit 1fb011f

Browse files
authored
Merge pull request #152 from ARMmbed/psa-test-psa_constant_names
Test psa_constant_names
2 parents a3abf54 + 21b33b0 commit 1fb011f

File tree

10 files changed

+447
-57
lines changed

10 files changed

+447
-57
lines changed

include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ endif(INSTALL_MBEDTLS_HEADERS)
1818
# Make config.h available in an out-of-source build. ssl-opt.sh requires it.
1919
if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
2020
link_to_source(mbedtls)
21+
link_to_source(psa)
2122
endif()

include/psa/crypto_values.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@
766766
* algorithm is considered identical to the untruncated algorithm
767767
* for policy comparison purposes.
768768
*
769-
* \param alg A MAC algorithm identifier (value of type
769+
* \param mac_alg A MAC algorithm identifier (value of type
770770
* #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
771771
* is true). This may be a truncated or untruncated
772772
* MAC algorithm.
@@ -782,14 +782,14 @@
782782
* MAC algorithm or if \p mac_length is too small or
783783
* too large for the specified MAC algorithm.
784784
*/
785-
#define PSA_ALG_TRUNCATED_MAC(alg, mac_length) \
786-
(((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
785+
#define PSA_ALG_TRUNCATED_MAC(mac_alg, mac_length) \
786+
(((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
787787
((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
788788

789789
/** Macro to build the base MAC algorithm corresponding to a truncated
790790
* MAC algorithm.
791791
*
792-
* \param alg A MAC algorithm identifier (value of type
792+
* \param mac_alg A MAC algorithm identifier (value of type
793793
* #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
794794
* is true). This may be a truncated or untruncated
795795
* MAC algorithm.
@@ -798,12 +798,12 @@
798798
* \return Unspecified if \p alg is not a supported
799799
* MAC algorithm.
800800
*/
801-
#define PSA_ALG_FULL_LENGTH_MAC(alg) \
802-
((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
801+
#define PSA_ALG_FULL_LENGTH_MAC(mac_alg) \
802+
((mac_alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
803803

804804
/** Length to which a MAC algorithm is truncated.
805805
*
806-
* \param alg A MAC algorithm identifier (value of type
806+
* \param mac_alg A MAC algorithm identifier (value of type
807807
* #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
808808
* is true).
809809
*
@@ -812,8 +812,8 @@
812812
* \return Unspecified if \p alg is not a supported
813813
* MAC algorithm.
814814
*/
815-
#define PSA_MAC_TRUNCATED_LENGTH(alg) \
816-
(((alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
815+
#define PSA_MAC_TRUNCATED_LENGTH(mac_alg) \
816+
(((mac_alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
817817

818818
#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
819819
#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
@@ -910,7 +910,7 @@
910910
* Depending on the algorithm, the tag length may affect the calculation
911911
* of the ciphertext.
912912
*
913-
* \param alg A AEAD algorithm identifier (value of type
913+
* \param aead_alg An AEAD algorithm identifier (value of type
914914
* #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
915915
* is true).
916916
* \param tag_length Desired length of the authentication tag in bytes.
@@ -921,26 +921,26 @@
921921
* AEAD algorithm or if \p tag_length is not valid
922922
* for the specified AEAD algorithm.
923923
*/
924-
#define PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, tag_length) \
925-
(((alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
924+
#define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length) \
925+
(((aead_alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
926926
((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
927927
PSA_ALG_AEAD_TAG_LENGTH_MASK))
928928

929929
/** Calculate the corresponding AEAD algorithm with the default tag length.
930930
*
931-
* \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
932-
* #PSA_ALG_IS_AEAD(\p alg) is true).
931+
* \param aead_alg An AEAD algorithm (\c PSA_ALG_XXX value such that
932+
* #PSA_ALG_IS_AEAD(\p alg) is true).
933933
*
934-
* \return The corresponding AEAD algorithm with the default tag length
935-
* for that algorithm.
934+
* \return The corresponding AEAD algorithm with the default
935+
* tag length for that algorithm.
936936
*/
937-
#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) \
937+
#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(aead_alg) \
938938
( \
939-
PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_CCM) \
940-
PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_GCM) \
939+
PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, PSA_ALG_CCM) \
940+
PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, PSA_ALG_GCM) \
941941
0)
942-
#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, ref) \
943-
PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, 0) == \
942+
#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(aead_alg, ref) \
943+
PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, 0) == \
944944
PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
945945
ref :
946946

programs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ EXTRA_GENERATED += psa/psa_constant_names_generated.c
103103
endif
104104

105105
psa/psa_constant_names$(EXEXT): psa/psa_constant_names_generated.c
106-
psa/psa_constant_names_generated.c: ../scripts/generate_psa_constants.py ../include/psa/crypto_values.h
106+
psa/psa_constant_names_generated.c: ../scripts/generate_psa_constants.py ../include/psa/crypto_values.h ../include/psa/crypto_extra.h
107107
../scripts/generate_psa_constants.py
108108

109109
aes/aescrypt2$(EXEXT): aes/aescrypt2.c $(DEP)

programs/psa/CMakeLists.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
add_executable(crypto_examples crypto_examples.c)
22
target_link_libraries(crypto_examples mbedtls)
33

4-
install(TARGETS crypto_examples
4+
add_executable(key_ladder_demo key_ladder_demo.c)
5+
target_link_libraries(key_ladder_demo mbedtls)
6+
7+
add_executable(psa_constant_names psa_constant_names.c)
8+
target_link_libraries(psa_constant_names mbedtls)
9+
10+
add_custom_target(
11+
psa_constant_names_generated
12+
COMMAND ${PYTHON_EXECUTABLE} scripts/generate_psa_constants.py
13+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../
14+
)
15+
add_dependencies(psa_constant_names psa_constant_names_generated)
16+
17+
install(TARGETS
18+
crypto_examples
19+
key_ladder_demo
20+
psa_constant_names
521
DESTINATION "bin"
622
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
723

24+
install(PROGRAMS
25+
key_ladder_demo.sh
26+
DESTINATION "bin")

programs/psa/key_ladder_demo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,9 @@ static void usage( void )
620620

621621
int main( int argc, char *argv[] )
622622
{
623-
char *key_file_name = "master.key";
624-
char *input_file_name = NULL;
625-
char *output_file_name = NULL;
623+
const char *key_file_name = "master.key";
624+
const char *input_file_name = NULL;
625+
const char *output_file_name = NULL;
626626
const char *ladder[MAX_LADDER_DEPTH];
627627
size_t ladder_depth = 0;
628628
int i;

programs/psa/psa_constant_names.c

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
138138

139139
static void usage(const char *program_name)
140140
{
141-
printf("Usage: %s TYPE VALUE\n",
141+
printf("Usage: %s TYPE VALUE [VALUE...]\n",
142142
program_name == NULL ? "psa_constant_names" : program_name);
143143
printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
144144
printf("Supported types (with = between aliases):\n");
@@ -149,11 +149,19 @@ static void usage(const char *program_name)
149149
printf(" error=status Status code (psa_status_t)\n");
150150
}
151151

152+
typedef enum {
153+
TYPE_STATUS,
154+
TYPE_ALGORITHM,
155+
TYPE_ECC_CURVE,
156+
TYPE_KEY_TYPE,
157+
TYPE_KEY_USAGE,
158+
} value_type;
159+
152160
int main(int argc, char *argv[])
153161
{
154-
char buffer[200];
155-
unsigned long value;
156-
char *end;
162+
value_type type;
163+
unsigned long max;
164+
int i;
157165

158166
if (argc <= 1 ||
159167
!strcmp(argv[1], "help") ||
@@ -162,31 +170,64 @@ int main(int argc, char *argv[])
162170
usage(argv[0]);
163171
return EXIT_FAILURE;
164172
}
165-
if (argc != 3) {
166-
usage(argv[0]);
167-
return EXIT_FAILURE;
168-
}
169-
value = strtoul(argv[2], &end, 0);
170-
if (*end) {
171-
printf("Non-numeric value: %s\n", argv[2]);
172-
return EXIT_FAILURE;
173-
}
174173

175-
if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status"))
176-
psa_snprint_status(buffer, sizeof(buffer), (psa_status_t) value);
177-
else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm"))
178-
psa_snprint_algorithm(buffer, sizeof(buffer), (psa_algorithm_t) value);
179-
else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve"))
180-
psa_snprint_ecc_curve(buffer, sizeof(buffer), (psa_ecc_curve_t) value);
181-
else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type"))
182-
psa_snprint_key_type(buffer, sizeof(buffer), (psa_key_type_t) value);
183-
else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage"))
184-
psa_snprint_key_usage(buffer, sizeof(buffer), (psa_key_usage_t) value);
185-
else {
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 {
186190
printf("Unknown type: %s\n", argv[1]);
187191
return EXIT_FAILURE;
188192
}
189193

190-
puts(buffer);
194+
for (i = 2; i < argc; i++) {
195+
char buffer[200];
196+
char *end;
197+
unsigned long value = strtoul(argv[i], &end, 0);
198+
if (*end) {
199+
printf("Non-numeric value: %s\n", argv[i]);
200+
return EXIT_FAILURE;
201+
}
202+
if (value > max) {
203+
printf("Value out of range: %s\n", argv[i]);
204+
return EXIT_FAILURE;
205+
}
206+
207+
switch (type) {
208+
case TYPE_STATUS:
209+
psa_snprint_status(buffer, sizeof(buffer),
210+
(psa_status_t) value);
211+
break;
212+
case TYPE_ALGORITHM:
213+
psa_snprint_algorithm(buffer, sizeof(buffer),
214+
(psa_algorithm_t) value);
215+
break;
216+
case TYPE_ECC_CURVE:
217+
psa_snprint_ecc_curve(buffer, sizeof(buffer),
218+
(psa_ecc_curve_t) value);
219+
break;
220+
case TYPE_KEY_TYPE:
221+
psa_snprint_key_type(buffer, sizeof(buffer),
222+
(psa_key_type_t) value);
223+
break;
224+
case TYPE_KEY_USAGE:
225+
psa_snprint_key_usage(buffer, sizeof(buffer),
226+
(psa_key_usage_t) value);
227+
break;
228+
}
229+
puts(buffer);
230+
}
231+
191232
return EXIT_SUCCESS;
192233
}

scripts/generate_psa_constants.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@
6262
}
6363
} else if (PSA_ALG_IS_AEAD(alg)) {
6464
core_alg = PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg);
65-
if (core_alg != alg) {
65+
if (core_alg == 0) {
66+
/* For unknown AEAD algorithms, there is no "default tag length". */
67+
core_alg = alg;
68+
} else if (core_alg != alg) {
6669
append(&buffer, buffer_size, &required_size,
6770
"PSA_ALG_AEAD_WITH_TAG_LENGTH(", 29);
6871
length_modifier = PSA_AEAD_TAG_LENGTH(alg);
@@ -73,7 +76,7 @@
7376
default:
7477
%(algorithm_code)s{
7578
append_integer(&buffer, buffer_size, &required_size,
76-
"0x%%08lx", (unsigned long) alg);
79+
"0x%%08lx", (unsigned long) core_alg);
7780
}
7881
break;
7982
}
@@ -273,10 +276,11 @@ def write_file(self, output_file):
273276
data['key_usage_code'] = self.make_key_usage_code()
274277
output_file.write(output_template % data)
275278

276-
def generate_psa_constants(header_file_name, output_file_name):
279+
def generate_psa_constants(header_file_names, output_file_name):
277280
collector = MacroCollector()
278-
with open(header_file_name) as header_file:
279-
collector.read_file(header_file)
281+
for header_file_name in header_file_names:
282+
with open(header_file_name) as header_file:
283+
collector.read_file(header_file)
280284
temp_file_name = output_file_name + '.tmp'
281285
with open(temp_file_name, 'w') as output_file:
282286
collector.write_file(output_file)
@@ -285,5 +289,6 @@ def generate_psa_constants(header_file_name, output_file_name):
285289
if __name__ == '__main__':
286290
if not os.path.isdir('programs') and os.path.isdir('../programs'):
287291
os.chdir('..')
288-
generate_psa_constants('include/psa/crypto_values.h',
292+
generate_psa_constants(['include/psa/crypto_values.h',
293+
'include/psa/crypto_extra.h'],
289294
'programs/psa/psa_constant_names_generated.c')

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,5 @@ if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
134134
link_to_source(data_files)
135135
link_to_source(scripts)
136136
link_to_source(ssl-opt.sh)
137+
link_to_source(suites)
137138
endif()

tests/scripts/all.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,9 @@ component_test_full_cmake_clang () {
711711
msg "test: main suites (full config)" # ~ 5s
712712
make test
713713

714+
msg "test: psa_constant_names (full config)" # ~ 1s
715+
record_status tests/scripts/test_psa_constant_names.py
716+
714717
msg "test: ssl-opt.sh default, ECJPAKE, SSL async (full config)" # ~ 1s
715718
if_build_succeeded tests/ssl-opt.sh -f 'Default\|ECJPAKE\|SSL async private'
716719

0 commit comments

Comments
 (0)