1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4  */
5 
6 #include <assert.h>
7 #include <attest_app.h>
8 #include <debug.h>
9 #include <measurement.h>
10 #include <simd.h>
11 #include <stdbool.h>
12 
13 #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
measurement_print(unsigned char * measurement,const enum hash_algo algorithm)14 static void measurement_print(unsigned char *measurement,
15 			      const enum hash_algo algorithm)
16 {
17 	unsigned int size = 0U;
18 
19 	assert(measurement != NULL);
20 
21 	VERBOSE("Measurement ");
22 
23 	switch (algorithm) {
24 	case HASH_SHA_256:
25 		VERBOSE("(SHA256): 0x");
26 		size = SHA256_SIZE;
27 		break;
28 	case HASH_SHA_512:
29 		VERBOSE("(SHA512): 0x");
30 		size = SHA512_SIZE;
31 		break;
32 	default:
33 		/* Prevent static check and MISRA warnings */
34 		assert(false);
35 	}
36 
37 	for (unsigned int i = 0U; i < size; ++i) {
38 		VERBOSE("%02x", *(measurement+i));
39 	}
40 	VERBOSE("\n");
41 }
42 #endif /* LOG_LEVEL */
43 
do_hash(enum hash_algo algorithm,void * data,size_t size,unsigned char * out)44 static void do_hash(enum hash_algo algorithm,
45 		    void *data,
46 		    size_t size,
47 		    unsigned char *out)
48 {
49 	attest_do_hash((unsigned int)algorithm, data, size, out);
50 
51 #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
52 	measurement_print(out, algorithm);
53 #endif
54 }
55 
measurement_hash_compute(enum hash_algo algorithm,void * data,size_t size,unsigned char * out)56 void measurement_hash_compute(enum hash_algo algorithm,
57 			      void *data,
58 			      size_t size,
59 			      unsigned char *out)
60 {
61 	do_hash(algorithm, data, size, out);
62 }
63 
do_extend(void * app_data_cfg,enum hash_algo algorithm,void * current_measurement,void * extend_measurement,size_t extend_measurement_size,unsigned char * out,size_t out_size)64 static void do_extend(void *app_data_cfg,
65 		      enum hash_algo algorithm,
66 		      void *current_measurement,
67 		      void *extend_measurement,
68 		      size_t extend_measurement_size,
69 		      unsigned char *out,
70 		      size_t out_size)
71 {
72 	attest_do_extend(app_data_cfg,
73 		      algorithm,
74 		      current_measurement,
75 		      extend_measurement,
76 		      extend_measurement_size,
77 		      out,
78 		      out_size);
79 }
80 
measurement_extend(void * app_data_cfg,enum hash_algo algorithm,void * current_measurement,void * extend_measurement,size_t extend_measurement_size,unsigned char * out,size_t out_size)81 void measurement_extend(void *app_data_cfg,
82 			enum hash_algo algorithm,
83 			void *current_measurement,
84 			void *extend_measurement,
85 			size_t extend_measurement_size,
86 			unsigned char *out,
87 			size_t out_size)
88 {
89 	/* We limit the maximum size of the payload to be of GRANULE_SIZE */
90 	assert(current_measurement != NULL);
91 	assert(extend_measurement_size <= GRANULE_SIZE);
92 	assert(extend_measurement != NULL);
93 	assert(out != NULL);
94 
95 	do_extend(app_data_cfg,
96 			algorithm,
97 			current_measurement,
98 			extend_measurement,
99 			extend_measurement_size,
100 			out,
101 			out_size);
102 
103 #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
104 	measurement_print(out, algorithm);
105 #endif
106 }
107