summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@crystal.(none)>2008-09-28 00:39:51 +0300
committerNikos Mavrogiannopoulos <nmav@crystal.(none)>2008-09-28 00:39:51 +0300
commitd01acbc48ae78ae85da6659f4183367f49495018 (patch)
treee478d2f5a8634cd7f03e385ea46a38f6476df572 /lib
parent2079a64d4fb65a0e62e1b361d2200860aa52af18 (diff)
downloadgnutls-d01acbc48ae78ae85da6659f4183367f49495018.tar.gz
optimized in order to avoid calling malloc for small buffers.
Diffstat (limited to 'lib')
-rw-r--r--lib/gnutls_mpi.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/lib/gnutls_mpi.c b/lib/gnutls_mpi.c
index 90763e1679..ad93ce642c 100644
--- a/lib/gnutls_mpi.c
+++ b/lib/gnutls_mpi.c
@@ -38,7 +38,6 @@
#define clearbit(v,n) ((unsigned char)(v) & ~( (unsigned char)(1) << (unsigned)(n)))
-/* FIXME: test this function */
bigint_t
_gnutls_mpi_randomize (bigint_t r, unsigned int bits,
gnutls_rnd_level_t level)
@@ -46,7 +45,26 @@ _gnutls_mpi_randomize (bigint_t r, unsigned int bits,
int size = 1 + (bits / 8), ret;
int rem, i;
bigint_t tmp;
- opaque buf[size];
+ char tmpbuf[512];
+ opaque *buf;
+ int buf_release;
+
+ if ( size < sizeof(tmpbuf))
+ {
+ buf = tmpbuf;
+ buf_release = 0;
+ }
+ else
+ {
+ buf = gnutls_malloc(size);
+ if (buf == NULL)
+ {
+ gnutls_assert();
+ goto cleanup;
+ }
+ buf_release = 1;
+ }
+
ret = _gnutls_rnd (level, buf, size);
if (ret < 0)
@@ -75,6 +93,12 @@ _gnutls_mpi_randomize (bigint_t r, unsigned int bits,
goto cleanup;
}
+ if (buf_release != 0)
+ {
+ gnutls_free( buf);
+ buf = NULL;
+ }
+
if (r != NULL)
{
_gnutls_mpi_set (r, tmp);
@@ -85,6 +109,8 @@ _gnutls_mpi_randomize (bigint_t r, unsigned int bits,
return tmp;
cleanup:
+ if (buf_release != 0)
+ gnutls_free( buf);
return NULL;
}