| 1 |
ECC |
| 2 |
=== |
| 3 |
|
| 4 |
ECC_ (Elliptic Curve Cryptography) is a modern and efficient type of public key cryptography. |
| 5 |
Its security is based on the difficulty to solve discrete logarithms |
| 6 |
on the field defined by specific equations computed over a curve. |
| 7 |
|
| 8 |
ECC can be used to create digital signatures or to perform a key exchange. |
| 9 |
|
| 10 |
Compared to traditional algorithms like RSA, an ECC key |
| 11 |
is significantly smaller at the same security level. |
| 12 |
For instance, a 3072-bit RSA key takes 768 bytes whereas the equally strong NIST P-256 |
| 13 |
private key only takes 32 bytes (that is, 256 bits). |
| 14 |
|
| 15 |
With this module you can generate new ECC keys:: |
| 16 |
|
| 17 |
>>> from Crypto.PublicKey import ECC |
| 18 |
>>> |
| 19 |
>>> mykey = ECC.generate(curve='p256') |
| 20 |
|
| 21 |
export an ECC private key and protect it with a password, so that it is |
| 22 |
resistant to brute force attacks:: |
| 23 |
|
| 24 |
>>> pwd = b'secret' |
| 25 |
>>> with open("myprivatekey.pem", "wt") as f: |
| 26 |
>>> data = mykey.export_key(format='PEM' |
| 27 |
passphrase=pwd, |
| 28 |
protection='PBKDF2WithHMAC-SHA512AndAES256-CBC', |
| 29 |
prot_params={'iteration_count':131072}) |
| 30 |
>>> f.write(data) |
| 31 |
|
| 32 |
and reimport it later:: |
| 33 |
|
| 34 |
>>> pwd = b'secret' |
| 35 |
>>> with open("myprivatekey.pem", "rt") as f: |
| 36 |
>>> data = f.read() |
| 37 |
>>> mykey = ECC.import_key(data, pwd) |
| 38 |
|
| 39 |
You can also export the public key, which is not sensitive:: |
| 40 |
|
| 41 |
>>> with open("mypublickey.pem", "wbt) as f: |
| 42 |
>>> data = mykey.public_key().export_key() |
| 43 |
|
| 44 |
.. _ECC table: |
| 45 |
.. csv-table:: |
| 46 |
:header: Curve, Strings accepted for the ``curve`` API parameter |
| 47 |
:widths: 20, 80 |
| 48 |
|
| 49 |
"NIST P-192", "``'NIST P-192'``, ``'p192'``, ``'P-192'``, ``'prime192v1'``, ``'secp192r1'``" |
| 50 |
"NIST P-224", "``'NIST P-224'``, ``'p224'``, ``'P-224'``, ``'prime224v1'``, ``'secp224r1'``" |
| 51 |
"NIST P-256", "``'NIST P-256'``, ``'p256'``, ``'P-256'``, ``'prime256v1'``, ``'secp256r1'``" |
| 52 |
"NIST P-384", "``'NIST P-384'``, ``'p384'``, ``'P-384'``, ``'prime384v1'``, ``'secp384r1'``" |
| 53 |
"NIST P-521", "``'NIST P-521'``, ``'p521'``, ``'P-521'``, ``'prime521v1'``, ``'secp521r1'``" |
| 54 |
"Ed25519", "``'ed25519'``, ``'Ed25519'``" |
| 55 |
"Ed448", "``'ed448'``, ``'Ed448'``" |
| 56 |
|
| 57 |
For more information about each NIST curve see `FIPS 186-4`_, Section D.1.2. |
| 58 |
|
| 59 |
The Ed25519 and the Ed448 curves are defined in RFC8032_. |
| 60 |
|
| 61 |
The ECC key can be used to perform or verify signatures, using the modules |
| 62 |
:mod:`Crypto.Signature.DSS` (ECDSA; NIST curves only) |
| 63 |
or :mod:`Crypto.Signature.eddsa` (EdDSA; Ed25519 and Ed448 curve only). |
| 64 |
|
| 65 |
.. _ECC: http://andrea.corbellini.name/2015/05/17/elliptic-curve-cryptography-a-gentle-introduction/ |
| 66 |
.. _`FIPS 186-4`: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf |
| 67 |
.. _RFC8032: https://datatracker.ietf.org/doc/html/rfc8032 |
| 68 |
|
| 69 |
.. automodule:: Crypto.PublicKey.ECC |
| 70 |
:members: |