Skip to content

Commit 25e8fd3

Browse files
author
Adriano Hernandez
committed
Went through and fixed all instances where Algorithm was referenced
instead of AlgorithmSuite. Changed messages in the docs accordingly. Now if you go to RTD you will see everything links together nicely. What I did not do: change the variable names from "algorithm" to i.e. "algorithm_suite". Mainly because this is a rabbit hole and not immediately important I think. To do it you need to change every single thing that calls another thing that calls another thing that has "algorithm" instead of "algorithm_suite" and I wanted to keep it simple. I also found what I think was an incorrect test. In /test/integration/test_i_aws_encryption_sdk_client.py in function test_remove_bad_client() there was an assertion error for my tox env tests that the dict containing the regional clients was not empty when it should have been supposedly. I believe that it was not meant for it to be empty, but for the bad client "us-fakey-12" to be removed, and it was assumed that in __init__ for KMSMasterKeyProvider() no regional clients were added, but this is false because there is a default added depending on some features of the botocore session etc... So when it was checking to see if it was empty it wanted to see that a bad client was added, and then was rightfully removed -> back to an empty dict, but if the dict did not start out empty the test would fail. So I made a one line change of this test to make it test that specifically the dict did not contain the exact bad client that the test was using. Please get back to me on this final change because you guys know this SDK way better than me and I don't want to break anything. Thanks!
1 parent 882dd03 commit 25e8fd3

18 files changed

+791
-285
lines changed

src/aws_encryption_sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def encrypt(**kwargs):
7676
this is not enforced if a `key_provider` is provided.
7777
7878
:param dict encryption_context: Dictionary defining encryption context
79-
:param algorithm: AlgorithmSuite to use for encryption
79+
:param algorithm: Algorithm suite to use for encryption
8080
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
8181
:param int frame_length: Frame length in bytes
8282
:returns: Tuple containing the encrypted ciphertext and the message header object

src/aws_encryption_sdk/internal/crypto/authentication.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
class _PrehashingAuthenticator(object):
3434
"""Parent class for Signer/Verifier. Provides common behavior and interface.
3535
36-
:param algorithm: Algorithm on which to base authenticator
37-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
36+
:param algorithm: Algorithm suite on which to base authenticator
37+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
3838
:param key: Key with which to build authenticator
3939
"""
4040

@@ -46,7 +46,7 @@ def __init__(self, algorithm, key):
4646
self._hasher = self._build_hasher()
4747

4848
def _set_signature_type(self):
49-
"""Ensures that the algorithm signature type is a known type and sets a reference value."""
49+
"""Ensures that the algorithm (suite) signature type is a known type and sets a reference value."""
5050
try:
5151
verify_interface(ec.EllipticCurve, self.algorithm.signing_algorithm_info)
5252
return ec.EllipticCurve
@@ -58,14 +58,16 @@ def _build_hasher(self):
5858
5959
:returns: Hasher object
6060
"""
61-
return hashes.Hash(self.algorithm.signing_hash_type(), backend=default_backend())
61+
return hashes.Hash(
62+
self.algorithm.signing_hash_type(), backend=default_backend()
63+
)
6264

6365

6466
class Signer(_PrehashingAuthenticator):
6567
"""Abstract signing handler.
6668
67-
:param algorithm: Algorithm on which to base signer
68-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
69+
:param algorithm: Algorithm suite on which to base signer
70+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
6971
:param key: Private key from which a signer can be generated
7072
:type key: currently only Elliptic Curve Private Keys are supported
7173
"""
@@ -74,12 +76,14 @@ class Signer(_PrehashingAuthenticator):
7476
def from_key_bytes(cls, algorithm, key_bytes):
7577
"""Builds a `Signer` from an algorithm suite and a raw signing key.
7678
77-
:param algorithm: Algorithm on which to base signer
78-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
79+
:param algorithm: Algorithm suite on which to base signer
80+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
7981
:param bytes key_bytes: Raw signing key
8082
:rtype: aws_encryption_sdk.internal.crypto.Signer
8183
"""
82-
key = serialization.load_der_private_key(data=key_bytes, password=None, backend=default_backend())
84+
key = serialization.load_der_private_key(
85+
data=key_bytes, password=None, backend=default_backend()
86+
)
8387
return cls(algorithm, key)
8488

8589
def key_bytes(self):
@@ -118,7 +122,9 @@ def finalize(self):
118122
:rtype: bytes
119123
"""
120124
prehashed_digest = self._hasher.finalize()
121-
return _ecc_static_length_signature(key=self.key, algorithm=self.algorithm, digest=prehashed_digest)
125+
return _ecc_static_length_signature(
126+
key=self.key, algorithm=self.algorithm, digest=prehashed_digest
127+
)
122128

123129

124130
class Verifier(_PrehashingAuthenticator):
@@ -127,41 +133,45 @@ class Verifier(_PrehashingAuthenticator):
127133
.. note::
128134
For ECC curves, the signature must be DER encoded as specified in RFC 3279.
129135
130-
:param algorithm: Algorithm on which to base verifier
131-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
132-
:param public_key: Appropriate public key object for algorithm
136+
:param algorithm: Algorithm suite on which to base verifier
137+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
138+
:param public_key: Appropriate public key object for algorithm suite
133139
:type public_key: may vary
134140
"""
135141

136142
@classmethod
137143
def from_encoded_point(cls, algorithm, encoded_point):
138-
"""Creates a Verifier object based on the supplied algorithm and encoded compressed ECC curve point.
144+
"""Creates a Verifier object based on the supplied algorithm suite and encoded compressed ECC curve point.
139145
140-
:param algorithm: Algorithm on which to base verifier
141-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
146+
:param algorithm: Algorithm suite on which to base verifier
147+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
142148
:param bytes encoded_point: ECC public point compressed and encoded with _ecc_encode_compressed_point
143149
:returns: Instance of Verifier generated from encoded point
144150
:rtype: aws_encryption_sdk.internal.crypto.Verifier
145151
"""
146152
return cls(
147153
algorithm=algorithm,
148154
key=_ecc_public_numbers_from_compressed_point(
149-
curve=algorithm.signing_algorithm_info(), compressed_point=base64.b64decode(encoded_point)
155+
curve=algorithm.signing_algorithm_info(),
156+
compressed_point=base64.b64decode(encoded_point),
150157
).public_key(default_backend()),
151158
)
152159

153160
@classmethod
154161
def from_key_bytes(cls, algorithm, key_bytes):
155-
"""Creates a `Verifier` object based on the supplied algorithm and raw verification key.
162+
"""Creates a `Verifier` object based on the supplied algorithm suite and raw verification key.
156163
157-
:param algorithm: Algorithm on which to base verifier
158-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
164+
:param algorithm: Algorithm suite on which to base verifier
165+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
159166
:param bytes encoded_point: Raw verification key
160167
:returns: Instance of Verifier generated from encoded point
161168
:rtype: aws_encryption_sdk.internal.crypto.Verifier
162169
"""
163170
return cls(
164-
algorithm=algorithm, key=serialization.load_der_public_key(data=key_bytes, backend=default_backend())
171+
algorithm=algorithm,
172+
key=serialization.load_der_public_key(
173+
data=key_bytes, backend=default_backend()
174+
),
165175
)
166176

167177
def key_bytes(self):
@@ -170,7 +180,8 @@ def key_bytes(self):
170180
:rtype: bytes
171181
"""
172182
return self.key.public_bytes(
173-
encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo
183+
encoding=serialization.Encoding.DER,
184+
format=serialization.PublicFormat.SubjectPublicKeyInfo,
174185
)
175186

176187
def update(self, data):

src/aws_encryption_sdk/internal/crypto/data_keys.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121

2222
def derive_data_encryption_key(source_key, algorithm, message_id):
23-
"""Derives the data encryption key using the defined algorithm.
23+
"""Derives the data encryption key using the defined algorithm suite.
2424
2525
:param bytes source_key: Raw source key
26-
:param algorithm: Algorithm used to encrypt this body
27-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
26+
:param algorithm: Algorithm suite used to encrypt this body
27+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
2828
:param bytes message_id: Message ID
2929
:returns: Derived data encryption key
3030
:rtype: bytes

src/aws_encryption_sdk/internal/crypto/elliptic_curve.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
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 Prehashed, decode_dss_signature, encode_dss_signature
21-
from cryptography.utils import InterfaceNotImplemented, int_from_bytes, int_to_bytes, verify_interface
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+
)
2231

2332
from ...exceptions import NotSupportedError
2433
from ..str_ops import to_bytes
@@ -57,8 +66,8 @@ def _ecc_static_length_signature(key, algorithm, digest):
5766
5867
:param key: Elliptic curve private key
5968
:type key: cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey
60-
:param algorithm: Master algorithm to use
61-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
69+
:param algorithm: Master algorithm suite to use
70+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
6271
:param bytes digest: Pre-calculated hash digest
6372
:returns: Signature with required length
6473
:rtype: bytes
@@ -67,14 +76,18 @@ def _ecc_static_length_signature(key, algorithm, digest):
6776
signature = b""
6877
while len(signature) != algorithm.signature_len:
6978
_LOGGER.debug(
70-
"Signature length %d is not desired length %d. Recalculating.", len(signature), algorithm.signature_len
79+
"Signature length %d is not desired length %d. Recalculating.",
80+
len(signature),
81+
algorithm.signature_len,
7182
)
7283
signature = key.sign(digest, pre_hashed_algorithm)
7384
if len(signature) != algorithm.signature_len:
7485
# Most of the time, a signature of the wrong length can be fixed
7586
# by negating s in the signature relative to the group order.
7687
_LOGGER.debug(
77-
"Signature length %d is not desired length %d. Negating s.", len(signature), algorithm.signature_len
88+
"Signature length %d is not desired length %d. Negating s.",
89+
len(signature),
90+
algorithm.signature_len,
7891
)
7992
r, s = decode_dss_signature(signature)
8093
s = _ECC_CURVE_PARAMETERS[algorithm.signing_algorithm_info.name].order - s
@@ -136,7 +149,9 @@ def _ecc_decode_compressed_point(curve, compressed_point):
136149
try:
137150
params = _ECC_CURVE_PARAMETERS[curve.name]
138151
except KeyError:
139-
raise NotSupportedError("Curve {name} is not supported at this time".format(name=curve.name))
152+
raise NotSupportedError(
153+
"Curve {name} is not supported at this time".format(name=curve.name)
154+
)
140155
alpha = (pow(x, 3, params.p) + (params.a * x % params.p) + params.b) % params.p
141156
# Only works for p % 4 == 3 at this time.
142157
# This is the case for all currently supported algorithms.
@@ -177,13 +192,15 @@ def _ecc_public_numbers_from_compressed_point(curve, compressed_point):
177192
def generate_ecc_signing_key(algorithm):
178193
"""Returns an ECC signing key.
179194
180-
:param algorithm: Algorithm object which determines what signature to generate
181-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
195+
:param algorithm: Algorithm suite object which determines what signature to generate
196+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
182197
:returns: Generated signing key
183-
:raises NotSupportedError: if signing algorithm is not supported on this platform
198+
:raises NotSupportedError: if signing algorithm suite is not supported on this platform
184199
"""
185200
try:
186201
verify_interface(ec.EllipticCurve, algorithm.signing_algorithm_info)
187-
return ec.generate_private_key(curve=algorithm.signing_algorithm_info(), backend=default_backend())
202+
return ec.generate_private_key(
203+
curve=algorithm.signing_algorithm_info(), backend=default_backend()
204+
)
188205
except InterfaceNotImplemented:
189206
raise NotSupportedError("Unsupported signing algorithm info")

src/aws_encryption_sdk/internal/crypto/encryption.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
class Encryptor(object):
2525
"""Abstract encryption handler.
2626
27-
:param algorithm: Algorithm used to encrypt this body
28-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
27+
:param algorithm: Algorithm suite used to encrypt this body
28+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
2929
:param bytes key: Encryption key
3030
:param bytes associated_data: Associated Data to send to encryption subsystem
3131
:param bytes iv: IV to use when encrypting message
@@ -39,7 +39,9 @@ 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), algorithm.encryption_mode(self.iv), backend=default_backend()
42+
algorithm.encryption_algorithm(key),
43+
algorithm.encryption_mode(self.iv),
44+
backend=default_backend(),
4345
).encryptor()
4446

4547
# associated_data will be authenticated but not encrypted,
@@ -76,8 +78,8 @@ def tag(self):
7678
def encrypt(algorithm, key, plaintext, associated_data, iv):
7779
"""Encrypts a frame body.
7880
79-
:param algorithm: Algorithm used to encrypt this body
80-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
81+
:param algorithm: Algorithm suite used to encrypt this body
82+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
8183
:param bytes key: Encryption key
8284
:param bytes plaintext: Body plaintext
8385
:param bytes associated_data: Body AAD Data
@@ -93,8 +95,8 @@ def encrypt(algorithm, key, plaintext, associated_data, iv):
9395
class Decryptor(object):
9496
"""Abstract decryption handler.
9597
96-
:param algorithm: Algorithm used to encrypt this body
97-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
98+
:param algorithm: Algorithm suite used to encrypt this body
99+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
98100
:param bytes key: Raw source key
99101
:param bytes associated_data: Associated Data to send to decryption subsystem
100102
:param bytes iv: IV value with which to initialize decryption subsystem
@@ -108,7 +110,9 @@ def __init__(self, algorithm, key, associated_data, iv, tag):
108110
# Construct a decryptor object with the given key and a provided IV.
109111
# This is intentionally generic to leave an option for non-Cipher decryptor types in the future.
110112
self._decryptor = Cipher(
111-
algorithm.encryption_algorithm(key), algorithm.encryption_mode(iv, tag), backend=default_backend()
113+
algorithm.encryption_algorithm(key),
114+
algorithm.encryption_mode(iv, tag),
115+
backend=default_backend(),
112116
).decryptor()
113117

114118
# Put associated_data back in or the tag will fail to verify when the _decryptor is finalized.
@@ -135,8 +139,8 @@ def finalize(self):
135139
def decrypt(algorithm, key, encrypted_data, associated_data):
136140
"""Decrypts a frame body.
137141
138-
:param algorithm: Algorithm used to encrypt this body
139-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
142+
:param algorithm: Algorithm suite used to encrypt this body
143+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
140144
:param bytes key: Plaintext data key
141145
:param encrypted_data: EncryptedData containing body data
142146
:type encrypted_data: :class:`aws_encryption_sdk.internal.structures.EncryptedData`,
@@ -147,5 +151,7 @@ def decrypt(algorithm, key, encrypted_data, associated_data):
147151
:returns: Plaintext of body
148152
:rtype: bytes
149153
"""
150-
decryptor = Decryptor(algorithm, key, associated_data, encrypted_data.iv, encrypted_data.tag)
154+
decryptor = Decryptor(
155+
algorithm, key, associated_data, encrypted_data.iv, encrypted_data.tag
156+
)
151157
return decryptor.update(encrypted_data.ciphertext) + decryptor.finalize()

src/aws_encryption_sdk/internal/crypto/iv.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
def frame_iv(algorithm, sequence_number):
4747
"""Builds the deterministic IV for a body frame.
4848
49-
:param algorithm: Algorithm for which to build IV
50-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
49+
:param algorithm: Algorithm suite for which to build IV
50+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
5151
:param int sequence_number: Frame sequence number
5252
:returns: Generated IV
5353
:rtype: bytes
@@ -67,8 +67,8 @@ def frame_iv(algorithm, sequence_number):
6767
def non_framed_body_iv(algorithm):
6868
"""Builds the deterministic IV for a non-framed body.
6969
70-
:param algorithm: Algorithm for which to build IV
71-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
70+
:param algorithm: Algorithm suite for which to build IV
71+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
7272
:returns: Generated IV
7373
:rtype: bytes
7474
"""
@@ -78,8 +78,8 @@ def non_framed_body_iv(algorithm):
7878
def header_auth_iv(algorithm):
7979
"""Builds the deterministic IV for header authentication.
8080
81-
:param algorithm: Algorithm for which to build IV
82-
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
81+
:param algorithm: Algorithm suite for which to build IV
82+
:type algorithm: aws_encryption_sdk.identifiers.AlgorithmSuite
8383
:returns: Generated IV
8484
:rtype: bytes
8585
"""

src/aws_encryption_sdk/internal/defaults.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
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 = aws_encryption_sdk.identifiers.Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
32+
ALGORITHM = (
33+
aws_encryption_sdk.identifiers.AlgorithmSuite.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384
34+
)
3335

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

0 commit comments

Comments
 (0)