aboutsummaryrefslogtreecommitdiff
path: root/vectors/ed25519/format_test_vector.py
blob: e0173d36c42fe68acf6b96e7dbc136f3cd3f6db0 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#
# format_test_vector.py
# -----------------------------------------
# Formats test vector for ed25519_fpga_model
#
# Author: Pavel Shatov
# Copyright (c) 2017-2018, NORDUnet A/S
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# - Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# - Neither the name of the NORDUnet nor the names of its contributors may
#   be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


#
USAGE = "USAGE: format_test_vector.py [openssl_binary]"
#

#
# This script reads the test vector generated by regenerate_test_vector.py
# and writes nicely formatted C header file and Verilog include file.
#

#
# IMPORTANT: As far as I understand, this will only work with OpenSSL 1.1.1,
# versions 1.1.0 and 1.0.2 don't seem to support the Ed25519 algoritm. If you
# system library is the older version, you can pass the location of the
# specific OpenSSL binary to use on the command line.
#


#
# imports
#
import sys
import subprocess
import hashlib


#
# variables
#
OPENSSL = ""


#
# format one test vector
#
def format_c_header(f, d, qy):
    
        # write all numbers in vector
    format_c_array(f, d,  "#define ED25519_D_HASHED_LSB_6" + " \\\n")
    format_c_array(f, qy, "#define ED25519_Q_Y_6"          + " \\\n")

    
#
# format one test vector
#
def format_verilog_include(f, d, qy):

        # write all numbers in vector
    format_verilog_concatenation(f, d,  "localparam [255:0] ED25519_D_HASHED_LSB_6 =\n")
    format_verilog_concatenation(f, qy, "localparam [255:0] ED25519_Q_Y_6          =\n")


#
# nicely format multi-word integer into C array initializer
#
def format_c_array(f, n, s):

        # print '#define XYZ \'
    f.write(s)

        # convert number to hex string and prepend it with zeroes if necessary
    n_hex = hex(n).lstrip("0x").rstrip("L")
    while (len(n_hex) % 8) > 0:
        n_hex = "0" + n_hex
    
        # get number of 32-bit words
    num_words = len(n_hex) // 8

        # print all words in n
    w = 0
    while w < num_words:
    
        n_part = ""

            # add tab for every new line
        if w == 0:
            n_part += "\t{"
        elif (w % 4) == 0:
            n_part += "\t "
            
            # add current word
        n_part += "0x" + n_hex[8 * w : 8 * (w + 1)]
        
            # add separator or newline
        if (w + 1) == num_words:
            n_part += "}\n"
        else:
            n_part += ", "
            if (w % 4) == 3:
                n_part += "\\\n"        
                
        w += 1
        
            # write current part
        f.write(n_part)
    
        # write final newline
    f.write("\n")

#
# nicely format wide number into Verilog concatenation
#
def format_verilog_concatenation(f, n, s):

        # print 'localparam ZZZ ='
    f.write(s)
    
        # convert number to hex string and prepend it with zeroes if necessary
    n_hex = hex(n).split("0x")[1]
    while (len(n_hex) % 8) > 0:
        n_hex = "0" + n_hex
    
        # get number of 32-bit words
    num_words = len(n_hex) // 8

        # print all words in n
    w = 0
    while w < num_words:
    
        n_part = ""
        
        if w == 0:
            n_part += "\t{"
        elif (w % 4) == 0:
            n_part += "\t "
            
        n_part += "32'h" + n_hex[8 * w : 8 * (w + 1)]
        
        if (w + 1) == num_words:
            n_part += "};\n"
        else:
            n_part += ", "
            if (w % 4) == 3:
                n_part += "\n"        
        w += 1
        
        f.write(n_part)
    
    f.write("\n")


    #
    # returns d, qy where
    # d is the private key and qy is the corresponding public key
    #
def get_key(openssl, party):

        # generate private key filename
    key_file = party + ".key"
        
        # retrieve key components using openssl
    openssl_command = [openssl, "pkey", "-in", key_file, "-noout", "-text"] 
    openssl_stdout = subprocess.check_output(openssl_command).decode("utf-8")
    stdout_lines = openssl_stdout.splitlines()
    
    key_priv = ""
    key_pub = ""

    found_priv = False
    found_pub = False
    
        # process lines looking for "priv:" and "pub:" markers
    for line in stdout_lines:
       
            # found private key marker?
        if line == "priv:":
            found_priv = True
            found_pub = False
            continue
            
            # found public key marker?
        if line == "pub:":
            found_pub = True
            found_priv = False
            continue
            
            # found part of private key?
        if found_priv:
            if not line.startswith(" "):
                found_priv = False
                continue
            else:
                key_priv += line.strip()
            
            # found part of public key?
        if found_pub:
            if not line.startswith(" "):
                found_pub = False
                continue
            else:
                key_pub += line.strip()
            
        # do some cleanup and sanity checking on private key
        #  * remove colons
        #  * check length (256 bits)
    key_priv = key_priv.replace(":", "")
    if len(key_priv) != (256 / 4): sys.exit()
    
        # do some cleanup and sanity checking on public key
        #  * remove colons
        #  * check length (256 bits)
    key_pub = key_pub.replace(":", "")
    if len(key_pub) != (256 / 4): sys.exit()

        # convert to byte arrays
    key_priv_bytes = bytes.fromhex(key_priv)
    key_pub_bytes = bytes.fromhex(key_pub)
        
        # hash private key
    key_priv_hashed_bytes = hashlib.sha512(key_priv_bytes).digest()
    
        # take the lower half and reverse byte order
    key_priv_hashed_lsb_bytes = key_priv_hashed_bytes[:32][::-1]
            
        # done
    return key_priv_hashed_lsb_bytes, key_pub_bytes

    
if __name__ == "__main__":

        # detect whether user requested some specific binary
    if len(sys.argv) == 1:
        OPENSSL = "openssl"
        print("Using system OpenSSL library.")
    elif len(sys.argv) == 2:
        OPENSSL = sys.argv[1]
        print("Using OpenSSL binary '" + OPENSSL + "'...")
    else:
        print(USAGE)

    if len(OPENSSL) > 0:
    
            # open output files
        file_h = open('ed25519_test_vector_randomized.h', 'w')
        file_v = open('ed25519_test_vector_randomized.vh', 'w')
        
            # write headers
        file_h.write("/* Generated automatically, do not edit. */\n\n")
        file_v.write("/* Generated automatically, do not edit. */\n\n")
        
            # load keys
        d, qy = get_key(OPENSSL, "test")

            # convert to integers
        d_int  = int(d.hex(), 16)
        qy_int = int(qy.hex(), 16)
        
            # format numbers and write to file
        format_c_header(file_h, d_int, qy_int)
        format_verilog_include(file_v, d_int, qy_int)

            # done
        file_h.close()
        file_v.close()
        
            # everything went just fine
        print("Test vector formatted.")

#
# End of file
#