TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
psa_ps_ns_interface_testsuite.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 "ps_ns_tests.h"
9 
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include "ns_test_helpers.h"
14 #include "psa/protected_storage.h"
15 #include "test_framework_helpers.h"
16 
17 /* Test UIDs */
18 #define WRITE_ONCE_UID 1U /* Cannot be modified or deleted once created */
19 #define TEST_UID_1 2U
20 #define TEST_UID_2 3U
21 #define TEST_UID_3 4U
22 
23 /* Invalid values */
24 #define INVALID_UID 0U
25 #define INVALID_DATA_LEN UINT32_MAX
26 #define INVALID_OFFSET UINT32_MAX
27 #define INVALID_FLAG (1U << 31)
28 #define INVALID_THREAD_NAME "Thread_INVALID"
29 
30 /* Write once data */
31 #define WRITE_ONCE_DATA "THE_FIVE_BOXING_WIZARDS_JUMP_QUICKLY"
32 #define WRITE_ONCE_DATA_SIZE (sizeof(WRITE_ONCE_DATA) - 1)
33 #define WRITE_ONCE_READ_DATA "############################################"
34 #define WRITE_ONCE_RESULT_DATA ("####" WRITE_ONCE_DATA "####")
35 #define OFFSET_READ_DATA "HEQUICKBROWNFOXJUMPSOVERALAZYDOG"
36 #define OFFSET_RESULT_DATA ("____" OFFSET_READ_DATA "_____")
37 
38 #define WRITE_DATA "THEQUICKBROWNFOXJUMPSOVERALAZYDOG"
39 #define WRITE_DATA_SIZE (sizeof(WRITE_DATA) - 1)
40 #define READ_DATA "_________________________________________"
41 #define RESULT_DATA ("____" WRITE_DATA "____")
42 
43 #define TEST_1025_CYCLES 3U
44 
45 static const uint8_t write_asset_data[PS_MAX_ASSET_SIZE] = {0xAF};
46 static uint8_t read_asset_data[PS_MAX_ASSET_SIZE] = {0};
47 static size_t read_asset_data_len = 0;
48 
49 /* List of tests */
50 static void tfm_ps_test_1001(struct test_result_t *ret);
51 static void tfm_ps_test_1002(struct test_result_t *ret);
52 static void tfm_ps_test_1003(struct test_result_t *ret);
53 static void tfm_ps_test_1004(struct test_result_t *ret);
54 static void tfm_ps_test_1005(struct test_result_t *ret);
55 static void tfm_ps_test_1006(struct test_result_t *ret);
56 static void tfm_ps_test_1007(struct test_result_t *ret);
57 static void tfm_ps_test_1008(struct test_result_t *ret);
58 static void tfm_ps_test_1009(struct test_result_t *ret);
59 static void tfm_ps_test_1010(struct test_result_t *ret);
60 static void tfm_ps_test_1011(struct test_result_t *ret);
61 static void tfm_ps_test_1012(struct test_result_t *ret);
62 static void tfm_ps_test_1013(struct test_result_t *ret);
63 static void tfm_ps_test_1014(struct test_result_t *ret);
64 static void tfm_ps_test_1015(struct test_result_t *ret);
65 #ifdef TFM_NS_CLIENT_IDENTIFICATION
66 static void tfm_ps_test_1016(struct test_result_t *ret);
67 static void tfm_ps_test_1017(struct test_result_t *ret);
68 static void tfm_ps_test_1018(struct test_result_t *ret);
69 static void tfm_ps_test_1019(struct test_result_t *ret);
70 static void tfm_ps_test_1020(struct test_result_t *ret);
71 #endif /* TFM_NS_CLIENT_IDENTIFICATION */
72 static void tfm_ps_test_1021(struct test_result_t *ret);
73 static void tfm_ps_test_1022(struct test_result_t *ret);
74 static void tfm_ps_test_1023(struct test_result_t *ret);
75 static void tfm_ps_test_1024(struct test_result_t *ret);
76 static void tfm_ps_test_1025(struct test_result_t *ret);
77 
78 static struct test_t psa_ps_ns_tests[] = {
79  {&tfm_ps_test_1001, "TFM_PS_TEST_1001",
80  "Set interface"},
81  {&tfm_ps_test_1002, "TFM_PS_TEST_1002",
82  "Set interface with create flags"},
83  {&tfm_ps_test_1003, "TFM_PS_TEST_1003",
84  "Set interface with NULL data pointer"},
85  {&tfm_ps_test_1004, "TFM_PS_TEST_1004",
86  "Set interface with write once UID"},
87  {&tfm_ps_test_1005, "TFM_PS_TEST_1005",
88  "Get interface with valid data"},
89  {&tfm_ps_test_1006, "TFM_PS_TEST_1006",
90  "Get interface with zero data length"},
91  {&tfm_ps_test_1007, "TFM_PS_TEST_1007",
92  "Get interface with invalid UIDs"},
93  {&tfm_ps_test_1008, "TFM_PS_TEST_1008",
94  "Get interface with invalid data lengths and offsets"},
95  {&tfm_ps_test_1009, "TFM_PS_TEST_1009",
96  "Get interface with NULL data pointer"},
97  {&tfm_ps_test_1010, "TFM_PS_TEST_1010",
98  "Get info interface with write once UID"},
99  {&tfm_ps_test_1011, "TFM_PS_TEST_1011",
100  "Get info interface with valid UID"},
101  {&tfm_ps_test_1012, "TFM_PS_TEST_1012",
102  "Get info interface with invalid UIDs"},
103  {&tfm_ps_test_1013, "TFM_PS_TEST_1013",
104  "Remove interface with valid UID"},
105  {&tfm_ps_test_1014, "TFM_PS_TEST_1014",
106  "Remove interface with write once UID"},
107  {&tfm_ps_test_1015, "TFM_PS_TEST_1015",
108  "Remove interface with invalid UID"},
109 #ifdef TFM_NS_CLIENT_IDENTIFICATION
110  {&tfm_ps_test_1016, "TFM_PS_TEST_1016",
111  "Get interface with invalid thread name"},
112  {&tfm_ps_test_1017, "TFM_PS_TEST_1017",
113  "Get info interface with invalid thread name"},
114  {&tfm_ps_test_1018, "TFM_PS_TEST_1018",
115  "Remove interface with invalid thread name"},
116  {&tfm_ps_test_1019, "TFM_PS_TEST_1019",
117  "Attempt to access UID belonging to another thread"},
118  {&tfm_ps_test_1020, "TFM_PS_TEST_1020",
119  "Set UID alternately from two threads"},
120 #endif /* TFM_NS_CLIENT_IDENTIFICATION */
121  {&tfm_ps_test_1021, "TFM_PS_TEST_1021",
122  "Block compaction after remove"},
123  {&tfm_ps_test_1022, "TFM_PS_TEST_1022",
124  "Multiple partial gets"},
125  {&tfm_ps_test_1023, "TFM_PS_TEST_1023",
126  "Multiple sets to same UID from same thread"},
127  {&tfm_ps_test_1024, "TFM_PS_TEST_1024",
128  "Get support interface"},
129  {&tfm_ps_test_1025, "TFM_PS_TEST_1025",
130  "Set, get and remove interface with different asset sizes"},
131 };
132 
134 {
135  uint32_t list_size;
136 
137  list_size = (sizeof(psa_ps_ns_tests) / sizeof(psa_ps_ns_tests[0]));
138 
139  set_testsuite("PSA protected storage NS interface tests (TFM_PS_TEST_1XXX)",
140  psa_ps_ns_tests, list_size, p_test_suite);
141 }
142 
148 TFM_PS_NS_TEST(1001, "Thread_A")
149 {
150  psa_status_t status;
151  const psa_storage_uid_t uid = TEST_UID_1;
153  const uint32_t data_len = 0;
154  const uint8_t write_data[] = {0};
155 
156  /* Set with no data and no flags and a valid UID */
157  status = psa_ps_set(uid, data_len, write_data, flags);
158  if (status != PSA_SUCCESS) {
159  TEST_FAIL("Set should not fail with valid UID");
160  return;
161  }
162 
163  /* Attempt to set a second time */
164  status = psa_ps_set(uid, data_len, write_data, flags);
165  if (status != PSA_SUCCESS) {
166  TEST_FAIL("Set should not fail the second time with valid UID");
167  return;
168  }
169 
170  /* Set with an invalid UID */
171  status = psa_ps_set(INVALID_UID, data_len, write_data, flags);
172  if (status != PSA_ERROR_INVALID_ARGUMENT) {
173  TEST_FAIL("Set should not succeed with an invalid UID");
174  return;
175  }
176 
177  /* Call remove to clean up storage for the next test */
178  status = psa_ps_remove(uid);
179  if (status != PSA_SUCCESS) {
180  TEST_FAIL("Remove should not fail with valid UID");
181  return;
182  }
183 
184  ret->val = TEST_PASSED;
185 }
186 
193 TFM_PS_NS_TEST(1002, "Thread_A")
194 {
195  psa_status_t status;
196  const psa_storage_uid_t uid = TEST_UID_2;
198  const uint32_t data_len = WRITE_DATA_SIZE;
199  const uint8_t write_data[] = WRITE_DATA;
200 
201  /* Set with no flags */
202  status = psa_ps_set(WRITE_ONCE_UID, data_len, write_data, flags);
203  if (status == PSA_SUCCESS) {
204  /* Set with valid flag: PSA_STORAGE_FLAG_WRITE_ONCE (with previously
205  * created UID)
206  * Note: Once created, WRITE_ONCE_UID cannot be deleted. It is reused
207  * across multiple tests.
208  */
211  if (status != PSA_SUCCESS) {
212  TEST_FAIL("Set should not fail with valid flags (existing UID)");
213  return;
214  }
215  } else if (status == PSA_ERROR_NOT_PERMITTED) {
216  /* The UID has already been created with the PSA_STORAGE_FLAG_WRITE_ONCE
217  * flag in a previous test run, so skip creating it again and emit a
218  * warning.
219  */
220  TEST_LOG("Note: The UID in this test has already been created with\r\n"
221  "the PSA_STORAGE_FLAG_WRITE_ONCE flag in a previous test\r\n"
222  "run. Wipe the storage area to run the full test.\r\n");
223  } else {
224  TEST_FAIL("Set should not fail with no flags");
225  return;
226  }
227 
228  /* Set with invalid flags */
229  status = psa_ps_set(uid, data_len, write_data, INVALID_FLAG);
230  if (status != PSA_ERROR_NOT_SUPPORTED) {
231  TEST_FAIL("Set should not succeed with invalid flags");
232  return;
233  }
234 
235  ret->val = TEST_PASSED;
236 }
237 
247 TFM_PS_NS_TEST(1003, "Thread_A")
248 {
249  psa_status_t status;
250  const psa_storage_uid_t uid = TEST_UID_3;
252  const uint32_t data_len = 0;
253 
254  /* Set with NULL data pointer */
255  status = psa_ps_set(uid, data_len, NULL, flags);
256  if (status != PSA_SUCCESS) {
257  TEST_FAIL("Set should succeed with NULL data pointer and zero length");
258  return;
259  }
260 
261  ret->val = TEST_PASSED;
262 }
263 
268 TFM_PS_NS_TEST(1004, "Thread_A")
269 {
270  psa_status_t status;
271  const psa_storage_uid_t uid = WRITE_ONCE_UID;
273  const uint32_t write_len = WRITE_DATA_SIZE;
274  const uint32_t read_len = WRITE_ONCE_DATA_SIZE;
275  const uint32_t offset = 0;
276  const uint8_t write_data[] = WRITE_DATA;
277  uint8_t read_data[] = WRITE_ONCE_READ_DATA;
278  size_t read_data_len = 0;
279 
280  /* Set a write once UID a second time */
281  status = psa_ps_set(uid, write_len, write_data, flags);
282  if (status != PSA_ERROR_NOT_PERMITTED) {
283  TEST_FAIL("Set should not rewrite a write once UID");
284  return;
285  }
286 
287  /* Get write once data */
288  status = psa_ps_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
289  &read_data_len);
290  if (status != PSA_SUCCESS) {
291  TEST_FAIL("Get should not fail");
292  return;
293  }
294 
295  /* Check that write once data has not changed */
296  if (memcmp(read_data, WRITE_ONCE_RESULT_DATA, sizeof(read_data)) != 0) {
297  TEST_FAIL("Write once data should not have changed");
298  return;
299  }
300 
301  ret->val = TEST_PASSED;
302 }
303 
309 TFM_PS_NS_TEST(1005, "Thread_A")
310 {
311  psa_status_t status;
312  const psa_storage_uid_t uid = TEST_UID_2;
314  uint32_t data_len = WRITE_DATA_SIZE;
315  uint32_t offset = 0;
316  const uint8_t write_data[] = WRITE_DATA;
317  uint8_t read_data[] = READ_DATA;
318  size_t read_data_len = 0;
319 
320  const uint8_t *p_read_data = read_data;
321 
322  status = psa_ps_set(uid, data_len, write_data, flags);
323  if (status != PSA_SUCCESS) {
324  TEST_FAIL("Set should not fail");
325  return;
326  }
327 
328  /* Get the entire data */
329  status = psa_ps_get(uid, offset, data_len, read_data + HALF_PADDING_SIZE,
330  &read_data_len);
331  if (status != PSA_SUCCESS) {
332  TEST_FAIL("Get should not fail");
333  return;
334  }
335 
336  /* Check that the data is correct, including no illegal pre- or post-data */
337  if (memcmp(read_data, RESULT_DATA, sizeof(read_data)) != 0) {
338  TEST_FAIL("Read data should be equal to result data");
339  return;
340  }
341 
342  /* Reset read data */
343  memcpy(read_data, READ_DATA, sizeof(read_data));
344 
345  /* Read from offset 2 to 2 bytes before end of the data */
346  offset = 2;
347  data_len -= offset + 2;
348 
349  status = psa_ps_get(uid, offset, data_len, read_data + HALF_PADDING_SIZE,
350  &read_data_len);
351  if (status != PSA_SUCCESS) {
352  TEST_FAIL("Get should not fail");
353  return;
354  }
355 
356  /* Check that the correct data was read */
357  if (memcmp(p_read_data, "____", HALF_PADDING_SIZE) != 0) {
358  TEST_FAIL("Read data contains illegal pre-data");
359  return;
360  }
361 
362  p_read_data += HALF_PADDING_SIZE;
363 
364  if (memcmp(p_read_data, write_data + offset, data_len) != 0) {
365  TEST_FAIL("Read data incorrect");
366  return;
367  }
368 
369  p_read_data += data_len;
370 
371  if (memcmp(p_read_data, "____", HALF_PADDING_SIZE) != 0) {
372  TEST_FAIL("Read data contains illegal post-data");
373  return;
374  }
375 
376  /* Call remove to clean up storage for the next test */
377  status = psa_ps_remove(uid);
378  if (status != PSA_SUCCESS) {
379  TEST_FAIL("Remove should not fail with valid UID");
380  return;
381  }
382 
383  ret->val = TEST_PASSED;
384 }
385 
391 TFM_PS_NS_TEST(1006, "Thread_A")
392 {
393  psa_status_t status;
394  const psa_storage_uid_t uid = TEST_UID_3;
396  const uint32_t write_len = WRITE_DATA_SIZE;
397  const uint32_t read_len = 0;
398  uint32_t offset = 0;
399  const uint8_t write_data[] = WRITE_DATA;
400  uint8_t read_data[] = READ_DATA;
401  size_t read_data_len = 0;
402 
403  status = psa_ps_set(uid, write_len, write_data, flags);
404  if (status != PSA_SUCCESS) {
405  TEST_FAIL("Set should not fail");
406  return;
407  }
408 
409  /* Get zero data from zero offset */
410  status = psa_ps_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
411  &read_data_len);
412  if (status != PSA_SUCCESS) {
413  TEST_FAIL("Get should not fail with zero data len");
414  return;
415  }
416 
417  /* Check that the read data is unchanged */
418  if (memcmp(read_data, READ_DATA, sizeof(read_data)) != 0) {
419  TEST_FAIL("Read data should be equal to original read data");
420  return;
421  }
422 
423  offset = 5;
424 
425  /* Get zero data from non-zero offset */
426  status = psa_ps_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
427  &read_data_len);
428  if (status != PSA_SUCCESS) {
429  TEST_FAIL("Get should not fail");
430  return;
431  }
432 
433  /* Check that the read data is unchanged */
434  if (memcmp(read_data, READ_DATA, sizeof(read_data)) != 0) {
435  TEST_FAIL("Read data should be equal to original read data");
436  return;
437  }
438 
439  /* Call remove to clean up storage for the next test */
440  status = psa_ps_remove(uid);
441  if (status != PSA_SUCCESS) {
442  TEST_FAIL("Remove should not fail with valid UID");
443  return;
444  }
445 
446  ret->val = TEST_PASSED;
447 }
448 
454 TFM_PS_NS_TEST(1007, "Thread_A")
455 {
456  psa_status_t status;
457  const psa_storage_uid_t uid = TEST_UID_1;
458  const uint32_t data_len = 1;
459  const uint32_t offset = 0;
460  uint8_t read_data[] = READ_DATA;
461  size_t read_data_len = 0;
462 
463  /* Get with UID that has not yet been set */
464  status = psa_ps_get(uid, offset, data_len, read_data + HALF_PADDING_SIZE,
465  &read_data_len);
466  if (status != PSA_ERROR_DOES_NOT_EXIST) {
467  TEST_FAIL("Get succeeded with non-existant UID");
468  return;
469  }
470 
471  /* Check that the read data is unchanged */
472  if (memcmp(read_data, READ_DATA, sizeof(read_data)) != 0) {
473  TEST_FAIL("Read data not equal to original read data");
474  return;
475  }
476 
477  /* Get with invalid UID */
478  status = psa_ps_get(INVALID_UID, offset, data_len,
479  read_data + HALF_PADDING_SIZE, &read_data_len);
480  if (status != PSA_ERROR_INVALID_ARGUMENT) {
481  TEST_FAIL("Get succeeded with invalid UID");
482  return;
483  }
484 
485  /* Check that the read data is unchanged */
486  if (memcmp(read_data, READ_DATA, sizeof(read_data)) != 0) {
487  TEST_FAIL("Read data not equal to original read data");
488  return;
489  }
490 
491  ret->val = TEST_PASSED;
492 }
493 
500 TFM_PS_NS_TEST(1008, "Thread_A")
501 {
502  psa_status_t status;
503  const psa_storage_uid_t uid = TEST_UID_2;
505  const uint32_t write_len = WRITE_DATA_SIZE;
506  uint32_t read_len;
507  uint32_t offset;
508  const uint8_t write_data[] = WRITE_DATA;
509  uint8_t read_data[] = READ_DATA;
510  size_t read_data_len = 0;
511 
512  status = psa_ps_set(uid, write_len, write_data, flags);
513  if (status != PSA_SUCCESS) {
514  TEST_FAIL("Set should not fail");
515  return;
516  }
517 
518  /* Get with offset greater than UID's length */
519  read_len = 1;
520  offset = write_len + 1;
521 
522  status = psa_ps_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
523  &read_data_len);
524  if (status != PSA_ERROR_INVALID_ARGUMENT) {
525  TEST_FAIL("Get should not succeed with offset too large");
526  return;
527  }
528 
529  /* Check that the read data is unchanged */
530  if (memcmp(read_data, READ_DATA, sizeof(read_data)) != 0) {
531  TEST_FAIL("Read data should be equal to original read data");
532  return;
533  }
534 
535  /* Get with data length greater than UID's length */
536  read_len = write_len + 1;
537  offset = 0;
538 
539  status = psa_ps_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
540  &read_data_len);
541  if (status != PSA_SUCCESS) {
542  TEST_FAIL("Get should succeed with data length larger than UID's "
543  "length");
544  return;
545  }
546 
547  if (read_data_len != write_len) {
548  TEST_FAIL("Read data length should be equal to UID's length");
549  return;
550  }
551 
552  /* Check that the read data is unchanged */
553  if (memcmp(read_data, RESULT_DATA, sizeof(read_data)) != 0) {
554  TEST_FAIL("Read data should be equal to original read data");
555  return;
556  }
557 
558  /* Get with offset + data length greater than UID's length, but individually
559  * valid
560  */
561  read_len = write_len;
562  offset = 1;
563 
564  /* Reset read_data to original READ_DATA */
565  memcpy(read_data, READ_DATA, sizeof(read_data));
566 
567  status = psa_ps_get(uid, offset, read_len, read_data + HALF_PADDING_SIZE,
568  &read_data_len);
569  if (status != PSA_SUCCESS) {
570  TEST_FAIL("Get should succeed with offset + data length too large, "
571  "but individually valid");
572  return;
573  }
574 
575  if (read_data_len != write_len - offset) {
576  TEST_FAIL("Read data length should be equal to the UID's remaining "
577  "size starting from offset");
578  return;
579  }
580 
581  /* Check that the read data is unchanged */
582  if (memcmp(read_data, OFFSET_RESULT_DATA, sizeof(read_data)) != 0) {
583  TEST_FAIL("Read data should be equal to original read data");
584  return;
585  }
586 
587  /* Call remove to clean up storage for the next test */
588  status = psa_ps_remove(uid);
589  if (status != PSA_SUCCESS) {
590  TEST_FAIL("Remove should not fail with valid UID");
591  return;
592  }
593 
594  ret->val = TEST_PASSED;
595 }
596 
606 TFM_PS_NS_TEST(1009, "Thread_A")
607 {
608  psa_status_t status;
609  const psa_storage_uid_t uid = TEST_UID_3;
611  const uint32_t data_len = WRITE_DATA_SIZE;
612  const uint32_t offset = 0;
613  const uint8_t write_data[] = WRITE_DATA;
614  size_t read_data_length = 0;
615 
616  status = psa_ps_set(uid, data_len, write_data, flags);
617  if (status != PSA_SUCCESS) {
618  TEST_FAIL("Set should not fail");
619  return;
620  }
621 
622  /* Get with NULL data pointer */
623  status = psa_ps_get(uid, offset, 0, NULL, &read_data_length);
624  if (status != PSA_SUCCESS) {
625  TEST_FAIL("Get should succeed with NULL data pointer and zero length");
626  return;
627  }
628 
629  /* Call remove to clean up storage for the next test */
630  status = psa_ps_remove(uid);
631  if (status != PSA_SUCCESS) {
632  TEST_FAIL("Remove should not fail with valid UID");
633  return;
634  }
635 
636  ret->val = TEST_PASSED;
637 }
638 
643 TFM_PS_NS_TEST(1010, "Thread_A")
644 {
645  psa_status_t status;
646  const psa_storage_uid_t uid = WRITE_ONCE_UID;
647  struct psa_storage_info_t info = {0};
648 
649  /* Get info for write once UID */
650  status = psa_ps_get_info(uid, &info);
651  if (status != PSA_SUCCESS) {
652  TEST_FAIL("Get info should not fail for write once UID");
653  return;
654  }
655 
656  /* Check that the info struct contains the correct values */
657  if (info.size != WRITE_ONCE_DATA_SIZE) {
658  TEST_FAIL("Size incorrect for write once UID");
659  return;
660  }
661 
662  if (info.flags != PSA_STORAGE_FLAG_WRITE_ONCE) {
663  TEST_FAIL("Flags incorrect for write once UID");
664  return;
665  }
666 
667  ret->val = TEST_PASSED;
668 }
669 
674 TFM_PS_NS_TEST(1011, "Thread_A")
675 {
676  psa_status_t status;
677  const psa_storage_uid_t uid = TEST_UID_1;
678  struct psa_storage_info_t info = {0};
680  const uint32_t data_len = WRITE_DATA_SIZE;
681  const uint8_t write_data[] = WRITE_DATA;
682 
683  status = psa_ps_set(uid, data_len, write_data, flags);
684  if (status != PSA_SUCCESS) {
685  TEST_FAIL("Set should not fail");
686  return;
687  }
688 
689  /* Get info for valid UID */
690  status = psa_ps_get_info(uid, &info);
691  if (status != PSA_SUCCESS) {
692  TEST_FAIL("Get info should not fail with valid UID");
693  return;
694  }
695 
696  /* Check that the info struct contains the correct values */
697  if (info.size != data_len) {
698  TEST_FAIL("Size incorrect for valid UID");
699  return;
700  }
701 
702  if (info.flags != flags) {
703  TEST_FAIL("Flags incorrect for valid UID");
704  return;
705  }
706 
707  /* Call remove to clean up storage for the next test */
708  status = psa_ps_remove(uid);
709  if (status != PSA_SUCCESS) {
710  TEST_FAIL("Remove should not fail with valid UID");
711  return;
712  }
713 
714  ret->val = TEST_PASSED;
715 }
716 
722 TFM_PS_NS_TEST(1012, "Thread_A")
723 {
724  psa_status_t status;
725  const psa_storage_uid_t uid = TEST_UID_2;
726  struct psa_storage_info_t info = {0};
727 
728  /* Get info with UID that has not yet been set */
729  status = psa_ps_get_info(uid, &info);
730  if (status != PSA_ERROR_DOES_NOT_EXIST) {
731  TEST_FAIL("Get info should not succeed with unset UID");
732  return;
733  }
734 
735  /* Check that the info struct has not been modified */
736  if (info.size != 0) {
737  TEST_FAIL("Size should not have changed");
738  return;
739  }
740 
741  if (info.flags != 0) {
742  TEST_FAIL("Flags should not have changed");
743  return;
744  }
745 
746  /* Get info with invalid UID */
747  status = psa_ps_get_info(INVALID_UID, &info);
748  if (status != PSA_ERROR_INVALID_ARGUMENT) {
749  TEST_FAIL("Get info should not succeed with invalid UID");
750  return;
751  }
752 
753  /* Check that the info struct has not been modified */
754  if (info.size != 0) {
755  TEST_FAIL("Size should not have changed");
756  return;
757  }
758 
759  if (info.flags != 0) {
760  TEST_FAIL("Flags should not have changed");
761  return;
762  }
763 
764  ret->val = TEST_PASSED;
765 }
766 
771 TFM_PS_NS_TEST(1013, "Thread_A")
772 {
773  psa_status_t status;
774  const psa_storage_uid_t uid = TEST_UID_1;
775  struct psa_storage_info_t info = {0};
777  const uint32_t data_len = WRITE_DATA_SIZE;
778  const uint32_t offset = 0;
779  const uint8_t write_data[] = WRITE_DATA;
780  uint8_t read_data[] = READ_DATA;
781  size_t read_data_len = 0;
782 
783  status = psa_ps_set(uid, data_len, write_data, flags);
784  if (status != PSA_SUCCESS) {
785  TEST_FAIL("Set should not fail");
786  return;
787  }
788 
789  /* Call remove with valid ID */
790  status = psa_ps_remove(uid);
791  if (status != PSA_SUCCESS) {
792  TEST_FAIL("Remove should not fail with valid UID");
793  return;
794  }
795 
796  /* Check that get info fails for removed UID */
797  status = psa_ps_get_info(uid, &info);
798  if (status != PSA_ERROR_DOES_NOT_EXIST) {
799  TEST_FAIL("Get info should not succeed with removed UID");
800  return;
801  }
802 
803  /* Check that get fails for removed UID */
804  status = psa_ps_get(uid, offset, data_len, read_data, &read_data_len);
805  if (status != PSA_ERROR_DOES_NOT_EXIST) {
806  TEST_FAIL("Get should not succeed with removed UID");
807  return;
808  }
809 
810  /* Check that remove fails for removed UID */
811  status = psa_ps_remove(uid);
812  if (status != PSA_ERROR_DOES_NOT_EXIST) {
813  TEST_FAIL("Remove should not succeed with removed UID");
814  return;
815  }
816 
817  ret->val = TEST_PASSED;
818 }
819 
824 TFM_PS_NS_TEST(1014, "Thread_A")
825 {
826  psa_status_t status;
827  const psa_storage_uid_t uid = WRITE_ONCE_UID;
828 
829  /* Call remove with write once UID */
830  status = psa_ps_remove(uid);
831  if (status != PSA_ERROR_NOT_PERMITTED) {
832  TEST_FAIL("Remove should not succeed with write once UID");
833  return;
834  }
835 
836  ret->val = TEST_PASSED;
837 }
838 
843 TFM_PS_NS_TEST(1015, "Thread_A")
844 {
845  psa_status_t status;
846  const psa_storage_uid_t uid = INVALID_UID;
847 
848  /* Call remove with an invalid UID */
849  status = psa_ps_remove(uid);
850  if (status != PSA_ERROR_INVALID_ARGUMENT) {
851  TEST_FAIL("Remove should not succeed with invalid UID");
852  return;
853  }
854 
855  ret->val = TEST_PASSED;
856 }
857 
858 #ifdef TFM_NS_CLIENT_IDENTIFICATION
859 
862 static void tfm_ps_test_1016_task_1(struct test_result_t *ret)
863 {
864  psa_status_t status;
865  const psa_storage_uid_t uid = TEST_UID_3;
867  const uint32_t data_len = WRITE_DATA_SIZE;
868  const uint8_t write_data[] = WRITE_DATA;
869 
870  status = psa_ps_set(uid, data_len, write_data, flags);
871  if (status != PSA_SUCCESS) {
872  TEST_FAIL("Set should not fail with valid thread name");
873  return;
874  }
875 
876  ret->val = TEST_PASSED;
877 }
878 
882 static void tfm_ps_test_1016_task_2(struct test_result_t *ret)
883 {
884  psa_status_t status;
885  const psa_storage_uid_t uid = TEST_UID_3;
886  const uint32_t data_len = WRITE_DATA_SIZE;
887  const uint32_t offset = 0;
888  uint8_t read_data[] = READ_DATA;
889  size_t read_data_len = 0;
890 
891  status = psa_ps_get(uid, offset, data_len, read_data, &read_data_len);
892  if (status != PSA_ERROR_DOES_NOT_EXIST) {
893  TEST_FAIL("Get should not succeed with invalid thread name");
894  return;
895  }
896 
897  /* Check that read data has not been modified */
898  if (memcmp(read_data, READ_DATA, sizeof(read_data)) != 0) {
899  TEST_FAIL("Read data should not have changed");
900  return;
901  }
902 
903  ret->val = TEST_PASSED;
904 }
905 
909 static void tfm_ps_test_1016_task_3(struct test_result_t *ret)
910 {
911  psa_status_t status;
912  const psa_storage_uid_t uid = TEST_UID_3;
913 
914  status = psa_ps_remove(uid);
915  if (status != PSA_SUCCESS) {
916  TEST_FAIL("Remove should not fail with valid thread name");
917  return;
918  }
919 
920  ret->val = TEST_PASSED;
921 }
922 
926 static void tfm_ps_test_1016(struct test_result_t *ret)
927 {
928  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1016_task_1);
929  if (ret->val != TEST_PASSED) {
930  return;
931  }
932 
933  tfm_ps_run_test(INVALID_THREAD_NAME, ret, tfm_ps_test_1016_task_2);
934  if (ret->val != TEST_PASSED) {
935  return;
936  }
937 
938  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1016_task_3);
939 }
940 
944 static void tfm_ps_test_1017_task_1(struct test_result_t *ret)
945 {
946  psa_status_t status;
947  const psa_storage_uid_t uid = TEST_UID_1;
949  const uint32_t data_len = WRITE_DATA_SIZE;
950  const uint8_t write_data[] = WRITE_DATA;
951 
952  status = psa_ps_set(uid, data_len, write_data, flags);
953  if (status != PSA_SUCCESS) {
954  TEST_FAIL("Set should not fail with valid thread name");
955  return;
956  }
957 
958  ret->val = TEST_PASSED;
959 }
960 
964 static void tfm_ps_test_1017_task_2(struct test_result_t *ret)
965 {
966  psa_status_t status;
967  const psa_storage_uid_t uid = TEST_UID_1;
968  struct psa_storage_info_t info = {0};
969 
970  status = psa_ps_get_info(uid, &info);
971  if (status != PSA_ERROR_DOES_NOT_EXIST) {
972  TEST_FAIL("Get info should not succeed with invalid thread name");
973  return;
974  }
975 
976  /* Check that info has not been modified */
977  if (info.size != 0 || info.flags != 0) {
978  TEST_FAIL("Info should not have changed");
979  return;
980  }
981 
982  ret->val = TEST_PASSED;
983 }
984 
988 static void tfm_ps_test_1017_task_3(struct test_result_t *ret)
989 {
990  psa_status_t status;
991  const psa_storage_uid_t uid = TEST_UID_1;
992 
993  status = psa_ps_remove(uid);
994  if (status != PSA_SUCCESS) {
995  TEST_FAIL("Remove should not fail with valid thread name");
996  return;
997  }
998 
999  ret->val = TEST_PASSED;
1000 }
1001 
1005 static void tfm_ps_test_1017(struct test_result_t *ret)
1006 {
1007  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1017_task_1);
1008  if (ret->val != TEST_PASSED) {
1009  return;
1010  }
1011 
1012  tfm_ps_run_test(INVALID_THREAD_NAME, ret, tfm_ps_test_1017_task_2);
1013  if (ret->val != TEST_PASSED) {
1014  return;
1015  }
1016 
1017  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1017_task_3);
1018 }
1019 
1023 static void tfm_ps_test_1018_task_1(struct test_result_t *ret)
1024 {
1025  psa_status_t status;
1026  const psa_storage_uid_t uid = TEST_UID_2;
1028  const uint32_t data_len = WRITE_DATA_SIZE;
1029  const uint8_t write_data[] = WRITE_DATA;
1030 
1031  status = psa_ps_set(uid, data_len, write_data, flags);
1032  if (status != PSA_SUCCESS) {
1033  TEST_FAIL("Set should not fail with valid thread name");
1034  return;
1035  }
1036 
1037  ret->val = TEST_PASSED;
1038 }
1039 
1043 static void tfm_ps_test_1018_task_2(struct test_result_t *ret)
1044 {
1045  psa_status_t status;
1046  const psa_storage_uid_t uid = TEST_UID_2;
1047 
1048  status = psa_ps_remove(uid);
1049  if (status != PSA_ERROR_DOES_NOT_EXIST) {
1050  TEST_FAIL("Remove should not succeed with invalid thread name");
1051  return;
1052  }
1053 
1054  ret->val = TEST_PASSED;
1055 }
1056 
1060 static void tfm_ps_test_1018_task_3(struct test_result_t *ret)
1061 {
1062  psa_status_t status;
1063  const psa_storage_uid_t uid = TEST_UID_2;
1064 
1065  status = psa_ps_remove(uid);
1066  if (status != PSA_SUCCESS) {
1067  TEST_FAIL("Remove should not fail with valid thread name");
1068  return;
1069  }
1070 
1071  ret->val = TEST_PASSED;
1072 }
1073 
1077 static void tfm_ps_test_1018(struct test_result_t *ret)
1078 {
1079  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1018_task_1);
1080  if (ret->val != TEST_PASSED) {
1081  return;
1082  }
1083 
1084  tfm_ps_run_test(INVALID_THREAD_NAME, ret, tfm_ps_test_1018_task_2);
1085  if (ret->val != TEST_PASSED) {
1086  return;
1087  }
1088 
1089  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1018_task_3);
1090 }
1091 
1095 static void tfm_ps_test_1019_task_1(struct test_result_t *ret)
1096 {
1097  psa_status_t status;
1098  const psa_storage_uid_t uid = TEST_UID_3;
1100  const uint8_t write_data[] = "Thread A data";
1101 
1102  status = psa_ps_set(uid, sizeof(write_data), write_data, flags);
1103  if (status != PSA_SUCCESS) {
1104  TEST_FAIL("Set should not fail with valid UID");
1105  return;
1106  }
1107 
1108  ret->val = TEST_PASSED;
1109 }
1110 
1114 static void tfm_ps_test_1019_task_2(struct test_result_t *ret)
1115 {
1116  psa_status_t status;
1117  const psa_storage_uid_t uid = TEST_UID_3;
1118  struct psa_storage_info_t info = {0};
1120  const uint32_t data_len = WRITE_DATA_SIZE;
1121  const uint32_t offset = 0;
1122  const uint8_t write_data[] = WRITE_DATA;
1123  uint8_t read_data[] = READ_DATA;
1124  size_t read_data_len = 0;
1125 
1126  /* Attempt to access the other thread's UID */
1127  status = psa_ps_get(uid, offset, data_len, read_data, &read_data_len);
1128  if (status != PSA_ERROR_DOES_NOT_EXIST) {
1129  TEST_FAIL("Get should not find another thread's UID");
1130  return;
1131  }
1132 
1133  /* Check that read data has not been modified */
1134  if (memcmp(read_data, READ_DATA, sizeof(read_data)) != 0) {
1135  TEST_FAIL("Read data should not have changed");
1136  return;
1137  }
1138 
1139  status = psa_ps_get_info(uid, &info);
1140  if (status != PSA_ERROR_DOES_NOT_EXIST) {
1141  TEST_FAIL("Get info should not find another thread's UID");
1142  return;
1143  }
1144 
1145  /* Check that info has not been modified */
1146  if (info.size != 0 || info.flags != 0) {
1147  TEST_FAIL("Info should not have changed");
1148  return;
1149  }
1150 
1151  status = psa_ps_remove(uid);
1152  if (status != PSA_ERROR_DOES_NOT_EXIST) {
1153  TEST_FAIL("Remove should not find another thread's UID");
1154  return;
1155  }
1156 
1157  /* Create the same UID, but belonging to this thread */
1158  status = psa_ps_set(uid, data_len, write_data, flags);
1159  if (status != PSA_SUCCESS) {
1160  TEST_FAIL("Set should not fail with valid UID");
1161  return;
1162  }
1163 
1164  status = psa_ps_get(uid, offset, data_len, read_data + HALF_PADDING_SIZE,
1165  &read_data_len);
1166  if (status != PSA_SUCCESS) {
1167  TEST_FAIL("Get should not fail with valid UID");
1168  return;
1169  }
1170 
1171  /* Check that the data read belongs to this thread, not the other one */
1172  if (memcmp(read_data, RESULT_DATA, sizeof(read_data)) != 0) {
1173  TEST_FAIL("Read data should be equal to result data");
1174  return;
1175  }
1176 
1177  /* Call remove to clean up storage for the next test */
1178  status = psa_ps_remove(uid);
1179  if (status != PSA_SUCCESS) {
1180  TEST_FAIL("Remove should not fail with valid UID");
1181  return;
1182  }
1183 
1184  ret->val = TEST_PASSED;
1185 }
1186 
1190 static void tfm_ps_test_1019_task_3(struct test_result_t *ret)
1191 {
1192  psa_status_t status;
1193  const psa_storage_uid_t uid = TEST_UID_3;
1194  struct psa_storage_info_t info = {0};
1196  const uint32_t offset = 0;
1197  const uint8_t write_data[] = "Thread A data";
1198  uint8_t read_data[] = READ_DATA;
1199  size_t read_data_len = 0;
1200 
1201  const uint32_t data_len = sizeof(write_data);
1202 
1203  /* Check that first thread can still get info for UID */
1204  status = psa_ps_get_info(uid, &info);
1205  if (status != PSA_SUCCESS) {
1206  TEST_FAIL("Get info should not fail with valid UID");
1207  return;
1208  }
1209 
1210  /* Check that first thread's UID info has not been modified */
1211  if (info.size != data_len || info.flags != flags) {
1212  TEST_FAIL("Info should be equal to original info");
1213  return;
1214  }
1215 
1216  /* Check that first thread can still get UID */
1217  status = psa_ps_get(uid, offset, data_len, read_data, &read_data_len);
1218  if (status != PSA_SUCCESS) {
1219  TEST_FAIL("Get should not fail with valid UID");
1220  return;
1221  }
1222 
1223  /* Check that first thread's UID data has not been modified */
1224  if (memcmp(read_data, write_data, data_len) != 0) {
1225  TEST_FAIL("Read data should be equal to original write data");
1226  return;
1227  }
1228 
1229  /* Call remove to clean up storage for the next test */
1230  status = psa_ps_remove(uid);
1231  if (status != PSA_SUCCESS) {
1232  TEST_FAIL("Remove should not fail with valid UID");
1233  return;
1234  }
1235 
1236  ret->val = TEST_PASSED;
1237 }
1238 
1242 static void tfm_ps_test_1019(struct test_result_t *ret)
1243 {
1244  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1019_task_1);
1245  if (ret->val != TEST_PASSED) {
1246  return;
1247  }
1248 
1249  tfm_ps_run_test("Thread_B", ret, tfm_ps_test_1019_task_2);
1250  if (ret->val != TEST_PASSED) {
1251  return;
1252  }
1253 
1254  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1019_task_3);
1255 }
1256 
1260 static void tfm_ps_test_1020_task_1(struct test_result_t *ret)
1261 {
1262  psa_status_t status;
1263  const psa_storage_uid_t uid = TEST_UID_1;
1265  const uint8_t write_data[] = "A";
1266 
1267  status = psa_ps_set(uid, sizeof(write_data), write_data, flags);
1268  if (status != PSA_SUCCESS) {
1269  TEST_FAIL("Set should succeed for Thread_A");
1270  return;
1271  }
1272 
1273  ret->val = TEST_PASSED;
1274 }
1275 
1279 static void tfm_ps_test_1020_task_2(struct test_result_t *ret)
1280 {
1281  psa_status_t status;
1282  const psa_storage_uid_t uid = TEST_UID_1;
1284  const uint8_t write_data[] = "B";
1285 
1286  status = psa_ps_set(uid, sizeof(write_data), write_data, flags);
1287  if (status != PSA_SUCCESS) {
1288  TEST_FAIL("Set should succeed for Thread_B");
1289  return;
1290  }
1291 
1292  ret->val = TEST_PASSED;
1293 }
1294 
1298 static void tfm_ps_test_1020_task_3(struct test_result_t *ret)
1299 {
1300  psa_status_t status;
1301  const psa_storage_uid_t uid = TEST_UID_1;
1303  const uint8_t write_data[] = "HELLO";
1304 
1305  status = psa_ps_set(uid, sizeof(write_data), write_data, flags);
1306  if (status != PSA_SUCCESS) {
1307  TEST_FAIL("Second set should succeed for Thread_A");
1308  return;
1309  }
1310 
1311  ret->val = TEST_PASSED;
1312 }
1313 
1317 static void tfm_ps_test_1020_task_4(struct test_result_t *ret)
1318 {
1319  psa_status_t status;
1320  const psa_storage_uid_t uid = TEST_UID_1;
1322  const uint8_t write_data[] = "WORLD_1234";
1323 
1324  status = psa_ps_set(uid, sizeof(write_data), write_data, flags);
1325  if (status != PSA_SUCCESS) {
1326  TEST_FAIL("Second set should succeed for Thread_B");
1327  return;
1328  }
1329 
1330  ret->val = TEST_PASSED;
1331 }
1332 
1336 static void tfm_ps_test_1020_task_5(struct test_result_t *ret)
1337 {
1338  psa_status_t status;
1339  const psa_storage_uid_t uid = TEST_UID_1;
1340  const uint32_t offset = 0;
1341  const uint8_t write_data[] = "HELLO";
1342  uint8_t read_data[] = READ_DATA;
1343  size_t read_data_len = 0;
1344 
1345  status = psa_ps_get(uid, offset, sizeof(write_data), read_data,
1346  &read_data_len);
1347  if (status != PSA_SUCCESS) {
1348  TEST_FAIL("Get should succeed for Thread_A");
1349  return;
1350  }
1351 
1352  /* Check that UID contains Thread_A's data */
1353  if (memcmp(read_data, write_data, sizeof(write_data)) != 0) {
1354  TEST_FAIL("Read data incorrect for Thread_A");
1355  return;
1356  }
1357 
1358  ret->val = TEST_PASSED;
1359 }
1360 
1364 static void tfm_ps_test_1020_task_6(struct test_result_t *ret)
1365 {
1366  psa_status_t status;
1367  const psa_storage_uid_t uid = TEST_UID_1;
1368  const uint32_t offset = 0;
1369  const uint8_t write_data[] = "WORLD_1234";
1370  uint8_t read_data[] = READ_DATA;
1371  size_t read_data_len = 0;
1372 
1373  status = psa_ps_get(uid, offset, sizeof(write_data), read_data,
1374  &read_data_len);
1375  if (status != PSA_SUCCESS) {
1376  TEST_FAIL("Get should succeed for Thread_B");
1377  return;
1378  }
1379 
1380  /* Check that UID contains Thread_B's data */
1381  if (memcmp(read_data, write_data, sizeof(write_data)) != 0) {
1382  TEST_FAIL("Read data incorrect for Thread_B");
1383  return;
1384  }
1385 
1386  /* Call remove to clean up storage for the next test */
1387  status = psa_ps_remove(uid);
1388  if (status != PSA_SUCCESS) {
1389  TEST_FAIL("Remove should work form Thread_B");
1390  return;
1391  }
1392 
1393  ret->val = TEST_PASSED;
1394 }
1395 
1399 static void tfm_ps_test_1020_task_7(struct test_result_t *ret)
1400 {
1401  psa_status_t status;
1402  const psa_storage_uid_t uid = TEST_UID_1;
1403 
1404  /* Call remove to clean up storage for the next test */
1405  status = psa_ps_remove(uid);
1406  if (status != PSA_SUCCESS) {
1407  TEST_FAIL("Remove should work form Thread_B");
1408  return;
1409  }
1410 
1411  ret->val = TEST_PASSED;
1412 }
1413 
1418 static void tfm_ps_test_1020(struct test_result_t *ret)
1419 {
1420  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1020_task_1);
1421  if (ret->val != TEST_PASSED) {
1422  return;
1423  }
1424 
1425  tfm_ps_run_test("Thread_B", ret, tfm_ps_test_1020_task_2);
1426  if (ret->val != TEST_PASSED) {
1427  return;
1428  }
1429 
1430  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1020_task_3);
1431  if (ret->val != TEST_PASSED) {
1432  return;
1433  }
1434 
1435  tfm_ps_run_test("Thread_B", ret, tfm_ps_test_1020_task_4);
1436  if (ret->val != TEST_PASSED) {
1437  return;
1438  }
1439 
1440  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1020_task_5);
1441  if (ret->val != TEST_PASSED) {
1442  return;
1443  }
1444 
1445  tfm_ps_run_test("Thread_B", ret, tfm_ps_test_1020_task_6);
1446  if (ret->val != TEST_PASSED) {
1447  return;
1448  }
1449 
1450  tfm_ps_run_test("Thread_A", ret, tfm_ps_test_1020_task_7);
1451 }
1452 #endif /* TFM_NS_CLIENT_IDENTIFICATION */
1453 
1461 TFM_PS_NS_TEST(1021, "Thread_A")
1462 {
1463  psa_status_t status;
1464  const psa_storage_uid_t uid_1 = TEST_UID_2;
1465  const psa_storage_uid_t uid_2 = TEST_UID_3;
1467  const uint32_t data_len_2 = WRITE_DATA_SIZE;
1468  const uint32_t offset = 0;
1469  const uint8_t write_data_1[] = "UID 1 DATA";
1470  const uint8_t write_data_2[] = WRITE_DATA;
1471  uint8_t read_data[] = READ_DATA;
1472  size_t read_data_len = 0;
1473 
1474  /* Set UID 1 */
1475  status = psa_ps_set(uid_1, sizeof(write_data_1), write_data_1, flags);
1476  if (status != PSA_SUCCESS) {
1477  TEST_FAIL("Set should not fail for UID 1");
1478  return;
1479  }
1480 
1481  /* Set UID 2 */
1482  status = psa_ps_set(uid_2, data_len_2, write_data_2, flags);
1483  if (status != PSA_SUCCESS) {
1484  TEST_FAIL("Set should not fail for UID 2");
1485  return;
1486  }
1487 
1488  /* Remove UID 1. This should cause UID 2 to be compacted to the beginning of
1489  * the block.
1490  */
1491  status = psa_ps_remove(uid_1);
1492  if (status != PSA_SUCCESS) {
1493  TEST_FAIL("Remove should not fail for UID 1");
1494  return;
1495  }
1496 
1497  /* If the compact worked as expected, the test should be able to read back
1498  * the data from UID 2 correctly.
1499  */
1500  status = psa_ps_get(uid_2, offset, data_len_2,
1501  read_data + HALF_PADDING_SIZE, &read_data_len);
1502  if (status != PSA_SUCCESS) {
1503  TEST_FAIL("Get should not fail for UID 2");
1504  return;
1505  }
1506 
1507  if (memcmp(read_data, RESULT_DATA, sizeof(read_data)) != 0) {
1508  TEST_FAIL("Read buffer has incorrect data");
1509  return;
1510  }
1511 
1512  /* Remove UID 2 to clean up storage for the next test */
1513  status = psa_ps_remove(uid_2);
1514  if (status != PSA_SUCCESS) {
1515  TEST_FAIL("Remove should not fail for UID 2");
1516  return;
1517  }
1518 
1519  ret->val = TEST_PASSED;
1520 }
1521 
1525 TFM_PS_NS_TEST(1022, "Thread_A")
1526 {
1527  psa_status_t status;
1528  uint32_t i;
1529  const psa_storage_uid_t uid = TEST_UID_1;
1531  const uint32_t data_len = WRITE_DATA_SIZE;
1532  uint32_t offset = 0;
1533  const uint8_t write_data[] = WRITE_DATA;
1534  uint8_t read_data[] = READ_DATA;
1535  size_t read_data_len = 0;
1536 
1537  /* Set the entire data into UID */
1538  status = psa_ps_set(uid, data_len, write_data, flags);
1539  if (status != PSA_SUCCESS) {
1540  TEST_FAIL("Set should not fail");
1541  return;
1542  }
1543 
1544  /* Get the data from UID one byte at a time */
1545  for (i = 0; i < data_len; ++i) {
1546  status = psa_ps_get(uid, offset, 1,
1547  (read_data + HALF_PADDING_SIZE + i),
1548  &read_data_len);
1549  if (status != PSA_SUCCESS) {
1550  TEST_FAIL("Get should not fail for partial read");
1551  return;
1552  }
1553 
1554  ++offset;
1555  }
1556 
1557  if (memcmp(read_data, RESULT_DATA, sizeof(read_data)) != 0) {
1558  TEST_FAIL("Read buffer has incorrect data");
1559  return;
1560  }
1561 
1562  /* Remove UID to clean up storage for the next test */
1563  status = psa_ps_remove(uid);
1564  if (status != PSA_SUCCESS) {
1565  TEST_FAIL("Remove should not fail");
1566  return;
1567  }
1568 
1569  ret->val = TEST_PASSED;
1570 }
1571 
1575 TFM_PS_NS_TEST(1023, "Thread_A")
1576 {
1577  psa_status_t status;
1578  const psa_storage_uid_t uid = TEST_UID_2;
1580  const uint32_t offset = 0;
1581  const uint8_t write_data_1[] = "ONE";
1582  const uint8_t write_data_2[] = "TWO";
1583  const uint8_t write_data_3[] = "THREE";
1584  uint8_t read_data[] = READ_DATA;
1585  size_t read_data_len = 0;
1586 
1587  /* Set write data 1 into UID */
1588  status = psa_ps_set(uid, sizeof(write_data_1), write_data_1, flags);
1589  if (status != PSA_SUCCESS) {
1590  TEST_FAIL("First set should not fail");
1591  return;
1592  }
1593 
1594  /* Set write data 2 into UID */
1595  status = psa_ps_set(uid, sizeof(write_data_2), write_data_2, flags);
1596  if (status != PSA_SUCCESS) {
1597  TEST_FAIL("Second set should not fail");
1598  return;
1599  }
1600 
1601  /* Set write data 3 into UID */
1602  status = psa_ps_set(uid, sizeof(write_data_3), write_data_3, flags);
1603  if (status != PSA_SUCCESS) {
1604  TEST_FAIL("Third set should not fail");
1605  return;
1606  }
1607 
1608  status = psa_ps_get(uid, offset, sizeof(write_data_3), read_data,
1609  &read_data_len);
1610  if (status != PSA_SUCCESS) {
1611  TEST_FAIL("Get should not fail");
1612  return;
1613  }
1614 
1615  /* Check that get returns the last data to be set */
1616  if (memcmp(read_data, write_data_3, sizeof(write_data_3)) != 0) {
1617  TEST_FAIL("Read buffer has incorrect data");
1618  return;
1619  }
1620 
1621  /* Remove UID to clean up storage for the next test */
1622  status = psa_ps_remove(uid);
1623  if (status != PSA_SUCCESS) {
1624  TEST_FAIL("Remove should not fail");
1625  return;
1626  }
1627 
1628  ret->val = TEST_PASSED;
1629 }
1630 
1634 TFM_PS_NS_TEST(1024, "Thread_A")
1635 {
1636  uint32_t support_flags;
1637 
1638  support_flags = psa_ps_get_support();
1639  if (support_flags != 0) {
1640  TEST_FAIL("Support flags should be 0");
1641  return;
1642  }
1643 
1644  ret->val = TEST_PASSED;
1645 }
1646 
1653 TFM_PS_NS_TEST(1025, "Thread_A")
1654 {
1655  uint8_t cycle;
1656  psa_status_t status;
1657  const psa_storage_uid_t test_uid[TEST_1025_CYCLES] = {
1658  TEST_UID_1,
1659  TEST_UID_2,
1660  TEST_UID_3};
1661  const uint32_t test_asset_sizes[TEST_1025_CYCLES] = {
1662  PS_MAX_ASSET_SIZE >> 2,
1663  PS_MAX_ASSET_SIZE >> 1,
1664  PS_MAX_ASSET_SIZE};
1665 
1666  /* Loop to test different asset sizes and UID's*/
1667  for (cycle = 0; cycle < TEST_1025_CYCLES; cycle++) {
1668  uint32_t data_size = test_asset_sizes[cycle];
1669  psa_storage_uid_t uid = test_uid[cycle];
1670  struct psa_storage_info_t info = {0};
1671 
1672  memset(read_asset_data, 0x00, sizeof(read_asset_data));
1673 
1674  /* Set with data and no flags and a valid UID */
1675  status = psa_ps_set(uid,
1676  data_size,
1677  write_asset_data,
1679  if (status != PSA_SUCCESS) {
1680  TEST_FAIL("Set should not fail with valid UID");
1681  return;
1682  }
1683 
1684  /* Get info for valid UID */
1685  status = psa_ps_get_info(uid, &info);
1686  if (status != PSA_SUCCESS) {
1687  TEST_FAIL("Get info should not fail with valid UID");
1688  return;
1689  }
1690 
1691  /* Check that the info struct contains the correct values */
1692  if (info.size != data_size) {
1693  TEST_FAIL("Size incorrect for valid UID");
1694  return;
1695  }
1696 
1697  if (info.flags != PSA_STORAGE_FLAG_NONE) {
1698  TEST_FAIL("Flags incorrect for valid UID");
1699  return;
1700  }
1701 
1702  /* Check that thread can still get UID */
1703  status = psa_ps_get(uid, 0, data_size, read_asset_data,
1704  &read_asset_data_len);
1705  if (status != PSA_SUCCESS) {
1706  TEST_FAIL("Get should not fail with valid UID");
1707  return;
1708  }
1709 
1710  /* Check that thread's UID data has not been modified */
1711  if (memcmp(read_asset_data, write_asset_data, data_size) != 0) {
1712  TEST_FAIL("Read data should be equal to original write data");
1713  return;
1714  }
1715 
1716  /* Call remove to clean up storage for the next test */
1717  status = psa_ps_remove(uid);
1718  if (status != PSA_SUCCESS) {
1719  TEST_FAIL("Remove should not fail with valid UID");
1720  return;
1721  }
1722  }
1723 
1724  ret->val = TEST_PASSED;
1725 }
#define OFFSET_RESULT_DATA
#define INVALID_THREAD_NAME
#define TEST_1025_CYCLES
psa_status_t psa_ps_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.
#define PSA_SUCCESS
Definition: crypto_values.h:35
#define WRITE_DATA_SIZE
#define INVALID_UID
#define TEST_FAIL(info_msg)
#define TEST_UID_2
enum test_suite_err_t set_testsuite(const char *name, struct test_t *test_list, uint32_t size, struct test_suite_t *p_ts)
Sets test suite parameters.
psa_status_t psa_ps_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 WRITE_ONCE_DATA
#define PSA_STORAGE_FLAG_WRITE_ONCE
#define RESULT_DATA
#define TEST_UID_1
#define PSA_ERROR_INVALID_ARGUMENT
psa_status_t psa_ps_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info)
Retrieve the metadata about the provided uid.
#define TEST_UID_3
void tfm_ps_run_test(const char *thread_name, struct test_result_t *ret, test_func_t *test_func)
Executes the given test function from the specified thread context.
#define PSA_ERROR_NOT_PERMITTED
Definition: crypto_values.h:65
void register_testsuite_ns_psa_ps_interface(struct test_suite_t *p_test_suite)
Register testsuite for the PSA protected storage NS interface tests.
struct test_result_t * ret
struct test_result_t ret
psa_status_t psa_ps_remove(psa_storage_uid_t uid)
Remove the provided uid and its associated data from the storage.
#define TFM_PS_NS_TEST(test_num, thread_name)
Defines a single-threaded PS NS test function and declares the corresponding task function...
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
#define WRITE_ONCE_UID
void * memcpy(void *dest, const void *src, size_t n)
Definition: crt_memcpy.c:10
uint32_t psa_ps_get_support(void)
Lists optional features.
#define TEST_LOG(...)
uint64_t psa_storage_uid_t
#define WRITE_ONCE_READ_DATA
enum test_status_t val
#define WRITE_DATA
psa_storage_create_flags_t flags
#define PSA_STORAGE_FLAG_NONE
#define READ_DATA
#define PSA_ERROR_DOES_NOT_EXIST
Definition: crypto_values.h:89
uint32_t psa_storage_create_flags_t
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:43
void * memset(void *s, int c, size_t n)
Definition: crt_memset.c:10
#define INVALID_FLAG
#define WRITE_ONCE_DATA_SIZE
#define WRITE_ONCE_RESULT_DATA