Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from datetime import datetime
from enum import Enum
# do some type checking to determine if we should check the identity of an object member
# these types are singletons
if isinstance(obj, (Enum, bool, type(None))):
return False
# these types are immutable
if isinstance(obj, (six.string_types, datetime)):
return False
# integers are kind of a special case.
# ints that do not exceed sys.maxsize are singletons, and in either case are immutable
# this shouldn't apply to MPIs, though, which are subclasses of int
if isinstance(obj, int) and not isinstance(obj, pgpy.packet.types.MPI):
return False
return True
def clear(self):
"""delete and re-initialize all private components to zero"""
for field in self.__privfields__:
delattr(self, field)
setattr(self, field, MPI(0))
padder = PKCS7(64).padder()
m = padder.update(_m) + padder.finalize()
km = pk.keymaterial
ct = cls()
# generate ephemeral key pair and keep public key in ct
# use private key to compute the shared point "s"
if km.oid == EllipticCurveOID.Curve25519:
v = x25519.X25519PrivateKey.generate()
x = v.public_key().public_bytes(encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw)
ct.p = ECPoint.from_values(km.oid.key_size, ECPointFormat.Native, x)
s = v.exchange(km.__pubkey__())
else:
v = ec.generate_private_key(km.oid.curve(), default_backend())
x = MPI(v.public_key().public_numbers().x)
y = MPI(v.public_key().public_numbers().y)
ct.p = ECPoint.from_values(km.oid.key_size, ECPointFormat.Standard, x, y)
s = v.exchange(ec.ECDH(), km.__pubkey__())
# derive the wrapping key
z = km.kdf.derive_key(s, km.oid, PubKeyAlgorithm.ECDH, pk.fingerprint)
# compute C
ct.c = aes_key_wrap(z, m, default_backend())
return ct
def _generate(self, key_size):
if any(c != 0 for c in self): # pragma: no cover
raise PGPError("key is already populated")
# generate some big numbers!
pk = rsa.generate_private_key(65537, key_size, default_backend())
pkn = pk.private_numbers()
self.n = MPI(pkn.public_numbers.n)
self.e = MPI(pkn.public_numbers.e)
self.d = MPI(pkn.d)
self.p = MPI(pkn.p)
self.q = MPI(pkn.q)
# from the RFC:
# "- MPI of u, the multiplicative inverse of p, mod q."
# or, simply, p^-1 mod p
# rsa.rsa_crt_iqmp(p, q) normally computes q^-1 mod p,
# so if we swap the values around we get the answer we want
self.u = MPI(rsa.rsa_crt_iqmp(pkn.q, pkn.p))
del pkn
del pk
self._compute_chksum()
# generate some big numbers!
pk = rsa.generate_private_key(65537, key_size, default_backend())
pkn = pk.private_numbers()
self.n = MPI(pkn.public_numbers.n)
self.e = MPI(pkn.public_numbers.e)
self.d = MPI(pkn.d)
self.p = MPI(pkn.p)
self.q = MPI(pkn.q)
# from the RFC:
# "- MPI of u, the multiplicative inverse of p, mod q."
# or, simply, p^-1 mod p
# rsa.rsa_crt_iqmp(p, q) normally computes q^-1 mod p,
# so if we swap the values around we get the answer we want
self.u = MPI(rsa.rsa_crt_iqmp(pkn.q, pkn.p))
del pkn
del pk
self._compute_chksum()
def parse(self, packet):
super(RSAPriv, self).parse(packet)
self.s2k.parse(packet)
if not self.s2k:
self.d = MPI(packet)
self.p = MPI(packet)
self.q = MPI(packet)
self.u = MPI(packet)
if self.s2k.usage == 0:
self.chksum = packet[:2]
del packet[:2]
else:
##TODO: this needs to be bounded to the length of the encrypted key material
self.encbytes = packet
def parse(self, packet):
super(ElGPriv, self).parse(packet)
self.s2k.parse(packet)
if not self.s2k:
self.x = MPI(packet)
else:
self.encbytes = packet
if self.s2k.usage in [0, 255]:
self.chksum = packet[:2]
del packet[:2]
def decrypt_keyblob(self, passphrase):
kb = super(EdDSAPriv, self).decrypt_keyblob(passphrase)
del passphrase
self.s = MPI(kb)
def parse(self, packet):
super(RSAPriv, self).parse(packet)
self.s2k.parse(packet)
if not self.s2k:
self.d = MPI(packet)
self.p = MPI(packet)
self.q = MPI(packet)
self.u = MPI(packet)
if self.s2k.usage == 0:
self.chksum = packet[:2]
del packet[:2]
else:
##TODO: this needs to be bounded to the length of the encrypted key material
self.encbytes = packet
def from_signer(self, sig):
seq, _ = decoder.decode(sig)
self.r = MPI(seq[0])
self.s = MPI(seq[1])