Skip to content

Commit c62ed68

Browse files
author
Adriano Hernandez
committed
Formatted better to pass linters.
1 parent 25e8fd3 commit c62ed68

21 files changed

+323
-1130
lines changed

src/aws_encryption_sdk/__init__.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,9 @@
1515
from aws_encryption_sdk.caches.local import LocalCryptoMaterialsCache # noqa
1616
from aws_encryption_sdk.caches.null import NullCryptoMaterialsCache # noqa
1717
from aws_encryption_sdk.identifiers import AlgorithmSuite, __version__ # noqa
18-
from aws_encryption_sdk.key_providers.kms import (
19-
KMSMasterKeyProvider,
20-
KMSMasterKeyProviderConfig,
21-
) # noqa
22-
from aws_encryption_sdk.materials_managers.caching import (
23-
CachingCryptoMaterialsManager,
24-
) # noqa
25-
from aws_encryption_sdk.materials_managers.default import (
26-
DefaultCryptoMaterialsManager,
27-
) # noqa
18+
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider, KMSMasterKeyProviderConfig # noqa
19+
from aws_encryption_sdk.materials_managers.caching import CachingCryptoMaterialsManager # noqa
20+
from aws_encryption_sdk.materials_managers.default import DefaultCryptoMaterialsManager # noqa
2821
from aws_encryption_sdk.streaming_client import ( # noqa
2922
DecryptorConfig,
3023
EncryptorConfig,
@@ -184,12 +177,7 @@ def stream(**kwargs):
184177
:raises ValueError: if supplied with an unsupported mode value
185178
"""
186179
mode = kwargs.pop("mode")
187-
_stream_map = {
188-
"e": StreamEncryptor,
189-
"encrypt": StreamEncryptor,
190-
"d": StreamDecryptor,
191-
"decrypt": StreamDecryptor,
192-
}
180+
_stream_map = {"e": StreamEncryptor, "encrypt": StreamEncryptor, "d": StreamDecryptor, "decrypt": StreamDecryptor}
193181
try:
194182
return _stream_map[mode.lower()](**kwargs)
195183
except KeyError:

src/aws_encryption_sdk/identifiers.py

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,7 @@ class EncryptionSuite(Enum):
5050
AES_192_GCM_IV12_TAG16 = (algorithms.AES, modes.GCM, 24, 12, 16)
5151
AES_256_GCM_IV12_TAG16 = (algorithms.AES, modes.GCM, 32, 12, 16)
5252

53-
def __init__(
54-
self,
55-
algorithm,
56-
mode,
57-
data_key_length,
58-
iv_length,
59-
auth_length,
60-
auth_key_length=0,
61-
):
53+
def __init__(self, algorithm, mode, data_key_length, iv_length, auth_length, auth_key_length=0):
6254
"""Prepare a new EncryptionSuite."""
6355
self.algorithm = algorithm
6456
self.mode = mode
@@ -165,21 +157,9 @@ class AlgorithmSuite(Enum): # pylint: disable=too-many-instance-attributes
165157
AES_128_GCM_IV12_TAG16 = (0x0014, EncryptionSuite.AES_128_GCM_IV12_TAG16)
166158
AES_192_GCM_IV12_TAG16 = (0x0046, EncryptionSuite.AES_192_GCM_IV12_TAG16)
167159
AES_256_GCM_IV12_TAG16 = (0x0078, EncryptionSuite.AES_256_GCM_IV12_TAG16)
168-
AES_128_GCM_IV12_TAG16_HKDF_SHA256 = (
169-
0x0114,
170-
EncryptionSuite.AES_128_GCM_IV12_TAG16,
171-
KDFSuite.HKDF_SHA256,
172-
)
173-
AES_192_GCM_IV12_TAG16_HKDF_SHA256 = (
174-
0x0146,
175-
EncryptionSuite.AES_192_GCM_IV12_TAG16,
176-
KDFSuite.HKDF_SHA256,
177-
)
178-
AES_256_GCM_IV12_TAG16_HKDF_SHA256 = (
179-
0x0178,
180-
EncryptionSuite.AES_256_GCM_IV12_TAG16,
181-
KDFSuite.HKDF_SHA256,
182-
)
160+
AES_128_GCM_IV12_TAG16_HKDF_SHA256 = (0x0114, EncryptionSuite.AES_128_GCM_IV12_TAG16, KDFSuite.HKDF_SHA256)
161+
AES_192_GCM_IV12_TAG16_HKDF_SHA256 = (0x0146, EncryptionSuite.AES_192_GCM_IV12_TAG16, KDFSuite.HKDF_SHA256)
162+
AES_256_GCM_IV12_TAG16_HKDF_SHA256 = (0x0178, EncryptionSuite.AES_256_GCM_IV12_TAG16, KDFSuite.HKDF_SHA256)
183163
AES_128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256 = (
184164
0x0214,
185165
EncryptionSuite.AES_128_GCM_IV12_TAG16,
@@ -315,38 +295,12 @@ class WrappingAlgorithm(Enum):
315295
None,
316296
)
317297
RSA_PKCS1 = (EncryptionType.ASYMMETRIC, rsa, padding.PKCS1v15, None, None)
318-
RSA_OAEP_SHA1_MGF1 = (
319-
EncryptionType.ASYMMETRIC,
320-
rsa,
321-
padding.OAEP,
322-
hashes.SHA1,
323-
padding.MGF1,
324-
)
325-
RSA_OAEP_SHA256_MGF1 = (
326-
EncryptionType.ASYMMETRIC,
327-
rsa,
328-
padding.OAEP,
329-
hashes.SHA256,
330-
padding.MGF1,
331-
)
332-
RSA_OAEP_SHA384_MGF1 = (
333-
EncryptionType.ASYMMETRIC,
334-
rsa,
335-
padding.OAEP,
336-
hashes.SHA384,
337-
padding.MGF1,
338-
)
339-
RSA_OAEP_SHA512_MGF1 = (
340-
EncryptionType.ASYMMETRIC,
341-
rsa,
342-
padding.OAEP,
343-
hashes.SHA512,
344-
padding.MGF1,
345-
)
298+
RSA_OAEP_SHA1_MGF1 = (EncryptionType.ASYMMETRIC, rsa, padding.OAEP, hashes.SHA1, padding.MGF1)
299+
RSA_OAEP_SHA256_MGF1 = (EncryptionType.ASYMMETRIC, rsa, padding.OAEP, hashes.SHA256, padding.MGF1)
300+
RSA_OAEP_SHA384_MGF1 = (EncryptionType.ASYMMETRIC, rsa, padding.OAEP, hashes.SHA384, padding.MGF1)
301+
RSA_OAEP_SHA512_MGF1 = (EncryptionType.ASYMMETRIC, rsa, padding.OAEP, hashes.SHA512, padding.MGF1)
346302

347-
def __init__(
348-
self, encryption_type, algorithm, padding_type, padding_algorithm, padding_mgf
349-
):
303+
def __init__(self, encryption_type, algorithm, padding_type, padding_algorithm, padding_mgf):
350304
"""Prepares new WrappingAlgorithm."""
351305
self.encryption_type = encryption_type
352306
self.algorithm = algorithm

src/aws_encryption_sdk/internal/crypto/authentication.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ def _build_hasher(self):
5858
5959
:returns: Hasher object
6060
"""
61-
return hashes.Hash(
62-
self.algorithm.signing_hash_type(), backend=default_backend()
63-
)
61+
return hashes.Hash(self.algorithm.signing_hash_type(), backend=default_backend())
6462

6563

6664
class Signer(_PrehashingAuthenticator):
@@ -81,9 +79,7 @@ def from_key_bytes(cls, algorithm, key_bytes):
8179
:param bytes key_bytes: Raw signing key
8280
:rtype: aws_encryption_sdk.internal.crypto.Signer
8381
"""
84-
key = serialization.load_der_private_key(
85-
data=key_bytes, password=None, backend=default_backend()
86-
)
82+
key = serialization.load_der_private_key(data=key_bytes, password=None, backend=default_backend())
8783
return cls(algorithm, key)
8884

8985
def key_bytes(self):
@@ -122,9 +118,7 @@ def finalize(self):
122118
:rtype: bytes
123119
"""
124120
prehashed_digest = self._hasher.finalize()
125-
return _ecc_static_length_signature(
126-
key=self.key, algorithm=self.algorithm, digest=prehashed_digest
127-
)
121+
return _ecc_static_length_signature(key=self.key, algorithm=self.algorithm, digest=prehashed_digest)
128122

129123

130124
class Verifier(_PrehashingAuthenticator):
@@ -152,8 +146,7 @@ def from_encoded_point(cls, algorithm, encoded_point):
152146
return cls(
153147
algorithm=algorithm,
154148
key=_ecc_public_numbers_from_compressed_point(
155-
curve=algorithm.signing_algorithm_info(),
156-
compressed_point=base64.b64decode(encoded_point),
149+
curve=algorithm.signing_algorithm_info(), compressed_point=base64.b64decode(encoded_point)
157150
).public_key(default_backend()),
158151
)
159152

@@ -168,10 +161,7 @@ def from_key_bytes(cls, algorithm, key_bytes):
168161
:rtype: aws_encryption_sdk.internal.crypto.Verifier
169162
"""
170163
return cls(
171-
algorithm=algorithm,
172-
key=serialization.load_der_public_key(
173-
data=key_bytes, backend=default_backend()
174-
),
164+
algorithm=algorithm, key=serialization.load_der_public_key(data=key_bytes, backend=default_backend())
175165
)
176166

177167
def key_bytes(self):
@@ -180,8 +170,7 @@ def key_bytes(self):
180170
:rtype: bytes
181171
"""
182172
return self.key.public_bytes(
183-
encoding=serialization.Encoding.DER,
184-
format=serialization.PublicFormat.SubjectPublicKeyInfo,
173+
encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo
185174
)
186175

187176
def update(self, data):

src/aws_encryption_sdk/internal/crypto/elliptic_curve.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,8 @@
1717
import six
1818
from cryptography.hazmat.backends import default_backend
1919
from cryptography.hazmat.primitives.asymmetric import ec
20-
from cryptography.hazmat.primitives.asymmetric.utils import (
21-
Prehashed,
22-
decode_dss_signature,
23-
encode_dss_signature,
24-
)
25-
from cryptography.utils import (
26-
InterfaceNotImplemented,
27-
int_from_bytes,
28-
int_to_bytes,
29-
verify_interface,
30-
)
20+
from cryptography.hazmat.primitives.asymmetric.utils import Prehashed, decode_dss_signature, encode_dss_signature
21+
from cryptography.utils import InterfaceNotImplemented, int_from_bytes, int_to_bytes, verify_interface
3122

3223
from ...exceptions import NotSupportedError
3324
from ..str_ops import to_bytes
@@ -76,18 +67,14 @@ def _ecc_static_length_signature(key, algorithm, digest):
7667
signature = b""
7768
while len(signature) != algorithm.signature_len:
7869
_LOGGER.debug(
79-
"Signature length %d is not desired length %d. Recalculating.",
80-
len(signature),
81-
algorithm.signature_len,
70+
"Signature length %d is not desired length %d. Recalculating.", len(signature), algorithm.signature_len
8271
)
8372
signature = key.sign(digest, pre_hashed_algorithm)
8473
if len(signature) != algorithm.signature_len:
8574
# Most of the time, a signature of the wrong length can be fixed
8675
# by negating s in the signature relative to the group order.
8776
_LOGGER.debug(
88-
"Signature length %d is not desired length %d. Negating s.",
89-
len(signature),
90-
algorithm.signature_len,
77+
"Signature length %d is not desired length %d. Negating s.", len(signature), algorithm.signature_len
9178
)
9279
r, s = decode_dss_signature(signature)
9380
s = _ECC_CURVE_PARAMETERS[algorithm.signing_algorithm_info.name].order - s
@@ -149,9 +136,7 @@ def _ecc_decode_compressed_point(curve, compressed_point):
149136
try:
150137
params = _ECC_CURVE_PARAMETERS[curve.name]
151138
except KeyError:
152-
raise NotSupportedError(
153-
"Curve {name} is not supported at this time".format(name=curve.name)
154-
)
139+
raise NotSupportedError("Curve {name} is not supported at this time".format(name=curve.name))
155140
alpha = (pow(x, 3, params.p) + (params.a * x % params.p) + params.b) % params.p
156141
# Only works for p % 4 == 3 at this time.
157142
# This is the case for all currently supported algorithms.
@@ -199,8 +184,6 @@ def generate_ecc_signing_key(algorithm):
199184
"""
200185
try:
201186
verify_interface(ec.EllipticCurve, algorithm.signing_algorithm_info)
202-
return ec.generate_private_key(
203-
curve=algorithm.signing_algorithm_info(), backend=default_backend()
204-
)
187+
return ec.generate_private_key(curve=algorithm.signing_algorithm_info(), backend=default_backend())
205188
except InterfaceNotImplemented:
206189
raise NotSupportedError("Unsupported signing algorithm info")

src/aws_encryption_sdk/internal/crypto/encryption.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ def __init__(self, algorithm, key, associated_data, iv):
3939
# This is intentionally generic to leave an option for non-Cipher encryptor types in the future.
4040
self.iv = iv
4141
self._encryptor = Cipher(
42-
algorithm.encryption_algorithm(key),
43-
algorithm.encryption_mode(self.iv),
44-
backend=default_backend(),
42+
algorithm.encryption_algorithm(key), algorithm.encryption_mode(self.iv), backend=default_backend()
4543
).encryptor()
4644

4745
# associated_data will be authenticated but not encrypted,
@@ -110,9 +108,7 @@ def __init__(self, algorithm, key, associated_data, iv, tag):
110108
# Construct a decryptor object with the given key and a provided IV.
111109
# This is intentionally generic to leave an option for non-Cipher decryptor types in the future.
112110
self._decryptor = Cipher(
113-
algorithm.encryption_algorithm(key),
114-
algorithm.encryption_mode(iv, tag),
115-
backend=default_backend(),
111+
algorithm.encryption_algorithm(key), algorithm.encryption_mode(iv, tag), backend=default_backend()
116112
).decryptor()
117113

118114
# Put associated_data back in or the tag will fail to verify when the _decryptor is finalized.
@@ -151,7 +147,5 @@ def decrypt(algorithm, key, encrypted_data, associated_data):
151147
:returns: Plaintext of body
152148
:rtype: bytes
153149
"""
154-
decryptor = Decryptor(
155-
algorithm, key, associated_data, encrypted_data.iv, encrypted_data.tag
156-
)
150+
decryptor = Decryptor(algorithm, key, associated_data, encrypted_data.iv, encrypted_data.tag)
157151
return decryptor.update(encrypted_data.ciphertext) + decryptor.finalize()

src/aws_encryption_sdk/internal/defaults.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
#: Default message structure Type as defined in specification
3030
TYPE = aws_encryption_sdk.identifiers.ObjectType.CUSTOMER_AE_DATA
3131
#: Default algorithm as defined in specification
32-
ALGORITHM = (
33-
aws_encryption_sdk.identifiers.AlgorithmSuite.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
34-
)
32+
ALGORITHM = aws_encryption_sdk.identifiers.AlgorithmSuite.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
3533

3634
#: Key to add encoded signing key to encryption context dictionary as defined in specification
3735
ENCODED_SIGNER_KEY = "aws-crypto-public-key"

0 commit comments

Comments
 (0)