Example #1
0
 def testD03RSA(self):
     """crypto.cipher: decrypt_symmetric() v3 RSA secret key (RSA_d,p,q,u)"""
     rsasec_armored = read_test_file([
         'pgpfiles', 'interop', 'pgp6.5.3', 'RSA1',
         'key.pgp6.5.3.RSA1.sec.asc'
     ])
     rsanopass_armored = read_test_file([
         'pgpfiles', 'interop', 'pgp6.5.3', 'RSA1',
         'key.pgp6.5.3.RSA1.sec.nopass.asc'
     ])
     rsasec_d, rsanopass_d = list_armored(
         rsasec_armored)[0].data, list_armored(rsanopass_armored)[0].data
     pkts, nopasspkts = list_pkts(rsasec_d), list_pkts(rsanopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     # for the future..
     if secretkey.s2k_usg in [254, 255]:  # we have an s2k
         key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     else:
         import md5
         key = md5.new(passphrase).digest()
     # Just comparing bytes, not integer values. The funky notation
     # 'enc_RSA_d_d', 'enc_RSA_p_d' means "the encrypted *integer*
     # data from the RSA d and p # MPIs, respectively.
     # 'len_RSA_d_d' means "the octet length of the integer portion
     # of the RSA d MPI."
     idx = 0
     # RSA_d
     len_RSA_d_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_d_d = secretkey._enc_d[idx:idx + len_RSA_d_d]
     idx = idx + len_RSA_d_d
     iv = secretkey.iv  # iv provided
     RSA_d_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_d_d, iv)
     self.assertEqual(RSA_d_d, nopasskey.RSA_d._int_d)
     # RSA_p
     len_RSA_p_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_p_d = secretkey._enc_d[idx:idx + len_RSA_p_d]
     idx = idx + len_RSA_p_d
     iv = enc_RSA_d_d[-8:]  # last 8 octets from "pre-sync" instream
     RSA_p_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_p_d, iv)
     self.assertEqual(RSA_p_d, nopasskey.RSA_p._int_d)
     # RSA_q
     len_RSA_q_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_q_d = secretkey._enc_d[idx:idx + len_RSA_q_d]
     idx = idx + len_RSA_q_d
     iv = enc_RSA_p_d[-8:]  # last 8 octets from "pre-sync" instream
     RSA_q_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_q_d, iv)
     self.assertEqual(RSA_q_d, nopasskey.RSA_q._int_d)
     # RSA_u
     len_RSA_u_d = mpilen2int(secretkey._enc_d[idx:idx + 2])
     idx = idx + 2
     enc_RSA_u_d = secretkey._enc_d[idx:idx + len_RSA_u_d]
     idx = idx + len_RSA_u_d
     iv = enc_RSA_q_d[-8:]  # last 8 octets from "pre-sync" instream
     RSA_u_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_u_d, iv)
     self.assertEqual(RSA_u_d, nopasskey.RSA_u._int_d)
 def testD03RSA(self):
     """crypto.cipher: decrypt_symmetric() v3 RSA secret key (RSA_d,p,q,u)"""
     rsasec_armored = read_test_file(['pgpfiles','interop','pgp6.5.3','RSA1','key.pgp6.5.3.RSA1.sec.asc'])
     rsanopass_armored = read_test_file(['pgpfiles','interop','pgp6.5.3','RSA1','key.pgp6.5.3.RSA1.sec.nopass.asc'])
     rsasec_d, rsanopass_d = list_armored(rsasec_armored)[0].data, list_armored(rsanopass_armored)[0].data
     pkts, nopasspkts = list_pkts(rsasec_d), list_pkts(rsanopass_d)
     secretkey_pkt, nopasskey_pkt = pkts[0], nopasspkts[0]
     secretkey, nopasskey = secretkey_pkt.body, nopasskey_pkt.body
     passphrase = 'test'
     # for the future..
     if secretkey.s2k_usg in [254, 255]: # we have an s2k
         key = string2key(secretkey.s2k, secretkey.alg_sym, passphrase)
     else:
         import md5
         key = md5.new(passphrase).digest()
     # Just comparing bytes, not integer values. The funky notation
     # 'enc_RSA_d_d', 'enc_RSA_p_d' means "the encrypted *integer*
     # data from the RSA d and p # MPIs, respectively.
     # 'len_RSA_d_d' means "the octet length of the integer portion
     # of the RSA d MPI."
     idx = 0
     # RSA_d
     len_RSA_d_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_d_d = secretkey._enc_d[idx:idx+len_RSA_d_d]
     idx = idx + len_RSA_d_d
     iv = secretkey.iv # iv provided
     RSA_d_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_d_d, iv)
     self.assertEqual(RSA_d_d, nopasskey.RSA_d._int_d)
     # RSA_p
     len_RSA_p_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_p_d = secretkey._enc_d[idx:idx+len_RSA_p_d]
     idx = idx + len_RSA_p_d
     iv = enc_RSA_d_d[-8:] # last 8 octets from "pre-sync" instream
     RSA_p_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_p_d, iv)
     self.assertEqual(RSA_p_d, nopasskey.RSA_p._int_d)
     # RSA_q
     len_RSA_q_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_q_d = secretkey._enc_d[idx:idx+len_RSA_q_d]
     idx = idx + len_RSA_q_d
     iv = enc_RSA_p_d[-8:] # last 8 octets from "pre-sync" instream
     RSA_q_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_q_d, iv)
     self.assertEqual(RSA_q_d, nopasskey.RSA_q._int_d)
     # RSA_u
     len_RSA_u_d = mpilen2int(secretkey._enc_d[idx:idx+2])
     idx = idx + 2
     enc_RSA_u_d = secretkey._enc_d[idx:idx+len_RSA_u_d]
     idx = idx + len_RSA_u_d
     iv = enc_RSA_q_d[-8:] # last 8 octets from "pre-sync" instream
     RSA_u_d = decrypt_symmetric(secretkey.alg_sym, key, enc_RSA_u_d, iv)
     self.assertEqual(RSA_u_d, nopasskey.RSA_u._int_d)
Example #3
0
    def fill(self, d):
        import struct
        idx = 0
        self._length_d, idx = STN.strcalc(None, d[idx:idx+2], idx)

        self.bit_length = STN.str2int(self._length_d)
        self.length = STN.mpilen2int(self._length_d)

        self._d = d[0:2+self.length]
        self._int_d = d[idx:idx+self.length]
        self.size = len(self._d) # explicitly..

        i = struct.unpack('>'+str(len(self._int_d))+'s', self._int_d)[0]
        self.value = STN.str2int(i) 

        if self.check():
            pass
        else:
            raise self.err[0], self.err[1]
Example #4
0
    def fill(self, d):
        import struct
        idx = 0
        self._length_d, idx = STN.strcalc(None, d[idx:idx + 2], idx)

        self.bit_length = STN.str2int(self._length_d)
        self.length = STN.mpilen2int(self._length_d)

        self._d = d[0:2 + self.length]
        self._int_d = d[idx:idx + self.length]
        self.size = len(self._d)  # explicitly..

        i = struct.unpack('>' + str(len(self._int_d)) + 's', self._int_d)[0]
        self.value = STN.str2int(i)

        if self.check():
            pass
        else:
            raise self.err[0], self.err[1]
Example #5
0
def str2mpi(s, limit):
    """Convert a MPI byte-string into an integer.

    :Parameters:
        - `s`: MPI string
        - `limit`: *optional* maximum number of MPIs to search for

    :Returns: list of MPI values (integers/longs)
    """
    import struct

    idx = 0
    mpi_list = []

    while True:

        if limit and limit == len(mpi_list):
            break

        mpilen_d, idx = read_slice(s, 2, idx)

        if 2 == len(mpilen_d):
            # we don't care about bit length, just the bytes containing them
            mpilen = mpilen2int(mpilen_d)
            int_d, idx = read_slice(s, mpilen, idx)
            len_int_d = len(int_d)

            if len_int_d == mpilen:
                i = struct.unpack('>' + str(len_int_d) + 's', int_d)[0]
                mpival = str2int(i)
                mpi_list.append(mpival)

                continue  # if good, keep going

        break  # otherwise quit

    return mpi_list, idx