mbed/COMPONENT_AWSIOT_PKCS11PSA/corePKCS11/core_pki_utils.c
/*
 * Simultaneously convert and move raw MPI from the beginning of a buffer
 * to an ASN.1 MPI at the end of the buffer.
 * See also mbedtls_asn1_write_mpi().
 *
 * p: pointer to the end of the output buffer
 * start: start of the output buffer, and also of the mpi to write at the end
 * n_len: length of the mpi to read from start
 */
Simultaneously convert and move raw MPI from the beginning of a buffer
to an ASN.1 MPI at the end of the buffer.

pointer to the end of the output buffer
start: start of the output buffer, and also of the mpi to write at the end
n_len: length of the mpi to read from start
Simultaneously convert and move raw MPI from the beginning of a buffer to an ASN . 1 MPI at the end of the buffer . pointer to the end of the output buffer start : start of the output buffer and also of the mpi to write at the end n_len : length of the mpi to read from start
static int asn1_write_mpibuf( unsigned char **p, unsigned char *start,
                              size_t n_len )
{
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
    size_t len = 0;
    if( (size_t)( *p - start ) < n_len )
        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
    len = n_len;
    *p -= len;
    memmove( *p, start, len );
    while( len > 0 && **p == 0x00 )
    {
        ++(*p);
        --len;
    }
    if( len == 0 )
        return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
    if( **p & 0x80 )
    {
        if( *p - start < 1 )
            return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
        *--(*p) = 0x00;
        len += 1;
    }
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
                                                MBEDTLS_ASN1_INTEGER ) );
    return( (int) len );
}
static int asn1_write_mpibuf ( unsigned char * * p , unsigned char * start , size_t n_len ) { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED ; size_t len = 0 ; if ( ( size_t ) ( * p - start ) < n_len ) return ( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ) ; len = n_len ; * p -= len ; memmove ( * p , start , len ) ; while ( len > 0 && * * p == 0x00 ) { ++ ( * p ) ; -- len ; } if ( len == 0 ) return ( MBEDTLS_ERR_PK_HW_ACCEL_FAILED ) ; if ( * * p & 0x80 ) { if ( * p - start < 1 ) return ( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ) ; * -- ( * p ) = 0x00 ; len += 1 ; } MBEDTLS_ASN1_CHK_ADD ( len , mbedtls_asn1_write_len ( p , start , len ) ) ; MBEDTLS_ASN1_CHK_ADD ( len , mbedtls_asn1_write_tag ( p , start , MBEDTLS_ASN1_INTEGER ) ) ; return ( ( int ) len ) ; }
Simultaneously convert and move raw MPI from the beginning of a buffer
 to an ASN.1 MPI at the end of the buffer.
Simultaneously convert and move raw MPI from the beginning of a buffer to an ASN . 1 MPI at the end of the buffer .
/* ASN.1 DER encoding requires minimal length, so skip leading 0s.
     * Neither r nor s should be 0, but as a failsafe measure, still detect
     * that rather than overflowing the buffer in case of a PSA error. */ /* this is only reached if the signature was invalid */ /* if the msb is 1, ASN.1 requires that we prepend a 0.
     * Neither r nor s can be 0, so we can assume len > 0 at all times. */