psa_util.h
mbedtls/config.h
mbedtls/ctr_drbg.h
mbedtls/aes.h
mbedtls/check_config.h
mbedtls/config.h
mbedtls/ctr_drbg.h
mbedtls/platform_time.h
mbedtls/platform_util.h
psa_util.h
mbedtls/threading.h
limits.h
pthread.h
stddef.h
stdint.h
stdlib.h
time.h
MBEDTLS_PSA_RANDOM_STATE
mbedtls_psa_random_state
int typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size) (void *p_rng, unsigned char *output, size_t output_size) mbedtls_f_rng_t
mbedtls_ctr_drbg_context typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t mbedtls_psa_drbg_context_t
mbedtls_f_rng_t *const mbedtls_f_rng_t* const mbedtls_psa_get_random mbedtls_psa_get_random = mbedtls_ctr_drbg_random
mbedtls_psa_drbg_context_t *const mbedtls_psa_drbg_context_t* const mbedtls_psa_random_state mbedtls_psa_random_state

Utility functions for the use of the PSA Crypto library.

This function is not part of the public API and may change at any time.

    /*
    *CopyrightTheMbedTLSContributors
    *SPDX-License-Identifier:Apache-2.0ORGPL-2.0-or-later
    */

    #ifndefMBEDTLS_PSA_UTIL_H
    #defineMBEDTLS_PSA_UTIL_H

    #if!defined(MBEDTLS_CONFIG_FILE)
    #include"mbedtls/config.h"
    #else
    #includeMBEDTLS_CONFIG_FILE
    #endif

    #ifdefined(MBEDTLS_USE_PSA_CRYPTO)

    #include"psa/crypto.h"

    #include"mbedtls/ecp.h"
    #include"mbedtls/md.h"
    #include"mbedtls/pk.h"
    #include"mbedtls/oid.h"

    #include<string.h>

    /*Translationsforsymmetriccrypto.*/

    staticinlinepsa_key_type_tmbedtls_psa_translate_cipher_type(
    mbedtls_cipher_type_tcipher)
    {
    switch(cipher){
    caseMBEDTLS_CIPHER_AES_128_CCM:
    caseMBEDTLS_CIPHER_AES_192_CCM:
    caseMBEDTLS_CIPHER_AES_256_CCM:
    caseMBEDTLS_CIPHER_AES_128_GCM:
    caseMBEDTLS_CIPHER_AES_192_GCM:
    caseMBEDTLS_CIPHER_AES_256_GCM:
    caseMBEDTLS_CIPHER_AES_128_CBC:
    caseMBEDTLS_CIPHER_AES_192_CBC:
    caseMBEDTLS_CIPHER_AES_256_CBC:
    caseMBEDTLS_CIPHER_AES_128_ECB:
    caseMBEDTLS_CIPHER_AES_192_ECB:
    caseMBEDTLS_CIPHER_AES_256_ECB:
    returnPSA_KEY_TYPE_AES;

    /*ARIAnotyetsupportedinPSA.*/
    /*caseMBEDTLS_CIPHER_ARIA_128_CCM:
    caseMBEDTLS_CIPHER_ARIA_192_CCM:
    caseMBEDTLS_CIPHER_ARIA_256_CCM:
    caseMBEDTLS_CIPHER_ARIA_128_GCM:
    caseMBEDTLS_CIPHER_ARIA_192_GCM:
    caseMBEDTLS_CIPHER_ARIA_256_GCM:
    caseMBEDTLS_CIPHER_ARIA_128_CBC:
    caseMBEDTLS_CIPHER_ARIA_192_CBC:
    caseMBEDTLS_CIPHER_ARIA_256_CBC:
    return(PSA_KEY_TYPE_ARIA);*/

    default:
    return0;
    }
    }

    staticinlinepsa_algorithm_tmbedtls_psa_translate_cipher_mode(
    mbedtls_cipher_mode_tmode,size_ttaglen)
    {
    switch(mode){
    caseMBEDTLS_MODE_ECB:
    returnPSA_ALG_ECB_NO_PADDING;
    caseMBEDTLS_MODE_GCM:
    returnPSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM,taglen);
    caseMBEDTLS_MODE_CCM:
    returnPSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM,taglen);
    caseMBEDTLS_MODE_CBC:
    if(taglen==0){
    returnPSA_ALG_CBC_NO_PADDING;
    }else{
    return0;
    }
    default:
    return0;
    }
    }

    staticinlinepsa_key_usage_tmbedtls_psa_translate_cipher_operation(
    mbedtls_operation_top)
    {
    switch(op){
    caseMBEDTLS_ENCRYPT:
    returnPSA_KEY_USAGE_ENCRYPT;
    caseMBEDTLS_DECRYPT:
    returnPSA_KEY_USAGE_DECRYPT;
    default:
    return0;
    }
    }

    /*Translationsforhashing.*/

    staticinlinepsa_algorithm_tmbedtls_psa_translate_md(mbedtls_md_type_tmd_alg)
    {
    switch(md_alg){
    #ifdefined(MBEDTLS_MD2_C)
    caseMBEDTLS_MD_MD2:
    returnPSA_ALG_MD2;
    #endif
    #ifdefined(MBEDTLS_MD4_C)
    caseMBEDTLS_MD_MD4:
    returnPSA_ALG_MD4;
    #endif
    #ifdefined(MBEDTLS_MD5_C)
    caseMBEDTLS_MD_MD5:
    returnPSA_ALG_MD5;
    #endif
    #ifdefined(MBEDTLS_SHA1_C)
    caseMBEDTLS_MD_SHA1:
    returnPSA_ALG_SHA_1;
    #endif
    #ifdefined(MBEDTLS_SHA256_C)
    caseMBEDTLS_MD_SHA224:
    returnPSA_ALG_SHA_224;
    caseMBEDTLS_MD_SHA256:
    returnPSA_ALG_SHA_256;
    #endif
    #ifdefined(MBEDTLS_SHA512_C)
    caseMBEDTLS_MD_SHA384:
    returnPSA_ALG_SHA_384;
    caseMBEDTLS_MD_SHA512:
    returnPSA_ALG_SHA_512;
    #endif
    #ifdefined(MBEDTLS_RIPEMD160_C)
    caseMBEDTLS_MD_RIPEMD160:
    returnPSA_ALG_RIPEMD160;
    #endif
    caseMBEDTLS_MD_NONE:
    return0;
    default:
    return0;
    }
    }

    /*TranslationsforECC.*/

    staticinlineintmbedtls_psa_get_ecc_oid_from_id(
    psa_ecc_family_tcurve,size_tbits,
    charconst**oid,size_t*oid_len)
    {
    switch(curve){
    casePSA_ECC_FAMILY_SECP_R1:
    switch(bits){
    #ifdefined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
    case192:
    *oid=MBEDTLS_OID_EC_GRP_SECP192R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP192R1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
    case224:
    *oid=MBEDTLS_OID_EC_GRP_SECP224R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP224R1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
    case256:
    *oid=MBEDTLS_OID_EC_GRP_SECP256R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP256R1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
    case384:
    *oid=MBEDTLS_OID_EC_GRP_SECP384R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP384R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP384R1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
    case521:
    *oid=MBEDTLS_OID_EC_GRP_SECP521R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP521R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP521R1_ENABLED*/
    }
    break;
    casePSA_ECC_FAMILY_SECP_K1:
    switch(bits){
    #ifdefined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
    case192:
    *oid=MBEDTLS_OID_EC_GRP_SECP192K1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192K1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP192K1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
    case224:
    *oid=MBEDTLS_OID_EC_GRP_SECP224K1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224K1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP224K1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
    case256:
    *oid=MBEDTLS_OID_EC_GRP_SECP256K1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256K1);
    return0;
    #endif/*MBEDTLS_ECP_DP_SECP256K1_ENABLED*/
    }
    break;
    casePSA_ECC_FAMILY_BRAINPOOL_P_R1:
    switch(bits){
    #ifdefined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
    case256:
    *oid=MBEDTLS_OID_EC_GRP_BP256R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP256R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_BP256R1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
    case384:
    *oid=MBEDTLS_OID_EC_GRP_BP384R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP384R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_BP384R1_ENABLED*/
    #ifdefined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
    case512:
    *oid=MBEDTLS_OID_EC_GRP_BP512R1;
    *oid_len=MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP512R1);
    return0;
    #endif/*MBEDTLS_ECP_DP_BP512R1_ENABLED*/
    }
    break;
    }
    (void)oid;
    (void)oid_len;
    return-1;
    }

    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH1

    #ifdefined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((192+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((192+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP192R1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((224+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((224+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP224R1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((256+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((256+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP256R1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((384+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((384+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP384R1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((521+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((521+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP521R1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((192+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((192+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP192K1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((224+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((224+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP224K1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((256+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((256+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_SECP256K1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((256+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((256+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_BP256R1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((384+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((384+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_BP384R1_ENABLED*/

    #ifdefined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
    #ifMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH<(2*((512+7)/8)+1)
    #undefMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
    #defineMBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH(2*((512+7)/8)+1)
    #endif
    #endif/*MBEDTLS_ECP_DP_BP512R1_ENABLED*/

    /*TranslationsforPKlayer*/

    staticinlineintmbedtls_psa_err_translate_pk(psa_status_tstatus)
    {
    switch(status){
    casePSA_SUCCESS:
    return0;
    casePSA_ERROR_NOT_SUPPORTED:
    returnMBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
    casePSA_ERROR_INSUFFICIENT_MEMORY:
    returnMBEDTLS_ERR_PK_ALLOC_FAILED;
    casePSA_ERROR_INSUFFICIENT_ENTROPY:
    returnMBEDTLS_ERR_ECP_RANDOM_FAILED;
    casePSA_ERROR_BAD_STATE:
    returnMBEDTLS_ERR_PK_BAD_INPUT_DATA;
    /*Allotherfailures*/
    casePSA_ERROR_COMMUNICATION_FAILURE:
    casePSA_ERROR_HARDWARE_FAILURE:
    casePSA_ERROR_CORRUPTION_DETECTED:
    returnMBEDTLS_ERR_PK_HW_ACCEL_FAILED;
    default:/*Wereturnthesameasforthe'otherfailures',
    *butlistthemseparatelynonethelesstoindicate
    *whichfailureconditionswehaveconsidered.*/
    returnMBEDTLS_ERR_PK_HW_ACCEL_FAILED;
    }
    }

    /*TranslationsforECC*/

    /*ThisfunctiontransformsanECCgroupidentifierfrom
    *https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
    *intoaPSAECCgroupidentifier.*/
    #ifdefined(MBEDTLS_ECP_C)
    staticinlinepsa_key_type_tmbedtls_psa_parse_tls_ecc_group(
    uint16_ttls_ecc_grp_reg_id,size_t*bits)
    {
    constmbedtls_ecp_curve_info*curve_info=
    mbedtls_ecp_curve_info_from_tls_id(tls_ecc_grp_reg_id);
    if(curve_info==NULL){
    return0;
    }
    returnPSA_KEY_TYPE_ECC_KEY_PAIR(
    mbedtls_ecc_group_to_psa(curve_info->grp_id,bits));
    }
    #endif/*MBEDTLS_ECP_C*/

    /*ThisfunctiontakesabufferholdinganECpublickey
    *exportedthroughpsa_export_public_key(),andconverts
    *itintoanECPointstructuretobeputintoaClientKeyExchange
    *messageinanECDHEexchange.
    *
    *BoththepresentandtheforeseeablefutureformatofECpublickeys
    *usedbyPSAhavetheECPointstructurecontainedintheexportedkey
    *asasubbuffer,andthefunctionmerelyselectsthissubbufferinstead
    *ofmakingacopy.
    */
    staticinlineintmbedtls_psa_tls_psa_ec_to_ecpoint(unsignedchar*src,
    size_tsrclen,
    unsignedchar**dst,
    size_t*dstlen)
    {
    *dst=src;
    *dstlen=srclen;
    return0;
    }

    /*ThisfunctiontakesabufferholdinganECPointstructure
    *(ascontainedinaTLSServerKeyExchangemessageforECDHE
    *exchanges)andconvertsitintoaformatthatthePSAkey
    *agreementAPIunderstands.
    */
    staticinlineintmbedtls_psa_tls_ecpoint_to_psa_ec(unsignedcharconst*src,
    size_tsrclen,
    unsignedchar*dst,
    size_tdstlen,
    size_t*olen)
    {
    if(srclen>dstlen){
    returnMBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
    }

    memcpy(dst,src,srclen);
    *olen=srclen;
    return0;
    }

    #endif/*MBEDTLS_USE_PSA_CRYPTO*/

    /*ExposewhateverRNGthePSAsubsystemusestoapplicationsusingthe
    *mbedtls_xxxAPI.Thedeclarationsanddefinitionshereneedtobe
    *consistentwiththeimplementationinlibrary/psa_crypto_random_impl.h.
    *Seethatfileforimplementationdocumentation.*/
    #ifdefined(MBEDTLS_PSA_CRYPTO_C)

    /*Thetypeofa`f_rng`randomgeneratorfunctionthatmanylibraryfunctions
    *take.
    *
    *ThistypenameisnotpartoftheMbedTLSstableAPI.Itmayberenamed
    *ormovedwithoutwarning.
    */
    typedefintmbedtls_f_rng_t(void*p_rng,unsignedchar*output,size_toutput_size);

    #ifdefined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)

    intmbedtls_psa_get_random(void*p_rng,
    unsignedchar*output,
    size_toutput_size);

    #defineMBEDTLS_PSA_RANDOM_STATENULL

    #else/*!defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)*/

    #ifdefined(MBEDTLS_CTR_DRBG_C)
    #include"mbedtls/ctr_drbg.h"
    typedefmbedtls_ctr_drbg_contextmbedtls_psa_drbg_context_t;
    staticmbedtls_f_rng_t*constmbedtls_psa_get_random=mbedtls_ctr_drbg_random;
    #elifdefined(MBEDTLS_HMAC_DRBG_C)
    #include"mbedtls/hmac_drbg.h"
    typedefmbedtls_hmac_drbg_contextmbedtls_psa_drbg_context_t;
    staticmbedtls_f_rng_t*constmbedtls_psa_get_random=mbedtls_hmac_drbg_random;
    #endif
    externmbedtls_psa_drbg_context_t*constmbedtls_psa_random_state;

    #defineMBEDTLS_PSA_RANDOM_STATEmbedtls_psa_random_state

    #endif/*!defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)*/

    #endif/*MBEDTLS_PSA_CRYPTO_C*/

    #endif/*MBEDTLS_PSA_UTIL_H*/