-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompactFIPS202-test.py
76 lines (67 loc) · 3.08 KB
/
CompactFIPS202-test.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
# -*- coding: utf-8 -*-
# Implementation by Renaud Bauvin,
# hereby denoted as "the implementer".
#
# To the extent possible under law, the implementer has waived all copyright
# and related or neighboring rights to the source code in this file.
# http://creativecommons.org/publicdomain/zero/1.0/
import binascii
import CompactFIPS202
import os
## Iterate through the files 'Short... and LongMsgKAT_XXX.txt' containing the
## test vectors and compare the computed values to the provided ones
## In case of difference, it stops the processing and print a message
dirTestVector=os.path.abspath(os.path.join('.'))
verbose=False
instances=[
['SHAKE128', 1344, 256, 0x1F, 0],
['SHAKE256', 1088, 512, 0x1F, 0],
['SHA3-224', 1152, 448, 0x06, 224],
['SHA3-256', 1088, 512, 0x06, 256],
['SHA3-384', 832, 768, 0x06, 384],
['SHA3-512', 576, 1024, 0x06, 512],
]
fileTypes=['Short']
def delimitedSuffixInBinary(delimitedSuffix):
binary = ''
while(delimitedSuffix != 1):
binary = binary + ('%d' % (delimitedSuffix%2))
delimitedSuffix = delimitedSuffix//2
return binary
for instance in instances:
[fileNameSuffix, r, c, delimitedSuffix, n] = instance
for fileType in fileTypes:
print('Processing file: %sMsgKAT_%s.txt...' % (fileType, fileNameSuffix))
print("Keccak[r=%d, c=%d] with '%s' suffix" % (r, c, delimitedSuffixInBinary(delimitedSuffix)))
#Open the corresponding file
try:
referenceFile=open(os.path.join(dirTestVector,fileType+('MsgKAT_%s.txt' % fileNameSuffix)), 'r')
except IOError:
print("Error: test vector files must be stored in %s" % (dirTestVector))
exit()
#Parse the document line by line (works only for Short and Long files)
for line in referenceFile:
if line.startswith('Len'):
Len=int(line.split(' = ')[1].strip('\n\r'))
if line.startswith('Msg'):
Msg=line.split(' = ')[1].strip('\n\r')
msg = bytearray(binascii.unhexlify(Msg))
msg = msg[:Len//8]
if (line.startswith('MD') or line.startswith('Squeezed')):
MD_ref=line.split(' = ')[1].strip('\n\r')
reference = bytearray(binascii.unhexlify(MD_ref))
# If line starts with 'Squeezed', use the output length from the test vector
if line.startswith('Squeezed'):
n = len(reference)*8
elif n == 0:
print("Error: the output length should be specified")
exit()
if ((Len % 8) == 0):
# Perform our own computation
computed = CompactFIPS202.Keccak(r, c, msg, delimitedSuffix, n//8)
#Compare the results
if (computed != reference):
print('ERROR: \n\t type=%s\n\t length=%d\n\t message=%s\n\t reference=%s\n\t computed=%s' % (fileNameSuffix, Len, Msg, binascii.hexlify(reference), binascii.hexlify(computed)))
exit()
print("OK\n")
referenceFile.close()