1/* BEGIN_HEADER */ 2#include <stdint.h> 3 4#include "common.h" 5 6#include "psa/crypto.h" 7 8#include "psa_crypto_core.h" 9#include "psa_crypto_invasive.h" 10 11#include "test/psa_crypto_helpers.h" 12#include "test/memory.h" 13 14/* Helper to fill a buffer with a data pattern. The pattern is not 15 * important, it just allows a basic check that the correct thing has 16 * been written, in a way that will detect an error in offset. */ 17static void fill_buffer_pattern(uint8_t *buffer, size_t len) 18{ 19 for (size_t i = 0; i < len; i++) { 20 buffer[i] = (uint8_t) (i % 256); 21 } 22} 23/* END_HEADER */ 24 25/* BEGIN_DEPENDENCIES 26 * depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_TEST_HOOKS 27 * END_DEPENDENCIES 28 */ 29 30/* BEGIN_CASE */ 31void copy_input(int src_len, int dst_len, psa_status_t exp_status) 32{ 33 uint8_t *src_buffer = NULL; 34 uint8_t *dst_buffer = NULL; 35 psa_status_t status; 36 37 TEST_CALLOC(src_buffer, src_len); 38 TEST_CALLOC(dst_buffer, dst_len); 39 40 fill_buffer_pattern(src_buffer, src_len); 41 42 status = psa_crypto_copy_input(src_buffer, src_len, dst_buffer, dst_len); 43 TEST_EQUAL(status, exp_status); 44 45 if (exp_status == PSA_SUCCESS) { 46 MBEDTLS_TEST_MEMORY_UNPOISON(src_buffer, src_len); 47 /* Note: We compare the first src_len bytes of each buffer, as this is what was copied. */ 48 TEST_MEMORY_COMPARE(src_buffer, src_len, dst_buffer, src_len); 49 } 50 51exit: 52 mbedtls_free(src_buffer); 53 mbedtls_free(dst_buffer); 54} 55/* END_CASE */ 56 57/* BEGIN_CASE */ 58void copy_output(int src_len, int dst_len, psa_status_t exp_status) 59{ 60 uint8_t *src_buffer = NULL; 61 uint8_t *dst_buffer = NULL; 62 psa_status_t status; 63 64 TEST_CALLOC(src_buffer, src_len); 65 TEST_CALLOC(dst_buffer, dst_len); 66 67 fill_buffer_pattern(src_buffer, src_len); 68 69 status = psa_crypto_copy_output(src_buffer, src_len, dst_buffer, dst_len); 70 TEST_EQUAL(status, exp_status); 71 72 if (exp_status == PSA_SUCCESS) { 73 MBEDTLS_TEST_MEMORY_UNPOISON(dst_buffer, dst_len); 74 /* Note: We compare the first src_len bytes of each buffer, as this is what was copied. */ 75 TEST_MEMORY_COMPARE(src_buffer, src_len, dst_buffer, src_len); 76 } 77 78exit: 79 mbedtls_free(src_buffer); 80 mbedtls_free(dst_buffer); 81} 82/* END_CASE */ 83 84/* BEGIN_CASE */ 85void local_input_alloc(int input_len, psa_status_t exp_status) 86{ 87 uint8_t *input = NULL; 88 psa_crypto_local_input_t local_input; 89 psa_status_t status; 90 91 local_input.buffer = NULL; 92 93 TEST_CALLOC(input, input_len); 94 fill_buffer_pattern(input, input_len); 95 96 status = psa_crypto_local_input_alloc(input, input_len, &local_input); 97 TEST_EQUAL(status, exp_status); 98 99 if (exp_status == PSA_SUCCESS) { 100 MBEDTLS_TEST_MEMORY_UNPOISON(input, input_len); 101 if (input_len != 0) { 102 TEST_ASSERT(local_input.buffer != input); 103 } 104 TEST_MEMORY_COMPARE(input, input_len, 105 local_input.buffer, local_input.length); 106 } 107 108exit: 109 mbedtls_free(local_input.buffer); 110 mbedtls_free(input); 111} 112/* END_CASE */ 113 114/* BEGIN_CASE */ 115void local_input_free(int input_len) 116{ 117 psa_crypto_local_input_t local_input; 118 119 local_input.buffer = NULL; 120 local_input.length = input_len; 121 TEST_CALLOC(local_input.buffer, local_input.length); 122 123 psa_crypto_local_input_free(&local_input); 124 125 TEST_ASSERT(local_input.buffer == NULL); 126 TEST_EQUAL(local_input.length, 0); 127 128exit: 129 mbedtls_free(local_input.buffer); 130 local_input.buffer = NULL; 131 local_input.length = 0; 132} 133/* END_CASE */ 134 135/* BEGIN_CASE */ 136void local_input_round_trip() 137{ 138 psa_crypto_local_input_t local_input; 139 uint8_t input[200]; 140 psa_status_t status; 141 142 fill_buffer_pattern(input, sizeof(input)); 143 144 status = psa_crypto_local_input_alloc(input, sizeof(input), &local_input); 145 TEST_EQUAL(status, PSA_SUCCESS); 146 147 MBEDTLS_TEST_MEMORY_UNPOISON(input, sizeof(input)); 148 TEST_MEMORY_COMPARE(local_input.buffer, local_input.length, 149 input, sizeof(input)); 150 TEST_ASSERT(local_input.buffer != input); 151 152 psa_crypto_local_input_free(&local_input); 153 TEST_ASSERT(local_input.buffer == NULL); 154 TEST_EQUAL(local_input.length, 0); 155} 156/* END_CASE */ 157 158/* BEGIN_CASE */ 159void local_output_alloc(int output_len, psa_status_t exp_status) 160{ 161 uint8_t *output = NULL; 162 psa_crypto_local_output_t local_output; 163 psa_status_t status; 164 165 local_output.buffer = NULL; 166 167 TEST_CALLOC(output, output_len); 168 169 status = psa_crypto_local_output_alloc(output, output_len, &local_output); 170 TEST_EQUAL(status, exp_status); 171 172 if (exp_status == PSA_SUCCESS) { 173 TEST_ASSERT(local_output.original == output); 174 TEST_EQUAL(local_output.length, output_len); 175 } 176 177exit: 178 mbedtls_free(local_output.buffer); 179 local_output.original = NULL; 180 local_output.buffer = NULL; 181 local_output.length = 0; 182 mbedtls_free(output); 183 output = NULL; 184} 185/* END_CASE */ 186 187/* BEGIN_CASE */ 188void local_output_free(int output_len, int original_is_null, 189 psa_status_t exp_status) 190{ 191 uint8_t *output = NULL; 192 uint8_t *buffer_copy_for_comparison = NULL; 193 psa_crypto_local_output_t local_output = PSA_CRYPTO_LOCAL_OUTPUT_INIT; 194 psa_status_t status; 195 196 if (!original_is_null) { 197 TEST_CALLOC(output, output_len); 198 } 199 TEST_CALLOC(buffer_copy_for_comparison, output_len); 200 TEST_CALLOC(local_output.buffer, output_len); 201 local_output.length = output_len; 202 local_output.original = output; 203 204 if (local_output.length != 0) { 205 fill_buffer_pattern(local_output.buffer, local_output.length); 206 memcpy(buffer_copy_for_comparison, local_output.buffer, local_output.length); 207 } 208 209 status = psa_crypto_local_output_free(&local_output); 210 TEST_EQUAL(status, exp_status); 211 212 if (exp_status == PSA_SUCCESS) { 213 MBEDTLS_TEST_MEMORY_UNPOISON(output, output_len); 214 TEST_ASSERT(local_output.buffer == NULL); 215 TEST_EQUAL(local_output.length, 0); 216 TEST_MEMORY_COMPARE(buffer_copy_for_comparison, output_len, 217 output, output_len); 218 } 219 220exit: 221 mbedtls_free(output); 222 mbedtls_free(buffer_copy_for_comparison); 223 mbedtls_free(local_output.buffer); 224 local_output.length = 0; 225} 226/* END_CASE */ 227 228/* BEGIN_CASE */ 229void local_output_round_trip() 230{ 231 psa_crypto_local_output_t local_output; 232 uint8_t output[200]; 233 uint8_t *buffer_copy_for_comparison = NULL; 234 psa_status_t status; 235 236 status = psa_crypto_local_output_alloc(output, sizeof(output), &local_output); 237 TEST_EQUAL(status, PSA_SUCCESS); 238 TEST_ASSERT(local_output.buffer != output); 239 240 /* Simulate the function generating output */ 241 fill_buffer_pattern(local_output.buffer, local_output.length); 242 243 TEST_CALLOC(buffer_copy_for_comparison, local_output.length); 244 memcpy(buffer_copy_for_comparison, local_output.buffer, local_output.length); 245 246 psa_crypto_local_output_free(&local_output); 247 TEST_ASSERT(local_output.buffer == NULL); 248 TEST_EQUAL(local_output.length, 0); 249 250 MBEDTLS_TEST_MEMORY_UNPOISON(output, sizeof(output)); 251 /* Check that the buffer was correctly copied back */ 252 TEST_MEMORY_COMPARE(output, sizeof(output), 253 buffer_copy_for_comparison, sizeof(output)); 254 255exit: 256 mbedtls_free(buffer_copy_for_comparison); 257} 258/* END_CASE */ 259