summary refs log tree commit diff
path: root/crypto/test/src/security/test/SecureRandomTest.cs
blob: f93afc0aaa0fae01459433f6d828975f8298a499 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Text;

using NUnit.Framework;

using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Security.Tests
{
    [TestFixture]
    public class SecureRandomTest
    {
#if !(NETCF_1_0 || PORTABLE)
        [Test]
        public void TestCryptoApi()
        {
            SecureRandom random = new SecureRandom(
                new CryptoApiRandomGenerator());

            CheckSecureRandom(random);
        }
#endif

        [Test]
        public void TestDefault()
        {
            SecureRandom random = new SecureRandom();

            CheckSecureRandom(random);
        }

        [Test]
        public void TestSha1Prng()
        {
            SecureRandom random = SecureRandom.GetInstance("SHA1PRNG");

            CheckSecureRandom(random);
        }

        [Test]
        public void TestSha1PrngBackward()
        {
            byte[] seed = Encoding.ASCII.GetBytes("backward compatible");

            SecureRandom sx = new SecureRandom(seed);
            SecureRandom sy = SecureRandom.GetInstance("SHA1PRNG", false); sy.SetSeed(seed);

            byte[] bx = new byte[128]; sx.NextBytes(bx);
            byte[] by = new byte[128]; sy.NextBytes(by);

            Assert.IsTrue(Arrays.AreEqual(bx, by));
        }

        [Test]
        public void TestSha256Prng()
        {
            SecureRandom random = SecureRandom.GetInstance("SHA256PRNG");

            CheckSecureRandom(random);
        }

        [Test]
        public void TestThreadedSeed()
        {
            SecureRandom random = SecureRandom.GetInstance("SHA1PRNG", false);
            random.SetSeed(new ThreadedSeedGenerator().GenerateSeed(20, false));

            CheckSecureRandom(random);
        }

        [Test]
        public void TestVmpcPrng()
        {
            SecureRandom random = new SecureRandom(new VmpcRandomGenerator());
            random.SetSeed(SecureRandom.GetSeed(32));

            CheckSecureRandom(random);
        }


        private static void CheckSecureRandom(SecureRandom random)
        {
            // Note: This will periodically (< 1e-6 probability) give a false alarm.
            // That's randomness for you!
            Assert.IsTrue(RunChiSquaredTests(random), "Chi2 test detected possible non-randomness");
        }

        private static bool RunChiSquaredTests(SecureRandom random)
        {
            int passes = 0;

            for (int tries = 0; tries < 100; ++tries)
            {
                double chi2 = MeasureChiSquared(random, 1000);

                // 255 degrees of freedom in test => Q ~ 10.0% for 285
                if (chi2 < 285.0)
                {
                    ++passes;
                }
            }

            return passes > 75;
        }

        private static double MeasureChiSquared(SecureRandom random, int rounds)
        {
            byte[] opts = SecureRandom.GetSeed(2);
            int[] counts = new int[256];

            byte[] bs = new byte[256];
            for (int i = 0; i < rounds; ++i)
            {
                random.NextBytes(bs);

                for (int b = 0; b < 256; ++b)
                {
                    ++counts[bs[b]];
                }
            }

            byte mask = opts[0];
            for (int i = 0; i < rounds; ++i)
            {
                random.NextBytes(bs);

                for (int b = 0; b < 256; ++b)
                {
                    ++counts[bs[b] ^ mask];
                }

                ++mask;
            }

            byte shift = opts[1];
            for (int i = 0; i < rounds; ++i)
            {
                random.NextBytes(bs);

                for (int b = 0; b < 256; ++b)
                {
                    ++counts[(byte)(bs[b] + shift)];
                }

                ++shift;
            }

            int total = 3 * rounds;

            double chi2 = 0;
            for (int k = 0; k < counts.Length; ++k)
            {
                double diff = ((double) counts[k]) - total;
                double diff2 = diff * diff;

                chi2 += diff2;
            }

            chi2 /= total;

            return chi2;
        }
    }
}