-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy path__init__.py
187 lines (158 loc) · 8.94 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""High level AWS Encryption SDK client functions."""
# Below are imported for ease of use by implementors
from aws_encryption_sdk.caches.local import LocalCryptoMaterialsCache # noqa
from aws_encryption_sdk.caches.null import NullCryptoMaterialsCache # noqa
from aws_encryption_sdk.identifiers import Algorithm, __version__ # noqa
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider, KMSMasterKeyProviderConfig # noqa
from aws_encryption_sdk.materials_managers.caching import CachingCryptoMaterialsManager # noqa
from aws_encryption_sdk.materials_managers.default import DefaultCryptoMaterialsManager # noqa
from aws_encryption_sdk.streaming_client import ( # noqa
DecryptorConfig,
EncryptorConfig,
StreamDecryptor,
StreamEncryptor,
)
def encrypt(**kwargs):
"""Encrypts and serializes provided plaintext.
.. note::
When using this function, the entire ciphertext message is encrypted into memory before returning
any data. If streaming is desired, see :class:`aws_encryption_sdk.stream`.
.. versionadded:: 1.5.0
The *keyring* parameter.
.. code:: python
>>> import aws_encryption_sdk
>>> kms_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(key_ids=[
... 'arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222',
... 'arn:aws:kms:us-east-1:3333333333333:key/33333333-3333-3333-3333-333333333333'
... ])
>>> my_ciphertext, encryptor_header = aws_encryption_sdk.encrypt(
... source=my_plaintext,
... key_provider=kms_key_provider
... )
:param config: Client configuration object (config or individual parameters required)
:type config: aws_encryption_sdk.streaming_client.EncryptorConfig
:param source: Source data to encrypt or decrypt
:type source: str, bytes, io.IOBase, or file
:param CryptoMaterialsManager materials_manager:
Cryptographic materials manager to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param Keyring keyring: Keyring to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param MasterKeyProvider key_provider:
Master key provider to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param int source_length: Length of source data (optional)
.. note::
If source_length is not provided and unframed message is being written or read() is called,
will attempt to seek() to the end of the stream and tell() to find the length of source data.
.. note::
.. versionadded:: 1.3.0
If `source_length` and `materials_manager` are both provided, the total plaintext bytes
encrypted will not be allowed to exceed `source_length`. To maintain backwards compatibility,
this is not enforced if a `key_provider` is provided.
:param dict encryption_context: Dictionary defining encryption context
:param algorithm: Algorithm to use for encryption
:type algorithm: aws_encryption_sdk.identifiers.Algorithm
:param int frame_length: Frame length in bytes
:returns: Tuple containing the encrypted ciphertext and the message header object
:rtype: tuple of bytes and :class:`aws_encryption_sdk.structures.MessageHeader`
"""
with StreamEncryptor(**kwargs) as encryptor:
ciphertext = encryptor.read()
return ciphertext, encryptor.header
def decrypt(**kwargs):
"""Deserializes and decrypts provided ciphertext.
.. note::
When using this function, the entire ciphertext message is decrypted into memory before returning
any data. If streaming is desired, see :class:`aws_encryption_sdk.stream`.
.. versionadded:: 1.5.0
The *keyring* parameter.
.. code:: python
>>> import aws_encryption_sdk
>>> kms_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(key_ids=[
... 'arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222',
... 'arn:aws:kms:us-east-1:3333333333333:key/33333333-3333-3333-3333-333333333333'
... ])
>>> my_ciphertext, encryptor_header = aws_encryption_sdk.decrypt(
... source=my_ciphertext,
... key_provider=kms_key_provider
... )
:param config: Client configuration object (config or individual parameters required)
:type config: aws_encryption_sdk.streaming_client.DecryptorConfig
:param source: Source data to encrypt or decrypt
:type source: str, bytes, io.IOBase, or file
:param CryptoMaterialsManager materials_manager:
Cryptographic materials manager to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param Keyring keyring: Keyring to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param MasterKeyProvider key_provider:
Master key provider to use for encryption
(either ``materials_manager``, ``keyring``, ``key_provider`` required)
:param int source_length: Length of source data (optional)
.. note::
If source_length is not provided and read() is called, will attempt to seek()
to the end of the stream and tell() to find the length of source data.
:param int max_body_length: Maximum frame size (or content length for non-framed messages)
in bytes to read from ciphertext message.
:returns: Tuple containing the decrypted plaintext and the message header object
:rtype: tuple of bytes and :class:`aws_encryption_sdk.structures.MessageHeader`
"""
with StreamDecryptor(**kwargs) as decryptor:
plaintext = decryptor.read()
return plaintext, decryptor.header
def stream(**kwargs):
"""Provides an :py:func:`open`-like interface to the streaming encryptor/decryptor classes.
.. warning::
Take care when decrypting framed messages with large frame length and large non-framed
messages. In order to protect the authenticity of the encrypted data, no plaintext
is returned until it has been authenticated. Because of this, potentially large amounts
of data may be read into memory. In the case of framed messages, the entire contents
of each frame are read into memory and authenticated before returning any plaintext.
In the case of non-framed messages, the entire message is read into memory and
authenticated before returning any plaintext. The authenticated plaintext is held in
memory until it is requested.
.. note::
Consequently, keep the above decrypting consideration in mind when encrypting messages
to ensure that issues are not encountered when decrypting those messages.
.. code:: python
>>> import aws_encryption_sdk
>>> kms_key_provider = aws_encryption_sdk.KMSMasterKeyProvider(key_ids=[
... 'arn:aws:kms:us-east-1:2222222222222:key/22222222-2222-2222-2222-222222222222',
... 'arn:aws:kms:us-east-1:3333333333333:key/33333333-3333-3333-3333-333333333333'
... ])
>>> plaintext_filename = 'my-secret-data.dat'
>>> ciphertext_filename = 'my-encrypted-data.ct'
>>> with open(plaintext_filename, 'rb') as pt_file, open(ciphertext_filename, 'wb') as ct_file:
... with aws_encryption_sdk.stream(
... mode='e',
... source=pt_file,
... key_provider=kms_key_provider
... ) as encryptor:
... for chunk in encryptor:
... ct_file.write(chunk)
>>> new_plaintext_filename = 'my-decrypted-data.dat'
>>> with open(ciphertext_filename, 'rb') as ct_file, open(new_plaintext_filename, 'wb') as pt_file:
... with aws_encryption_sdk.stream(
... mode='d',
... source=ct_file,
... key_provider=kms_key_provider
... ) as decryptor:
... for chunk in decryptor:
... pt_file.write(chunk)
:param str mode: Type of streaming client to return (e/encrypt: encryptor, d/decrypt: decryptor)
:param **kwargs: All other parameters provided are passed to the appropriate Streaming client
:returns: Streaming Encryptor or Decryptor, as requested
:rtype: :class:`aws_encryption_sdk.streaming_client.StreamEncryptor`
or :class:`aws_encryption_sdk.streaming_client.StreamDecryptor`
:raises ValueError: if supplied with an unsupported mode value
"""
mode = kwargs.pop("mode")
_stream_map = {"e": StreamEncryptor, "encrypt": StreamEncryptor, "d": StreamDecryptor, "decrypt": StreamDecryptor}
try:
return _stream_map[mode.lower()](**kwargs)
except KeyError:
raise ValueError("Unsupported mode: {}".format(mode))
__all__ = ("encrypt", "decrypt", "stream")