test_suite_psa_crypto_attributes.function 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* BEGIN_HEADER */
  2. #include "psa/crypto.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_PSA_CRYPTO_CLIENT
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void attributes_set_get(int owner_id_arg, int id_arg, int lifetime_arg,
  10. int usage_flags_arg, int alg_arg,
  11. int type_arg, int bits_arg)
  12. {
  13. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  14. mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make(owner_id_arg, id_arg);
  15. psa_key_lifetime_t lifetime = lifetime_arg;
  16. psa_key_usage_t usage_flags = usage_flags_arg;
  17. psa_algorithm_t alg = alg_arg;
  18. psa_key_type_t type = type_arg;
  19. size_t bits = bits_arg;
  20. TEST_EQUAL(
  21. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
  22. TEST_EQUAL(
  23. MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
  24. TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
  25. TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
  26. TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
  27. TEST_EQUAL(psa_get_key_type(&attributes), 0);
  28. TEST_EQUAL(psa_get_key_bits(&attributes), 0);
  29. psa_set_key_id(&attributes, id);
  30. psa_set_key_lifetime(&attributes, lifetime);
  31. psa_set_key_usage_flags(&attributes, usage_flags);
  32. psa_set_key_algorithm(&attributes, alg);
  33. psa_set_key_type(&attributes, type);
  34. psa_set_key_bits(&attributes, bits);
  35. TEST_ASSERT(mbedtls_svc_key_id_equal(
  36. psa_get_key_id(&attributes), id));
  37. TEST_EQUAL(psa_get_key_lifetime(&attributes), lifetime);
  38. TEST_EQUAL(psa_get_key_usage_flags(&attributes), usage_flags);
  39. TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
  40. TEST_EQUAL(psa_get_key_type(&attributes), type);
  41. TEST_EQUAL(psa_get_key_bits(&attributes), bits);
  42. psa_reset_key_attributes(&attributes);
  43. TEST_EQUAL(
  44. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
  45. TEST_EQUAL(
  46. MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
  47. TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
  48. TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
  49. TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
  50. TEST_EQUAL(psa_get_key_type(&attributes), 0);
  51. TEST_EQUAL(psa_get_key_bits(&attributes), 0);
  52. }
  53. /* END_CASE */
  54. /* BEGIN_CASE */
  55. void persistence_attributes(int id1_arg, int owner_id1_arg, int lifetime_arg,
  56. int id2_arg, int owner_id2_arg,
  57. int expected_id_arg, int expected_owner_id_arg,
  58. int expected_lifetime_arg)
  59. {
  60. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  61. mbedtls_svc_key_id_t id1 =
  62. mbedtls_svc_key_id_make(owner_id1_arg, id1_arg);
  63. psa_key_lifetime_t lifetime = lifetime_arg;
  64. mbedtls_svc_key_id_t id2 =
  65. mbedtls_svc_key_id_make(owner_id2_arg, id2_arg);
  66. mbedtls_svc_key_id_t expected_id =
  67. mbedtls_svc_key_id_make(expected_owner_id_arg, expected_id_arg);
  68. psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
  69. if (id1_arg != -1) {
  70. psa_set_key_id(&attributes, id1);
  71. }
  72. if (lifetime_arg != -1) {
  73. psa_set_key_lifetime(&attributes, lifetime);
  74. }
  75. if (id2_arg != -1) {
  76. psa_set_key_id(&attributes, id2);
  77. }
  78. TEST_ASSERT(mbedtls_svc_key_id_equal(
  79. psa_get_key_id(&attributes), expected_id));
  80. TEST_EQUAL(psa_get_key_lifetime(&attributes), expected_lifetime);
  81. }
  82. /* END_CASE */
  83. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
  84. void slot_number_attribute()
  85. {
  86. psa_key_slot_number_t slot_number = 0xdeadbeef;
  87. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  88. /* Initially, there is no slot number. */
  89. TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
  90. PSA_ERROR_INVALID_ARGUMENT);
  91. /* Test setting a slot number. */
  92. psa_set_key_slot_number(&attributes, 0);
  93. PSA_ASSERT(psa_get_key_slot_number(&attributes, &slot_number));
  94. TEST_EQUAL(slot_number, 0);
  95. /* Test changing the slot number. */
  96. psa_set_key_slot_number(&attributes, 42);
  97. PSA_ASSERT(psa_get_key_slot_number(&attributes, &slot_number));
  98. TEST_EQUAL(slot_number, 42);
  99. /* Test clearing the slot number. */
  100. psa_clear_key_slot_number(&attributes);
  101. TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
  102. PSA_ERROR_INVALID_ARGUMENT);
  103. /* Clearing again should have no effect. */
  104. psa_clear_key_slot_number(&attributes);
  105. TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
  106. PSA_ERROR_INVALID_ARGUMENT);
  107. /* Test that reset clears the slot number. */
  108. psa_set_key_slot_number(&attributes, 42);
  109. PSA_ASSERT(psa_get_key_slot_number(&attributes, &slot_number));
  110. TEST_EQUAL(slot_number, 42);
  111. psa_reset_key_attributes(&attributes);
  112. TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
  113. PSA_ERROR_INVALID_ARGUMENT);
  114. }
  115. /* END_CASE */