X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=d0_blind_id.c;h=9bb650c7b9dae4b6a84f7f20039cbe7ec5cc4d05;hb=a0b5239a7c040e5694b62993ee09d3bfbc589b03;hp=4989e9e33cef7f32f22253a4be883acc19f3a8a7;hpb=45e4bc1701370aa0576e3f693c08dbf078fab028;p=xonotic%2Fd0_blind_id.git diff --git a/d0_blind_id.c b/d0_blind_id.c index 4989e9e..9bb650c 100644 --- a/d0_blind_id.c +++ b/d0_blind_id.c @@ -1,21 +1,37 @@ /* -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_blind_id.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$ + */ #include "d0_blind_id.h" @@ -88,9 +104,9 @@ struct d0_blind_id_s static d0_bignum_t *zero, *one, *four, *temp0, *temp1, *temp2, *temp3, *temp4; -void d0_blind_id_INITIALIZE(void) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_INITIALIZE(void) { - d0_bignum_INITIALIZE(); + CHECK(d0_bignum_INITIALIZE()); CHECK_ASSIGN(zero, d0_bignum_int(zero, 0)); CHECK_ASSIGN(one, d0_bignum_int(one, 1)); CHECK_ASSIGN(four, d0_bignum_int(four, 4)); @@ -99,8 +115,9 @@ void d0_blind_id_INITIALIZE(void) CHECK_ASSIGN(temp2, d0_bignum_int(temp2, 0)); CHECK_ASSIGN(temp3, d0_bignum_int(temp3, 0)); CHECK_ASSIGN(temp4, d0_bignum_int(temp4, 0)); + return 1; fail: - ; + return 0; } void d0_blind_id_SHUTDOWN(void) @@ -135,7 +152,7 @@ fail: return NULL; } -BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G) +D0_BOOL d0_dl_generate_key(size_t size, d0_bignum_t *G) { // using: temp0 if(size < 16) @@ -157,7 +174,7 @@ fail: return 0; } -BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n) +D0_BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t *d, d0_bignum_t *n) { // uses temp0 to temp4 int fail = 0; @@ -213,7 +230,7 @@ fail: return 0; } -BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass) +D0_BOOL d0_rsa_generate_key_fastreject(size_t size, d0_fastreject_function reject, d0_blind_id_t *ctx, void *pass) { // uses temp0 to temp4 int fail = 0; @@ -272,7 +289,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_longhash_destructive(d0_bignum_t *clobberme, char *outbuf, size_t outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_longhash_destructive(d0_bignum_t *clobberme, char *outbuf, size_t outbuflen) { d0_iobuf_t *out = NULL; static unsigned char convbuf[1024]; @@ -315,7 +332,7 @@ void d0_blind_id_clear(d0_blind_id_t *ctx) memset(ctx, 0, sizeof(*ctx)); } -WARN_UNUSED_RESULT BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src) { d0_blind_id_clear(ctx); if(src->rsa_n) CHECK_ASSIGN(ctx->rsa_n, d0_bignum_mov(NULL, src->rsa_n)); @@ -340,7 +357,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass) { REPLACING(rsa_e); REPLACING(rsa_d); REPLACING(rsa_n); @@ -356,12 +373,12 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k) { return d0_blind_id_generate_private_key_fastreject(ctx, k, NULL, NULL); } -WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) { d0_iobuf_t *in = NULL; @@ -379,7 +396,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) { d0_iobuf_t *in = NULL; @@ -395,7 +412,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; @@ -412,7 +429,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; @@ -429,7 +446,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_key(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; static unsigned char convbuf[2048]; @@ -461,7 +478,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_modulus(d0_blind_id_t *ctx) { USING(rsa_n); REPLACING(schnorr_G); @@ -473,7 +490,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_modulus(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) { d0_iobuf_t *in = NULL; @@ -488,7 +505,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_modulus(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; @@ -503,7 +520,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx) { // temps: temp0 = order USING(schnorr_G); @@ -519,7 +536,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_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], shabuf[2048]; @@ -552,7 +569,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_answer_private_id_request(const d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_answer_private_id_request(const d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen) { d0_iobuf_t *in = NULL; d0_iobuf_t *out = NULL; @@ -576,7 +593,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) { d0_iobuf_t *in = NULL; @@ -597,7 +614,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id_request_camouflage(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) { d0_iobuf_t *in = NULL; @@ -614,7 +631,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id_request_camouflage(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; @@ -631,7 +648,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) { d0_iobuf_t *in = NULL; @@ -650,7 +667,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen) { d0_iobuf_t *in = NULL; @@ -668,7 +685,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_private_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; @@ -687,7 +704,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_write_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; @@ -705,7 +722,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, BOOL is_first, BOOL send_modulus, char *msg, size_t msglen, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, char *msg, size_t msglen, char *outbuf, size_t *outbuflen) // start = // first run: send 4^s, 4^s signature // 1. get random r, send HASH(4^r) @@ -714,7 +731,7 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_ static unsigned char convbuf[1024]; d0_iobuf_t *conv = NULL; size_t sz = 0; - BOOL failed = 0; + D0_BOOL failed = 0; // temps: temp0 order, temp0 4^r if(is_first) @@ -746,18 +763,8 @@ WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_ CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0)); // can we SOMEHOW do this with just one mod_pow? -#pragma omp parallel default(shared) reduction(||:failed) -#pragma omp sections - { -#pragma omp section - { - MPCHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G)); - } -#pragma omp section - { - MPCHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G)); - } - } + MPCHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G)); + MPCHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G)); CHECK(!failed); // hash it, hash it, everybody hash it @@ -779,7 +786,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, BOOL is_first, BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, BOOL *status) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, D0_BOOL *status) // first run: get 4^s, 4^s signature // 1. check sig // 2. save HASH(4^r) @@ -873,7 +880,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen) // 1. read challenge challenge of SCHNORR_BITS // 2. reply with r + s * challenge mod order { @@ -914,7 +921,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, BOOL *status) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status) // 1. read y = r + s * challenge mod order // 2. verify: g^y (g^s)^-challenge = g^(r+s*challenge-s*challenge) = g^r // (check using H(g^r) which we know) @@ -979,7 +986,7 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_authenticate_with_private_id_generate_missing_signature(d0_blind_id_t *ctx) { size_t sz; static unsigned char shabuf[2048]; @@ -1004,18 +1011,22 @@ fail: return 0; } -WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_WARN_UNUSED_RESULT D0_BOOL d0_blind_id_fingerprint64_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { d0_iobuf_t *out = NULL; static unsigned char convbuf[1024]; d0_iobuf_t *conv = NULL; size_t sz, n; + USING(rsa_n); + USING(rsa_e); USING(schnorr_g_to_s); out = d0_iobuf_open_write(outbuf, *outbuflen); conv = d0_iobuf_open_write(convbuf, sizeof(convbuf)); + CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_n)); + CHECK(d0_iobuf_write_bignum(conv, ctx->rsa_e)); CHECK(d0_iobuf_write_bignum(conv, ctx->schnorr_g_to_s)); CHECK(d0_iobuf_close(conv, &sz)); conv = NULL; @@ -1035,7 +1046,7 @@ fail: return 0; } -BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) +D0_BOOL d0_blind_id_sessionkey_public_id(const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen) { USING(t); USING(other_g_to_t); USING(schnorr_G);