Mistake on this page?
Report an issue in GitHub or email us
psa_crypto_core.h
1 /*
2  * PSA crypto core internal interfaces
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 
21 #ifndef PSA_CRYPTO_CORE_H
22 #define PSA_CRYPTO_CORE_H
23 
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29 
30 #include "psa/crypto.h"
31 #include "psa/crypto_se_driver.h"
32 
33 /** The data structure representing a key slot, containing key material
34  * and metadata for one key.
35  */
36 typedef struct
37 {
39  union
40  {
41  /* Dynamically allocated key data buffer.
42  * Format as specified in psa_export_key(). */
43  struct key_data
44  {
45  uint8_t *data;
46  size_t bytes;
47  } key;
48 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
49  /* Any key type in a secure element */
50  struct se
51  {
52  psa_key_slot_number_t slot_number;
53  } se;
54 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
55  } data;
57 
58 /* A mask of key attribute flags used only internally.
59  * Currently there aren't any. */
60 #define PSA_KA_MASK_INTERNAL_ONLY ( \
61  0 )
62 
63 /** Test whether a key slot is occupied.
64  *
65  * A key slot is occupied iff the key type is nonzero. This works because
66  * no valid key can have 0 as its key type.
67  *
68  * \param[in] slot The key slot to test.
69  *
70  * \return 1 if the slot is occupied, 0 otherwise.
71  */
72 static inline int psa_is_key_slot_occupied( const psa_key_slot_t *slot )
73 {
74  return( slot->attr.type != 0 );
75 }
76 
77 /** Retrieve flags from psa_key_slot_t::attr::core::flags.
78  *
79  * \param[in] slot The key slot to query.
80  * \param mask The mask of bits to extract.
81  *
82  * \return The key attribute flags in the given slot,
83  * bitwise-anded with \p mask.
84  */
85 static inline uint16_t psa_key_slot_get_flags( const psa_key_slot_t *slot,
86  uint16_t mask )
87 {
88  return( slot->attr.flags & mask );
89 }
90 
91 /** Set flags in psa_key_slot_t::attr::core::flags.
92  *
93  * \param[in,out] slot The key slot to modify.
94  * \param mask The mask of bits to modify.
95  * \param value The new value of the selected bits.
96  */
97 static inline void psa_key_slot_set_flags( psa_key_slot_t *slot,
98  uint16_t mask,
99  uint16_t value )
100 {
101  slot->attr.flags = ( ( ~mask & slot->attr.flags ) |
102  ( mask & value ) );
103 }
104 
105 /** Turn on flags in psa_key_slot_t::attr::core::flags.
106  *
107  * \param[in,out] slot The key slot to modify.
108  * \param mask The mask of bits to set.
109  */
110 static inline void psa_key_slot_set_bits_in_flags( psa_key_slot_t *slot,
111  uint16_t mask )
112 {
113  slot->attr.flags |= mask;
114 }
115 
116 /** Turn off flags in psa_key_slot_t::attr::core::flags.
117  *
118  * \param[in,out] slot The key slot to modify.
119  * \param mask The mask of bits to clear.
120  */
121 static inline void psa_key_slot_clear_bits( psa_key_slot_t *slot,
122  uint16_t mask )
123 {
124  slot->attr.flags &= ~mask;
125 }
126 
127 /** Completely wipe a slot in memory, including its policy.
128  *
129  * Persistent storage is not affected.
130  *
131  * \param[in,out] slot The key slot to wipe.
132  *
133  * \retval PSA_SUCCESS
134  * Success. This includes the case of a key slot that was
135  * already fully wiped.
136  * \retval PSA_ERROR_CORRUPTION_DETECTED
137  */
138 psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot );
139 
140 /** Import key data into a slot.
141  *
142  * `slot->type` must have been set previously.
143  * This function assumes that the slot does not contain any key material yet.
144  * On failure, the slot content is unchanged.
145  *
146  * Persistent storage is not affected.
147  *
148  * \param[in,out] slot The key slot to import data into.
149  * Its `type` field must have previously been set to
150  * the desired key type.
151  * It must not contain any key material yet.
152  * \param[in] data Buffer containing the key material to parse and import.
153  * \param data_length Size of \p data in bytes.
154  *
155  * \retval PSA_SUCCESS
156  * \retval PSA_ERROR_INVALID_ARGUMENT
157  * \retval PSA_ERROR_NOT_SUPPORTED
158  * \retval PSA_ERROR_INSUFFICIENT_MEMORY
159  */
160 psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
161  const uint8_t *data,
162  size_t data_length );
163 
164 #endif /* PSA_CRYPTO_CORE_H */
uint64_t psa_key_slot_number_t
An internal designation of a key slot between the core part of the PSA Crypto implementation and the ...
PSA external cryptoprocessor driver module.
int32_t psa_status_t
Function return status.
The data structure representing a key slot, containing key material and metadata for one key...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.