Package Crypto :: Package PublicKey :: Module ElGamal :: Class ElGamalobj
[frames] | no frames]

Class ElGamalobj

pubkey.pubkey --+
                |
               ElGamalobj

Class defining an ElGamal key.
Instance Methods
 
encrypt(self, plaintext, K)
Encrypt a piece of data with ElGamal.
 
decrypt(self, ciphertext)
Decrypt a piece of data with ElGamal.
 
sign(self, M, K)
Sign a piece of data with ElGamal.
 
verify(self, M, signature)
Verify the validity of an ElGamal signature.
 
size(self)
Tell the maximum number of bits that can be handled by this key.
 
has_private(self)
Tell if the key object contains private components.
 
publickey(self)
Construct a new key carrying only the public information.

Inherited from pubkey.pubkey: __init__, blind, can_blind, can_encrypt, can_sign, unblind

Class Variables
  keydata = ['p', 'g', 'y', 'x']
Dictionary of ElGamal parameters.
Method Details

encrypt(self, plaintext, K)

 
Encrypt a piece of data with ElGamal.
Parameters:
  • plaintext (byte string or long) - The piece of data to encrypt with ElGamal. It must be numerically smaller than the module (p).
  • K (long (recommended) or byte string (not recommended)) - A secret number, chosen randomly in the closed range [1,p-2].
Returns:
A tuple with two items. Each item is of the same type as the plaintext (string or long).
Overrides: pubkey.pubkey.encrypt
Attention:
  • selection of K is crucial for security. Generating a random number larger than p-1 and taking the modulus by p-1 is not secure, since smaller values will occur more frequently. Generating a random number systematically smaller than p-1 (e.g. floor((p-1)/8) random bytes) is also not secure. In general, it shall not be possible for an attacker to know the value of any bit of K.
  • The number K shall not be reused for any other operation and shall be discarded immediately.

decrypt(self, ciphertext)

 
Decrypt a piece of data with ElGamal.
Parameters:
  • ciphertext (byte string, long or a 2-item tuple as returned by encrypt) - The piece of data to decrypt with ElGamal.
Returns:
A byte string if ciphertext was a byte string or a tuple of byte strings. A long otherwise.
Overrides: pubkey.pubkey.decrypt

sign(self, M, K)

 
Sign a piece of data with ElGamal.
Parameters:
  • M (byte string or long) - The piece of data to sign with ElGamal. It may not be longer in bit size than p-1.
  • K (long (recommended) or byte string (not recommended)) - A secret number, chosen randomly in the closed range [1,p-2] and such that gcd(k,p-1)=1.
Returns:
A tuple with 2 longs.
Overrides: pubkey.pubkey.sign
Attention:
  • selection of K is crucial for security. Generating a random number larger than p-1 and taking the modulus by p-1 is not secure, since smaller values will occur more frequently. Generating a random number systematically smaller than p-1 (e.g. floor((p-1)/8) random bytes) is also not secure. In general, it shall not be possible for an attacker to know the value of any bit of K.
  • The number K shall not be reused for any other operation and shall be discarded immediately.
  • M must be be a cryptographic hash, otherwise an attacker may mount an existential forgery attack.

verify(self, M, signature)

 
Verify the validity of an ElGamal signature.
Parameters:
  • M (byte string or long) - The expected message.
  • signature (A tuple with 2 longs as return by sign) - The ElGamal signature to verify.
Returns:
True if the signature is correct, False otherwise.
Overrides: pubkey.pubkey.verify

size(self)

 
Tell the maximum number of bits that can be handled by this key.
Returns:
int
Overrides: pubkey.pubkey.size
(inherited documentation)

has_private(self)

 
Tell if the key object contains private components.
Returns:
bool
Overrides: pubkey.pubkey.has_private
(inherited documentation)

publickey(self)

 
Construct a new key carrying only the public information.
Returns:
A new pubkey object.
Overrides: pubkey.pubkey.publickey
(inherited documentation)

Class Variable Details

keydata

Dictionary of ElGamal parameters.

A public key will only have the following entries:

  • y, the public key.
  • g, the generator.
  • p, the modulus.

A private key will also have:

  • x, the private key.
Value:
['p', 'g', 'y', 'x']