From: Nikos Mavrogiannopoulos Date: Tue, 7 Nov 2000 20:41:25 +0000 (+0000) Subject: minor fixes and cleanups X-Git-Tag: gnutls0-0-4~24 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=76722a500b7d38b06fda851ec6d7456f38263a8a;p=thirdparty%2Fgnutls.git minor fixes and cleanups --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 4e4e92a1c5..7b13218908 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -9,4 +9,4 @@ libgnutls_la_SOURCES = gnutls.c gnutls_compress.c debug.c gnutls_plaintext.c \ gnutls_cipher.c gnutls_buffers.c gnutls_handshake.c gnutls_num.c \ gnutls_errors.c gnutls_algorithms.c gnutls_dh.c gnutls_kx.c \ gnutls_priority.c gnutls_hash_int.c gnutls_cipher_int.c gnutls_der.c -libgnutls_la_LDFLAGS = -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) -ldmalloc +libgnutls_la_LDFLAGS = -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) diff --git a/lib/gnutls.c b/lib/gnutls.c index 775039bab9..075cc64ce5 100644 --- a/lib/gnutls.c +++ b/lib/gnutls.c @@ -133,15 +133,12 @@ int gnutls_deinit(GNUTLS_STATE * state) void *_gnutls_cal_PRF_A(MACAlgorithm algorithm, void *secret, int secret_size, void *seed, int seed_size) { GNUTLS_MAC_HANDLE td1; - void *A; td1 = gnutls_hmac_init(algorithm, secret, secret_size); gnutls_hmac(td1, seed, seed_size); - A = gnutls_hmac_deinit(td1); - - return A; + return gnutls_hmac_deinit(td1); } @@ -154,7 +151,7 @@ svoid *gnutls_P_hash(MACAlgorithm algorithm, opaque * secret, int secret_size, o GNUTLS_MAC_HANDLE td2; opaque *ret; void *A, *Atmp; - int i = 0, times, copy_bytes = 0, how, blocksize, A_size; + int i = 0, times, how, blocksize, A_size; void *final; ret = secure_calloc(1, total_bytes); @@ -171,6 +168,7 @@ svoid *gnutls_P_hash(MACAlgorithm algorithm, opaque * secret, int secret_size, o for (i = 0; i < times; i++) { td2 = gnutls_hmac_init(algorithm, secret, secret_size); + /* here we calculate A[i+1] */ Atmp = _gnutls_cal_PRF_A(algorithm, secret, secret_size, A, A_size); gnutls_free(A); A = Atmp; @@ -179,15 +177,14 @@ svoid *gnutls_P_hash(MACAlgorithm algorithm, opaque * secret, int secret_size, o gnutls_hmac(td2, seed, seed_size); final = gnutls_hmac_deinit(td2); - copy_bytes = blocksize; - if ((i + 1) * copy_bytes < total_bytes) { + if ( (1+i) * blocksize < total_bytes) { how = blocksize; } else { - how = total_bytes - (i) * copy_bytes; + how = total_bytes - (i) * blocksize; } if (how > 0) { - memmove(&ret[i * copy_bytes], final, how); + memmove(&ret[i * blocksize], final, how); } gnutls_free(final); } @@ -230,7 +227,7 @@ svoid *gnutls_PRF(opaque * secret, int secret_size, uint8 * label, int label_siz ret = secure_calloc(1, total_bytes); gnutls_free(s_seed); for (i = 0; i < total_bytes; i++) { - ret[i] = o1[i]; //^ o2[i]; + ret[i] = o1[i] ^ o2[i]; } secure_free(o1); diff --git a/lib/gnutls_cipher.c b/lib/gnutls_cipher.c index bf41af06fa..7eb55f01a4 100644 --- a/lib/gnutls_cipher.c +++ b/lib/gnutls_cipher.c @@ -447,7 +447,6 @@ int _gnutls_TLSCiphertext2TLSCompressed(GNUTLS_STATE state, ciphertext->version.minor; break; case CIPHER_BLOCK: - if ( (ciphertext->length < blocksize) || (ciphertext->length % blocksize != 0) ) { gnutls_assert(); return GNUTLS_E_DECRYPTION_FAILED; diff --git a/lib/gnutls_hash_int.c b/lib/gnutls_hash_int.c index ca835fcc29..c68ac2a201 100644 --- a/lib/gnutls_hash_int.c +++ b/lib/gnutls_hash_int.c @@ -20,7 +20,136 @@ #include #include +#include +#ifdef USE_MHASH +/* This file handles all the internal functions that cope with hashes + * and hmacs. Currently it uses the functions provided by + * the gcrypt library that this can be easily changed. + */ + +MHASH gnutls_hash_init(MACAlgorithm algorithm) { + +MHASH ret; + + switch (algorithm) { + case GNUTLS_MAC_NULL: + ret = GNUTLS_HASH_FAILED; + break; + case GNUTLS_MAC_SHA: + ret = mhash_init( MHASH_SHA1); + if (!ret) return GNUTLS_HASH_FAILED; + break; + case GNUTLS_MAC_MD5: + ret = mhash_init( MHASH_MD5); + if (!ret) return GNUTLS_HASH_FAILED; + break; + default: + ret = GNUTLS_HASH_FAILED; + } + + return ret; +} + +int gnutls_hash_get_algo_len(MACAlgorithm algorithm) { +int ret; + + switch (algorithm) { + case GNUTLS_MAC_NULL: + ret = 0; + break; + case GNUTLS_MAC_SHA: + ret = mhash_get_block_size(MHASH_SHA1); + break; + case GNUTLS_MAC_MD5: + ret = mhash_get_block_size(MHASH_MD5); + break; + default: + ret = 0; + } + +return ret; + +} + +int gnutls_hash(GNUTLS_HASH_HANDLE handle, void* text, int textlen) { + + mhash( handle, text, textlen); + return 0; +} + +void* gnutls_hash_deinit(GNUTLS_HASH_HANDLE handle) { +char* mac; +int maclen; +char* ret; + + ret = mhash_end(handle); + + return ret; +} + + +GNUTLS_MAC_HANDLE gnutls_hmac_init(MACAlgorithm algorithm, char* key, int keylen) { +GNUTLS_MAC_HANDLE ret; + + switch (algorithm) { + case GNUTLS_MAC_NULL: + ret = GNUTLS_MAC_FAILED; + break; + case GNUTLS_MAC_SHA: + ret = mhash_hmac_init( MHASH_SHA1, key, keylen, 0); + if (!ret) ret = GNUTLS_MAC_FAILED; + break; + case GNUTLS_MAC_MD5: + ret = mhash_hmac_init( MHASH_MD5, key, keylen, 0); + if (!ret) ret = GNUTLS_MAC_FAILED; + break; + default: + ret = GNUTLS_MAC_FAILED; + } + + return ret; +} + +int gnutls_hmac_get_algo_len(MACAlgorithm algorithm) { +int ret; + + switch (algorithm) { + case GNUTLS_MAC_NULL: + ret = 0; + break; + case GNUTLS_MAC_SHA: + ret = mhash_get_hash_pblock(MHASH_SHA1); + break; + case GNUTLS_MAC_MD5: + ret = mhash_get_hash_pblock(MHASH_MD5); + break; + default: + ret = 0; + } + +return ret; + +} + +int gnutls_hmac(GNUTLS_MAC_HANDLE handle, void* text, int textlen) { + + mhash( handle, text, textlen); + return 0; + +} + +void* gnutls_hmac_deinit(GNUTLS_MAC_HANDLE handle) { +char* mac; +int maclen; +char* ret; + + ret = mhash_hmac_end(handle); + + return ret; +} + +#else /* This file handles all the internal functions that cope with hashes * and hmacs. Currently it uses the functions provided by * the gcrypt library that this can be easily changed. @@ -159,3 +288,4 @@ char* ret; return ret; } +#endif /* MHASH */ diff --git a/lib/gnutls_kx.c b/lib/gnutls_kx.c index 0f7de47b76..8da9e3127e 100644 --- a/lib/gnutls_kx.c +++ b/lib/gnutls_kx.c @@ -37,7 +37,7 @@ int _gnutls_send_server_kx_message(int cd, GNUTLS_STATE state) { KXAlgorithm algorithm; GNUTLS_MPI x, X, g, p; - int n_X, n_g, n_p; + size_t n_X, n_g, n_p; uint16 _n_X, _n_g, _n_p; uint8 *data = NULL; uint8 *data_p; @@ -122,7 +122,7 @@ int _gnutls_send_client_kx_message(int cd, GNUTLS_STATE state) { KXAlgorithm algorithm; GNUTLS_MPI x, X; - int n_X; + size_t n_X; uint16 _n_X; uint8 *data; int ret = 0; @@ -130,9 +130,12 @@ int _gnutls_send_client_kx_message(int cd, GNUTLS_STATE state) int premaster_size = 0; svoid *master; char *random = gnutls_malloc(64); + #ifdef HARD_DEBUG fprintf(stderr, "Sending client KX message\n"); #endif + + memmove(random, state->security_parameters.client_random, 32); memmove(&random[32], state->security_parameters.server_random, 32); algorithm = @@ -173,12 +176,14 @@ int _gnutls_send_client_kx_message(int cd, GNUTLS_STATE state) _gnutls_calc_dh_key(state->gnutls_internals.client_Y, x, state->gnutls_internals.client_p); + gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &premaster_size, state->gnutls_internals.KEY); premaster = secure_malloc(premaster_size); gcry_mpi_print(GCRYMPI_FMT_USG, premaster, &premaster_size, state->gnutls_internals.KEY); + /* THIS SHOULD BE DISCARDED */ gnutls_mpi_release(state->gnutls_internals.KEY); gnutls_mpi_release(state->gnutls_internals.client_Y); @@ -255,13 +260,16 @@ int _gnutls_recv_server_kx_message(int cd, GNUTLS_STATE state) { KXAlgorithm algorithm; uint16 n_Y, n_g, n_p; - int _n_Y, _n_g, _n_p; + size_t _n_Y, _n_g, _n_p; uint8 *data; int datasize; uint8 *data_p; uint8 *data_g; uint8 *data_Y; int ret = 0, i; +unsigned char tmpy[2048]; +int ii; + #ifdef HARD_DEBUG fprintf(stderr, "Receiving Server KX message\n"); #endif @@ -356,7 +364,7 @@ int _gnutls_recv_client_kx_message(int cd, GNUTLS_STATE state) { KXAlgorithm algorithm; uint16 n_Y; - int _n_Y; + size_t _n_Y; uint8 *data; int datasize; int ret = 0; @@ -399,8 +407,11 @@ int _gnutls_recv_client_kx_message(int cd, GNUTLS_STATE state) n_Y = byteswap16(n_Y); #endif _n_Y = n_Y; - gcry_mpi_scan(&state->gnutls_internals.client_Y, - GCRYMPI_FMT_USG, &data[2], &_n_Y); + if (gcry_mpi_scan(&state->gnutls_internals.client_Y, + GCRYMPI_FMT_USG, &data[2], &_n_Y)) { + gnutls_assert(); + return GNUTLS_E_MPI_SCAN_FAILED; + } state->gnutls_internals.KEY = gnutls_calc_dh_key(state-> gnutls_internals.client_Y,