summaryrefslogtreecommitdiffstats
path: root/Core/Tests/Util/SecureRandomTest.php
blob: a78d5a2099cf97f270bc7739164406352218ca75 (plain)
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
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Tests\Util;

use Symfony\Component\Security\Core\Util\SecureRandom;

/**
 * @group legacy
 */
class SecureRandomTest extends \PHPUnit_Framework_TestCase
{
    /**
     * T1: Monobit test.
     */
    public function testMonobit()
    {
        $secureRandom = new SecureRandom();
        $nbOnBits = substr_count($this->getBitSequence($secureRandom, 20000), '1');
        $this->assertTrue($nbOnBits > 9654 && $nbOnBits < 10346, 'Monobit test failed, number of turned on bits: '.$nbOnBits);
    }

    /**
     * T2: Chi-square test with 15 degrees of freedom (chi-Quadrat-Anpassungstest).
     */
    public function testPoker()
    {
        $secureRandom = new SecureRandom();
        $b = $this->getBitSequence($secureRandom, 20000);
        $c = array();
        for ($i = 0; $i <= 15; ++$i) {
            $c[$i] = 0;
        }

        for ($j = 1; $j <= 5000; ++$j) {
            $k = 4 * $j - 1;
            ++$c[8 * $b[$k - 3] + 4 * $b[$k - 2] + 2 * $b[$k - 1] + $b[$k]];
        }

        $f = 0;
        for ($i = 0; $i <= 15; ++$i) {
            $f += $c[$i] * $c[$i];
        }

        $Y = 16 / 5000 * $f - 5000;

        $this->assertTrue($Y > 1.03 && $Y < 57.4, 'Poker test failed, Y = '.$Y);
    }

    /**
     * Run test.
     */
    public function testRun()
    {
        $secureRandom = new SecureRandom();
        $b = $this->getBitSequence($secureRandom, 20000);

        $runs = array();
        for ($i = 1; $i <= 6; ++$i) {
            $runs[$i] = 0;
        }

        $addRun = function ($run) use (&$runs) {
            if ($run > 6) {
                $run = 6;
            }

            ++$runs[$run];
        };

        $currentRun = 0;
        $lastBit = null;
        for ($i = 0; $i < 20000; ++$i) {
            if ($lastBit === $b[$i]) {
                ++$currentRun;
            } else {
                if ($currentRun > 0) {
                    $addRun($currentRun);
                }

                $lastBit = $b[$i];
                $currentRun = 0;
            }
        }
        if ($currentRun > 0) {
            $addRun($currentRun);
        }

        $this->assertTrue($runs[1] > 2267 && $runs[1] < 2733, 'Runs of length 1 outside of defined interval: '.$runs[1]);
        $this->assertTrue($runs[2] > 1079 && $runs[2] < 1421, 'Runs of length 2 outside of defined interval: '.$runs[2]);
        $this->assertTrue($runs[3] > 502 && $runs[3] < 748, 'Runs of length 3 outside of defined interval: '.$runs[3]);
        $this->assertTrue($runs[4] > 233 && $runs[4] < 402, 'Runs of length 4 outside of defined interval: '.$runs[4]);
        $this->assertTrue($runs[5] > 90 && $runs[5] < 223, 'Runs of length 5 outside of defined interval: '.$runs[5]);
        $this->assertTrue($runs[6] > 90 && $runs[6] < 233, 'Runs of length 6 outside of defined interval: '.$runs[6]);
    }

    /**
     * Long-run test.
     */
    public function testLongRun()
    {
        $secureRandom = new SecureRandom();
        $b = $this->getBitSequence($secureRandom, 20000);

        $longestRun = $currentRun = 0;
        $lastBit = null;
        for ($i = 0; $i < 20000; ++$i) {
            if ($lastBit === $b[$i]) {
                ++$currentRun;
            } else {
                if ($currentRun > $longestRun) {
                    $longestRun = $currentRun;
                }
                $lastBit = $b[$i];
                $currentRun = 0;
            }
        }
        if ($currentRun > $longestRun) {
            $longestRun = $currentRun;
        }

        $this->assertTrue($longestRun < 34, 'Failed longest run test: '.$longestRun);
    }

    /**
     * Serial Correlation (Autokorrelationstest).
     */
    public function testSerialCorrelation()
    {
        $secureRandom = new SecureRandom();
        $shift = mt_rand(1, 5000);
        $b = $this->getBitSequence($secureRandom, 20000);

        $Z = 0;
        for ($i = 0; $i < 5000; ++$i) {
            $Z += $b[$i] === $b[$i + $shift] ? 1 : 0;
        }

        $this->assertTrue($Z > 2326 && $Z < 2674, 'Failed serial correlation test: '.$Z);
    }

    private function getBitSequence($secureRandom, $length)
    {
        $bitSequence = '';
        for ($i = 0; $i < $length; $i += 40) {
            $value = unpack('H*', $secureRandom->nextBytes(5));
            $value = str_pad(base_convert($value[1], 16, 2), 40, '0', STR_PAD_LEFT);
            $bitSequence .= $value;
        }

        return substr($bitSequence, 0, $length);
    }
}