Skip to content

Commit de46c4c

Browse files
authored
Merge pull request #34 from mattsb42-aws/jce-fix
fix JceNameLocalDelegatedKey.generate to always operate in bits
2 parents aaef57d + a22eefe commit de46c4c

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

src/dynamodb_encryption_sdk/delegated_keys/jce.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Delegated key that JCE StandardName algorithm values to determine behavior."""
14+
from __future__ import division
15+
1416
import logging
1517
import os
1618

@@ -32,17 +34,17 @@
3234
def _generate_symmetric_key(key_length):
3335
"""Generate a new AES key.
3436
35-
:param int key_length: Required key length in bytes
37+
:param int key_length: Required key length in bits
3638
:returns: raw key, symmetric key identifier, and RAW encoding identifier
3739
:rtype: tuple of bytes, EncryptionKeyType, and KeyEncodingType
3840
"""
39-
return os.urandom(key_length), EncryptionKeyType.SYMMETRIC, KeyEncodingType.RAW
41+
return os.urandom(key_length // 8), EncryptionKeyType.SYMMETRIC, KeyEncodingType.RAW
4042

4143

4244
def _generate_rsa_key(key_length):
4345
"""Generate a new RSA private key.
4446
45-
:param int key_length: Required key length in bytes
47+
:param int key_length: Required key length in bits
4648
:returns: DER-encoded private key, private key identifier, and DER encoding identifier
4749
:rtype: tuple of bytes, EncryptionKeyType, and KeyEncodingType
4850
"""
@@ -180,7 +182,7 @@ def generate(cls, algorithm, key_length=None):
180182
"""Generate an instance of this DelegatedKey using the specified algorithm and key length.
181183
182184
:param str algorithm: Text description of algorithm to be used
183-
:param int key_length: Size of key to generate
185+
:param int key_length: Size in bits of key to generate
184186
:returns: Generated delegated key
185187
:rtype: dynamodb_encryption_sdk.delegated_keys.DelegatedKey
186188
"""

src/dynamodb_encryption_sdk/materials/wrapped.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# ANY KIND, either express or implied. See the License for the specific
1212
# language governing permissions and limitations under the License.
1313
"""Cryptographic materials to use ephemeral content encryption keys wrapped by delegated keys."""
14-
from __future__ import division
15-
1614
import base64
1715
import copy
1816

@@ -140,7 +138,7 @@ def _generate_content_key(self):
140138
args = self._content_key_algorithm.split('/', 1)
141139
content_algorithm = args[0]
142140
try:
143-
content_key_length = int(args[1]) // 8
141+
content_key_length = int(args[1])
144142
except IndexError:
145143
content_key_length = None
146144
content_key = JceNameLocalDelegatedKey.generate(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Dummy stub to make linters work better."""
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
"""Functional test suite for ``dynamodb_encryption_sdk.delegated_keys.jce``."""
14+
from cryptography.hazmat.backends import default_backend
15+
from cryptography.hazmat.primitives import serialization
16+
import pytest
17+
18+
from dynamodb_encryption_sdk.delegated_keys.jce import JceNameLocalDelegatedKey
19+
20+
pytestmark = [pytest.mark.functional, pytest.mark.local]
21+
22+
23+
def _find_aes_key_length(key):
24+
return len(key) * 8
25+
26+
27+
def _find_rsa_key_length(key):
28+
loaded_key = serialization.load_der_private_key(data=key, password=None, backend=default_backend())
29+
return loaded_key._key_size
30+
31+
32+
@pytest.mark.parametrize('algorithm, requested_bits, expected_bits, length_finder', (
33+
('AES', 256, 256, _find_aes_key_length),
34+
('AESWrap', 256, 256, _find_aes_key_length),
35+
('RSA', 4096, 4096, _find_rsa_key_length),
36+
('HmacSHA512', 256, 256, _find_aes_key_length),
37+
('HmacSHA256', 256, 256, _find_aes_key_length),
38+
('HmacSHA384', 256, 256, _find_aes_key_length),
39+
('HmacSHA224', 256, 256, _find_aes_key_length),
40+
('SHA512withRSA', 4096, 4096, _find_rsa_key_length),
41+
('SHA256withRSA', 4096, 4096, _find_rsa_key_length),
42+
('SHA384withRSA', 4096, 4096, _find_rsa_key_length),
43+
('SHA224withRSA', 4096, 4096, _find_rsa_key_length)
44+
))
45+
def test_generate_correct_key_length(algorithm, requested_bits, expected_bits, length_finder):
46+
test = JceNameLocalDelegatedKey.generate(algorithm, requested_bits)
47+
48+
assert length_finder(test.key) == expected_bits

test/functional/functional_test_utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def _some_algorithm_pairs():
215215
def _all_possible_cmps(algorithm_generator):
216216
"""Generate all possible cryptographic materials providers based on the supplied generator."""
217217
# The AES combinations do the same thing, but this makes sure that the AESWrap name works as expected.
218-
yield _build_wrapped_jce_cmp('AESWrap', 32, 'HmacSHA256', 32)
218+
yield _build_wrapped_jce_cmp('AESWrap', 256, 'HmacSHA256', 256)
219219

220220
for builder_info, args in itertools.product(_cmp_builders.items(), algorithm_generator()):
221221
builder_type, builder_func = builder_info
@@ -233,9 +233,6 @@ def _all_possible_cmps(algorithm_generator):
233233
sig_key_length=signing_key_length
234234
)
235235

236-
if encryption_algorithm == 'AES':
237-
encryption_key_length //= 8
238-
239236
yield pytest.param(
240237
builder_func(
241238
encryption_algorithm,

0 commit comments

Comments
 (0)