]> git.rm.cloudns.org Git - xonotic/d0_blind_id.git/commitdiff
prevent a malleability attack; this BREAKS THE PROTOCOL. Also change the library...
authorRudolf Polzer <divverent@alientrap.org>
Sat, 10 Jul 2010 06:11:36 +0000 (08:11 +0200)
committerRudolf Polzer <divverent@alientrap.org>
Sat, 10 Jul 2010 06:45:06 +0000 (08:45 +0200)
Makefile.am
d0_bignum-gmp.c
d0_bignum.h
d0_blind_id.c

index 7706cf3b1a5d5f04e4c92bf082e3dbdaf0f7ceb3..2ec8b3d15560ea49144c825eb04cf5ee4144802d 100644 (file)
@@ -2,12 +2,12 @@ ACLOCAL_AMFLAGS = -I m4
 
 bin_PROGRAMS = blind_id
 blind_id_SOURCES = main.c
-blind_id_LDADD = libblind_id.la
+blind_id_LDADD = libd0_blind_id.la
 
-lib_LTLIBRARIES = libblind_id.la
-libblind_id_la_SOURCES = d0_bignum-gmp.c d0_blind_id.c d0.c d0_iobuf.c sha1.c
-libblind_id_la_LDFLAGS = -versioninfo 1:0:1
-libblind_id_la_CFLAGS = -fvisibility=hidden -Wold-style-definition -Wstrict-prototypes -Wsign-compare -Wdeclaration-after-statement
+lib_LTLIBRARIES = libd0_blind_id.la
+libd0_blind_id_la_SOURCES = d0_bignum-gmp.c d0_blind_id.c d0.c d0_iobuf.c sha1.c
+libd0_blind_id_la_LDFLAGS = -versioninfo 1:0:1
+libd0_blind_id_la_CFLAGS = -fvisibility=hidden -Wold-style-definition -Wstrict-prototypes -Wsign-compare -Wdeclaration-after-statement
 # versioninfo:
 #   - compatible interface change: c:r:a -> c+1:0:a+1
 #   - incompatible interface change: c:r:a -> c+1:0:0
index 26414498c6342d6abe68211d9808f82006e0de0c..30d630103d6a026671b42cf98000371ce3e8b443 100644 (file)
@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "d0_bignum.h"
 
 #include <gmp.h>
+#include <string.h>
 
 struct d0_bignum_s
 {
@@ -96,6 +97,54 @@ d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
        return bignum;
 }
 
+ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
+{
+       size_t count;
+       count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
+       if(count > bufsize)
+               return -1;
+       if(bufsize > count)
+       {
+               // pad from left (big endian numbers!)
+               memset(buf, 0, bufsize - count);
+               buf += bufsize - count;
+       }
+       bufsize = count;
+       mpz_export(buf, &bufsize, 1, 1, 0, 0, bignum->z);
+       if(bufsize > count)
+       {
+               // REALLY BAD
+               // mpz_sizeinbase lied to us
+               // buffer overflow
+               // there is no sane way whatsoever to handle this
+               abort();
+       }
+       if(bufsize < count)
+       {
+               // BAD
+               // mpz_sizeinbase lied to us
+               // move the number
+               if(bufsize == 0)
+               {
+                       memset(buf, 0, count);
+               }
+               else
+               {
+                       memmove(buf + count - bufsize, buf, bufsize);
+                       memset(buf, 0, count - bufsize);
+               }
+       }
+       return bufsize;
+}
+
+d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
+{
+       size_t count;
+       if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
+       mpz_import(bignum->z, bufsize, 1, 1, 0, 0, buf);
+       return bignum;
+}
+
 d0_bignum_t *d0_bignum_new(void)
 {
        d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
index 26fafc3e56dfdbd47c3f93753b629e3d70045dd5..bccaee5256adf24c7752e73e94089bb0eb97712e 100644 (file)
@@ -6,8 +6,10 @@
 
 typedef struct d0_bignum_s d0_bignum_t;
 
-WARN_UNUSED_RESULT BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum);
+WARN_UNUSED_RESULT BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum); // byte 0: 01=+ 11=- 00=0, rest = big endian number
 WARN_UNUSED_RESULT d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum);
+WARN_UNUSED_RESULT ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize); // big endian, return value = number of significant bytes (or -1 on error)
+WARN_UNUSED_RESULT d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize);
 
 void d0_bignum_INITIALIZE(void);
 void d0_bignum_SHUTDOWN(void);
index 8a1b77bf081567f54175e6b5510cdec1b36fb55b..ae60b035c9dadfb321e56e95b7fe14fbba829a9c 100644 (file)
@@ -51,7 +51,8 @@ struct d0_blind_id_s
 
        // public data (player ID public key, this is what the server gets to know)
        d0_bignum_t *schnorr_4_to_s;
-       d0_bignum_t *schnorr_4_to_s_signature; // 0 when signature is invalid
+       d0_bignum_t *schnorr_H_4_to_s_signature; // 0 when signature is invalid
+       // as hash function H, we get the SHA1 and reinterpret as bignum - yes, it always is < 160 bits
 
        // temp data
        d0_bignum_t *rsa_blind_signature_camouflage; // random number blind signature
@@ -265,7 +266,7 @@ void d0_blind_id_clear(d0_blind_id_t *ctx)
        if(ctx->schnorr_G) d0_bignum_free(ctx->schnorr_G);
        if(ctx->schnorr_s) d0_bignum_free(ctx->schnorr_s);
        if(ctx->schnorr_4_to_s) d0_bignum_free(ctx->schnorr_4_to_s);
-       if(ctx->schnorr_4_to_s_signature) d0_bignum_free(ctx->schnorr_4_to_s_signature);
+       if(ctx->schnorr_H_4_to_s_signature) d0_bignum_free(ctx->schnorr_H_4_to_s_signature);
        if(ctx->rsa_blind_signature_camouflage) d0_bignum_free(ctx->rsa_blind_signature_camouflage);
        if(ctx->r) d0_bignum_free(ctx->r);
        if(ctx->challenge) d0_bignum_free(ctx->challenge);
@@ -282,7 +283,7 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t
        if(src->schnorr_G) CHECK_ASSIGN(ctx->schnorr_G, d0_bignum_mov(NULL, src->schnorr_G));
        if(src->schnorr_s) CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_mov(NULL, src->schnorr_s));
        if(src->schnorr_4_to_s) CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mov(NULL, src->schnorr_4_to_s));
-       if(src->schnorr_4_to_s_signature) CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mov(NULL, src->schnorr_4_to_s_signature));
+       if(src->schnorr_H_4_to_s_signature) CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_bignum_mov(NULL, src->schnorr_H_4_to_s_signature));
        if(src->rsa_blind_signature_camouflage) CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_mov(NULL, src->rsa_blind_signature_camouflage));
        if(src->r) CHECK_ASSIGN(ctx->r, d0_bignum_mov(NULL, src->r));
        if(src->challenge) CHECK_ASSIGN(ctx->challenge, d0_bignum_mov(NULL, src->challenge));
@@ -466,9 +467,10 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx
        REPLACING(schnorr_s); REPLACING(schnorr_4_to_s);
 
        CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
+       CHECK(d0_bignum_shl(temp1, ctx->schnorr_G, -1));
        CHECK_ASSIGN(ctx->schnorr_s, d0_bignum_rand_range(ctx->schnorr_s, zero, temp0));
        CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_bignum_mod_pow(ctx->schnorr_4_to_s, four, ctx->schnorr_s, ctx->schnorr_G));
-       CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_zero(ctx->schnorr_4_to_s_signature));
+       CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_bignum_zero(ctx->schnorr_H_4_to_s_signature));
        return 1;
 
 fail:
@@ -478,6 +480,8 @@ fail:
 WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen)
 {
        d0_iobuf_t *out = NULL;
+       static unsigned char convbuf[2048];
+       size_t sz;
 
        // temps: temp0 rsa_blind_signature_camouflage^challenge, temp1 (4^s)*rsa_blind_signature_camouflage^challenge
        USING(rsa_n); USING(rsa_e); USING(schnorr_4_to_s);
@@ -487,7 +491,12 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *c
 
        CHECK_ASSIGN(ctx->rsa_blind_signature_camouflage, d0_bignum_rand_bit_atmost(ctx->rsa_blind_signature_camouflage, d0_bignum_size(ctx->rsa_n)));
        CHECK(d0_bignum_mod_pow(temp0, ctx->rsa_blind_signature_camouflage, ctx->rsa_e, ctx->rsa_n));
-       CHECK(d0_bignum_mod_mul(temp1, ctx->schnorr_4_to_s, temp0, ctx->rsa_n));
+       // we will actually sign SHA(4^s) to prevent a malleability attack!
+       sz = (d0_bignum_size(ctx->schnorr_4_to_s) + 7) / 8;
+       CHECK(d0_bignum_export_unsigned(ctx->schnorr_4_to_s, convbuf, sz) >= 0);
+       CHECK(d0_bignum_import_unsigned(temp2, sha(convbuf, sz), SHA_DIGESTSIZE));
+       // hash complete
+       CHECK(d0_bignum_mod_mul(temp1, temp2, temp0, ctx->rsa_n));
        CHECK(d0_iobuf_write_bignum(out, temp1));
        return d0_iobuf_close(out, outbuflen);
 
@@ -526,13 +535,13 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx
 
        // temps: temp0 input, temp1 rsa_blind_signature_camouflage^-1
        USING(rsa_blind_signature_camouflage); USING(rsa_n);
-       REPLACING(schnorr_4_to_s_signature);
+       REPLACING(schnorr_H_4_to_s_signature);
 
        in = d0_iobuf_open_read(inbuf, inbuflen);
 
        CHECK(d0_iobuf_read_bignum(in, temp0));
        CHECK(d0_bignum_mod_inv(temp1, ctx->rsa_blind_signature_camouflage, ctx->rsa_n));
-       CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_4_to_s_signature, temp0, temp1, ctx->rsa_n));
+       CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_bignum_mod_mul(ctx->schnorr_H_4_to_s_signature, temp0, temp1, ctx->rsa_n));
 
        return d0_iobuf_close(in, NULL);
 
@@ -579,13 +588,13 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const ch
 {
        d0_iobuf_t *in = NULL;
 
-       REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
+       REPLACING(schnorr_s); REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
 
        in = d0_iobuf_open_read(inbuf, inbuflen);
 
        CHECK_ASSIGN(ctx->schnorr_s, d0_iobuf_read_bignum(in, ctx->schnorr_s));
        CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
-       CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
+       CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
 
        return d0_iobuf_close(in, NULL);
 
@@ -598,12 +607,12 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const cha
 {
        d0_iobuf_t *in = NULL;
 
-       REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
+       REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
 
        in = d0_iobuf_open_read(inbuf, inbuflen);
 
        CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
-       CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
+       CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
 
        return d0_iobuf_close(in, NULL);
 
@@ -616,13 +625,13 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, c
 {
        d0_iobuf_t *out = NULL;
 
-       USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
+       USING(schnorr_s); USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
 
        out = d0_iobuf_open_write(outbuf, *outbuflen);
 
        CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_s));
        CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
-       CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
+       CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
 
        return d0_iobuf_close(out, outbuflen);
 
@@ -635,12 +644,12 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, ch
 {
        d0_iobuf_t *out = NULL;
 
-       USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
+       USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
 
        out = d0_iobuf_open_write(outbuf, *outbuflen);
 
        CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
-       CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
+       CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
 
        return d0_iobuf_close(out, outbuflen);
 
@@ -662,7 +671,7 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_
        // temps: temp0 order, temp0 4^r
        if(is_first)
        {
-               USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
+               USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
        }
        USING(schnorr_G);
        REPLACING(r);
@@ -675,7 +684,7 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_
                if(send_modulus)
                        CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_G));
                CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s));
-               CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_4_to_s_signature));
+               CHECK(d0_iobuf_write_bignum(out, ctx->schnorr_H_4_to_s_signature));
        }
 
        // start schnorr ID scheme
@@ -709,11 +718,13 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_bl
 {
        d0_iobuf_t *in = NULL;
        d0_iobuf_t *out = NULL;
+       static unsigned char convbuf[2048];
+       size_t sz;
 
        // temps: temp0 order, temp0 signature check
        if(is_first)
        {
-               REPLACING(schnorr_4_to_s); REPLACING(schnorr_4_to_s_signature);
+               REPLACING(schnorr_4_to_s); REPLACING(schnorr_H_4_to_s_signature);
                if(recv_modulus)
                        REPLACING(schnorr_G);
                else
@@ -721,7 +732,7 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_bl
        }
        else
        {
-               USING(schnorr_4_to_s); USING(schnorr_4_to_s_signature);
+               USING(schnorr_4_to_s); USING(schnorr_H_4_to_s_signature);
                USING(schnorr_G);
        }
        USING(rsa_e); USING(rsa_n);
@@ -739,18 +750,24 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_bl
                        CHECK(d0_bignum_cmp(ctx->schnorr_G, ctx->rsa_n) < 0);
                }
                CHECK_ASSIGN(ctx->schnorr_4_to_s, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s));
-               CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) > 0);
+               CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, zero) >= 0);
                CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s, ctx->schnorr_G) < 0);
-               CHECK_ASSIGN(ctx->schnorr_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_4_to_s_signature));
-               CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero) >= 0);
-               CHECK(d0_bignum_cmp(ctx->schnorr_4_to_s_signature, ctx->rsa_n) < 0);
+               CHECK_ASSIGN(ctx->schnorr_H_4_to_s_signature, d0_iobuf_read_bignum(in, ctx->schnorr_H_4_to_s_signature));
+               CHECK(d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero) >= 0);
+               CHECK(d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, ctx->rsa_n) < 0);
 
                // check signature of key (t = k^d, so, t^challenge = k)
-               CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
-               if(d0_bignum_cmp(temp0, ctx->schnorr_4_to_s))
+               CHECK(d0_bignum_mod_pow(temp0, ctx->schnorr_H_4_to_s_signature, ctx->rsa_e, ctx->rsa_n));
+               // we will actually sign SHA(4^s) to prevent a malleability attack!
+               sz = (d0_bignum_size(ctx->schnorr_4_to_s) + 7) / 8;
+               CHECK(d0_bignum_export_unsigned(ctx->schnorr_4_to_s, convbuf, sz) >= 0);
+               CHECK(d0_bignum_import_unsigned(temp2, sha(convbuf, sz), SHA_DIGESTSIZE));
+               CHECK(d0_bignum_divmod(NULL, temp1, temp2, ctx->rsa_n));
+               // hash complete
+               if(d0_bignum_cmp(temp0, temp1))
                {
                        // accept the key anyway, but mark as failed signature! will later return 0 in status
-                       CHECK(d0_bignum_zero(ctx->schnorr_4_to_s_signature));
+                       CHECK(d0_bignum_zero(ctx->schnorr_H_4_to_s_signature));
                }
        }
 
@@ -770,7 +787,7 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_bl
        CHECK(d0_iobuf_write_bignum(out, temp0));
 
        if(status)
-               *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
+               *status = !!d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero);
 
        d0_iobuf_close(in, NULL);
        return d0_iobuf_close(out, outbuflen);
@@ -863,7 +880,7 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind
        }
 
        if(status)
-               *status = !!d0_bignum_cmp(ctx->schnorr_4_to_s_signature, zero);
+               *status = !!d0_bignum_cmp(ctx->schnorr_H_4_to_s_signature, zero);
 
        if(ctx->msglen <= *msglen)
                memcpy(msg, ctx->msg, ctx->msglen);