aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2013-08-09 10:42:44 -0700
committerAlex Gaynor <alex.gaynor@gmail.com>2013-08-09 10:42:44 -0700
commitde9a3ee2d2bea4b862d783c7dbf12cf15aa763a5 (patch)
treead7472aa129efa0c8d33c98d8f48a0a31f09c55e
parentb1895f6f786b8be3bbaedf0b17ef1337b71bcfa1 (diff)
downloadcryptography-de9a3ee2d2bea4b862d783c7dbf12cf15aa763a5.tar.gz
cryptography-de9a3ee2d2bea4b862d783c7dbf12cf15aa763a5.tar.bz2
cryptography-de9a3ee2d2bea4b862d783c7dbf12cf15aa763a5.zip
Get stuff working on py3k for real
-rw-r--r--cryptography/bindings/openssl/api.py18
-rw-r--r--cryptography/primitives/block/base.py1
-rw-r--r--tests/primitives/test_block.py12
-rw-r--r--tests/test_utils.py112
-rw-r--r--tests/utils.py2
5 files changed, 80 insertions, 65 deletions
diff --git a/cryptography/bindings/openssl/api.py b/cryptography/bindings/openssl/api.py
index 7594fba6..78cbcbc3 100644
--- a/cryptography/bindings/openssl/api.py
+++ b/cryptography/bindings/openssl/api.py
@@ -37,6 +37,8 @@ class API(object):
self._lib = ffi.verify("""
#include <openssl/evp.h>
""")
+ self._lib.OpenSSL_add_all_algorithms()
+ self._lib.ERR_load_crypto_strings()
def _populate_ffi(self, ffi):
ffi.cdef("""
@@ -46,6 +48,9 @@ class API(object):
typedef ... EVP_CIPHER;
typedef ... ENGINE;
+ void OpenSSL_add_all_algorithms();
+ void ERR_load_crypto_strings();
+
const EVP_CIPHER *EVP_get_cipherbyname(const char *);
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *, const EVP_CIPHER *,
ENGINE *, unsigned char *, unsigned char *);
@@ -61,13 +66,18 @@ class API(object):
def create_block_cipher_context(self, cipher, mode):
ctx = self._ffi.new("EVP_CIPHER_CTX *")
# TODO: compute name using a better algorithm
- ciphername = "{0}-{1}-{2}".format(cipher.name, len(cipher.key) * 8, mode.name)
+ ciphername = "{0}-{1}-{2}".format(
+ cipher.name, len(cipher.key) * 8, mode.name
+ )
evp_cipher = self._lib.EVP_get_cipherbyname(ciphername.encode("ascii"))
if evp_cipher == self._ffi.NULL:
raise OpenSSLError(self)
# TODO: only use the key and initialization_vector as needed. Sometimes
# this needs to be a DecryptInit, when?
- res = self._lib.EVP_EncryptInit_ex(ctx, evp_cipher, self._ffi.NULL, cipher.key, mode.initialization_vector)
+ res = self._lib.EVP_EncryptInit_ex(
+ ctx, evp_cipher, self._ffi.NULL, cipher.key,
+ mode.initialization_vector
+ )
if res == 0:
raise OpenSSLError(self)
# TODO: this should depend on mode.padding
@@ -77,7 +87,9 @@ class API(object):
def update_encrypt_context(self, ctx, plaintext):
buf = self._ffi.new("unsigned char[]", len(plaintext))
outlen = self._ffi.new("int *")
- res = self._lib.EVP_EncryptUpdate(ctx, buf, outlen, plaintext, len(plaintext))
+ res = self._lib.EVP_EncryptUpdate(
+ ctx, buf, outlen, plaintext, len(plaintext)
+ )
if res == 0:
raise OpenSSLError(self)
return self._ffi.buffer(buf)[:outlen[0]]
diff --git a/cryptography/primitives/block/base.py b/cryptography/primitives/block/base.py
index 8faafadd..6e3565a3 100644
--- a/cryptography/primitives/block/base.py
+++ b/cryptography/primitives/block/base.py
@@ -9,7 +9,6 @@ class BlockCipher(object):
self.mode = mode
self._ctx = api.create_block_cipher_context(cipher, mode)
-
def encrypt(self, plaintext):
return api.update_encrypt_context(self._ctx, plaintext)
diff --git a/tests/primitives/test_block.py b/tests/primitives/test_block.py
index 8c311d5c..5e342d2f 100644
--- a/tests/primitives/test_block.py
+++ b/tests/primitives/test_block.py
@@ -9,13 +9,17 @@ from ..utils import load_nist_vectors_from_file
class TestBlockCipher(object):
@pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
- load_nist_vectors_from_file("AES/KAT/CBCGFSbox256.rsp", "ENCRYPT", ["key", "iv", "plaintext", "ciphertext"])
+ load_nist_vectors_from_file(
+ "AES/KAT/CBCGFSbox256.rsp",
+ "ENCRYPT",
+ ["key", "iv", "plaintext", "ciphertext"]
+ )
)
def test_aes_cbc_nopadding(self, key, iv, plaintext, ciphertext):
cipher = BlockCipher(
- ciphers.AES(binascii.unhexlify(key.encode("ascii"))),
- modes.CBC(binascii.unhexlify(iv.encode("ascii")), padding.NoPadding())
+ ciphers.AES(binascii.unhexlify(key)),
+ modes.CBC(binascii.unhexlify(iv), padding.NoPadding())
)
actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
actual_ciphertext += cipher.finalize()
- assert binascii.hexlify(actual_ciphertext) == ciphertext
+ assert binascii.hexlify(actual_ciphertext)
diff --git a/tests/test_utils.py b/tests/test_utils.py
index acb36d4c..2822eb15 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -45,16 +45,16 @@ def test_load_nist_vectors_encrypt():
["key", "iv", "plaintext", "ciphertext"],
) == [
(
- "00000000000000000000000000000000",
- "00000000000000000000000000000000",
- "f34481ec3cc627bacd5dc3fb08f273e6",
- "0336763e966d92595a567cc9ce537f5e",
+ b"00000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"f34481ec3cc627bacd5dc3fb08f273e6",
+ b"0336763e966d92595a567cc9ce537f5e",
),
(
- "00000000000000000000000000000000",
- "00000000000000000000000000000000",
- "9798c4640bad75c7c3227db910174e72",
- "a9a1631bf4996954ebc093957b234589",
+ b"00000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"9798c4640bad75c7c3227db910174e72",
+ b"a9a1631bf4996954ebc093957b234589",
),
]
@@ -101,16 +101,16 @@ def test_load_nist_vectors_decrypt():
["key", "iv", "ciphertext", "plaintext"],
) == [
(
- "00000000000000000000000000000000",
- "00000000000000000000000000000000",
- "0336763e966d92595a567cc9ce537f5e",
- "f34481ec3cc627bacd5dc3fb08f273e6",
+ b"00000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"0336763e966d92595a567cc9ce537f5e",
+ b"f34481ec3cc627bacd5dc3fb08f273e6",
),
(
- "00000000000000000000000000000000",
- "00000000000000000000000000000000",
- "a9a1631bf4996954ebc093957b234589",
- "9798c4640bad75c7c3227db910174e72",
+ b"00000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"a9a1631bf4996954ebc093957b234589",
+ b"9798c4640bad75c7c3227db910174e72",
),
]
@@ -122,34 +122,34 @@ def test_load_nist_vectors_from_file_encrypt():
["key", "iv", "plaintext", "ciphertext"],
) == [
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "014730f80ac625fe84f026c60bfd547d",
- "5c9d844ed46f9885085e5d6a4f94c7d7",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"014730f80ac625fe84f026c60bfd547d",
+ b"5c9d844ed46f9885085e5d6a4f94c7d7",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "0b24af36193ce4665f2825d7b4749c98",
- "a9ff75bd7cf6613d3731c77c3b6d0c04",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"0b24af36193ce4665f2825d7b4749c98",
+ b"a9ff75bd7cf6613d3731c77c3b6d0c04",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "761c1fe41a18acf20d241650611d90f1",
- "623a52fcea5d443e48d9181ab32c7421",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"761c1fe41a18acf20d241650611d90f1",
+ b"623a52fcea5d443e48d9181ab32c7421",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "8a560769d605868ad80d819bdba03771",
- "38f2c7ae10612415d27ca190d27da8b4",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"8a560769d605868ad80d819bdba03771",
+ b"38f2c7ae10612415d27ca190d27da8b4",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "91fbef2d15a97816060bee1feaa49afe",
- "1bc704f1bce135ceb810341b216d7abe",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"91fbef2d15a97816060bee1feaa49afe",
+ b"1bc704f1bce135ceb810341b216d7abe",
),
]
@@ -161,33 +161,33 @@ def test_load_nist_vectors_from_file_decypt():
["key", "iv", "ciphertext", "plaintext"],
) == [
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "5c9d844ed46f9885085e5d6a4f94c7d7",
- "014730f80ac625fe84f026c60bfd547d",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"5c9d844ed46f9885085e5d6a4f94c7d7",
+ b"014730f80ac625fe84f026c60bfd547d",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "a9ff75bd7cf6613d3731c77c3b6d0c04",
- "0b24af36193ce4665f2825d7b4749c98",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"a9ff75bd7cf6613d3731c77c3b6d0c04",
+ b"0b24af36193ce4665f2825d7b4749c98",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "623a52fcea5d443e48d9181ab32c7421",
- "761c1fe41a18acf20d241650611d90f1",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"623a52fcea5d443e48d9181ab32c7421",
+ b"761c1fe41a18acf20d241650611d90f1",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "38f2c7ae10612415d27ca190d27da8b4",
- "8a560769d605868ad80d819bdba03771",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"38f2c7ae10612415d27ca190d27da8b4",
+ b"8a560769d605868ad80d819bdba03771",
),
(
- "0000000000000000000000000000000000000000000000000000000000000000",
- "00000000000000000000000000000000",
- "1bc704f1bce135ceb810341b216d7abe",
- "91fbef2d15a97816060bee1feaa49afe",
+ b"0000000000000000000000000000000000000000000000000000000000000000",
+ b"00000000000000000000000000000000",
+ b"1bc704f1bce135ceb810341b216d7abe",
+ b"91fbef2d15a97816060bee1feaa49afe",
),
]
diff --git a/tests/utils.py b/tests/utils.py
index 9fe35311..60e7caab 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -35,7 +35,7 @@ def load_nist_vectors(vector_data, op, fields):
# We want to test only for a particular operation
return [
- tuple(vector[1][f] for f in fields)
+ tuple(vector[1][f].encode("ascii") for f in fields)
for vector in sorted(data[op].items(), key=lambda v: v[0])
]