]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
updated prototype of _gnutls_mpi_powm
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Sun, 26 Jan 2014 09:35:01 +0000 (10:35 +0100)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Sun, 26 Jan 2014 09:35:01 +0000 (10:35 +0100)
lib/auth/srp.c
lib/crypto-backend.h
lib/gnutls_srp.c
lib/nettle/mpi.c
lib/nettle/pk.c
lib/x509/privkey_pkcs8.c

index e5a5b1f2dacefc3edae3c08b2b548fb7fc26b4b7..090776072164679f9a4996515496977ce38bbdff 100644 (file)
@@ -769,7 +769,11 @@ group_check_g_n(gnutls_session_t session, bigint_t g, bigint_t n)
        /* check if g^q mod N == N-1
         * w = g^q mod N
         */
-       _gnutls_mpi_powm(w, g, q, n);
+       ret = _gnutls_mpi_powm(w, g, q, n);
+       if (ret < 0) {
+               gnutls_assert();
+               goto error;
+       }
 
        /* w++
         */
index 0b9964c9042d64e6c55c04dde0b13323f68460c6..7b06e15eab4a66818278e690f25f33e3fe3cce81 100644 (file)
@@ -122,7 +122,7 @@ typedef struct gnutls_crypto_bigint {
        int (*bigint_set_ui) (bigint_t a, unsigned long b);
        unsigned int (*bigint_get_nbits) (const bigint_t a);
        /* w = b ^ e mod m */
-        bigint_t(*bigint_powm) (bigint_t w, const bigint_t b,
+       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,
index 85185eec5358eff366c9560f339ccdce80701ae8..db01c74bb9146e087b45cec58ba00322848de9ea 100644 (file)
@@ -44,7 +44,7 @@ static int
 _gnutls_srp_gx(uint8_t * text, size_t textsize, uint8_t ** result,
               bigint_t g, bigint_t prime)
 {
-       bigint_t x, e;
+       bigint_t x, e = NULL;
        size_t result_size;
        int ret;
 
@@ -55,26 +55,34 @@ _gnutls_srp_gx(uint8_t * text, size_t textsize, uint8_t ** result,
 
        ret = _gnutls_mpi_init(&e);
        if (ret < 0)
-               return gnutls_assert_val(ret);
+               goto cleanup;
 
        /* e = g^x mod prime (n) */
-       _gnutls_mpi_powm(e, g, x, prime);
-       _gnutls_mpi_release(&x);
+       ret = _gnutls_mpi_powm(e, g, x, prime);
+       if (ret < 0)
+               goto cleanup;
 
        ret = _gnutls_mpi_print(e, NULL, &result_size);
        if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
                *result = gnutls_malloc(result_size);
-               if ((*result) == NULL)
-                       return GNUTLS_E_MEMORY_ERROR;
+               if ((*result) == NULL) {
+                       ret = GNUTLS_E_MEMORY_ERROR;
+                       goto cleanup;
+               }
+
+               ret = _gnutls_mpi_print(e, *result, &result_size);
+               if (ret < 0)
+                       goto cleanup;
 
-               _gnutls_mpi_print(e, *result, &result_size);
                ret = result_size;
        } else {
                gnutls_assert();
                ret = GNUTLS_E_MPI_PRINT_FAILED;
        }
 
+cleanup:
        _gnutls_mpi_release(&e);
+       _gnutls_mpi_release(&x);
 
        return ret;
 
@@ -108,7 +116,10 @@ _gnutls_calc_srp_B(bigint_t * ret_b, bigint_t g, bigint_t n, bigint_t v)
        }
 
        _gnutls_mpi_mulm(tmpV, k, v, n);
-       _gnutls_mpi_powm(tmpB, g, b, n);
+       
+       ret = _gnutls_mpi_powm(tmpB, g, b, n);
+       if (ret < 0)
+               goto error;
 
        _gnutls_mpi_addm(B, tmpV, tmpB, n);
 
@@ -200,7 +211,10 @@ _gnutls_calc_srp_S1(bigint_t A, bigint_t b, bigint_t u, bigint_t v,
        if (ret < 0)
                return NULL;
 
-       _gnutls_mpi_powm(tmp1, v, u, n);
+       ret = _gnutls_mpi_powm(tmp1, v, u, n);
+       if (ret < 0)
+               goto error;
+
        _gnutls_mpi_mulm(tmp2, A, tmp1, n);
        _gnutls_mpi_powm(S, tmp2, b, n);
 
@@ -208,6 +222,12 @@ _gnutls_calc_srp_S1(bigint_t A, bigint_t b, bigint_t u, bigint_t v,
        _gnutls_mpi_release(&tmp2);
 
        return S;
+
+error:
+       _gnutls_mpi_release(&S);
+       _gnutls_mpi_release(&tmp1);
+       _gnutls_mpi_release(&tmp2);
+       return NULL;
 }
 
 /* A = g^a % N 
@@ -227,7 +247,9 @@ bigint_t _gnutls_calc_srp_A(bigint_t * a, bigint_t g, bigint_t n)
 
        _gnutls_mpi_random_modp(tmpa, n, GNUTLS_RND_RANDOM);
 
-       _gnutls_mpi_powm(A, g, tmpa, n);
+       ret = _gnutls_mpi_powm(A, g, tmpa, n);
+       if (ret < 0)
+               goto error;
 
        if (a != NULL)
                *a = tmpa;
@@ -235,6 +257,10 @@ bigint_t _gnutls_calc_srp_A(bigint_t * a, bigint_t g, bigint_t n)
                _gnutls_mpi_release(&tmpa);
 
        return A;
+error:
+       _gnutls_mpi_release(&tmpa);
+       _gnutls_mpi_release(&A);
+       return NULL;
 }
 
 /* generate x = SHA(s | SHA(U | ":" | p))
@@ -306,13 +332,23 @@ _gnutls_calc_srp_S2(bigint_t B, bigint_t g, bigint_t x, bigint_t a,
                goto freeall;
        }
 
-       _gnutls_mpi_powm(tmp1, g, x, n);        /* g^x */
+       ret = _gnutls_mpi_powm(tmp1, g, x, n);  /* g^x */
+       if (ret < 0) {
+               gnutls_assert();
+               goto freeall;
+       }
+
        _gnutls_mpi_mulm(tmp3, tmp1, k, n);     /* k*g^x mod n */
        _gnutls_mpi_subm(tmp2, B, tmp3, n);
 
        _gnutls_mpi_mul(tmp1, u, x);
        _gnutls_mpi_add(tmp4, a, tmp1);
-       _gnutls_mpi_powm(S, tmp2, tmp4, n);
+       
+       ret = _gnutls_mpi_powm(S, tmp2, tmp4, n);
+       if (ret < 0) {
+               gnutls_assert();
+               goto freeall;
+       }
 
        _gnutls_mpi_release(&tmp1);
        _gnutls_mpi_release(&tmp2);
index db2207ffd61f3bbae07dcf3e5f9b6c38cb3f407f..f36d7273697e0cc1a0f6aa2418af3ecfc2a1d434 100644 (file)
@@ -242,21 +242,13 @@ static int wrap_nettle_mpi_modm(bigint_t r, const bigint_t a, const bigint_t b)
        return 0;
 }
 
-static bigint_t
+static int
 wrap_nettle_mpi_powm(bigint_t w, const bigint_t b, const bigint_t e,
                     const bigint_t m)
 {
-int ret;
-
-       if (w == NULL) {
-               ret = wrap_nettle_mpi_init(&w);
-               if (ret < 0)
-                       return NULL;
-       }
-
        mpz_powm(TOMPZ(w), TOMPZ(b), TOMPZ(e), TOMPZ(m));
 
-       return w;
+       return 0;
 }
 
 static bigint_t
index 7b92fd8589e99cdd08ca8ac87e83a66b2ce0b573..bbfd85ea6dfccfc3809ba7f819981a5281e8e966 100644 (file)
@@ -225,7 +225,11 @@ static int _wrap_nettle_pk_derive(gnutls_pk_algorithm_t algo,
                }
 
 
-               _gnutls_mpi_powm(k, f, x, prime);
+               ret = _gnutls_mpi_powm(k, f, x, prime);
+               if (ret < 0) {
+                       gnutls_assert();
+                       goto dh_cleanup;
+               }
 
                ret = _gnutls_mpi_dprint(k, out);
                if (ret < 0) {
@@ -1218,9 +1222,13 @@ wrap_nettle_pk_verify_params(gnutls_pk_algorithm_t algo,
                                return
                                    gnutls_assert_val(ret);
 
-                       _gnutls_mpi_powm(t1, params->params[DSA_G],
+                       ret = _gnutls_mpi_powm(t1, params->params[DSA_G],
                                         params->params[DSA_X],
                                         params->params[DSA_P]);
+                       if (ret < 0) {
+                               gnutls_assert();
+                               goto dsa_cleanup;
+                       }
 
                        if (_gnutls_mpi_cmp(t1, params->params[DSA_Y]) !=
                            0) {
index accf4ae411e5df09b5e09d9e36e836ad26b7a49d..271fc3554aad7716cdf69f3dc20f7a8f643bba7d 100644 (file)
@@ -1087,8 +1087,12 @@ _decode_pkcs8_dsa_key(ASN1_TYPE pkcs8_asn, gnutls_x509_privkey_t pkey)
                goto error;
        }
 
-       _gnutls_mpi_powm(pkey->params.params[3], pkey->params.params[2],
+       ret = _gnutls_mpi_powm(pkey->params.params[3], pkey->params.params[2],
                         pkey->params.params[4], pkey->params.params[0]);
+       if (ret < 0) {
+               gnutls_assert();
+               goto error;
+       }
 
        ret =
            _gnutls_asn1_encode_privkey(GNUTLS_PK_DSA, &pkey->key,