diff options
Diffstat (limited to '')
-rw-r--r-- | cipher/dsa.c | 205 |
1 files changed, 42 insertions, 163 deletions
diff --git a/cipher/dsa.c b/cipher/dsa.c index 07f9e7075..8024ac0a2 100644 --- a/cipher/dsa.c +++ b/cipher/dsa.c @@ -32,6 +32,7 @@ void dsa_free_public_key( DSA_public_key *pk ) { mpi_free( pk->p ); pk->p = NULL; + mpi_free( pk->q ); pk->q = NULL; mpi_free( pk->g ); pk->g = NULL; mpi_free( pk->y ); pk->y = NULL; } @@ -40,121 +41,13 @@ void dsa_free_secret_key( DSA_secret_key *sk ) { mpi_free( sk->p ); sk->p = NULL; + mpi_free( sk->q ); sk->q = NULL; mpi_free( sk->g ); sk->g = NULL; mpi_free( sk->y ); sk->y = NULL; mpi_free( sk->x ); sk->x = NULL; } -static void -test_keys( DSA_public_key *pk, DSA_secret_key *sk, unsigned nbits ) -{ - MPI test = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); - MPI out1_a = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); - MPI out1_b = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); - MPI out2 = mpi_alloc( nbits / BITS_PER_MPI_LIMB ); - - mpi_set_bytes( test, nbits, get_random_byte, 0 ); - - dsa_sign( out1_a, out1_b, test, sk ); - if( !dsa_verify( out1_a, out1_b, test, pk ) ) - log_fatal("DSA operation: sign, verify failed\n"); - - mpi_free( test ); - mpi_free( out1_a ); - mpi_free( out1_b ); - mpi_free( out2 ); -} - - -/**************** - * generate a random secret exponent k from prime p, so - * that k is relatively prime to p-1 - */ -static MPI -gen_k( MPI p ) -{ - MPI k = mpi_alloc_secure( mpi_get_nlimbs(p) ); - MPI temp = mpi_alloc( mpi_get_nlimbs(p) ); - MPI p_1 = mpi_copy(p); - unsigned nbits = mpi_get_nbits(p); - - if( DBG_CIPHER ) - log_debug("choosing a random k "); - mpi_sub_ui( p_1, p, 1); - for(;;) { - if( DBG_CIPHER ) - fputc('.', stderr); - mpi_set_bytes( k, nbits, get_random_byte, 1 ); - mpi_set_highbit( k, nbits-1 ); /* make sure it's high (really needed?) */ - if( mpi_cmp( k, p_1 ) >= 0 ) - continue; /* is not smaller than (p-1) */ - if( mpi_gcd( temp, k, p_1 ) ) - break; /* okay, k is relatively prime to (p-1) */ - } - if( DBG_CIPHER ) - fputc('\n', stderr); - mpi_free(p_1); - mpi_free(temp); - - return k; -} - -/**************** - * Generate a key pair with a key of size NBITS - * Returns: 2 structures filles with all needed values - */ -void -dsa_generate( DSA_public_key *pk, DSA_secret_key *sk, unsigned nbits ) -{ - MPI p; /* the prime */ - MPI g; - MPI x; /* the secret exponent */ - MPI y; - - p = generate_public_prime( nbits ); - /* FIXME: check wether we shall assert that (p-1)/2 is also prime - * Schneier votes against it - */ - g = mpi_alloc_set_ui(3); - - /* select a random number */ - x = mpi_alloc_secure( nbits/BITS_PER_MPI_LIMB ); - if( DBG_CIPHER ) - log_debug("choosing a random x "); - do { - if( DBG_CIPHER ) - fputc('.', stderr); - mpi_set_bytes( x, nbits, get_random_byte, 1 ); /* fixme: should be 2 */ - mpi_set_highbit( x, nbits-1 ); /* make sure it's high (needed?) */ - } while( mpi_cmp( x, p ) >= 0 ); /* x must be smaller than p */ - - y = mpi_alloc(nbits/BITS_PER_MPI_LIMB); - mpi_powm( y, g, x, p ); - - if( DBG_CIPHER ) { - fputc('\n', stderr); - log_mpidump("dsa p= ", p ); - log_mpidump("dsa g= ", g ); - log_mpidump("dsa y= ", y ); - log_mpidump("dsa x= ", x ); - } - - - /* copy the stuff to the key structures */ - pk->p = mpi_copy(p); - pk->g = mpi_copy(g); - pk->y = mpi_copy(y); - sk->p = p; - sk->g = g; - sk->y = y; - sk->x = x; - - /* now we can test our keys (this should never fail!) */ - test_keys( pk, sk, nbits - 64 ); -} - - /**************** * Test wether the secret key is valid. * Returns: if this is a valid key. @@ -174,72 +67,58 @@ dsa_check_secret_key( DSA_secret_key *sk ) /**************** - * Make an Elgamal signature out of INPUT + * Make a DSA signature out of INPUT */ void -dsa_sign(MPI a, MPI b, MPI input, DSA_secret_key *skey ) +dsa_sign(MPI r, MPI s, MPI input, DSA_secret_key *skey ) { - MPI k; - MPI t = mpi_alloc( mpi_get_nlimbs(a) ); - MPI inv = mpi_alloc( mpi_get_nlimbs(a) ); - MPI p_1 = mpi_copy(skey->p); - - /* - * b = (t * inv) mod (p-1) - * b = (t * inv(k,(p-1),(p-1)) mod (p-1) - * b = (((M-x*a) mod (p-1)) * inv(k,(p-1),(p-1))) mod (p-1) - * - */ - mpi_sub_ui(p_1, p_1, 1); - k = gen_k( skey->p ); - mpi_powm( a, skey->g, k, skey->p ); - mpi_mul(t, skey->x, a ); - mpi_subm(t, input, t, p_1 ); - while( mpi_is_neg(t) ) - mpi_add(t, t, p_1); - mpi_invm(inv, k, p_1 ); - mpi_mulm(b, t, inv, p_1 ); - - #if 0 - if( DBG_CIPHER ) { - log_mpidump("dsa sign p= ", skey->p); - log_mpidump("dsa sign g= ", skey->g); - log_mpidump("dsa sign y= ", skey->y); - log_mpidump("dsa sign x= ", skey->x); - log_mpidump("dsa sign k= ", k); - log_mpidump("dsa sign M= ", input); - log_mpidump("dsa sign a= ", a); - log_mpidump("dsa sign b= ", b); - } - #endif - mpi_free(k); - mpi_free(t); - mpi_free(inv); - mpi_free(p_1); } /**************** - * Returns true if the signature composed from A and B is valid. + * Returns true if the signature composed from R and S is valid. */ int -dsa_verify(MPI a, MPI b, MPI input, DSA_public_key *pkey ) +dsa_verify(MPI r, MPI s, MPI input, DSA_public_key *pkey ) { int rc; - MPI t1 = mpi_alloc( mpi_get_nlimbs(a) ); - MPI t2 = mpi_alloc( mpi_get_nlimbs(a) ); - - mpi_powm( t1, pkey->y, a, pkey->p ); - mpi_powm( t2, a, b, pkey->p ); - mpi_mulm( t1, t1, t2, pkey->p ); - - mpi_powm( t2, pkey->g, input, pkey->p ); - - rc = !mpi_cmp( t1, t2 ); - - mpi_free(t1); - mpi_free(t2); + MPI w, u1, u2, v; + MPI base[3]; + MPI exp[3]; + + if( !(mpi_cmp_ui( r, 0 ) > 0 && mpi_cmp( r, pkey->q ) < 0) ) + return 0; /* assertion 0 < r < q failed */ + if( !(mpi_cmp_ui( s, 0 ) > 0 && mpi_cmp( s, pkey->q ) < 0) ) + return 0; /* assertion 0 < s < q failed */ + + w = mpi_alloc( mpi_get_nlimbs(pkey->q) ); + u1 = mpi_alloc( mpi_get_nlimbs(pkey->q) ); + u2 = mpi_alloc( mpi_get_nlimbs(pkey->q) ); + v = mpi_alloc( mpi_get_nlimbs(pkey->p) ); + + /* w = s^(-1) mod q */ + mpi_invm( w, s, pkey->q ); + + /* u1 = (input * w) mod q */ + mpi_mulm( u1, input, w, pkey->q ); + + /* u2 = r * w mod q */ + mpi_mulm( u2, r, w, pkey->q ); + + /* v = g^u1 * y^u2 mod p mod q */ + base[0] = pkey->g; exp[0] = u1; + base[1] = pkey->y; exp[1] = u2; + base[2] = NULL; exp[2] = NULL; + mpi_mulpowm( v, base, exp, pkey->p ); + mpi_fdiv_r( v, v, pkey->q ); + + rc = !mpi_cmp( v, r ); + + mpi_free(w); + mpi_free(u1); + mpi_free(u2); + mpi_free(v); return rc; } |