Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2018, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <limits.h> |
| 9 | |
| 10 | #include "tfm_crypto_defs.h" |
| 11 | |
| 12 | /* Pre include Mbed TLS headers */ |
| 13 | #define LIB_PREFIX_NAME __tfm_crypto__ |
| 14 | #include "mbedtls_global_symbols.h" |
| 15 | |
| 16 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it |
| 17 | * in each of its header files. |
| 18 | */ |
| 19 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 20 | #include "platform/ext/common/tfm_mbedtls_config.h" |
| 21 | #else |
| 22 | #include MBEDTLS_CONFIG_FILE |
| 23 | #endif |
| 24 | |
| 25 | #include "psa_crypto.h" |
| 26 | |
| 27 | /* The file "psa_crypto_struct.h" contains definitions for |
| 28 | * implementation-specific structs that are declared in "psa_crypto.h". |
| 29 | */ |
| 30 | #include "psa_crypto_struct.h" |
| 31 | |
| 32 | /** |
| 33 | * \brief This value defines the maximum number of simultaneous key stores |
| 34 | * supported by this implementation. |
| 35 | */ |
| 36 | #define TFM_CRYPTO_KEY_STORAGE_NUM (4) |
| 37 | |
| 38 | struct tfm_crypto_key_storage_s { |
| 39 | uint8_t in_use; /*!< Indicates if the key store is in use */ |
| 40 | psa_key_type_t type; /*!< Type of the key stored */ |
| 41 | size_t data_length; /*!< Length of the key stored */ |
| 42 | uint8_t data[TFM_CRYPTO_MAX_KEY_LENGTH]; /*!< Buffer containining the key */ |
| 43 | }; |
| 44 | |
| 45 | static struct tfm_crypto_key_storage_s |
| 46 | key_storage[TFM_CRYPTO_KEY_STORAGE_NUM] = {{0}}; |
| 47 | /*! |
| 48 | * \defgroup public Public functions |
| 49 | * |
| 50 | */ |
| 51 | |
| 52 | /*!@{*/ |
| 53 | enum tfm_crypto_err_t tfm_crypto_import_key(psa_key_slot_t key, |
| 54 | psa_key_type_t type, |
| 55 | const uint8_t *data, |
| 56 | size_t data_length) |
| 57 | { |
| 58 | size_t i; |
| 59 | |
| 60 | if (key >= TFM_CRYPTO_KEY_STORAGE_NUM) { |
| 61 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 62 | } |
| 63 | |
| 64 | if (key_storage[key].in_use != TFM_CRYPTO_NOT_IN_USE) { |
| 65 | return TFM_CRYPTO_ERR_PSA_ERROR_OCCUPIED_SLOT; |
| 66 | } |
| 67 | |
| 68 | if (data_length > TFM_CRYPTO_MAX_KEY_LENGTH) { |
| 69 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_SUPPORTED; |
| 70 | } |
| 71 | |
| 72 | key_storage[key].in_use = TFM_CRYPTO_IN_USE; |
| 73 | key_storage[key].type = type; |
| 74 | |
| 75 | for (i=0; i<data_length; i++) { |
| 76 | key_storage[key].data[i] = data[i]; |
| 77 | } |
| 78 | |
| 79 | key_storage[key].data_length = data_length; |
| 80 | |
| 81 | |
| 82 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 83 | } |
| 84 | |
| 85 | enum tfm_crypto_err_t tfm_crypto_destroy_key(psa_key_slot_t key) |
| 86 | { |
| 87 | uint32_t i; |
| 88 | |
| 89 | if (key >= TFM_CRYPTO_KEY_STORAGE_NUM) { |
| 90 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 91 | } |
| 92 | |
| 93 | volatile uint8_t *p_mem = (uint8_t *) &(key_storage[key]); |
| 94 | uint32_t size_mem = sizeof(struct tfm_crypto_key_storage_s); |
| 95 | |
| 96 | /* memset the key_storage */ |
| 97 | for (i=0; i<size_mem; i++) { |
| 98 | p_mem[i] = 0; |
| 99 | } |
| 100 | |
| 101 | /* Set default values */ |
| 102 | key_storage[key].in_use = TFM_CRYPTO_NOT_IN_USE; |
| 103 | |
| 104 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 105 | } |
| 106 | |
| 107 | enum tfm_crypto_err_t tfm_crypto_get_key_information(psa_key_slot_t key, |
| 108 | psa_key_type_t *type, |
| 109 | size_t *bits) |
| 110 | { |
| 111 | if (key >= TFM_CRYPTO_KEY_STORAGE_NUM) { |
| 112 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 113 | } |
| 114 | |
| 115 | if (key_storage[key].in_use == TFM_CRYPTO_NOT_IN_USE) { |
| 116 | return TFM_CRYPTO_ERR_PSA_ERROR_EMPTY_SLOT; |
| 117 | } |
| 118 | |
| 119 | /* Get basic metadata */ |
| 120 | *type = key_storage[key].type; |
| 121 | *bits = PSA_BYTES_TO_BITS(key_storage[key].data_length); |
| 122 | |
| 123 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 124 | } |
| 125 | |
| 126 | enum tfm_crypto_err_t tfm_crypto_export_key(psa_key_slot_t key, |
| 127 | uint8_t *data, |
| 128 | size_t data_size, |
| 129 | size_t *data_length) |
| 130 | { |
| 131 | size_t i; |
| 132 | |
| 133 | if (key >= TFM_CRYPTO_KEY_STORAGE_NUM) { |
| 134 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 135 | } |
| 136 | |
| 137 | if (key_storage[key].in_use == TFM_CRYPTO_NOT_IN_USE) { |
| 138 | return TFM_CRYPTO_ERR_PSA_ERROR_EMPTY_SLOT; |
| 139 | } |
| 140 | |
| 141 | if (key_storage[key].data_length > data_size) { |
| 142 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_SUPPORTED; |
| 143 | } |
| 144 | |
| 145 | for (i=0; i<key_storage[key].data_length; i++) { |
| 146 | data[i] = key_storage[key].data[i]; |
| 147 | } |
| 148 | |
| 149 | *data_length = key_storage[key].data_length; |
| 150 | |
| 151 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | enum tfm_crypto_err_t tfm_crypto_export_public_key(psa_key_slot_t key, |
| 155 | uint8_t *data, |
| 156 | size_t data_size, |
| 157 | size_t *data_length) |
| 158 | { |
| 159 | /* FIXME: This API is not supported yet */ |
| 160 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_SUPPORTED; |
| 161 | } |
| 162 | /*!@}*/ |