Skip to content

Commit 6dcf5ec

Browse files
m
1 parent a1d864b commit 6dcf5ec

File tree

4 files changed

+146
-89
lines changed

4 files changed

+146
-89
lines changed

TestVectors/runtimes/python/pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ include = ["**/internaldafny/generated/*.py"]
1414
python = "^3.11.0"
1515
aws-cryptographic-material-providers = { path = "../../../mpl/AwsCryptographicMaterialProviders/runtimes/python", develop = false}
1616
aws-cryptography-internal-mpl-testvectors = { path = "../../../mpl/TestVectorsAwsCryptographicMaterialProviders/runtimes/python", develop = false}
17+
18+
# Use the Dafny ESDK Python for type conversions to/from Dafny TestVectors code
1719
aws-encryption-sdk-dafny = { path = "../../../AwsEncryptionSDK/runtimes/python", develop = false}
18-
aws-encryption-sdk = "4.0.0"
1920

21+
# Run TestVectors against the released native ESDK-Python
22+
aws-encryption-sdk = "4.0.0"
2023

2124
[tool.poetry.group.test.dependencies]
2225
pytest = "^7.4.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# ESDK TestVectors have 2 main methods; one in ESDK TestVectors, another in MPL TestVectors.
5+
# This isn't really supported, and results in running the MPL TestVectors' main method.
6+
# Other languages use sed on Dafny-generated code to replace the generated call to the main method.
7+
# It's (arguably) less hacky to override the function that is called in Python.
8+
import aws_encryption_sdk_test_vectors.internaldafny.generated.module_ as module_
9+
import aws_encryption_sdk_test_vectors.internaldafny.generated.WrappedESDKMain as WrappedESDKMain
10+
11+
12+
def new_test_main(args):
13+
WrappedESDKMain.default__.Main2(args)
14+
15+
16+
module_.default__.Test____Main____ = new_test_main

TestVectors/runtimes/python/src/aws_encryption_sdk_test_vectors/internaldafny/extern/use_esdk_testvectors.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

TestVectors/runtimes/python/src/aws_encryption_sdk_test_vectors/internaldafny/extern/wrapped_esdk.py

Lines changed: 126 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
13
import aws_encryption_sdk.streaming_client
24
import aws_encryption_sdk_test_vectors.internaldafny.generated.WrappedESDK as WrappedESDK
35
import smithy_dafny_standard_library.internaldafny.generated.Wrappers as Wrappers
@@ -29,7 +31,9 @@
2931
_smithy_error_to_dafny_error,
3032
)
3133
from aws_encryption_sdk.materials_managers.mpl.cmm import CryptoMaterialsManagerFromMPL
32-
from aws_encryption_sdk.materials_managers.mpl.materials import _mpl_algorithm_id_to_native_algorithm_id
34+
from aws_encryption_sdk.materials_managers.mpl.materials import (
35+
_mpl_algorithm_id_to_native_algorithm_id,
36+
)
3337
from aws_encryption_sdk.identifiers import AlgorithmSuite
3438

3539

@@ -51,96 +55,141 @@ def __init__(self, native_esdk):
5155

5256
def Encrypt(self, dafny_encrypt_input):
5357
try:
54-
native_encrypt_input = dafny_to_smithy_EncryptInput(dafny_encrypt_input)
55-
56-
if native_encrypt_input.algorithm_suite_id is None:
58+
native_encrypt_input = dafny_to_smithy_EncryptInput(dafny_encrypt_input)
59+
60+
# Manual conversion of ESDK-Dafny EncryptInput to unmodelled native ESDK-Python encrypt parameters
61+
native_esdk_input = {
62+
"source": native_encrypt_input.plaintext,
63+
"encryption_context": native_encrypt_input.encryption_context,
64+
}
65+
if native_encrypt_input.keyring is not None:
66+
native_esdk_input["keyring"] = native_encrypt_input.keyring
5767
if native_encrypt_input.materials_manager is not None:
58-
native_esdk_ciphertext, native_esdk_header = self.native_esdk.encrypt(
59-
source=native_encrypt_input.plaintext,
60-
materials_manager=native_encrypt_input.materials_manager,
61-
encryption_context=native_encrypt_input.encryption_context,
62-
)
63-
else:
64-
native_esdk_ciphertext, native_esdk_header = self.native_esdk.encrypt(
65-
source=native_encrypt_input.plaintext,
66-
materials_manager=native_encrypt_input.keyring,
67-
encryption_context=native_encrypt_input.encryption_context,
68-
)
69-
else:
70-
if native_encrypt_input.materials_manager is not None:
71-
native_esdk_ciphertext, native_esdk_header = self.native_esdk.encrypt(
72-
source=native_encrypt_input.plaintext,
73-
materials_manager=native_encrypt_input.materials_manager,
74-
encryption_context=native_encrypt_input.encryption_context,
75-
algorithm = AlgorithmSuite.get_by_id(
76-
_mpl_algorithm_id_to_native_algorithm_id(native_encrypt_input.algorithm_suite_id)
77-
)
78-
)
79-
else:
80-
native_esdk_ciphertext, native_esdk_header = self.native_esdk.encrypt(
81-
source=native_encrypt_input.plaintext,
82-
materials_manager=native_encrypt_input.keyring,
83-
encryption_context=native_encrypt_input.encryption_context,
84-
algorithm = AlgorithmSuite.get_by_id(
85-
_mpl_algorithm_id_to_native_algorithm_id(native_encrypt_input.algorithm_suite_id)
86-
)
68+
native_esdk_input["materials_manager"] = native_encrypt_input.materials_manager
69+
if native_encrypt_input.algorithm_suite_id is not None:
70+
native_esdk_input["algorithm"] = AlgorithmSuite.get_by_id(
71+
_mpl_algorithm_id_to_native_algorithm_id(native_encrypt_input.algorithm_suite_id)
8772
)
8873

89-
dafny_esdk_native_encrypt_output = EncryptOutput(
90-
ciphertext=native_esdk_ciphertext,
91-
encryption_context=native_esdk_header.encryption_context,
92-
algorithm_suite_id=CryptoMaterialsManagerFromMPL._native_algorithm_id_to_mpl_algorithm_id(
93-
native_esdk_header.algorithm.algorithm_id
94-
).value,
95-
)
96-
97-
dafny_esdk_dafny_encrypt_output = smithy_to_dafny_EncryptOutput(
98-
dafny_esdk_native_encrypt_output
99-
)
100-
101-
return Wrappers.Result_Success(dafny_esdk_dafny_encrypt_output)
74+
native_esdk_ciphertext, native_esdk_header = (
75+
self.native_esdk.encrypt(**native_encrypt_input)
76+
)
77+
78+
# if native_encrypt_input.algorithm_suite_id is None:
79+
# if native_encrypt_input.materials_manager is not None:
80+
# native_esdk_ciphertext, native_esdk_header = (
81+
# self.native_esdk.encrypt(
82+
# source=native_encrypt_input.plaintext,
83+
# materials_manager=native_encrypt_input.materials_manager,
84+
# encryption_context=native_encrypt_input.encryption_context,
85+
# )
86+
# )
87+
# else:
88+
# native_esdk_ciphertext, native_esdk_header = (
89+
# self.native_esdk.encrypt(
90+
# source=native_encrypt_input.plaintext,
91+
# materials_manager=native_encrypt_input.keyring,
92+
# encryption_context=native_encrypt_input.encryption_context,
93+
# )
94+
# )
95+
# else:
96+
# if native_encrypt_input.materials_manager is not None:
97+
# native_esdk_ciphertext, native_esdk_header = (
98+
# self.native_esdk.encrypt(
99+
# source=native_encrypt_input.plaintext,
100+
# materials_manager=native_encrypt_input.materials_manager,
101+
# encryption_context=native_encrypt_input.encryption_context,
102+
# algorithm=AlgorithmSuite.get_by_id(
103+
# _mpl_algorithm_id_to_native_algorithm_id(
104+
# native_encrypt_input.algorithm_suite_id
105+
# )
106+
# ),
107+
# )
108+
# )
109+
# else:
110+
# native_esdk_ciphertext, native_esdk_header = (
111+
# self.native_esdk.encrypt(
112+
# source=native_encrypt_input.plaintext,
113+
# materials_manager=native_encrypt_input.keyring,
114+
# encryption_context=native_encrypt_input.encryption_context,
115+
# algorithm=AlgorithmSuite.get_by_id(
116+
# _mpl_algorithm_id_to_native_algorithm_id(
117+
# native_encrypt_input.algorithm_suite_id
118+
# )
119+
# ),
120+
# )
121+
# )
122+
123+
dafny_esdk_native_encrypt_output = EncryptOutput(
124+
ciphertext=native_esdk_ciphertext,
125+
encryption_context=native_esdk_header.encryption_context,
126+
algorithm_suite_id=CryptoMaterialsManagerFromMPL._native_algorithm_id_to_mpl_algorithm_id(
127+
native_esdk_header.algorithm.algorithm_id
128+
).value,
129+
)
130+
131+
dafny_esdk_dafny_encrypt_output = smithy_to_dafny_EncryptOutput(
132+
dafny_esdk_native_encrypt_output
133+
)
134+
135+
return Wrappers.Result_Success(dafny_esdk_dafny_encrypt_output)
102136
except Exception as e:
103-
return Wrappers.Result_Failure(_smithy_error_to_dafny_error(e))
137+
return Wrappers.Result_Failure(_smithy_error_to_dafny_error(e))
104138

105139
def Decrypt(self, dafny_decrypt_input):
106-
140+
107141
try:
108142

109-
native_decrypt_input = dafny_to_smithy_DecryptInput(dafny_decrypt_input)
110-
111-
if native_decrypt_input.materials_manager is not None:
112-
native_esdk_plaintext, native_esdk_header = self.native_esdk.decrypt(
113-
source=native_decrypt_input.ciphertext,
114-
materials_manager=native_decrypt_input.materials_manager,
115-
encryption_context=native_decrypt_input.encryption_context,
116-
)
117-
else:
118-
native_esdk_plaintext, native_esdk_header = self.native_esdk.decrypt(
119-
source=native_decrypt_input.ciphertext,
120-
materials_manager=native_decrypt_input.keyring,
121-
encryption_context=native_decrypt_input.encryption_context,
122-
)
123-
124-
dafny_esdk_native_decrypt_output = DecryptOutput(
125-
plaintext=native_esdk_plaintext,
126-
encryption_context=native_esdk_header.encryption_context,
127-
algorithm_suite_id=CryptoMaterialsManagerFromMPL._native_algorithm_id_to_mpl_algorithm_id(
128-
native_esdk_header.algorithm.algorithm_id
129-
).value,
130-
)
131-
132-
dafny_esdk_dafny_decrypt_output = smithy_to_dafny_DecryptOutput(
133-
dafny_esdk_native_decrypt_output
134-
)
135-
136-
return Wrappers.Result_Success(dafny_esdk_dafny_decrypt_output)
143+
native_decrypt_input = dafny_to_smithy_DecryptInput(dafny_decrypt_input)
144+
145+
# Manual conversion of ESDK-Dafny DecryptInput to unmodelled native ESDK-Python decrypt parameters
146+
native_esdk_input = {
147+
"source": native_decrypt_input.ciphertext,
148+
"encryption_context": native_decrypt_input.encryption_context,
149+
}
150+
if native_decrypt_input.keyring is not None:
151+
native_esdk_input["keyring"] = native_decrypt_input.keyring
152+
if native_decrypt_input.materials_manager is not None:
153+
native_esdk_input["materials_manager"] = native_decrypt_input.materials_manager
154+
155+
native_esdk_plaintext, native_esdk_header = (
156+
self.native_esdk.encrypt(**native_esdk_input)
157+
)
158+
159+
# if native_decrypt_input.materials_manager is not None:
160+
# native_esdk_plaintext, native_esdk_header = self.native_esdk.decrypt(
161+
# source=native_decrypt_input.ciphertext,
162+
# materials_manager=native_decrypt_input.materials_manager,
163+
# encryption_context=native_decrypt_input.encryption_context,
164+
# )
165+
# else:
166+
# native_esdk_plaintext, native_esdk_header = self.native_esdk.decrypt(
167+
# source=native_decrypt_input.ciphertext,
168+
# materials_manager=native_decrypt_input.keyring,
169+
# encryption_context=native_decrypt_input.encryption_context,
170+
# )
171+
172+
dafny_esdk_native_decrypt_output = DecryptOutput(
173+
plaintext=native_esdk_plaintext,
174+
encryption_context=native_esdk_header.encryption_context,
175+
algorithm_suite_id=CryptoMaterialsManagerFromMPL._native_algorithm_id_to_mpl_algorithm_id(
176+
native_esdk_header.algorithm.algorithm_id
177+
).value,
178+
)
179+
180+
dafny_esdk_dafny_decrypt_output = smithy_to_dafny_DecryptOutput(
181+
dafny_esdk_native_decrypt_output
182+
)
183+
184+
return Wrappers.Result_Success(dafny_esdk_dafny_decrypt_output)
137185
except Exception as e:
138186
return Wrappers.Result_Failure(_smithy_error_to_dafny_error(e))
139187

140188

141189
class default__(WrappedESDK.default__):
142190

143-
# Dafny-generated ESDK. Not launched right now.
191+
# This commented-out method wraps the Dafny-generated ESDK.
192+
# Not testing right now.
144193
# @staticmethod
145194
# def WrappedESDK(config):
146195
# smithy_client = aws_encryption_sdk.smithygenerated.aws_cryptography_encryptionsdk.client.AwsEncryptionSdk(
@@ -154,8 +203,6 @@ class default__(WrappedESDK.default__):
154203
def WrappedESDK(dafny_config):
155204
native_config = dafny_config_to_smithy_config(dafny_config)
156205

157-
# TODO deny net 4.0.0 allow retry
158-
159206
if native_config.net_v4_0_0_retry_policy == NetV4_0_0_RetryPolicy.ALLOW_RETRY:
160207
raise ValueError("net 4.0.0 retry policy is not supported")
161208

0 commit comments

Comments
 (0)