huaweicloud-encryption-sdk-python 1.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- examples/file_encryption_example.py +57 -0
- examples/key_capsule_encryption_example.py +240 -0
- examples/kms_discovery_encryption_example.py +56 -0
- examples/local_aes_encryption_example.py +59 -0
- examples/test_rsa.py +42 -0
- examples/test_sm2.py +38 -0
- examples/test_sm4_cbc.py +37 -0
- examples/test_sm4_gcm.py +37 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/METADATA +18 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/RECORD +174 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/WHEEL +5 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/top_level.txt +3 -0
- huaweicloud_encryptionsdk/__init__.py +5 -0
- huaweicloud_encryptionsdk/_apig_sdk/__init__.py +1 -0
- huaweicloud_encryptionsdk/_apig_sdk/signer.py +278 -0
- huaweicloud_encryptionsdk/_apig_sdk/signer_v11.py +70 -0
- huaweicloud_encryptionsdk/_apig_sdk/sm3_hash.py +141 -0
- huaweicloud_encryptionsdk/_lts_sdk/auth/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/auth/signer.py +174 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/client/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/client/consumer_client_adapter.py +59 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/client/http_client.py +211 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/client_consumer_worker.py +106 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/consumer_heartbeat.py +73 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/log_shard_consumer.py +179 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/consumer_check_point_tracker.py +120 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/consumer_processor.py +18 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/consumer_processor_factory.py +3 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/check_point.py +20 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/consumer_config.py +27 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/log_data.py +18 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/task_result.py +40 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/consumer_fetch_task.py +53 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/initial_task.py +55 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/process_task.py +25 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/shutdown_task.py +24 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer_main.py +88 -0
- huaweicloud_encryptionsdk/_lts_sdk/parallel_main.py +64 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/callback/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/callback/call_back.py +7 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/accumulator.py +135 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/io_thread_pool.py +107 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/io_worker.py +124 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/mover.py +69 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/producer.py +98 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/lts_http/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/lts_http/http_client.py +63 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/lts_http/simple_request.py +14 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/config.py +50 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/error.py +18 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/log.py +87 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/log_batch.py +154 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/lts_store.py +21 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/result.py +17 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/retry_queue.py +39 -0
- huaweicloud_encryptionsdk/_lts_sdk/serial_main.py +77 -0
- huaweicloud_encryptionsdk/_lts_sdk/utils/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/utils/common.py +57 -0
- huaweicloud_encryptionsdk/_lts_sdk/utils/lts_threading.py +32 -0
- huaweicloud_encryptionsdk/cache/__init__.py +0 -0
- huaweicloud_encryptionsdk/cache/data_key_cache.py +79 -0
- huaweicloud_encryptionsdk/cache/local_data_key_cache.py +161 -0
- huaweicloud_encryptionsdk/common/__init__.py +0 -0
- huaweicloud_encryptionsdk/common/constants.py +15 -0
- huaweicloud_encryptionsdk/common/counting_output_stream.py +18 -0
- huaweicloud_encryptionsdk/common/crypto_factory.py +48 -0
- huaweicloud_encryptionsdk/common/ec_private_key_loader.py +43 -0
- huaweicloud_encryptionsdk/common/ecs_metadata_service.py +42 -0
- huaweicloud_encryptionsdk/common/file_path_for_example_constants.py +4 -0
- huaweicloud_encryptionsdk/common/utils.py +162 -0
- huaweicloud_encryptionsdk/exception/__init__.py +0 -0
- huaweicloud_encryptionsdk/exception/errors.py +109 -0
- huaweicloud_encryptionsdk/handler/__init__.py +0 -0
- huaweicloud_encryptionsdk/handler/cipher_handler.py +294 -0
- huaweicloud_encryptionsdk/handler/crypto_handler.py +24 -0
- huaweicloud_encryptionsdk/handler/default_crypto_handler.py +148 -0
- huaweicloud_encryptionsdk/handler/default_serialize_handler.py +45 -0
- huaweicloud_encryptionsdk/handler/file_decrypt_handler.py +101 -0
- huaweicloud_encryptionsdk/handler/file_encrypt_handler.py +30 -0
- huaweicloud_encryptionsdk/handler/serialize_handler.py +14 -0
- huaweicloud_encryptionsdk/huawei_config.py +49 -0
- huaweicloud_encryptionsdk/huawei_crypto.py +311 -0
- huaweicloud_encryptionsdk/keyrings/__init__.py +0 -0
- huaweicloud_encryptionsdk/keyrings/keyring.py +14 -0
- huaweicloud_encryptionsdk/keyrings/keyring_factory.py +10 -0
- huaweicloud_encryptionsdk/keyrings/kms_keyring_factory.py +21 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/__init__.py +0 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_discovery_keyring.py +51 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_key_capsule_keyring.py +100 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_keyring.py +155 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_multi_region_keyring.py +8 -0
- huaweicloud_encryptionsdk/keyrings/raw_keyring_factory.py +27 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/__init__.py +0 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_aes_keyring.py +63 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_keyring.py +112 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_rsa_keyring.py +55 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_sm2_keyring.py +75 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_sm4_cbc_keyring.py +66 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_sm4_keyring.py +70 -0
- huaweicloud_encryptionsdk/kms/__init__.py +0 -0
- huaweicloud_encryptionsdk/kms/create_key_capsule_request.py +35 -0
- huaweicloud_encryptionsdk/kms/create_key_capsule_response.py +19 -0
- huaweicloud_encryptionsdk/kms/decrypt_key_capsule_request.py +32 -0
- huaweicloud_encryptionsdk/kms/decrypt_key_capsule_response.py +19 -0
- huaweicloud_encryptionsdk/kms/kms_http_client.py +116 -0
- huaweicloud_encryptionsdk/log/__init__.py +0 -0
- huaweicloud_encryptionsdk/log/log_config.py +30 -0
- huaweicloud_encryptionsdk/log/log_file_backup.py +144 -0
- huaweicloud_encryptionsdk/log/log_metrics.py +132 -0
- huaweicloud_encryptionsdk/log/log_reporter_manager.py +163 -0
- huaweicloud_encryptionsdk/log/lts_sdk_producer.py +386 -0
- huaweicloud_encryptionsdk/log/lts_struct_log_builder.py +106 -0
- huaweicloud_encryptionsdk/meterialmanager/__init__.py +0 -0
- huaweicloud_encryptionsdk/meterialmanager/cache_crypto_meterial_manager.py +221 -0
- huaweicloud_encryptionsdk/meterialmanager/crypto_meterial_manager.py +19 -0
- huaweicloud_encryptionsdk/meterialmanager/data_key_generate.py +10 -0
- huaweicloud_encryptionsdk/meterialmanager/data_key_generate_factory.py +20 -0
- huaweicloud_encryptionsdk/meterialmanager/default_crypto_meterials_manager.py +71 -0
- huaweicloud_encryptionsdk/meterialmanager/key_capsule_data_key_generate.py +63 -0
- huaweicloud_encryptionsdk/meterialmanager/kms_data_key_generate.py +65 -0
- huaweicloud_encryptionsdk/meterialmanager/local_data_key_generate.py +13 -0
- huaweicloud_encryptionsdk/model/__init__.py +0 -0
- huaweicloud_encryptionsdk/model/access_point_config.py +9 -0
- huaweicloud_encryptionsdk/model/cipher_body.py +81 -0
- huaweicloud_encryptionsdk/model/cipher_header.py +405 -0
- huaweicloud_encryptionsdk/model/ciphertext_data_key.py +166 -0
- huaweicloud_encryptionsdk/model/crypto_result.py +11 -0
- huaweicloud_encryptionsdk/model/data_key_materials.py +17 -0
- huaweicloud_encryptionsdk/model/data_materials.py +11 -0
- huaweicloud_encryptionsdk/model/enums/__init__.py +0 -0
- huaweicloud_encryptionsdk/model/enums/access_point_type.py +28 -0
- huaweicloud_encryptionsdk/model/enums/crypto_algorithm.py +75 -0
- huaweicloud_encryptionsdk/model/enums/data_key_generate_type.py +20 -0
- huaweicloud_encryptionsdk/model/enums/key_provider_enum.py +19 -0
- huaweicloud_encryptionsdk/model/enums/keyring_type_enum.py +27 -0
- huaweicloud_encryptionsdk/model/key_capsule.py +10 -0
- huaweicloud_encryptionsdk/model/key_capsule_config.py +12 -0
- huaweicloud_encryptionsdk/model/kms_config.py +10 -0
- huaweicloud_encryptionsdk/model/request/__init__.py +0 -0
- huaweicloud_encryptionsdk/model/request/encrypt_request.py +10 -0
- huaweicloud_encryptionsdk/model/request/sign_request.py +2 -0
- huaweicloud_encryptionsdk/model/request/verify_request.py +2 -0
- huaweicloud_encryptionsdk/proof/__init__.py +0 -0
- huaweicloud_encryptionsdk/proof/attestation_document.py +36 -0
- huaweicloud_encryptionsdk/proof/collector/__init__.py +0 -0
- huaweicloud_encryptionsdk/proof/collector/cce_proof_collector.py +24 -0
- huaweicloud_encryptionsdk/proof/collector/ecs_proof_collector.py +11 -0
- huaweicloud_encryptionsdk/proof/collector/key_pair_proof_collector.py +55 -0
- huaweicloud_encryptionsdk/proof/collector/ma_proof_collector.py +51 -0
- huaweicloud_encryptionsdk/proof/private_key_decrypt_callback.py +7 -0
- huaweicloud_encryptionsdk/proof/proof_collector.py +11 -0
- huaweicloud_encryptionsdk/proof/proof_collector_factory.py +53 -0
- huaweicloud_encryptionsdk/proof/proof_info.py +10 -0
- huaweicloud_encryptionsdk/util/__init__.py +0 -0
- huaweicloud_encryptionsdk/util/common_utils.py +12 -0
- tests/__init__.py +0 -0
- tests/test_enums.py +84 -0
- tests/test_exceptions.py +38 -0
- tests/test_factories.py +71 -0
- tests/test_huawei_crypto.py +67 -0
- tests/test_imports.py +46 -0
- tests/test_kms_models.py +95 -0
- tests/test_log_file_backup.py +45 -0
- tests/test_lts_struct_log_builder.py +97 -0
- tests/test_models.py +142 -0
- tests/test_utils.py +111 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import struct
|
|
2
|
+
from io import BytesIO
|
|
3
|
+
|
|
4
|
+
from huaweicloud_encryptionsdk.common.constants import Constants
|
|
5
|
+
from huaweicloud_encryptionsdk.common.counting_output_stream import CountingOutputStream
|
|
6
|
+
from huaweicloud_encryptionsdk.handler.cipher_handler import CipherHandler, ENCRYPT_MODE, DECRYPT_MODE
|
|
7
|
+
from huaweicloud_encryptionsdk.handler.crypto_handler import CryptoHandler
|
|
8
|
+
from huaweicloud_encryptionsdk.handler.default_serialize_handler import DefaultSerializeHandler
|
|
9
|
+
from huaweicloud_encryptionsdk.handler.file_encrypt_handler import FileEncryptHandler
|
|
10
|
+
from huaweicloud_encryptionsdk.handler.file_decrypt_handler import FileDecryptHandler
|
|
11
|
+
from huaweicloud_encryptionsdk.model.cipher_body import CipherBody
|
|
12
|
+
from huaweicloud_encryptionsdk.model.cipher_header import CipherHeader
|
|
13
|
+
from huaweicloud_encryptionsdk.model.crypto_result import CryptoResult
|
|
14
|
+
from huaweicloud_encryptionsdk.model.data_key_materials import DataKeyMaterials
|
|
15
|
+
from huaweicloud_encryptionsdk.model.data_materials import DataMaterials
|
|
16
|
+
from huaweicloud_encryptionsdk.model.enums.crypto_algorithm import CryptoAlgorithm
|
|
17
|
+
from huaweicloud_encryptionsdk.model.request.encrypt_request import EncryptRequest
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class DefaultCryptoHandler(CryptoHandler):
|
|
21
|
+
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self._serialize_handler = DefaultSerializeHandler()
|
|
24
|
+
|
|
25
|
+
def encrypt(self, request, data_key_materials):
|
|
26
|
+
cipher_handler = CipherHandler(
|
|
27
|
+
data_key_materials.crypto_algorithm,
|
|
28
|
+
data_key_materials.plaintext_data_key,
|
|
29
|
+
ENCRYPT_MODE,
|
|
30
|
+
)
|
|
31
|
+
encrypted_data = cipher_handler.cipher_data(
|
|
32
|
+
request.plain_text, request.encryption_context, 0, len(request.plain_text)
|
|
33
|
+
)
|
|
34
|
+
if data_key_materials.crypto_algorithm == CryptoAlgorithm.SM4_128_CBC_PADDING:
|
|
35
|
+
tag_len = 0
|
|
36
|
+
else:
|
|
37
|
+
tag_len = data_key_materials.crypto_algorithm.tag_len
|
|
38
|
+
|
|
39
|
+
if tag_len != 0:
|
|
40
|
+
cipher_text = encrypted_data[: len(encrypted_data) - tag_len]
|
|
41
|
+
auth_tag = encrypted_data[len(encrypted_data) - tag_len:]
|
|
42
|
+
else:
|
|
43
|
+
cipher_text = encrypted_data
|
|
44
|
+
auth_tag = b""
|
|
45
|
+
|
|
46
|
+
cipher_header = CipherHeader(
|
|
47
|
+
data_key_materials.crypto_algorithm,
|
|
48
|
+
data_key_materials.encryption_contexts,
|
|
49
|
+
data_key_materials.ciphertext_data_keys,
|
|
50
|
+
data_key_materials.plaintext_data_key,
|
|
51
|
+
data_key_materials.key_capsule,
|
|
52
|
+
)
|
|
53
|
+
cipher_body = CipherBody(
|
|
54
|
+
cipher_handler.get_iv(), cipher_text, len(cipher_text), auth_tag
|
|
55
|
+
)
|
|
56
|
+
data_materials = DataMaterials(cipher_body, cipher_header)
|
|
57
|
+
serialized_data = self._serialize_handler.serialize(data_materials)
|
|
58
|
+
return CryptoResult(
|
|
59
|
+
result=serialized_data,
|
|
60
|
+
headers=cipher_header,
|
|
61
|
+
data_length=len(cipher_text),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
def decrypt(self, cipher_text, data_key_materials):
|
|
65
|
+
data_materials = self._serialize_handler.deserialize(cipher_text)
|
|
66
|
+
headers = data_materials.headers
|
|
67
|
+
header_tag = headers.header_tag
|
|
68
|
+
header_tag_iv = header_tag[: headers.algorithm.iv_len]
|
|
69
|
+
|
|
70
|
+
cipher_handler = CipherHandler(
|
|
71
|
+
data_key_materials.crypto_algorithm,
|
|
72
|
+
data_key_materials.plaintext_data_key,
|
|
73
|
+
DECRYPT_MODE,
|
|
74
|
+
)
|
|
75
|
+
cipher_handler.set_iv(header_tag_iv)
|
|
76
|
+
headers.verify_header(cipher_handler)
|
|
77
|
+
|
|
78
|
+
cipher_body = data_materials.cipher_body
|
|
79
|
+
if cipher_body is None:
|
|
80
|
+
raise Exception("cipher text changed error")
|
|
81
|
+
|
|
82
|
+
iv = cipher_body.iv
|
|
83
|
+
encrypted_content = cipher_body.encrypted_content
|
|
84
|
+
auth_tag = cipher_body.auth_tag
|
|
85
|
+
|
|
86
|
+
if (
|
|
87
|
+
data_key_materials.crypto_algorithm != CryptoAlgorithm.SM4_128_CBC_PADDING
|
|
88
|
+
and len(auth_tag) != data_key_materials.crypto_algorithm.tag_len
|
|
89
|
+
):
|
|
90
|
+
raise ValueError("Invalid tag length: " + str(len(auth_tag)))
|
|
91
|
+
|
|
92
|
+
result = bytearray(len(iv) + len(encrypted_content) + len(auth_tag))
|
|
93
|
+
result[: len(iv)] = iv
|
|
94
|
+
result[len(iv): len(iv) + len(encrypted_content)] = encrypted_content
|
|
95
|
+
result[len(iv) + len(encrypted_content):] = auth_tag
|
|
96
|
+
|
|
97
|
+
cipher_handler.set_iv(iv)
|
|
98
|
+
decrypt_data = cipher_handler.cipher_data(
|
|
99
|
+
bytes(result), data_key_materials.encryption_contexts, len(iv), len(result) - len(iv)
|
|
100
|
+
)
|
|
101
|
+
return CryptoResult(result=decrypt_data, headers=data_materials.headers)
|
|
102
|
+
|
|
103
|
+
def encrypt_stream(self, input_stream, output_stream, data_key_materials, encryption_context):
|
|
104
|
+
counting_stream = CountingOutputStream(output_stream)
|
|
105
|
+
cipher_header = CipherHeader(
|
|
106
|
+
data_key_materials.crypto_algorithm,
|
|
107
|
+
data_key_materials.encryption_contexts,
|
|
108
|
+
data_key_materials.ciphertext_data_keys,
|
|
109
|
+
data_key_materials.plaintext_data_key,
|
|
110
|
+
data_key_materials.key_capsule,
|
|
111
|
+
)
|
|
112
|
+
authenticated_fields = cipher_header.serialize_authenticated_fields()
|
|
113
|
+
counting_stream.write(
|
|
114
|
+
struct.pack(">h", len(authenticated_fields) + 2 + cipher_header.header_tag_length)
|
|
115
|
+
)
|
|
116
|
+
counting_stream.write(authenticated_fields)
|
|
117
|
+
counting_stream.write(struct.pack(">h", cipher_header.header_tag_length))
|
|
118
|
+
counting_stream.write(cipher_header.header_tag)
|
|
119
|
+
|
|
120
|
+
file_encrypt_handler = FileEncryptHandler(
|
|
121
|
+
data_key_materials.crypto_algorithm,
|
|
122
|
+
data_key_materials.plaintext_data_key,
|
|
123
|
+
ENCRYPT_MODE,
|
|
124
|
+
)
|
|
125
|
+
file_encrypt_handler.process_byte(
|
|
126
|
+
input_stream, counting_stream, data_key_materials.encryption_contexts, Constants.BLOCK_SIZE
|
|
127
|
+
)
|
|
128
|
+
return CryptoResult(
|
|
129
|
+
result=counting_stream,
|
|
130
|
+
headers=cipher_header,
|
|
131
|
+
data_length=counting_stream.get_bytes_written(),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
def decrypt_stream(self, input_stream, output_stream, data_key_materials):
|
|
135
|
+
counting_stream = CountingOutputStream(output_stream)
|
|
136
|
+
file_decrypt_handler = FileDecryptHandler(
|
|
137
|
+
data_key_materials.crypto_algorithm,
|
|
138
|
+
data_key_materials.plaintext_data_key,
|
|
139
|
+
DECRYPT_MODE,
|
|
140
|
+
)
|
|
141
|
+
file_decrypt_handler.process_byte(
|
|
142
|
+
input_stream, counting_stream, data_key_materials.encryption_contexts, Constants.BLOCK_SIZE
|
|
143
|
+
)
|
|
144
|
+
return CryptoResult(
|
|
145
|
+
result=counting_stream,
|
|
146
|
+
headers=None,
|
|
147
|
+
data_length=counting_stream.get_bytes_written(),
|
|
148
|
+
)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import struct
|
|
2
|
+
from io import BytesIO
|
|
3
|
+
|
|
4
|
+
from huaweicloud_encryptionsdk.exception.errors import HuaweicloudException, ErrorMessage
|
|
5
|
+
from huaweicloud_encryptionsdk.handler.serialize_handler import SerializeHandler
|
|
6
|
+
from huaweicloud_encryptionsdk.model.cipher_body import CipherBody
|
|
7
|
+
from huaweicloud_encryptionsdk.model.cipher_header import CipherHeader
|
|
8
|
+
from huaweicloud_encryptionsdk.model.data_materials import DataMaterials
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DefaultSerializeHandler(SerializeHandler):
|
|
12
|
+
|
|
13
|
+
def serialize(self, data_materials):
|
|
14
|
+
cipher_body = data_materials.cipher_body
|
|
15
|
+
headers = data_materials.headers.serialize_authenticated_fields()
|
|
16
|
+
header_tag = data_materials.headers.header_tag
|
|
17
|
+
header_tag_length = data_materials.headers.header_tag_length
|
|
18
|
+
body_bytes = cipher_body.to_byte_array()
|
|
19
|
+
total_length = len(body_bytes) + len(headers) + 2 + len(header_tag)
|
|
20
|
+
buf = bytearray(total_length)
|
|
21
|
+
offset = 0
|
|
22
|
+
buf[offset:offset + len(headers)] = headers
|
|
23
|
+
offset += len(headers)
|
|
24
|
+
struct.pack_into(">h", buf, offset, header_tag_length)
|
|
25
|
+
offset += 2
|
|
26
|
+
buf[offset:offset + len(header_tag)] = header_tag
|
|
27
|
+
offset += len(header_tag)
|
|
28
|
+
buf[offset:offset + len(body_bytes)] = body_bytes
|
|
29
|
+
return bytes(buf)
|
|
30
|
+
|
|
31
|
+
def deserialize(self, cipher_data):
|
|
32
|
+
try:
|
|
33
|
+
data_stream = BytesIO(cipher_data)
|
|
34
|
+
cipher_header = CipherHeader()
|
|
35
|
+
cipher_header.deserialize_authenticated_fields(data_stream)
|
|
36
|
+
cipher_body = None
|
|
37
|
+
remaining = len(cipher_data) - data_stream.tell()
|
|
38
|
+
if remaining > 0:
|
|
39
|
+
cipher_body = CipherBody()
|
|
40
|
+
cipher_body.deserialize(data_stream, cipher_header.algorithm)
|
|
41
|
+
return DataMaterials(cipher_body, cipher_header)
|
|
42
|
+
except HuaweicloudException:
|
|
43
|
+
raise
|
|
44
|
+
except Exception as e:
|
|
45
|
+
raise HuaweicloudException(ErrorMessage.DESERIALIZE_EXCEPTION.message, e)
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from huaweicloud_encryptionsdk.handler.cipher_handler import CipherHandler, DECRYPT_MODE
|
|
2
|
+
from huaweicloud_encryptionsdk.model.cipher_header import CipherHeader
|
|
3
|
+
from huaweicloud_encryptionsdk.exception.errors import HuaweicloudException, ErrorMessage
|
|
4
|
+
from huaweicloud_encryptionsdk.common.utils import serialize_context
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class FileDecryptHandler:
|
|
8
|
+
|
|
9
|
+
def __init__(self, algorithm, secret_key, mode):
|
|
10
|
+
self._mode = mode
|
|
11
|
+
self._algorithm = algorithm
|
|
12
|
+
self._secret_key = secret_key
|
|
13
|
+
self._un_parsed_byte = b""
|
|
14
|
+
self._header = None
|
|
15
|
+
|
|
16
|
+
def process_byte(self, input_stream, output_stream, encryption_context, block_size):
|
|
17
|
+
try:
|
|
18
|
+
cipher_handler = CipherHandler(self._algorithm, self._secret_key, self._mode)
|
|
19
|
+
aad = serialize_context(encryption_context)
|
|
20
|
+
file_header_length = CipherHeader().get_file_header_length(self._algorithm)
|
|
21
|
+
|
|
22
|
+
while True:
|
|
23
|
+
read_data = input_stream.read(block_size)
|
|
24
|
+
if not read_data:
|
|
25
|
+
break
|
|
26
|
+
|
|
27
|
+
# 拼接未解析数据和新读取数据
|
|
28
|
+
if self._un_parsed_byte:
|
|
29
|
+
buf = self._un_parsed_byte + read_data
|
|
30
|
+
self._un_parsed_byte = b""
|
|
31
|
+
else:
|
|
32
|
+
buf = read_data
|
|
33
|
+
|
|
34
|
+
offset = 0
|
|
35
|
+
total = len(buf)
|
|
36
|
+
|
|
37
|
+
# 如果有残留的 header,先处理
|
|
38
|
+
if self._header is not None:
|
|
39
|
+
remaining = total - offset
|
|
40
|
+
if self._header.content_length > remaining:
|
|
41
|
+
self._un_parsed_byte = buf[offset:]
|
|
42
|
+
continue
|
|
43
|
+
cipher_text = buf[offset:offset + self._header.content_length]
|
|
44
|
+
offset += self._header.content_length
|
|
45
|
+
cipher_handler.set_iv(self._header.iv)
|
|
46
|
+
cipher_handler.process_decrypt_byte(cipher_text, len(cipher_text), output_stream, aad)
|
|
47
|
+
self._header = None
|
|
48
|
+
|
|
49
|
+
# 循环处理所有完整块
|
|
50
|
+
while offset + file_header_length <= total:
|
|
51
|
+
temp_header = CipherHeader()
|
|
52
|
+
header_bytes = buf[offset:offset + file_header_length]
|
|
53
|
+
temp_header.deserialize_file_fields(header_bytes, self._algorithm)
|
|
54
|
+
content_length = temp_header.content_length
|
|
55
|
+
offset += file_header_length
|
|
56
|
+
|
|
57
|
+
if content_length == 0:
|
|
58
|
+
raise HuaweicloudException(ErrorMessage.FILE_CHANGED_ERROR.message)
|
|
59
|
+
|
|
60
|
+
if content_length > total - offset:
|
|
61
|
+
# 数据不完整,保存 header 和剩余数据
|
|
62
|
+
self._header = temp_header
|
|
63
|
+
self._un_parsed_byte = buf[offset:]
|
|
64
|
+
offset = total
|
|
65
|
+
break
|
|
66
|
+
|
|
67
|
+
cipher_text = buf[offset:offset + content_length]
|
|
68
|
+
offset += content_length
|
|
69
|
+
cipher_handler.set_iv(temp_header.iv)
|
|
70
|
+
cipher_handler.process_decrypt_byte(cipher_text, len(cipher_text), output_stream, aad)
|
|
71
|
+
|
|
72
|
+
# 保存未处理的剩余数据
|
|
73
|
+
if offset < total and not self._un_parsed_byte:
|
|
74
|
+
self._un_parsed_byte = buf[offset:]
|
|
75
|
+
if not self._header:
|
|
76
|
+
# 剩余数据不足一个 header
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
# 处理文件末尾剩余数据
|
|
80
|
+
while self._un_parsed_byte and len(self._un_parsed_byte) > 0:
|
|
81
|
+
if self._header is None:
|
|
82
|
+
if len(self._un_parsed_byte) < file_header_length:
|
|
83
|
+
break
|
|
84
|
+
temp_header = CipherHeader()
|
|
85
|
+
header_bytes = self._un_parsed_byte[:file_header_length]
|
|
86
|
+
temp_header.deserialize_file_fields(header_bytes, self._algorithm)
|
|
87
|
+
self._header = temp_header
|
|
88
|
+
self._un_parsed_byte = self._un_parsed_byte[file_header_length:]
|
|
89
|
+
if self._header.content_length == 0:
|
|
90
|
+
raise HuaweicloudException(ErrorMessage.FILE_CHANGED_ERROR.message)
|
|
91
|
+
if self._header.content_length > len(self._un_parsed_byte):
|
|
92
|
+
break
|
|
93
|
+
cipher_text = self._un_parsed_byte[:self._header.content_length]
|
|
94
|
+
cipher_handler.set_iv(self._header.iv)
|
|
95
|
+
cipher_handler.process_decrypt_byte(cipher_text, len(cipher_text), output_stream, aad)
|
|
96
|
+
self._un_parsed_byte = self._un_parsed_byte[self._header.content_length:]
|
|
97
|
+
self._header = None
|
|
98
|
+
except HuaweicloudException:
|
|
99
|
+
raise
|
|
100
|
+
except Exception as e:
|
|
101
|
+
raise HuaweicloudException(ErrorMessage.FILE_CHANGED_ERROR.message) from e
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from huaweicloud_encryptionsdk.handler.cipher_handler import CipherHandler
|
|
2
|
+
from huaweicloud_encryptionsdk.common.utils import serialize_context
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class FileEncryptHandler:
|
|
6
|
+
|
|
7
|
+
def __init__(self, algorithm, secret_key, mode):
|
|
8
|
+
self._mode = mode
|
|
9
|
+
self._algorithm = algorithm
|
|
10
|
+
self._secret_key = secret_key
|
|
11
|
+
|
|
12
|
+
def process_byte(self, input_stream, output_stream, encryption_context, block_size):
|
|
13
|
+
read_bytes = bytearray(block_size)
|
|
14
|
+
cipher_handler = CipherHandler(self._algorithm, self._secret_key, self._mode)
|
|
15
|
+
aad = serialize_context(encryption_context)
|
|
16
|
+
while True:
|
|
17
|
+
read_len = input_stream.readinto(read_bytes) if hasattr(input_stream, 'readinto') else self._read_stream(input_stream, read_bytes)
|
|
18
|
+
if read_len is None or read_len == -1 or read_len == 0:
|
|
19
|
+
break
|
|
20
|
+
cipher_handler.reset_iv()
|
|
21
|
+
cipher_handler.process_encrypt_byte(read_bytes[:read_len], read_len, output_stream, aad)
|
|
22
|
+
|
|
23
|
+
@staticmethod
|
|
24
|
+
def _read_stream(input_stream, buf):
|
|
25
|
+
data = input_stream.read(len(buf))
|
|
26
|
+
if data is None or len(data) == 0:
|
|
27
|
+
return -1
|
|
28
|
+
n = len(data)
|
|
29
|
+
buf[:n] = data
|
|
30
|
+
return n
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
from huaweicloud_encryptionsdk.model.data_materials import DataMaterials
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SerializeHandler(ABC):
|
|
7
|
+
|
|
8
|
+
@abstractmethod
|
|
9
|
+
def serialize(self, data_materials):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
@abstractmethod
|
|
13
|
+
def deserialize(self, cipher_data):
|
|
14
|
+
pass
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
from huaweicloud_encryptionsdk.exception.errors import HuaweicloudException, ErrorMessage
|
|
7
|
+
from huaweicloud_encryptionsdk.model.kms_config import KMSConfig
|
|
8
|
+
from huaweicloud_encryptionsdk.model.enums.crypto_algorithm import CryptoAlgorithm
|
|
9
|
+
from huaweicloud_encryptionsdk.model.key_capsule_config import KeyCapsuleConfig
|
|
10
|
+
from huaweicloud_encryptionsdk.log.log_config import LogConfig
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class HuaweiConfig:
|
|
15
|
+
ak: str = ""
|
|
16
|
+
sk: str = ""
|
|
17
|
+
security_token: Optional[str] = None
|
|
18
|
+
is_discovery: bool = False
|
|
19
|
+
kms_config_list: Optional[list[KMSConfig]] = None
|
|
20
|
+
origin_kms_config_list: Optional[list[KMSConfig]] = None
|
|
21
|
+
crypto_algorithm: Optional[CryptoAlgorithm] = None
|
|
22
|
+
key_capsule_config: Optional[KeyCapsuleConfig] = None
|
|
23
|
+
log_config: Optional[LogConfig] = None
|
|
24
|
+
local_log_only: bool = False
|
|
25
|
+
|
|
26
|
+
def __post_init__(self):
|
|
27
|
+
if self.ak:
|
|
28
|
+
self._validate_non_empty(self.ak, ErrorMessage.AK_NULL_EXCEPTION)
|
|
29
|
+
if self.sk:
|
|
30
|
+
self._validate_non_empty(self.sk, ErrorMessage.SK_NULL_EXCEPTION)
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def _validate_non_empty(value: str, error: ErrorMessage) -> None:
|
|
34
|
+
if not value or not value.strip():
|
|
35
|
+
raise HuaweicloudException(error.message)
|
|
36
|
+
|
|
37
|
+
def set_ak(self, value: str) -> None:
|
|
38
|
+
self._validate_non_empty(value, ErrorMessage.AK_NULL_EXCEPTION)
|
|
39
|
+
self.ak = value
|
|
40
|
+
|
|
41
|
+
def set_sk(self, value: str) -> None:
|
|
42
|
+
self._validate_non_empty(value, ErrorMessage.SK_NULL_EXCEPTION)
|
|
43
|
+
self.sk = value
|
|
44
|
+
|
|
45
|
+
def get_ak(self) -> str:
|
|
46
|
+
return self.ak
|
|
47
|
+
|
|
48
|
+
def get_sk(self) -> str:
|
|
49
|
+
return self.sk
|