TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
its_tests_common.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "its_tests_common.h"
10 #if DOMAIN_NS == 1
11 #include <string.h>
12 #else
13 #include "tfm_memory_utils.h"
14 #endif
15 
16 #define TEST_019_CYCLES 3U
17 
18 static const uint8_t write_asset_data[ITS_MAX_ASSET_SIZE] = {0xBF};
19 static uint8_t read_asset_data[ITS_MAX_ASSET_SIZE] = {0};
20 
22 {
23  psa_status_t status;
24  const psa_storage_uid_t uid = TEST_UID_1;
26  const size_t data_len = 0;
27  const uint8_t write_data[] = {0};
28 
29  /* Set with no data and no flags and a valid UID */
30  status = psa_its_set(uid, data_len, write_data, flags);
31  if (status != PSA_SUCCESS) {
32  TEST_FAIL("Set should not fail with valid UID");
33  return;
34  }
35 
36  /* Attempt to set a second time */
37  status = psa_its_set(uid, data_len, write_data, flags);
38  if (status != PSA_SUCCESS) {
39  TEST_FAIL("Set should not fail the second time with valid UID");
40  return;
41  }
42 
43  /* Set with an invalid UID */
44  status = psa_its_set(INVALID_UID, data_len, write_data, flags);
45  if (status != PSA_ERROR_INVALID_ARGUMENT) {
46  TEST_FAIL("Set should not succeed with an invalid UID");
47  return;
48  }
49 
50  /* Call remove to clean up storage for the next test */
51  status = psa_its_remove(uid);
52  if (status != PSA_SUCCESS) {
53  TEST_FAIL("Remove should not fail with valid UID");
54  return;
55  }
56 
57  ret->val = TEST_PASSED;
58 }
59 
61 {
62  psa_status_t status;
63  const psa_storage_uid_t uid = TEST_UID_2;
65  const size_t data_len = WRITE_DATA_SIZE;
66  const uint8_t write_data[] = WRITE_DATA;
67 
68  /* Set with no flags */
69  status = psa_its_set(WRITE_ONCE_UID, data_len, write_data, flags);
70  if (status == PSA_SUCCESS) {
71  /* Set with valid flag: PSA_STORAGE_FLAG_WRITE_ONCE (with previously
72  * created UID)
73  * Note: Once created, WRITE_ONCE_UID cannot be deleted. It is reused
74  * across multiple tests.
75  */
78  if (status != PSA_SUCCESS) {
79  TEST_FAIL("Set should not fail with valid flags (existing UID)");
80  return;
81  }
82  } else if (status == PSA_ERROR_NOT_PERMITTED) {
83  /* The UID has already been created with the PSA_STORAGE_FLAG_WRITE_ONCE
84  * flag in a previous test run, so skip creating it again and emit a
85  * warning.
86  */
87  TEST_LOG("Note: The UID in this test has already been created with\r\n"
88  "the PSA_STORAGE_FLAG_WRITE_ONCE flag in a previous test\r\n"
89  "run. Wipe the storage area to run the full test.\r\n");
90  } else {
91  TEST_FAIL("Set should not fail with no flags");
92  return;
93  }
94 
95  /* Set with invalid flags */
96  status = psa_its_set(uid, data_len, write_data, INVALID_FLAG);
97  if (status != PSA_ERROR_NOT_SUPPORTED) {
98  TEST_FAIL("Set should not succeed with invalid flags");
99  return;
100  }
101 
102  ret->val = TEST_PASSED;
103 }
104 
106 {
107  psa_status_t status;
108  const psa_storage_uid_t uid = TEST_UID_3;
110  const size_t data_len = 0;
111 
112  /* Set with NULL data pointer */
113  status = psa_its_set(uid, data_len, NULL, flags);
114  if (status != PSA_SUCCESS) {
115  TEST_FAIL("Set should succeed with NULL data pointer and zero length");
116  return;
117  }
118 
119  ret->val = TEST_PASSED;
120 }
121 
123 {
124  psa_status_t status;
125  const psa_storage_uid_t uid = WRITE_ONCE_UID;
127  const size_t write_len = WRITE_DATA_SIZE;
128  const size_t read_len = WRITE_ONCE_DATA_SIZE;
129  const size_t offset = 0;
130  const uint8_t write_data[] = WRITE_DATA;
131  uint8_t read_data[] = WRITE_ONCE_READ_DATA;
132  size_t read_data_length = 0;
133  int comp_result;
134 
135  /* Set a write once UID a second time */
136  status = psa_its_set(uid, write_len, write_data, flags);
137  if (status != PSA_ERROR_NOT_PERMITTED) {
138  TEST_FAIL("Set should not rewrite a write once UID");
139  return;
140  }
141 
142  /* Get write once data */
143  status = psa_its_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
144  &read_data_length);
145  if (status != PSA_SUCCESS) {
146  TEST_FAIL("Get should not fail");
147  return;
148  }
149 
150 #if DOMAIN_NS == 1U
151  /* Check that write once data has not changed */
152  comp_result = memcmp(read_data, WRITE_ONCE_RESULT_DATA, sizeof(read_data));
153 #else
154  comp_result = tfm_memcmp(read_data, WRITE_ONCE_RESULT_DATA,
155  sizeof(read_data));
156 #endif
157  if (comp_result != 0) {
158  TEST_FAIL("Write once data should not have changed");
159  return;
160  }
161 
162  /* Check that write once data length has not changed */
163  if (read_data_length != read_len) {
164  TEST_FAIL("Write once data length should not have changed");
165  return;
166  }
167 
168  ret->val = TEST_PASSED;
169 }
170 
172 {
173  psa_status_t status;
174  const psa_storage_uid_t uid = TEST_UID_2;
176  size_t data_len = WRITE_DATA_SIZE;
177  size_t offset = 0;
178  const uint8_t write_data[] = WRITE_DATA;
179  uint8_t read_data[] = READ_DATA;
180  const uint8_t *p_read_data = read_data;
181  size_t read_data_length = 0;
182  int comp_result;
183 
184  status = psa_its_set(uid, data_len, write_data, flags);
185  if (status != PSA_SUCCESS) {
186  TEST_FAIL("Set should not fail");
187  return;
188  }
189 
190  /* Get the entire data */
191  status = psa_its_get(uid, offset, data_len, read_data + HALF_PADDING_SIZE,
192  &read_data_length);
193  if (status != PSA_SUCCESS) {
194  TEST_FAIL("Get should not fail");
195  return;
196  }
197 
198 #if DOMAIN_NS == 1U
199  /* Check that the data is correct, including no illegal pre- or post-data */
200  comp_result = memcmp(read_data, RESULT_DATA, sizeof(read_data));
201 #else
202  comp_result = tfm_memcmp(read_data, RESULT_DATA, sizeof(read_data));
203 #endif
204  if (comp_result != 0) {
205  TEST_FAIL("Read data should be equal to result data");
206  return;
207  }
208 
209  /* Check that the length of data is correct */
210  if (read_data_length != data_len) {
211  TEST_FAIL("Read data length should be equal to requested data length");
212  return;
213  }
214 
215 #if DOMAIN_NS == 1U
216  /* Reset read data */
217  memcpy(read_data, READ_DATA, sizeof(read_data));
218 #else
219  tfm_memcpy(read_data, READ_DATA, sizeof(read_data));
220 #endif
221 
222  /* Read from offset 2 to 2 bytes before end of the data */
223  offset = 2;
224  data_len -= offset + 2;
225 
226  status = psa_its_get(uid, offset, data_len, read_data + HALF_PADDING_SIZE,
227  &read_data_length);
228  if (status != PSA_SUCCESS) {
229  TEST_FAIL("Get should not fail");
230  return;
231  }
232 
233 #if DOMAIN_NS == 1U
234  /* Check that the correct data was read */
235  comp_result = memcmp(p_read_data, "____", HALF_PADDING_SIZE);
236 #else
237  comp_result = tfm_memcmp(p_read_data, "____", HALF_PADDING_SIZE);
238 #endif
239  if (comp_result != 0) {
240  TEST_FAIL("Read data contains illegal pre-data");
241  return;
242  }
243 
244  p_read_data += HALF_PADDING_SIZE;
245 
246 #if DOMAIN_NS == 1U
247  comp_result = memcmp(p_read_data, write_data + offset, data_len);
248 #else
249  comp_result = tfm_memcmp(p_read_data, write_data + offset, data_len);
250 #endif
251  if (comp_result != 0) {
252  TEST_FAIL("Read data incorrect");
253  return;
254  }
255 
256  p_read_data += data_len;
257 
258 #if DOMAIN_NS == 1U
259  comp_result = memcmp(p_read_data, "____", HALF_PADDING_SIZE);
260 #else
261  comp_result = tfm_memcmp(p_read_data, "____", HALF_PADDING_SIZE);
262 #endif
263  if (comp_result != 0) {
264  TEST_FAIL("Read data contains illegal post-data");
265  return;
266  }
267 
268  /* Call remove to clean up storage for the next test */
269  status = psa_its_remove(uid);
270  if (status != PSA_SUCCESS) {
271  TEST_FAIL("Remove should not fail with valid UID");
272  return;
273  }
274 
275  ret->val = TEST_PASSED;
276 }
277 
279 {
280  psa_status_t status;
281  const psa_storage_uid_t uid = TEST_UID_3;
283  const size_t write_len = WRITE_DATA_SIZE;
284  const size_t read_len = 0;
285  size_t offset = 0;
286  const uint8_t write_data[] = WRITE_DATA;
287  uint8_t read_data[] = READ_DATA;
288  size_t read_data_length = 1;
289  int comp_result;
290 
291  status = psa_its_set(uid, write_len, write_data, flags);
292  if (status != PSA_SUCCESS) {
293  TEST_FAIL("Set should not fail");
294  return;
295  }
296 
297  /* Get zero data from zero offset */
298  status = psa_its_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
299  &read_data_length);
300  if (status != PSA_SUCCESS) {
301  TEST_FAIL("Get should not fail with zero data len");
302  return;
303  }
304 
305 #if DOMAIN_NS == 1U
306  /* Check that the read data is unchanged */
307  comp_result = memcmp(read_data, READ_DATA, sizeof(read_data));
308 #else
309  comp_result = tfm_memcmp(read_data, READ_DATA, sizeof(read_data));
310 #endif
311  if (comp_result != 0) {
312  TEST_FAIL("Read data should be equal to original read data");
313  return;
314  }
315 
316  /* Check that the read data length is zero */
317  if (read_data_length != 0) {
318  TEST_FAIL("Read data length should be equal to zero");
319  return;
320  }
321 
322  offset = 5;
323  read_data_length = 1;
324 
325  /* Get zero data from non-zero offset */
326  status = psa_its_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
327  &read_data_length);
328  if (status != PSA_SUCCESS) {
329  TEST_FAIL("Get should not fail");
330  return;
331  }
332 
333 #if DOMAIN_NS == 1U
334  /* Check that the read data is unchanged */
335  comp_result = memcmp(read_data, READ_DATA, sizeof(read_data));
336 #else
337  comp_result = tfm_memcmp(read_data, READ_DATA, sizeof(read_data));
338 #endif
339  if (comp_result != 0) {
340  TEST_FAIL("Read data should be equal to original read data");
341  return;
342  }
343 
344  /* Check that the read data length is zero */
345  if (read_data_length != 0) {
346  TEST_FAIL("Read data length should be equal to zero");
347  return;
348  }
349 
350  /* Call remove to clean up storage for the next test */
351  status = psa_its_remove(uid);
352  if (status != PSA_SUCCESS) {
353  TEST_FAIL("Remove should not fail with valid UID");
354  return;
355  }
356 
357  ret->val = TEST_PASSED;
358 }
359 
361 {
362  psa_status_t status;
363  const psa_storage_uid_t uid = TEST_UID_1;
364  const size_t data_len = 1;
365  const size_t offset = 0;
366  uint8_t read_data[] = READ_DATA;
367  size_t read_data_length = 0;
368  int comp_result;
369 
370  /* Get with UID that has not yet been set */
371  status = psa_its_get(uid, offset, data_len, read_data + HALF_PADDING_SIZE,
372  &read_data_length);
373  if (status != PSA_ERROR_DOES_NOT_EXIST) {
374  TEST_FAIL("Get succeeded with non-existant UID");
375  return;
376  }
377 
378 #if DOMAIN_NS == 1U
379  /* Check that the read data is unchanged */
380  comp_result = memcmp(read_data, READ_DATA, sizeof(read_data));
381 #else
382  comp_result = tfm_memcmp(read_data, READ_DATA, sizeof(read_data));
383 #endif
384  if (comp_result != 0) {
385  TEST_FAIL("Read data not equal to original read data");
386  return;
387  }
388 
389  /* Get with invalid UID */
390  status = psa_its_get(INVALID_UID, offset, data_len,
391  read_data + HALF_PADDING_SIZE, &read_data_length);
392  if (status != PSA_ERROR_INVALID_ARGUMENT) {
393  TEST_FAIL("Get succeeded with invalid UID");
394  return;
395  }
396 
397 #if DOMAIN_NS == 1U
398  /* Check that the read data is unchanged */
399  comp_result = memcmp(read_data, READ_DATA, sizeof(read_data));
400 #else
401  comp_result = tfm_memcmp(read_data, READ_DATA, sizeof(read_data));
402 #endif
403  if (comp_result != 0) {
404  TEST_FAIL("Read data not equal to original read data");
405  return;
406  }
407 
408  ret->val = TEST_PASSED;
409 }
410 
412 {
413  psa_status_t status;
414  const psa_storage_uid_t uid = TEST_UID_2;
416  const size_t write_len = WRITE_DATA_SIZE;
417  size_t read_len;
418  size_t offset;
419  const uint8_t write_data[] = WRITE_DATA;
420  uint8_t read_data[] = READ_DATA;
421  size_t read_data_length = 0;
422  int comp_result;
423 
424  status = psa_its_set(uid, write_len, write_data, flags);
425  if (status != PSA_SUCCESS) {
426  TEST_FAIL("Set should not fail");
427  return;
428  }
429 
430  /* Get with offset greater than UID's length */
431  read_len = 1;
432  offset = write_len + 1;
433 
434  status = psa_its_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
435  &read_data_length);
436  if (status != PSA_ERROR_INVALID_ARGUMENT) {
437  TEST_FAIL("Get should not succeed with offset too large");
438  return;
439  }
440 
441 #if DOMAIN_NS == 1U
442  /* Check that the read data is unchanged */
443  comp_result = memcmp(read_data, READ_DATA, sizeof(read_data));
444 #else
445  comp_result = tfm_memcmp(read_data, READ_DATA, sizeof(read_data));
446 #endif
447  if (comp_result != 0) {
448  TEST_FAIL("Read data should be equal to original read data");
449  return;
450  }
451 
452  /* Get with data length greater than UID's length */
453  read_len = write_len + 1;
454  offset = 0;
455 
456  status = psa_its_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
457  &read_data_length);
458  if (status != PSA_SUCCESS) {
459  TEST_FAIL("Get should succeed with data length larger than UID's "
460  "length");
461  return;
462  }
463 
464  if (read_data_length != write_len) {
465  TEST_FAIL("Read data length should be equal to UID's length");
466  return;
467  }
468 
469 #if DOMAIN_NS == 1U
470  /* Check that the read data is changed */
471  comp_result = memcmp(read_data, RESULT_DATA, sizeof(read_data));
472 #else
473  comp_result = tfm_memcmp(read_data, RESULT_DATA, sizeof(read_data));
474 #endif
475  if (comp_result != 0) {
476  TEST_FAIL("Read data should be equal to newly read data");
477  return;
478  }
479 
480  /* Get with offset + data length greater than UID's length, but individually
481  * valid
482  */
483 #if DOMAIN_NS == 1U
484  /* Reset read_data to original READ_DATA */
485  memcpy(read_data, READ_DATA, sizeof(read_data));
486 #else
487  tfm_memcpy(read_data, READ_DATA, sizeof(read_data));
488 #endif
489  read_len = write_len;
490  offset = 1;
491 
492  status = psa_its_get(uid, offset, read_len,
493  read_data + HALF_PADDING_SIZE,
494  &read_data_length);
495  if (status != PSA_SUCCESS) {
496  TEST_FAIL("Get should succeed with offset + data length too large, "
497  "but individually valid");
498  return;
499  }
500 
501  if (read_data_length != write_len - offset) {
502  TEST_FAIL("Read data length should be equal to the UID's remaining "
503  "size starting from offset");
504  return;
505  }
506 
507 #if DOMAIN_NS == 1U
508  /* Check that the read data is changed */
509  comp_result = memcmp(read_data, OFFSET_RESULT_DATA, sizeof(read_data));
510 #else
511  comp_result = tfm_memcmp(read_data, OFFSET_RESULT_DATA, sizeof(read_data));
512 #endif
513  if (comp_result != 0) {
514  TEST_FAIL("Read data should be equal to newly read data starting at "
515  "offset");
516  return;
517  }
518 
519  /* Call remove to clean up storage for the next test */
520  status = psa_its_remove(uid);
521  if (status != PSA_SUCCESS) {
522  TEST_FAIL("Remove should not fail with valid UID");
523  return;
524  }
525 
526  ret->val = TEST_PASSED;
527 }
528 
530 {
531  psa_status_t status;
532  const psa_storage_uid_t uid = TEST_UID_3;
534  const size_t data_len = WRITE_DATA_SIZE;
535  const size_t offset = 0;
536  const uint8_t write_data[] = WRITE_DATA;
537  size_t read_data_length = 0;
538 
539  status = psa_its_set(uid, data_len, write_data, flags);
540  if (status != PSA_SUCCESS) {
541  TEST_FAIL("Set should not fail");
542  return;
543  }
544 
545  /* Get with NULL data pointer */
546  status = psa_its_get(uid, offset, 0, NULL, &read_data_length);
547  if (status != PSA_SUCCESS) {
548  TEST_FAIL("Get should succeed with NULL data pointer and zero length");
549  return;
550  }
551 
552  /* Check that the read data length is unchanged */
553  if (read_data_length != 0) {
554  TEST_FAIL("Read data length should be 0 with NULL data pointer and "
555  "zero length");
556  return;
557  }
558 
559  /* Call remove to clean up storage for the next test */
560  status = psa_its_remove(uid);
561  if (status != PSA_SUCCESS) {
562  TEST_FAIL("Remove should not fail with valid UID");
563  return;
564  }
565 
566  ret->val = TEST_PASSED;
567 }
568 
570 {
571  psa_status_t status;
572  const psa_storage_uid_t uid = WRITE_ONCE_UID;
573  struct psa_storage_info_t info = {0};
574 
575  /* Get info for write once UID */
576  status = psa_its_get_info(uid, &info);
577  if (status != PSA_SUCCESS) {
578  TEST_FAIL("Get info should not fail for write once UID");
579  return;
580  }
581 
582  /* Check that the info struct contains the correct values */
583  if (info.size != WRITE_ONCE_DATA_SIZE) {
584  TEST_FAIL("Size incorrect for write once UID");
585  return;
586  }
587 
588  if (info.capacity != WRITE_ONCE_DATA_SIZE) {
589  TEST_FAIL("Capacity incorrect for write once UID");
590  return;
591  }
592 
593  if (info.flags != PSA_STORAGE_FLAG_WRITE_ONCE) {
594  TEST_FAIL("Flags incorrect for write once UID");
595  return;
596  }
597 
598  ret->val = TEST_PASSED;
599 }
600 
602 {
603  psa_status_t status;
604  const psa_storage_uid_t uid = TEST_UID_1;
605  struct psa_storage_info_t info = {0};
607  const size_t data_len = WRITE_DATA_SIZE;
608  const uint8_t write_data[] = WRITE_DATA;
609 
610  status = psa_its_set(uid, data_len, write_data, flags);
611  if (status != PSA_SUCCESS) {
612  TEST_FAIL("Set should not fail");
613  return;
614  }
615 
616  /* Get info for valid UID */
617  status = psa_its_get_info(uid, &info);
618  if (status != PSA_SUCCESS) {
619  TEST_FAIL("Get info should not fail with valid UID");
620  return;
621  }
622 
623  /* Check that the info struct contains the correct values */
624  if (info.size != data_len) {
625  TEST_FAIL("Size incorrect for valid UID");
626  return;
627  }
628 
629  if (info.capacity != data_len) {
630  TEST_FAIL("Capacity incorrect for valid UID");
631  return;
632  }
633 
634  if (info.flags != flags) {
635  TEST_FAIL("Flags incorrect for valid UID");
636  return;
637  }
638 
639  /* Call remove to clean up storage for the next test */
640  status = psa_its_remove(uid);
641  if (status != PSA_SUCCESS) {
642  TEST_FAIL("Remove should not fail with valid UID");
643  return;
644  }
645 
646  ret->val = TEST_PASSED;
647 }
648 
650 {
651  psa_status_t status;
652  const psa_storage_uid_t uid = TEST_UID_2;
653  struct psa_storage_info_t info = {0};
654 
655  /* Get info with UID that has not yet been set */
656  status = psa_its_get_info(uid, &info);
657  if (status != PSA_ERROR_DOES_NOT_EXIST) {
658  TEST_FAIL("Get info should not succeed with unset UID");
659  return;
660  }
661 
662  /* Check that the info struct has not been modified */
663  if (info.size != 0) {
664  TEST_FAIL("Size should not have changed");
665  return;
666  }
667 
668  /* Get info with invalid UID */
669  status = psa_its_get_info(INVALID_UID, &info);
670  if (status != PSA_ERROR_INVALID_ARGUMENT) {
671  TEST_FAIL("Get info should not succeed with invalid UID");
672  return;
673  }
674 
675  /* Check that the info struct has not been modified */
676  if (info.size != 0) {
677  TEST_FAIL("Size should not have changed");
678  return;
679  }
680 
681  if (info.capacity != 0) {
682  TEST_FAIL("Capacity should not have changed");
683  return;
684  }
685 
686  if (info.flags != PSA_STORAGE_FLAG_NONE) {
687  TEST_FAIL("Flags should not have changed");
688  return;
689  }
690 
691  ret->val = TEST_PASSED;
692 }
693 
695 {
696  psa_status_t status;
697  const psa_storage_uid_t uid = TEST_UID_1;
698  struct psa_storage_info_t info = {0};
700  const size_t data_len = WRITE_DATA_SIZE;
701  const size_t offset = 0;
702  const uint8_t write_data[] = WRITE_DATA;
703  uint8_t read_data[] = READ_DATA;
704  size_t read_data_length = 0;
705 
706  status = psa_its_set(uid, data_len, write_data, flags);
707  if (status != PSA_SUCCESS) {
708  TEST_FAIL("Set should not fail");
709  return;
710  }
711 
712  /* Call remove with valid ID */
713  status = psa_its_remove(uid);
714  if (status != PSA_SUCCESS) {
715  TEST_FAIL("Remove should not fail with valid UID");
716  return;
717  }
718 
719  /* Check that get info fails for removed UID */
720  status = psa_its_get_info(uid, &info);
721  if (status != PSA_ERROR_DOES_NOT_EXIST) {
722  TEST_FAIL("Get info should not succeed with removed UID");
723  return;
724  }
725 
726  /* Check that get fails for removed UID */
727  status = psa_its_get(uid, offset, data_len, read_data, &read_data_length);
728  if (status != PSA_ERROR_DOES_NOT_EXIST) {
729  TEST_FAIL("Get should not succeed with removed UID");
730  return;
731  }
732 
733  /* Check that remove fails for removed UID */
734  status = psa_its_remove(uid);
735  if (status != PSA_ERROR_DOES_NOT_EXIST) {
736  TEST_FAIL("Remove should not succeed with removed UID");
737  return;
738  }
739 
740  ret->val = TEST_PASSED;
741 }
742 
744 {
745  psa_status_t status;
746  const psa_storage_uid_t uid = WRITE_ONCE_UID;
747 
748  /* Call remove with write once UID */
749  status = psa_its_remove(uid);
750  if (status != PSA_ERROR_NOT_PERMITTED) {
751  TEST_FAIL("Remove should not succeed with write once UID");
752  return;
753  }
754 
755  ret->val = TEST_PASSED;
756 }
757 
759 {
760  psa_status_t status;
761  const psa_storage_uid_t uid = INVALID_UID;
762 
763  /* Call remove with an invalid UID */
764  status = psa_its_remove(uid);
765  if (status != PSA_ERROR_INVALID_ARGUMENT) {
766  TEST_FAIL("Remove should not succeed with invalid UID");
767  return;
768  }
769 
770  ret->val = TEST_PASSED;
771 }
772 
774 {
775  psa_status_t status;
776  const psa_storage_uid_t uid_1 = TEST_UID_2;
777  const psa_storage_uid_t uid_2 = TEST_UID_3;
779  const size_t data_len_2 = WRITE_DATA_SIZE;
780  const size_t offset = 0;
781  const uint8_t write_data_1[] = "UID 1 DATA";
782  const uint8_t write_data_2[] = WRITE_DATA;
783  uint8_t read_data[] = READ_DATA;
784  size_t read_data_length = 0;
785  int comp_result;
786 
787  /* Set UID 1 */
788  status = psa_its_set(uid_1, sizeof(write_data_1), write_data_1, flags);
789  if (status != PSA_SUCCESS) {
790  TEST_FAIL("Set should not fail for UID 1");
791  return;
792  }
793 
794  /* Set UID 2 */
795  status = psa_its_set(uid_2, data_len_2, write_data_2, flags);
796  if (status != PSA_SUCCESS) {
797  TEST_FAIL("Set should not fail for UID 2");
798  return;
799  }
800 
801  /* Remove UID 1. This should cause UID 2 to be compacted to the beginning of
802  * the block.
803  */
804  status = psa_its_remove(uid_1);
805  if (status != PSA_SUCCESS) {
806  TEST_FAIL("Remove should not fail for UID 1");
807  return;
808  }
809 
810  /* If the compact worked as expected, the test should be able to read back
811  * the data from UID 2 correctly.
812  */
813  status = psa_its_get(uid_2, offset, data_len_2,
814  read_data + HALF_PADDING_SIZE, &read_data_length);
815  if (status != PSA_SUCCESS) {
816  TEST_FAIL("Get should not fail for UID 2");
817  return;
818  }
819 
820 #if DOMAIN_NS == 1U
821  comp_result = memcmp(read_data, RESULT_DATA, sizeof(read_data));
822 #else
823  comp_result = tfm_memcmp(read_data, RESULT_DATA, sizeof(read_data));
824 #endif
825  if (comp_result != 0) {
826  TEST_FAIL("Read buffer has incorrect data");
827  return;
828  }
829 
830  if (read_data_length != WRITE_DATA_SIZE) {
831  TEST_FAIL("Read data length should be equal to result data length");
832  return;
833  }
834 
835  /* Remove UID 2 to clean up storage for the next test */
836  status = psa_its_remove(uid_2);
837  if (status != PSA_SUCCESS) {
838  TEST_FAIL("Remove should not fail for UID 2");
839  return;
840  }
841 
842  ret->val = TEST_PASSED;
843 }
844 
846 {
847  psa_status_t status;
848  const psa_storage_uid_t uid = TEST_UID_1;
850  const size_t data_len = WRITE_DATA_SIZE;
851  size_t offset = 0;
852  const uint8_t write_data[] = WRITE_DATA;
853  uint8_t read_data[] = READ_DATA;
854  size_t read_data_length = 0;
855  int comp_result;
856 
857  /* Set the entire data into UID */
858  status = psa_its_set(uid, data_len, write_data, flags);
859  if (status != PSA_SUCCESS) {
860  TEST_FAIL("Set should not fail");
861  return;
862  }
863 
864  /* Get the data from UID one byte at a time */
865  for (offset = 0; offset < data_len; ++offset) {
866  status = psa_its_get(uid, offset, 1,
867  (read_data + HALF_PADDING_SIZE + offset),
868  &read_data_length);
869  if (status != PSA_SUCCESS) {
870  TEST_FAIL("Get should not fail for partial read");
871  return;
872  }
873  }
874 
875 #if DOMAIN_NS == 1U
876  comp_result = memcmp(read_data, RESULT_DATA, sizeof(read_data));
877 #else
878  comp_result = tfm_memcmp(read_data, RESULT_DATA, sizeof(read_data));
879 #endif
880  if (comp_result != 0) {
881  TEST_FAIL("Read buffer has incorrect data");
882  return;
883  }
884 
885  /* Remove UID to clean up storage for the next test */
886  status = psa_its_remove(uid);
887  if (status != PSA_SUCCESS) {
888  TEST_FAIL("Remove should not fail");
889  return;
890  }
891 
892  ret->val = TEST_PASSED;
893 }
894 
896 {
897  psa_status_t status;
898  const psa_storage_uid_t uid = TEST_UID_2;
900  const size_t offset = 0;
901  const uint8_t write_data_1[] = "ONE";
902  const uint8_t write_data_2[] = "TWO";
903  const uint8_t write_data_3[] = "THREE";
904  uint8_t read_data[] = READ_DATA;
905  size_t read_data_length = 0;
906  int comp_result;
907 
908  /* Set write data 1 into UID */
909  status = psa_its_set(uid, sizeof(write_data_1), write_data_1, flags);
910  if (status != PSA_SUCCESS) {
911  TEST_FAIL("First set should not fail");
912  return;
913  }
914 
915  /* Set write data 2 into UID */
916  status = psa_its_set(uid, sizeof(write_data_2), write_data_2, flags);
917  if (status != PSA_SUCCESS) {
918  TEST_FAIL("Second set should not fail");
919  return;
920  }
921 
922  /* Set write data 3 into UID */
923  status = psa_its_set(uid, sizeof(write_data_3), write_data_3, flags);
924  if (status != PSA_SUCCESS) {
925  TEST_FAIL("Third set should not fail");
926  return;
927  }
928 
929  status = psa_its_get(uid, offset, sizeof(write_data_3), read_data,
930  &read_data_length);
931  if (status != PSA_SUCCESS) {
932  TEST_FAIL("Get should not fail");
933  return;
934  }
935 
936 #if DOMAIN_NS == 1U
937  /* Check that get returns the last data to be set */
938  comp_result = memcmp(read_data, write_data_3, sizeof(write_data_3));
939 #else
940  comp_result = tfm_memcmp(read_data, write_data_3, sizeof(write_data_3));
941 #endif
942  if (comp_result != 0) {
943  TEST_FAIL("Read buffer has incorrect data");
944  return;
945  }
946 
947  if (read_data_length != sizeof(write_data_3)) {
948  TEST_FAIL("Read data length should be equal to result data length");
949  return;
950  }
951 
952  /* Remove UID to clean up storage for the next test */
953  status = psa_its_remove(uid);
954  if (status != PSA_SUCCESS) {
955  TEST_FAIL("Remove should not fail");
956  return;
957  }
958 
959  ret->val = TEST_PASSED;
960 }
961 
963 {
964  uint8_t cycle;
965  psa_status_t status;
966  int comp_result;
967  size_t read_data_length = 0;
968  const psa_storage_uid_t test_uid[TEST_019_CYCLES] = {
969  TEST_UID_1,
970  TEST_UID_2,
971  TEST_UID_3};
972  const size_t test_asset_sizes[TEST_019_CYCLES] = {
973  ITS_MAX_ASSET_SIZE >> 2,
974  ITS_MAX_ASSET_SIZE >> 1,
975  ITS_MAX_ASSET_SIZE};
976 
977  /* Loop to test different asset sizes and UID's*/
978  for (cycle = 0; cycle < TEST_019_CYCLES; cycle++) {
979  size_t data_size = test_asset_sizes[cycle];
980  psa_storage_uid_t uid = test_uid[cycle];
981  struct psa_storage_info_t info = {0};
982 
983 #if DOMAIN_NS == 1U
984  memset(read_asset_data, 0x00, sizeof(read_asset_data));
985 #else
986  tfm_memset(read_asset_data, 0x00, sizeof(read_asset_data));
987 #endif
988 
989  /* Set with data and no flags and a valid UID */
990  status = psa_its_set(uid,
991  data_size,
992  write_asset_data,
994  if (status != PSA_SUCCESS) {
995  TEST_FAIL("Set should not fail with valid UID");
996  return;
997  }
998 
999  /* Get info for valid UID */
1000  status = psa_its_get_info(uid, &info);
1001  if (status != PSA_SUCCESS) {
1002  TEST_FAIL("Get info should not fail with valid UID");
1003  return;
1004  }
1005 
1006  /* Check that the info struct contains the correct values */
1007  if (info.size != data_size) {
1008  TEST_FAIL("Size incorrect for valid UID");
1009  return;
1010  }
1011 
1012  if (info.flags != PSA_STORAGE_FLAG_NONE) {
1013  TEST_FAIL("Flags incorrect for valid UID");
1014  return;
1015  }
1016 
1017  /* Check that thread can still get UID */
1018  status = psa_its_get(uid, 0, data_size, read_asset_data,
1019  &read_data_length);
1020  if (status != PSA_SUCCESS) {
1021  TEST_FAIL("Get should not fail with valid UID");
1022  return;
1023  }
1024 
1025 #if DOMAIN_NS == 1U
1026  /* Check that get returns the last data which was set */
1027  comp_result = memcmp(read_asset_data, write_asset_data, data_size);
1028 #else
1029  comp_result = tfm_memcmp(read_asset_data, write_asset_data, data_size);
1030 #endif
1031  if (comp_result != 0) {
1032  TEST_FAIL("Read data should be equal to original write data");
1033  return;
1034  }
1035 
1036  if (read_data_length != data_size) {
1037  TEST_FAIL("Read data length should be equal to result data length");
1038  return;
1039  }
1040 
1041  /* Call remove to clean up storage for the next test */
1042  status = psa_its_remove(uid);
1043  if (status != PSA_SUCCESS) {
1044  TEST_FAIL("Remove should not fail with valid UID");
1045  return;
1046  }
1047  }
1048 
1049  ret->val = TEST_PASSED;
1050 }
void tfm_its_test_common_019(struct test_result_t *ret)
Tests set, get_info, get and remove function with:
#define WRITE_ONCE_READ_DATA
#define TEST_UID_2
__STATIC_INLINE void * tfm_memset(void *ptr, int value, size_t num)
#define PSA_SUCCESS
Definition: crypto_values.h:35
#define TEST_FAIL(info_msg)
#define TEST_UID_3
void tfm_its_test_common_016(struct test_result_t *ret)
Tests data block compact feature. Set UID 1 to locate it at the beginning of the block. Then set UID 2 to be located after UID 1 and remove UID 1. UID 2 will be compacted to the beginning of the block. This test verifies that the compaction works correctly by reading back UID 2.
void tfm_its_test_common_007(struct test_result_t *ret)
Tests get function with:
void tfm_its_test_common_012(struct test_result_t *ret)
Tests get info function with:
void tfm_its_test_common_010(struct test_result_t *ret)
Tests get info function with:
void tfm_its_test_common_002(struct test_result_t *ret)
Tests set function with:
void tfm_its_test_common_017(struct test_result_t *ret)
Tests set and multiple partial gets.
#define INVALID_FLAG
psa_status_t psa_its_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info)
Retrieve the metadata about the provided uid.
void tfm_its_test_common_011(struct test_result_t *ret)
Tests get info function with:
#define WRITE_ONCE_DATA_SIZE
#define WRITE_ONCE_RESULT_DATA
#define READ_DATA
#define PSA_STORAGE_FLAG_WRITE_ONCE
#define TEST_019_CYCLES
psa_status_t psa_its_get(psa_storage_uid_t uid, size_t data_offset, size_t data_size, void *p_data, size_t *p_data_length)
Retrieve data associated with a provided UID.
void tfm_its_test_common_008(struct test_result_t *ret)
Tests get function with:
psa_status_t psa_its_remove(psa_storage_uid_t uid)
Remove the provided uid and its associated data from the storage.
void tfm_its_test_common_005(struct test_result_t *ret)
Tests get function with:
void tfm_its_test_common_004(struct test_result_t *ret)
Tests set function with:
#define INVALID_UID
#define PSA_ERROR_INVALID_ARGUMENT
#define WRITE_DATA
psa_status_t psa_its_set(psa_storage_uid_t uid, size_t data_length, const void *p_data, psa_storage_create_flags_t create_flags)
Create a new, or modify an existing, uid/value pair.
#define PSA_ERROR_NOT_PERMITTED
Definition: crypto_values.h:65
struct test_result_t ret
void tfm_its_test_common_001(struct test_result_t *ret)
Tests set function with:
int memcmp(const void *s1, const void *s2, size_t n)
Definition: crt_memcmp.c:11
#define PSA_ERROR_NOT_SUPPORTED
Definition: crypto_values.h:52
#define HALF_PADDING_SIZE
__STATIC_INLINE void * tfm_memcpy(void *dest, const void *src, size_t num)
void * memcpy(void *dest, const void *src, size_t n)
Definition: crt_memcpy.c:10
#define WRITE_ONCE_UID
void tfm_its_test_common_015(struct test_result_t *ret)
Tests remove function with:
#define TEST_LOG(...)
uint64_t psa_storage_uid_t
#define WRITE_ONCE_DATA
void tfm_its_test_common_003(struct test_result_t *ret)
Tests set function with:
enum test_status_t val
void tfm_its_test_common_013(struct test_result_t *ret)
Tests remove function with:
#define RESULT_DATA
__STATIC_INLINE int tfm_memcmp(const void *ptr1, const void *ptr2, size_t num)
#define OFFSET_RESULT_DATA
psa_storage_create_flags_t flags
#define PSA_STORAGE_FLAG_NONE
#define WRITE_DATA_SIZE
void tfm_its_test_common_018(struct test_result_t *ret)
Tests multiple sets to the same UID.
#define PSA_ERROR_DOES_NOT_EXIST
Definition: crypto_values.h:89
uint32_t psa_storage_create_flags_t
void tfm_its_test_common_009(struct test_result_t *ret)
Tests get function with:
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:43
void tfm_its_test_common_014(struct test_result_t *ret)
Tests remove function with:
void * memset(void *s, int c, size_t n)
Definition: crt_memset.c:10
#define TEST_UID_1
void tfm_its_test_common_006(struct test_result_t *ret)
Tests get function with: