mbed/COMPONENT_AWSIOT_PKCS11PSA/corePKCS11/core_pki_utils.c
/* Convert the EC signature from DER encoded to PKCS #11 format. */
/* @[declare pkcs11_utils_pkipkcs11signaturetombedtlssignature] */
Convert the EC signature from DER encoded to PKCS #11 format.
Convert the EC signature from DER encoded to PKCS #11 format .
int8_t PKI_mbedTLSSignatureToPkcs11Signature( uint8_t * pxPKCSSignature,
                                              size_t *pxPKCSSignatureLength,
                                              const uint8_t * pxMbedSignature,
                                              size_t xMbedSignatureLength )
{
    int8_t xReturn = 0;
    const uint8_t * pxNextLength = NULL;
    size_t xRSComponentLength = 0;
    if( ( pxPKCSSignature == NULL ) ||
        ( pxPKCSSignatureLength == NULL ) ||
        ( pxMbedSignature == NULL ) )
    {
        xReturn = FAILURE;
    }
    if (xReturn == 0) {
        mbedtls_mpi r, s;
        mbedtls_mpi_init( &r );
        mbedtls_mpi_init( &s );
        unsigned char *p = (unsigned char *) pxMbedSignature;
        const unsigned char *end = pxMbedSignature + xMbedSignatureLength;
        size_t len;
        size_t r_bitlen;
        size_t s_bitlen;
        if ( 0 != mbedtls_asn1_get_tag( &p, end, &len,
                                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) {
            xReturn = FAILURE;
            goto cleanup_rs;
        }
        if ( ( p + len ) != end) {
            xReturn = FAILURE;
            goto cleanup_rs;
        }
        if ( 0 != mbedtls_asn1_get_mpi( &p, end, &r ) ) {
            xReturn = FAILURE;
            goto cleanup_rs;
        }
        if ( 0 != mbedtls_asn1_get_mpi( &p, end, &s ) ) {
            xReturn = FAILURE;
            goto cleanup_rs;
        }
        r_bitlen = mbedtls_mpi_bitlen( &r );
        s_bitlen = mbedtls_mpi_bitlen( &s );
        xRSComponentLength = ( r_bitlen + 7 ) / 8;
cleanup_rs:
        mbedtls_mpi_free( &r );
        mbedtls_mpi_free( &s );
    }
    if( xReturn == 0 ) {
        if ((xRSComponentLength * 2) > *pxPKCSSignatureLength) {
            xReturn = FAILURE;
        }
    }
    if( xReturn == 0 )
    {
        int rc_mbedtls = 0;
        unsigned char *p = (unsigned char *) pxMbedSignature;
        rc_mbedtls = extract_ecdsa_sig(&p,
                                       (const unsigned char *) (pxMbedSignature + xMbedSignatureLength),
                                       (unsigned char *) pxPKCSSignature,
                                       xRSComponentLength);
        if (0 != rc_mbedtls) {
            printf("extract_ecdsa_sig failed: %d\r\n", rc_mbedtls);
            xReturn = FAILURE;
        }
        *pxPKCSSignatureLength = xRSComponentLength * 2;
    }
    return xReturn;
}
int8_t PKI_mbedTLSSignatureToPkcs11Signature ( uint8_t * pxPKCSSignature , size_t * pxPKCSSignatureLength , const uint8_t * pxMbedSignature , size_t xMbedSignatureLength ) { int8_t xReturn = 0 ; const uint8_t * pxNextLength = NULL ; size_t xRSComponentLength = 0 ; if ( ( pxPKCSSignature == NULL ) || ( pxPKCSSignatureLength == NULL ) || ( pxMbedSignature == NULL ) ) { xReturn = FAILURE ; } if ( xReturn == 0 ) { mbedtls_mpi r , s ; mbedtls_mpi_init ( & r ) ; mbedtls_mpi_init ( & s ) ; unsigned char * p = ( unsigned char * ) pxMbedSignature ; const unsigned char * end = pxMbedSignature + xMbedSignatureLength ; size_t len ; size_t r_bitlen ; size_t s_bitlen ; if ( 0 != mbedtls_asn1_get_tag ( & p , end , & len , MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) { xReturn = FAILURE ; goto cleanup_rs ; } if ( ( p + len ) != end ) { xReturn = FAILURE ; goto cleanup_rs ; } if ( 0 != mbedtls_asn1_get_mpi ( & p , end , & r ) ) { xReturn = FAILURE ; goto cleanup_rs ; } if ( 0 != mbedtls_asn1_get_mpi ( & p , end , & s ) ) { xReturn = FAILURE ; goto cleanup_rs ; } r_bitlen = mbedtls_mpi_bitlen ( & r ) ; s_bitlen = mbedtls_mpi_bitlen ( & s ) ; xRSComponentLength = ( r_bitlen + 7 ) / 8 ; cleanup_rs : mbedtls_mpi_free ( & r ) ; mbedtls_mpi_free ( & s ) ; } if ( xReturn == 0 ) { if ( ( xRSComponentLength * 2 ) > * pxPKCSSignatureLength ) { xReturn = FAILURE ; } } if ( xReturn == 0 ) { int rc_mbedtls = 0 ; unsigned char * p = ( unsigned char * ) pxMbedSignature ; rc_mbedtls = extract_ecdsa_sig ( & p , ( const unsigned char * ) ( pxMbedSignature + xMbedSignatureLength ) , ( unsigned char * ) pxPKCSSignature , xRSComponentLength ) ; if ( 0 != rc_mbedtls ) { printf ( " \r \n " , rc_mbedtls ) ; xReturn = FAILURE ; } * pxPKCSSignatureLength = xRSComponentLength * 2 ; } return xReturn ; }
Convert the EC signature from DER encoded to PKCS #11 format.
Convert the EC signature from DER encoded to PKCS #11 format .
/* Abstracting from mbedtls ecdsa.c, fetch {r, s} component length. */