Example #1
0
        protected override bool ReadImageFromDeviceWorker(ulong sectorSize, ulong numSectors)
        {
            Stopwatch sw = new Stopwatch();
            Stopwatch percentStopwatch = new Stopwatch();

            byte[] deviceData            = new byte[1024 * sectorSize];
            ulong  totalBytesReaded      = 0;
            ulong  bytesReaded           = 0;
            ulong  bytesToRead           = sectorSize * numSectors;
            ulong  bytesReadedPerPercent = 0;
            int    lastProgress          = 0;
            int    progress = 0;
            int    readed   = 0;

            sw.Start();
            percentStopwatch.Start();

            for (ulong i = 0; i < numSectors; i += 1024)
            {
                if (cancelPending)
                {
                    return(false);
                }

                readed = NativeDiskWrapper.ReadSectorDataFromHandle(deviceHandles[0], deviceData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);
                NativeDiskWrapper.WriteSectorDataToHandle(fileHandle, deviceData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize, readed);

                totalBytesReaded      += (ulong)readed;
                bytesReaded           += (ulong)readed;
                bytesReadedPerPercent += (ulong)readed;
                bytesToRead           -= (ulong)readed;

                progress = (int)(i / (numSectors / 100.0)) + 1;

                if (progress != lastProgress)
                {
                    ulong averageBps = (ulong)(bytesReadedPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                    OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                    lastProgress          = progress;
                    bytesReadedPerPercent = 0;
                    percentStopwatch.Restart();
                }

                if (sw.ElapsedMilliseconds >= 1000)
                {
                    ulong averageBps = (ulong)(bytesReaded / (sw.ElapsedMilliseconds / 1000.0));
                    OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesReaded, bytesToRead));
                    bytesReaded = 0;
                    sw.Restart();
                }
            }

            return(true);
        }
Example #2
0
        protected async Task <bool> VerifyImageAndDeviceCryptoWorkerAsync(IntPtr fileHandle, ulong sectorSize, ulong numSectors)
        {
            byte[]              fileData                = new byte[1024 * sectorSize];
            Stopwatch           msStopwatch             = new Stopwatch();
            Stopwatch           percentStopwatch        = new Stopwatch();
            ulong               totalBytesVerified      = 0;
            ulong               bytesVerified           = 0;
            ulong               bytesToVerify           = sectorSize * numSectors;
            ulong               bytesVerifiedPerPercent = 0;
            int                 lastProgress            = 0;
            int                 progress                = 0;
            int                 readed   = 0;
            List <Task <bool> > taskList = new List <Task <bool> >(deviceHandles.Length);

            byte[][] deviceData        = new byte[deviceHandles.Length][];
            int      failedDeviceIndex = 0;

            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            byte[] salt          = new byte[32];

            for (int i = 0; i < deviceHandles.Length; i++)
            {
                deviceData[i] = new byte[1024 * sectorSize];
            }

            msStopwatch.Start();
            percentStopwatch.Start();

            using (FileStream inputFile = new FileStream(new SafeFileHandle(fileHandle, false), FileAccess.Read))
                using (RijndaelManaged rijndael = new RijndaelManaged())
                {
                    inputFile.Seek(0, SeekOrigin.Begin);
                    inputFile.Read(salt, 0, encryptionSignature.Length);
                    inputFile.Read(salt, 0, salt.Length);

                    rijndael.KeySize   = 256;
                    rijndael.BlockSize = 128;

                    using (var key = new Rfc2898DeriveBytes(passwordBytes, salt, 1000))
                    {
                        rijndael.Key = key.GetBytes(rijndael.KeySize / 8);
                        rijndael.IV  = key.GetBytes(rijndael.BlockSize / 8);
                    }

                    rijndael.Padding = PaddingMode.Zeros;
                    rijndael.Mode    = CipherMode.CFB;

                    using (CryptoStream cs = new CryptoStream(inputFile, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        int passLen = cs.ReadByte();
                        cs.Read(fileData, 0, passLen);

                        for (ulong i = 0; i < numSectors; i += 1024)
                        {
                            taskList.Clear();

                            if (cancelPending)
                            {
                                return(false);
                            }

                            readed = cs.Read(fileData, 0, fileData.Length);

                            for (int x = 0; x < deviceHandles.Length; x++)
                            {
                                int index = x;
                                taskList.Add(Task.Run(() =>
                                {
                                    NativeDiskWrapper.ReadSectorDataFromHandle(deviceHandles[index], deviceData[index], i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);

                                    if (!NativeDiskWrapper.ByteArrayCompare(fileData, deviceData[index]))
                                    {
                                        failedDeviceIndex = index;
                                        return(false);
                                    }
                                    else
                                    {
                                        return(true);
                                    }
                                }));
                            }

                            await Task.WhenAll(taskList);

                            foreach (var task in taskList)
                            {
                                if (!task.Result)
                                {
                                    for (ulong x = 0; x < 1024 * sectorSize; x++)
                                    {
                                        if (deviceData[failedDeviceIndex][x] != fileData[x])
                                        {
                                            throw new Exception(string.Format("Verify found different data. Device {0}:\\ at byte {1:n0}, file data: 0x{2:X2}, device data: 0x{3:X2}", DriveLetters[failedDeviceIndex], i * sectorSize + x, deviceData[failedDeviceIndex][x], fileData[x]));
                                        }
                                    }
                                    return(false);
                                }
                            }

                            totalBytesVerified      += (ulong)readed;
                            bytesVerified           += (ulong)readed;
                            bytesVerifiedPerPercent += (ulong)readed;
                            bytesToVerify           -= (ulong)readed;

                            progress = (int)(i / (numSectors / 100.0)) + 1;

                            if (progress != lastProgress)
                            {
                                ulong averageBps = (ulong)(bytesVerifiedPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                                OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                                lastProgress            = progress;
                                bytesVerifiedPerPercent = 0;
                                percentStopwatch.Restart();
                            }

                            if (msStopwatch.ElapsedMilliseconds >= 1000)
                            {
                                ulong averageBps = (ulong)(bytesVerified / (msStopwatch.ElapsedMilliseconds / 1000.0));
                                OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesVerified, bytesToVerify));
                                bytesVerified = 0;
                                msStopwatch.Restart();
                            }
                        }
                    }
                }

            return(true);
        }
Example #3
0
        protected override async Task <bool> VerifyImageAndDeviceWorkerAsync(IntPtr fileHandle, ulong sectorSize, ulong numSectors)
        {
            byte[]              fileData                = new byte[1024 * sectorSize];
            Stopwatch           msStopwatch             = new Stopwatch();
            Stopwatch           percentStopwatch        = new Stopwatch();
            ulong               totalBytesVerified      = 0;
            ulong               bytesVerified           = 0;
            ulong               bytesToVerify           = sectorSize * numSectors;
            ulong               bytesVerifiedPerPercent = 0;
            int                 lastProgress            = 0;
            int                 progress                = 0;
            int                 readed   = 0;
            List <Task <bool> > taskList = new List <Task <bool> >(deviceHandles.Length);

            byte[][] deviceData        = new byte[deviceHandles.Length][];
            int      failedDeviceIndex = 0;

            for (int i = 0; i < deviceHandles.Length; i++)
            {
                deviceData[i] = new byte[1024 * sectorSize];
            }

            msStopwatch.Start();
            percentStopwatch.Start();

            for (ulong i = 0; i < numSectors; i += 1024)
            {
                taskList.Clear();

                if (cancelPending)
                {
                    return(false);
                }

                readed = NativeDiskWrapper.ReadSectorDataFromHandle(fileHandle, fileData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);
                for (int x = 0; x < deviceHandles.Length; x++)
                {
                    int index = x;
                    taskList.Add(Task.Run(() =>
                    {
                        NativeDiskWrapper.ReadSectorDataFromHandle(deviceHandles[index], deviceData[index], i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);

                        if (!NativeDiskWrapper.ByteArrayCompare(fileData, deviceData[index]))
                        {
                            failedDeviceIndex = index;
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    }));
                }

                await Task.WhenAll(taskList);

                foreach (var task in taskList)
                {
                    if (!task.Result)
                    {
                        for (ulong x = 0; x < 1024 * sectorSize; x++)
                        {
                            if (deviceData[failedDeviceIndex][x] != fileData[x])
                            {
                                throw new Exception(string.Format("Verify found different data. Device {0}:\\ at byte {1:n0}, file data: 0x{2:X2}, device data: 0x{3:X2}", DriveLetters[failedDeviceIndex], i * sectorSize + x, deviceData[failedDeviceIndex][x], fileData[x]));
                            }
                        }
                        return(false);
                    }
                }

                totalBytesVerified      += (ulong)readed;
                bytesVerified           += (ulong)readed;
                bytesVerifiedPerPercent += (ulong)readed;
                bytesToVerify           -= (ulong)readed;

                progress = (int)(i / (numSectors / 100.0)) + 1;

                if (progress != lastProgress)
                {
                    ulong averageBps = (ulong)(bytesVerifiedPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                    OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                    lastProgress            = progress;
                    bytesVerifiedPerPercent = 0;
                    percentStopwatch.Restart();
                }

                if (msStopwatch.ElapsedMilliseconds >= 1000)
                {
                    ulong averageBps = (ulong)(bytesVerified / (msStopwatch.ElapsedMilliseconds / 1000.0));
                    OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesVerified, bytesToVerify));
                    bytesVerified = 0;
                    msStopwatch.Restart();
                }
            }

            return(true);
        }
Example #4
0
        protected async Task <bool> WriteImageToDeviceCryptoWorker(ulong sectorSize, ulong numSectors)
        {
            Stopwatch msStopwatch      = new Stopwatch();
            Stopwatch percentStopwatch = new Stopwatch();

            byte[]      imageData              = new byte[1024 * sectorSize];
            ulong       totalBytesWritten      = 0;
            ulong       bytesWritten           = 0;
            ulong       bytesToWrite           = sectorSize * numSectors;
            ulong       bytesWrittenPerPercent = 0;
            int         lastProgress           = 0;
            int         progress = 0;
            int         readed   = 0;
            List <Task> taskList = new List <Task>(deviceHandles.Length);

            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            byte[] salt          = new byte[32];

            msStopwatch.Start();
            percentStopwatch.Start();

            using (FileStream inputFile = new FileStream(new SafeFileHandle(fileHandle, false), FileAccess.Read))
                using (RijndaelManaged rijndael = new RijndaelManaged())
                {
                    inputFile.Read(salt, 0, encryptionSignature.Length);
                    inputFile.Read(salt, 0, salt.Length);

                    rijndael.KeySize   = 256;
                    rijndael.BlockSize = 128;

                    using (var key = new Rfc2898DeriveBytes(passwordBytes, salt, 1000))
                    {
                        rijndael.Key = key.GetBytes(rijndael.KeySize / 8);
                        rijndael.IV  = key.GetBytes(rijndael.BlockSize / 8);
                    }

                    rijndael.Padding = PaddingMode.Zeros;
                    rijndael.Mode    = CipherMode.CFB;

                    using (CryptoStream cs = new CryptoStream(inputFile, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        int passLen = cs.ReadByte();
                        cs.Read(imageData, 0, passLen);

                        for (ulong i = 0; i < numSectors; i += 1024)
                        {
                            taskList.Clear();

                            if (cancelPending)
                            {
                                return(false);
                            }

                            readed = cs.Read(imageData, 0, imageData.Length);

                            foreach (var deviceHandle in deviceHandles)
                            {
                                taskList.Add(NativeDiskWrapper.WriteSectorDataToHandleAsync(deviceHandle, imageData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize));
                            }

                            await Task.WhenAll(taskList);

                            totalBytesWritten      += (ulong)readed;
                            bytesWritten           += (ulong)readed;
                            bytesWrittenPerPercent += (ulong)readed;
                            bytesToWrite           -= (ulong)readed;

                            progress = (int)(i / (numSectors / 100.0)) + 1;

                            if (progress != lastProgress)
                            {
                                ulong averageBps = (ulong)(bytesWrittenPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                                OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                                lastProgress           = progress;
                                bytesWrittenPerPercent = 0;
                                percentStopwatch.Restart();
                            }

                            if (msStopwatch.ElapsedMilliseconds >= 1000)
                            {
                                ulong averageBps = (ulong)(bytesWritten / (msStopwatch.ElapsedMilliseconds / 1000.0));
                                OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesWritten, bytesToWrite));
                                bytesWritten = 0;
                                msStopwatch.Restart();
                            }
                        }
                    }
                }
            return(true);
        }
Example #5
0
        protected override async Task <bool> WriteImageToDeviceWorker(ulong sectorSize, ulong numSectors)
        {
            Stopwatch msStopwatch      = new Stopwatch();
            Stopwatch percentStopwatch = new Stopwatch();

            byte[]      imageData              = new byte[1024 * sectorSize];
            ulong       totalBytesWritten      = 0;
            ulong       bytesWritten           = 0;
            ulong       bytesToWrite           = sectorSize * numSectors;
            ulong       bytesWrittenPerPercent = 0;
            int         lastProgress           = 0;
            int         progress = 0;
            int         readed   = 0;
            List <Task> taskList = new List <Task>(deviceHandles.Length);

            msStopwatch.Start();
            percentStopwatch.Start();

            for (ulong i = 0; i < numSectors; i += 1024)
            {
                taskList.Clear();

                if (cancelPending)
                {
                    return(false);
                }

                readed = NativeDiskWrapper.ReadSectorDataFromHandle(fileHandle, imageData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);

                foreach (var deviceHandle in deviceHandles)
                {
                    taskList.Add(NativeDiskWrapper.WriteSectorDataToHandleAsync(deviceHandle, imageData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize));
                }

                await Task.WhenAll(taskList);

                totalBytesWritten      += (ulong)readed;
                bytesWritten           += (ulong)readed;
                bytesWrittenPerPercent += (ulong)readed;
                bytesToWrite           -= (ulong)readed;

                progress = (int)(i / (numSectors / 100.0)) + 1;

                if (progress != lastProgress)
                {
                    ulong averageBps = (ulong)(bytesWrittenPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                    OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                    lastProgress           = progress;
                    bytesWrittenPerPercent = 0;
                    percentStopwatch.Restart();
                }

                if (msStopwatch.ElapsedMilliseconds >= 1000)
                {
                    ulong averageBps = (ulong)(bytesWritten / (msStopwatch.ElapsedMilliseconds / 1000.0));
                    OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesWritten, bytesToWrite));
                    bytesWritten = 0;
                    msStopwatch.Restart();
                }
            }

            return(true);
        }
Example #6
0
        protected bool ReadImageFromDeviceCryptoWorker(ulong sectorSize, ulong numSectors)
        {
            Stopwatch sw = new Stopwatch();
            Stopwatch percentStopwatch = new Stopwatch();

            byte[] deviceData            = new byte[1024 * sectorSize];
            ulong  totalBytesReaded      = 0;
            ulong  bytesReaded           = 0;
            ulong  bytesToRead           = sectorSize * numSectors;
            ulong  bytesReadedPerPercent = 0;
            int    lastProgress          = 0;
            int    progress = 0;
            int    readed   = 0;

            sw.Start();
            percentStopwatch.Start();

            using (FileStream fileStream = new FileStream(new SafeFileHandle(fileHandle, false), FileAccess.ReadWrite))
                using (RijndaelManaged rijndael = new RijndaelManaged())
                {
                    byte[] salt          = GenerateRandomSalt();
                    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

                    fileStream.Write(encryptionSignature, 0, encryptionSignature.Length);
                    fileStream.Write(salt, 0, salt.Length);

                    rijndael.KeySize   = 256;
                    rijndael.BlockSize = 128;
                    rijndael.Padding   = PaddingMode.Zeros;

                    using (var key = new Rfc2898DeriveBytes(passwordBytes, salt, Crypto_Iterations))
                    {
                        rijndael.Key = key.GetBytes(rijndael.KeySize / 8);
                        rijndael.IV  = key.GetBytes(rijndael.BlockSize / 8);
                    }
                    rijndael.Mode = CipherMode.CFB;

                    using (CryptoStream cryptoStream = new CryptoStream(fileStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cryptoStream.WriteByte((byte)passwordBytes.Length);
                        cryptoStream.Write(passwordBytes, 0, passwordBytes.Length);

                        for (ulong i = 0; i < numSectors; i += 1024)
                        {
                            if (cancelPending)
                            {
                                return(false);
                            }

                            readed = NativeDiskWrapper.ReadSectorDataFromHandle(deviceHandles[0], deviceData, i, (numSectors - i >= 1024) ? 1024 : (numSectors - i), sectorSize);
                            cryptoStream.Write(deviceData, 0, readed);

                            totalBytesReaded      += (ulong)readed;
                            bytesReaded           += (ulong)readed;
                            bytesReadedPerPercent += (ulong)readed;
                            bytesToRead           -= (ulong)readed;

                            progress = (int)(i / (numSectors / 100.0)) + 1;

                            if (progress != lastProgress)
                            {
                                ulong averageBps = (ulong)(bytesReadedPerPercent / (percentStopwatch.ElapsedMilliseconds / 1000.0));
                                OperationProgressChanged?.Invoke(this, new OperationProgressChangedEventArgs(progress, averageBps, currentDiskOperation));
                                lastProgress          = progress;
                                bytesReadedPerPercent = 0;
                                percentStopwatch.Restart();
                            }

                            if (sw.ElapsedMilliseconds >= 1000)
                            {
                                ulong averageBps = (ulong)(bytesReaded / (sw.ElapsedMilliseconds / 1000.0));
                                OperationProgressReport?.Invoke(this, new OperationProgressReportEventArgs(averageBps, totalBytesReaded, bytesToRead));
                                bytesReaded = 0;
                                sw.Restart();
                            }
                        }
                    }
                }

            return(true);
        }