Repository navigation
Expand file tree
/
Copy pathecdsa_lib.py
More file actions
148 lines (122 loc) · 4.62 KB
/
Copy pathecdsa_lib.py
File metadata and controls
148 lines (122 loc) · 4.62 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
#!/usr/bin/env python3
# Lattice ECDSA Attack : ECDSA and cryptographic library
# Copyright (C) 2021 Antoine Ferron - BitLogiK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#
# Install cryptography
# pip3 install cryptography
# or
# apt install python3-cryptography
import hashlib
import secrets
from cryptography.hazmat import backends
from cryptography.hazmat.primitives.asymmetric import ec
CURVES_ORDER = {
"SECP224R1": int(
"2695994666715063979466701508701962594045780771442439172168272236" "8061"
),
"SECP256K1": int(
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16
),
"SECP256R1": int(
"11579208921035624876269744694940757352999695522413576034242225906"
"1068512044369"
),
"SECP384R1": int(
"39402006196394479212279040100143613805079739270465446667946905279"
"627659399113263569398956308152294913554433653942643"
),
"SECP521R1": int(
"68647976601306097149819007990813932172694353001433054093944634591"
"85543183397655394245057746333217197532963996371363321113864768612"
"440380340372808892707005449"
),
}
def inverse_mod(a_num, m_mod):
# a_num^-1 mod m_mod, m_mod must be prime
# If not used on a prime modulo,
# can throw ZeroDivisionError.
if a_num < 0 or m_mod <= a_num:
a_num = a_num % m_mod
i, j = a_num, m_mod
x_a, x_b = 1, 0
while i != 1:
quot, rem = divmod(j, i)
x_rem = x_b - quot * x_a
j, i, x_b, x_a = i, rem, x_a, x_rem
return x_a % m_mod
def sha2(raw_message):
# SHA-2 256
return hashlib.sha256(raw_message).digest()
def bytes_to_int(bytes_data):
return int.from_bytes(bytes_data, "big")
def sha2_int(data):
return bytes_to_int(sha2(data))
def curve_size(curve_name):
# return the curve size (log2 N) from its name string
try:
curve_obj = getattr(ec, curve_name.upper())()
except Exception as exc:
raise Exception(
f"Unknown curves. Curves names available : {list(CURVES_ORDER.keys())}"
) from exc
return curve_obj.key_size
def curve_n(curve_name):
# return the curve order "N" from its name string
order = CURVES_ORDER.get(curve_name.upper())
if not order:
raise Exception(
f"Unknown curves. Curves names available : {list(CURVES_ORDER.keys())}"
)
return order
def check_publickey(pubkey, curve_str):
# Check pubkey (x,y) belongs on the curve
try:
curve_obj = getattr(ec, curve_str.upper())()
except Exception as exc:
raise Exception(
f"Unknown curves. Curves names available : {list(CURVES_ORDER.keys())}"
) from exc
if len(pubkey) != 2:
raise Exception(
'Public key data shall be provided as :\n "public_key" : [ x, y ]'
)
publickey_obj = ec.EllipticCurvePublicNumbers(pubkey[0], pubkey[1], curve_obj)
ret = False
try:
publickey_obj.public_key(backends.default_backend())
ret = True
except ValueError:
pass
return ret
def privkey_to_pubkey(pv_key_int, curve_name):
# Return public point coordinates (Scalar multiplication of pvkey with base point G)
ec_backend = getattr(ec, curve_name.upper())()
pubkey = (
ec.derive_private_key(pv_key_int, ec_backend, backends.default_backend())
.public_key()
.public_numbers()
)
return [pubkey.x, pubkey.y]
def ecdsa_sign_kout(z_hash, pvkey, curve_name):
# Perform ECDSA, but insecurely return the private k nonce
n_mod = curve_n(curve_name)
k_nonce = secrets.randbelow(n_mod)
r_sig = scalar_mult_x(k_nonce, curve_name)
s_sig = inverse_mod(k_nonce, n_mod) * (z_hash + r_sig * pvkey) % n_mod
return r_sig, s_sig, k_nonce
def scalar_mult_x(d_scalar, curve):
# Scalar multiplication of d with base point G
# and return x, like ECDH with G.
return privkey_to_pubkey(d_scalar, curve)[0]