| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: ecdsa |
| 3 |
Version: 0.19.2 |
| 4 |
Summary: ECDSA cryptographic signature library (pure python) |
| 5 |
Home-page: http://github.com/tlsfuzzer/python-ecdsa |
| 6 |
Author: Brian Warner |
| 7 |
Author-email: [email protected] |
| 8 |
License: MIT |
| 9 |
Classifier: Programming Language :: Python |
| 10 |
Classifier: Programming Language :: Python :: 2 |
| 11 |
Classifier: Programming Language :: Python :: 2.6 |
| 12 |
Classifier: Programming Language :: Python :: 2.7 |
| 13 |
Classifier: Programming Language :: Python :: 3 |
| 14 |
Classifier: Programming Language :: Python :: 3.6 |
| 15 |
Classifier: Programming Language :: Python :: 3.7 |
| 16 |
Classifier: Programming Language :: Python :: 3.8 |
| 17 |
Classifier: Programming Language :: Python :: 3.9 |
| 18 |
Classifier: Programming Language :: Python :: 3.10 |
| 19 |
Classifier: Programming Language :: Python :: 3.11 |
| 20 |
Classifier: Programming Language :: Python :: 3.12 |
| 21 |
Classifier: Programming Language :: Python :: 3.13 |
| 22 |
Requires-Python: >=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.* |
| 23 |
Description-Content-Type: text/markdown |
| 24 |
License-File: LICENSE |
| 25 |
Requires-Dist: six>=1.9.0 |
| 26 |
Provides-Extra: gmpy2 |
| 27 |
Requires-Dist: gmpy2; extra == "gmpy2" |
| 28 |
Provides-Extra: gmpy |
| 29 |
Requires-Dist: gmpy; extra == "gmpy" |
| 30 |
|
| 31 |
# Pure-Python ECDSA and ECDH |
| 32 |
|
| 33 |
[](https://github.com/tlsfuzzer/python-ecdsa/actions/workflows/ci.yml) |
| 34 |
[](https://ecdsa.readthedocs.io/en/latest/?badge=latest) |
| 35 |
[](https://coveralls.io/github/tlsfuzzer/python-ecdsa?branch=master) |
| 36 |
 |
| 37 |
 |
| 38 |
[](https://github.com/tlsfuzzer/python-ecdsa/actions/workflows/codeql.yml) |
| 39 |
[](https://pypi.python.org/pypi/ecdsa/) |
| 40 |
 |
| 41 |
|
| 42 |
|
| 43 |
This is an easy-to-use implementation of ECC (Elliptic Curve Cryptography) |
| 44 |
with support for ECDSA (Elliptic Curve Digital Signature Algorithm), |
| 45 |
EdDSA (Edwards-curve Digital Signature Algorithm) and ECDH |
| 46 |
(Elliptic Curve Diffie-Hellman), implemented purely in Python, released under |
| 47 |
the MIT license. With this library, you can quickly create key pairs (signing |
| 48 |
key and verifying key), sign messages, and verify the signatures. You can |
| 49 |
also agree on a shared secret key based on exchanged public keys. |
| 50 |
The keys and signatures are very short, making them easy to handle and |
| 51 |
incorporate into other protocols. |
| 52 |
|
| 53 |
**NOTE: This library should not be used in production settings, see [Security](#Security) for more details.** |
| 54 |
|
| 55 |
## Features |
| 56 |
|
| 57 |
This library provides key generation, signing, verifying, and shared secret |
| 58 |
derivation for five |
| 59 |
popular NIST "Suite B" GF(p) (_prime field_) curves, with key lengths of 192, |
| 60 |
224, 256, 384, and 521 bits. The "short names" for these curves, as known by |
| 61 |
the OpenSSL tool (`openssl ecparam -list_curves`), are: `prime192v1`, |
| 62 |
`secp224r1`, `prime256v1`, `secp384r1`, and `secp521r1`. It includes the |
| 63 |
256-bit curve `secp256k1` used by Bitcoin. There is also support for the |
| 64 |
regular (non-twisted) variants of Brainpool curves from 160 to 512 bits. The |
| 65 |
"short names" of those curves are: `brainpoolP160r1`, `brainpoolP192r1`, |
| 66 |
`brainpoolP224r1`, `brainpoolP256r1`, `brainpoolP320r1`, `brainpoolP384r1`, |
| 67 |
`brainpoolP512r1`. Few of the small curves from SEC standard are also |
| 68 |
included (mainly to speed-up testing of the library), those are: |
| 69 |
`secp112r1`, `secp112r2`, `secp128r1`, and `secp160r1`. |
| 70 |
Key generation, siging and verifying is also supported for Ed25519 and |
| 71 |
Ed448 curves. |
| 72 |
No other curves are included, but it is not too hard to add support for more |
| 73 |
curves over prime fields. |
| 74 |
|
| 75 |
## Dependencies |
| 76 |
|
| 77 |
This library uses only Python and the 'six' package. It is compatible with |
| 78 |
Python 2.6, 2.7, and 3.6+. It also supports execution on alternative |
| 79 |
implementations like pypy and pypy3. |
| 80 |
|
| 81 |
If `gmpy2` or `gmpy` is installed, they will be used for faster arithmetic. |
| 82 |
Either of them can be installed after this library is installed, |
| 83 |
`python-ecdsa` will detect their presence on start-up and use them |
| 84 |
automatically. |
| 85 |
You should prefer `gmpy2` on Python3 for optimal performance. |
| 86 |
|
| 87 |
To run the OpenSSL compatibility tests, the 'openssl' tool must be in your |
| 88 |
`PATH`. This release has been tested successfully against OpenSSL 0.9.8o, |
| 89 |
1.0.0a, 1.0.2f, 1.1.1d and 3.0.1 (among others). |
| 90 |
|
| 91 |
|
| 92 |
## Installation |
| 93 |
|
| 94 |
This library is available on PyPI, it's recommended to install it using `pip`: |
| 95 |
|
| 96 |
``` |
| 97 |
pip install ecdsa |
| 98 |
``` |
| 99 |
|
| 100 |
In case higher performance is wanted and using native code is not a problem, |
| 101 |
it's possible to specify installation together with `gmpy2`: |
| 102 |
|
| 103 |
``` |
| 104 |
pip install ecdsa[gmpy2] |
| 105 |
``` |
| 106 |
|
| 107 |
or (slower, legacy option): |
| 108 |
``` |
| 109 |
pip install ecdsa[gmpy] |
| 110 |
``` |
| 111 |
|
| 112 |
## Speed |
| 113 |
|
| 114 |
The following table shows how long this library takes to generate key pairs |
| 115 |
(`keygen`), to sign data (`sign`), to verify those signatures (`verify`), |
| 116 |
to derive a shared secret (`ecdh`), and |
| 117 |
to verify the signatures with no key-specific precomputation (`no PC verify`). |
| 118 |
All those values are in seconds. |
| 119 |
For convenience, the inverses of those values are also provided: |
| 120 |
how many keys per second can be generated (`keygen/s`), how many signatures |
| 121 |
can be made per second (`sign/s`), how many signatures can be verified |
| 122 |
per second (`verify/s`), how many shared secrets can be derived per second |
| 123 |
(`ecdh/s`), and how many signatures with no key specific |
| 124 |
precomputation can be verified per second (`no PC verify/s`). The size of raw |
| 125 |
signature (generally the smallest |
| 126 |
the way a signature can be encoded) is also provided in the `siglen` column. |
| 127 |
Use `tox -e speed` to generate this table on your own computer. |
| 128 |
On an Intel Core i7 4790K @ 4.0GHz I'm getting the following performance: |
| 129 |
|
| 130 |
``` |
| 131 |
siglen keygen keygen/s sign sign/s verify verify/s no PC verify no PC verify/s |
| 132 |
NIST192p: 48 0.00032s 3134.06 0.00033s 2985.53 0.00063s 1598.36 0.00129s 774.43 |
| 133 |
NIST224p: 56 0.00040s 2469.24 0.00042s 2367.88 0.00081s 1233.41 0.00170s 586.66 |
| 134 |
NIST256p: 64 0.00051s 1952.73 0.00054s 1867.80 0.00098s 1021.86 0.00212s 471.27 |
| 135 |
NIST384p: 96 0.00107s 935.92 0.00111s 904.23 0.00203s 491.77 0.00446s 224.00 |
| 136 |
NIST521p: 132 0.00210s 475.52 0.00215s 464.16 0.00398s 251.28 0.00874s 114.39 |
| 137 |
SECP256k1: 64 0.00052s 1921.54 0.00054s 1847.49 0.00105s 948.68 0.00210s 477.01 |
| 138 |
BRAINPOOLP160r1: 40 0.00025s 4003.88 0.00026s 3845.12 0.00053s 1893.93 0.00105s 949.92 |
| 139 |
BRAINPOOLP192r1: 48 0.00033s 3043.97 0.00034s 2975.98 0.00063s 1581.50 0.00135s 742.29 |
| 140 |
BRAINPOOLP224r1: 56 0.00041s 2436.44 0.00043s 2315.51 0.00078s 1278.49 0.00180s 556.16 |
| 141 |
BRAINPOOLP256r1: 64 0.00053s 1892.49 0.00054s 1846.24 0.00114s 875.64 0.00229s 437.25 |
| 142 |
BRAINPOOLP320r1: 80 0.00073s 1361.26 0.00076s 1309.25 0.00143s 699.29 0.00322s 310.49 |
| 143 |
BRAINPOOLP384r1: 96 0.00107s 931.29 0.00111s 901.80 0.00230s 434.19 0.00476s 210.20 |
| 144 |
BRAINPOOLP512r1: 128 0.00207s 483.41 0.00212s 471.42 0.00425s 235.43 0.00912s 109.61 |
| 145 |
SECP112r1: 28 0.00015s 6672.53 0.00016s 6440.34 0.00031s 3265.41 0.00056s 1774.20 |
| 146 |
SECP112r2: 28 0.00015s 6697.11 0.00015s 6479.98 0.00028s 3524.72 0.00058s 1716.16 |
| 147 |
SECP128r1: 32 0.00018s 5497.65 0.00019s 5272.89 0.00036s 2747.39 0.00072s 1396.16 |
| 148 |
SECP160r1: 42 0.00025s 3949.32 0.00026s 3894.45 0.00046s 2153.85 0.00102s 985.07 |
| 149 |
Ed25519: 64 0.00076s 1324.48 0.00042s 2405.01 0.00109s 918.05 0.00344s 290.50 |
| 150 |
Ed448: 114 0.00176s 569.53 0.00115s 870.94 0.00282s 355.04 0.01024s 97.69 |
| 151 |
|
| 152 |
ecdh ecdh/s |
| 153 |
NIST192p: 0.00104s 964.89 |
| 154 |
NIST224p: 0.00134s 748.63 |
| 155 |
NIST256p: 0.00170s 587.08 |
| 156 |
NIST384p: 0.00352s 283.90 |
| 157 |
NIST521p: 0.00717s 139.51 |
| 158 |
SECP256k1: 0.00154s 648.40 |
| 159 |
BRAINPOOLP160r1: 0.00082s 1220.70 |
| 160 |
BRAINPOOLP192r1: 0.00105s 956.75 |
| 161 |
BRAINPOOLP224r1: 0.00136s 734.52 |
| 162 |
BRAINPOOLP256r1: 0.00178s 563.32 |
| 163 |
BRAINPOOLP320r1: 0.00252s 397.23 |
| 164 |
BRAINPOOLP384r1: 0.00376s 266.27 |
| 165 |
BRAINPOOLP512r1: 0.00733s 136.35 |
| 166 |
SECP112r1: 0.00046s 2180.40 |
| 167 |
SECP112r2: 0.00045s 2229.14 |
| 168 |
SECP128r1: 0.00054s 1868.15 |
| 169 |
SECP160r1: 0.00080s 1243.98 |
| 170 |
``` |
| 171 |
|
| 172 |
To test performance with `gmpy2` loaded, use `tox -e speedgmpy2`. |
| 173 |
On the same machine I'm getting the following performance with `gmpy2`: |
| 174 |
``` |
| 175 |
siglen keygen keygen/s sign sign/s verify verify/s no PC verify no PC verify/s |
| 176 |
NIST192p: 48 0.00017s 5933.40 0.00017s 5751.70 0.00032s 3125.28 0.00067s 1502.41 |
| 177 |
NIST224p: 56 0.00021s 4782.87 0.00022s 4610.05 0.00040s 2487.04 0.00089s 1126.90 |
| 178 |
NIST256p: 64 0.00023s 4263.98 0.00024s 4125.16 0.00045s 2200.88 0.00098s 1016.82 |
| 179 |
NIST384p: 96 0.00041s 2449.54 0.00042s 2399.96 0.00083s 1210.57 0.00172s 581.43 |
| 180 |
NIST521p: 132 0.00071s 1416.07 0.00072s 1389.81 0.00144s 692.93 0.00312s 320.40 |
| 181 |
SECP256k1: 64 0.00024s 4245.05 0.00024s 4122.09 0.00045s 2206.40 0.00094s 1068.32 |
| 182 |
BRAINPOOLP160r1: 40 0.00014s 6939.17 0.00015s 6681.55 0.00029s 3452.43 0.00057s 1769.81 |
| 183 |
BRAINPOOLP192r1: 48 0.00017s 5920.05 0.00017s 5774.36 0.00034s 2979.00 0.00069s 1453.19 |
| 184 |
BRAINPOOLP224r1: 56 0.00021s 4732.12 0.00022s 4622.65 0.00041s 2422.47 0.00087s 1149.87 |
| 185 |
BRAINPOOLP256r1: 64 0.00024s 4233.02 0.00024s 4115.20 0.00047s 2143.27 0.00098s 1015.60 |
| 186 |
BRAINPOOLP320r1: 80 0.00032s 3162.38 0.00032s 3077.62 0.00063s 1598.83 0.00136s 737.34 |
| 187 |
BRAINPOOLP384r1: 96 0.00041s 2436.88 0.00042s 2395.62 0.00083s 1202.68 0.00178s 562.85 |
| 188 |
BRAINPOOLP512r1: 128 0.00063s 1587.60 0.00064s 1558.83 0.00125s 799.96 0.00281s 355.83 |
| 189 |
SECP112r1: 28 0.00009s 11118.66 0.00009s 10775.48 0.00018s 5456.00 0.00033s 3020.83 |
| 190 |
SECP112r2: 28 0.00009s 11322.97 0.00009s 10857.71 0.00017s 5748.77 0.00032s 3094.28 |
| 191 |
SECP128r1: 32 0.00010s 10078.39 0.00010s 9665.27 0.00019s 5200.58 0.00036s 2760.88 |
| 192 |
SECP160r1: 42 0.00015s 6875.51 0.00015s 6647.35 0.00029s 3422.41 0.00057s 1768.35 |
| 193 |
Ed25519: 64 0.00030s 3322.56 0.00018s 5568.63 0.00046s 2165.35 0.00153s 654.02 |
| 194 |
Ed448: 114 0.00060s 1680.53 0.00039s 2567.40 0.00096s 1036.67 0.00350s 285.62 |
| 195 |
|
| 196 |
ecdh ecdh/s |
| 197 |
NIST192p: 0.00050s 1985.70 |
| 198 |
NIST224p: 0.00066s 1524.16 |
| 199 |
NIST256p: 0.00071s 1413.07 |
| 200 |
NIST384p: 0.00127s 788.89 |
| 201 |
NIST521p: 0.00230s 434.85 |
| 202 |
SECP256k1: 0.00071s 1409.95 |
| 203 |
BRAINPOOLP160r1: 0.00042s 2374.65 |
| 204 |
BRAINPOOLP192r1: 0.00051s 1960.01 |
| 205 |
BRAINPOOLP224r1: 0.00066s 1518.37 |
| 206 |
BRAINPOOLP256r1: 0.00071s 1399.90 |
| 207 |
BRAINPOOLP320r1: 0.00100s 997.21 |
| 208 |
BRAINPOOLP384r1: 0.00129s 777.51 |
| 209 |
BRAINPOOLP512r1: 0.00210s 475.99 |
| 210 |
SECP112r1: 0.00022s 4457.70 |
| 211 |
SECP112r2: 0.00024s 4252.33 |
| 212 |
SECP128r1: 0.00028s 3589.31 |
| 213 |
SECP160r1: 0.00043s 2305.02 |
| 214 |
``` |
| 215 |
|
| 216 |
(there's also `gmpy` version, execute it using `tox -e speedgmpy`) |
| 217 |
|
| 218 |
For comparison, a highly optimised implementation (including curve-specific |
| 219 |
assembly for some curves), like the one in OpenSSL 1.1.1d, provides the |
| 220 |
following performance numbers on the same machine. |
| 221 |
Run `openssl speed ecdsa` and `openssl speed ecdh` to reproduce it: |
| 222 |
``` |
| 223 |
sign verify sign/s verify/s |
| 224 |
192 bits ecdsa (nistp192) 0.0002s 0.0002s 4785.6 5380.7 |
| 225 |
224 bits ecdsa (nistp224) 0.0000s 0.0001s 22475.6 9822.0 |
| 226 |
256 bits ecdsa (nistp256) 0.0000s 0.0001s 45069.6 14166.6 |
| 227 |
384 bits ecdsa (nistp384) 0.0008s 0.0006s 1265.6 1648.1 |
| 228 |
521 bits ecdsa (nistp521) 0.0003s 0.0005s 3753.1 1819.5 |
| 229 |
256 bits ecdsa (brainpoolP256r1) 0.0003s 0.0003s 2983.5 3333.2 |
| 230 |
384 bits ecdsa (brainpoolP384r1) 0.0008s 0.0007s 1258.8 1528.1 |
| 231 |
512 bits ecdsa (brainpoolP512r1) 0.0015s 0.0012s 675.1 860.1 |
| 232 |
|
| 233 |
sign verify sign/s verify/s |
| 234 |
253 bits EdDSA (Ed25519) 0.0000s 0.0001s 28217.9 10897.7 |
| 235 |
456 bits EdDSA (Ed448) 0.0003s 0.0005s 3926.5 2147.7 |
| 236 |
|
| 237 |
op op/s |
| 238 |
192 bits ecdh (nistp192) 0.0002s 4853.4 |
| 239 |
224 bits ecdh (nistp224) 0.0001s 15252.1 |
| 240 |
256 bits ecdh (nistp256) 0.0001s 18436.3 |
| 241 |
384 bits ecdh (nistp384) 0.0008s 1292.7 |
| 242 |
521 bits ecdh (nistp521) 0.0003s 2884.7 |
| 243 |
256 bits ecdh (brainpoolP256r1) 0.0003s 3066.5 |
| 244 |
384 bits ecdh (brainpoolP384r1) 0.0008s 1298.0 |
| 245 |
512 bits ecdh (brainpoolP512r1) 0.0014s 694.8 |
| 246 |
``` |
| 247 |
|
| 248 |
Keys and signature can be serialized in different ways (see Usage, below). |
| 249 |
For a NIST192p key, the three basic representations require strings of the |
| 250 |
following lengths (in bytes): |
| 251 |
|
| 252 |
to_string: signkey= 24, verifykey= 48, signature=48 |
| 253 |
compressed: signkey=n/a, verifykey= 25, signature=n/a |
| 254 |
DER: signkey=106, verifykey= 80, signature=55 |
| 255 |
PEM: signkey=278, verifykey=162, (no support for PEM signatures) |
| 256 |
|
| 257 |
## History |
| 258 |
|
| 259 |
In 2006, Peter Pearson announced his pure-python implementation of ECDSA in a |
| 260 |
[message to sci.crypt][1], available from his [download site][2]. In 2010, |
| 261 |
Brian Warner wrote a wrapper around this code, to make it a bit easier and |
| 262 |
safer to use. In 2020, Hubert Kario included an implementation of elliptic |
| 263 |
curve cryptography that uses Jacobian coordinates internally, improving |
| 264 |
performance about 20-fold. You are looking at the README for this wrapper. |
| 265 |
|
| 266 |
[1]: http://www.derkeiler.com/Newsgroups/sci.crypt/2006-01/msg00651.html |
| 267 |
[2]: http://webpages.charter.net/curryfans/peter/downloads.html |
| 268 |
|
| 269 |
## Testing |
| 270 |
|
| 271 |
To run the full test suite, do this: |
| 272 |
|
| 273 |
tox -e coverage |
| 274 |
|
| 275 |
On an Intel Core i7 4790K @ 4.0GHz, the tests take about 18 seconds to execute. |
| 276 |
The test suite uses |
| 277 |
[`hypothesis`](https://github.com/HypothesisWorks/hypothesis) so there is some |
| 278 |
inherent variability in the test suite execution time. |
| 279 |
|
| 280 |
One part of `test_pyecdsa.py` and `test_ecdh.py` checks compatibility with |
| 281 |
OpenSSL, by running the "openssl" CLI tool, make sure it's in your `PATH` if |
| 282 |
you want to test compatibility with it (if OpenSSL is missing, too old, or |
| 283 |
doesn't support all the curves supported in upstream releases you will see |
| 284 |
skipped tests in the above `coverage` run). |
| 285 |
|
| 286 |
## Security |
| 287 |
|
| 288 |
This library was not designed with security in mind. If you are processing |
| 289 |
data that needs to be protected we suggest you use a quality wrapper around |
| 290 |
OpenSSL. [pyca/cryptography](https://cryptography.io) is one example of such |
| 291 |
a wrapper. The primary use-case of this library is as a portable library for |
| 292 |
interoperability testing and as a teaching tool. |
| 293 |
|
| 294 |
**This library does not protect against side-channel attacks.** |
| 295 |
|
| 296 |
Do not allow attackers to measure how long it takes you to generate a key pair |
| 297 |
or sign a message. Do not allow attackers to run code on the same physical |
| 298 |
machine when key pair generation or signing is taking place (this includes |
| 299 |
virtual machines). Do not allow attackers to measure how much power your |
| 300 |
computer uses while generating the key pair or signing a message. Do not allow |
| 301 |
attackers to measure RF interference coming from your computer while generating |
| 302 |
a key pair or signing a message. Note: just loading the private key will cause |
| 303 |
key pair generation. Other operations or attack vectors may also be |
| 304 |
vulnerable to attacks. **For a sophisticated attacker observing just one |
| 305 |
operation with a private key will be sufficient to completely |
| 306 |
reconstruct the private key**. |
| 307 |
|
| 308 |
Please also note that any Pure-python cryptographic library will be vulnerable |
| 309 |
to the same side-channel attacks. This is because Python does not provide |
| 310 |
side-channel secure primitives (with the exception of |
| 311 |
[`hmac.compare_digest()`][3]), making side-channel secure programming |
| 312 |
impossible. |
| 313 |
|
| 314 |
This library depends upon a strong source of random numbers. Do not use it on |
| 315 |
a system where `os.urandom()` does not provide cryptographically secure |
| 316 |
random numbers. |
| 317 |
|
| 318 |
[3]: https://docs.python.org/3/library/hmac.html#hmac.compare_digest |
| 319 |
|
| 320 |
## Usage |
| 321 |
|
| 322 |
You start by creating a `SigningKey`. You can use this to sign data, by passing |
| 323 |
in data as a byte string and getting back the signature (also a byte string). |
| 324 |
You can also ask a `SigningKey` to give you the corresponding `VerifyingKey`. |
| 325 |
The `VerifyingKey` can be used to verify a signature, by passing it both the |
| 326 |
data string and the signature byte string: it either returns True or raises |
| 327 |
`BadSignatureError`. |
| 328 |
|
| 329 |
```python |
| 330 |
from ecdsa import SigningKey |
| 331 |
sk = SigningKey.generate() # uses NIST192p |
| 332 |
vk = sk.verifying_key |
| 333 |
signature = sk.sign(b"message") |
| 334 |
assert vk.verify(signature, b"message") |
| 335 |
``` |
| 336 |
|
| 337 |
Each `SigningKey`/`VerifyingKey` is associated with a specific curve, like |
| 338 |
NIST192p (the default one). Longer curves are more secure, but take longer to |
| 339 |
use, and result in longer keys and signatures. |
| 340 |
|
| 341 |
```python |
| 342 |
from ecdsa import SigningKey, NIST384p |
| 343 |
sk = SigningKey.generate(curve=NIST384p) |
| 344 |
vk = sk.verifying_key |
| 345 |
signature = sk.sign(b"message") |
| 346 |
assert vk.verify(signature, b"message") |
| 347 |
``` |
| 348 |
|
| 349 |
The `SigningKey` can be serialized into several different formats: the shortest |
| 350 |
is to call `s=sk.to_string()`, and then re-create it with |
| 351 |
`SigningKey.from_string(s, curve)` . This short form does not record the |
| 352 |
curve, so you must be sure to pass to `from_string()` the same curve you used |
| 353 |
for the original key. The short form of a NIST192p-based signing key is just 24 |
| 354 |
bytes long. If a point encoding is invalid or it does not lie on the specified |
| 355 |
curve, `from_string()` will raise `MalformedPointError`. |
| 356 |
|
| 357 |
```python |
| 358 |
from ecdsa import SigningKey, NIST384p |
| 359 |
sk = SigningKey.generate(curve=NIST384p) |
| 360 |
sk_string = sk.to_string() |
| 361 |
sk2 = SigningKey.from_string(sk_string, curve=NIST384p) |
| 362 |
print(sk_string.hex()) |
| 363 |
print(sk2.to_string().hex()) |
| 364 |
``` |
| 365 |
|
| 366 |
Note: while the methods are called `to_string()` the type they return is |
| 367 |
actually `bytes`, the "string" part is leftover from Python 2. |
| 368 |
|
| 369 |
`sk.to_pem()` and `sk.to_der()` will serialize the signing key into the same |
| 370 |
formats that OpenSSL uses. The PEM file looks like the familiar ASCII-armored |
| 371 |
`"-----BEGIN EC PRIVATE KEY-----"` base64-encoded format, and the DER format |
| 372 |
is a shorter binary form of the same data. |
| 373 |
`SigningKey.from_pem()/.from_der()` will undo this serialization. These |
| 374 |
formats include the curve name, so you do not need to pass in a curve |
| 375 |
identifier to the deserializer. In case the file is malformed `from_der()` |
| 376 |
and `from_pem()` will raise `UnexpectedDER` or` MalformedPointError`. |
| 377 |
|
| 378 |
```python |
| 379 |
from ecdsa import SigningKey, NIST384p |
| 380 |
sk = SigningKey.generate(curve=NIST384p) |
| 381 |
sk_pem = sk.to_pem() |
| 382 |
sk2 = SigningKey.from_pem(sk_pem) |
| 383 |
# sk and sk2 are the same key |
| 384 |
``` |
| 385 |
|
| 386 |
Likewise, the `VerifyingKey` can be serialized in the same way: |
| 387 |
`vk.to_string()/VerifyingKey.from_string()`, `to_pem()/from_pem()`, and |
| 388 |
`to_der()/from_der()`. The same `curve=` argument is needed for |
| 389 |
`VerifyingKey.from_string()`. |
| 390 |
|
| 391 |
```python |
| 392 |
from ecdsa import SigningKey, VerifyingKey, NIST384p |
| 393 |
sk = SigningKey.generate(curve=NIST384p) |
| 394 |
vk = sk.verifying_key |
| 395 |
vk_string = vk.to_string() |
| 396 |
vk2 = VerifyingKey.from_string(vk_string, curve=NIST384p) |
| 397 |
# vk and vk2 are the same key |
| 398 |
|
| 399 |
from ecdsa import SigningKey, VerifyingKey, NIST384p |
| 400 |
sk = SigningKey.generate(curve=NIST384p) |
| 401 |
vk = sk.verifying_key |
| 402 |
vk_pem = vk.to_pem() |
| 403 |
vk2 = VerifyingKey.from_pem(vk_pem) |
| 404 |
# vk and vk2 are the same key |
| 405 |
``` |
| 406 |
|
| 407 |
There are a couple of different ways to compute a signature. Fundamentally, |
| 408 |
ECDSA takes a number that represents the data being signed, and returns a |
| 409 |
pair of numbers that represent the signature. The `hashfunc=` argument to |
| 410 |
`sk.sign()` and `vk.verify()` is used to turn an arbitrary string into a |
| 411 |
fixed-length digest, which is then turned into a number that ECDSA can sign, |
| 412 |
and both sign and verify must use the same approach. The default value is |
| 413 |
`hashlib.sha1`, but if you use NIST256p or a longer curve, you can use |
| 414 |
`hashlib.sha256` instead. |
| 415 |
|
| 416 |
There are also multiple ways to represent a signature. The default |
| 417 |
`sk.sign()` and `vk.verify()` methods present it as a short string, for |
| 418 |
simplicity and minimal overhead. To use a different scheme, use the |
| 419 |
`sk.sign(sigencode=)` and `vk.verify(sigdecode=)` arguments. There are helper |
| 420 |
functions in the `ecdsa.util` module that can be useful here. |
| 421 |
|
| 422 |
It is also possible to create a `SigningKey` from a "seed", which is |
| 423 |
deterministic. This can be used in protocols where you want to derive |
| 424 |
consistent signing keys from some other secret, for example when you want |
| 425 |
three separate keys and only want to store a single master secret. You should |
| 426 |
start with a uniformly-distributed unguessable seed with about `curve.baselen` |
| 427 |
bytes of entropy, and then use one of the helper functions in `ecdsa.util` to |
| 428 |
convert it into an integer in the correct range, and then finally pass it |
| 429 |
into `SigningKey.from_secret_exponent()`, like this: |
| 430 |
|
| 431 |
```python |
| 432 |
import os |
| 433 |
from ecdsa import NIST384p, SigningKey |
| 434 |
from ecdsa.util import randrange_from_seed__trytryagain |
| 435 |
|
| 436 |
def make_key(seed): |
| 437 |
secexp = randrange_from_seed__trytryagain(seed, NIST384p.order) |
| 438 |
return SigningKey.from_secret_exponent(secexp, curve=NIST384p) |
| 439 |
|
| 440 |
seed = os.urandom(NIST384p.baselen) # or other starting point |
| 441 |
sk1a = make_key(seed) |
| 442 |
sk1b = make_key(seed) |
| 443 |
# note: sk1a and sk1b are the same key |
| 444 |
assert sk1a.to_string() == sk1b.to_string() |
| 445 |
sk2 = make_key(b"2-"+seed) # different key |
| 446 |
assert sk1a.to_string() != sk2.to_string() |
| 447 |
``` |
| 448 |
|
| 449 |
In case the application will verify a lot of signatures made with a single |
| 450 |
key, it's possible to precompute some of the internal values to make |
| 451 |
signature verification significantly faster. The break-even point occurs at |
| 452 |
about 100 signatures verified. |
| 453 |
|
| 454 |
To perform precomputation, you can call the `precompute()` method |
| 455 |
on `VerifyingKey` instance: |
| 456 |
```python |
| 457 |
from ecdsa import SigningKey, NIST384p |
| 458 |
sk = SigningKey.generate(curve=NIST384p) |
| 459 |
vk = sk.verifying_key |
| 460 |
vk.precompute() |
| 461 |
signature = sk.sign(b"message") |
| 462 |
assert vk.verify(signature, b"message") |
| 463 |
``` |
| 464 |
|
| 465 |
Once `precompute()` was called, all signature verifications with this key will |
| 466 |
be faster to execute. |
| 467 |
|
| 468 |
## OpenSSL Compatibility |
| 469 |
|
| 470 |
To produce signatures that can be verified by OpenSSL tools, or to verify |
| 471 |
signatures that were produced by those tools, use: |
| 472 |
|
| 473 |
```python |
| 474 |
# openssl ecparam -name prime256v1 -genkey -out sk.pem |
| 475 |
# openssl ec -in sk.pem -pubout -out vk.pem |
| 476 |
# echo "data for signing" > data |
| 477 |
# openssl dgst -sha256 -sign sk.pem -out data.sig data |
| 478 |
# openssl dgst -sha256 -verify vk.pem -signature data.sig data |
| 479 |
# openssl dgst -sha256 -prverify sk.pem -signature data.sig data |
| 480 |
|
| 481 |
import hashlib |
| 482 |
from ecdsa import SigningKey, VerifyingKey |
| 483 |
from ecdsa.util import sigencode_der, sigdecode_der |
| 484 |
|
| 485 |
with open("vk.pem") as f: |
| 486 |
vk = VerifyingKey.from_pem(f.read()) |
| 487 |
|
| 488 |
with open("data", "rb") as f: |
| 489 |
data = f.read() |
| 490 |
|
| 491 |
with open("data.sig", "rb") as f: |
| 492 |
signature = f.read() |
| 493 |
|
| 494 |
assert vk.verify(signature, data, hashlib.sha256, sigdecode=sigdecode_der) |
| 495 |
|
| 496 |
with open("sk.pem") as f: |
| 497 |
sk = SigningKey.from_pem(f.read(), hashlib.sha256) |
| 498 |
|
| 499 |
new_signature = sk.sign_deterministic(data, sigencode=sigencode_der) |
| 500 |
|
| 501 |
with open("data.sig2", "wb") as f: |
| 502 |
f.write(new_signature) |
| 503 |
|
| 504 |
# openssl dgst -sha256 -verify vk.pem -signature data.sig2 data |
| 505 |
``` |
| 506 |
|
| 507 |
Note: if compatibility with OpenSSL 1.0.0 or earlier is necessary, the |
| 508 |
`sigencode_string` and `sigdecode_string` from `ecdsa.util` can be used for |
| 509 |
respectively writing and reading the signatures. |
| 510 |
|
| 511 |
The keys also can be written in format that openssl can handle: |
| 512 |
|
| 513 |
```python |
| 514 |
from ecdsa import SigningKey, VerifyingKey |
| 515 |
|
| 516 |
with open("sk.pem") as f: |
| 517 |
sk = SigningKey.from_pem(f.read()) |
| 518 |
with open("sk.pem", "wb") as f: |
| 519 |
f.write(sk.to_pem()) |
| 520 |
|
| 521 |
with open("vk.pem") as f: |
| 522 |
vk = VerifyingKey.from_pem(f.read()) |
| 523 |
with open("vk.pem", "wb") as f: |
| 524 |
f.write(vk.to_pem()) |
| 525 |
``` |
| 526 |
|
| 527 |
## Entropy |
| 528 |
|
| 529 |
Creating a signing key with `SigningKey.generate()` requires some form of |
| 530 |
entropy (as opposed to |
| 531 |
`from_secret_exponent`/`from_string`/`from_der`/`from_pem`, |
| 532 |
which are deterministic and do not require an entropy source). The default |
| 533 |
source is `os.urandom()`, but you can pass any other function that behaves |
| 534 |
like `os.urandom` as the `entropy=` argument to do something different. This |
| 535 |
may be useful in unit tests, where you want to achieve repeatable results. The |
| 536 |
`ecdsa.util.PRNG` utility is handy here: it takes a seed and produces a strong |
| 537 |
pseudo-random stream from it: |
| 538 |
|
| 539 |
```python |
| 540 |
from ecdsa.util import PRNG |
| 541 |
from ecdsa import SigningKey |
| 542 |
rng1 = PRNG(b"seed") |
| 543 |
sk1 = SigningKey.generate(entropy=rng1) |
| 544 |
rng2 = PRNG(b"seed") |
| 545 |
sk2 = SigningKey.generate(entropy=rng2) |
| 546 |
# sk1 and sk2 are the same key |
| 547 |
``` |
| 548 |
|
| 549 |
Likewise, ECDSA signature generation requires a random number, and each |
| 550 |
signature must use a different one (using the same number twice will |
| 551 |
immediately reveal the private signing key). The `sk.sign()` method takes an |
| 552 |
`entropy=` argument which behaves the same as `SigningKey.generate(entropy=)`. |
| 553 |
|
| 554 |
## Deterministic Signatures |
| 555 |
|
| 556 |
If you call `SigningKey.sign_deterministic(data)` instead of `.sign(data)`, |
| 557 |
the code will generate a deterministic signature instead of a random one. |
| 558 |
This uses the algorithm from RFC6979 to safely generate a unique `k` value, |
| 559 |
derived from the private key and the message being signed. Each time you sign |
| 560 |
the same message with the same key, you will get the same signature (using |
| 561 |
the same `k`). |
| 562 |
|
| 563 |
This may become the default in a future version, as it is not vulnerable to |
| 564 |
failures of the entropy source. |
| 565 |
|
| 566 |
## Examples |
| 567 |
|
| 568 |
Create a NIST192p key pair and immediately save both to disk: |
| 569 |
|
| 570 |
```python |
| 571 |
from ecdsa import SigningKey |
| 572 |
sk = SigningKey.generate() |
| 573 |
vk = sk.verifying_key |
| 574 |
with open("private.pem", "wb") as f: |
| 575 |
f.write(sk.to_pem()) |
| 576 |
with open("public.pem", "wb") as f: |
| 577 |
f.write(vk.to_pem()) |
| 578 |
``` |
| 579 |
|
| 580 |
Load a signing key from disk, use it to sign a message (using SHA-1), and write |
| 581 |
the signature to disk: |
| 582 |
|
| 583 |
```python |
| 584 |
from ecdsa import SigningKey |
| 585 |
with open("private.pem") as f: |
| 586 |
sk = SigningKey.from_pem(f.read()) |
| 587 |
with open("message", "rb") as f: |
| 588 |
message = f.read() |
| 589 |
sig = sk.sign(message) |
| 590 |
with open("signature", "wb") as f: |
| 591 |
f.write(sig) |
| 592 |
``` |
| 593 |
|
| 594 |
Load the verifying key, message, and signature from disk, and verify the |
| 595 |
signature (assume SHA-1 hash): |
| 596 |
|
| 597 |
```python |
| 598 |
from ecdsa import VerifyingKey, BadSignatureError |
| 599 |
vk = VerifyingKey.from_pem(open("public.pem").read()) |
| 600 |
with open("message", "rb") as f: |
| 601 |
message = f.read() |
| 602 |
with open("signature", "rb") as f: |
| 603 |
sig = f.read() |
| 604 |
try: |
| 605 |
vk.verify(sig, message) |
| 606 |
print "good signature" |
| 607 |
except BadSignatureError: |
| 608 |
print "BAD SIGNATURE" |
| 609 |
``` |
| 610 |
|
| 611 |
Create a NIST521p key pair: |
| 612 |
|
| 613 |
```python |
| 614 |
from ecdsa import SigningKey, NIST521p |
| 615 |
sk = SigningKey.generate(curve=NIST521p) |
| 616 |
vk = sk.verifying_key |
| 617 |
``` |
| 618 |
|
| 619 |
Create three independent signing keys from a master seed: |
| 620 |
|
| 621 |
```python |
| 622 |
from ecdsa import NIST192p, SigningKey |
| 623 |
from ecdsa.util import randrange_from_seed__trytryagain |
| 624 |
|
| 625 |
def make_key_from_seed(seed, curve=NIST192p): |
| 626 |
secexp = randrange_from_seed__trytryagain(seed, curve.order) |
| 627 |
return SigningKey.from_secret_exponent(secexp, curve) |
| 628 |
|
| 629 |
sk1 = make_key_from_seed("1:%s" % seed) |
| 630 |
sk2 = make_key_from_seed("2:%s" % seed) |
| 631 |
sk3 = make_key_from_seed("3:%s" % seed) |
| 632 |
``` |
| 633 |
|
| 634 |
Load a verifying key from disk and print it using hex encoding in |
| 635 |
uncompressed and compressed format (defined in X9.62 and SEC1 standards): |
| 636 |
|
| 637 |
```python |
| 638 |
from ecdsa import VerifyingKey |
| 639 |
|
| 640 |
with open("public.pem") as f: |
| 641 |
vk = VerifyingKey.from_pem(f.read()) |
| 642 |
|
| 643 |
print("uncompressed: {0}".format(vk.to_string("uncompressed").hex())) |
| 644 |
print("compressed: {0}".format(vk.to_string("compressed").hex())) |
| 645 |
``` |
| 646 |
|
| 647 |
Load a verifying key from a hex string from compressed format, output |
| 648 |
uncompressed: |
| 649 |
|
| 650 |
```python |
| 651 |
from ecdsa import VerifyingKey, NIST256p |
| 652 |
|
| 653 |
comp_str = '022799c0d0ee09772fdd337d4f28dc155581951d07082fb19a38aa396b67e77759' |
| 654 |
vk = VerifyingKey.from_string(bytearray.fromhex(comp_str), curve=NIST256p) |
| 655 |
print(vk.to_string("uncompressed").hex()) |
| 656 |
``` |
| 657 |
|
| 658 |
ECDH key exchange with remote party: |
| 659 |
|
| 660 |
```python |
| 661 |
from ecdsa import ECDH, NIST256p |
| 662 |
|
| 663 |
ecdh = ECDH(curve=NIST256p) |
| 664 |
ecdh.generate_private_key() |
| 665 |
local_public_key = ecdh.get_public_key() |
| 666 |
#send `local_public_key` to remote party and receive `remote_public_key` from remote party |
| 667 |
with open("remote_public_key.pem") as e: |
| 668 |
remote_public_key = e.read() |
| 669 |
ecdh.load_received_public_key_pem(remote_public_key) |
| 670 |
secret = ecdh.generate_sharedsecret_bytes() |
| 671 |
``` |