Package Crypto :: Package PublicKey :: Module DSA :: Class _DSAobj
[frames] | no frames]

Class _DSAobj

pubkey.pubkey --+
                |
               _DSAobj

Class defining an actual DSA key.
Instance Methods
 
__init__(self, implementation, key, randfunc=None)
 
sign(self, M, K)
Sign a piece of data with DSA.
 
verify(self, M, signature)
Verify the validity of a DSA signature.
 
has_private(self)
Tell if the key object contains private components.
 
size(self)
Tell the maximum number of bits that can be handled by this key.
 
can_blind(self)
Tell if the algorithm can deal with data blinding.
 
can_encrypt(self)
Tell if the algorithm can deal with data encryption.
 
can_sign(self)
Tell if the algorithm can deal with cryptographic signatures.
 
publickey(self)
Construct a new key carrying only the public information.
 
exportKey(self, format='PEM', pkcs8=None, passphrase=None, protection=None)
Export this DSA key.

Inherited from pubkey.pubkey: blind, decrypt, encrypt, unblind

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

__init__(self, implementation, key, randfunc=None)
(Constructor)

 
Overrides: pubkey.pubkey.__init__

sign(self, M, K)

 

Sign a piece of data with DSA.

Parameters:
  • M (byte string or long) - The piece of data to sign with DSA. It may not be longer in bit size than the sub-group order (q).
  • K (long (recommended) or byte string (not recommended)) - A secret number, chosen randomly in the closed range [1,q-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 q and taking the modulus by q is not secure, since smaller values will occur more frequently. Generating a random number systematically smaller than q-1 (e.g. floor((q-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 a digest cryptographic hash, otherwise an attacker may mount an existential forgery attack.

verify(self, M, signature)

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

has_private(self)

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

size(self)

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

can_blind(self)

 

Tell if the algorithm can deal with data blinding.

This property concerns the algorithm, not the key itself. It may happen that this particular key object hasn't got the private information required carry out blinding.

Returns:
boolean
Overrides: pubkey.pubkey.can_blind
(inherited documentation)

can_encrypt(self)

 

Tell if the algorithm can deal with data encryption.

This property concerns the algorithm, not the key itself. It may happen that this particular key object hasn't got the private information required to decrypt data.

Returns:
boolean
Overrides: pubkey.pubkey.can_encrypt
(inherited documentation)

can_sign(self)

 

Tell if the algorithm can deal with cryptographic signatures.

This property concerns the algorithm, not the key itself. It may happen that this particular key object hasn't got the private information required to generate a signature.

Returns:
boolean
Overrides: pubkey.pubkey.can_sign
(inherited documentation)

publickey(self)

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

exportKey(self, format='PEM', pkcs8=None, passphrase=None, protection=None)

 

Export this DSA key.

Parameters:
  • format (string) - The format to use for wrapping the key:

    • 'DER'. Binary encoding.
    • 'PEM'. Textual encoding, done according to RFC1421/ RFC1423 (default).
    • 'OpenSSH'. Textual encoding, one line of text, see RFC4253. Only suitable for public keys, not private keys.
  • passphrase (string) - For private keys only. The pass phrase to use for deriving the encryption key.
  • pkcs8 (boolean) - For private keys only. If True (default), the key is arranged according to PKCS#8 and if False, according to the custom OpenSSL/OpenSSH encoding.
  • protection (string) - The encryption scheme to use for protecting the private key. It is only meaningful when a pass phrase is present too.

    If pkcs8 takes value True, protection is the PKCS#8 algorithm to use for deriving the secret and encrypting the private DSA key. For a complete list of algorithms, see Crypto.IO.PKCS8. The default is PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC.

    If pkcs8 is False, the obsolete PEM encryption scheme is used. It is based on MD5 for key derivation, and Triple DES for encryption. Parameter protection is ignored.

    The combination format='DER' and pkcs8=False is not allowed if a passphrase is present.

Returns:
A byte string with the encoded public or private half of the key.
Raises:
  • ValueError - When the format is unknown or when you try to encrypt a private key with DER format and OpenSSL/OpenSSH.

Attention: If you don't provide a pass phrase, the private key will be exported in the clear!


Class Variable Details

keydata

Dictionary of DSA parameters.

A public key will only have the following entries:

  • y, the public key.
  • g, the generator.
  • p, the modulus.
  • q, the order of the sub-group.

A private key will also have:

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