summaryrefslogtreecommitdiff
path: root/cipher/elgamal.c
diff options
context:
space:
mode:
authorMoritz Schulte <mo@g10code.com>2003-06-09 13:50:37 +0000
committerMoritz Schulte <mo@g10code.com>2003-06-09 13:50:37 +0000
commit109ae59a947c8ed29ff8762c3afd34a3b51611c8 (patch)
tree2179db0e047dd70ffb577eeab4efb65a02c0f627 /cipher/elgamal.c
parent6b2617d1f7a8869b4f544f9b1df4bfcf5d6db3a4 (diff)
downloadlibgcrypt-109ae59a947c8ed29ff8762c3afd34a3b51611c8.tar.gz
2003-06-08 Moritz Schulte <moritz@g10code.com>
* pubkey.c (gcry_pk_encrypt): Improve calculation for size of the format string. 2003-06-07 Moritz Schulte <moritz@g10code.com> * arcfour.c, bithelp.h, blowfish.c, cast5.c, cipher.c, crc.c, des.c, dsa.c, elgamal.c, md4.c, md5.c, md.c, primegen.c, pubkey.c, rand-internal.h, random.c, random.h, rijndael.c, rmd160.c, rmd160test.c, rmd.h, rndeged.c, rndlinux.c, rndunix.c, rndw32.c, rsa.c, sha1.c, sha256.c, sha512.c, tiger.c, twofish.c: Edited all preprocessor instructions to remove whitespace before the '#'. This is not required by C89, but there are some compilers out there that don't like it. Replaced any occurence of the now deprecated type names with the new ones. 2003-06-04 Moritz Schulte <moritz@g10code.com> * pubkey.c (gcry_pk_encrypt): Construct an arg_list and use gcry_sexp_build_array instead of gcry_sexp_build. (gcry_pk_sign): Likewise. (gcry_pk_genkey): Likewise. 2003-06-01 Moritz Schulte <moritz@g10code.com> * dsa.c (_gcry_dsa_generate): Do not check wether the algorithm ID does indeed belong to DSA. (_gcry_dsa_sign): Likewise. (_gcry_dsa_verify): Likewise. (_gcry_dsa_get_nbits): Likewise. * elgamal.c (_gcry_elg_check_secret_key): Do not check wether the algorithm ID does indeed belong to ElGamal. (_gcry_elg_encrypt): Likewise. (_gcry_elg_decrypt): Likewise. (_gcry_elg_sign): Likewise. (_gcry_elg_verify): Likewise. (_gcry_elg_get_nbits): Likewise. (_gcry_elg_generate): Likewise. * rsa.c (_gcry_rsa_generate): Do not check wether the algorithm ID does indeed belong to RSA. (_gcry_rsa_encrypt): Likewise. (_gcry_rsa_decrypt): Likewise. (_gcry_rsa_sign): Likewise. (_gcry_rsa_verify): Likewise. (_gcry_rsa_get_nbits): Likewise. 2003-05-30 Moritz Schulte <moritz@g10code.com> * md.c (md_get_algo): Return zero in case to algorithm is enabled. * md.c (gcry_md_info): Adjusted for new no-errno-API. (md_final): Likewise. (gcry_md_get_algo): Likewise. * pubkey.c (gcry_pk_get_keygrip): Likewise. (gcry_pk_ctl): Likewise. (gcry_pk_algo_info): Likewise. * des.c (selftest): Likewise. 2003-05-29 Moritz Schulte <moritz@g10code.com> * md.c (md_enable): Do not forget to release module on error. (gcry_md_open): Adjusted for new no-errno-API. (md_open): Likewise. (md_copy): Likewise. (gcry_md_copy): Likewise. (gcry_md_setkey): Likewise. (gcry_md_algo_info): Likewise. * cipher.c (gcry_cipher_open): Adjusted for new no-errno-API and also fixed a locking bug. (gcry_cipher_encrypt): Adjusted for new no-errno-API. (gcry_cipher_decrypt): Likewise. (gcry_cipher_ctl): Likewise. (gcry_cipher_info): Likewise. (gcry_cipher_algo_info): Likewise. 2003-05-28 Moritz Schulte <moritz@g10code.com> * md.c (md_enable): Adjusted for libgpg-error. (gcry_md_enable): Likewise. (gcry_digest_register_default): Likewise. (gcry_digest_register): Likewise. (check_digest_algo): Likewise. (prepare_macpads): Likewise. (gcry_md_setkey): Likewise. (gcry_md_ctl): Likewise. (gcry_md_get): Likewise. (gcry_md_algo_info): Likewise. (gcry_md_info): Likewise. * dsa.c (_gcry_dsa_generate): Likewise. (_gcry_dsa_check_secret_key): Likewise. (_gcry_dsa_sign): Likewie. (_gcry_dsa_verify): Likewise. * twofish.c (do_twofish_setkey): Likewise. (twofish_setkey): Likewise. * cipher.c (gcry_cipher_register): Likewise. 2003-05-25 Moritz Schulte <moritz@g10code.com> * rijndael.c (do_setkey): Adjusted for libgpg-error. (rijndael_setkey): Likewise. * random.c (gcry_random_add_bytes): Likewise. * elgamal.c (_gcry_elg_generate): Likewise. (_gcry_elg_check_secret_key): Likewise. (_gcry_elg_encrypt): Likewise. (_gcry_elg_decrypt): Likewise. (_gcry_elg_sign): Likewise. (_gcry_elg_verify): Likewise. * rsa.c (_gcry_rsa_generate): Likewise. (_gcry_rsa_check_secret_key): Likewise. (_gcry_rsa_encrypt): Likewise. (_gcry_rsa_decrypt): Likewise. (_gcry_rsa_sign): Likewise. (_gcry_rsa_verify): Likewise. * pubkey.c (dummy_generate, dummy_check_secret_key, dummy_encrypt, dummy_decrypt, dummy_sign, dummy_verify): Likewise. (gcry_pubkey_register): Likewise. (check_pubkey_algo): Likewise. (pubkey_generate): Likewise. (pubkey_check_secret_key): Likewise. (pubkey_encrypt): Likewise. (pubkey_decrypt): Likewise. (pubkey_sign): Likewise. (pubkey_verify): Likewise. (sexp_elements_extract): Likewise. (sexp_to_key): Likewise. (sexp_to_sig): Likewise. (sexp_to_enc): Likewise. (sexp_data_to_mpi): Likewise. (gcry_pk_encrypt): Likewise. (gcry_pk_decrypt): Likewise. (gcry_pk_sign): Likewise. (gcry_pk_verify): Likewise. (gcry_pk_testkey): Likewise. (gcry_pk_genkey): Likewise. (gcry_pk_ctl): Likewise. * cipher.c (dummy_setkey): Likewise. (check_cipher_algo): Likewise. (gcry_cipher_open): Likewise. (cipher_setkey): Likewise. (gcry_cipher_ctl): Likewise. (cipher_encrypt): Likewise. (gcry_cipher_encrypt): Likewise. (cipher_decrypt): Likewise. (gcry_cipher_decrypt): Likewise. (gcry_cipher_info): Likewise. (gcry_cipher_algo_info): Likewise. * cast5.c (cast_setkey): Likewise. (do_cast_setkey): Likewise. * arcfour.c (arcfour_setkey): Likewise. (do_arcfour_setkey): Likewise. * blowfish.c (do_bf_setkey): Likewise. (bf_setkey): Likewise. * des.c (do_des_setkey): Likewise. (do_tripledes_setkey): Likewise. 2003-05-22 Moritz Schulte <moritz@g10code.com> * tiger.c: Merged code ussing the U64_C macro from GnuPG. * sha512.c: Likewise. 2003-05-17 Moritz Schulte <moritz@g10code.com> * pubkey.c (gcry_pk_genkey): Fix type: acquire a lock, instead of releasing it. 2003-05-11 Moritz Schulte <moritz@g10code.com> * pubkey.c (gcry_pk_testkey): Call REGISTER_DEFAULT_CIPHERS. (gcry_pk_ctl): Likewise.
Diffstat (limited to 'cipher/elgamal.c')
-rw-r--r--cipher/elgamal.c277
1 files changed, 142 insertions, 135 deletions
diff --git a/cipher/elgamal.c b/cipher/elgamal.c
index e9a4804e..582533e4 100644
--- a/cipher/elgamal.c
+++ b/cipher/elgamal.c
@@ -30,45 +30,47 @@
#include "mpi.h"
#include "cipher.h"
-typedef struct {
- MPI p; /* prime */
- MPI g; /* group generator */
- MPI y; /* g^x mod p */
+typedef struct
+{
+ MPI p; /* prime */
+ MPI g; /* group generator */
+ MPI y; /* g^x mod p */
} ELG_public_key;
-typedef struct {
- MPI p; /* prime */
- MPI g; /* group generator */
- MPI y; /* g^x mod p */
- MPI x; /* secret exponent */
+typedef struct
+{
+ MPI p; /* prime */
+ MPI g; /* group generator */
+ MPI y; /* g^x mod p */
+ MPI x; /* secret exponent */
} ELG_secret_key;
-static void test_keys( ELG_secret_key *sk, unsigned nbits );
-static MPI gen_k( MPI p );
-static void generate( ELG_secret_key *sk, unsigned nbits, MPI **factors );
-static int check_secret_key( ELG_secret_key *sk );
-static void do_encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey );
-static void decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey );
-static void sign(MPI a, MPI b, MPI input, ELG_secret_key *skey);
-static int verify(MPI a, MPI b, MPI input, ELG_public_key *pkey);
+static void test_keys (ELG_secret_key *sk, unsigned nbits);
+static MPI gen_k (MPI p);
+static void generate (ELG_secret_key *sk, unsigned nbits, MPI **factors);
+static int check_secret_key (ELG_secret_key *sk);
+static void do_encrypt (MPI a, MPI b, MPI input, ELG_public_key *pkey);
+static void decrypt (MPI output, MPI a, MPI b, ELG_secret_key *skey);
+static void sign (MPI a, MPI b, MPI input, ELG_secret_key *skey);
+static int verify (MPI a, MPI b, MPI input, ELG_public_key *pkey);
-static void (*progress_cb) ( void *, const char *, int, int, int );
+static void (*progress_cb) (void *, const char *, int, int, int);
static void *progress_cb_data;
void
-_gcry_register_pk_elg_progress ( void (*cb)( void *,const char*, int,int,int),
- void *cb_data )
+_gcry_register_pk_elg_progress (void (*cb) (void *, const char *, int, int, int),
+ void *cb_data)
{
- progress_cb = cb;
- progress_cb_data = cb_data;
+ progress_cb = cb;
+ progress_cb_data = cb_data;
}
static void
-progress( int c )
+progress (int c)
{
if (progress_cb)
progress_cb (progress_cb_data, "pk_elg", c, 0, 0);
@@ -353,7 +355,7 @@ do_encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey )
*/
gcry_mpi_powm( b, pkey->y, k, pkey->p );
gcry_mpi_mulm( b, b, input, pkey->p );
- #if 0
+#if 0
if( DBG_CIPHER ) {
log_mpidump("elg encrypted y= ", pkey->y);
log_mpidump("elg encrypted p= ", pkey->p);
@@ -362,7 +364,7 @@ do_encrypt(MPI a, MPI b, MPI input, ELG_public_key *pkey )
log_mpidump("elg encrypted a= ", a);
log_mpidump("elg encrypted b= ", b);
}
- #endif
+#endif
mpi_free(k);
}
@@ -378,7 +380,7 @@ decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey )
gcry_mpi_powm( t1, a, skey->x, skey->p );
mpi_invm( t1, t1, skey->p );
mpi_mulm( output, b, t1, skey->p );
- #if 0
+#if 0
if( DBG_CIPHER ) {
log_mpidump("elg decrypted x= ", skey->x);
log_mpidump("elg decrypted p= ", skey->p);
@@ -386,7 +388,7 @@ decrypt(MPI output, MPI a, MPI b, ELG_secret_key *skey )
log_mpidump("elg decrypted b= ", b);
log_mpidump("elg decrypted M= ", output);
}
- #endif
+#endif
mpi_free(t1);
}
@@ -417,7 +419,7 @@ sign(MPI a, MPI b, MPI input, ELG_secret_key *skey )
mpi_invm(inv, k, p_1 );
mpi_mulm(b, t, inv, p_1 );
- #if 0
+#if 0
if( DBG_CIPHER ) {
log_mpidump("elg sign p= ", skey->p);
log_mpidump("elg sign g= ", skey->g);
@@ -428,7 +430,7 @@ sign(MPI a, MPI b, MPI input, ELG_secret_key *skey )
log_mpidump("elg sign a= ", a);
log_mpidump("elg sign b= ", b);
}
- #endif
+#endif
mpi_free(k);
mpi_free(t);
mpi_free(inv);
@@ -454,7 +456,7 @@ verify(MPI a, MPI b, MPI input, ELG_public_key *pkey )
t1 = mpi_alloc( mpi_get_nlimbs(a) );
t2 = mpi_alloc( mpi_get_nlimbs(a) );
- #if 0
+#if 0
/* t1 = (y^a mod p) * (a^b mod p) mod p */
gcry_mpi_powm( t1, pkey->y, a, pkey->p );
gcry_mpi_powm( t2, a, b, pkey->p );
@@ -464,7 +466,7 @@ verify(MPI a, MPI b, MPI input, ELG_public_key *pkey )
gcry_mpi_powm( t2, pkey->g, input, pkey->p );
rc = !mpi_cmp( t1, t2 );
- #elif 0
+#elif 0
/* t1 = (y^a mod p) * (a^b mod p) mod p */
base[0] = pkey->y; exp[0] = a;
base[1] = a; exp[1] = b;
@@ -475,7 +477,7 @@ verify(MPI a, MPI b, MPI input, ELG_public_key *pkey )
gcry_mpi_powm( t2, pkey->g, input, pkey->p );
rc = !mpi_cmp( t1, t2 );
- #else
+#else
/* t1 = g ^ - input * y ^ a * a ^ b mod p */
mpi_invm(t2, pkey->g, pkey->p );
base[0] = t2 ; exp[0] = input;
@@ -485,7 +487,7 @@ verify(MPI a, MPI b, MPI input, ELG_public_key *pkey )
mpi_mulpowm( t1, base, exp, pkey->p );
rc = !mpi_cmp_ui( t1, 1 );
- #endif
+#endif
mpi_free(t1);
mpi_free(t2);
@@ -496,133 +498,138 @@ verify(MPI a, MPI b, MPI input, ELG_public_key *pkey )
************** interface ******************
*********************************************/
-int
-_gcry_elg_generate( int algo, unsigned nbits, unsigned long dummy,
- MPI *skey, MPI **retfactors )
+gpg_err_code_t
+_gcry_elg_generate (int algo, unsigned nbits, unsigned long dummy,
+ MPI *skey, MPI **retfactors)
{
- ELG_secret_key sk;
-
- if( !is_ELGAMAL(algo) )
- return GCRYERR_INV_PK_ALGO;
-
- generate( &sk, nbits, retfactors );
- skey[0] = sk.p;
- skey[1] = sk.g;
- skey[2] = sk.y;
- skey[3] = sk.x;
- return 0;
+ ELG_secret_key sk;
+
+ generate (&sk, nbits, retfactors);
+ skey[0] = sk.p;
+ skey[1] = sk.g;
+ skey[2] = sk.y;
+ skey[3] = sk.x;
+
+ return GPG_ERR_NO_ERROR;
}
-int
-_gcry_elg_check_secret_key( int algo, MPI *skey )
+gpg_err_code_t
+_gcry_elg_check_secret_key (int algo, MPI *skey)
{
- ELG_secret_key sk;
-
- if( !is_ELGAMAL(algo) )
- return GCRYERR_INV_PK_ALGO;
- if( !skey[0] || !skey[1] || !skey[2] || !skey[3] )
- return GCRYERR_BAD_MPI;
-
- sk.p = skey[0];
- sk.g = skey[1];
- sk.y = skey[2];
- sk.x = skey[3];
- if( !check_secret_key( &sk ) )
- return GCRYERR_BAD_SECRET_KEY;
+ gpg_err_code_t err = GPG_ERR_NO_ERROR;
+ ELG_secret_key sk;
+
+ if ((! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
+ err = GPG_ERR_BAD_MPI;
+ else
+ {
+ sk.p = skey[0];
+ sk.g = skey[1];
+ sk.y = skey[2];
+ sk.x = skey[3];
+
+ if (! check_secret_key (&sk))
+ err = GPG_ERR_BAD_SECKEY;
+ }
- return 0;
+ return err;
}
-
-int
-_gcry_elg_encrypt( int algo, MPI *resarr, MPI data, MPI *pkey, int flags)
+gpg_err_code_t
+_gcry_elg_encrypt (int algo, MPI *resarr, MPI data, MPI *pkey, int flags)
{
- ELG_public_key pk;
-
- if( !is_ELGAMAL(algo) )
- return GCRYERR_INV_PK_ALGO;
- if( !data || !pkey[0] || !pkey[1] || !pkey[2] )
- return GCRYERR_BAD_MPI;
-
- pk.p = pkey[0];
- pk.g = pkey[1];
- pk.y = pkey[2];
- resarr[0] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
- resarr[1] = mpi_alloc( mpi_get_nlimbs( pk.p ) );
- do_encrypt( resarr[0], resarr[1], data, &pk );
- return 0;
+ gpg_err_code_t err = GPG_ERR_NO_ERROR;
+ ELG_public_key pk;
+
+ if ((! data) || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
+ err = GPG_ERR_BAD_MPI;
+ else
+ {
+ pk.p = pkey[0];
+ pk.g = pkey[1];
+ pk.y = pkey[2];
+ resarr[0] = mpi_alloc (mpi_get_nlimbs (pk.p));
+ resarr[1] = mpi_alloc (mpi_get_nlimbs (pk.p));
+ do_encrypt (resarr[0], resarr[1], data, &pk);
+ }
+ return err;
}
-int
-_gcry_elg_decrypt( int algo, MPI *result, MPI *data, MPI *skey, int flags)
+
+gpg_err_code_t
+_gcry_elg_decrypt (int algo, MPI *result, MPI *data, MPI *skey, int flags)
{
- ELG_secret_key sk;
-
- if( !is_ELGAMAL(algo) )
- return GCRYERR_INV_PK_ALGO;
- if( !data[0] || !data[1]
- || !skey[0] || !skey[1] || !skey[2] || !skey[3] )
- return GCRYERR_BAD_MPI;
-
- sk.p = skey[0];
- sk.g = skey[1];
- sk.y = skey[2];
- sk.x = skey[3];
- *result = mpi_alloc_secure( mpi_get_nlimbs( sk.p ) );
- decrypt( *result, data[0], data[1], &sk );
- return 0;
+ gpg_err_code_t err = GPG_ERR_NO_ERROR;
+ ELG_secret_key sk;
+
+ if ((! data[0]) || (! data[1])
+ || (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
+ err = GPG_ERR_BAD_MPI;
+ else
+ {
+ sk.p = skey[0];
+ sk.g = skey[1];
+ sk.y = skey[2];
+ sk.x = skey[3];
+ *result = mpi_alloc_secure (mpi_get_nlimbs (sk.p));
+ decrypt (*result, data[0], data[1], &sk);
+ }
+ return err;
}
-int
-_gcry_elg_sign( int algo, MPI *resarr, MPI data, MPI *skey )
+
+gpg_err_code_t
+_gcry_elg_sign (int algo, MPI *resarr, MPI data, MPI *skey)
{
- ELG_secret_key sk;
-
- if( !is_ELGAMAL(algo) )
- return GCRYERR_INV_PK_ALGO;
- if( !data || !skey[0] || !skey[1] || !skey[2] || !skey[3] )
- return GCRYERR_BAD_MPI;
-
- sk.p = skey[0];
- sk.g = skey[1];
- sk.y = skey[2];
- sk.x = skey[3];
- resarr[0] = mpi_alloc( mpi_get_nlimbs( sk.p ) );
- resarr[1] = mpi_alloc( mpi_get_nlimbs( sk.p ) );
- sign( resarr[0], resarr[1], data, &sk );
- return 0;
+ gpg_err_code_t err = GPG_ERR_NO_ERROR;
+ ELG_secret_key sk;
+
+ if ((! data)
+ || (! skey[0]) || (! skey[1]) || (! skey[2]) || (! skey[3]))
+ err = GPG_ERR_BAD_MPI;
+ else
+ {
+ sk.p = skey[0];
+ sk.g = skey[1];
+ sk.y = skey[2];
+ sk.x = skey[3];
+ resarr[0] = mpi_alloc (mpi_get_nlimbs (sk.p));
+ resarr[1] = mpi_alloc (mpi_get_nlimbs (sk.p));
+ sign (resarr[0], resarr[1], data, &sk);
+ }
+
+ return err;
}
-int
-_gcry_elg_verify( int algo, MPI hash, MPI *data, MPI *pkey,
- int (*cmp)(void *, MPI), void *opaquev )
+gpg_err_code_t
+_gcry_elg_verify (int algo, MPI hash, MPI *data, MPI *pkey,
+ int (*cmp) (void *, MPI), void *opaquev)
{
- ELG_public_key pk;
+ gpg_err_code_t err = GPG_ERR_NO_ERROR;
+ ELG_public_key pk;
+
+ if ((! data[0]) || (! data[1]) || (! hash)
+ || (! pkey[0]) || (! pkey[1]) || (! pkey[2]))
+ err = GPG_ERR_BAD_MPI;
+ else
+ {
+ pk.p = pkey[0];
+ pk.g = pkey[1];
+ pk.y = pkey[2];
+ if (! verify (data[0], data[1], hash, &pk))
+ err = GPG_ERR_BAD_SIGNATURE;
+ }
- if( !is_ELGAMAL(algo) )
- return GCRYERR_INV_PK_ALGO;
- if( !data[0] || !data[1] || !hash
- || !pkey[0] || !pkey[1] || !pkey[2] )
- return GCRYERR_BAD_MPI;
-
- pk.p = pkey[0];
- pk.g = pkey[1];
- pk.y = pkey[2];
- if( !verify( data[0], data[1], hash, &pk ) )
- return GCRYERR_BAD_SIGNATURE;
- return 0;
+ return err;
}
-
unsigned int
-_gcry_elg_get_nbits( int algo, MPI *pkey )
+_gcry_elg_get_nbits (int algo, MPI *pkey)
{
- if( !is_ELGAMAL(algo) )
- return 0;
- return mpi_get_nbits( pkey[0] );
+ return mpi_get_nbits (pkey[0]);
}
static char *elg_names[] =