diff options
Diffstat (limited to 'lib/mpi-libgcrypt.c')
-rw-r--r-- | lib/mpi-libgcrypt.c | 154 |
1 files changed, 89 insertions, 65 deletions
diff --git a/lib/mpi-libgcrypt.c b/lib/mpi-libgcrypt.c index 5e20ab7971..29e31b9cf2 100644 --- a/lib/mpi-libgcrypt.c +++ b/lib/mpi-libgcrypt.c @@ -36,22 +36,27 @@ /* Functions that refer to the libgcrypt library. */ -static inline int _format_conv( gnutls_bigint_format_t format) +static inline int +_format_conv (gnutls_bigint_format_t format) { - if (format == GNUTLS_MPI_FORMAT_USG) return GCRYMPI_FMT_USG; - else if (format == GNUTLS_MPI_FORMAT_STD) return GCRYMPI_FMT_STD; - else return GCRYMPI_FMT_PGP; + if (format == GNUTLS_MPI_FORMAT_USG) + return GCRYMPI_FMT_USG; + else if (format == GNUTLS_MPI_FORMAT_STD) + return GCRYMPI_FMT_STD; + else + return GCRYMPI_FMT_PGP; } /* returns zero on success */ bigint_t -wrap_gcry_mpi_scan (const void * buffer, size_t nbytes, gnutls_bigint_format_t format) +wrap_gcry_mpi_scan (const void *buffer, size_t nbytes, + gnutls_bigint_format_t format) { gcry_mpi_t ret_mpi = NULL; int ret; - ret = gcry_mpi_scan (&ret_mpi, _format_conv(format), buffer, nbytes, NULL); + ret = gcry_mpi_scan (&ret_mpi, _format_conv (format), buffer, nbytes, NULL); if (ret != 0) return NULL; @@ -59,16 +64,17 @@ wrap_gcry_mpi_scan (const void * buffer, size_t nbytes, gnutls_bigint_format_t f } int -wrap_gcry_mpi_print (const bigint_t a, void *buffer, size_t * nbytes, gnutls_bigint_format_t format) +wrap_gcry_mpi_print (const bigint_t a, void *buffer, size_t * nbytes, + gnutls_bigint_format_t format) { int ret; - format = _format_conv(format); - + format = _format_conv (format); + if (nbytes == NULL || a == NULL) return GNUTLS_E_INVALID_REQUEST; - ret = gcry_mpi_print( format, buffer, *nbytes, nbytes, a); + ret = gcry_mpi_print (format, buffer, *nbytes, nbytes, a); if (!ret) return 0; @@ -78,168 +84,186 @@ wrap_gcry_mpi_print (const bigint_t a, void *buffer, size_t * nbytes, gnutls_big #undef _gnutls_mpi_alloc_like #define _gnutls_mpi_alloc_like(x) gcry_mpi_new(gcry_mpi_get_nbits(x)) -bigint_t wrap_gcry_mpi_mod( const bigint_t a, const bigint_t b) +bigint_t +wrap_gcry_mpi_mod (const bigint_t a, const bigint_t b) { bigint_t r = _gnutls_mpi_alloc_like (b); - + if (r == NULL) return NULL; - - gcry_mpi_mod( r, a, b); + + gcry_mpi_mod (r, a, b); return r; } -bigint_t wrap_gcry_mpi_powm( bigint_t w, const bigint_t b, const bigint_t e, const bigint_t m) +bigint_t +wrap_gcry_mpi_powm (bigint_t w, const bigint_t b, const bigint_t e, + const bigint_t m) { if (w == NULL) w = _gnutls_mpi_alloc_like (m); - + if (w == NULL) return NULL; - - gcry_mpi_powm( w, b, e, m); + + gcry_mpi_powm (w, b, e, m); return w; } -bigint_t wrap_gcry_mpi_addm( bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m) +bigint_t +wrap_gcry_mpi_addm (bigint_t w, const bigint_t a, const bigint_t b, + const bigint_t m) { if (w == NULL) w = _gnutls_mpi_alloc_like (m); - + if (w == NULL) return NULL; - - gcry_mpi_addm( w, a, b, m); + + gcry_mpi_addm (w, a, b, m); return w; } -bigint_t wrap_gcry_mpi_subm( bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m) +bigint_t +wrap_gcry_mpi_subm (bigint_t w, const bigint_t a, const bigint_t b, + const bigint_t m) { if (w == NULL) w = _gnutls_mpi_alloc_like (m); - + if (w == NULL) return NULL; - - gcry_mpi_subm( w, a, b, m); + + gcry_mpi_subm (w, a, b, m); return w; } -bigint_t wrap_gcry_mpi_mulm( bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m) +bigint_t +wrap_gcry_mpi_mulm (bigint_t w, const bigint_t a, const bigint_t b, + const bigint_t m) { if (w == NULL) w = _gnutls_mpi_alloc_like (m); - + if (w == NULL) return NULL; - - gcry_mpi_mulm( w, a, b, m); + + gcry_mpi_mulm (w, a, b, m); return w; } -bigint_t wrap_gcry_mpi_add( bigint_t w, const bigint_t a, const bigint_t b) +bigint_t +wrap_gcry_mpi_add (bigint_t w, const bigint_t a, const bigint_t b) { if (w == NULL) w = _gnutls_mpi_alloc_like (b); - + if (w == NULL) return NULL; - - gcry_mpi_add( w, a, b); + + gcry_mpi_add (w, a, b); return w; } -bigint_t wrap_gcry_mpi_sub( bigint_t w, const bigint_t a, const bigint_t b) +bigint_t +wrap_gcry_mpi_sub (bigint_t w, const bigint_t a, const bigint_t b) { if (w == NULL) w = _gnutls_mpi_alloc_like (b); - + if (w == NULL) return NULL; - - gcry_mpi_sub( w, a, b); + + gcry_mpi_sub (w, a, b); return w; } -bigint_t wrap_gcry_mpi_mul( bigint_t w, const bigint_t a, const bigint_t b) +bigint_t +wrap_gcry_mpi_mul (bigint_t w, const bigint_t a, const bigint_t b) { if (w == NULL) w = _gnutls_mpi_alloc_like (b); - + if (w == NULL) return NULL; - - gcry_mpi_mul( w, a, b); + + gcry_mpi_mul (w, a, b); return w; } /* q = a / b */ -bigint_t wrap_gcry_mpi_div( bigint_t q, const bigint_t a, const bigint_t b) +bigint_t +wrap_gcry_mpi_div (bigint_t q, const bigint_t a, const bigint_t b) { if (q == NULL) q = _gnutls_mpi_alloc_like (a); - + if (q == NULL) return NULL; - - gcry_mpi_div( q, NULL, a, b, 0); + + gcry_mpi_div (q, NULL, a, b, 0); return q; } -bigint_t wrap_gcry_mpi_add_ui( bigint_t w, const bigint_t a, unsigned long b) +bigint_t +wrap_gcry_mpi_add_ui (bigint_t w, const bigint_t a, unsigned long b) { if (w == NULL) w = _gnutls_mpi_alloc_like (a); - + if (w == NULL) return NULL; - - gcry_mpi_add_ui( w, a, b); + + gcry_mpi_add_ui (w, a, b); return w; } -bigint_t wrap_gcry_mpi_sub_ui( bigint_t w, const bigint_t a, unsigned long b) +bigint_t +wrap_gcry_mpi_sub_ui (bigint_t w, const bigint_t a, unsigned long b) { if (w == NULL) w = _gnutls_mpi_alloc_like (a); - + if (w == NULL) return NULL; - - gcry_mpi_sub_ui( w, a, b); + + gcry_mpi_sub_ui (w, a, b); return w; } -bigint_t wrap_gcry_mpi_mul_ui( bigint_t w, const bigint_t a, unsigned long b) +bigint_t +wrap_gcry_mpi_mul_ui (bigint_t w, const bigint_t a, unsigned long b) { if (w == NULL) w = _gnutls_mpi_alloc_like (a); - + if (w == NULL) return NULL; - - gcry_mpi_mul_ui( w, a, b); + + gcry_mpi_mul_ui (w, a, b); return w; } -int wrap_gcry_prime_check( bigint_t pp) +int +wrap_gcry_prime_check (bigint_t pp) { - return gcry_prime_check( pp, 0); + return gcry_prime_check (pp, 0); } -int wrap_gcry_generate_group( gnutls_group_st *group, unsigned int bits) +int +wrap_gcry_generate_group (gnutls_group_st * group, unsigned int bits) { bigint_t g = NULL, prime = NULL; gcry_error_t err; @@ -253,7 +277,7 @@ int wrap_gcry_generate_group( gnutls_group_st *group, unsigned int bits) qbits = bits / 2; else { - qbits = (bits/40) + 105; + qbits = (bits / 40) + 105; } if (qbits & 1) /* better have an even number */ @@ -270,7 +294,7 @@ int wrap_gcry_generate_group( gnutls_group_st *group, unsigned int bits) gcry_prime_release_factors (factors); } - err = gcry_prime_generate ((gcry_mpi_t*)&prime, bits, qbits, + err = gcry_prime_generate ((gcry_mpi_t *) & prime, bits, qbits, &factors, NULL, NULL, GCRY_STRONG_RANDOM, GCRY_PRIME_FLAG_SPECIAL_FACTOR); @@ -296,7 +320,7 @@ int wrap_gcry_generate_group( gnutls_group_st *group, unsigned int bits) /* generate the group generator. */ - err = gcry_prime_group_generator ((gcry_mpi_t*)&g, prime, factors, NULL); + err = gcry_prime_group_generator ((gcry_mpi_t *) & g, prime, factors, NULL); if (err != 0) { gnutls_assert (); @@ -326,7 +350,7 @@ int crypto_bigint_prio = INT_MAX; gnutls_crypto_bigint_st _gnutls_mpi_ops = { .bigint_new = gcry_mpi_new, .bigint_cmp = gcry_mpi_cmp, - .bigint_cmp_ui = gcry_mpi_cmp_ui, + .bigint_cmp_ui = gcry_mpi_cmp_ui, .bigint_mod = wrap_gcry_mpi_mod, .bigint_set = gcry_mpi_set, .bigint_set_ui = gcry_mpi_set_ui, |