예제 #1
0
    def testStreamingCBCEncryptor(self):
        key = rdf_crypto.AES128Key.GenerateKey()
        iv = rdf_crypto.AES128Key.GenerateKey()
        # 160 characters.
        message = b"Hello World!!!!!" * 10

        for plaintext, partitions in [
            (message, [
                [160],
                [80, 80],
                [75, 75, 10],
                [1, 159],
                [10] * 16,
                [1] * 160,
            ]),
                # Prime length, not a multiple of blocksize.
            (message[:149], [
                [149],
                [80, 69],
                [75, 55, 19],
                [1, 148],
                [10] * 14 + [9],
                [1] * 149,
            ])
        ]:
            for partition in partitions:
                cipher = rdf_crypto.AES128CBCCipher(key, iv)
                streaming_cbc = rdf_crypto.StreamingCBCEncryptor(cipher)
                offset = 0
                out = []
                for n in partition:
                    next_partition = plaintext[offset:offset + n]
                    out.append(streaming_cbc.Update(next_partition))
                    offset += n
                out.append(streaming_cbc.Finalize())

                self.assertEqual(cipher.Decrypt(b"".join(out)), plaintext)
예제 #2
0
    def Run(self, args):
        """Run."""

        # Open the file.
        fd = vfs.VFSOpen(args.pathspec, progress_callback=self.Progress)

        if args.address_family == rdf_client_network.NetworkAddress.Family.INET:
            family = socket.AF_INET
        elif args.address_family == rdf_client_network.NetworkAddress.Family.INET6:
            family = socket.AF_INET6
        else:
            raise RuntimeError("Socket address family not supported.")

        s = socket.socket(family, socket.SOCK_STREAM)

        try:
            s.connect((args.host, args.port))
        except socket.error as e:
            raise RuntimeError(str(e))

        cipher = rdf_crypto.AES128CBCCipher(args.key, args.iv)
        streaming_encryptor = rdf_crypto.StreamingCBCEncryptor(cipher)

        while True:
            data = fd.read(self.BLOCK_SIZE)
            if not data:
                break

            self.Send(s, streaming_encryptor.Update(data))
            # Send heartbeats for long files.
            self.Progress()

        self.Send(s, streaming_encryptor.Finalize())
        s.close()

        self.SendReply(fd.Stat())