From 71386e2530fa351d22f942ce3cf9d18eee99ce78 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 28 Aug 2018 20:18:02 +0200 Subject: [PATCH] gcrypt: Let read_bignum() return bits, not bytes. Callers need this to check if the size of the public key is acceptable. Since sizes of cryptographic keys are generally measured in bits, its natural to return bits here. --- gcrypt.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/gcrypt.c b/gcrypt.c index 9b05a949..abc3272a 100644 --- a/gcrypt.c +++ b/gcrypt.c @@ -205,11 +205,11 @@ static inline int get_long_form_num_length_bytes(unsigned char c) /* * Returns: Number of bytes scanned. This may differ from the value returned via - * bn_bytes because the latter does not include the ASN.1 prefix and a leading - * zero is not considered as an additional byte for bn_bytes. + * bitsp because the latter does not include the ASN.1 prefix and a leading + * zero is not considered as an additional byte for the number of bits. */ static int read_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn, - int *bn_bytes) + unsigned *bitsp) { int i, bn_size; gcry_error_t gret; @@ -247,8 +247,8 @@ static int read_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn, cp++; bn_size--; } - if (bn_bytes) - *bn_bytes = bn_size; + if (bitsp) + *bitsp = bn_size * 8; cp += bn_size; // unsigned char *buf; // gcry_mpi_aprint(GCRYMPI_FMT_HEX, &buf, NULL, *bn); @@ -293,7 +293,8 @@ static int get_private_key(const char *key_file, struct asymmetric_key **result) gcry_mpi_t n = NULL, e = NULL, d = NULL, p = NULL, q = NULL, u = NULL; unsigned char *blob, *cp, *end; - int ret, n_size; + int ret; + unsigned bits; gcry_error_t gret; size_t erroff, blob_size; gcry_sexp_t sexp; @@ -310,7 +311,7 @@ static int get_private_key(const char *key_file, struct asymmetric_key **result) PARA_INFO_LOG("reading RSA params at offset %d\n", ret); cp = blob + ret; - ret = read_bignum(cp, end, &n, &n_size); + ret = read_bignum(cp, end, &n, &bits); if (ret < 0) goto free_blob; cp += ret; @@ -357,7 +358,7 @@ static int get_private_key(const char *key_file, struct asymmetric_key **result) key = para_malloc(sizeof(*key)); key->sexp = sexp; *result = key; - ret = n_size * 8; + ret = bits; PARA_INFO_LOG("succesfully read %d bit private key\n", ret); release_u: gcry_mpi_release(u); -- 2.39.2