ECC¶
ECC (Elliptic Curve Cryptography) is a modern and efficient type of public key cryptography. Its security is based on the difficulty to solve discrete logarithms on the field defined by specific equations computed over a curve.
ECC can be used to create digital signatures or to perform a key exchange.
Compared to traditional algorithms like RSA, an ECC key is significantly smaller at the same security level. For instance, a 3072bit RSA key takes 768 bytes whereas the equally strong NIST P256 private key only takes 32 bytes (that is, 256 bits).
This module provides mechanisms for generating new ECC keys, exporting and importing them using widely supported formats like PEM or DER.
Curve  Possible identifiers 

NIST P256  'NIST P256' , 'p256' , 'P256' , 'prime256v1' , 'secp256r1' 
NIST P384  'NIST P384' , 'p384' , 'P384' , 'prime384v1' , 'secp384r1' 
NIST P521  'NIST P521' , 'p521' , 'P521' , 'prime521v1' , 'secp521r1' 
For more information about each NIST curve see FIPS 1864, Section D.1.2.
The following example demonstrates how to generate a new ECC key, export it, and subsequently reload it back into the application:
>>> from Crypto.PublicKey import ECC
>>>
>>> key = ECC.generate(curve='P256')
>>>
>>> f = open('myprivatekey.pem','wt')
>>> f.write(key.export_key(format='PEM'))
>>> f.close()
...
>>> f = open('myprivatekey.pem','rt')
>>> key = ECC.import_key(f.read())
The ECC key can be used to perform or verify ECDSA signatures, using the module
Crypto.Signature.DSS
.

class
Crypto.PublicKey.ECC.
EccKey
(**kwargs)¶ Class defining an ECC key. Do not instantiate directly. Use
generate()
,construct()
orimport_key()
instead.Variables: 
export_key
(**kwargs)¶ Export this ECC key.
Parameters:  format (string) –
The format to use for encoding the key:
'DER'
. The key will be encoded in ASN.1 DER format (binary). For a public key, the ASN.1subjectPublicKeyInfo
structure defined in RFC5480 will be used. For a private key, the ASN.1ECPrivateKey
structure defined in RFC5915 is used instead (possibly within a PKCS#8 envelope, see theuse_pkcs8
flag below).'PEM'
. The key will be encoded in a PEM envelope (ASCII).'OpenSSH'
. The key will be encoded in the OpenSSH format (ASCII, public keys only).
 passphrase (byte string or string) – The passphrase to use for protecting the private key.
 use_pkcs8 (boolean) –
Only relevant for private keys.
If
True
(default and recommended), the PKCS#8 representation will be used.If
False
, the much weaker PEM encryption mechanism will be used.  protection (string) – When a private key is exported with passwordprotection
and PKCS#8 (both
DER
andPEM
formats), this parameter MUST be present and be a valid algorithm supported byCrypto.IO.PKCS8
. It is recommended to usePBKDF2WithHMACSHA1AndAES128CBC
.  compress (boolean) –
If
True
, a more compact representation of the public key with the Xcoordinate only is used.If
False
(default), the full public key will be exported.
Warning
If you don’t provide a passphrase, the private key will be exported in the clear!
Note
When exporting a private key with passwordprotection and PKCS#8 (both
DER
andPEM
formats), any extra parameters toexport_key()
will be passed toCrypto.IO.PKCS8
.Returns: A multiline string (for PEM and OpenSSH) or bytes (for DER) with the encoded key.  format (string) –

has_private
()¶ True
if this key can be used for making signatures or decrypting data.


class
Crypto.PublicKey.ECC.
EccPoint
(x, y, curve='p256')¶ A class to abstract a point over an Elliptic Curve.
The class support special methods for:
 Adding two points:
R = S + T
 Inplace addition:
S += T
 Negating a point:
R = T
 Comparing two points:
if S == T: ...
 Multiplying a point by a scalar:
R = S*k
 Inplace multiplication by a scalar:
T *= k
Variables:  x (integer) – The affine Xcoordinate of the ECC point
 y (integer) – The affine Ycoordinate of the ECC point
 xy – The tuple with X and Y coordinates

copy
()¶ Return a copy of this point.

double
()¶ Double this point (inplace operation).
Return: EccPoint
: this same object (to enable chaining)

is_point_at_infinity
()¶ True
if this is the pointatinfinity.

point_at_infinity
()¶ Return the pointatinfinity for the curve this point is on.

size_in_bits
()¶ Size of each coordinate, in bits.

size_in_bytes
()¶ Size of each coordinate, in bytes.
 Adding two points:

exception
Crypto.PublicKey.ECC.
UnsupportedEccFeature
¶

Crypto.PublicKey.ECC.
construct
(**kwargs)¶ Build a new ECC key (private or public) starting from some base components.
Parameters:  curve (string) – Mandatory. It must be a curve name defined in Table 1.
 d (integer) – Only for a private key. It must be in the range
[1..order1]
.  point_x (integer) – Mandatory for a public key. X coordinate (affine) of the ECC point.
 point_y (integer) – Mandatory for a public key. Y coordinate (affine) of the ECC point.
Returns: a new ECC key object
Return type:

Crypto.PublicKey.ECC.
generate
(**kwargs)¶ Generate a new private key on the given curve.
Parameters:  curve (string) – Mandatory. It must be a curve name defined in Table 1.
 randfunc (callable) – Optional. The RNG to read randomness from.
If
None
,Crypto.Random.get_random_bytes()
is used.

Crypto.PublicKey.ECC.
import_key
(encoded, passphrase=None)¶ Import an ECC key (public or private).
Parameters:  encoded (bytes or multiline string) –
The ECC key to import.
An ECC public key can be:
 An X.509 certificate, binary (DER) or ASCII (PEM)
 An X.509
subjectPublicKeyInfo
, binary (DER) or ASCII (PEM)  An OpenSSH line (e.g. the content of
~/.ssh/id_ecdsa
, ASCII)
An ECC private key can be:
 In binary format (DER, see section 3 of RFC5915 or PKCS#8)
 In ASCII format (PEM or OpenSSH 6.5+)
Private keys can be in the clear or passwordprotected.
 passphrase (byte string) – The passphrase to use for decrypting a private key. Encryption may be applied protected at the PEM level or at the PKCS#8 level. This parameter is ignored if the key in input is not encrypted.
Returns: a new ECC key object
Return type: Raises: ValueError
– when the given key cannot be parsed (possibly because the pass phrase is wrong). encoded (bytes or multiline string) –