示例#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.BigIntToBytes(key.d),
               'primeP': util.BigIntToBytes(key.q),
               'primeQ': util.BigIntToBytes(key.p),
               'primeExponentP': util.BigIntToBytes(key.d 
                    % (key.q - 1)),
               'primeExponentQ': util.BigIntToBytes(key.d 
                    % (key.p - 1)),
               'crtCoefficient': util.BigIntToBytes(key.u) }
    pubkey = key.publickey()
    pub_params = { 'modulus': util.BigIntToBytes(key.n),
                   'publicExponent': util.BigIntToBytes(key.e) }
    pub = RsaPublicKey(pub_params, pubkey, size)
    return RsaPrivateKey(params, pub, key, size)
示例#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.BigIntToBytes(key.x)}
    pubkey = key.publickey()
    pub_params = { 'g': util.BigIntToBytes(pubkey.g),
                   'p': util.BigIntToBytes(pubkey.p),
                   'q': util.BigIntToBytes(pubkey.q),
                   'y': util.BigIntToBytes(pubkey.y) }
    pub = DsaPublicKey(pub_params, pubkey, size)
    return DsaPrivateKey(params, pub, key, size)
示例#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)
    bigint_bytes = util.TrimBytes(util.BigIntToBytes(self.key.sign(emsa_encoded, None)[0]))
    return util.PadBytes(bigint_bytes, (self.size // 8) - len(bigint_bytes))