psa_crypto_client.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * PSA crypto client code
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #include "psa/crypto.h"
  22. #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
  23. #include <string.h>
  24. #include "mbedtls/platform.h"
  25. #if !defined(MBEDTLS_PLATFORM_C)
  26. #define mbedtls_calloc calloc
  27. #define mbedtls_free free
  28. #endif
  29. void psa_reset_key_attributes( psa_key_attributes_t *attributes )
  30. {
  31. mbedtls_free( attributes->domain_parameters );
  32. memset( attributes, 0, sizeof( *attributes ) );
  33. }
  34. psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
  35. psa_key_type_t type,
  36. const uint8_t *data,
  37. size_t data_length )
  38. {
  39. uint8_t *copy = NULL;
  40. if( data_length != 0 )
  41. {
  42. copy = mbedtls_calloc( 1, data_length );
  43. if( copy == NULL )
  44. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  45. memcpy( copy, data, data_length );
  46. }
  47. /* After this point, this function is guaranteed to succeed, so it
  48. * can start modifying `*attributes`. */
  49. if( attributes->domain_parameters != NULL )
  50. {
  51. mbedtls_free( attributes->domain_parameters );
  52. attributes->domain_parameters = NULL;
  53. attributes->domain_parameters_size = 0;
  54. }
  55. attributes->domain_parameters = copy;
  56. attributes->domain_parameters_size = data_length;
  57. attributes->core.type = type;
  58. return( PSA_SUCCESS );
  59. }
  60. psa_status_t psa_get_key_domain_parameters(
  61. const psa_key_attributes_t *attributes,
  62. uint8_t *data, size_t data_size, size_t *data_length )
  63. {
  64. if( attributes->domain_parameters_size > data_size )
  65. return( PSA_ERROR_BUFFER_TOO_SMALL );
  66. *data_length = attributes->domain_parameters_size;
  67. if( attributes->domain_parameters_size != 0 )
  68. memcpy( data, attributes->domain_parameters,
  69. attributes->domain_parameters_size );
  70. return( PSA_SUCCESS );
  71. }
  72. #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */