-
Notifications
You must be signed in to change notification settings - Fork 45
/
sp800_22_tests.py
executable file
·167 lines (143 loc) · 5.09 KB
/
sp800_22_tests.py
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
166
167
#!/usr/bin/env python
# sp800_22_tests.py
#
# Copyright (C) 2017 David Johnston
# This program is distributed under the terms of the GNU General Public License.
#
# This file is part of sp800_22_tests.
#
# sp800_22_tests is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# sp800_22_tests is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with sp800_22_tests. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import argparse
import sys
def read_bits_from_file(filename,bigendian):
bitlist = list()
if filename == None:
f = sys.stdin
else:
f = open(filename, "rb")
while True:
bytes = f.read(16384)
if bytes:
for bytech in bytes:
if sys.version_info > (3,0):
byte = bytech
else:
byte = ord(bytech)
for i in range(8):
if bigendian:
bit = (byte & 0x80) >> 7
byte = byte << 1
else:
bit = (byte >> i) & 1
bitlist.append(bit)
else:
break
f.close()
return bitlist
import argparse
import sys
parser = argparse.ArgumentParser(description='Test data for distinguishability form random, using NIST SP800-22Rev1a algorithms.')
parser.add_argument('filename', type=str, nargs='?', help='Filename of binary file to test')
parser.add_argument('--be', action='store_false',help='Treat data as big endian bits within bytes. Defaults to little endian')
parser.add_argument('-t', '--testname', default=None,help='Select the test to run. Defaults to running all tests. Use --list_tests to see the list')
parser.add_argument('--list_tests', action='store_true',help='Display the list of tests')
args = parser.parse_args()
bigendian = args.be
filename = args.filename
# X 3.1 Frequency (Monobits) Test
# X 3.2 Frequency Test within a Block
# X 3.3 Runs Test
# X 3.4 Test for the Longest Run of Ones in a Block
# X 3.5 Binary Matrix Rank Test
# X 3.6 Discrete Fourier Transform (Specral) Test
# X 3.7 Non-Overlapping Template Matching Test
# X 3.8 Overlapping Template Matching Test
# X 3.9 Maurers Universal Statistical Test
# X 3.10 Linear Complexity Test
# X 3.11 Serial Test
# X 3.12 Approximate Entropy Test
# X 3.13 Cumulative Sums Test
# X 3.14 Random Excursions Test
# X 3.15 Random Excursions Variant Test
testlist = [
'monobit_test',
'frequency_within_block_test',
'runs_test',
'longest_run_ones_in_a_block_test',
'binary_matrix_rank_test',
'dft_test',
'non_overlapping_template_matching_test',
'overlapping_template_matching_test',
'maurers_universal_test',
'linear_complexity_test',
'serial_test',
'approximate_entropy_test',
'cumulative_sums_test',
'random_excursion_test',
'random_excursion_variant_test']
print("Tests of Distinguishability from Random")
if args.list_tests:
for i,testname in zip(range(len(testlist)),testlist):
print(str(i+1).ljust(4)+": "+testname)
exit()
bits = read_bits_from_file(filename,bigendian)
gotresult=False
if args.testname:
if args.testname in testlist:
m = __import__ ("sp800_22_"+args.testname)
func = getattr(m,args.testname)
print("TEST: %s" % args.testname)
success,p,plist = func(bits)
gotresult = True
if success:
print("PASS")
else:
print("FAIL")
if p:
print("P="+str(p))
if plist:
for pval in plist:
print("P="+str(pval))
else:
print("Test name (%s) not known" % args.ttestname)
exit()
else:
results = list()
for testname in testlist:
print("TEST: %s" % testname)
m = __import__ ("sp800_22_"+testname)
func = getattr(m,testname)
(success,p,plist) = func(bits)
summary_name = testname
if success:
print(" PASS")
summary_result = "PASS"
else:
print(" FAIL")
summary_result = "FAIL"
if p != None:
print(" P="+str(p))
summary_p = str(p)
if plist != None:
for pval in plist:
print("P="+str(pval))
summary_p = str(min(plist))
results.append((summary_name,summary_p, summary_result))
print()
print("SUMMARY")
print("-------")
for result in results:
(summary_name,summary_p, summary_result) = result
print(summary_name.ljust(40),summary_p.ljust(18),summary_result)