test_suite_psa_crypto_init.function 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* BEGIN_HEADER */
  2. #include <stdint.h>
  3. /* Some tests in this module configure entropy sources. */
  4. #include "psa_crypto_invasive.h"
  5. #include "mbedtls/entropy.h"
  6. #include "entropy_poll.h"
  7. #define ENTROPY_MIN_NV_SEED_SIZE \
  8. MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
  9. #include "psa_crypto_random_impl.h"
  10. #if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
  11. /* PSA crypto uses the HMAC_DRBG module. It reads from the entropy source twice:
  12. * once for the initial entropy and once for a nonce. The nonce length is
  13. * half the entropy length. For SHA-256, SHA-384 or SHA-512, the
  14. * entropy length is 256 per the documentation of mbedtls_hmac_drbg_seed(),
  15. * and PSA crypto doesn't support other hashes for HMAC_DRBG. */
  16. #define ENTROPY_NONCE_LEN (256 / 2)
  17. #else
  18. /* PSA crypto uses the CTR_DRBG module. In some configurations, it needs
  19. * to read from the entropy source twice: once for the initial entropy
  20. * and once for a nonce. */
  21. #include "mbedtls/ctr_drbg.h"
  22. #define ENTROPY_NONCE_LEN MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN
  23. #endif
  24. #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  25. typedef struct {
  26. size_t threshold; /* Minimum bytes to make mbedtls_entropy_func happy */
  27. size_t max_steps;
  28. size_t *length_sequence;
  29. size_t step;
  30. } fake_entropy_state_t;
  31. static int fake_entropy_source(void *state_arg,
  32. unsigned char *output, size_t len,
  33. size_t *olen)
  34. {
  35. fake_entropy_state_t *state = state_arg;
  36. size_t i;
  37. if (state->step >= state->max_steps) {
  38. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  39. }
  40. *olen = MIN(len, state->length_sequence[state->step]);
  41. for (i = 0; i < *olen; i++) {
  42. output[i] = i;
  43. }
  44. ++state->step;
  45. return 0;
  46. }
  47. #define ENTROPY_SOURCE_PLATFORM 0x00000001
  48. #define ENTROPY_SOURCE_TIMING 0x00000002
  49. #define ENTROPY_SOURCE_HARDWARE 0x00000004
  50. #define ENTROPY_SOURCE_NV_SEED 0x00000008
  51. #define ENTROPY_SOURCE_FAKE 0x40000000
  52. static uint32_t custom_entropy_sources_mask;
  53. static fake_entropy_state_t fake_entropy_state;
  54. /* This is a modified version of mbedtls_entropy_init() from entropy.c
  55. * which chooses entropy sources dynamically. */
  56. static void custom_entropy_init(mbedtls_entropy_context *ctx)
  57. {
  58. ctx->source_count = 0;
  59. memset(ctx->source, 0, sizeof(ctx->source));
  60. #if defined(MBEDTLS_THREADING_C)
  61. mbedtls_mutex_init(&ctx->mutex);
  62. #endif
  63. ctx->accumulator_started = 0;
  64. #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
  65. mbedtls_sha512_init(&ctx->accumulator);
  66. #else
  67. mbedtls_sha256_init(&ctx->accumulator);
  68. #endif
  69. #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
  70. if (custom_entropy_sources_mask & ENTROPY_SOURCE_PLATFORM) {
  71. mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
  72. MBEDTLS_ENTROPY_MIN_PLATFORM,
  73. MBEDTLS_ENTROPY_SOURCE_STRONG);
  74. }
  75. #endif
  76. #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
  77. if (custom_entropy_sources_mask & ENTROPY_SOURCE_HARDWARE) {
  78. mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
  79. MBEDTLS_ENTROPY_MIN_HARDWARE,
  80. MBEDTLS_ENTROPY_SOURCE_STRONG);
  81. }
  82. #endif
  83. #if defined(MBEDTLS_ENTROPY_NV_SEED)
  84. if (custom_entropy_sources_mask & ENTROPY_SOURCE_NV_SEED) {
  85. mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
  86. MBEDTLS_ENTROPY_BLOCK_SIZE,
  87. MBEDTLS_ENTROPY_SOURCE_STRONG);
  88. ctx->initial_entropy_run = 0;
  89. } else {
  90. /* Skip the NV seed even though it's compiled in. */
  91. ctx->initial_entropy_run = 1;
  92. }
  93. #endif
  94. if (custom_entropy_sources_mask & ENTROPY_SOURCE_FAKE) {
  95. mbedtls_entropy_add_source(ctx,
  96. fake_entropy_source, &fake_entropy_state,
  97. fake_entropy_state.threshold,
  98. MBEDTLS_ENTROPY_SOURCE_STRONG);
  99. }
  100. }
  101. #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
  102. /* END_HEADER */
  103. /* BEGIN_DEPENDENCIES
  104. * depends_on:MBEDTLS_PSA_CRYPTO_C
  105. * END_DEPENDENCIES
  106. */
  107. /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  108. void create_nv_seed()
  109. {
  110. static unsigned char seed[ENTROPY_MIN_NV_SEED_SIZE];
  111. TEST_ASSERT(mbedtls_nv_seed_write(seed, sizeof(seed)) >= 0);
  112. }
  113. /* END_CASE */
  114. /* BEGIN_CASE */
  115. void init_deinit(int count)
  116. {
  117. psa_status_t status;
  118. int i;
  119. for (i = 0; i < count; i++) {
  120. status = psa_crypto_init();
  121. PSA_ASSERT(status);
  122. status = psa_crypto_init();
  123. PSA_ASSERT(status);
  124. PSA_DONE();
  125. }
  126. }
  127. /* END_CASE */
  128. /* BEGIN_CASE */
  129. void deinit_without_init(int count)
  130. {
  131. int i;
  132. for (i = 0; i < count; i++) {
  133. PSA_ASSERT(psa_crypto_init());
  134. PSA_DONE();
  135. }
  136. PSA_DONE();
  137. }
  138. /* END_CASE */
  139. /* BEGIN_CASE */
  140. void validate_module_init_generate_random(int count)
  141. {
  142. psa_status_t status;
  143. uint8_t random[10] = { 0 };
  144. int i;
  145. for (i = 0; i < count; i++) {
  146. status = psa_crypto_init();
  147. PSA_ASSERT(status);
  148. PSA_DONE();
  149. }
  150. status = psa_generate_random(random, sizeof(random));
  151. TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
  152. }
  153. /* END_CASE */
  154. /* BEGIN_CASE */
  155. void validate_module_init_key_based(int count)
  156. {
  157. psa_status_t status;
  158. uint8_t data[10] = { 0 };
  159. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  160. mbedtls_svc_key_id_t key = mbedtls_svc_key_id_make(0xdead, 0xdead);
  161. int i;
  162. for (i = 0; i < count; i++) {
  163. status = psa_crypto_init();
  164. PSA_ASSERT(status);
  165. PSA_DONE();
  166. }
  167. psa_set_key_type(&attributes, PSA_KEY_TYPE_RAW_DATA);
  168. status = psa_import_key(&attributes, data, sizeof(data), &key);
  169. TEST_EQUAL(status, PSA_ERROR_BAD_STATE);
  170. TEST_ASSERT(mbedtls_svc_key_id_is_null(key));
  171. }
  172. /* END_CASE */
  173. /* BEGIN_CASE depends_on:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  174. void custom_entropy_sources(int sources_arg, int expected_init_status_arg)
  175. {
  176. psa_status_t expected_init_status = expected_init_status_arg;
  177. uint8_t random[10] = { 0 };
  178. custom_entropy_sources_mask = sources_arg;
  179. PSA_ASSERT(mbedtls_psa_crypto_configure_entropy_sources(
  180. custom_entropy_init, mbedtls_entropy_free));
  181. TEST_EQUAL(psa_crypto_init(), expected_init_status);
  182. if (expected_init_status != PSA_SUCCESS) {
  183. goto exit;
  184. }
  185. PSA_ASSERT(psa_generate_random(random, sizeof(random)));
  186. exit:
  187. PSA_DONE();
  188. }
  189. /* END_CASE */
  190. /* BEGIN_CASE depends_on:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  191. void fake_entropy_source(int threshold,
  192. int amount1,
  193. int amount2,
  194. int amount3,
  195. int amount4,
  196. int expected_init_status_arg)
  197. {
  198. psa_status_t expected_init_status = expected_init_status_arg;
  199. uint8_t random[10] = { 0 };
  200. size_t lengths[4];
  201. fake_entropy_state.threshold = threshold;
  202. fake_entropy_state.step = 0;
  203. fake_entropy_state.max_steps = 0;
  204. if (amount1 >= 0) {
  205. lengths[fake_entropy_state.max_steps++] = amount1;
  206. }
  207. if (amount2 >= 0) {
  208. lengths[fake_entropy_state.max_steps++] = amount2;
  209. }
  210. if (amount3 >= 0) {
  211. lengths[fake_entropy_state.max_steps++] = amount3;
  212. }
  213. if (amount4 >= 0) {
  214. lengths[fake_entropy_state.max_steps++] = amount4;
  215. }
  216. fake_entropy_state.length_sequence = lengths;
  217. custom_entropy_sources_mask = ENTROPY_SOURCE_FAKE;
  218. PSA_ASSERT(mbedtls_psa_crypto_configure_entropy_sources(
  219. custom_entropy_init, mbedtls_entropy_free));
  220. TEST_EQUAL(psa_crypto_init(), expected_init_status);
  221. if (expected_init_status != PSA_SUCCESS) {
  222. goto exit;
  223. }
  224. PSA_ASSERT(psa_generate_random(random, sizeof(random)));
  225. exit:
  226. PSA_DONE();
  227. }
  228. /* END_CASE */
  229. /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  230. void entropy_from_nv_seed(int seed_size_arg,
  231. int expected_init_status_arg)
  232. {
  233. psa_status_t expected_init_status = expected_init_status_arg;
  234. uint8_t random[10] = { 0 };
  235. uint8_t *seed = NULL;
  236. size_t seed_size = seed_size_arg;
  237. ASSERT_ALLOC(seed, seed_size);
  238. TEST_ASSERT(mbedtls_nv_seed_write(seed, seed_size) >= 0);
  239. custom_entropy_sources_mask = ENTROPY_SOURCE_NV_SEED;
  240. PSA_ASSERT(mbedtls_psa_crypto_configure_entropy_sources(
  241. custom_entropy_init, mbedtls_entropy_free));
  242. TEST_EQUAL(psa_crypto_init(), expected_init_status);
  243. if (expected_init_status != PSA_SUCCESS) {
  244. goto exit;
  245. }
  246. PSA_ASSERT(psa_generate_random(random, sizeof(random)));
  247. exit:
  248. mbedtls_free(seed);
  249. PSA_DONE();
  250. }
  251. /* END_CASE */