Package Crypto :: Package PublicKey :: Module RSA :: Class RSAImplementation
[frames] | no frames]

Class RSAImplementation

object --+
         |
        RSAImplementation

An RSA key factory.

This class is only internally used to implement the methods of the Crypto.PublicKey.RSA module.

Instance Methods
 
__init__(self, **kwargs)
Create a new RSA key factory.
 
generate(self, bits, randfunc=None, progress_func=None, e=65537)
Randomly generate a fresh, new RSA key.
 
construct(self, tup)
Construct an RSA key from a tuple of valid RSA components.
 
importKey(self, externKey, passphrase=None)
Import an RSA key (public or private half), encoded in standard form.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, **kwargs)
(Constructor)

 
Create a new RSA key factory.
Parameters:
  • use_fast_math (bool) - Specify which mathematic library to use:

    • None (default). Use fastest math available.
    • True . Use fast math.
    • False . Use slow math.
  • default_randfunc (callable) - Specify how to collect random data:

    • None (default). Use Random.new().read().
    • not None . Use the specified function directly.
Raises:
  • RuntimeError - When use_fast_math =True but fast math is not available.
Overrides: object.__init__

generate(self, bits, randfunc=None, progress_func=None, e=65537)

 
Randomly generate a fresh, new RSA key.
Parameters:
  • bits (int) - Key length, or size (in bits) of the RSA modulus. It must be a multiple of 256, and no smaller than 1024.
  • randfunc (callable) - Random number generation function; it should accept a single integer N and return a string of random data N bytes long. If not specified, a new one will be instantiated from Crypto.Random.
  • progress_func (callable) - Optional function that will be called with a short string containing the key parameter currently being generated; it's useful for interactive applications where a user is waiting for a key to be generated.
  • e (int) - Public RSA exponent. It must be an odd positive integer. It is typically a small number with very few ones in its binary representation. The default value 65537 (= 0b10000000000000001 ) is a safe choice: other common values are 5, 7, 17, and 257.
Returns:
An RSA key object (_RSAobj).
Raises:
  • ValueError - When bits is too little or not a multiple of 256, or when e is not odd or smaller than 2.
Attention:
  • You should always use a cryptographically secure random number generator, such as the one defined in the Crypto.Random module; don't just use the current time and the random module.
  • Exponent 3 is also widely used, but it requires very special care when padding the message.

construct(self, tup)

 

Construct an RSA key from a tuple of valid RSA components.

The modulus n must be the product of two primes. The public exponent e must be odd and larger than 1.

In case of a private key, the following equations must apply:

  • e != 1
  • p*q = n
  • e*d = 1 mod (p-1)(q-1)
  • p*u = 1 mod q
Parameters:
  • tup (tuple) - A tuple of long integers, with at least 2 and no more than 6 items. The items come in the following order:

    1. RSA modulus (n).
    2. Public exponent (e).
    3. Private exponent (d). Only required if the key is private.
    4. First factor of n (p). Optional.
    5. Second factor of n (q). Optional.
    6. CRT coefficient, (1/p) mod q (u). Optional.
Returns:
An RSA key object (_RSAobj).

importKey(self, externKey, passphrase=None)

 

Import an RSA key (public or private half), encoded in standard form.

Parameters:
  • externKey (string) - The RSA key to import, encoded as a string.

    An RSA public key can be in any of the following formats:

    • X.509 subjectPublicKeyInfo DER SEQUENCE (binary or PEM encoding)
    • PKCS#1 RSAPublicKey DER SEQUENCE (binary or PEM encoding)
    • OpenSSH (textual public key only)

    An RSA private key can be in any of the following formats:

    • PKCS#1 RSAPrivateKey DER SEQUENCE (binary or PEM encoding)
    • PKCS#8 PrivateKeyInfo DER SEQUENCE (binary or PEM encoding)
    • OpenSSH (textual public key only)

    For details about the PEM encoding, see RFC1421/RFC1423.

    In case of PEM encoding, the private key can be encrypted with DES or 3TDES according to a certain pass phrase. Only OpenSSL-compatible pass phrases are supported.

  • passphrase (string) - In case of an encrypted PEM key, this is the pass phrase from which the encryption key is derived.
Returns:
An RSA key object (_RSAobj).
Raises:
  • ValueError/IndexError/TypeError - When the given key cannot be parsed (possibly because the pass phrase is wrong).