]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blobdiff - d0_bignum-openssl.c
also do this for COPYING
[xonotic/d0_blind_id.git] / d0_bignum-openssl.c
index ad04a1fbd46c7f0ca428662968d868e754be32c2..5b29f416de4577887765221da19c5c77827c2a90 100644 (file)
@@ -1,24 +1,48 @@
 /*
-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-openssl.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$
+ */
+
+/* NOTE: this file links against openssl (http://www.openssl.org), which is
+ * under the OpenSSL License. You may have to abide to its terms too if you use
+ * this file.
+ * To alternatively link to GMP, provide the option --without-openssl to
+ * ./configure.
+ */
 
 #include "d0_bignum.h"
 
+#include <assert.h>
+#include <string.h>
 #include <openssl/bn.h>
 
 struct d0_bignum_s
@@ -27,14 +51,14 @@ struct d0_bignum_s
 };
 
 static d0_bignum_t temp;
-static BN_CTX ctx;
+static BN_CTX *ctx;
 
 #include <time.h>
 #include <stdio.h>
 
 WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
 {
-       BN_CTX_init(&ctx);
+       ctx = BN_CTX_new();
        d0_bignum_init(&temp);
        return 1;
 }
@@ -42,7 +66,8 @@ WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
 void d0_bignum_SHUTDOWN(void)
 {
        d0_bignum_clear(&temp);
-       BN_CTX_free(&ctx);
+       BN_CTX_free(ctx);
+       ctx = NULL;
 }
 
 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
@@ -55,7 +80,7 @@ BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
                count = BN_num_bytes(&bignum->z);
                if(count > sizeof(numbuf) - 1)
                        return 0;
-               BN_bn2bin(numbuf+1, &bignum->z);
+               BN_bn2bin(&bignum->z, numbuf+1);
        }
        return d0_iobuf_write_packet(buf, numbuf, count + 1);
 }
@@ -207,7 +232,7 @@ d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
        if(n > 0)
                BN_lshift(&r->z, &a->z, n);
        else if(n < 0)
-               BN_rshift(&r->z, &a->z, n);
+               BN_rshift(&r->z, &a->z, -n);
        else if(r != a)
                BN_copy(&r->z, &a->z);
        return r;
@@ -230,7 +255,7 @@ d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum
 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
 {
        if(!r) r = d0_bignum_new(); if(!r) return NULL;
-       BN_mul(&r->z, &a->z, &b->z);
+       BN_mul(&r->z, &a->z, &b->z, ctx);
        return r;
 }
 
@@ -241,13 +266,13 @@ d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t
        if(q)
        {
                if(m)
-                       BN_div(&q->z, &m->z, &a->z, &b->z, &ctx);
+                       BN_div(&q->z, &m->z, &a->z, &b->z, ctx);
                else
-                       BN_div(&q->z, NULL, &a->z, &b->z, &ctx);
+                       BN_div(&q->z, NULL, &a->z, &b->z, ctx);
                assert(!"I know this code is broken (rounds towards zero), need handle negative correctly");
        }
        else
-               BN_nnmod(&q->z, NULL, &a->z, &b->z, &ctx);
+               BN_nnmod(&m->z, &a->z, &b->z, ctx);
        if(m)
                return m;
        else
@@ -257,33 +282,33 @@ d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t
 d0_bignum_t *d0_bignum_mod_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
 {
        if(!r) r = d0_bignum_new(); if(!r) return NULL;
-       BN_mod_add(&r->z, &a->z, &b->z, &m->z, &ctx);
+       BN_mod_add(&r->z, &a->z, &b->z, &m->z, ctx);
        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)
 {
        if(!r) r = d0_bignum_new(); if(!r) return NULL;
-       BN_mod_mul(&r->z, &a->z, &b->z, &m->z, &ctx);
+       BN_mod_mul(&r->z, &a->z, &b->z, &m->z, ctx);
        return r;
 }
 
 d0_bignum_t *d0_bignum_mod_pow(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
 {
        if(!r) r = d0_bignum_new(); if(!r) return NULL;
-       BN_mod_exp(&r->z, &a->z, &b->z, &m->z, &ctx);
+       BN_mod_exp(&r->z, &a->z, &b->z, &m->z, ctx);
        return r;
 }
 
 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 !!BN_mod_inverse(&r->z, &a->z, &m->z, &ctx);
+       return !!BN_mod_inverse(&r->z, &a->z, &m->z, ctx);
 }
 
 int d0_bignum_isprime(d0_bignum_t *r, int param)
 {
-       return BN_is_prime(&r->z, param, NULL, &ctx, NULL);
+       return BN_is_prime(&r->z, param, NULL, ctx, NULL);
 }
 
 d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const d0_bignum_t *a, const d0_bignum_t *b)
@@ -294,7 +319,7 @@ d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const
        else if(t)
                assert(!"Extended gcd not implemented");
        else
-               BN_gcd(&r->z, &a->z, &b->z, &ctx);
+               BN_gcd(&r->z, &a->z, &b->z, ctx);
        return r;
 }