aboutsummaryrefslogtreecommitdiff
path: root/test/format_test_vectors.py
blob: 21b92627cbbe8bb3cd9b9434d21152d13ead5051 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#
# format_test_vectors.py
# ------------------------------------------
# Formats test vectors for modexp_fpga_model
#
# Author: Pavel Shatov
# Copyright (c) 2017, 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.
#

#
# This script reads the test vectors generated by regenerate_test_vectors.py
# and writes nicely formatted C header file.
#

#
# imports
#
import subprocess

#
# get part of string between two markers
#
def string_between(s, s_left, s_right):
	s_begin = s.index(s_left) + len(s_left)
	s_end = s.index(s_right, s_begin)
	return s[s_begin:s_end]

#
# load message from file
#
def read_message(key):
	with open(key + ".txt", "r") as f:
		return f.readlines()[0]
	
#
# read modulus from file
#
def read_modulus(key):
	openssl_command = ["openssl", "rsa", "-in", key + ".key", "-noout", "-modulus"]
	openssl_stdout = subprocess.check_output(openssl_command).decode("utf-8")
	return openssl_stdout.strip().split("=")[1]

#
# read private exponent from file
#
def read_secret(key):
	openssl_command = ["openssl", "rsa", "-in", key + ".key", "-noout", "-text"]
	openssl_stdout = subprocess.check_output(openssl_command).decode("utf-8")
	openssl_secret = string_between(openssl_stdout, "privateExponent", "prime1")
	openssl_secret = openssl_secret.replace(":", "")
	openssl_secret = openssl_secret.replace("\n", "")
	openssl_secret = openssl_secret.replace(" ", "")	
	return openssl_secret

#
# read part of private key from file
#
def read_prime1(key):
	openssl_command = ["openssl", "rsa", "-in", key + ".key", "-noout", "-text"]
	openssl_stdout = subprocess.check_output(openssl_command).decode("utf-8")
	openssl_secret = string_between(openssl_stdout, "prime1", "prime2")
	openssl_secret = openssl_secret.replace(":", "")
	openssl_secret = openssl_secret.replace("\n", "")
	openssl_secret = openssl_secret.replace(" ", "")	
	return openssl_secret
def read_prime2(key):
	openssl_command = ["openssl", "rsa", "-in", key + ".key", "-noout", "-text"]
	openssl_stdout = subprocess.check_output(openssl_command).decode("utf-8")
	openssl_secret = string_between(openssl_stdout, "prime2", "exponent1")
	openssl_secret = openssl_secret.replace(":", "")
	openssl_secret = openssl_secret.replace("\n", "")
	openssl_secret = openssl_secret.replace(" ", "")	
	return openssl_secret

#
# read prive exponent from file
#
def read_exponent1(key):
	openssl_command = ["openssl", "rsa", "-in", key + ".key", "-noout", "-text"]
	openssl_stdout = subprocess.check_output(openssl_command).decode("utf-8")
	openssl_secret = string_between(openssl_stdout, "exponent1", "exponent2")
	openssl_secret = openssl_secret.replace(":", "")
	openssl_secret = openssl_secret.replace("\n", "")
	openssl_secret = openssl_secret.replace(" ", "")	
	return openssl_secret
def read_exponent2(key):
	openssl_command = ["openssl", "rsa", "-in", key + ".key", "-noout", "-text"]
	openssl_stdout = subprocess.check_output(openssl_command).decode("utf-8")
	openssl_secret = string_between(openssl_stdout, "exponent2", "coefficient")
	openssl_secret = openssl_secret.replace(":", "")
	openssl_secret = openssl_secret.replace("\n", "")
	openssl_secret = openssl_secret.replace(" ", "")	
	return openssl_secret

# 
# https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
#
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        (g, y, x) = egcd(b % a, a)
        return (g, x - (b // a) * y, y)

def modinv(a, m):
    (g, x, y) = egcd(a, m)
    if g != 1:
        raise Exception("Can't invert a = " + a)
    else:
        return x % m
		
#
# format one test vector
#
def format_c_header(f, key, n, m, d, s, p, q, dp, dq, mp, mq):

		# write all numbers in vector
	format_c_array(f, n,  "#define N_"  + str(key) + " \\\n")
	format_c_array(f, m,  "#define M_"  + str(key) + " \\\n")
	format_c_array(f, d,  "#define D_"  + str(key) + " \\\n")
	format_c_array(f, s,  "#define S_"  + str(key) + " \\\n")
	format_c_array(f, p,  "#define P_"  + str(key) + " \\\n")
	format_c_array(f, q,  "#define Q_"  + str(key) + " \\\n")
	format_c_array(f, dp, "#define DP_" + str(key) + " \\\n")
	format_c_array(f, dq, "#define DQ_" + str(key) + " \\\n")
	format_c_array(f, mp, "#define MP_" + str(key) + " \\\n")
	format_c_array(f, mq, "#define MQ_" + str(key) + " \\\n")

#
# calculate Montgomery factor
#
def calc_montgomery_factor(k, n):
	
	f = 1
	
	for i in range(2 * k):
		f1 = 2 * f
		f2 = f1 - n
		if f2 < 0:
			f = f1
		else:
			f = f2
	
	return f


#
# calculate Montgomery modulus-dependent helper coefficient
#
def calc_montgomery_n_coeff(k, n):

	r = 1
	b = 1 << k
	
	nn = b - n
	
	for i in range(k-1):
		t = (r * nn) % b
		mask = 1 << (i + 1)
		if (t & mask) == mask:
			r = r + (1 << (i + 1))

	return r
				

	
#
# format one test vector
#
def format_verilog_include(f, key, n, m):

		# calculate factor to bring message into Montgomery domain
	factor = calc_montgomery_factor(int(key), n)
	
		# calculate helper coefficient for Montgomery multiplication
	n_coeff = calc_montgomery_n_coeff(int(key), n)
			
		# calculate the extra coefficient Montgomery multiplication brings in
	coeff = modinv(1 << int(key), n)
	
		# convert m into Montgomery representation
	m_factor = (m * factor * coeff) % n
		
		# write all numbers
	format_verilog_concatenation(f, m,        "localparam [" + str(int(key)-1) + ":0] M_"        + str(key) + " =\n")
	format_verilog_concatenation(f, n,        "localparam [" + str(int(key)-1) + ":0] N_"        + str(key) + " =\n")
	format_verilog_concatenation(f, n_coeff,  "localparam [" + str(int(key)-1) + ":0] N_COEFF_"  + str(key) + " =\n")
	format_verilog_concatenation(f, factor,   "localparam [" + str(int(key)-1) + ":0] FACTOR_"   + str(key) + " =\n")
	format_verilog_concatenation(f, coeff,    "localparam [" + str(int(key)-1) + ":0] COEFF_"    + str(key) + " =\n")
	format_verilog_concatenation(f, m_factor, "localparam [" + str(int(key)-1) + ":0] M_FACTOR_" + str(key) + " =\n")
	
	
#
# nicely format multi-word integer into C array initializer
#
def format_c_array(f, n, s):

		# print '#define 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 = ""

			# 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")

	
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")
	
if __name__ == "__main__":

		# list of key lengths to process
	keys = ["384", "512"]

		# open output files
	file_h = open('modexp_fpga_model_vectors.h', 'w')
	file_v = open('modexp_fpga_model_vectors.v', 'w')
	
		# write headers
	file_h.write("/* Generated automatically, do not edit. */\n\n")
	file_v.write("/* Generated automatically, do not edit. */\n\n")

	
		# process all the keys
	for key in keys:
	
			# prepare all the numbers
		modulus = int(read_modulus(key), 16)			# read number n from .key file
		message = int(read_message(key), 16)			# read number m from .txt file
		secret  = int(read_secret(key),  16)			# read number d from .key file
		signature = pow(message, secret, modulus)		# calculate signature
		prime1 = int(read_prime1(key), 16)				# read p
		prime2 = int(read_prime2(key), 16)				# read q
		exponent1 = int(read_exponent1(key), 16)		# read dp
		exponent2 = int(read_exponent2(key), 16)		# read dq
		message1 = pow(message, exponent1, prime1)		# calculate mp = m ^ dp mod p
		message2 = pow(message, exponent2, prime2)		# calculate mq = m ^ dq mod q
		coefficient = modinv(prime2, prime1)			# calculate

			# do CRT to make sure everything is correct
		h = coefficient * (message1 - message2) % prime1
		crt = message2 + h * prime2
		
			# print all the numbers
		print("key = " + key)
		print("  modulus     = " + hex(modulus))
		print("  message     = " + hex(message))
		print("  secret      = " + hex(secret))
		print("  signature   = " + hex(signature))
		print("  prime1      = " + hex(prime1))
		print("  prime2      = " + hex(prime2))
		print("  exponent1   = " + hex(exponent1))
		print("  exponent2   = " + hex(exponent2))
		print("  message1    = " + hex(message1))
		print("  message2    = " + hex(message2))
		print("  coefficient = " + hex(coefficient))
		print("  crt         = " + hex(crt))
		
			# check
		if crt != signature:
			raise Exception("Error, crt != signature (?)")			
			
			# format numbers and write to file
		format_c_header(file_h, key, modulus, message, secret, signature, prime1, prime2, exponent1, exponent2, message1, message2)
		format_verilog_include(file_v, key, modulus, message)


		# done
	file_h.close()
	
		# everything went just fine
	print("Test vectors formatted.")
	
#
# End of file
#