| 1 |
Frequently Asked Questions |
| 2 |
-------------------------- |
| 3 |
|
| 4 |
Is CTR cipher mode compatible with Java? |
| 5 |
++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 6 |
|
| 7 |
Yes. When you instantiate your AES cipher in Java:: |
| 8 |
|
| 9 |
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); |
| 10 |
|
| 11 |
SecretKeySpec keySpec = new SecretKeySpec(new byte[16], "AES"); |
| 12 |
IvParameterSpec ivSpec = new IvParameterSpec(new byte[16]); |
| 13 |
|
| 14 |
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); |
| 15 |
|
| 16 |
You are effectively using :ref:`ctr_mode` without a fixed nonce and with |
| 17 |
a 128-bit big endian counter starting at 0. |
| 18 |
The counter will wrap around only after 2¹²⁸ blocks. |
| 19 |
|
| 20 |
You can replicate the same keystream in PyCryptodome with:: |
| 21 |
|
| 22 |
ivSpec = b'\x00' * 16 |
| 23 |
ctr = AES.new(keySpec, AES.MODE_CTR, initial_value=ivSpec, nonce=b'') |
| 24 |
|
| 25 |
Are RSASSA-PSS signatures compatible with Java? |
| 26 |
+++++++++++++++++++++++++++++++++++++++++++++++ |
| 27 |
|
| 28 |
Yes. For Java, you must consider that by default the |
| 29 |
mask is generated by MGF1 with SHA-1 (regardless of how you hash |
| 30 |
the message) and the salt is 20 bytes long. |
| 31 |
|
| 32 |
If you want to use another algorithm or another salt length, |
| 33 |
you must instantiate a ``PSSParameterSpec`` object, for instance:: |
| 34 |
|
| 35 |
Signature ss = Signature.getInstance("SHA256withRSA/PSS"); |
| 36 |
AlgorithmParameters pss1 = ss.getParameters(); |
| 37 |
PSSParameterSpec pssParameterSpec = new PSSParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), 32, 0xBC); |
| 38 |
ss.setParameter(spec1); |
| 39 |
|
| 40 |
Are RSASSA-PSS signatures compatible with OpenSSL? |
| 41 |
++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 42 |
|
| 43 |
Yes, but one quirk of OpenSSL (and of a few other libraries, |
| 44 |
especially if they are wrappers to OpenSSL) is that the salt |
| 45 |
length is computed in two possible ways: |
| 46 |
|
| 47 |
.. list-table:: |
| 48 |
|
| 49 |
* - **Salt length** |
| 50 |
- **Value for** ``EVP_PKEY_CTX_set_rsa_pss_saltlen()`` |
| 51 |
- ``openssl pkeyutl`` **command** |
| 52 |
* - Same as digest size |
| 53 |
- ``RSA_PSS_SALTLEN_DIGEST`` |
| 54 |
- ``-pkeyopt rsa_pss_saltlen:digest`` |
| 55 |
* - Maximized |
| 56 |
- ``RSA_PSS_SALTLEN_MAX`` |
| 57 |
- ``-pkeyopt rsa_pss_saltlen:max`` |
| 58 |
|
| 59 |
In PyCryptodome, the salt length matches the digest size by default |
| 60 |
(which is what `RFC8017 <https://tools.ietf.org/html/rfc8017#page-40>`_ recommends). |
| 61 |
However, you can also maximize the salt length with:: |
| 62 |
|
| 63 |
key = RSA.import_key(open('privkey.der').read()) |
| 64 |
h = SHA256.new(message) |
| 65 |
max_salt_bytes = key.size_in_bytes() - h.digest_size - 2 |
| 66 |
signature = pss.new(key, salt_bytes=max_salt_bytes).sign(h) |
| 67 |
|
| 68 |
Why do I get the error ``No module named Crypto`` on Windows? |
| 69 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 70 |
|
| 71 |
Check the directory where Python packages are installed, like:: |
| 72 |
|
| 73 |
/path/to/python/Lib/site-packages/ |
| 74 |
|
| 75 |
You might find a directory named ``crypto``, with all the PyCryptodome files in it. |
| 76 |
|
| 77 |
The most likely cause is described `here <https://github.com/dlitz/pycrypto/issues/156>`_ and you can fix the problem with:: |
| 78 |
|
| 79 |
pip uninstall crypto |
| 80 |
pip uninstall pycryptodome |
| 81 |
pip install pycryptodome |
| 82 |
|
| 83 |
The root cause is that, in the past, you most likely have installed an unrelated but similarly named package called `crypto <https://pypi.org/project/crypto/>`_, |
| 84 |
which happens to operate under the namespace ``crypto``. |
| 85 |
|
| 86 |
The Windows filesystem is **case-insensitive** so ``crypto`` and ``Crypto`` are effectively considered the same thing. |
| 87 |
When you subsequently install ``pycryptodome``, ``pip`` finds that a directory named with the target namespace already exists (under the rules of the underlying filesystem), |
| 88 |
and therefore installs all the sub-packages of ``pycryptodome`` in it. |
| 89 |
This is probably a reasonable behavior, if it wasn't that `pip does not issue any warning even if it could detect the issue <https://github.com/pypa/pip/issues/3309>`_. |
| 90 |
|
| 91 |
Why does ``strxor`` raise ``TypeError: argument 2 must be bytes, not bytearray``? |
| 92 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 93 |
|
| 94 |
Most probably you have installed both the ``pycryptodome`` and the old ``pycrypto`` packages. |
| 95 |
|
| 96 |
Run ``pip uninstall pycrypto`` and try again. |
| 97 |
|
| 98 |
The old PyCrypto shipped with a ``strxor`` module written as a native library (``.so`` or ``.dll`` file). |
| 99 |
If you install ``pycryptodome``, the old native module will still take priority over the new Python extension that comes in the latter. |
| 100 |
|
| 101 |
Why do I get a ``translation_unit_or_empty undefined`` error with ``pycparser``? |
| 102 |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 103 |
|
| 104 |
Unfortunately,``pycparser`` does not work with optimzed (``-O``) Python builds, |
| 105 |
which strips out the docstrings, causing this error. |
| 106 |
This is a `known issue <https://github.com/eliben/pycparser/issues/291>`_ and it will not be fixed. |
| 107 |
|
| 108 |
The possible workarounds are: |
| 109 |
|
| 110 |
* Do not run Python iwth ``-O`` |
| 111 |
* Remove `cffi` and ``cparser``. PyCryptodome will fall back to ``ctypes`` for interfacing with the native modules. |
| 112 |
* Use an earlier version of ``cparser`` (2.14) |