psa_crypto_driver_wrappers_no_static.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Functions to delegate cryptographic operations to an available
  3. * and appropriate accelerator.
  4. * Warning: This file is now auto-generated.
  5. */
  6. /* Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  8. */
  9. /* BEGIN-common headers */
  10. #include "common.h"
  11. #include "psa_crypto_aead.h"
  12. #include "psa_crypto_cipher.h"
  13. #include "psa_crypto_core.h"
  14. #include "psa_crypto_driver_wrappers_no_static.h"
  15. #include "psa_crypto_hash.h"
  16. #include "psa_crypto_mac.h"
  17. #include "psa_crypto_pake.h"
  18. #include "psa_crypto_rsa.h"
  19. #include "mbedtls/platform.h"
  20. /* END-common headers */
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. /* BEGIN-driver headers */
  23. /* Headers for mbedtls_test opaque driver */
  24. #if defined(PSA_CRYPTO_DRIVER_TEST)
  25. #include "test/drivers/test_driver.h"
  26. #endif
  27. /* Headers for mbedtls_test transparent driver */
  28. #if defined(PSA_CRYPTO_DRIVER_TEST)
  29. #include "test/drivers/test_driver.h"
  30. #endif
  31. /* Headers for p256 transparent driver */
  32. #if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
  33. #include "../3rdparty/p256-m/p256-m_driver_entrypoints.h"
  34. #endif
  35. /* END-driver headers */
  36. /* Auto-generated values depending on which drivers are registered.
  37. * ID 0 is reserved for unallocated operations.
  38. * ID 1 is reserved for the Mbed TLS software driver. */
  39. /* BEGIN-driver id definition */
  40. #define PSA_CRYPTO_MBED_TLS_DRIVER_ID (1)
  41. #define MBEDTLS_TEST_OPAQUE_DRIVER_ID (2)
  42. #define MBEDTLS_TEST_TRANSPARENT_DRIVER_ID (3)
  43. #define P256_TRANSPARENT_DRIVER_ID (4)
  44. /* END-driver id */
  45. /* BEGIN-Common Macro definitions */
  46. /* END-Common Macro definitions */
  47. /* Support the 'old' SE interface when asked to */
  48. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  49. /* PSA_CRYPTO_DRIVER_PRESENT is defined when either a new-style or old-style
  50. * SE driver is present, to avoid unused argument errors at compile time. */
  51. #ifndef PSA_CRYPTO_DRIVER_PRESENT
  52. #define PSA_CRYPTO_DRIVER_PRESENT
  53. #endif
  54. #include "psa_crypto_se.h"
  55. #endif
  56. /** Get the key buffer size required to store the key material of a key
  57. * associated with an opaque driver.
  58. *
  59. * \param[in] attributes The key attributes.
  60. * \param[out] key_buffer_size Minimum buffer size to contain the key material
  61. *
  62. * \retval #PSA_SUCCESS
  63. * The minimum size for a buffer to contain the key material has been
  64. * returned successfully.
  65. * \retval #PSA_ERROR_NOT_SUPPORTED
  66. * The type and/or the size in bits of the key or the combination of
  67. * the two is not supported.
  68. * \retval #PSA_ERROR_INVALID_ARGUMENT
  69. * The key is declared with a lifetime not known to us.
  70. */
  71. psa_status_t psa_driver_wrapper_get_key_buffer_size(
  72. const psa_key_attributes_t *attributes,
  73. size_t *key_buffer_size )
  74. {
  75. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  76. psa_key_type_t key_type = psa_get_key_type(attributes);
  77. size_t key_bits = psa_get_key_bits(attributes);
  78. *key_buffer_size = 0;
  79. switch( location )
  80. {
  81. #if defined(PSA_CRYPTO_DRIVER_TEST)
  82. case PSA_CRYPTO_TEST_DRIVER_LOCATION:
  83. #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
  84. /* Emulate property 'builtin_key_size' */
  85. if( psa_key_id_is_builtin(
  86. MBEDTLS_SVC_KEY_ID_GET_KEY_ID(
  87. psa_get_key_id( attributes ) ) ) )
  88. {
  89. *key_buffer_size = sizeof( psa_drv_slot_number_t );
  90. return( PSA_SUCCESS );
  91. }
  92. #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
  93. *key_buffer_size = mbedtls_test_opaque_size_function( key_type,
  94. key_bits );
  95. return( ( *key_buffer_size != 0 ) ?
  96. PSA_SUCCESS : PSA_ERROR_NOT_SUPPORTED );
  97. #endif /* PSA_CRYPTO_DRIVER_TEST */
  98. default:
  99. (void)key_type;
  100. (void)key_bits;
  101. return( PSA_ERROR_INVALID_ARGUMENT );
  102. }
  103. }
  104. psa_status_t psa_driver_wrapper_export_public_key(
  105. const psa_key_attributes_t *attributes,
  106. const uint8_t *key_buffer, size_t key_buffer_size,
  107. uint8_t *data, size_t data_size, size_t *data_length )
  108. {
  109. psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
  110. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION(
  111. psa_get_key_lifetime( attributes ) );
  112. /* Try dynamically-registered SE interface first */
  113. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  114. const psa_drv_se_t *drv;
  115. psa_drv_se_context_t *drv_context;
  116. if( psa_get_se_driver( psa_get_key_lifetime(attributes), &drv, &drv_context ) )
  117. {
  118. if( ( drv->key_management == NULL ) ||
  119. ( drv->key_management->p_export_public == NULL ) )
  120. {
  121. return( PSA_ERROR_NOT_SUPPORTED );
  122. }
  123. return( drv->key_management->p_export_public(
  124. drv_context,
  125. *( (psa_key_slot_number_t *)key_buffer ),
  126. data, data_size, data_length ) );
  127. }
  128. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  129. switch( location )
  130. {
  131. case PSA_KEY_LOCATION_LOCAL_STORAGE:
  132. /* Key is stored in the slot in export representation, so
  133. * cycle through all known transparent accelerators */
  134. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  135. #if (defined(PSA_CRYPTO_DRIVER_TEST) )
  136. status = mbedtls_test_transparent_export_public_key
  137. (attributes,
  138. key_buffer,
  139. key_buffer_size,
  140. data,
  141. data_size,
  142. data_length
  143. );
  144. if( status != PSA_ERROR_NOT_SUPPORTED )
  145. return( status );
  146. #endif
  147. #if (defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED) )
  148. status = p256_transparent_export_public_key
  149. (attributes,
  150. key_buffer,
  151. key_buffer_size,
  152. data,
  153. data_size,
  154. data_length
  155. );
  156. if( status != PSA_ERROR_NOT_SUPPORTED )
  157. return( status );
  158. #endif
  159. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  160. /* Fell through, meaning no accelerator supports this operation */
  161. return( psa_export_public_key_internal( attributes,
  162. key_buffer,
  163. key_buffer_size,
  164. data,
  165. data_size,
  166. data_length ) );
  167. /* Add cases for opaque driver here */
  168. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  169. #if (defined(PSA_CRYPTO_DRIVER_TEST) )
  170. case 0x7fffff:
  171. return( mbedtls_test_opaque_export_public_key
  172. (attributes,
  173. key_buffer,
  174. key_buffer_size,
  175. data,
  176. data_size,
  177. data_length
  178. ));
  179. #endif
  180. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  181. default:
  182. /* Key is declared with a lifetime not known to us */
  183. return( status );
  184. }
  185. }
  186. psa_status_t psa_driver_wrapper_get_builtin_key(
  187. psa_drv_slot_number_t slot_number,
  188. psa_key_attributes_t *attributes,
  189. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  190. {
  191. psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( psa_get_key_lifetime(attributes) );
  192. switch( location )
  193. {
  194. #if defined(PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT)
  195. #if (defined(PSA_CRYPTO_DRIVER_TEST) )
  196. case 0x7fffff:
  197. return( mbedtls_test_opaque_get_builtin_key
  198. (slot_number,
  199. attributes,
  200. key_buffer,
  201. key_buffer_size,
  202. key_buffer_length
  203. ));
  204. #endif
  205. #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
  206. default:
  207. (void) slot_number;
  208. (void) key_buffer;
  209. (void) key_buffer_size;
  210. (void) key_buffer_length;
  211. return( PSA_ERROR_DOES_NOT_EXIST );
  212. }
  213. }
  214. #endif /* MBEDTLS_PSA_CRYPTO_C */