programs_dh_client/server: Updated programs to use mbedtls_rsa_get_len()

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This commit is contained in:
Minos Galanakis 2024-01-12 14:34:14 +00:00
parent f4dfd1c8a5
commit b6a96195fb
2 changed files with 13 additions and 10 deletions

View File

@ -66,6 +66,7 @@ int main(void)
unsigned char *p, *end; unsigned char *p, *end;
unsigned char buf[2048]; unsigned char buf[2048];
unsigned char hash[MBEDTLS_MD_CAN_SHA256_MAX_SIZE]; unsigned char hash[MBEDTLS_MD_CAN_SHA256_MAX_SIZE];
mbedtls_mpi N, E;
const char *pers = "dh_client"; const char *pers = "dh_client";
mbedtls_entropy_context entropy; mbedtls_entropy_context entropy;
@ -78,6 +79,8 @@ int main(void)
mbedtls_dhm_init(&dhm); mbedtls_dhm_init(&dhm);
mbedtls_aes_init(&aes); mbedtls_aes_init(&aes);
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_mpi_init(&N);
mbedtls_mpi_init(&E);
/* /*
* 1. Setup the RNG * 1. Setup the RNG
@ -106,16 +109,13 @@ int main(void)
} }
mbedtls_rsa_init(&rsa); mbedtls_rsa_init(&rsa);
if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
if ((ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(N), 16, f)) != 0 || (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
(ret = mbedtls_mpi_read_file(&rsa.MBEDTLS_PRIVATE(E), 16, f)) != 0) { (ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E) != 0)) {
mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret); mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
fclose(f); fclose(f);
goto exit; goto exit;
} }
rsa.MBEDTLS_PRIVATE(len) = (mbedtls_mpi_bitlen(&rsa.MBEDTLS_PRIVATE(N)) + 7) >> 3;
fclose(f); fclose(f);
/* /*
@ -182,7 +182,7 @@ int main(void)
p += 2; p += 2;
if ((n = (size_t) (end - p)) != rsa.MBEDTLS_PRIVATE(len)) { if ((n = (size_t) (end - p)) != mbedtls_rsa_get_len(&rsa)) {
mbedtls_printf(" failed\n ! Invalid RSA signature size\n\n"); mbedtls_printf(" failed\n ! Invalid RSA signature size\n\n");
goto exit; goto exit;
} }
@ -273,6 +273,8 @@ exit:
mbedtls_dhm_free(&dhm); mbedtls_dhm_free(&dhm);
mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy); mbedtls_entropy_free(&entropy);
mbedtls_mpi_free(&N);
mbedtls_mpi_free(&E);
mbedtls_exit(exit_code); mbedtls_exit(exit_code);
} }

View File

@ -190,8 +190,9 @@ int main(void)
goto exit; goto exit;
} }
buf[n] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len) >> 8); const size_t rsa_key_len = mbedtls_rsa_get_len(&rsa);
buf[n + 1] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len)); buf[n] = (unsigned char) (rsa_key_len >> 8);
buf[n + 1] = (unsigned char) (rsa_key_len);
if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg,
MBEDTLS_MD_SHA256, MBEDTLS_MD_CAN_SHA256_MAX_SIZE, MBEDTLS_MD_SHA256, MBEDTLS_MD_CAN_SHA256_MAX_SIZE,
@ -200,7 +201,7 @@ int main(void)
goto exit; goto exit;
} }
buflen = n + 2 + rsa.MBEDTLS_PRIVATE(len); buflen = n + 2 + rsa_key_len;
buf2[0] = (unsigned char) (buflen >> 8); buf2[0] = (unsigned char) (buflen >> 8);
buf2[1] = (unsigned char) (buflen); buf2[1] = (unsigned char) (buflen);