コード例 #1
0
ファイル: CryptoRandom.cs プロジェクト: mauron85/KeePassLib
        private static byte[] GetSystemData()
        {
            MemoryStream ms = new MemoryStream();

            byte[] pb;

            pb = MemUtil.Int32ToBytes(Environment.TickCount);
            MemUtil.Write(ms, pb);

            pb = MemUtil.Int64ToBytes(DateTime.UtcNow.ToBinary());
            MemUtil.Write(ms, pb);

#if !KeePassLibSD && !KeePassUWP
            // In try-catch for systems without GUI;
            // https://sourceforge.net/p/keepass/discussion/329221/thread/20335b73/
            try
            {
                Point pt = Cursor.Position;
                pb = MemUtil.Int32ToBytes(pt.X);
                MemUtil.Write(ms, pb);
                pb = MemUtil.Int32ToBytes(pt.Y);
                MemUtil.Write(ms, pb);
            }
            catch (Exception) { }
#endif

            pb = MemUtil.UInt32ToBytes((uint)NativeLib.GetPlatformID());
            MemUtil.Write(ms, pb);

            try
            {
#if KeePassUWP
                string strOS = InfoUtil.SystemVersion;
#elif KeePassUAP
                string strOS = EnvironmentExt.OSVersion.VersionString;
#else
                string strOS = Environment.OSVersion.VersionString;
#endif
                AddStrHash(ms, strOS);

                pb = MemUtil.Int32ToBytes(Environment.ProcessorCount);
                MemUtil.Write(ms, pb);

#if !KeePassUAP && !KeePassUWP
                AddStrHash(ms, Environment.CommandLine);

                pb = MemUtil.Int64ToBytes(Environment.WorkingSet);
                MemUtil.Write(ms, pb);
#endif
            }
            catch (Exception) { Debug.Assert(false); }

            try
            {
                foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
                {
                    AddStrHash(ms, (de.Key as string));
                    AddStrHash(ms, (de.Value as string));
                }
            }
            catch (Exception) { Debug.Assert(false); }

#if KeePassUWP
            // TODO: check process entropy
#elif KeePassUAP
            pb = DiagnosticsExt.GetProcessEntropy();
            MemUtil.Write(ms, pb);
#elif !KeePassLibSD
            try
            {
                using (Process p = Process.GetCurrentProcess())
                {
                    pb = MemUtil.Int64ToBytes(p.Handle.ToInt64());
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int32ToBytes(p.HandleCount);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int32ToBytes(p.Id);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.NonpagedSystemMemorySize64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.PagedMemorySize64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.PagedSystemMemorySize64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.PeakPagedMemorySize64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.PeakVirtualMemorySize64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.PeakWorkingSet64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.PrivateMemorySize64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.StartTime.ToBinary());
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.VirtualMemorySize64);
                    MemUtil.Write(ms, pb);
                    pb = MemUtil.Int64ToBytes(p.WorkingSet64);
                    MemUtil.Write(ms, pb);

                    // Not supported in Mono 1.2.6:
                    // pb = MemUtil.UInt32ToBytes((uint)p.SessionId);
                    // MemUtil.Write(ms, pb);
                }
            }
            catch (Exception) { Debug.Assert(NativeLib.IsUnix()); }
#endif

            try
            {
                CultureInfo ci = CultureInfo.CurrentCulture;
                if (ci != null)
                {
                    pb = MemUtil.Int32ToBytes(ci.GetHashCode());
                    MemUtil.Write(ms, pb);
                }
                else
                {
                    Debug.Assert(false);
                }
            }
            catch (Exception) { Debug.Assert(false); }

            pb = Guid.NewGuid().ToByteArray();
            MemUtil.Write(ms, pb);

            byte[] pbAll = ms.ToArray();
#if KeePassUWP
            ms.Dispose();
#else
            ms.Close();
#endif
            return(pbAll);
        }
コード例 #2
0
        private byte[] GetSystemEntropy()
        {
            SHA512Managed h = new SHA512Managed();

            byte[] pb4 = new byte[4];
            byte[] pb8 = new byte[8];

            GAction <byte[], bool> f = delegate(byte[] pbValue, bool bClearValue)
            {
                if (pbValue == null)
                {
                    Debug.Assert(false); return;
                }
                if (pbValue.Length == 0)
                {
                    return;
                }
                h.TransformBlock(pbValue, 0, pbValue.Length, pbValue, 0);
                if (bClearValue)
                {
                    MemUtil.ZeroByteArray(pbValue);
                }
            };
            Action <int> fI32 = delegate(int iValue)
            {
                MemUtil.Int32ToBytesEx(iValue, pb4, 0);
                f(pb4, false);
            };
            Action <long> fI64 = delegate(long lValue)
            {
                MemUtil.Int64ToBytesEx(lValue, pb8, 0);
                f(pb8, false);
            };
            Action <string> fStr = delegate(string strValue)
            {
                if (strValue == null)
                {
                    Debug.Assert(false); return;
                }
                if (strValue.Length == 0)
                {
                    return;
                }
                f(StrUtil.Utf8.GetBytes(strValue), false);
            };

            fI32(Environment.TickCount);
            fI64(DateTime.UtcNow.ToBinary());

#if !KeePassLibSD && !NETSTANDARD2_0
            // In try-catch for systems without GUI;
            // https://sourceforge.net/p/keepass/discussion/329221/thread/20335b73/
            try
            {
                Point pt = Cursor.Position;
                fI32(pt.X);
                fI32(pt.Y);
            }
            catch (Exception) { Debug.Assert(NativeLib.IsUnix()); }
#endif

            try
            {
                fI32((int)NativeLib.GetPlatformID());
#if KeePassUAP
                fStr(EnvironmentExt.OSVersion.VersionString);
#else
                fStr(Environment.OSVersion.VersionString);
#endif

                fI32(Environment.ProcessorCount);

#if !KeePassUAP && !NETSTANDARD2_0
                fStr(Environment.CommandLine);
                fI64(Environment.WorkingSet);
#endif
            }
            catch (Exception) { Debug.Assert(false); }

            try
            {
                foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
                {
                    fStr(de.Key as string);
                    fStr(de.Value as string);
                }
            }
            catch (Exception) { Debug.Assert(false); }

            try
            {
#if KeePassUAP
                f(DiagnosticsExt.GetProcessEntropy(), true);
#elif !KeePassLibSD && !NETSTANDARD2_0
                using (Process p = Process.GetCurrentProcess())
                {
                    fI64(p.Handle.ToInt64());
                    fI32(p.HandleCount);
                    fI32(p.Id);
                    fI64(p.NonpagedSystemMemorySize64);
                    fI64(p.PagedMemorySize64);
                    fI64(p.PagedSystemMemorySize64);
                    fI64(p.PeakPagedMemorySize64);
                    fI64(p.PeakVirtualMemorySize64);
                    fI64(p.PeakWorkingSet64);
                    fI64(p.PrivateMemorySize64);
                    fI64(p.StartTime.ToBinary());
                    fI64(p.VirtualMemorySize64);
                    fI64(p.WorkingSet64);

                    // Not supported in Mono 1.2.6:
                    // fI32(p.SessionId);
                }
#endif
            }
            catch (Exception) { Debug.Assert(NativeLib.IsUnix()); }

            try
            {
                CultureInfo ci = CultureInfo.CurrentCulture;
                if (ci != null)
                {
                    fI32(ci.GetHashCode());
                }
                else
                {
                    Debug.Assert(false);
                }
            }
            catch (Exception) { Debug.Assert(false); }

            f(Guid.NewGuid().ToByteArray(), false);
            f(GetCspRandom(), true);

            h.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
            byte[] pbHash = h.Hash;
            h.Clear();
            MemUtil.ZeroByteArray(pb4);
            MemUtil.ZeroByteArray(pb8);
            return(pbHash);
        }
コード例 #3
0
        private static byte[] GetSystemData(Random rWeak)
        {
            MemoryStream ms = new MemoryStream();

            byte[] pb;

            pb = MemUtil.UInt32ToBytes((uint)Environment.TickCount);
            ms.Write(pb, 0, pb.Length);

            pb = TimeUtil.PackTime(DateTime.Now);
            ms.Write(pb, 0, pb.Length);

#if !KeePassLibSD && !KeePassUAP
            // In try-catch for systems without GUI;
            // https://sourceforge.net/p/keepass/discussion/329221/thread/20335b73/
            try
            {
                Point pt = Cursor.Position;
                pb = MemUtil.UInt32ToBytes((uint)pt.X);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt32ToBytes((uint)pt.Y);
                ms.Write(pb, 0, pb.Length);
            }
            catch (Exception) { }
#endif

            pb = MemUtil.UInt32ToBytes((uint)rWeak.Next());
            ms.Write(pb, 0, pb.Length);

#if FEATURE_NATIVELIBS
            pb = MemUtil.UInt32ToBytes((uint)NativeLib.GetPlatformID());
            ms.Write(pb, 0, pb.Length);
#endif

            try
            {
                pb = MemUtil.UInt32ToBytes((uint)Environment.ProcessorCount);
                ms.Write(pb, 0, pb.Length);

#if !KeePassUAP
                Version v = Environment.OSVersion.Version;
                pb = MemUtil.UInt32ToBytes((uint)v.GetHashCode());
                ms.Write(pb, 0, pb.Length);

                pb = MemUtil.UInt64ToBytes((ulong)Environment.WorkingSet);
                ms.Write(pb, 0, pb.Length);
#endif
            }
            catch (Exception) { Debug.Assert(false); }

#if KeePassUAP
#if FALSE
            pb = DiagnosticsExt.GetProcessEntropy();
            ms.Write(pb, 0, pb.Length);
#endif
#elif !KeePassLibSD
            Process p = null;
            try
            {
                p = Process.GetCurrentProcess();

                pb = MemUtil.UInt64ToBytes((ulong)p.Handle.ToInt64());
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt32ToBytes((uint)p.HandleCount);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt32ToBytes((uint)p.Id);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.NonpagedSystemMemorySize64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.PagedMemorySize64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.PagedSystemMemorySize64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.PeakPagedMemorySize64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.PeakVirtualMemorySize64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.PeakWorkingSet64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.PrivateMemorySize64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.StartTime.ToBinary());
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.VirtualMemorySize64);
                ms.Write(pb, 0, pb.Length);
                pb = MemUtil.UInt64ToBytes((ulong)p.WorkingSet64);
                ms.Write(pb, 0, pb.Length);

                // Not supported in Mono 1.2.6:
                // pb = MemUtil.UInt32ToBytes((uint)p.SessionId);
                // ms.Write(pb, 0, pb.Length);
            }
#if FEATURE_NATIVELIBS
            catch (Exception) { Debug.Assert(NativeLib.IsUnix()); }
#endif
            finally
            {
                try { if (p != null)
                      {
                          p.Dispose();
                      }
                }
                catch (Exception) { Debug.Assert(false); }
            }
#endif

            pb = Guid.NewGuid().ToByteArray();
            ms.Write(pb, 0, pb.Length);

            byte[] pbAll = ms.ToArray();
            ms.Close();
            return(pbAll);
        }