Mistake on this page?
Report an issue in GitHub or email us
TARGET_MBED_PSA_SRV/inc/psa/crypto_extra.h
1 /**
2  * \file psa/crypto_extra.h
3  *
4  * \brief PSA cryptography module: Mbed TLS vendor extensions
5  *
6  * \note This file may not be included directly. Applications must
7  * include psa/crypto.h.
8  *
9  * This file is reserved for vendor-specific definitions.
10  */
11 /*
12  * Copyright The Mbed TLS Contributors
13  * SPDX-License-Identifier: Apache-2.0
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License"); you may
16  * not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */
27 
28 #ifndef PSA_CRYPTO_EXTRA_H
29 #define PSA_CRYPTO_EXTRA_H
30 
31 #include "mbedtls/platform_util.h"
32 
33 #include "crypto_compat.h"
34 
35 #include "platform/mbed_toolchain.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* UID for secure storage seed */
42 #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
43 
44 
45 /** \addtogroup attributes
46  * @{
47  */
48 
49 /** \brief Declare the enrollment algorithm for a key.
50  *
51  * An operation on a key may indifferently use the algorithm set with
52  * psa_set_key_algorithm() or with this function.
53  *
54  * \param[out] attributes The attribute structure to write to.
55  * \param alg2 A second algorithm that the key may be used
56  * for, in addition to the algorithm set with
57  * psa_set_key_algorithm().
58  *
59  * \deprecated This is for backward compatibility only.
60  * Setting an enrollment algorithm is not recommended, because
61  * using the same key with different algorithms can allow some
62  * attacks based on arithmetic relations between different
63  * computations made with the same key, or can escalate harmless
64  * side channels into exploitable ones. Use this function only
65  * if it is necessary to support a protocol for which it has been
66  * verified that the usage of the key with multiple algorithms
67  * is safe.
68  */
69 MBED_DEPRECATED("Setting enrollment algorithm is for backward compatibility and not recommended.")
71  psa_key_attributes_t *attributes,
72  psa_algorithm_t alg2)
73 {
74  attributes->core.policy.alg2 = alg2;
75 }
76 
77 /** Retrieve the enrollment algorithm policy from key attributes.
78  *
79  * \param[in] attributes The key attribute structure to query.
80  *
81  * \return The enrollment algorithm stored in the attribute structure.
82  * \deprecated This is for backward compatibility only.
83  * Deprecated along with psa_set_key_enrollment_algorithm().
84  */
85 MBED_DEPRECATED("Getting enrollment algorithm is for backward compatibility and not recommended.")
87  const psa_key_attributes_t *attributes)
88 {
89  return( attributes->core.policy.alg2 );
90 }
91 
92 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
93 
94 /** Retrieve the slot number where a key is stored.
95  *
96  * A slot number is only defined for keys that are stored in a secure
97  * element.
98  *
99  * This information is only useful if the secure element is not entirely
100  * managed through the PSA Cryptography API. It is up to the secure
101  * element driver to decide how PSA slot numbers map to any other interface
102  * that the secure element may have.
103  *
104  * \param[in] attributes The key attribute structure to query.
105  * \param[out] slot_number On success, the slot number containing the key.
106  *
107  * \retval #PSA_SUCCESS
108  * The key is located in a secure element, and \p *slot_number
109  * indicates the slot number that contains it.
110  * \retval #PSA_ERROR_NOT_PERMITTED
111  * The caller is not permitted to query the slot number.
112  * Mbed Crypto currently does not return this error.
113  * \retval #PSA_ERROR_INVALID_ARGUMENT
114  * The key is not located in a secure element.
115  */
116 psa_status_t psa_get_key_slot_number(
117  const psa_key_attributes_t *attributes,
118  psa_key_slot_number_t *slot_number );
119 
120 /** Choose the slot number where a key is stored.
121  *
122  * This function declares a slot number in the specified attribute
123  * structure.
124  *
125  * A slot number is only meaningful for keys that are stored in a secure
126  * element. It is up to the secure element driver to decide how PSA slot
127  * numbers map to any other interface that the secure element may have.
128  *
129  * \note Setting a slot number in key attributes for a key creation can
130  * cause the following errors when creating the key:
131  * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
132  * not support choosing a specific slot number.
133  * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
134  * choose slot numbers in general or to choose this specific slot.
135  * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
136  * valid in general or not valid for this specific key.
137  * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
138  * selected slot.
139  *
140  * \param[out] attributes The attribute structure to write to.
141  * \param slot_number The slot number to set.
142  */
143 static inline void psa_set_key_slot_number(
144  psa_key_attributes_t *attributes,
145  psa_key_slot_number_t slot_number )
146 {
147  attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
148  attributes->slot_number = slot_number;
149 }
150 
151 /** Remove the slot number attribute from a key attribute structure.
152  *
153  * This function undoes the action of psa_set_key_slot_number().
154  *
155  * \param[out] attributes The attribute structure to write to.
156  */
157 static inline void psa_clear_key_slot_number(
158  psa_key_attributes_t *attributes )
159 {
160  attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
161 }
162 
163 /** Register a key that is already present in a secure element.
164  *
165  * The key must be located in a secure element designated by the
166  * lifetime field in \p attributes, in the slot set with
167  * psa_set_key_slot_number() in the attribute structure.
168  * This function makes the key available through the key identifier
169  * specified in \p attributes.
170  *
171  * \param[in] attributes The attributes of the existing key.
172  *
173  * \retval #PSA_SUCCESS
174  * The key was successfully registered.
175  * Note that depending on the design of the driver, this may or may
176  * not guarantee that a key actually exists in the designated slot
177  * and is compatible with the specified attributes.
178  * \retval #PSA_ERROR_ALREADY_EXISTS
179  * There is already a key with the identifier specified in
180  * \p attributes.
181  * \retval #PSA_ERROR_NOT_SUPPORTED
182  * The secure element driver for the specified lifetime does not
183  * support registering a key.
184  * \retval #PSA_ERROR_INVALID_ARGUMENT
185  * \p attributes specifies a lifetime which is not located
186  * in a secure element.
187  * \retval #PSA_ERROR_INVALID_ARGUMENT
188  * No slot number is specified in \p attributes,
189  * or the specified slot number is not valid.
190  * \retval #PSA_ERROR_NOT_PERMITTED
191  * The caller is not authorized to register the specified key slot.
192  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
193  * \retval #PSA_ERROR_COMMUNICATION_FAILURE
194  * \retval #PSA_ERROR_HARDWARE_FAILURE
195  * \retval #PSA_ERROR_CORRUPTION_DETECTED
196  * \retval #PSA_ERROR_BAD_STATE
197  * The library has not been previously initialized by psa_crypto_init().
198  * It is implementation-dependent whether a failure to initialize
199  * results in this error code.
200  */
201 psa_status_t mbedtls_psa_register_se_key(
202  const psa_key_attributes_t *attributes);
203 
204 #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
205 
206 /**@}*/
207 
208 /**
209  * \brief Library deinitialization.
210  *
211  * This function clears all data associated with the PSA layer,
212  * including the whole key store.
213  *
214  * This is an Mbed TLS extension.
215  */
216 void mbedtls_psa_crypto_free( void );
217 
218 /** \brief Statistics about
219  * resource consumption related to the PSA keystore.
220  *
221  * \note The content of this structure is not part of the stable API and ABI
222  * of Mbed Crypto and may change arbitrarily from version to version.
223  */
224 typedef struct mbedtls_psa_stats_s
225 {
226  /** Number of slots containing key material for a volatile key. */
228  /** Number of slots containing key material for a key which is in
229  * internal persistent storage. */
231  /** Number of slots containing a reference to a key in a
232  * secure element. */
234  /** Number of slots which are occupied, but do not contain
235  * key material yet. */
237  /** Number of slots that contain cache data. */
238  size_t cache_slots;
239  /** Number of slots that are not used for anything. */
240  size_t empty_slots;
241  /** Largest key id value among open keys in internal persistent storage. */
242  psa_app_key_id_t max_open_internal_key_id;
243  /** Largest key id value among open keys in secure elements. */
244  psa_app_key_id_t max_open_external_key_id;
246 
247 /** \brief Get statistics about
248  * resource consumption related to the PSA keystore.
249  *
250  * \note When Mbed Crypto is built as part of a service, with isolation
251  * between the application and the keystore, the service may or
252  * may not expose this function.
253  */
254 void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats );
255 
256 /**
257  * \brief Inject an initial entropy seed for the random generator into
258  * secure storage.
259  *
260  * This function injects data to be used as a seed for the random generator
261  * used by the PSA Crypto implementation. On devices that lack a trusted
262  * entropy source (preferably a hardware random number generator),
263  * the Mbed PSA Crypto implementation uses this value to seed its
264  * random generator.
265  *
266  * On devices without a trusted entropy source, this function must be
267  * called exactly once in the lifetime of the device. On devices with
268  * a trusted entropy source, calling this function is optional.
269  * In all cases, this function may only be called before calling any
270  * other function in the PSA Crypto API, including psa_crypto_init().
271  *
272  * When this function returns successfully, it populates a file in
273  * persistent storage. Once the file has been created, this function
274  * can no longer succeed.
275  *
276  * If any error occurs, this function does not change the system state.
277  * You can call this function again after correcting the reason for the
278  * error if possible.
279  *
280  * \warning This function **can** fail! Callers MUST check the return status.
281  *
282  * \warning If you use this function, you should use it as part of a
283  * factory provisioning process. The value of the injected seed
284  * is critical to the security of the device. It must be
285  * *secret*, *unpredictable* and (statistically) *unique per device*.
286  * You should be generate it randomly using a cryptographically
287  * secure random generator seeded from trusted entropy sources.
288  * You should transmit it securely to the device and ensure
289  * that its value is not leaked or stored anywhere beyond the
290  * needs of transmitting it from the point of generation to
291  * the call of this function, and erase all copies of the value
292  * once this function returns.
293  *
294  * This is an Mbed TLS extension.
295  *
296  * \note This function is only available on the following platforms:
297  * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
298  * Note that you must provide compatible implementations of
299  * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
300  * * In a client-server integration of PSA Cryptography, on the client side,
301  * if the server supports this feature.
302  * \param[in] seed Buffer containing the seed value to inject.
303  * \param[in] seed_size Size of the \p seed buffer.
304  * The size of the seed in bytes must be greater
305  * or equal to both #MBEDTLS_ENTROPY_MIN_PLATFORM
306  * and #MBEDTLS_ENTROPY_BLOCK_SIZE.
307  * It must be less or equal to
308  * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
309  *
310  * \retval #PSA_SUCCESS
311  * The seed value was injected successfully. The random generator
312  * of the PSA Crypto implementation is now ready for use.
313  * You may now call psa_crypto_init() and use the PSA Crypto
314  * implementation.
315  * \retval #PSA_ERROR_INVALID_ARGUMENT
316  * \p seed_size is out of range.
317  * \retval #PSA_ERROR_STORAGE_FAILURE
318  * There was a failure reading or writing from storage.
319  * \retval #PSA_ERROR_NOT_PERMITTED
320  * The library has already been initialized. It is no longer
321  * possible to call this function.
322  */
323 psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
324  size_t seed_size);
325 
326 /** \addtogroup crypto_types
327  * @{
328  */
329 
330 /** DSA public key.
331  *
332  * The import and export format is the
333  * representation of the public key `y = g^x mod p` as a big-endian byte
334  * string. The length of the byte string is the length of the base prime `p`
335  * in bytes.
336  */
337 #define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002)
338 
339 /** DSA key pair (private and public key).
340  *
341  * The import and export format is the
342  * representation of the private key `x` as a big-endian byte string. The
343  * length of the byte string is the private key size in bytes (leading zeroes
344  * are not stripped).
345  *
346  * Determinstic DSA key derivation with psa_generate_derived_key follows
347  * FIPS 186-4 §B.1.2: interpret the byte string as integer
348  * in big-endian order. Discard it if it is not in the range
349  * [0, *N* - 2] where *N* is the boundary of the private key domain
350  * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
351  * or the order of the curve's base point for ECC).
352  * Add 1 to the resulting integer and use this as the private key *x*.
353  *
354  */
355 #define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002)
356 
357 /** Whether a key type is an DSA key (pair or public-only). */
358 #define PSA_KEY_TYPE_IS_DSA(type) \
359  (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
360 
361 #define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
362 /** DSA signature with hashing.
363  *
364  * This is the signature scheme defined by FIPS 186-4,
365  * with a random per-message secret number (*k*).
366  *
367  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
368  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
369  * This includes #PSA_ALG_ANY_HASH
370  * when specifying the algorithm in a usage policy.
371  *
372  * \return The corresponding DSA signature algorithm.
373  * \return Unspecified if \p hash_alg is not a supported
374  * hash algorithm.
375  */
376 #define PSA_ALG_DSA(hash_alg) \
377  (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
378 #define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
379 #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
380 /** Deterministic DSA signature with hashing.
381  *
382  * This is the deterministic variant defined by RFC 6979 of
383  * the signature scheme defined by FIPS 186-4.
384  *
385  * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
386  * #PSA_ALG_IS_HASH(\p hash_alg) is true).
387  * This includes #PSA_ALG_ANY_HASH
388  * when specifying the algorithm in a usage policy.
389  *
390  * \return The corresponding DSA signature algorithm.
391  * \return Unspecified if \p hash_alg is not a supported
392  * hash algorithm.
393  */
394 #define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
395  (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
396 #define PSA_ALG_IS_DSA(alg) \
397  (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
398  PSA_ALG_DSA_BASE)
399 #define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
400  (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
401 #define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
402  (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
403 #define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
404  (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
405 
406 
407 /* We need to expand the sample definition of this macro from
408  * the API definition. */
409 #undef PSA_ALG_IS_HASH_AND_SIGN
410 #define PSA_ALG_IS_HASH_AND_SIGN(alg) \
411  (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
412  PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg))
413 
414 /**@}*/
415 
416 /** \addtogroup attributes
417  * @{
418  */
419 
420 /** Custom Diffie-Hellman group.
421  *
422  * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
423  * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
424  * from domain parameters set by psa_set_key_domain_parameters().
425  */
426 #define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
427 
428 
429 /**
430  * \brief Set domain parameters for a key.
431  *
432  * Some key types require additional domain parameters in addition to
433  * the key type identifier and the key size. Use this function instead
434  * of psa_set_key_type() when you need to specify domain parameters.
435  *
436  * The format for the required domain parameters varies based on the key type.
437  *
438  * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
439  * the domain parameter data consists of the public exponent,
440  * represented as a big-endian integer with no leading zeros.
441  * This information is used when generating an RSA key pair.
442  * When importing a key, the public exponent is read from the imported
443  * key data and the exponent recorded in the attribute structure is ignored.
444  * As an exception, the public exponent 65537 is represented by an empty
445  * byte string.
446  * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
447  * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
448  * ```
449  * Dss-Parms ::= SEQUENCE {
450  * p INTEGER,
451  * q INTEGER,
452  * g INTEGER
453  * }
454  * ```
455  * - For Diffie-Hellman key exchange keys
456  * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
457  * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
458  * `DomainParameters` format as defined by RFC 3279 §2.3.3.
459  * ```
460  * DomainParameters ::= SEQUENCE {
461  * p INTEGER, -- odd prime, p=jq +1
462  * g INTEGER, -- generator, g
463  * q INTEGER, -- factor of p-1
464  * j INTEGER OPTIONAL, -- subgroup factor
465  * validationParms ValidationParms OPTIONAL
466  * }
467  * ValidationParms ::= SEQUENCE {
468  * seed BIT STRING,
469  * pgenCounter INTEGER
470  * }
471  * ```
472  *
473  * \note This function may allocate memory or other resources.
474  * Once you have called this function on an attribute structure,
475  * you must call psa_reset_key_attributes() to free these resources.
476  *
477  * \note This is an experimental extension to the interface. It may change
478  * in future versions of the library.
479  *
480  * \param[in,out] attributes Attribute structure where the specified domain
481  * parameters will be stored.
482  * If this function fails, the content of
483  * \p attributes is not modified.
484  * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
485  * \param[in] data Buffer containing the key domain parameters.
486  * The content of this buffer is interpreted
487  * according to \p type as described above.
488  * \param data_length Size of the \p data buffer in bytes.
489  *
490  * \retval #PSA_SUCCESS
491  * \retval #PSA_ERROR_INVALID_ARGUMENT
492  * \retval #PSA_ERROR_NOT_SUPPORTED
493  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
494  */
496  psa_key_type_t type,
497  const uint8_t *data,
498  size_t data_length);
499 
500 /**
501  * \brief Get domain parameters for a key.
502  *
503  * Get the domain parameters for a key with this function, if any. The format
504  * of the domain parameters written to \p data is specified in the
505  * documentation for psa_set_key_domain_parameters().
506  *
507  * \note This is an experimental extension to the interface. It may change
508  * in future versions of the library.
509  *
510  * \param[in] attributes The key attribute structure to query.
511  * \param[out] data On success, the key domain parameters.
512  * \param data_size Size of the \p data buffer in bytes.
513  * The buffer is guaranteed to be large
514  * enough if its size in bytes is at least
515  * the value given by
516  * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
517  * \param[out] data_length On success, the number of bytes
518  * that make up the key domain parameters data.
519  *
520  * \retval #PSA_SUCCESS
521  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
522  */
524  const psa_key_attributes_t *attributes,
525  uint8_t *data,
526  size_t data_size,
527  size_t *data_length);
528 
529 /** Safe output buffer size for psa_get_key_domain_parameters().
530  *
531  * This macro returns a compile-time constant if its arguments are
532  * compile-time constants.
533  *
534  * \warning This function may call its arguments multiple times or
535  * zero times, so you should not pass arguments that contain
536  * side effects.
537  *
538  * \note This is an experimental extension to the interface. It may change
539  * in future versions of the library.
540  *
541  * \param key_type A supported key type.
542  * \param key_bits The size of the key in bits.
543  *
544  * \return If the parameters are valid and supported, return
545  * a buffer size in bytes that guarantees that
546  * psa_get_key_domain_parameters() will not fail with
547  * #PSA_ERROR_BUFFER_TOO_SMALL.
548  * If the parameters are a valid combination that is not supported
549  * by the implementation, this macro shall return either a
550  * sensible size or 0.
551  * If the parameters are not valid, the
552  * return value is unspecified.
553  */
554 #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
555  (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
556  PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
557  PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
558  0)
559 #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
560  (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
561 #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
562  (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
563 
564 /**@}*/
565 
566 /** \defgroup psa_tls_helpers TLS helper functions
567  * @{
568  */
569 
570 #if defined(MBEDTLS_ECP_C)
571 #include <mbedtls/ecp.h>
572 
573 /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
574  *
575  * \note This function is provided solely for the convenience of
576  * Mbed TLS and may be removed at any time without notice.
577  *
578  * \param grpid An Mbed TLS elliptic curve identifier
579  * (`MBEDTLS_ECP_DP_xxx`).
580  * \param[out] bits On success, the bit size of the curve.
581  *
582  * \return The corresponding PSA elliptic curve identifier
583  * (`PSA_ECC_FAMILY_xxx`).
584  * \return \c 0 on failure (\p grpid is not recognized).
585  */
586 static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
587  size_t *bits )
588 {
589  switch( grpid )
590  {
591  case MBEDTLS_ECP_DP_SECP192R1:
592  *bits = 192;
593  return( PSA_ECC_FAMILY_SECP_R1 );
594  case MBEDTLS_ECP_DP_SECP224R1:
595  *bits = 224;
596  return( PSA_ECC_FAMILY_SECP_R1 );
597  case MBEDTLS_ECP_DP_SECP256R1:
598  *bits = 256;
599  return( PSA_ECC_FAMILY_SECP_R1 );
600  case MBEDTLS_ECP_DP_SECP384R1:
601  *bits = 384;
602  return( PSA_ECC_FAMILY_SECP_R1 );
603  case MBEDTLS_ECP_DP_SECP521R1:
604  *bits = 521;
605  return( PSA_ECC_FAMILY_SECP_R1 );
606  case MBEDTLS_ECP_DP_BP256R1:
607  *bits = 256;
609  case MBEDTLS_ECP_DP_BP384R1:
610  *bits = 384;
612  case MBEDTLS_ECP_DP_BP512R1:
613  *bits = 512;
615  case MBEDTLS_ECP_DP_CURVE25519:
616  *bits = 255;
617  return( PSA_ECC_FAMILY_MONTGOMERY );
618  case MBEDTLS_ECP_DP_SECP192K1:
619  *bits = 192;
620  return( PSA_ECC_FAMILY_SECP_K1 );
621  case MBEDTLS_ECP_DP_SECP224K1:
622  *bits = 224;
623  return( PSA_ECC_FAMILY_SECP_K1 );
624  case MBEDTLS_ECP_DP_SECP256K1:
625  *bits = 256;
626  return( PSA_ECC_FAMILY_SECP_K1 );
627  case MBEDTLS_ECP_DP_CURVE448:
628  *bits = 448;
629  return( PSA_ECC_FAMILY_MONTGOMERY );
630  default:
631  *bits = 0;
632  return( 0 );
633  }
634 }
635 
636 /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
637  *
638  * \note This function is provided solely for the convenience of
639  * Mbed TLS and may be removed at any time without notice.
640  *
641  * \param curve A PSA elliptic curve identifier
642  * (`PSA_ECC_FAMILY_xxx`).
643  * \param byte_length The byte-length of a private key on \p curve.
644  *
645  * \return The corresponding Mbed TLS elliptic curve identifier
646  * (`MBEDTLS_ECP_DP_xxx`).
647  * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
648  * \return #MBEDTLS_ECP_DP_NONE if \p byte_length is not
649  * correct for \p curve.
650  */
651 mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
652  size_t byte_length );
653 #endif /* MBEDTLS_ECP_C */
654 
655 /**@}*/
656 
657 #ifdef __cplusplus
658 }
659 #endif
660 
661 #endif /* PSA_CRYPTO_EXTRA_H */
size_t empty_slots
Number of slots that are not used for anything.
psa_app_key_id_t max_open_internal_key_id
Largest key id value among open keys in internal persistent storage.
#define PSA_ECC_FAMILY_MONTGOMERY
Curve25519 and Curve448.
static void psa_set_key_enrollment_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg2)
Declare the enrollment algorithm for a key.
size_t cache_slots
Number of slots that contain cache data.
psa_app_key_id_t max_open_external_key_id
Largest key id value among open keys in secure elements.
size_t half_filled_slots
Number of slots which are occupied, but do not contain key material yet.
psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes, psa_key_type_t type, const uint8_t *data, size_t data_length)
Set domain parameters for a key.
#define PSA_ECC_FAMILY_BRAINPOOL_P_R1
Brainpool P random curves.
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 ...
static psa_algorithm_t psa_get_key_enrollment_algorithm(const psa_key_attributes_t *attributes)
Retrieve the enrollment algorithm policy from key attributes.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
size_t external_slots
Number of slots containing a reference to a key in a secure element.
#define PSA_ECC_FAMILY_SECP_R1
SEC random curves over prime fields.
size_t persistent_slots
Number of slots containing key material for a key which is in internal persistent storage...
uint16_t psa_key_type_t
Encoding of a key type.
size_t volatile_slots
Number of slots containing key material for a volatile key.
psa_status_t psa_get_key_domain_parameters(const psa_key_attributes_t *attributes, uint8_t *data, size_t data_size, size_t *data_length)
Get domain parameters for a key.
#define PSA_ECC_FAMILY_SECP_K1
SEC Koblitz curves over prime fields.
uint8_t psa_ecc_family_t
The type of PSA elliptic curve family identifiers.
int32_t psa_status_t
Function return status.
Statistics about resource consumption related to the PSA keystore.
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.