diff options
-rw-r--r-- | lib/crypto-backend.h | 12 | ||||
-rw-r--r-- | lib/gnutls_srp.c | 57 | ||||
-rw-r--r-- | lib/nettle/mpi.c | 74 | ||||
-rw-r--r-- | lib/x509/pkcs12_encr.c | 8 | ||||
-rw-r--r-- | tests/mpi.c | 6 |
5 files changed, 75 insertions, 82 deletions
diff --git a/lib/crypto-backend.h b/lib/crypto-backend.h index 42f83635e1..f7267c827c 100644 --- a/lib/crypto-backend.h +++ b/lib/crypto-backend.h @@ -125,22 +125,22 @@ typedef struct gnutls_crypto_bigint { int (*bigint_powm) (bigint_t w, const bigint_t b, const bigint_t e, const bigint_t m); /* w = a + b mod m */ - bigint_t(*bigint_addm) (bigint_t w, const bigint_t a, + int (*bigint_addm) (bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m); /* w = a - b mod m */ - bigint_t(*bigint_subm) (bigint_t w, const bigint_t a, + int (*bigint_subm) (bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m); /* w = a * b mod m */ - bigint_t(*bigint_mulm) (bigint_t w, const bigint_t a, + int (*bigint_mulm) (bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m); - /* w = a + b */ bigint_t(*bigint_add) (bigint_t w, + /* w = a + b */ int (*bigint_add) (bigint_t w, const bigint_t a, const bigint_t b); - /* w = a - b */ bigint_t(*bigint_sub) (bigint_t w, + /* w = a - b */ int (*bigint_sub) (bigint_t w, const bigint_t a, const bigint_t b); /* w = a * b */ - bigint_t(*bigint_mul) (bigint_t w, const bigint_t a, + int (*bigint_mul) (bigint_t w, const bigint_t a, const bigint_t b); /* w = a + b */ int (*bigint_add_ui) (bigint_t w, const bigint_t a, diff --git a/lib/gnutls_srp.c b/lib/gnutls_srp.c index db01c74bb9..1ee7473703 100644 --- a/lib/gnutls_srp.c +++ b/lib/gnutls_srp.c @@ -115,13 +115,23 @@ _gnutls_calc_srp_B(bigint_t * ret_b, bigint_t g, bigint_t n, bigint_t v) goto error; } - _gnutls_mpi_mulm(tmpV, k, v, n); - + ret = _gnutls_mpi_mulm(tmpV, k, v, n); + if (ret < 0) { + gnutls_assert(); + goto error; + } + ret = _gnutls_mpi_powm(tmpB, g, b, n); - if (ret < 0) + if (ret < 0) { + gnutls_assert(); goto error; + } - _gnutls_mpi_addm(B, tmpV, tmpB, n); + ret = _gnutls_mpi_addm(B, tmpV, tmpB, n); + if (ret < 0) { + gnutls_assert(); + goto error; + } _gnutls_mpi_release(&k); _gnutls_mpi_release(&tmpB); @@ -212,10 +222,17 @@ _gnutls_calc_srp_S1(bigint_t A, bigint_t b, bigint_t u, bigint_t v, return NULL; ret = _gnutls_mpi_powm(tmp1, v, u, n); - if (ret < 0) + if (ret < 0) { + gnutls_assert(); goto error; + } + + ret = _gnutls_mpi_mulm(tmp2, A, tmp1, n); + if (ret < 0) { + gnutls_assert(); + goto error; + } - _gnutls_mpi_mulm(tmp2, A, tmp1, n); _gnutls_mpi_powm(S, tmp2, b, n); _gnutls_mpi_release(&tmp1); @@ -338,12 +355,30 @@ _gnutls_calc_srp_S2(bigint_t B, bigint_t g, bigint_t x, bigint_t a, goto freeall; } - _gnutls_mpi_mulm(tmp3, tmp1, k, n); /* k*g^x mod n */ - _gnutls_mpi_subm(tmp2, B, tmp3, n); + ret = _gnutls_mpi_mulm(tmp3, tmp1, k, n); /* k*g^x mod n */ + if (ret < 0) { + gnutls_assert(); + goto freeall; + } + + ret = _gnutls_mpi_subm(tmp2, B, tmp3, n); + if (ret < 0) { + gnutls_assert(); + goto freeall; + } + + ret = _gnutls_mpi_mul(tmp1, u, x); + if (ret < 0) { + gnutls_assert(); + goto freeall; + } + + ret = _gnutls_mpi_add(tmp4, a, tmp1); + if (ret < 0) { + gnutls_assert(); + goto freeall; + } - _gnutls_mpi_mul(tmp1, u, x); - _gnutls_mpi_add(tmp4, a, tmp1); - ret = _gnutls_mpi_powm(S, tmp2, tmp4, n); if (ret < 0) { gnutls_assert(); diff --git a/lib/nettle/mpi.c b/lib/nettle/mpi.c index f8bb6dc135..54ec77bcf7 100644 --- a/lib/nettle/mpi.c +++ b/lib/nettle/mpi.c @@ -251,106 +251,58 @@ wrap_nettle_mpi_powm(bigint_t w, const bigint_t b, const bigint_t e, return 0; } -static bigint_t +static int wrap_nettle_mpi_addm(bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m) { -int ret; - - if (w == NULL) { - ret = wrap_nettle_mpi_init(&w); - if (ret < 0) - return NULL; - } - mpz_add(TOMPZ(w), TOMPZ(b), TOMPZ(a)); mpz_fdiv_r(TOMPZ(w), TOMPZ(w), TOMPZ(m)); - return w; + return 0; } -static bigint_t +static int wrap_nettle_mpi_subm(bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m) { -int ret; - - if (w == NULL) { - ret = wrap_nettle_mpi_init(&w); - if (ret < 0) - return NULL; - } - mpz_sub(TOMPZ(w), TOMPZ(a), TOMPZ(b)); mpz_fdiv_r(TOMPZ(w), TOMPZ(w), TOMPZ(m)); - return w; + return 0; } -static bigint_t +static int wrap_nettle_mpi_mulm(bigint_t w, const bigint_t a, const bigint_t b, const bigint_t m) { -int ret; - - if (w == NULL) { - ret = wrap_nettle_mpi_init(&w); - if (ret < 0) - return NULL; - } - mpz_mul(TOMPZ(w), TOMPZ(a), TOMPZ(b)); mpz_fdiv_r(TOMPZ(w), TOMPZ(w), TOMPZ(m)); - return w; + return 0; } -static bigint_t +static int wrap_nettle_mpi_add(bigint_t w, const bigint_t a, const bigint_t b) { -int ret; - - if (w == NULL) { - ret = wrap_nettle_mpi_init(&w); - if (ret < 0) - return NULL; - } - mpz_add(TOMPZ(w), TOMPZ(a), TOMPZ(b)); - return w; + return 0; } -static bigint_t +static int wrap_nettle_mpi_sub(bigint_t w, const bigint_t a, const bigint_t b) { -int ret; - - if (w == NULL) { - ret = wrap_nettle_mpi_init(&w); - if (ret < 0) - return NULL; - } - mpz_sub(TOMPZ(w), TOMPZ(a), TOMPZ(b)); - return w; + return 0; } -static bigint_t +static int wrap_nettle_mpi_mul(bigint_t w, const bigint_t a, const bigint_t b) { -int ret; - - if (w == NULL) { - ret = wrap_nettle_mpi_init(&w); - if (ret < 0) - return NULL; - } - mpz_mul(TOMPZ(w), TOMPZ(a), TOMPZ(b)); - return w; + return 0; } /* q = a / b */ @@ -370,7 +322,7 @@ wrap_nettle_mpi_add_ui(bigint_t w, const bigint_t a, unsigned long b) return 0; } -static bigint_t +static int wrap_nettle_mpi_sub_ui(bigint_t w, const bigint_t a, unsigned long b) { mpz_sub_ui(TOMPZ(w), TOMPZ(a), b); diff --git a/lib/x509/pkcs12_encr.c b/lib/x509/pkcs12_encr.c index 950f98405c..bac1840bff 100644 --- a/lib/x509/pkcs12_encr.c +++ b/lib/x509/pkcs12_encr.c @@ -171,7 +171,13 @@ _gnutls_pkcs12_string_to_key(unsigned int id, const uint8_t * salt, gnutls_assert(); goto cleanup; } - _gnutls_mpi_addm(num_ij, num_ij, num_b1, mpi512); + + rc = _gnutls_mpi_addm(num_ij, num_ij, num_b1, mpi512); + if (rc < 0) { + gnutls_assert(); + goto cleanup; + } + n = 64; #ifndef PKCS12_BROKEN_KEYGEN m = (_gnutls_mpi_get_nbits(num_ij) + 7) / 8; diff --git a/tests/mpi.c b/tests/mpi.c index 58aa527815..f0cf9aba8a 100644 --- a/tests/mpi.c +++ b/tests/mpi.c @@ -48,7 +48,7 @@ void doit(void) if (debug) gnutls_global_set_log_level(99); - ret = _gnutls_mpi_init_multi(&n1, &n2, &n3, NULL); + ret = _gnutls_mpi_init_multi(&n1, &n2, &n3, &n4, NULL); if (ret < 0) fail("mpi_new failed\n"); @@ -64,8 +64,8 @@ void doit(void) if (ret < 0) fail("mpi_set_ui failed\n"); - n4 = _gnutls_mpi_addm(NULL, n1, n3, n2); - if (n4 == 0) + ret = _gnutls_mpi_addm(n4, n1, n3, n2); + if (ret < 0) fail("mpi_set_ui failed\n"); if (_gnutls_mpi_cmp_ui(n4, 0) != 0 |