示例#1
0
 public static bool AuthenticateFile(string filePath, byte[] macKey, out byte[] storedHash)
 {
     try
     {
         bool tampered = true;
         storedHash = ReadStoredHash(filePath);
         if (storedHash != null)
         {
             byte[] computedHash = new byte[Constants.HashLength];
             using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
             {
                 // Remove the stored MAC from the file before computing the MAC
                 fileStream.SetLength(fileStream.Length - computedHash.Length);
                 MemoryEncryption.DecryptByteArray(ref macKey);
                 computedHash = HashingAlgorithms.Blake2(fileStream, macKey);
                 MemoryEncryption.EncryptByteArray(ref macKey);
             }
             // Invert result
             tampered = !Sodium.Utilities.Compare(storedHash, computedHash);
             if (tampered == true)
             {
                 // Restore the stored MAC
                 AppendHash(filePath, storedHash);
             }
         }
         return(tampered);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(filePath, ex.GetType().Name, "Unable to authenticate the file.");
         storedHash = null;
         return(true);
     }
 }
示例#2
0
        public static byte[] AssociatedData()
        {
            Enum   cipher     = (Cipher)Globals.EncryptionAlgorithm;
            string cipherName = Enum.GetName(cipher.GetType(), cipher);

            return(HashingAlgorithms.Blake2(cipherName));
        }
示例#3
0
 private static byte[] HashPasswordBytes(byte[] passwordBytes)
 {
     byte[] associatedData = Generate.AssociatedData();
     // Combine associated data and password bytes
     passwordBytes = HashingAlgorithms.Blake2(passwordBytes, associatedData);
     MemoryEncryption.EncryptByteArray(ref passwordBytes);
     return(passwordBytes);
 }
示例#4
0
 private static byte[] GetKeyfileBytes(byte[] passwordBytes)
 {
     if (!string.IsNullOrEmpty(Globals.KeyfilePath))
     {
         byte[] keyfileBytes = Keyfiles.ReadKeyfile(Globals.KeyfilePath);
         if (keyfileBytes != null)
         {
             MemoryEncryption.DecryptByteArray(ref passwordBytes);
             // Combine password and keyfile bytes
             passwordBytes = HashingAlgorithms.Blake2(passwordBytes, keyfileBytes);
             MemoryEncryption.EncryptByteArray(ref passwordBytes);
             Utilities.ZeroArray(keyfileBytes);
         }
     }
     return(passwordBytes);
 }
示例#5
0
 private static byte[] ComputeFileHash(string encryptedFilePath, byte[] macKey)
 {
     try
     {
         byte[] computedHash = new byte[Constants.HashLength];
         using (var fileStream = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             MemoryEncryption.DecryptByteArray(ref macKey);
             computedHash = HashingAlgorithms.Blake2(fileStream, macKey);
             MemoryEncryption.EncryptByteArray(ref macKey);
         }
         return(computedHash);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccessExceptions(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.High);
         DisplayMessage.ErrorResultsText(encryptedFilePath, ex.GetType().Name, "Unable to compute MAC.");
         return(null);
     }
 }