-
Notifications
You must be signed in to change notification settings - Fork 4
/
jc_curve25519.py
518 lines (398 loc) · 15.6 KB
/
jc_curve25519.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
By David Oswald, [email protected]
26 August 2015
Some of this code is based on information or code from
- Sam Kerr: http://samuelkerr.com/?p=431
- Eli Bendersky: http://eli.thegreenplace.net/2009/03/07/computing-modular-square-roots-in-python/
- http://cr.yp.to/highspeed/naclcrypto-20090310.pdf, page 7
The code of Eli is in the public domain:
"Some of the blog posts contain code; unless otherwise stated, all of it is
in the public domain"
=======================================================================
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
=======================================================================
If this software is useful to you, I'd appreciate an attribution,
contribution (e.g. bug fixes, improvements, ...), or a beer.
"""
from smartcard.Exceptions import NoCardException
from smartcard.System import *
from smartcard.util import toHexString
from struct import *
from timeit import default_timer as timer
class JCCurve25519:
# Montgomery parameters of Curve25519
p = pow(2, 255) - 19
a_m = 486662
b_m = 1
r = pow(2, 252) + 27742317777372353535851937790883648493
# Precomputed Weierstrass parameters of Curve25510
a_w = 0x2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa984914a144
b_w = 0x7b425ed097b425ed097b425ed097b425ed097b425ed097b4260b5e9c7710c864
Gx_w = 0x2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad245a
Gy_w = 0x20ae19a1b8a086b4e01edd2c7748d14c923d4d7e6d7c61b229e9c5a27eced3d9
@staticmethod
def modular_sqrt(a, p):
""" Find a quadratic residue (mod p) of 'a'. p
must be an odd prime.
Solve the congruence of the form:
x^2 = a (mod p)
And returns x. Note that p - x is also a root.
0 is returned is no square root exists for
these a and p.
The Tonelli-Shanks algorithm is used (except
for some simple cases in which the solution
is known from an identity). This algorithm
runs in polynomial time (unless the
generalized Riemann hypothesis is false).
"""
# Simple cases
#
if JCCurve25519.legendre_symbol(a, p) != 1:
return 0
elif a == 0:
return 0
elif p == 2:
return p
elif p % 4 == 3:
return pow(a, (p + 1) // 4, p)
# Partition p-1 to s * 2^e for an odd s (i.e.
# reduce all the powers of 2 from p-1)
#
s = p - 1
e = 0
while s % 2 == 0:
s //= 2
e += 1
# Find some 'n' with a legendre symbol n|p = -1.
# Shouldn't take long.
#
n = 2
while JCCurve25519.legendre_symbol(n, p) != -1:
n += 1
# Here be dragons!
# Read the paper "Square roots from 1; 24, 51,
# 10 to Dan Shanks" by Ezra Brown for more
# information
#
# x is a guess of the square root that gets better
# with each iteration.
# b is the "fudge factor" - by how much we're off
# with the guess. The invariant x^2 = ab (mod p)gx_w = (9 + a_m/3)%p
# is maintained throughout the loop.
# g is used for successive powers of n to update
# both a and b
# r is the exponent - decreases with each update
#
x = pow(a, (s + 1) // 2, p)
b = pow(a, s, p)
g = pow(n, s, p)
r = e
while True:
t = b
m = 0
for m in range(r):
if t == 1:
break
t = pow(t, 2, p)
if m == 0:
return x
gs = pow(g, 2 ** (r - m - 1), p)
g = (gs * gs) % p
x = (x * gs) % p
b = (b * g) % p
r = m
@staticmethod
def legendre_symbol(a, q):
""" Compute the Legendre symbol a|p using
Euler's criterion. p is a prime, a is
relatively prime to p (if p divides
a, then a|p = 0)
Returns 1 if a has a square root modulo
p, -1 otherwise.
"""
ls = pow(a, (q - 1) // 2, q)
return -1 if ls == q - 1 else ls
@staticmethod
def weierstrass_to_montgomery(xW):
xM = (((JCCurve25519.b_m * xW) % JCCurve25519.p) - JCCurve25519.a_m * JCCurve25519.inv(3)) % JCCurve25519.p
return xM
@staticmethod
def montgomery_to_weierstrass(xp):
xp = (xp + JCCurve25519.a_m * JCCurve25519.inv(3)) % JCCurve25519.p
yp2 = (((pow(xp,
3) % JCCurve25519.p) + JCCurve25519.a_w * xp) % JCCurve25519.p + JCCurve25519.b_w) % JCCurve25519.p
yp = JCCurve25519.modular_sqrt(yp2, JCCurve25519.p)
return [xp, yp]
@staticmethod
def unpack_le(s):
if len(s) != 32:
raise Exception("Length != 32")
return sum((s[i]) << (8 * i) for i in range(32))
@staticmethod
def pack_le(n):
r = []
for i in range(32):
r.append(int((n >> (8 * i)) & 0xff))
return r
@staticmethod
def unpack_be(s):
if len(s) != 32:
raise Exception("Length != 32")
return sum((s[i]) << (8 * (31 - i)) for i in range(32))
@staticmethod
def pack_be(n):
r = []
for i in range(32):
r.append(int((n >> (8 * (31 - i))) & 0xff))
return r
# The follwing code is based on
# http://cr.yp.to/highspeed/naclcrypto-20090310.pdf, page 7
@staticmethod
def clamp(n):
n &= ~7
n &= ~(128 << 8 * 31)
n |= 64 << 8 * 31
return n
@staticmethod
def expmod(b, e, m):
if e == 0:
return 1
t = JCCurve25519.expmod(b, e // 2, m) ** 2 % m
if e & 1:
t = (t * b) % m
return t
@staticmethod
def inv(x):
return JCCurve25519.expmod(x, JCCurve25519.p - 2, JCCurve25519.p)
# Addition and doubling formulas taken
# from Appendix D of "Curve25519:
# new Diffie-Hellman speed records".
@staticmethod
# def add((xn,zn), (xm,zm), (xd,zd)):
def add(n, m, z):
(xn, zn), (xm, zm), (xd, zd) = n, m, z
x = 4 * (xm * xn - zm * zn) ** 2 * zd
z = 4 * (xm * zn - zm * xn) ** 2 * xd
return (x % JCCurve25519.p, z % JCCurve25519.p)
@staticmethod
def double(n):
(xn, zn) = n
x = (xn ** 2 - zn ** 2) ** 2
z = 4 * xn * zn * (xn ** 2 + JCCurve25519.a_m * xn * zn + zn ** 2)
return (x % JCCurve25519.p, z % JCCurve25519.p)
@staticmethod
def smul(s, base):
one = (base, 1)
two = JCCurve25519.double(one)
# f(m) evaluates to a tuple
# containing the mth multiple and the
# (m+1)th multiple of base.
def f(m):
if m == 1:
return (one, two)
(pm, pm1) = f(m // 2)
if m & 1:
return JCCurve25519.add(pm, pm1, one), JCCurve25519.double(pm1)
return JCCurve25519.double(pm), JCCurve25519.add(pm, pm1, one)
((x, z), _) = f(s)
return (x * JCCurve25519.inv(z)) % JCCurve25519.p
def __init__(self):
self.connected = False
def isConnected(self):
return self.connected
def transmitReceive(self, apdu):
response, sw1, sw2 = self.c.transmit(apdu)
if sw1 == 0x61:
GET_RESPONSE = [0x00, 0xC0, 0x00, 0x00]
apdu = GET_RESPONSE + [sw2]
response, sw1, sw2 = self.c.transmit(apdu)
if sw1 == 0x6c:
apdu[4] = sw2
response, sw1, sw2 = self.c.transmit(apdu)
return response, sw1, sw2
def connect(self):
print("== Available readers:")
self.connected = False
rl = smartcard.System.readers()
i = 0
for r in rl:
print(str(i) + ") " + r.name)
i = i + 1
if len(rl) == 0:
raise Exception("No readers available")
print(" Connecting to a first reader with a card ... ")
usable_card_found = False
for r in rl:
try:
self.c = r.createConnection()
self.c.connect() # try to connect
print(" ATR: " + toHexString(self.c.getATR()))
usable_card_found = True
break # we found it, stop searching
# if no card is found, NoCardException is emmit, but capture broadly for other reader-related errors
except Exception:
continue # try next reader
if not usable_card_found:
raise Exception("No reader with card was found")
# select app
# SELECT = [0x00, 0xA4, 0x04, 0x00, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
SELECT = [0x00, 0xA4, 0x04, 0x00, 0x08, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8]
response, sw1, sw2 = self.transmitReceive(SELECT)
if sw1 == 0x90 and sw2 == 0x00:
print(" App selected")
self.connected = True
else:
raise Exception("App select failed")
def generateKeypair(self):
""" Generates a key pair on card for debug purposes, will
return public and private key
This method handles the conversion to Montgomery coordinates etc.
"""
if self.connected == False:
raise Exception("Not connected")
# Generate key APDU
GENKEY = [0x00, 0x01, 0x0, 0x00, 0x00]
b = timer()
response, sw1, sw2 = self.transmitReceive(GENKEY)
e = timer()
print("Execution time: " + str((e - b) * 1000) + ' ms')
if sw1 != 0x90 or sw2 != 0x00:
raise Exception("Card error")
return False
if len(response) != 64:
raise Exception("Response is " + str(len(response)) + " byte")
# Unpack and convert internally
skW = JCCurve25519.unpack_be(response[0:32])
pkW = JCCurve25519.unpack_be(response[32:64])
# print "skW = " + hex(skW)
# print "pkW = " + hex(pkW)
# convert to Curve25519 standards
sk = skW << 3
pk = JCCurve25519.weierstrass_to_montgomery(pkW)
# Multiply PK by 8 (three doublings)
pk = JCCurve25519.smul(8, pk)
return sk, pk
def setPrivateKey(self, sk):
""" Sets a private key and returns the public key
This method handles the conversion to Montgomery coordinates etc.
"""
if self.connected == False:
raise Exception("Not connected")
# swap endianess
sk = JCCurve25519.pack_be(sk)
# Generate key APDU
SETKEY = [0x00, 0x02, 0x0, 0x00, 0x20] + sk
b = timer()
response, sw1, sw2 = self.transmitReceive(SETKEY)
e = timer()
print("Execution time: " + str((e - b) * 1000) + ' ms')
if sw1 != 0x90 or sw2 != 0x00:
raise Exception("Card error")
return False
if len(response) != 32:
raise Exception("Response is " + str(len(response)) + " byte")
# Unpack and convert internally
pkW = JCCurve25519.unpack_be(response)
# convert to Curve25519 standards
pk = JCCurve25519.weierstrass_to_montgomery(pkW)
# Multiply PK by 8 (three doublings)
pk = JCCurve25519.smul(8, pk)
return pk
def generateSharedSecret(self, pk):
""" Generates a shared secret from the internal private key and the
passed public key
This method handles the conversion to Montgomery coordinates etc.
"""
if self.connected == False:
raise Exception("Not connected")
# Generate key APDU
pkW = JCCurve25519.montgomery_to_weierstrass(pk);
# send to card MSByte first
pkCard = JCCurve25519.pack_be(pkW[0]) + JCCurve25519.pack_be(pkW[1])
GENSECRET = [0x00, 0x03, 0x0, 0x00, 0x40] + pkCard
b = timer()
response, sw1, sw2 = self.transmitReceive(GENSECRET)
e = timer()
print("Execution time: " + str((e - b) * 1000) + ' ms')
if sw1 != 0x90 or sw2 != 0x00:
raise Exception("Card error")
return False
if len(response) != 32:
raise Exception("Response is " + str(len(response)) + " byte")
# Unpack and convert internally
sharedSecretW = JCCurve25519.unpack_be(response)
# convert to Curve25519 standards
sharedSecret = JCCurve25519.weierstrass_to_montgomery(sharedSecretW)
# Multiply secret by 8 (three doublings)
sharedSecret = JCCurve25519.smul(8, sharedSecret)
return sharedSecret
def main():
# test vector
skTV = [0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c,
0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a]
pkTV = [0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b, 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf,
0x3a, 0x0d, 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, 0x4e, 0x6a]
pkN = JCCurve25519.unpack_le(pkTV)
skN = JCCurve25519.unpack_le(skTV)
skN = JCCurve25519.clamp(skN)
pkTest = JCCurve25519.smul(skN, 9)
print('\n')
print("== Testing against test vector == ")
print("pkRef = " + hex(pkN))
print("pkTest = " + hex(pkTest))
print("diff = " + hex(pkTest - pkN))
print('\n')
if (pkTest - pkN) != 0:
return
# Operations with Javacard
curve = JCCurve25519()
curve.connect()
print('\n')
print("== Testing on-card key generation")
sk, pk = curve.generateKeypair()
# Compute reference
pkRef = JCCurve25519.smul(sk, 9)
diff = pkRef - pk
print("pkRef = " + hex(pkRef))
print("pkTest = " + hex(pk))
print("diff = " + hex(diff))
print('\n')
if diff != 0:
return
print("== Testing setting the private key")
pkGen = curve.setPrivateKey(skN)
diff = pkN - pkGen
print("pkRef = " + hex(pkN))
print("pkTest = " + hex(pkGen))
print("diff = " + hex(diff))
print('\n')
if diff != 0:
return
print("== Testing generating shared secret")
pkBob = [0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4, 0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37, 0x3f, 0x83,
0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d, 0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f]
sharedSecret = [0x4a, 0x5d, 0x9d, 0x5b, 0xa4, 0xce, 0x2d, 0xe1, 0x72, 0x8e, 0x3b, 0xf4, 0x80, 0x35, 0x0f, 0x25,
0xe0, 0x7e, 0x21, 0xc9, 0x47, 0xd1, 0x9e, 0x33, 0x76, 0xf0, 0x9b, 0x3c, 0x1e, 0x16, 0x17, 0x42]
pkBobN = JCCurve25519.unpack_le(pkBob)
sharedSecretN = JCCurve25519.unpack_le(sharedSecret)
ssGen = curve.generateSharedSecret(pkBobN)
diff = sharedSecretN - ssGen
print("secretRef = " + hex(sharedSecretN))
print("secretTest = " + hex(ssGen))
print("diff = " + hex(diff))
print('\n')
if diff != 0:
return
if __name__ == '__main__':
main()