def __init__(self, s=None, length=0, passphraseCallback=None): PublicKeyPacket.__init__(self, s) self.passphrase = None if s is None: return self.s2kUsage = ord(s.read(1)) if self.s2kUsage == 255 or self.s2kUsage == 254: self.symAlgorithm = ord(s.read(1)) self.s2k = S2KElement(s) self.iv = s.read(crypto.SYMALGORITHM_BLOCKSIZE[self.symAlgorithm]) encrypted = s.read(length - s.tell() + 3) if passphraseCallback is None: raise Exception('encrypted key and no passphraseCallback') symkey = self.s2k.generateKey(passphraseCallback(), self.symAlgorithm) decrypted = io.BytesIO(crypto.decryptCFB(symkey, encrypted, self.iv, self.symAlgorithm)) self.d = MPIElement(decrypted) self.p = MPIElement(decrypted) self.q = MPIElement(decrypted) self.u = MPIElement(decrypted) if self.s2kUsage == 254: self.checksum = decrypted.read(20) if self.checksum != crypto.hash_sha1(decrypted.getvalue()[:-20]): raise Exception('invalid passphrase') else: raise Exception('not implemented') elif self.s2kUsage == 0: self.d = MPIElement(s) self.p = MPIElement(s) self.q = MPIElement(s) self.u = MPIElement(s) self.checksum = s.read(2) else: raise Exception('not implemented')
def testCFB(self): key = 'ebb7109b9203ce8570722a947d548913'.decode('hex') iv = '4b31d8f203ffc5d6'.decode('hex') c = ('347107356c024871153410cb3c87231ac3814599df4e9e5d4bcd21ed5ac5c3e0ea' '65d674752c1e16410e84426ceaa2d741ecaa794ceedba347ac64e6909dffb8b995' 'b1282bcf6e1d6c40d2251e2eda9e0db766ad5ba188a7b6eca241967535e4196152' '91725470c6d0c6170f99f812f498327d5bd70230cc759ca56e56b4816c38756b60' 'eb5ce1ec09c53a069214f079af242904aceb0ee7bcc9e2640e7a34c9aba22697bd' '7cc42ac1bcb8c914622f096e8eda6195e18a24ee2c90965d7c7262077977116dfd' 'b2cc1ae77da9be2437502e44b497d4ec8ae90915e562ecccf66e3b355cfb4ceffa' 'af86f1d202fd4d6826e77a4d21134f9577895658f65befdf7b9eaf3e98dff19072' '658a2ca4b0ab5d859354dd1c7d68c8582e8b53eb884df964de54f87453141ccd7b' '712eafbc0d9db4f16880996aab56a39e64258d9cd9f28d9cf7e82396fe0582666e' '0215e185d102464ff19ad5fb55ed9f1ea85bd659c91a728ed74bbe56de0fbb7352' '1f2c69797a2c692a1b2a4f3a87fd5a9749f37dcd9d00c0e95b21a708e91bddef94' 'd0e68da26fbbdf4793603e9bd0baa71d77d8e556d7b8f59d8ba544c76574e36d31' '015b3ae72ea05e6e2a1d410e1af0ce6b75aa79f8ba84e50caeff68936703485990' '54928aecb2443f988be5ac3b0eb9cd87b3448ef24811b928c1b6ec0a7614b25480' 'aaa530d51b6be969aa386c203a114b9a9679eb1a2d6a3c4f304c69930b2ea76f3d' 'a6cc53cfea7c76823b259df7174cc58c5c90ac91c570249326197aa336601e052d' '25b8de75032e7c4bd71e6a8732501eb12be91190699242e4f2b1aa53c23994bfc7' 'b7ac6f36e20aca984bf885e943178cf7174336ad0183037d531c4c128f18b8707d' 'd3b75a308d75ced1c226a1a10d7604d1934721342069d2dc7712fd1c25ab4ed432' '4342f6e7db673ffe').decode('hex') p = ('07ff654f375aacf466501f4bc069519586cde3cfc5ac36b8167e1055bef9932631' 'c881852544e2b78b3d56871a330c100195902255625cc8a44b8a26bd98967380df' '25f3ac0d80cf0b3be05b17874ad753823f6c8447dee76e1fb1bd78ffec188c6af8' 'cd1f3f4acb30b4c7fea7766d00a828665aef1c3cfe610d7039f1179900804597fa' '411e48b220acbf6f72a710ac0669beee5b6e634578b4e59532ac7b1be45164dd88' '80b6f397390ad7f6a0a7db48fe695d8e4a956dcb8d9678b8017a1feb73f224fb7a' '9383523b38656cdfdd9462cb4c5efb33d04023ea7b16bfb3a5c9f4e35159a3df0d' '9a474b3d4db0bfb6a309facbb52c0b82a5e0e7e6d40b079523c1190400ee7a03e6' 'ec4e938a8744faa392d79dd9dcbb45a5d14317dbac8f4f8f009a84ed9fe334d993' 'df4e044fb4d88af1509ffc7b449563f20c5346a08ca397ea7510bcb113069df0bb' 'dfe84fd81d042f351b1740590a43315c44f6cda43fdce55e61fd633ca116f9f153' 'ddd41baac427eba7c521f8fc2e9d31510b019de2f81b145a670400f993751c5f58' '9d1731b7107759129f950ce18fe40836d648cd526aae001ed27238cd177a95376b' '1dda533f361fdb869d86c8c50d18514e09c450621909112ba0c2b12b5b16ce63f6' '721b4d550e29abdbbdc0b22abbe3298f9244eef2ab3d7827490a04999c7e1c4525' '545a9225e5fff09d63b1f6395cbe80f1e4126593fd4c4f03ff4fd0d21a408b87ab' '9181b4754a5c3bb8c63381b3a5307097b5e6151bbeb248d79707f34faac2d02024' '20d7f76ea14e55abf8e0613642620076503e644481bf6ff6b6e736b1fe5455da4b' '896b50770f67756ea3d65ba026ccf46807ad9d214585a6f883760baef2b4c40c25' '08fa8b380a8de16a2900996e3cd793e380ae263f3983e7cbdec37f837bf3659788' '6152e9f28a5ae5ec').decode('hex') self.assertEqual(crypto.decryptCFB(key, c, iv, crypto.SYMALGORITHM_CAST5), p) m = 'Foobarrr' key = '1234567890123456' iv = '00000000' c = crypto.encryptCFB(key, m, iv, crypto.SYMALGORITHM_CAST5) self.assertEqual(crypto.decryptCFB(key, c, iv, crypto.SYMALGORITHM_CAST5), m) m = 'FoobarrrFoobarrr' key = '1234567890123456' iv = '00000000' c = crypto.encryptCFB(key, m, iv, crypto.SYMALGORITHM_CAST5) self.assertEqual(crypto.decryptCFB(key, c, iv, crypto.SYMALGORITHM_CAST5), m) m = 'FoobarrrFoobarrr1234' key = '1234567890123456' iv = '00000000' c = crypto.encryptCFB(key, m, iv, crypto.SYMALGORITHM_CAST5) self.assertEqual(crypto.decryptCFB(key, c, iv, crypto.SYMALGORITHM_CAST5), m) key = '1234567890123456' self.assertEqual(crypto.decryptCFB(key, 16*'c', 16*'0', crypto.SYMALGORITHM_AES128), 'f0df6cc895ab3dfd7f30b4ebe6545ea4'.decode('hex')) self.assertEqual(crypto.decryptCFB(key, 18*'c', 16*'0', crypto.SYMALGORITHM_AES128), 'f0df6cc895ab3dfd7f30b4ebe6545ea442ec'.decode('hex'))