Arm Mbed OS support forum

Mbedtls_pk_decrypt returns -0x4080 RSA - Bad input parameters to function

Hi,

Here is our code snippet

static uint8_t privkey[] =
"-----BEGIN RSA PRIVATE KEY-----\n"
"MIICXQIBAAKBgQDBLQdgAyX5GRCXUb95ihK0KrNJGCql/JKeAS/qIDBLS8EAVExj\n"
"PXi5RvidtuOcDS+mqyG+pdCVFM9UKEsor8gegrcw3MzNGBiicNJ2DCTwd2U34UZr\n"
"dUaeRBXYJsdKDrRNKYUvZNJXAi6Q1kLsSOi4fiTgoXzHPBPjVGZjP1HnuwIDAQAB\n"
"AoGAFJO8VmDfeaJO3x+JGuQ3S9PEl0Oov1ehMo2D0hetrUebfnJ8VJHmuy2xK/Og\n"
"WS4aZlGSXaGCD29RCnDkWKgo08+0WNChN59C260Ky3XfL5Ayhg39LYro7sDWhFbB\n"
"VqUHvOQRdHGfWyun+uHQpVLupfluG3uwgF5Pvh7Iue1XIQkCQQDta10TMIWJLNhw\n"
"isU/B9JO4etRES7tq17GcYZCsD5d3Sxywoz+tAm5T5UujD9ObAdjlJgCnFrUIvth\n"
"1w21r+F3AkEA0EtCGDtZU38ZQC4mMnw58NfaY0cJ/nCJ55MKl7dbQNVMEOvoN7NE\n"
"ViPw/cwySvsZnPZqXYeRTcpivT72Q5bc3QJAGILTEx4ZSzY0Cjppbr5x+e7dCnuJ\n"
"qsPcfwRNZikn5yzkX9yyDcGsl1MeBbY4+vGwOZmnr+1Qe0I5TbqUEcPctQJBAJet\n"
"Jc0Vcp+NnuqQcHo04dBSrpG6ijmA47sA5SbDeyzD5acw0iG1Zd7AP13hEwJsxSL6\n"
"gUvoS1P1O1+JeLSRJ/UCQQDQfj2nNEBpBiC4+vFonS6Tfenenl6pNbO6Q2m/JFOi\n"
"QI1+WfU65gYoc24YvoNKrKCYiEhd/CmiOkY5G48sBAKH\n"
"-----END RSA PRIVATE KEY-----";

static uint8_t ct[] = 
"\x31\x37\x77\x61\x35\x79\x39\x57\x45\x62\x50\x72\x46\x75\x4f\x74\x33\x38\x37\x67\x4b\x41\x4c\x58\x64\x51\x4a\x7a\x6b\x43\x32\x79\x45\x77\x36\x72\x4b\x33\x6a\x70\x74\x69\x46\x4b\x46\x4a\x70\x6e\x4e\x32\x37\x38\x49\x36\x36\x75\x66\x57\x4d\x4b\x33\x53\x43\x31\x6a\x2f\x45\x55\x41\x37\x43\x76\x64\x44\x72\x32\x49\x62\x6b\x64\x6c\x4e\x6b\x7a\x49\x39\x34\x61\x50\x6e\x75\x7a\x4a\x55\x68\x38\x4b\x48\x66\x69\x76\x55\x6f\x31\x73\x4b\x42\x66\x33\x56\x69\x78\x52\x47\x79\x72\x74\x72\x45\x46\x39\x4d\x55\x50\x6c\x6f\x49\x43\x37\x51\x51\x32\x2b\x30\x6e\x52\x68\x74\x37\x77\x71\x63\x4d\x71\x38\x44\x51\x70\x33\x75\x53\x67\x6d\x62\x64\x57\x48\x57\x62\x53\x4f\x45\x61\x7a\x58\x72\x54\x49\x6f\x46\x4d\x3d";

uint8_t result[1024*80];

void decrypt_data(){
    char *personalization = "decrypt";
    mbedtls_entropy_context entropy;
    mbedtls_entropy_init( &entropy );
    mbedtls_ctr_drbg_context ctr_drbg;
    mbedtls_ctr_drbg_init( &ctr_drbg );
    int ret;

    ret = mbedtls_ctr_drbg_seed( &ctr_drbg , mbedtls_entropy_func, &entropy,
                     (const unsigned char *) personalization,
                     strlen( personalization ) );

    mbedtls_pk_context pk;
    mbedtls_pk_init( &pk );

    if ((ret=mbedtls_pk_parse_key(&pk, privkey, strlen((const char *)privkey) + 1, NULL, 0)) != 0)
    {
        printf( "parse key failed\n  ! mbedtls_pk_decrypt returned -0x%04x \n", -ret );
        return -1;
    }

    size_t olen = 0;


    if( ( ret = mbedtls_pk_decrypt( &pk, ct, sizeof(ct)-1, result, &olen, sizeof(result),
                    mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
    {
        char buffer[100];
        mbedtls_strerror(ret,buffer, 100) ;
        printf( "decrypt failed\n  ! mbedtls_pk_decrypt returned -0x%04x %s\n", -ret,buffer );

    }
    else
    {
        printf("result: %s\n", result);
    }
    mbedtls_pk_free( &pk );
    mbedtls_entropy_free( &entropy );
    mbedtls_ctr_drbg_free( &ctr_drbg );
}

mbedtls_pk_decrypt always returns -0x4080 RSA - Bad input parameters to function.
Have even tried to encrypt first using mbedtls_pk_encrypt and feed the output directly
to the mbedtls_pk_decrypt method, still it throws the same error.

Using RSA 1024 keypair.

[Private key shared is testing only]

Would appreciate any help.
Thanks in advance!