Esempio n. 1
0
    def Generate(size=keyinfo.RSA_PRIV.default_size):
        """
    Return a newly generated RSA private key.

    @param size: length of key in bits to generate
    @type size: integer

    @return: a RSA private key
    @rtype: L{RsaPrivateKey}
    """
        key = RSA.generate(size, util.RandBytes)
        #NOTE: PyCrypto stores p < q, u = p^{-1} mod q
        #But OpenSSL and PKCS8 stores q < p, invq = q^{-1} mod p
        #So we have to reverse the p and q values
        params = {
            'privateExponent':
            util.PadBytes(util.BigIntToBytes(key.d), 1),
            'primeP':
            util.PadBytes(util.BigIntToBytes(key.q), 1),
            'primeQ':
            util.PadBytes(util.BigIntToBytes(key.p), 1),
            'primeExponentP':
            util.PadBytes(util.BigIntToBytes(key.d % (key.q - 1)), 1),
            'primeExponentQ':
            util.PadBytes(util.BigIntToBytes(key.d % (key.p - 1)), 1),
            'crtCoefficient':
            util.PadBytes(util.BigIntToBytes(key.u), 1)
        }
        pubkey = key.publickey()
        pub_params = {
            'modulus': util.PadBytes(util.BigIntToBytes(key.n), 1),
            'publicExponent': util.PadBytes(util.BigIntToBytes(key.e), 1)
        }
        pub = RsaPublicKey(pub_params, pubkey, size)
        return RsaPrivateKey(params, pub, key, size)
Esempio n. 2
0
  def Generate(size=keyinfo.DSA_PRIV.default_size):
    """
    Return a newly generated DSA private key.

    @param size: length of key in bits to generate
    @type size: integer

    @return: a DSA private key
    @rtype: L{DsaPrivateKey}
    """
    key = DSA.generate(size, util.RandBytes)
    params = { 'x': util.PadBytes(util.BigIntToBytes(key.x), 1) }
    pubkey = key.publickey()
    pub_params = { 'g': util.PadBytes(util.BigIntToBytes(pubkey.g), 1),
                   'p': util.PadBytes(util.BigIntToBytes(pubkey.p), 1),
                   'q': util.PadBytes(util.BigIntToBytes(pubkey.q), 1),
                   'y': util.PadBytes(util.BigIntToBytes(pubkey.y), 1)
                   }
    pub = DsaPublicKey(pub_params, pubkey, size)
    return DsaPrivateKey(params, pub, key, size)
Esempio n. 3
0
    def Sign(self, msg):
        """
    Return raw byte string of signature on the SHA-1 hash_id of the message.

    @param msg: message to be signed
    @type msg: string

    @return: string representation of long int signature over message
    @rtype: string
    """
        emsa_encoded = util.MakeEmsaMessage(msg, self.size)
        return util.BigIntToBytes(self.key.sign(emsa_encoded, None)[0])