MCPcopy Create free account
hub / github.com/SecurityInnovation/PGPy / decrypt_keyblob

Method decrypt_keyblob

pgpy/packet/fields.py:1240–1271  ·  view source on GitHub ↗
(self, passphrase)

Source from the content-addressed store, hash-verified

1238
1239 @abc.abstractmethod
1240 def decrypt_keyblob(self, passphrase):
1241 if not self.s2k: # pragma: no cover
1242 # not encrypted
1243 return
1244
1245 # Encryption/decryption of the secret data is done in CFB mode using
1246 # the key created from the passphrase and the Initial Vector from the
1247 # packet. A different mode is used with V3 keys (which are only RSA)
1248 # than with other key formats. (...)
1249 #
1250 # With V4 keys, a simpler method is used. All secret MPI values are
1251 # encrypted in CFB mode, including the MPI bitcount prefix.
1252
1253 # derive the session key from our passphrase, and then unreference passphrase
1254 sessionkey = self.s2k.derive_key(passphrase)
1255 del passphrase
1256
1257 # attempt to decrypt this key
1258 pt = _decrypt(bytes(self.encbytes), bytes(sessionkey), self.s2k.encalg, bytes(self.s2k.iv))
1259
1260 # check the hash to see if we decrypted successfully or not
1261 if self.s2k.usage == 254 and not pt[-20:] == hashlib.new('sha1', pt[:-20]).digest():
1262 # if the usage byte is 254, key material is followed by a 20-octet sha-1 hash of the rest
1263 # of the key material block
1264 raise PGPDecryptionError("Passphrase was incorrect!")
1265
1266 if self.s2k.usage == 255 and not self.bytes_to_int(pt[-2:]) == (sum(bytearray(pt[:-2])) % 65536): # pragma: no cover
1267 # if the usage byte is 255, key material is followed by a 2-octet checksum of the rest
1268 # of the key material block
1269 raise PGPDecryptionError("Passphrase was incorrect!")
1270
1271 return bytearray(pt)
1272
1273 def sign(self, sigdata, hash_alg):
1274 return NotImplemented # pragma: no cover

Callers 6

decrypt_keyblobMethod · 0.45
decrypt_keyblobMethod · 0.45
decrypt_keyblobMethod · 0.45
decrypt_keyblobMethod · 0.45
decrypt_keyblobMethod · 0.45
unprotectMethod · 0.45

Calls 5

_decryptFunction · 0.85
PGPDecryptionErrorClass · 0.85
bytes_to_intMethod · 0.80
derive_keyMethod · 0.45
newMethod · 0.45

Tested by

no test coverage detected