| 1 |
PKCS#8 |
| 2 |
====== |
| 3 |
|
| 4 |
`PKCS#8`_ is a standard for encoding asymmetric private keys, |
| 5 |
such as RSA or ECC, so that they can be stored or exchanged. |
| 6 |
The private key can either be encrypted with a passphrase or |
| 7 |
left in the clear. |
| 8 |
|
| 9 |
Example of how to encrypt an ECC private key (even though |
| 10 |
normally you would use the ``export_key`` method of the key itself):: |
| 11 |
|
| 12 |
from Crypto.PublicKey import ECC |
| 13 |
from Crypto.IO import PKCS8 |
| 14 |
|
| 15 |
key = ECC.generate(curve='p256') |
| 16 |
pkey = key.export_key(format='DER'), |
| 17 |
passphrase = b'secret santa' |
| 18 |
encrypted_key = PKCS8.wrap( |
| 19 |
pkey, |
| 20 |
"1.2.840.10045.2.1", # unrestricted ECC |
| 21 |
passphrase=passphrase, |
| 22 |
protection='PBKDF2WithHMAC-SHA512AndAES256-CBC', |
| 23 |
prot_params={'iteration_count': 210000} |
| 24 |
) |
| 25 |
|
| 26 |
.. _enc_params: |
| 27 |
|
| 28 |
Encryption parameters |
| 29 |
----------------------- |
| 30 |
|
| 31 |
When creating an encrypted PKCS#8 container, the two parameters |
| 32 |
``protection`` and ``prot_params`` drive the encryption algorithm: |
| 33 |
|
| 34 |
* ``protection`` (mandatory), a string that defines how the encryption |
| 35 |
key is derived from the passphrase, and which cipher to use. |
| 36 |
The string must follow one of the two patterns: |
| 37 |
|
| 38 |
#. ``'PBKDF2WithHMAC-'`` + **hash** + ``'And'`` + **cipher** |
| 39 |
#. ``'scryptAnd'`` + **cipher** |
| 40 |
|
| 41 |
where **hash** is the name of the cryptographic hash |
| 42 |
(recommended: ``'SHA512'``) and **cipher** is the name |
| 43 |
of the cipher mode to use (recommended: ``'AES256-CBC'``). |
| 44 |
|
| 45 |
Other values for **hash** are ``'SHA1'``, ``'SHA224'``, ``'SHA256'``, |
| 46 |
``'SHA384'``, ``'SHA512-224'``, ``'SHA512-256'``, ``'SHA3-224'``, |
| 47 |
``'SHA3-256'``, ``'SHA3-384'``, ``'SHA3-512'``. |
| 48 |
|
| 49 |
Other values for **cipher** are |
| 50 |
``'AES128-GCM'``, ``'AES192-GCM'``, ``'AES256-GCM'``, |
| 51 |
``'AES128-CBC'``, ``'AES192-CBC'`` or ``'DES-EDE3-CBC'``. |
| 52 |
|
| 53 |
* ``prot_params`` (optional), a dictionary to override the parameters of the |
| 54 |
key derivation function: |
| 55 |
|
| 56 |
+------------------+-----------------------------------------------+ |
| 57 |
| Key | Description | |
| 58 |
+==================+===============================================+ |
| 59 |
| iteration_count | The KDF algorithm is repeated several times to| |
| 60 |
| | slow down brute force attacks on passwords | |
| 61 |
| | (called *N* or CPU/memory cost in scrypt). | |
| 62 |
| | | |
| 63 |
| | **For PBKDF2 with SHA512 the recommended | |
| 64 |
| | value is 210 000** (default is 1 000). | |
| 65 |
| | | |
| 66 |
| | **For scrypt the recommended value is | |
| 67 |
| | 131 072** (default value is 16 384). | |
| 68 |
+------------------+-----------------------------------------------+ |
| 69 |
| salt_size | Salt is used to thwart dictionary and rainbow | |
| 70 |
| | attacks on passwords. The default value is 8 | |
| 71 |
| | bytes. | |
| 72 |
+------------------+-----------------------------------------------+ |
| 73 |
| block_size | *(scrypt only)* Memory-cost (r). The default | |
| 74 |
| | value is 8. | |
| 75 |
+------------------+-----------------------------------------------+ |
| 76 |
| parallelization | *(scrypt only)* CPU-cost (p). The default | |
| 77 |
| | value is 1. | |
| 78 |
+------------------+-----------------------------------------------+ |
| 79 |
|
| 80 |
|
| 81 |
Legacy algorithms |
| 82 |
----------------- |
| 83 |
|
| 84 |
The following ``protection`` mechanisms are only supported for importing keys. |
| 85 |
They are much weaker than the ones listed above, and they are provided |
| 86 |
for backward compatibility only: |
| 87 |
|
| 88 |
* ``pbeWithMD5AndRC2-CBC`` |
| 89 |
* ``pbeWithMD5AndDES-CBC`` |
| 90 |
* ``pbeWithSHA1AndRC2-CBC`` |
| 91 |
* ``pbeWithSHA1AndDES-CBC`` |
| 92 |
|
| 93 |
.. _`PKCS#8`: http://www.ietf.org/rfc/rfc5208.txt |
| 94 |
|
| 95 |
.. automodule:: Crypto.IO.PKCS8 |
| 96 |
:members: |