diff options
author | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-02-05 15:44:08 -0600 |
---|---|---|
committer | Paul Kehrer <paul.l.kehrer@gmail.com> | 2014-02-05 15:44:08 -0600 |
commit | c91f2392f9cd5b63e1d0440ce851db768944964a (patch) | |
tree | b21a3eab441b72ef3b1f1fe5545a8895c9c32702 | |
parent | 27864789563c90edb42772a9af1602be87029abc (diff) | |
parent | f970eaa676eb0cd89cdb2389f03d365899812822 (diff) | |
download | cryptography-c91f2392f9cd5b63e1d0440ce851db768944964a.tar.gz cryptography-c91f2392f9cd5b63e1d0440ce851db768944964a.tar.bz2 cryptography-c91f2392f9cd5b63e1d0440ce851db768944964a.zip |
Merge branch 'master' into urandom-engine
* master:
PKCS #1 RSA test vector loader
Removed pointless anchor
Docs need virtualenv as well
Everything about bash is the worst
Some reST markup nonsense
Fix for OS X
More clearly describe the behavior of constant_time.bytes_eq
Run the doc tests under OS X
Made OpenSSL's derive_pbkdf2_hmac raise the right exception
Document which backends implement which itnerfaces. Fixes #538
pep8
Fixed a typo in the docs
Make the default backend be a multi-backend
Conflicts:
tests/hazmat/backends/test_openssl.py
-rw-r--r-- | .travis.yml | 3 | ||||
-rwxr-xr-x | .travis/install.sh | 16 | ||||
-rw-r--r-- | cryptography/hazmat/backends/__init__.py | 6 | ||||
-rw-r--r-- | cryptography/hazmat/backends/openssl/backend.py | 7 | ||||
-rw-r--r-- | docs/hazmat/backends/interfaces.rst | 21 | ||||
-rw-r--r-- | docs/hazmat/backends/multibackend.rst | 4 | ||||
-rw-r--r-- | docs/hazmat/bindings/commoncrypto.rst | 2 | ||||
-rw-r--r-- | docs/hazmat/primitives/constant-time.rst | 13 | ||||
-rw-r--r-- | tests/hazmat/backends/test_openssl.py | 8 | ||||
-rw-r--r-- | tests/test_utils.py | 299 | ||||
-rw-r--r-- | tests/utils.py | 77 |
11 files changed, 436 insertions, 20 deletions
diff --git a/.travis.yml b/.travis.yml index b77a7b76..b7fa090e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -87,9 +87,6 @@ matrix: env: TOX_ENV=py3pep8 compiler: gcc - os: osx - env: TOX_ENV=docs - compiler: clang - - os: osx env: TOX_ENV=pep8 compiler: clang - os: osx diff --git a/.travis/install.sh b/.travis/install.sh index b6dd5acc..7e77fc87 100755 --- a/.travis/install.sh +++ b/.travis/install.sh @@ -16,9 +16,14 @@ if [[ "${OPENSSL}" == "0.9.8" ]]; then fi fi -if [[ "${TOX_ENV}" == "docs" && "$(name -s)" != "Darwin" ]]; then - sudo apt-get -y update - sudo apt-get install libenchant-dev +if [[ "${TOX_ENV}" == "docs" ]]; then + if [[ "$(uname -s)" == "Darwin" ]]; then + brew update + brew install enchant + else + sudo apt-get -y update + sudo apt-get install libenchant-dev + fi fi if [[ "$(uname -s)" == "Darwin" ]]; then @@ -51,6 +56,11 @@ if [[ "$(uname -s)" == "Darwin" ]]; then pyenv global 3.3.2 pip install virtualenv ;; + docs) + curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py + sudo python get-pip.py + sudo pip install virtualenv + ;; esac pyenv rehash else diff --git a/cryptography/hazmat/backends/__init__.py b/cryptography/hazmat/backends/__init__.py index d1b95f2a..41d260a8 100644 --- a/cryptography/hazmat/backends/__init__.py +++ b/cryptography/hazmat/backends/__init__.py @@ -12,6 +12,7 @@ # limitations under the License. from cryptography.hazmat.backends import openssl +from cryptography.hazmat.backends.multibackend import MultiBackend from cryptography.hazmat.bindings.commoncrypto.binding import ( Binding as CommonCryptoBinding ) @@ -23,5 +24,8 @@ if CommonCryptoBinding.is_available(): _ALL_BACKENDS.append(commoncrypto.backend) +_default_backend = MultiBackend(_ALL_BACKENDS) + + def default_backend(): - return openssl.backend + return _default_backend diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py index 31ffe0d1..f4b5c3ac 100644 --- a/cryptography/hazmat/backends/openssl/backend.py +++ b/cryptography/hazmat/backends/openssl/backend.py @@ -217,8 +217,11 @@ class Backend(object): ) assert res == 1 else: - # OpenSSL < 1.0.0 - assert isinstance(algorithm, hashes.SHA1) + if not isinstance(algorithm, hashes.SHA1): + raise UnsupportedAlgorithm( + "This version of OpenSSL only supports PBKDF2HMAC with " + "SHA1" + ) res = self._lib.PKCS5_PBKDF2_HMAC_SHA1( key_material, len(key_material), diff --git a/docs/hazmat/backends/interfaces.rst b/docs/hazmat/backends/interfaces.rst index 49e4c88c..5131ca12 100644 --- a/docs/hazmat/backends/interfaces.rst +++ b/docs/hazmat/backends/interfaces.rst @@ -19,6 +19,11 @@ A specific ``backend`` may provide one or more of these interfaces. A backend which provides methods for using ciphers for encryption and decryption. + The following backends implement this interface: + + * :doc:`/hazmat/backends/openssl` + * :doc:`/hazmat/backends/commoncrypto` + .. method:: cipher_supported(cipher, mode) Check if a ``cipher`` and ``mode`` combination is supported by @@ -76,6 +81,11 @@ A specific ``backend`` may provide one or more of these interfaces. A backend with methods for using cryptographic hash functions. + The following backends implement this interface: + + * :doc:`/hazmat/backends/openssl` + * :doc:`/hazmat/backends/commoncrypto` + .. method:: hash_supported(algorithm) Check if the specified ``algorithm`` is supported by this backend. @@ -107,6 +117,11 @@ A specific ``backend`` may provide one or more of these interfaces. A backend with methods for using cryptographic hash functions as message authentication codes. + The following backends implement this interface: + + * :doc:`/hazmat/backends/openssl` + * :doc:`/hazmat/backends/commoncrypto` + .. method:: hmac_supported(algorithm) Check if the specified ``algorithm`` is supported by this backend. @@ -139,6 +154,11 @@ A specific ``backend`` may provide one or more of these interfaces. A backend with methods for using PBKDF2 using HMAC as a PRF. + The following backends implement this interface: + + * :doc:`/hazmat/backends/openssl` + * :doc:`/hazmat/backends/commoncrypto` + .. method:: pbkdf2_hmac_supported(algorithm) Check if the specified ``algorithm`` is supported by this backend. @@ -171,4 +191,3 @@ A specific ``backend`` may provide one or more of these interfaces. the derived key. This is typically a password. :return bytes: Derived key. - diff --git a/docs/hazmat/backends/multibackend.rst b/docs/hazmat/backends/multibackend.rst index 63177bef..95538ac8 100644 --- a/docs/hazmat/backends/multibackend.rst +++ b/docs/hazmat/backends/multibackend.rst @@ -18,10 +18,10 @@ MultiBackend >>> from cryptography.hazmat.primitives import hashes >>> backend1.hash_supported(hashes.SHA256()) False - >>> backend2.hash_supported(hashes.SHA1()) + >>> backend2.hash_supported(hashes.SHA256()) True >>> multi_backend = MultiBackend([backend1, backend2]) - >>> multi_backend.hash_supported(hashes.SHA1()) + >>> multi_backend.hash_supported(hashes.SHA256()) True :param backends: A ``list`` of backend objects. Backends are checked for diff --git a/docs/hazmat/bindings/commoncrypto.rst b/docs/hazmat/bindings/commoncrypto.rst index c4f614c2..50dbe69a 100644 --- a/docs/hazmat/bindings/commoncrypto.rst +++ b/docs/hazmat/bindings/commoncrypto.rst @@ -27,4 +27,4 @@ Mac OS X. .. _`CFFI`: https://cffi.readthedocs.org/ -.. _`CommonCrypto`: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/Common%20Crypto.3cc.html#//apple_ref/doc/man/3cc/CommonCrypto +.. _`CommonCrypto`: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/Common%20Crypto.3cc.html diff --git a/docs/hazmat/primitives/constant-time.rst b/docs/hazmat/primitives/constant-time.rst index 632e7c68..c6fcb3a3 100644 --- a/docs/hazmat/primitives/constant-time.rst +++ b/docs/hazmat/primitives/constant-time.rst @@ -19,8 +19,10 @@ about the timing attacks on KeyCzar and Java's ``MessageDigest.isEqual()``. .. function:: bytes_eq(a, b) - Compare ``a`` and ``b`` to one another in constant time if they are of the - same length. + Compares ``a`` and ``b`` with one another. If ``a`` and ``b`` have + different lengths, this returns ``False`` immediately. Otherwise it + compares them in a way that takes the same amount of time, regardless of + how many characters are the same between the two. .. doctest:: @@ -30,9 +32,10 @@ about the timing attacks on KeyCzar and Java's ``MessageDigest.isEqual()``. >>> constant_time.bytes_eq(b"foo", b"bar") False - :param a bytes: The left-hand side. - :param b bytes: The right-hand side. - :returns boolean: True if ``a`` has the same bytes as ``b``. + :param bytes a: The left-hand side. + :param bytes b: The right-hand side. + :returns bool: ``True`` if ``a`` has the same bytes as ``b``, otherwise + ``False``. .. _`Coda Hale's blog post`: http://codahale.com/a-lesson-in-timing-attacks/ diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index ef7560f7..1fd513d5 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -20,7 +20,7 @@ import pytest from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm, InternalError from cryptography.hazmat.backends.openssl.backend import backend, Backend -from cryptography.hazmat.primitives import interfaces +from cryptography.hazmat.primitives import interfaces, hashes from cryptography.hazmat.primitives.ciphers import Cipher from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers.modes import CBC @@ -246,6 +246,12 @@ class TestOpenSSL(object): b"data not multiple of block length" ) + def test_derive_pbkdf2_raises_unsupported_on_old_openssl(self): + if backend.pbkdf2_hmac_supported(hashes.SHA256()): + pytest.skip("Requires an older OpenSSL") + with pytest.raises(UnsupportedAlgorithm): + backend.derive_pbkdf2_hmac(hashes.SHA256(), 10, b"", 1000, b"") + # This test is not in the next class because to check if it's really # default we don't want to run the setup_method before it def test_osrandom_engine_is_default(self): diff --git a/tests/test_utils.py b/tests/test_utils.py index 8ecb33f9..2f4a43c8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -21,7 +21,7 @@ import pytest from .utils import ( load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors, load_openssl_vectors, load_hash_vectors, check_for_iface, - check_backend_support, select_backends + check_backend_support, select_backends, load_pkcs1_vectors ) @@ -529,3 +529,300 @@ def test_load_nist_gcm_vectors(): 'ct': b'15c4db4cbb451211179d57017f', 'fail': True}, ] + + +def test_load_pkcs1_vectors(): + vector_data = textwrap.dedent(""" + Test vectors for RSA-PSS + ======================== + + This file contains an extract of the original pss-vect.txt + + Key lengths: + + Key 8: 1031 bits + Key 9: 1536 bits + =========================================================================== + + <snip> + + # Example 8: A 1031-bit RSA key pair + # ----------------------------------- + + + # Public key + # ---------- + + # Modulus: + 49 53 70 a1 fb 18 54 3c 16 d3 63 1e 31 63 25 5d + f6 2b e6 ee e8 90 d5 f2 55 09 e4 f7 78 a8 ea 6f + bb bc df 85 df f6 4e 0d 97 20 03 ab 36 81 fb ba + 6d d4 1f d5 41 82 9b 2e 58 2d e9 f2 a4 a4 e0 a2 + d0 90 0b ef 47 53 db 3c ee 0e e0 6c 7d fa e8 b1 + d5 3b 59 53 21 8f 9c ce ea 69 5b 08 66 8e de aa + dc ed 94 63 b1 d7 90 d5 eb f2 7e 91 15 b4 6c ad + 4d 9a 2b 8e fa b0 56 1b 08 10 34 47 39 ad a0 73 + 3f + + # Exponent: + 01 00 01 + + # Private key + # ----------- + + # Modulus: + 49 53 70 a1 fb 18 54 3c 16 d3 63 1e 31 63 25 5d + f6 2b e6 ee e8 90 d5 f2 55 09 e4 f7 78 a8 ea 6f + bb bc df 85 df f6 4e 0d 97 20 03 ab 36 81 fb ba + 6d d4 1f d5 41 82 9b 2e 58 2d e9 f2 a4 a4 e0 a2 + d0 90 0b ef 47 53 db 3c ee 0e e0 6c 7d fa e8 b1 + d5 3b 59 53 21 8f 9c ce ea 69 5b 08 66 8e de aa + dc ed 94 63 b1 d7 90 d5 eb f2 7e 91 15 b4 6c ad + 4d 9a 2b 8e fa b0 56 1b 08 10 34 47 39 ad a0 73 + 3f + + # Public exponent: + 01 00 01 + + # Exponent: + 6c 66 ff e9 89 80 c3 8f cd ea b5 15 98 98 83 61 + 65 f4 b4 b8 17 c4 f6 a8 d4 86 ee 4e a9 13 0f e9 + b9 09 2b d1 36 d1 84 f9 5f 50 4a 60 7e ac 56 58 + 46 d2 fd d6 59 7a 89 67 c7 39 6e f9 5a 6e ee bb + 45 78 a6 43 96 6d ca 4d 8e e3 de 84 2d e6 32 79 + c6 18 15 9c 1a b5 4a 89 43 7b 6a 61 20 e4 93 0a + fb 52 a4 ba 6c ed 8a 49 47 ac 64 b3 0a 34 97 cb + e7 01 c2 d6 26 6d 51 72 19 ad 0e c6 d3 47 db e9 + + # Prime 1: + 08 da d7 f1 13 63 fa a6 23 d5 d6 d5 e8 a3 19 32 + 8d 82 19 0d 71 27 d2 84 6c 43 9b 0a b7 26 19 b0 + a4 3a 95 32 0e 4e c3 4f c3 a9 ce a8 76 42 23 05 + bd 76 c5 ba 7b e9 e2 f4 10 c8 06 06 45 a1 d2 9e + db + + # Prime 2: + 08 47 e7 32 37 6f c7 90 0f 89 8e a8 2e b2 b0 fc + 41 85 65 fd ae 62 f7 d9 ec 4c e2 21 7b 97 99 0d + d2 72 db 15 7f 99 f6 3c 0d cb b9 fb ac db d4 c4 + da db 6d f6 77 56 35 8c a4 17 48 25 b4 8f 49 70 + 6d + + # Prime exponent 1: + 05 c2 a8 3c 12 4b 36 21 a2 aa 57 ea 2c 3e fe 03 + 5e ff 45 60 f3 3d de bb 7a da b8 1f ce 69 a0 c8 + c2 ed c1 65 20 dd a8 3d 59 a2 3b e8 67 96 3a c6 + 5f 2c c7 10 bb cf b9 6e e1 03 de b7 71 d1 05 fd + 85 + + # Prime exponent 2: + 04 ca e8 aa 0d 9f aa 16 5c 87 b6 82 ec 14 0b 8e + d3 b5 0b 24 59 4b 7a 3b 2c 22 0b 36 69 bb 81 9f + 98 4f 55 31 0a 1a e7 82 36 51 d4 a0 2e 99 44 79 + 72 59 51 39 36 34 34 e5 e3 0a 7e 7d 24 15 51 e1 + b9 + + # Coefficient: + 07 d3 e4 7b f6 86 60 0b 11 ac 28 3c e8 8d bb 3f + 60 51 e8 ef d0 46 80 e4 4c 17 1e f5 31 b8 0b 2b + 7c 39 fc 76 63 20 e2 cf 15 d8 d9 98 20 e9 6f f3 + 0d c6 96 91 83 9c 4b 40 d7 b0 6e 45 30 7d c9 1f + 3f + + # RSA-PSS signing of 6 random messages with random salts + # ------------------------------------------------------- + + <snip> + + # ============================================= + + # Example 9: A 1536-bit RSA key pair + # ----------------------------------- + + + # Public key + # ---------- + + # Modulus: + e6 bd 69 2a c9 66 45 79 04 03 fd d0 f5 be b8 b9 + bf 92 ed 10 00 7f c3 65 04 64 19 dd 06 c0 5c 5b + 5b 2f 48 ec f9 89 e4 ce 26 91 09 97 9c bb 40 b4 + a0 ad 24 d2 24 83 d1 ee 31 5a d4 cc b1 53 42 68 + 35 26 91 c5 24 f6 dd 8e 6c 29 d2 24 cf 24 69 73 + ae c8 6c 5b f6 b1 40 1a 85 0d 1b 9a d1 bb 8c bc + ec 47 b0 6f 0f 8c 7f 45 d3 fc 8f 31 92 99 c5 43 + 3d db c2 b3 05 3b 47 de d2 ec d4 a4 ca ef d6 14 + 83 3d c8 bb 62 2f 31 7e d0 76 b8 05 7f e8 de 3f + 84 48 0a d5 e8 3e 4a 61 90 4a 4f 24 8f b3 97 02 + 73 57 e1 d3 0e 46 31 39 81 5c 6f d4 fd 5a c5 b8 + 17 2a 45 23 0e cb 63 18 a0 4f 14 55 d8 4e 5a 8b + + # Exponent: + 01 00 01 + + # Private key + # ----------- + + # Modulus: + e6 bd 69 2a c9 66 45 79 04 03 fd d0 f5 be b8 b9 + bf 92 ed 10 00 7f c3 65 04 64 19 dd 06 c0 5c 5b + 5b 2f 48 ec f9 89 e4 ce 26 91 09 97 9c bb 40 b4 + a0 ad 24 d2 24 83 d1 ee 31 5a d4 cc b1 53 42 68 + 35 26 91 c5 24 f6 dd 8e 6c 29 d2 24 cf 24 69 73 + ae c8 6c 5b f6 b1 40 1a 85 0d 1b 9a d1 bb 8c bc + ec 47 b0 6f 0f 8c 7f 45 d3 fc 8f 31 92 99 c5 43 + 3d db c2 b3 05 3b 47 de d2 ec d4 a4 ca ef d6 14 + 83 3d c8 bb 62 2f 31 7e d0 76 b8 05 7f e8 de 3f + 84 48 0a d5 e8 3e 4a 61 90 4a 4f 24 8f b3 97 02 + 73 57 e1 d3 0e 46 31 39 81 5c 6f d4 fd 5a c5 b8 + 17 2a 45 23 0e cb 63 18 a0 4f 14 55 d8 4e 5a 8b + + # Public exponent: + 01 00 01 + + # Exponent: + 6a 7f d8 4f b8 5f ad 07 3b 34 40 6d b7 4f 8d 61 + a6 ab c1 21 96 a9 61 dd 79 56 5e 9d a6 e5 18 7b + ce 2d 98 02 50 f7 35 95 75 35 92 70 d9 15 90 bb + 0e 42 7c 71 46 0b 55 d5 14 10 b1 91 bc f3 09 fe + a1 31 a9 2c 8e 70 27 38 fa 71 9f 1e 00 41 f5 2e + 40 e9 1f 22 9f 4d 96 a1 e6 f1 72 e1 55 96 b4 51 + 0a 6d ae c2 61 05 f2 be bc 53 31 6b 87 bd f2 13 + 11 66 60 70 e8 df ee 69 d5 2c 71 a9 76 ca ae 79 + c7 2b 68 d2 85 80 dc 68 6d 9f 51 29 d2 25 f8 2b + 3d 61 55 13 a8 82 b3 db 91 41 6b 48 ce 08 88 82 + 13 e3 7e eb 9a f8 00 d8 1c ab 32 8c e4 20 68 99 + 03 c0 0c 7b 5f d3 1b 75 50 3a 6d 41 96 84 d6 29 + + # Prime 1: + f8 eb 97 e9 8d f1 26 64 ee fd b7 61 59 6a 69 dd + cd 0e 76 da ec e6 ed 4b f5 a1 b5 0a c0 86 f7 92 + 8a 4d 2f 87 26 a7 7e 51 5b 74 da 41 98 8f 22 0b + 1c c8 7a a1 fc 81 0c e9 9a 82 f2 d1 ce 82 1e dc + ed 79 4c 69 41 f4 2c 7a 1a 0b 8c 4d 28 c7 5e c6 + 0b 65 22 79 f6 15 4a 76 2a ed 16 5d 47 de e3 67 + + # Prime 2: + ed 4d 71 d0 a6 e2 4b 93 c2 e5 f6 b4 bb e0 5f 5f + b0 af a0 42 d2 04 fe 33 78 d3 65 c2 f2 88 b6 a8 + da d7 ef e4 5d 15 3e ef 40 ca cc 7b 81 ff 93 40 + 02 d1 08 99 4b 94 a5 e4 72 8c d9 c9 63 37 5a e4 + 99 65 bd a5 5c bf 0e fe d8 d6 55 3b 40 27 f2 d8 + 62 08 a6 e6 b4 89 c1 76 12 80 92 d6 29 e4 9d 3d + + # Prime exponent 1: + 2b b6 8b dd fb 0c 4f 56 c8 55 8b ff af 89 2d 80 + 43 03 78 41 e7 fa 81 cf a6 1a 38 c5 e3 9b 90 1c + 8e e7 11 22 a5 da 22 27 bd 6c de eb 48 14 52 c1 + 2a d3 d6 1d 5e 4f 77 6a 0a b5 56 59 1b ef e3 e5 + 9e 5a 7f dd b8 34 5e 1f 2f 35 b9 f4 ce e5 7c 32 + 41 4c 08 6a ec 99 3e 93 53 e4 80 d9 ee c6 28 9f + + # Prime exponent 2: + 4f f8 97 70 9f ad 07 97 46 49 45 78 e7 0f d8 54 + 61 30 ee ab 56 27 c4 9b 08 0f 05 ee 4a d9 f3 e4 + b7 cb a9 d6 a5 df f1 13 a4 1c 34 09 33 68 33 f1 + 90 81 6d 8a 6b c4 2e 9b ec 56 b7 56 7d 0f 3c 9c + 69 6d b6 19 b2 45 d9 01 dd 85 6d b7 c8 09 2e 77 + e9 a1 cc cd 56 ee 4d ba 42 c5 fd b6 1a ec 26 69 + + # Coefficient: + 77 b9 d1 13 7b 50 40 4a 98 27 29 31 6e fa fc 7d + fe 66 d3 4e 5a 18 26 00 d5 f3 0a 0a 85 12 05 1c + 56 0d 08 1d 4d 0a 18 35 ec 3d 25 a6 0f 4e 4d 6a + a9 48 b2 bf 3d bb 5b 12 4c bb c3 48 92 55 a3 a9 + 48 37 2f 69 78 49 67 45 f9 43 e1 db 4f 18 38 2c + ea a5 05 df c6 57 57 bb 3f 85 7a 58 dc e5 21 56 + + # RSA-PSS signing of 6 random messages with random salts + # ------------------------------------------------------- + + <snip> + + # ============================================= + + <snip> + """).splitlines() + + vectors = tuple(load_pkcs1_vectors(vector_data)) + expected = ( + ( + { + 'modulus': int( + '495370a1fb18543c16d3631e3163255df62be6eee890d5f25509e4f77' + '8a8ea6fbbbcdf85dff64e0d972003ab3681fbba6dd41fd541829b2e58' + '2de9f2a4a4e0a2d0900bef4753db3cee0ee06c7dfae8b1d53b5953218' + 'f9cceea695b08668edeaadced9463b1d790d5ebf27e9115b46cad4d9a' + '2b8efab0561b0810344739ada0733f', 16), + 'public_exponent': int('10001', 16), + 'private_exponent': int( + '6c66ffe98980c38fcdeab5159898836165f4b4b817c4f6a8d486ee4ea' + '9130fe9b9092bd136d184f95f504a607eac565846d2fdd6597a8967c7' + '396ef95a6eeebb4578a643966dca4d8ee3de842de63279c618159c1ab' + '54a89437b6a6120e4930afb52a4ba6ced8a4947ac64b30a3497cbe701' + 'c2d6266d517219ad0ec6d347dbe9', 16), + 'p': int( + '8dad7f11363faa623d5d6d5e8a319328d82190d7127d2846c439b0ab7' + '2619b0a43a95320e4ec34fc3a9cea876422305bd76c5ba7be9e2f410c' + '8060645a1d29edb', 16), + 'q': int( + '847e732376fc7900f898ea82eb2b0fc418565fdae62f7d9ec4ce2217b' + '97990dd272db157f99f63c0dcbb9fbacdbd4c4dadb6df67756358ca41' + '74825b48f49706d', 16) + }, + + { + 'modulus': int( + '495370a1fb18543c16d3631e3163255df62be6eee890d5f25509e4f77' + '8a8ea6fbbbcdf85dff64e0d972003ab3681fbba6dd41fd541829b2e58' + '2de9f2a4a4e0a2d0900bef4753db3cee0ee06c7dfae8b1d53b5953218' + 'f9cceea695b08668edeaadced9463b1d790d5ebf27e9115b46cad4d9a' + '2b8efab0561b0810344739ada0733f', 16), + 'public_exponent': int('10001', 16) + } + ), + ( + { + 'modulus': int( + 'e6bd692ac96645790403fdd0f5beb8b9bf92ed10007fc365046419dd0' + '6c05c5b5b2f48ecf989e4ce269109979cbb40b4a0ad24d22483d1ee31' + '5ad4ccb1534268352691c524f6dd8e6c29d224cf246973aec86c5bf6b' + '1401a850d1b9ad1bb8cbcec47b06f0f8c7f45d3fc8f319299c5433ddb' + 'c2b3053b47ded2ecd4a4caefd614833dc8bb622f317ed076b8057fe8d' + 'e3f84480ad5e83e4a61904a4f248fb397027357e1d30e463139815c6f' + 'd4fd5ac5b8172a45230ecb6318a04f1455d84e5a8b', 16), + 'public_exponent': int('10001', 16), + 'private_exponent': int( + '6a7fd84fb85fad073b34406db74f8d61a6abc12196a961dd79565e9da' + '6e5187bce2d980250f7359575359270d91590bb0e427c71460b55d514' + '10b191bcf309fea131a92c8e702738fa719f1e0041f52e40e91f229f4' + 'd96a1e6f172e15596b4510a6daec26105f2bebc53316b87bdf2131166' + '6070e8dfee69d52c71a976caae79c72b68d28580dc686d9f5129d225f' + '82b3d615513a882b3db91416b48ce08888213e37eeb9af800d81cab32' + '8ce420689903c00c7b5fd31b75503a6d419684d629', 16), + 'p': int( + 'f8eb97e98df12664eefdb761596a69ddcd0e76daece6ed4bf5a1b50ac' + '086f7928a4d2f8726a77e515b74da41988f220b1cc87aa1fc810ce99a' + '82f2d1ce821edced794c6941f42c7a1a0b8c4d28c75ec60b652279f61' + '54a762aed165d47dee367', 16), + 'q': int( + 'ed4d71d0a6e24b93c2e5f6b4bbe05f5fb0afa042d204fe3378d365c2f' + '288b6a8dad7efe45d153eef40cacc7b81ff934002d108994b94a5e472' + '8cd9c963375ae49965bda55cbf0efed8d6553b4027f2d86208a6e6b48' + '9c176128092d629e49d3d', 16) + }, + + { + 'modulus': int( + 'e6bd692ac96645790403fdd0f5beb8b9bf92ed10007fc365046419dd0' + '6c05c5b5b2f48ecf989e4ce269109979cbb40b4a0ad24d22483d1ee31' + '5ad4ccb1534268352691c524f6dd8e6c29d224cf246973aec86c5bf6b' + '1401a850d1b9ad1bb8cbcec47b06f0f8c7f45d3fc8f319299c5433ddb' + 'c2b3053b47ded2ecd4a4caefd614833dc8bb622f317ed076b8057fe8d' + 'e3f84480ad5e83e4a61904a4f248fb397027357e1d30e463139815c6f' + 'd4fd5ac5b8172a45230ecb6318a04f1455d84e5a8b', 16), + 'public_exponent': int('10001', 16) + } + ) + ) + assert vectors == expected diff --git a/tests/utils.py b/tests/utils.py index 5c0e524f..408b05f6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -14,6 +14,7 @@ import collections import os +import six import pytest @@ -191,3 +192,79 @@ def load_hash_vectors(vector_data): else: raise ValueError("Unknown line in hash vector") return vectors + + +def load_pkcs1_vectors(vector_data): + """ + Loads data out of RSA PKCS #1 vector files. + + Currently only returns the key pairs. + """ + private_key_vector = None + public_key_vector = None + attr = None + key = None + vectors = [] + for line in vector_data: + if ( + line.startswith("# Example") or + line.startswith("# =============================================") + ): + if key: + assert private_key_vector + assert public_key_vector + + for key, value in six.iteritems(public_key_vector): + hex_str = "".join(value).replace(" ", "") + public_key_vector[key] = int(hex_str, 16) + + for key, value in six.iteritems(private_key_vector): + hex_str = "".join(value).replace(" ", "") + private_key_vector[key] = int(hex_str, 16) + + assert ( + private_key_vector['public_exponent'] == + public_key_vector['public_exponent'] + ) + + assert ( + private_key_vector['modulus'] == + public_key_vector['modulus'] + ) + + vectors.append( + (private_key_vector, public_key_vector) + ) + + public_key_vector = collections.defaultdict(list) + private_key_vector = collections.defaultdict(list) + key = None + attr = None + + if private_key_vector is None or public_key_vector is None: + continue + + if line.startswith("# Private key"): + key = private_key_vector + elif line.startswith("# Public key"): + key = public_key_vector + elif line.startswith("# Modulus:"): + attr = "modulus" + elif line.startswith("# Public exponent:"): + attr = "public_exponent" + elif line.startswith("# Exponent:"): + if key is public_key_vector: + attr = "public_exponent" + else: + assert key is private_key_vector + attr = "private_exponent" + elif line.startswith("# Prime 1:"): + attr = "p" + elif line.startswith("# Prime 2:"): + attr = "q" + elif line.startswith("#"): + attr = None + else: + if key is not None and attr is not None: + key[attr].append(line.strip()) + return vectors |