Arm Mbed OS support forum

ECC signatures with ARM PSA

Hi I want to compute a simple ecc signature by using mbedtls. My code is given below. It compiles and runs. However, the signature is incorrect and by each execution it is different. What I am doing wrong?

void signature_key_import_sign(void)
{
	// uint8_t ecc_pk[] = { 0x04, 0xD7, 0x82, 0xC0, 0xBC, 0x51, 0x45, 0x08,
	// 		     0x49, 0x9E, 0xA1, 0xE4, 0x13, 0x22, 0xAC, 0x3E,
	// 		     0x3C, 0xED, 0xB5, 0x7E, 0xC8, 0x37, 0xE8, 0x2C,
	// 		     0x6A, 0x64, 0xA6, 0x45, 0xC2, 0x6E, 0x5D, 0x23,
	// 		     0x6D, 0xF8, 0x97, 0x1C, 0x97, 0xAD, 0xA5, 0xF9,
	// 		     0x0A, 0x19, 0x21, 0x3A, 0xA7, 0xAE, 0xA8, 0x3E,
	// 		     0x7D, 0xE9, 0x60, 0xFE, 0x91, 0x94, 0x2F, 0x8A,
	// 		     0x50, 0x69, 0xBA, 0x5B, 0xCC, 0x36, 0x17, 0x49,
	// 		     0x80 };
	uint8_t ecc_sk[] = { 0x8C, 0x14, 0x19, 0x4A, 0xEA, 0x42, 0x37, 0xF9,
			     0xAF, 0xB2, 0x54, 0x95, 0x7D, 0xF4, 0x99, 0xB5,
			     0x7F, 0x3D, 0xDD, 0x2A, 0x14, 0xC4, 0xB3, 0x8A,
			     0xD4, 0xB9, 0xD7, 0x78, 0xEA, 0x99, 0x17, 0x71 };

	uint8_t msg[] = {
		0x84, 0x6A, 0x53, 0x69, 0x67, 0x6E, 0x61, 0x74, 0x75, 0x72,
		0x65, 0x31, 0x43, 0xA1, 0x04, 0x0E, 0x58, 0x8A, 0x58, 0x20,
		0x4E, 0x81, 0x66, 0x2A, 0x88, 0x7B, 0xF6, 0xB1, 0x23, 0x61,
		0x81, 0x84, 0x18, 0xAE, 0xA3, 0xCD, 0x2C, 0x92, 0xD7, 0x3C,
		0x7F, 0x7A, 0x50, 0xAA, 0xAA, 0x94, 0x61, 0x82, 0x82, 0xFF,
		0x73, 0x11, 0xA2, 0x02, 0x77, 0x34, 0x32, 0x2D, 0x35, 0x30,
		0x2D, 0x33, 0x31, 0x2D, 0x46, 0x46, 0x2D, 0x45, 0x46, 0x2D,
		0x33, 0x37, 0x2D, 0x33, 0x32, 0x2D, 0x33, 0x39, 0x08, 0xA1,
		0x01, 0xA4, 0x01, 0x01, 0x02, 0x0E, 0x20, 0x01, 0x21, 0x58,
		0x41, 0x04, 0xD7, 0x82, 0xC0, 0xBC, 0x51, 0x45, 0x08, 0x49,
		0x9E, 0xA1, 0xE4, 0x13, 0x22, 0xAC, 0x3E, 0x3C, 0xED, 0xB5,
		0x7E, 0xC8, 0x37, 0xE8, 0x2C, 0x6A, 0x64, 0xA6, 0x45, 0xC2,
		0x6E, 0x5D, 0x23, 0x6D, 0xF8, 0x97, 0x1C, 0x97, 0xAD, 0xA5,
		0xF9, 0x0A, 0x19, 0x21, 0x3A, 0xA7, 0xAE, 0xA8, 0x3E, 0x7D,
		0xE9, 0x60, 0xFE, 0x91, 0x94, 0x2F, 0x8A, 0x50, 0x69, 0xBA,
		0x5B, 0xCC, 0x36, 0x17, 0x49, 0x80, 0x58, 0x20, 0xF4, 0x6C,
		0xAA, 0x49, 0x1E, 0x65, 0x40, 0xA4, 0x7F, 0xEC, 0x7D, 0x62,
		0x8D, 0xBC, 0xC2, 0x5C, 0x17, 0x1B, 0xAB, 0xBA, 0x34, 0x63,
		0x16, 0x7E, 0x4E, 0xAD, 0x79, 0x0E, 0x06, 0xFC, 0x34, 0xD4
	};

	uint8_t signature_buf[64];
	size_t signature_len;

	psa_status_t ret;
	psa_key_id_t key_id;
	psa_key_attributes_t key_attr;

	ret = psa_crypto_init();
	
	key_attr = psa_key_attributes_init();
	psa_set_key_type(&key_attr,
			 PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
	psa_set_key_usage_flags(
		&key_attr,
		PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE |
			PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH);
	psa_set_key_algorithm(&key_attr, PSA_ALG_ECDSA(PSA_ALG_SHA_256));

	ret = psa_import_key(&key_attr, ecc_sk, sizeof(ecc_sk), &key_id);

	ret = psa_sign_message(key_id, PSA_ALG_ECDSA(PSA_ALG_SHA_256), msg,
			       sizeof(msg), signature_buf,
			       sizeof(signature_buf), &signature_len);
	if (ret != 0) {
		printf("error during signing %d\n", ret);
	}

	PRINT_ARRAY("psa_sign_message with ECC", signature_buf, signature_len);
}