From: Rudolf Polzer Date: Thu, 8 Jul 2010 18:07:32 +0000 (+0200) Subject: a "fastreject" facility helpful to generate keys with a specific facility (only makes... X-Git-Tag: xonotic-v0.1.0preview~38^2~15^2~6 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=2060c9a103a104b87de2efa57384d89806b947ef;p=xonotic%2Fd0_blind_id.git a "fastreject" facility helpful to generate keys with a specific facility (only makes sense for the RSA part) --- diff --git a/Makefile.am b/Makefile.am index da2a57b..7706cf3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,7 +6,7 @@ blind_id_LDADD = libblind_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 0:0:0 +libblind_id_la_LDFLAGS = -versioninfo 1:0:1 libblind_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 diff --git a/d0_blind_id.c b/d0_blind_id.c index 7c1f010..f366b52 100644 --- a/d0_blind_id.c +++ b/d0_blind_id.c @@ -189,7 +189,7 @@ BOOL d0_rsa_generate_key(size_t size, const d0_bignum_t *challenge, d0_bignum_t // n = temp0*temp1 CHECK(d0_bignum_mul(n, temp0, temp1)); - + // d = challenge^-1 mod (temp0-1)(temp1-1) CHECK(d0_bignum_mul(temp0, temp2, temp3)); CHECK(d0_bignum_mod_inv(d, challenge, temp0)); @@ -198,6 +198,65 @@ fail: return 0; } +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; + int gcdfail = 0; + int pb = (size + 1)/2; + int qb = size - pb; + if(pb < 8) + pb = 8; + if(qb < 8) + qb = 8; + for (;;) + { + CHECK(d0_bignum_rand_bit_exact(temp0, pb)); + if(d0_bignum_isprime(temp0, 10) == 0) + continue; + CHECK(d0_bignum_sub(temp2, temp0, one)); + CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp2, ctx->rsa_e)); + if(!d0_bignum_cmp(temp4, one)) + break; + if(++gcdfail == 3) + return 0; + ++gcdfail; + } + gcdfail = 0; + for (;;) + { + CHECK(d0_bignum_rand_bit_exact(temp1, qb)); + if(!d0_bignum_cmp(temp1, temp0)) + { + if(++fail == 3) + return 0; + } + fail = 0; + + // n = temp0*temp1 + CHECK(d0_bignum_mul(ctx->rsa_n, temp0, temp1)); + if(reject(ctx, pass)) + continue; + + if(d0_bignum_isprime(temp1, 10) == 0) + continue; + CHECK(d0_bignum_sub(temp3, temp1, one)); + CHECK(d0_bignum_gcd(temp4, NULL, NULL, temp3, ctx->rsa_e)); + if(!d0_bignum_cmp(temp4, one)) + break; + if(++gcdfail == 3) + return 0; + ++gcdfail; + } + + // ctx->rsa_d = ctx->rsa_e^-1 mod (temp0-1)(temp1-1) + CHECK(d0_bignum_mul(temp0, temp2, temp3)); + CHECK(d0_bignum_mod_inv(ctx->rsa_d, ctx->rsa_e, temp0)); + return 1; +fail: + return 0; +} + void d0_blind_id_clear(d0_blind_id_t *ctx) { if(ctx->rsa_n) d0_bignum_free(ctx->rsa_n); @@ -233,19 +292,27 @@ void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src) memcpy(ctx->msghash, src->msghash, sizeof(ctx->msghash)); } -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k) +WARN_UNUSED_RESULT 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); CHECK_ASSIGN(ctx->rsa_e, d0_bignum_int(ctx->rsa_e, 65537)); CHECK_ASSIGN(ctx->rsa_d, d0_bignum_zero(ctx->rsa_d)); CHECK_ASSIGN(ctx->rsa_n, d0_bignum_zero(ctx->rsa_n)); - CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure + if(reject) + CHECK(d0_rsa_generate_key_fastreject(k+1, reject, ctx, pass)); // must fit G for sure + else + CHECK(d0_rsa_generate_key(k+1, ctx->rsa_e, ctx->rsa_d, ctx->rsa_n)); // must fit G for sure return 1; fail: return 0; } +WARN_UNUSED_RESULT 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_iobuf_t *in = NULL; diff --git a/d0_blind_id.h b/d0_blind_id.h index 511ea82..339f463 100644 --- a/d0_blind_id.h +++ b/d0_blind_id.h @@ -4,12 +4,14 @@ #include "d0.h" typedef struct d0_blind_id_s d0_blind_id_t; +typedef BOOL (*d0_fastreject_function) (d0_blind_id_t *ctx, void *pass); EXPORT WARN_UNUSED_RESULT d0_blind_id_t *d0_blind_id_new(void); EXPORT void d0_blind_id_free(d0_blind_id_t *a); EXPORT void d0_blind_id_clear(d0_blind_id_t *ctx); EXPORT void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src); EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key(d0_blind_id_t *ctx, int k); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_key_fastreject(d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass); EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_key(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_key(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);