]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blobdiff - d0_bignum-gmp.c
also support signing
[xonotic/d0_blind_id.git] / d0_bignum-gmp.c
index 87aa0b88432bec07d64263e5764656ac755debab..3b36b7da9ce384b9ed9f0f3afcd5da38260823fc 100644 (file)
@@ -1,25 +1,55 @@
 /*
-Blind-ID library for user identification using RSA blind signatures
-Copyright (C) 2010  Rudolf Polzer
-
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Lesser General Public
-License as published by the Free Software Foundation; either
-version 2.1 of the License, or (at your option) any later version.
-
-This library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Lesser General Public License for more details.
-
-You should have received a copy of the GNU Lesser General Public
-License along with this library; if not, write to the Free Software
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-*/
+ * FILE:       d0_bignum-gmp.c
+ * AUTHOR:     Rudolf Polzer - divVerent@xonotic.org
+ * 
+ * Copyright (c) 2010, Rudolf Polzer
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Format:commit %H$
+ * $Id$
+ */
+
+/* NOTE: this file links against libgmp (http://gmplib.org), which is under the
+ * Lesser General Public License 2.1 or later. You may have to abide to its
+ * terms too if you use this file.
+ * To alternatively link to OpenSSL, provide the option --with-openssl to
+ * ./configure.
+ */
+
+#ifdef WIN32
+#include <windows.h>
+#include <wincrypt.h>
+#endif
 
 #include "d0_bignum.h"
 
 #include <gmp.h>
+#include <string.h>
+#include <stdlib.h>
 
 struct d0_bignum_s
 {
@@ -31,32 +61,77 @@ static d0_bignum_t temp;
 
 #include <time.h>
 #include <stdio.h>
-void d0_bignum_INITIALIZE()
+
+D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
 {
        FILE *f;
+       D0_BOOL ret = 1;
+       unsigned char buf[256];
        d0_bignum_init(&temp);
        gmp_randinit_mt(RANDSTATE);
        gmp_randseed_ui(RANDSTATE, time(NULL));
-       f = fopen("/dev/random", "rb");
+       * (time_t *) (&buf[0]) = time(0); // if everything else fails, we use the current time + uninitialized data
+#ifdef WIN32
+       {
+               HCRYPTPROV hCryptProv;
+               if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
+               {
+                       if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
+                       {
+                               fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
+                               ret = 0;
+                       }
+                       CryptReleaseContext(hCryptProv, 0);
+               }
+               else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
+               {
+                       if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
+                       {
+                               fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
+                               ret = 0;
+                       }
+                       CryptReleaseContext(hCryptProv, 0);
+               }
+               else
+               {
+                       fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
+                       ret = 0;
+               }
+       }
+#else
+       f = fopen("/dev/urandom", "rb");
+       if(!f)
+               f = fopen("/dev/random", "rb");
        if(f)
        {
-               unsigned char buf[256];
-               if(fread(buf, sizeof(buf), 1, f) == 1)
+               setbuf(f, NULL);
+               if(fread(buf, sizeof(buf), 1, f) != 1)
                {
-                       mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
-                       gmp_randseed(RANDSTATE, temp.z);
+                       fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n");
+                       ret = 0;
                }
                fclose(f);
        }
+       else
+       {
+               fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
+               ret = 0;
+       }
+#endif
+
+       mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
+       gmp_randseed(RANDSTATE, temp.z);
+
+       return ret;
 }
 
-void d0_bignum_SHUTDOWN()
+void d0_bignum_SHUTDOWN(void)
 {
        d0_bignum_clear(&temp);
        gmp_randclear(RANDSTATE);
 }
 
-BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
+D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
 {
        static unsigned char numbuf[65536];
        size_t count = 0;
@@ -93,7 +168,55 @@ d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
        return bignum;
 }
 
-d0_bignum_t *d0_bignum_new()
+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));
        mpz_init(b->z);
@@ -240,6 +363,13 @@ d0_bignum_t *d0_bignum_mod_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bi
        return r;
 }
 
+d0_bignum_t *d0_bignum_mod_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
+{
+       r = d0_bignum_sub(r, a, b);
+       mpz_fdiv_r(r->z, r->z, m->z);
+       return r;
+}
+
 d0_bignum_t *d0_bignum_mod_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
 {
        r = d0_bignum_mul(r, a, b);
@@ -254,7 +384,7 @@ d0_bignum_t *d0_bignum_mod_pow(d0_bignum_t *r, const d0_bignum_t *a, const d0_bi
        return r;
 }
 
-BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
+D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
 {
        // here, r MUST be set, as otherwise we cannot return error state!
        return mpz_invert(r->z, a->z, m->z);