psa_crypto_client.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. void psa_reset_key_attributes( psa_key_attributes_t *attributes )
  26. {
  27. mbedtls_free( attributes->domain_parameters );
  28. memset( attributes, 0, sizeof( *attributes ) );
  29. }
  30. psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
  31. psa_key_type_t type,
  32. const uint8_t *data,
  33. size_t data_length )
  34. {
  35. uint8_t *copy = NULL;
  36. if( data_length != 0 )
  37. {
  38. copy = mbedtls_calloc( 1, data_length );
  39. if( copy == NULL )
  40. return( PSA_ERROR_INSUFFICIENT_MEMORY );
  41. memcpy( copy, data, data_length );
  42. }
  43. /* After this point, this function is guaranteed to succeed, so it
  44. * can start modifying `*attributes`. */
  45. if( attributes->domain_parameters != NULL )
  46. {
  47. mbedtls_free( attributes->domain_parameters );
  48. attributes->domain_parameters = NULL;
  49. attributes->domain_parameters_size = 0;
  50. }
  51. attributes->domain_parameters = copy;
  52. attributes->domain_parameters_size = data_length;
  53. attributes->core.type = type;
  54. return( PSA_SUCCESS );
  55. }
  56. psa_status_t psa_get_key_domain_parameters(
  57. const psa_key_attributes_t *attributes,
  58. uint8_t *data, size_t data_size, size_t *data_length )
  59. {
  60. if( attributes->domain_parameters_size > data_size )
  61. return( PSA_ERROR_BUFFER_TOO_SMALL );
  62. *data_length = attributes->domain_parameters_size;
  63. if( attributes->domain_parameters_size != 0 )
  64. memcpy( data, attributes->domain_parameters,
  65. attributes->domain_parameters_size );
  66. return( PSA_SUCCESS );
  67. }
  68. #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */