From 933873052795f6abc14deead27f0f652a7e401e1 Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Thu, 18 Aug 2011 11:57:29 +0200 Subject: [PATCH] version 0.3: allow variable length for sign data buffer --- Makefile.am | 2 +- configure.ac | 2 +- d0_blind_id.c | 14 +++++++++----- d0_iobuf.c | 41 ++++++++++++++++++++++++++++++++++++++++- d0_iobuf.h | 3 ++- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Makefile.am b/Makefile.am index 2912c1b..aa3e6e5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,7 +23,7 @@ else libd0_blind_id_la_SOURCES += d0_bignum-gmp.c endif endif -libd0_blind_id_la_LDFLAGS = -versioninfo 4:0:4 +libd0_blind_id_la_LDFLAGS = -versioninfo 5:0:5 libd0_blind_id_la_CFLAGS = -fvisibility=hidden -Wold-style-definition -Wstrict-prototypes -Wsign-compare -Wdeclaration-after-statement library_includedir = $(includedir)/d0_blind_id library_include_HEADERS = d0_blind_id.h d0.h diff --git a/configure.ac b/configure.ac index 46853a1..b5d4e11 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([d0_blind_id], [0.2], [divVerent@xonotic.org]) +AC_INIT([d0_blind_id], [0.3], [divVerent@xonotic.org]) AC_CONFIG_MACRO_DIR([m4]) AM_INIT_AUTOMAKE([-Wall foreign]) AC_PROG_CC diff --git a/d0_blind_id.c b/d0_blind_id.c index a02aaa7..8852402 100644 --- a/d0_blind_id.c +++ b/d0_blind_id.c @@ -1052,8 +1052,8 @@ fail: D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_internal(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, D0_BOOL with_msg, const char *message, size_t msglen, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; - static unsigned char convbuf[1024]; - static unsigned char shabuf[1024]; + unsigned char *convbuf = NULL; + static unsigned char shabuf[2048]; d0_iobuf_t *conv = NULL; size_t sz = 0; @@ -1083,12 +1083,14 @@ D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_sign_internal(d0_ CHECK(d0_bignum_mod_pow(temp1, four, ctx->r, ctx->schnorr_G)); // hash it, hash it, everybody hash it - conv = d0_iobuf_open_write(convbuf, sizeof(convbuf)); + conv = d0_iobuf_open_write_p((void **) &convbuf, 0); CHECK(d0_iobuf_write_packet(conv, message, msglen)); CHECK(d0_iobuf_write_bignum(conv, temp1)); d0_iobuf_close(conv, &sz); conv = NULL; CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp0) + 7) / 8)); + d0_free(convbuf); + convbuf = NULL; CHECK(d0_bignum_import_unsigned(temp2, shabuf, (d0_bignum_size(temp0) + 7) / 8)); CHECK(d0_iobuf_write_bignum(out, temp2)); @@ -1124,7 +1126,7 @@ D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_internal(d { d0_iobuf_t *in = NULL; d0_iobuf_t *conv = NULL; - static unsigned char convbuf[2048]; + unsigned char *convbuf = NULL; static unsigned char shabuf[2048]; size_t sz; @@ -1202,12 +1204,14 @@ D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_sign_with_private_id_verify_internal(d CHECK_ASSIGN(temp3, d0_bignum_mod_mul(temp3, temp1, temp2, ctx->schnorr_G)); // temp3 now is g^r // hash it, hash it, everybody hash it - conv = d0_iobuf_open_write(convbuf, sizeof(convbuf)); + conv = d0_iobuf_open_write_p((void **) &convbuf, 0); CHECK(d0_iobuf_write_packet(conv, msg, *msglen)); CHECK(d0_iobuf_write_bignum(conv, temp3)); d0_iobuf_close(conv, &sz); conv = NULL; CHECK(d0_longhash_destructive(convbuf, sz, shabuf, (d0_bignum_size(temp4) + 7) / 8)); + d0_free(convbuf); + convbuf = NULL; CHECK(d0_bignum_import_unsigned(temp1, shabuf, (d0_bignum_size(temp4) + 7) / 8)); // verify signature diff --git a/d0_iobuf.c b/d0_iobuf.c index ce8e58d..4fa37e2 100644 --- a/d0_iobuf.c +++ b/d0_iobuf.c @@ -41,8 +41,10 @@ struct d0_iobuf_s { const unsigned char *inbuf; unsigned char *outbuf; + unsigned char **outbufp; size_t inpos, outpos, inbuflen, outbuflen; D0_BOOL ok; + D0_BOOL pdata; }; d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len) @@ -50,6 +52,7 @@ d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len) d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t)); b->inbuf = (const unsigned char *) buf; b->outbuf = NULL; + b->outbufp = NULL; b->inpos = b->outpos = 0; b->inbuflen = len; b->outbuflen = 0; @@ -62,8 +65,22 @@ d0_iobuf_t *d0_iobuf_open_write(void *buf, size_t len) d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t)); b->inbuf = (const unsigned char *) buf; b->outbuf = (unsigned char *) buf; + b->outbufp = NULL; b->inpos = b->outpos = 0; - b->inbuflen = len; + b->inbuflen = 0; + b->outbuflen = len; + b->ok = 1; + return b; +} + +d0_iobuf_t *d0_iobuf_open_write_p(void **buf, size_t len) +{ + d0_iobuf_t *b = d0_malloc(sizeof(d0_iobuf_t)); + b->inbuf = (const unsigned char *) *buf; + b->outbuf = (unsigned char *) *buf; + b->outbufp = (unsigned char **) buf; + b->inpos = b->outpos = 0; + b->inbuflen = 0; b->outbuflen = len; b->ok = 1; return b; @@ -81,6 +98,27 @@ D0_BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len) size_t d0_iobuf_write_raw(d0_iobuf_t *buf, const void *s, size_t n) { size_t nreal = n; + + // if packet doesn't fit, expand buffer + if(buf->outbufp && nreal > buf->outbuflen - buf->outpos) + { + size_t newsize = 1; + while(nreal + buf->outpos > newsize) + newsize <<= 1; + + { + char *newbuf = d0_malloc(newsize); + if(buf->outbuf) + { + memcpy(newbuf, buf->outbuf, buf->outbuflen); + d0_free(buf->outbuf); + } + buf->outbuf = newbuf; + *buf->outbufp = newbuf; + buf->outbuflen = newsize; + } + } + if(nreal > buf->outbuflen - buf->outpos) { buf->ok = 0; @@ -88,6 +126,7 @@ size_t d0_iobuf_write_raw(d0_iobuf_t *buf, const void *s, size_t n) } memcpy(buf->outbuf + buf->outpos, s, nreal); buf->outpos += nreal; + buf->inbuflen = buf->outpos; return nreal; } diff --git a/d0_iobuf.h b/d0_iobuf.h index d774e67..9705e2c 100644 --- a/d0_iobuf.h +++ b/d0_iobuf.h @@ -40,8 +40,9 @@ typedef struct d0_iobuf_s d0_iobuf_t; -D0_WARN_UNUSED_RESULT d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len); // note: can read AND write! +D0_WARN_UNUSED_RESULT d0_iobuf_t *d0_iobuf_open_read(const void *buf, size_t len); // note: can read D0_WARN_UNUSED_RESULT d0_iobuf_t *d0_iobuf_open_write(void *buf, size_t len); // note: can read AND write! +D0_WARN_UNUSED_RESULT d0_iobuf_t *d0_iobuf_open_write_p(void **buf, size_t len); // note: can read AND write! D0_WARN_UNUSED_RESULT D0_BOOL d0_iobuf_conv_base64_in(d0_iobuf_t *buf); D0_WARN_UNUSED_RESULT D0_BOOL d0_iobuf_conv_base64_out(d0_iobuf_t *buf); D0_BOOL d0_iobuf_close(d0_iobuf_t *buf, size_t *len); // don't warn -- 2.39.2