tfm_ps.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-01-10 Kevin/Karl Add PS demo
  9. *
  10. */
  11. #include <rtdevice.h>
  12. #include <string.h>
  13. #include "tfm_ns_lock.h"
  14. #include "psa_protected_storage.h"
  15. #define TEST_UID_A 2U
  16. #define ASSET_A "THEQUICKBROWNFOXJUMPSOVERALAZYDOG"
  17. #define ASSET_A_SIZE (sizeof( ASSET_A ) - 1)
  18. #define RESETDATA "THISIS"
  19. #define RESETDATA_SIZE (sizeof( RESETDATA ) - 1)
  20. #define READ_LENGTH (ASSET_A_SIZE > RESETDATA_SIZE ? \
  21. ASSET_A_SIZE : RESETDATA_SIZE)
  22. void protected_storage_demo_thread(void * parameters)
  23. {
  24. psa_ps_status_t status;
  25. const psa_ps_uid_t uid = TEST_UID_A;
  26. const psa_ps_create_flags_t flags = PSA_PS_FLAG_NONE;
  27. uint8_t write_data[] = ASSET_A;
  28. const uint32_t data_length = ASSET_A_SIZE;
  29. uint8_t rewrite_data[] = RESETDATA;
  30. const uint32_t reset_data_length = RESETDATA_SIZE;
  31. uint8_t get_data[READ_LENGTH];
  32. uint32_t counter = 0;
  33. tfm_ns_lock_init();
  34. for ( ; ; )
  35. {
  36. /* Call TF-M protected storage service and set the asset. */
  37. status = psa_ps_set(uid, data_length, write_data, flags);
  38. if (status != PSA_PS_SUCCESS)
  39. {
  40. rt_kprintf("[Protected Storage Asset A Set Round %ld] Fail\r\n", counter);
  41. for( ; ; );
  42. }
  43. rt_kprintf("[Protected Storage Asset A Set Round %ld] Success\r\n", counter);
  44. /* Read the asset. */
  45. status = psa_ps_get(uid, 0, data_length, get_data);
  46. if (status != PSA_PS_SUCCESS)
  47. {
  48. rt_kprintf("[Protected Storage Asset A Get Round %ld] Fail\r\n", counter);
  49. for ( ; ; );
  50. }
  51. rt_kprintf("[Protected Storage Asset A Get Round %ld] Success\r\n", counter);
  52. /* Check the read data. */
  53. if (memcmp(write_data, get_data, sizeof(write_data) - 1) != 0)
  54. {
  55. rt_kprintf("[Protected Storage Asset A Get Round %ld] Get the wrong data\r\n", counter);
  56. for ( ; ; );
  57. }
  58. /* Change the asset. */
  59. status = psa_ps_set(uid, reset_data_length, rewrite_data, flags);
  60. if (status != PSA_PS_SUCCESS)
  61. {
  62. rt_kprintf("[Protected Storage Asset A Reset Round %ld] Fail\r\n", counter);
  63. }
  64. rt_kprintf("[Protected Storage Asset A Reset Round %ld] Success\r\n", counter);
  65. /* Read the asset. */
  66. status = psa_ps_get(uid, 0, reset_data_length, get_data);
  67. if (status != PSA_PS_SUCCESS)
  68. {
  69. rt_kprintf("[Protected Storage Asset A Get Round %ld] Fail\r\n", counter);
  70. for ( ; ; );
  71. }
  72. rt_kprintf("[Protected Storage Asset A Get Round %ld] Success\r\n", counter);
  73. /* Check the read data. */
  74. if (memcmp(rewrite_data, get_data, sizeof(rewrite_data) - 1) != 0)
  75. {
  76. rt_kprintf("[Protected Storage Asset A Get Round %ld] Get the wrong data\r\n", counter);
  77. for ( ; ; );
  78. }
  79. /* Remove the asset. */
  80. status = psa_ps_remove(uid);
  81. if (status != PSA_PS_SUCCESS)
  82. {
  83. rt_kprintf("[Protected Storage Asset A Remove Round %ld] Fail\r\n", counter);
  84. for ( ; ; );
  85. }
  86. rt_kprintf("[Protected Storage Asset A Remove Round %ld] Success\r\n\n", counter);
  87. /* Wait for a second. */
  88. rt_thread_mdelay(1000);
  89. counter++;
  90. }
  91. }
  92. // end file