-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
280 lines (248 loc) · 9.27 KB
/
Copy pathtest.py
File metadata and controls
280 lines (248 loc) · 9.27 KB
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
import os
import sys
import ecdsa
import hashlib
import unittest
import elat
class TestParams:
def __init__(self,
num_traces: int,
known_bits: int,
rebalance: bool,
msb: bool,
zerobits: bool,
curve_name: str,
hashfn):
self.num_traces = num_traces
self.known_bits = known_bits
self.rebalance = rebalance
self.msb = msb
self.zerobits = zerobits
self.curve_name = curve_name
self.hashfn = hashfn
return
def __repr__(self) -> str:
return self.__str__()
def __str__(self) -> str:
return f"TestCase(num_traces={self.num_traces}, known_bits={self.known_bits}, rebalance={self.rebalance}, msb={self.msb}, zerobits={self.zerobits}, curve={self.curve_name}, hash={self.hashfn.__name__})"
pass
# END TestParams
class ECDSALatticeAttackTest:
def __init__(self, testcase: TestParams):
self.testcase = testcase
self.num_traces = testcase.num_traces
self.known_bits = testcase.known_bits
self.rebalance = testcase.rebalance
self.msb = testcase.msb
self.zerobits = testcase.zerobits
self.hashfn = testcase.hashfn
self.curve = ecdsa.curves.curve_by_name(testcase.curve_name)
self.ecparams = self._getECParams()
return
def run(self) -> bool:
attack = elat.ECDSALatticeAttack(self.ecparams, self.known_bits, self.msb, self.zerobits, self.rebalance)
privkey = ecdsa.SigningKey.generate(curve=self.curve)
pubkey = privkey.get_verifying_key()
for ii in range(self.num_traces):
msg = os.urandom(16)
trace = self._generate_trace(msg, privkey, self.hashfn, debug=ii == self.num_traces - 1)
assert pubkey.verify(trace.r + trace.s, trace.m, hashfunc=self.hashfn)
attack.add_trace(trace)
pass
sk0, sk1 = attack.compute_privkey_candidates()
return ecdsa.SigningKey.from_secret_exponent(sk0, curve=self.curve) == privkey or ecdsa.SigningKey.from_secret_exponent(sk1, curve=self.curve) == privkey
def _getECParams(self) -> elat.ECParams:
bits = int(self.curve.baselen) * 8
pp = int(self.curve.curve.p())
aa = int(self.curve.curve.a())
bb = int(self.curve.curve.b())
Gx = int(self.curve.generator.x())
Gy = int(self.curve.generator.y())
nn = int(self.curve.order)
return elat.ECParams(self.curve.name, bits, pp, aa, bb, Gx, Gy, nn)
def _getNonce(self) -> int:
k = bytearray(os.urandom(self.ecparams.bits // 8))
while int.from_bytes(k, 'big') >= self.ecparams.n:
k = bytearray(os.urandom(self.ecparams.bits // 8))
pass
if self.zerobits:
for i in range(self.known_bits // 8):
if self.msb:
k[i] = 0
pass
else:
k[-i - 1] = 0
pass
pass
if self.known_bits % 8 != 0:
if self.msb:
mask = ((1 << (8 - (self.known_bits % 8))) - 1)
k[self.known_bits//8] = k[self.known_bits//8] & mask
pass
else:
mask = (0xFF << (self.known_bits % 8)) & 0xFF
k[-(self.known_bits//8) - 1] = k[-(self.known_bits//8) - 1] & mask
pass
pass
pass
return int.from_bytes(k, 'big')
def _generate_trace(self, data: bytes, sk: ecdsa.SigningKey, hashfn, debug: bool = False) -> elat.ECDSATrace:
mask = ((1 << self.ecparams.bits) - 1)
bits = self.ecparams.bits - self.known_bits
if self.msb:
mask = (mask << bits) & mask
pass
else:
mask = (mask >> bits) & mask
pass
nonce = self._getNonce()
sig = sk.sign(data, k=nonce, hashfunc=hashfn)
hh: bytes = hashfn(data).digest()
aa: bytes = self._int2bytes(nonce & mask)
rr: bytes = sig[:self.curve.signature_length//2]
ss: bytes = sig[self.curve.signature_length//2:]
mm: bytes = data
return elat.ECDSATrace(hh, aa, rr, ss, mm)
def _int2bytes(self, val: int) -> bytes:
return val.to_bytes(self.curve.baselen, 'big')
def __repr__(self) -> str:
return self.__str__()
def __str__(self) -> str:
s = f"TestCase:\n"
s += f"\tEC Params ({self.curve.name}):\n"
s += f"\t\tbits: {self.ecparams.bits}\n"
s += f"\t\t p: {self._int2bytes(self.ecparams.p).hex()}\n"
s += f"\t\t a: {self._int2bytes(self.ecparams.a % self.ecparams.p).hex()}\n"
s += f"\t\t b: {self._int2bytes(self.ecparams.b % self.ecparams.p).hex()}\n"
s += f"\t\t Gx: {self._int2bytes(self.ecparams.Gx).hex()}\n"
s += f"\t\t Gy: {self._int2bytes(self.ecparams.Gy).hex()}\n"
s += f"\t\t n: {self._int2bytes(self.ecparams.n).hex()}\n"
s += f"\t{self.testcase}"
return s
pass
# END ECDSALatticeAttackTest
class TestCase_Collection(unittest.TestCase):
NUM_TRACES = 10
KNOWN_BITS = 128
CURVE_NAME = 'NIST256p'
HASH_FUNC = hashlib.sha256
def test_notRebalance_Msb_Zero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=False, msb=True, zerobits=True, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
def test_notRebalance_Msb_notZero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=False, msb=True, zerobits=False, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
def test_notRebalance_Lsb_Zero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=False, msb=False, zerobits=True, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
def test_notRebalance_Lsb_notZero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=False, msb=False, zerobits=False, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
def test_Rebalance_Msb_Zero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=True, msb=True, zerobits=True, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
def test_Rebalance_Msb_notZero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=True, msb=True, zerobits=False, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
def test_Rebalance_Lsb_Zero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=True, msb=False, zerobits=True, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
def test_Rebalance_Lsb_notZero(self):
self.assertTrue(
run_test(
TestParams(self.NUM_TRACES, self.KNOWN_BITS, rebalance=True, msb=False, zerobits=False, curve_name=self.CURVE_NAME, hashfn=self.HASH_FUNC)
)
)
return
pass
# END TestCase_Collection
class NIST192p_SHA1_TestCase(TestCase_Collection):
NUM_TRACES = 10
KNOWN_BITS = 96
CURVE_NAME = 'NIST192p'
HASH_FUNC = hashlib.sha1
pass
# END NIST192p_SHA1_TestCase
class NIST256p_SHA1_TestCase(TestCase_Collection):
NUM_TRACES = 10
KNOWN_BITS = 128
CURVE_NAME = 'NIST256p'
HASH_FUNC = hashlib.sha1
pass
# END NIST256p_SHA1_TestCase
class NIST256p_SHA256_TestCase(TestCase_Collection):
NUM_TRACES = 10
KNOWN_BITS = 128
CURVE_NAME = 'NIST256p'
HASH_FUNC = hashlib.sha256
pass
# END NIST256p_SHA256_TestCase
class NIST256p_SHA256_TestCase_2(TestCase_Collection):
NUM_TRACES = 70
KNOWN_BITS = 8
CURVE_NAME = 'NIST256p'
HASH_FUNC = hashlib.sha256
pass
# END NIST256p_SHA256_TestCase_2
class BRAINPOOLP256r1_SHA1_TestCase(TestCase_Collection):
NUM_TRACES = 10
KNOWN_BITS = 128
CURVE_NAME = 'BRAINPOOLP256r1'
HASH_FUNC = hashlib.sha1
pass
# END BRAINPOOLP256r1_SHA1_TestCase
class BRAINPOOLP256r1_SHA256_TestCase(TestCase_Collection):
NUM_TRACES = 10
KNOWN_BITS = 128
CURVE_NAME = 'BRAINPOOLP256r1'
HASH_FUNC = hashlib.sha256
pass
# END BRAINPOOLP256r1_SHA256_TestCase
class SECP256k1_SHA1_TestCase(TestCase_Collection):
NUM_TRACES = 10
KNOWN_BITS = 128
CURVE_NAME = 'SECP256k1'
HASH_FUNC = hashlib.sha1
pass
# END SECP256k1_SHA1_TestCase
class SECP256k1_SHA256_TestCase(TestCase_Collection):
NUM_TRACES = 10
KNOWN_BITS = 128
CURVE_NAME = 'SECP256k1'
HASH_FUNC = hashlib.sha256
pass
# END SECP256k1_SHA256_TestCase
def run_test(testcase: TestParams) -> bool:
test = ECDSALatticeAttackTest(testcase)
return test.run()
if __name__ == '__main__':
unittest.main()
pass