test_suite_psa_crypto_entropy.function 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* BEGIN_HEADER */
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <psa/crypto.h>
  5. #include "mbedtls/entropy.h"
  6. #include "entropy_poll.h"
  7. /* Calculating the minimum allowed entropy size in bytes */
  8. #define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, \
  9. MBEDTLS_ENTROPY_BLOCK_SIZE)
  10. #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
  11. #if defined(MBEDTLS_PSA_ITS_FILE_C)
  12. #include <stdio.h>
  13. #else
  14. #include <psa/internal_trusted_storage.h>
  15. #endif
  16. /* Remove the entropy seed file. Since the library does not expose a way
  17. * to do this (it would be a security risk if such a function was ever
  18. * accessible in production), implement this functionality in a white-box
  19. * manner. */
  20. psa_status_t remove_seed_file(void)
  21. {
  22. #if defined(MBEDTLS_PSA_ITS_FILE_C)
  23. if (remove("00000000ffffff52.psa_its") == 0) {
  24. return PSA_SUCCESS;
  25. } else {
  26. return PSA_ERROR_DOES_NOT_EXIST;
  27. }
  28. #else
  29. return psa_its_remove(PSA_CRYPTO_ITS_RANDOM_SEED_UID);
  30. #endif
  31. }
  32. #endif /* MBEDTLS_PSA_INJECT_ENTROPY */
  33. /* END_HEADER */
  34. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  35. void external_rng_failure_generate()
  36. {
  37. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  38. psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
  39. psa_set_key_bits(&attributes, 128);
  40. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  41. uint8_t output[1];
  42. PSA_ASSERT(psa_crypto_init());
  43. PSA_ASSERT(psa_generate_random(output, sizeof(output)));
  44. PSA_ASSERT(psa_generate_key(&attributes, &key));
  45. PSA_ASSERT(psa_destroy_key(key));
  46. mbedtls_test_disable_insecure_external_rng();
  47. TEST_EQUAL(PSA_ERROR_INSUFFICIENT_ENTROPY,
  48. psa_generate_random(output, sizeof(output)));
  49. TEST_EQUAL(PSA_ERROR_INSUFFICIENT_ENTROPY,
  50. psa_generate_key(&attributes, &key));
  51. exit:
  52. psa_destroy_key(key);
  53. PSA_DONE();
  54. }
  55. /* END_CASE */
  56. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  57. void external_rng_failure_sign(int key_type, data_t *key_data, int alg,
  58. int input_size_arg)
  59. {
  60. /* This test case is only expected to pass if the signature mechanism
  61. * requires randomness, either because it is a randomized signature
  62. * or because the implementation uses blinding. */
  63. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  64. psa_set_key_type(&attributes, key_type);
  65. psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
  66. psa_set_key_algorithm(&attributes, alg);
  67. mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
  68. size_t input_size = input_size_arg;
  69. uint8_t *input = NULL;
  70. uint8_t *signature = NULL;
  71. size_t signature_size = PSA_SIGNATURE_MAX_SIZE;
  72. size_t signature_length;
  73. ASSERT_ALLOC(input, input_size);
  74. ASSERT_ALLOC(signature, signature_size);
  75. PSA_ASSERT(psa_crypto_init());
  76. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  77. &key));
  78. PSA_ASSERT(psa_sign_hash(key, alg,
  79. input, input_size,
  80. signature, signature_size,
  81. &signature_length));
  82. PSA_ASSERT(psa_destroy_key(key));
  83. mbedtls_test_disable_insecure_external_rng();
  84. /* Import the key again, because for RSA Mbed TLS caches blinding values
  85. * in the key object and this could perturb the test. */
  86. PSA_ASSERT(psa_import_key(&attributes, key_data->x, key_data->len,
  87. &key));
  88. TEST_EQUAL(PSA_ERROR_INSUFFICIENT_ENTROPY,
  89. psa_sign_hash(key, alg,
  90. input, input_size,
  91. signature, signature_size,
  92. &signature_length));
  93. PSA_ASSERT(psa_destroy_key(key));
  94. exit:
  95. psa_destroy_key(key);
  96. PSA_DONE();
  97. mbedtls_free(input);
  98. mbedtls_free(signature);
  99. }
  100. /* END_CASE */
  101. /* BEGIN_CASE depends_on:MBEDTLS_PSA_INJECT_ENTROPY */
  102. void validate_entropy_seed_injection(int seed_length_a,
  103. int expected_status_a,
  104. int seed_length_b,
  105. int expected_status_b)
  106. {
  107. psa_status_t status;
  108. uint8_t output[32] = { 0 };
  109. uint8_t zeros[32] = { 0 };
  110. uint8_t *seed = NULL;
  111. int i;
  112. int seed_size;
  113. if (seed_length_a > seed_length_b) {
  114. seed_size = seed_length_a;
  115. } else {
  116. seed_size = seed_length_b;
  117. }
  118. ASSERT_ALLOC(seed, seed_size);
  119. /* fill seed with some data */
  120. for (i = 0; i < seed_size; ++i) {
  121. seed[i] = i;
  122. }
  123. status = remove_seed_file();
  124. TEST_ASSERT((status == PSA_SUCCESS) ||
  125. (status == PSA_ERROR_DOES_NOT_EXIST));
  126. status = mbedtls_psa_inject_entropy(seed, seed_length_a);
  127. TEST_EQUAL(status, expected_status_a);
  128. status = mbedtls_psa_inject_entropy(seed, seed_length_b);
  129. TEST_EQUAL(status, expected_status_b);
  130. PSA_ASSERT(psa_crypto_init());
  131. PSA_ASSERT(psa_generate_random(output,
  132. sizeof(output)));
  133. TEST_ASSERT(memcmp(output, zeros, sizeof(output)) != 0);
  134. exit:
  135. mbedtls_free(seed);
  136. remove_seed_file();
  137. PSA_DONE();
  138. }
  139. /* END_CASE */
  140. /* BEGIN_CASE depends_on:MBEDTLS_PSA_INJECT_ENTROPY */
  141. void run_entropy_inject_with_crypto_init()
  142. {
  143. psa_status_t status;
  144. size_t i;
  145. uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = { 0 };
  146. /* fill seed with some data */
  147. for (i = 0; i < sizeof(seed); ++i) {
  148. seed[i] = i;
  149. }
  150. status = remove_seed_file();
  151. TEST_ASSERT((status == PSA_SUCCESS) ||
  152. (status == PSA_ERROR_DOES_NOT_EXIST));
  153. status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
  154. PSA_ASSERT(status);
  155. status = remove_seed_file();
  156. TEST_EQUAL(status, PSA_SUCCESS);
  157. status = psa_crypto_init();
  158. TEST_EQUAL(status, PSA_ERROR_INSUFFICIENT_ENTROPY);
  159. status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
  160. PSA_ASSERT(status);
  161. status = psa_crypto_init();
  162. PSA_ASSERT(status);
  163. PSA_DONE();
  164. /* The seed is written by nv_seed callback functions therefore the injection will fail */
  165. status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
  166. TEST_EQUAL(status, PSA_ERROR_NOT_PERMITTED);
  167. exit:
  168. remove_seed_file();
  169. PSA_DONE();
  170. }
  171. /* END_CASE */