From: Nikos Mavrogiannopoulos Date: Sun, 26 Jan 2014 08:06:14 +0000 (+0100) Subject: Updated _gnutls_mpi_init prototype and added _gnutls_mpi_init_multi X-Git-Tag: gnutls_3_3_0pre0~278 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a02809a1a715e65fcd4a3e1577dfdbc5ab43ed66;p=thirdparty%2Fgnutls.git Updated _gnutls_mpi_init prototype and added _gnutls_mpi_init_multi --- diff --git a/lib/auth/srp.c b/lib/auth/srp.c index 702b94052c..3966b0b9b2 100644 --- a/lib/auth/srp.c +++ b/lib/auth/srp.c @@ -722,17 +722,10 @@ group_check_g_n(gnutls_session_t session, bigint_t g, bigint_t n) return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER; } - two = _gnutls_mpi_new(4); - if (two == NULL) { - gnutls_assert(); - return GNUTLS_E_MEMORY_ERROR; - } - - q = _gnutls_mpi_alloc_like(n); - if (q == NULL) { + ret = _gnutls_mpi_init_multi(&two, &q, &w, NULL); + if (ret < 0) { gnutls_assert(); - ret = GNUTLS_E_MEMORY_ERROR; - goto error; + return ret; } /* q = n-1 @@ -763,13 +756,6 @@ group_check_g_n(gnutls_session_t session, bigint_t g, bigint_t n) goto error; } - w = _gnutls_mpi_alloc_like(q); - if (w == NULL) { - gnutls_assert(); - ret = GNUTLS_E_MEMORY_ERROR; - goto error; - } - /* check if g^q mod N == N-1 * w = g^q mod N */ diff --git a/lib/crypto-backend.h b/lib/crypto-backend.h index a497f56146..25fdeb07e3 100644 --- a/lib/crypto-backend.h +++ b/lib/crypto-backend.h @@ -105,7 +105,8 @@ typedef enum { /* Multi precision integer arithmetic */ typedef struct gnutls_crypto_bigint { - bigint_t(*bigint_new) (int nbits); + int (*bigint_init) (bigint_t*); + int (*bigint_init_multi) (bigint_t*, ...); void (*bigint_release) (bigint_t n); void (*bigint_clear) (bigint_t n); /* zeros the int */ /* 0 for equality, > 0 for m1>m2, < 0 for m1 0) { return 0; } @@ -384,7 +474,8 @@ static int wrap_nettle_prime_check(bigint_t pp) int crypto_bigint_prio = INT_MAX; gnutls_crypto_bigint_st _gnutls_mpi_ops = { - .bigint_new = wrap_nettle_mpi_new, + .bigint_init = wrap_nettle_mpi_init, + .bigint_init_multi = wrap_nettle_mpi_init_multi, .bigint_cmp = wrap_nettle_mpi_cmp, .bigint_cmp_ui = wrap_nettle_mpi_cmp_ui, .bigint_modm = wrap_nettle_mpi_modm, diff --git a/lib/nettle/pk.c b/lib/nettle/pk.c index adf6353577..72407fa37b 100644 --- a/lib/nettle/pk.c +++ b/lib/nettle/pk.c @@ -215,10 +215,9 @@ static int _wrap_nettle_pk_derive(gnutls_pk_algorithm_t algo, goto dh_cleanup; } - k = _gnutls_mpi_alloc_like(prime); - if (k == NULL) { + ret = _gnutls_mpi_init(&k); + if (ret < 0) { gnutls_assert(); - ret = GNUTLS_E_MEMORY_ERROR; goto dh_cleanup; } @@ -825,15 +824,14 @@ wrap_nettle_pk_generate_params(gnutls_pk_algorithm_t algo, #endif params->params_nr = 0; - for (i = 0; i < DSA_PRIVATE_PARAMS-2; i++) { - params->params[i] = - _gnutls_mpi_alloc_like(&pub.p); - if (params->params[i] == NULL) { - ret = GNUTLS_E_MEMORY_ERROR; - goto dsa_fail; - } - params->params_nr++; + + ret = _gnutls_mpi_init_multi(¶ms->params[0], ¶ms->params[1], + ¶ms->params[2], NULL); + if (ret < 0) { + gnutls_assert(); + goto dsa_fail; } + params->params_nr = 3; ret = 0; _gnutls_mpi_set(params->params[0], pub.p); @@ -908,16 +906,9 @@ wrap_nettle_pk_generate_keys(gnutls_pk_algorithm_t algo, goto dsa_fail; } - params->params[DSA_Y] = _gnutls_mpi_alloc_like(&pub.p); - if (params->params[DSA_Y] == NULL) { - ret = GNUTLS_E_MEMORY_ERROR; - goto dsa_fail; - } - - params->params[DSA_X] = _gnutls_mpi_alloc_like(&pub.p); - if (params->params[DSA_X] == NULL) { - _gnutls_mpi_release(¶ms->params[DSA_Y]); - ret = GNUTLS_E_MEMORY_ERROR; + ret = _gnutls_mpi_init_multi(¶ms->params[DSA_Y], ¶ms->params[DSA_X], NULL); + if (ret < 0) { + gnutls_assert(); goto dsa_fail; } @@ -985,16 +976,9 @@ wrap_nettle_pk_generate_keys(gnutls_pk_algorithm_t algo, } } while(mpz_cmp_ui(y, 1) == 0); - params->params[DSA_Y] = _gnutls_mpi_alloc_like(&pub.p); - if (params->params[DSA_Y] == NULL) { - ret = GNUTLS_E_MEMORY_ERROR; - goto dh_fail; - } - - params->params[DSA_X] = _gnutls_mpi_alloc_like(&pub.p); - if (params->params[DSA_X] == NULL) { - _gnutls_mpi_release(¶ms->params[DSA_Y]); - ret = GNUTLS_E_MEMORY_ERROR; + ret = _gnutls_mpi_init_multi(¶ms->params[DSA_Y], ¶ms->params[DSA_X], NULL); + if (ret < 0) { + gnutls_assert(); goto dh_fail; } @@ -1036,14 +1020,12 @@ wrap_nettle_pk_generate_keys(gnutls_pk_algorithm_t algo, params->params_nr = 0; for (i = 0; i < RSA_PRIVATE_PARAMS; i++) { - params->params[i] = - _gnutls_mpi_alloc_like(&pub.n); - if (params->params[i] == NULL) { - ret = GNUTLS_E_MEMORY_ERROR; + ret = _gnutls_mpi_init(¶ms->params[i]); + if (ret < 0) { + gnutls_assert(); goto rsa_fail; } params->params_nr++; - } ret = 0; @@ -1083,20 +1065,10 @@ wrap_nettle_pk_generate_keys(gnutls_pk_algorithm_t algo, ecdsa_generate_keypair(&pub, &key, NULL, rnd_func); - params->params[ECC_X] = _gnutls_mpi_new(0); - params->params[ECC_Y] = _gnutls_mpi_new(0); - params->params[ECC_K] = _gnutls_mpi_new(0); - - if (params->params[ECC_X] == NULL - || params->params[ECC_Y] == NULL - || params->params[ECC_K] == NULL) { - _gnutls_mpi_release(¶ms-> - params[ECC_X]); - _gnutls_mpi_release(¶ms-> - params[ECC_Y]); - _gnutls_mpi_release(¶ms-> - params[ECC_K]); - ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); + ret = _gnutls_mpi_init_multi(¶ms->params[ECC_X], ¶ms->params[ECC_Y], + ¶ms->params[ECC_K], NULL); + if (ret < 0) { + gnutls_assert(); goto ecc_fail; } @@ -1153,11 +1125,10 @@ wrap_nettle_pk_verify_params(gnutls_pk_algorithm_t algo, gnutls_assert_val (GNUTLS_E_INVALID_REQUEST); - t1 = _gnutls_mpi_new(256); - if (t1 == NULL) + ret = _gnutls_mpi_init(&t1); + if (ret < 0) return - gnutls_assert_val - (GNUTLS_E_MEMORY_ERROR); + gnutls_assert_val(ret); _gnutls_mpi_mulm(t1, params->params[RSA_PRIME1], params->params[RSA_PRIME2], @@ -1236,11 +1207,10 @@ wrap_nettle_pk_verify_params(gnutls_pk_algorithm_t algo, gnutls_assert_val (GNUTLS_E_INVALID_REQUEST); - t1 = _gnutls_mpi_new(256); - if (t1 == NULL) + ret = _gnutls_mpi_init(&t1); + if (ret < 0) return - gnutls_assert_val - (GNUTLS_E_MEMORY_ERROR); + gnutls_assert_val(ret); _gnutls_mpi_powm(t1, params->params[DSA_G], params->params[DSA_X], @@ -1338,17 +1308,17 @@ wrap_nettle_pk_verify_params(gnutls_pk_algorithm_t algo, static int calc_rsa_exp(gnutls_pk_params_st * params) { - bigint_t tmp = _gnutls_mpi_alloc_like(params->params[0]); - + bigint_t tmp; + int ret; + if (params->params_nr < RSA_PRIVATE_PARAMS - 2) { gnutls_assert(); return GNUTLS_E_INTERNAL_ERROR; } - if (tmp == NULL) { - gnutls_assert(); - return GNUTLS_E_MEMORY_ERROR; - } + ret = _gnutls_mpi_init(&tmp); + if (ret < 0) + return gnutls_assert_val(ret); /* [6] = d % p-1, [7] = d % q-1 */ _gnutls_mpi_sub_ui(tmp, params->params[3], 1); diff --git a/lib/x509/privkey_pkcs8.c b/lib/x509/privkey_pkcs8.c index 9b91ba9ea8..accf4ae411 100644 --- a/lib/x509/privkey_pkcs8.c +++ b/lib/x509/privkey_pkcs8.c @@ -1081,9 +1081,8 @@ _decode_pkcs8_dsa_key(ASN1_TYPE pkcs8_asn, gnutls_x509_privkey_t pkey) } /* the public key can be generated as g^x mod p */ - pkey->params.params[3] = - _gnutls_mpi_alloc_like(pkey->params.params[0]); - if (pkey->params.params[3] == NULL) { + ret = _gnutls_mpi_init(&pkey->params.params[3]); + if (ret < 0) { gnutls_assert(); goto error; }