shellzapy 0.1.0__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.
- shellzapy-0.1.0.dist-info/METADATA +112 -0
- shellzapy-0.1.0.dist-info/RECORD +345 -0
- shellzapy-0.1.0.dist-info/WHEEL +4 -0
- shellzapy-0.1.0.dist-info/licenses/LICENSE +21 -0
- shellzapy-0.1.0.dist-info/licenses/NOTICE +9 -0
- zapy/__init__.py +58 -0
- zapy/_appstate/__init__.py +59 -0
- zapy/_appstate/constants.py +82 -0
- zapy/_appstate/crypto/__init__.py +17 -0
- zapy/_appstate/crypto/app_state_crypto.py +401 -0
- zapy/_appstate/parsers/__init__.py +23 -0
- zapy/_appstate/parsers/encoding.py +157 -0
- zapy/_appstate/parsers/response_parser.py +135 -0
- zapy/_appstate/sync/__init__.py +11 -0
- zapy/_appstate/sync/sync_client.py +1559 -0
- zapy/_appstate/types.py +137 -0
- zapy/_appstate/utils.py +100 -0
- zapy/_auth/__init__.py +3 -0
- zapy/_auth/auth_client.py +618 -0
- zapy/_auth/credentials_flow.py +290 -0
- zapy/_auth/pairing/__init__.py +3 -0
- zapy/_auth/pairing/companion_host.py +239 -0
- zapy/_auth/pairing/flow.py +808 -0
- zapy/_auth/pairing/pairing_code_crypto.py +143 -0
- zapy/_auth/pairing/qr_flow.py +123 -0
- zapy/_auth/pairing/shortcake_crypto.py +122 -0
- zapy/_auth/pairing/shortcake_flow.py +393 -0
- zapy/_auth/types.py +110 -0
- zapy/_client/__init__.py +26 -0
- zapy/_client/client.py +972 -0
- zapy/_client/connection/__init__.py +6 -0
- zapy/_client/connection/manager.py +525 -0
- zapy/_client/connection/receipt_queue.py +74 -0
- zapy/_client/coordinators/__init__.py +81 -0
- zapy/_client/coordinators/ab_props.py +260 -0
- zapy/_client/coordinators/app_state_mutation.py +2538 -0
- zapy/_client/coordinators/bot.py +830 -0
- zapy/_client/coordinators/broadcast_list.py +101 -0
- zapy/_client/coordinators/business.py +247 -0
- zapy/_client/coordinators/email.py +158 -0
- zapy/_client/coordinators/group.py +1478 -0
- zapy/_client/coordinators/incoming_node.py +758 -0
- zapy/_client/coordinators/low_level.py +119 -0
- zapy/_client/coordinators/message.py +1153 -0
- zapy/_client/coordinators/message_dispatch.py +1637 -0
- zapy/_client/coordinators/newsletter.py +110 -0
- zapy/_client/coordinators/offline_resume.py +271 -0
- zapy/_client/coordinators/passive_tasks.py +447 -0
- zapy/_client/coordinators/presence.py +103 -0
- zapy/_client/coordinators/privacy.py +623 -0
- zapy/_client/coordinators/profile.py +1024 -0
- zapy/_client/coordinators/retry.py +1878 -0
- zapy/_client/coordinators/status.py +132 -0
- zapy/_client/coordinators/stream_control.py +260 -0
- zapy/_client/coordinators/trusted_contact_token.py +417 -0
- zapy/_client/events/__init__.py +266 -0
- zapy/_client/events/abprops.py +58 -0
- zapy/_client/events/appstate_mutation.py +248 -0
- zapy/_client/events/business.py +565 -0
- zapy/_client/events/call.py +144 -0
- zapy/_client/events/chatstate.py +41 -0
- zapy/_client/events/devices.py +122 -0
- zapy/_client/events/dirty.py +384 -0
- zapy/_client/events/group.py +655 -0
- zapy/_client/events/identity.py +40 -0
- zapy/_client/events/incoming.py +1079 -0
- zapy/_client/events/mex_notification.py +317 -0
- zapy/_client/events/offline.py +123 -0
- zapy/_client/events/picture.py +67 -0
- zapy/_client/events/presence.py +74 -0
- zapy/_client/events/privacy.py +187 -0
- zapy/_client/events/privacy_token.py +72 -0
- zapy/_client/events/receipt.py +109 -0
- zapy/_client/events/registration.py +70 -0
- zapy/_client/events/username.py +76 -0
- zapy/_client/factory.py +2020 -0
- zapy/_client/media.py +829 -0
- zapy/_client/newsletter/__init__.py +90 -0
- zapy/_client/newsletter/admin.py +323 -0
- zapy/_client/newsletter/content.py +499 -0
- zapy/_client/newsletter/discovery.py +233 -0
- zapy/_client/newsletter/messaging.py +396 -0
- zapy/_client/newsletter/mex.py +99 -0
- zapy/_client/newsletter/parse.py +406 -0
- zapy/_client/newsletter/types.py +393 -0
- zapy/_client/persistence/__init__.py +89 -0
- zapy/_client/persistence/companion_host.py +133 -0
- zapy/_client/persistence/ephemeral_setting.py +245 -0
- zapy/_client/persistence/group_history.py +361 -0
- zapy/_client/persistence/history_blob.py +169 -0
- zapy/_client/persistence/history_sync.py +646 -0
- zapy/_client/persistence/mailbox.py +232 -0
- zapy/_client/persistence/write_behind_persistence.py +368 -0
- zapy/_client/plugins/__init__.py +23 -0
- zapy/_client/plugins/define.py +44 -0
- zapy/_client/plugins/install.py +254 -0
- zapy/_client/plugins/types.py +64 -0
- zapy/_client/tokens/__init__.py +21 -0
- zapy/_client/tokens/cs_token.py +47 -0
- zapy/_client/tokens/tc_token.py +43 -0
- zapy/_client/types.py +988 -0
- zapy/_crypto/__init__.py +65 -0
- zapy/_crypto/core/__init__.py +62 -0
- zapy/_crypto/core/hkdf.py +41 -0
- zapy/_crypto/core/keys.py +51 -0
- zapy/_crypto/core/nonce.py +25 -0
- zapy/_crypto/core/primitives.py +179 -0
- zapy/_crypto/core/random.py +32 -0
- zapy/_crypto/core/xeddsa.py +72 -0
- zapy/_crypto/curves/__init__.py +3 -0
- zapy/_crypto/curves/constants.py +10 -0
- zapy/_crypto/curves/ed25519.py +61 -0
- zapy/_crypto/curves/types.py +18 -0
- zapy/_crypto/curves/x25519.py +117 -0
- zapy/_crypto/math/__init__.py +3 -0
- zapy/_crypto/math/constants.py +35 -0
- zapy/_crypto/math/edwards.py +174 -0
- zapy/_crypto/math/fe.py +120 -0
- zapy/_crypto/math/le.py +13 -0
- zapy/_crypto/math/mod.py +15 -0
- zapy/_crypto/math/prime.py +11 -0
- zapy/_crypto/math/types.py +25 -0
- zapy/_crypto/native_backend.py +40 -0
- zapy/_infra/__init__.py +3 -0
- zapy/_infra/log.py +142 -0
- zapy/_infra/perf.py +77 -0
- zapy/_media/__init__.py +64 -0
- zapy/_media/constants.py +133 -0
- zapy/_media/container.py +110 -0
- zapy/_media/crypto/__init__.py +19 -0
- zapy/_media/crypto/crypto.py +633 -0
- zapy/_media/crypto/media_retry.py +107 -0
- zapy/_media/processor.py +100 -0
- zapy/_media/sticker/__init__.py +8 -0
- zapy/_media/sticker/pack.py +188 -0
- zapy/_media/transfer/__init__.py +15 -0
- zapy/_media/transfer/client.py +635 -0
- zapy/_media/transfer/conn.py +48 -0
- zapy/_media/types.py +169 -0
- zapy/_message/__init__.py +121 -0
- zapy/_message/addons/__init__.py +5 -0
- zapy/_message/addons/link_preview/__init__.py +54 -0
- zapy/_message/addons/link_preview/builder.py +56 -0
- zapy/_message/addons/link_preview/detect.py +72 -0
- zapy/_message/addons/link_preview/fetcher.py +553 -0
- zapy/_message/addons/link_preview/types.py +100 -0
- zapy/_message/client.py +486 -0
- zapy/_message/context_info.py +424 -0
- zapy/_message/crypto/__init__.py +71 -0
- zapy/_message/crypto/addon_crypto.py +656 -0
- zapy/_message/crypto/icdc.py +138 -0
- zapy/_message/crypto/phash.py +87 -0
- zapy/_message/crypto/reporting_token.py +498 -0
- zapy/_message/crypto/use_case_secret.py +113 -0
- zapy/_message/encode/__init__.py +84 -0
- zapy/_message/encode/content.py +636 -0
- zapy/_message/encode/device_sent.py +88 -0
- zapy/_message/encode/media_payload.py +122 -0
- zapy/_message/encode/padding.py +27 -0
- zapy/_message/kinds/__init__.py +1 -0
- zapy/_message/kinds/bot.py +179 -0
- zapy/_message/kinds/group_history.py +206 -0
- zapy/_message/kinds/newsletter.py +388 -0
- zapy/_message/kinds/sticker_pack.py +74 -0
- zapy/_message/messaging/__init__.py +336 -0
- zapy/_message/messaging/companion_host.py +117 -0
- zapy/_message/messaging/fanout.py +311 -0
- zapy/_message/messaging/group_metadata.py +444 -0
- zapy/_message/messaging/ignore_key.py +264 -0
- zapy/_message/messaging/key_protocol.py +217 -0
- zapy/_message/messaging/link_preview.py +448 -0
- zapy/_message/messaging/messages.py +1429 -0
- zapy/_message/primitives/__init__.py +1 -0
- zapy/_message/primitives/ack.py +232 -0
- zapy/_message/primitives/incoming.py +1141 -0
- zapy/_message/primitives/media_retry.py +451 -0
- zapy/_message/primitives/peer_data_operation.py +271 -0
- zapy/_message/types.py +434 -0
- zapy/_proto/WAProto_pb2.py +2584 -0
- zapy/_proto/__init__.py +1 -0
- zapy/_protocol/__init__.py +3 -0
- zapy/_protocol/abprops_spec.py +6216 -0
- zapy/_protocol/auth.py +17 -0
- zapy/_protocol/browser.py +57 -0
- zapy/_protocol/defaults.py +38 -0
- zapy/_protocol/device_identity.py +54 -0
- zapy/_protocol/jid.py +241 -0
- zapy/_protocol/message.py +127 -0
- zapy/_protocol/nodes.py +144 -0
- zapy/_protocol/notification.py +18 -0
- zapy/_retry/__init__.py +77 -0
- zapy/_retry/codec.py +209 -0
- zapy/_retry/constants.py +28 -0
- zapy/_retry/parse.py +132 -0
- zapy/_retry/reason.py +65 -0
- zapy/_retry/replay.py +483 -0
- zapy/_retry/tracker.py +161 -0
- zapy/_retry/types.py +122 -0
- zapy/_signal/__init__.py +171 -0
- zapy/_signal/api/__init__.py +133 -0
- zapy/_signal/api/codec.py +164 -0
- zapy/_signal/api/constants.py +14 -0
- zapy/_signal/api/device_sync.py +915 -0
- zapy/_signal/api/digest_sync.py +363 -0
- zapy/_signal/api/identity_sync.py +250 -0
- zapy/_signal/api/missing_pre_keys_sync.py +267 -0
- zapy/_signal/api/prekeys.py +191 -0
- zapy/_signal/api/result_map.py +40 -0
- zapy/_signal/api/rotate_key.py +111 -0
- zapy/_signal/api/session_sync.py +297 -0
- zapy/_signal/attestation.py +145 -0
- zapy/_signal/constants.py +22 -0
- zapy/_signal/encoding.py +78 -0
- zapy/_signal/group/__init__.py +35 -0
- zapy/_signal/group/chain.py +139 -0
- zapy/_signal/group/codec.py +78 -0
- zapy/_signal/group/encoding.py +166 -0
- zapy/_signal/group/manager.py +361 -0
- zapy/_signal/registration/__init__.py +44 -0
- zapy/_signal/registration/keygen.py +46 -0
- zapy/_signal/registration/reg_encoding.py +66 -0
- zapy/_signal/registration/reg_utils.py +52 -0
- zapy/_signal/session/__init__.py +114 -0
- zapy/_signal/session/encoding.py +385 -0
- zapy/_signal/session/protocol.py +590 -0
- zapy/_signal/session/ratchet.py +381 -0
- zapy/_signal/session/resolver.py +630 -0
- zapy/_signal/session/serializer.py +96 -0
- zapy/_signal/session/session.py +272 -0
- zapy/_signal/types.py +214 -0
- zapy/_store/__init__.py +118 -0
- zapy/_store/cache/__init__.py +166 -0
- zapy/_store/cache/identity.py +130 -0
- zapy/_store/cache/privacy_token.py +116 -0
- zapy/_store/cache/sender_key.py +168 -0
- zapy/_store/cache/session.py +142 -0
- zapy/_store/contracts/__init__.py +84 -0
- zapy/_store/contracts/appstate.py +82 -0
- zapy/_store/contracts/auth.py +16 -0
- zapy/_store/contracts/chat_metadata.py +29 -0
- zapy/_store/contracts/contact.py +31 -0
- zapy/_store/contracts/device_list.py +35 -0
- zapy/_store/contracts/group_metadata.py +33 -0
- zapy/_store/contracts/identity.py +18 -0
- zapy/_store/contracts/message.py +36 -0
- zapy/_store/contracts/message_secret.py +41 -0
- zapy/_store/contracts/pre_key.py +32 -0
- zapy/_store/contracts/privacy_token.py +32 -0
- zapy/_store/contracts/retry.py +47 -0
- zapy/_store/contracts/sender_key.py +37 -0
- zapy/_store/contracts/session.py +21 -0
- zapy/_store/contracts/signal.py +23 -0
- zapy/_store/contracts/thread.py +35 -0
- zapy/_store/create_store.py +890 -0
- zapy/_store/locks.py +1154 -0
- zapy/_store/memory/__init__.py +40 -0
- zapy/_store/memory/appstate.py +200 -0
- zapy/_store/memory/auth.py +24 -0
- zapy/_store/memory/chat_metadata.py +124 -0
- zapy/_store/memory/contact.py +125 -0
- zapy/_store/memory/device_list.py +161 -0
- zapy/_store/memory/group_metadata.py +125 -0
- zapy/_store/memory/identity.py +86 -0
- zapy/_store/memory/message.py +85 -0
- zapy/_store/memory/message_secret.py +145 -0
- zapy/_store/memory/pre_key.py +112 -0
- zapy/_store/memory/privacy_token.py +103 -0
- zapy/_store/memory/retry.py +250 -0
- zapy/_store/memory/sender_key.py +162 -0
- zapy/_store/memory/session.py +111 -0
- zapy/_store/memory/signal.py +45 -0
- zapy/_store/memory/thread.py +66 -0
- zapy/_store/noop.py +279 -0
- zapy/_transport/__init__.py +3 -0
- zapy/_transport/binary/__init__.py +77 -0
- zapy/_transport/binary/constants.py +80 -0
- zapy/_transport/binary/decoder.py +260 -0
- zapy/_transport/binary/encoder.py +279 -0
- zapy/_transport/binary/tokens.py +1289 -0
- zapy/_transport/comms.py +759 -0
- zapy/_transport/keepalive/__init__.py +3 -0
- zapy/_transport/keepalive/keep_alive.py +252 -0
- zapy/_transport/node/__init__.py +3 -0
- zapy/_transport/node/builders/__init__.py +9 -0
- zapy/_transport/node/builders/abprops.py +33 -0
- zapy/_transport/node/builders/account_sync.py +120 -0
- zapy/_transport/node/builders/bot.py +56 -0
- zapy/_transport/node/builders/business.py +261 -0
- zapy/_transport/node/builders/chatstate.py +74 -0
- zapy/_transport/node/builders/community.py +116 -0
- zapy/_transport/node/builders/device.py +30 -0
- zapy/_transport/node/builders/email.py +181 -0
- zapy/_transport/node/builders/global_.py +74 -0
- zapy/_transport/node/builders/group.py +279 -0
- zapy/_transport/node/builders/media.py +21 -0
- zapy/_transport/node/builders/message.py +436 -0
- zapy/_transport/node/builders/mobile.py +227 -0
- zapy/_transport/node/builders/newsletter.py +293 -0
- zapy/_transport/node/builders/offline.py +20 -0
- zapy/_transport/node/builders/pairing.py +130 -0
- zapy/_transport/node/builders/passive.py +28 -0
- zapy/_transport/node/builders/prekeys.py +215 -0
- zapy/_transport/node/builders/presence.py +100 -0
- zapy/_transport/node/builders/privacy.py +243 -0
- zapy/_transport/node/builders/privacy_token.py +94 -0
- zapy/_transport/node/builders/profile.py +176 -0
- zapy/_transport/node/builders/retry.py +156 -0
- zapy/_transport/node/builders/shortcake.py +87 -0
- zapy/_transport/node/builders/tos.py +92 -0
- zapy/_transport/node/builders/usync.py +185 -0
- zapy/_transport/node/helpers.py +201 -0
- zapy/_transport/node/orchestrator.py +210 -0
- zapy/_transport/node/query.py +82 -0
- zapy/_transport/node/transport.py +105 -0
- zapy/_transport/node/usync.py +33 -0
- zapy/_transport/node/xml.py +38 -0
- zapy/_transport/noise/__init__.py +7 -0
- zapy/_transport/noise/cert.py +85 -0
- zapy/_transport/noise/client_payload.py +344 -0
- zapy/_transport/noise/constants.py +12 -0
- zapy/_transport/noise/frame_codec.py +120 -0
- zapy/_transport/noise/handshake.py +92 -0
- zapy/_transport/noise/session.py +493 -0
- zapy/_transport/noise/socket.py +45 -0
- zapy/_transport/noise/types.py +66 -0
- zapy/_transport/noise/version.json +3 -0
- zapy/_transport/proxy.py +35 -0
- zapy/_transport/types.py +19 -0
- zapy/_transport/wa_version_fetcher.py +159 -0
- zapy/_transport/web_socket.py +602 -0
- zapy/_util/__init__.py +3 -0
- zapy/_util/bytes.py +131 -0
- zapy/_util/coercion.py +29 -0
- zapy/_util/collections.py +45 -0
- zapy/_util/primitives.py +49 -0
- zapy/client.py +840 -0
- zapy/errors.py +148 -0
- zapy/events.py +564 -0
- zapy/jid.py +140 -0
- zapy/py.typed +0 -0
- zapy/stores/__init__.py +50 -0
- zapy/stores/base.py +1627 -0
- zapy/stores/json_file.py +359 -0
- zapy/stores/memory.py +322 -0
- zapy/stores/sqlite.py +343 -0
zapy/_crypto/__init__.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Cryptographic primitives (mirrors ``src/crypto/index.ts``)."""
|
|
2
|
+
|
|
3
|
+
from zapy._crypto.core import (
|
|
4
|
+
Ed25519,
|
|
5
|
+
X25519,
|
|
6
|
+
aes_cbc_decrypt,
|
|
7
|
+
aes_cbc_encrypt,
|
|
8
|
+
aes_cbc_encrypt_with_trailer,
|
|
9
|
+
aes_ctr_decrypt,
|
|
10
|
+
aes_ctr_encrypt,
|
|
11
|
+
aes_gcm_decrypt,
|
|
12
|
+
aes_gcm_encrypt,
|
|
13
|
+
hkdf,
|
|
14
|
+
hkdf_split,
|
|
15
|
+
hmac_sha256_sign,
|
|
16
|
+
hmac_sha512_sign,
|
|
17
|
+
md5_bytes,
|
|
18
|
+
pbkdf2_sha256,
|
|
19
|
+
prepend_version,
|
|
20
|
+
random_bytes,
|
|
21
|
+
random_fill,
|
|
22
|
+
random_int,
|
|
23
|
+
read_versioned_content,
|
|
24
|
+
sha1,
|
|
25
|
+
sha256,
|
|
26
|
+
sha512,
|
|
27
|
+
to_raw_pub_key,
|
|
28
|
+
to_serialized_pub_key,
|
|
29
|
+
write_nonce_counter,
|
|
30
|
+
xeddsa_sign,
|
|
31
|
+
xeddsa_verify,
|
|
32
|
+
)
|
|
33
|
+
from zapy._crypto.curves.types import SignalKeyPair
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"Ed25519",
|
|
37
|
+
"SignalKeyPair",
|
|
38
|
+
"X25519",
|
|
39
|
+
"aes_cbc_decrypt",
|
|
40
|
+
"aes_cbc_encrypt",
|
|
41
|
+
"aes_cbc_encrypt_with_trailer",
|
|
42
|
+
"aes_ctr_decrypt",
|
|
43
|
+
"aes_ctr_encrypt",
|
|
44
|
+
"aes_gcm_decrypt",
|
|
45
|
+
"aes_gcm_encrypt",
|
|
46
|
+
"hkdf",
|
|
47
|
+
"hkdf_split",
|
|
48
|
+
"hmac_sha256_sign",
|
|
49
|
+
"hmac_sha512_sign",
|
|
50
|
+
"md5_bytes",
|
|
51
|
+
"pbkdf2_sha256",
|
|
52
|
+
"prepend_version",
|
|
53
|
+
"random_bytes",
|
|
54
|
+
"random_fill",
|
|
55
|
+
"random_int",
|
|
56
|
+
"read_versioned_content",
|
|
57
|
+
"sha1",
|
|
58
|
+
"sha256",
|
|
59
|
+
"sha512",
|
|
60
|
+
"to_raw_pub_key",
|
|
61
|
+
"to_serialized_pub_key",
|
|
62
|
+
"write_nonce_counter",
|
|
63
|
+
"xeddsa_sign",
|
|
64
|
+
"xeddsa_verify",
|
|
65
|
+
]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Core crypto barrel (mirrors ``src/crypto/core/index.ts``)."""
|
|
2
|
+
|
|
3
|
+
from zapy._crypto.core.hkdf import hkdf, hkdf_split
|
|
4
|
+
from zapy._crypto.core.keys import (
|
|
5
|
+
prepend_version,
|
|
6
|
+
read_versioned_content,
|
|
7
|
+
to_raw_pub_key,
|
|
8
|
+
to_serialized_pub_key,
|
|
9
|
+
)
|
|
10
|
+
from zapy._crypto.core.nonce import write_nonce_counter
|
|
11
|
+
from zapy._crypto.core.primitives import (
|
|
12
|
+
aes_cbc_decrypt,
|
|
13
|
+
aes_cbc_encrypt,
|
|
14
|
+
aes_cbc_encrypt_with_trailer,
|
|
15
|
+
aes_ctr_decrypt,
|
|
16
|
+
aes_ctr_encrypt,
|
|
17
|
+
aes_gcm_decrypt,
|
|
18
|
+
aes_gcm_encrypt,
|
|
19
|
+
hmac_sha256_sign,
|
|
20
|
+
hmac_sha512_sign,
|
|
21
|
+
md5_bytes,
|
|
22
|
+
pbkdf2_sha256,
|
|
23
|
+
sha1,
|
|
24
|
+
sha256,
|
|
25
|
+
sha512,
|
|
26
|
+
)
|
|
27
|
+
from zapy._crypto.core.random import random_bytes, random_fill, random_int
|
|
28
|
+
from zapy._crypto.core.xeddsa import xeddsa_sign, xeddsa_verify
|
|
29
|
+
from zapy._crypto.curves.ed25519 import Ed25519
|
|
30
|
+
from zapy._crypto.curves.x25519 import X25519
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"Ed25519",
|
|
34
|
+
"X25519",
|
|
35
|
+
"aes_cbc_decrypt",
|
|
36
|
+
"aes_cbc_encrypt",
|
|
37
|
+
"aes_cbc_encrypt_with_trailer",
|
|
38
|
+
"aes_ctr_decrypt",
|
|
39
|
+
"aes_ctr_encrypt",
|
|
40
|
+
"aes_gcm_decrypt",
|
|
41
|
+
"aes_gcm_encrypt",
|
|
42
|
+
"hkdf",
|
|
43
|
+
"hkdf_split",
|
|
44
|
+
"hmac_sha256_sign",
|
|
45
|
+
"hmac_sha512_sign",
|
|
46
|
+
"md5_bytes",
|
|
47
|
+
"pbkdf2_sha256",
|
|
48
|
+
"prepend_version",
|
|
49
|
+
"random_bytes",
|
|
50
|
+
"random_fill",
|
|
51
|
+
"random_int",
|
|
52
|
+
"read_versioned_content",
|
|
53
|
+
"sha1",
|
|
54
|
+
"write_nonce_counter",
|
|
55
|
+
"sha256",
|
|
56
|
+
"sha512",
|
|
57
|
+
"to_raw_pub_key",
|
|
58
|
+
"to_serialized_pub_key",
|
|
59
|
+
"write_nonce_counter",
|
|
60
|
+
"xeddsa_sign",
|
|
61
|
+
"xeddsa_verify",
|
|
62
|
+
]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""HKDF-SHA256 key derivation (manual extract-and-expand, RFC 5869)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import hmac
|
|
7
|
+
|
|
8
|
+
HashInput = (
|
|
9
|
+
bytes | bytearray | list[bytes] | list[bytearray] | tuple[bytes | bytearray, ...]
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _hmac_sha256(key: bytes, data: bytes) -> bytes:
|
|
14
|
+
return hmac.new(key, data, hashlib.sha256).digest()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def hkdf(
|
|
18
|
+
ikm: bytes | bytearray,
|
|
19
|
+
salt: bytes | bytearray | None,
|
|
20
|
+
info: bytes | bytearray,
|
|
21
|
+
out_length: int,
|
|
22
|
+
) -> bytes:
|
|
23
|
+
"""Derive ``out_length`` bytes from ``ikm`` (empty salt when ``None``)."""
|
|
24
|
+
prk = _hmac_sha256(bytes(salt) if salt is not None else b"", bytes(ikm))
|
|
25
|
+
info_bytes = bytes(info)
|
|
26
|
+
okm = b""
|
|
27
|
+
block = b""
|
|
28
|
+
counter = 1
|
|
29
|
+
while len(okm) < out_length:
|
|
30
|
+
block = _hmac_sha256(prk, block + info_bytes + bytes([counter]))
|
|
31
|
+
okm += block
|
|
32
|
+
counter += 1
|
|
33
|
+
return okm[:out_length]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def hkdf_split(
|
|
37
|
+
ikm: bytes | bytearray, salt: bytes | bytearray | None, info: bytes | bytearray
|
|
38
|
+
) -> tuple[bytes, bytes]:
|
|
39
|
+
"""Derive 64 bytes and return them split into two 32-byte halves."""
|
|
40
|
+
out = hkdf(ikm, salt, info, 64)
|
|
41
|
+
return out[:32], out[32:]
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Signal-style public key serialization and versioned envelopes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from zapy._util.bytes import assert_byte_length
|
|
6
|
+
|
|
7
|
+
_SERIALIZED_PUB_KEY_PREFIX = 5
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def to_serialized_pub_key(key: bytes | bytearray) -> bytes:
|
|
11
|
+
"""32-byte raw key to 33-byte prefixed form; valid 33-byte input passes through."""
|
|
12
|
+
raw = bytes(key)
|
|
13
|
+
if len(raw) == 33:
|
|
14
|
+
if raw[0] != _SERIALIZED_PUB_KEY_PREFIX:
|
|
15
|
+
raise ValueError("invalid serialized signal public key prefix")
|
|
16
|
+
return raw
|
|
17
|
+
assert_byte_length(raw, 32, f"invalid signal public key length {len(raw)}")
|
|
18
|
+
return bytes([_SERIALIZED_PUB_KEY_PREFIX]) + raw
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def to_raw_pub_key(key: bytes | bytearray) -> bytes:
|
|
22
|
+
"""33-byte serialized key to 32-byte raw form; 32-byte input passes through."""
|
|
23
|
+
raw = bytes(key)
|
|
24
|
+
if len(raw) == 32:
|
|
25
|
+
return raw
|
|
26
|
+
if len(raw) == 33 and raw[0] == _SERIALIZED_PUB_KEY_PREFIX:
|
|
27
|
+
return raw[1:]
|
|
28
|
+
raise ValueError(f"invalid signal public key length {len(raw)}")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def prepend_version(content: bytes | bytearray, version: int) -> bytes:
|
|
32
|
+
"""Prepend a ``(version << 4) | version`` byte to ``content``."""
|
|
33
|
+
return bytes([(version << 4 | version) & 0xFF]) + bytes(content)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def read_versioned_content(
|
|
37
|
+
version_content: bytes | bytearray, expected_version: int, suffix_length: int
|
|
38
|
+
) -> bytes:
|
|
39
|
+
"""Validate the version nibble and return the body without the trailing suffix."""
|
|
40
|
+
raw = bytes(version_content)
|
|
41
|
+
if len(raw) < 1:
|
|
42
|
+
raise ValueError("signal versioned content is empty")
|
|
43
|
+
version = raw[0] >> 4
|
|
44
|
+
if version != expected_version:
|
|
45
|
+
if version < expected_version:
|
|
46
|
+
raise ValueError("legacy signal version")
|
|
47
|
+
raise ValueError(f"unsupported signal version {version}")
|
|
48
|
+
body_end = len(raw) - suffix_length
|
|
49
|
+
if body_end <= 1:
|
|
50
|
+
raise ValueError("invalid signal content length")
|
|
51
|
+
return raw[1:body_end]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""AES-GCM 12-byte nonce counter writer.
|
|
2
|
+
|
|
3
|
+
# DEVIATION: the TS version coerces ``counter`` with ``>>>`` (so negatives
|
|
4
|
+
wrap and floats truncate). Python requires a plain ``int`` in uint32 range
|
|
5
|
+
and raises ``ValueError`` otherwise; protocol callers always pass counters.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def write_nonce_counter(out: bytearray, counter: int) -> None:
|
|
12
|
+
"""Write a 32-bit big-endian ``counter`` into the last 4 bytes of a 12-byte nonce.
|
|
13
|
+
|
|
14
|
+
The first 8 bytes are left untouched.
|
|
15
|
+
"""
|
|
16
|
+
if len(out) < 12:
|
|
17
|
+
raise ValueError(f"nonce buffer must be at least 12 bytes, got {len(out)}")
|
|
18
|
+
if isinstance(counter, bool) or not isinstance(counter, int):
|
|
19
|
+
raise ValueError(f"nonce counter must be an integer, got {counter!r}")
|
|
20
|
+
if counter < 0 or counter > 0xFFFFFFFF:
|
|
21
|
+
raise ValueError("nonce counter overflow: exceeds uint32 range")
|
|
22
|
+
out[8] = (counter >> 24) & 0xFF
|
|
23
|
+
out[9] = (counter >> 16) & 0xFF
|
|
24
|
+
out[10] = (counter >> 8) & 0xFF
|
|
25
|
+
out[11] = counter & 0xFF
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Symmetric primitives over hashlib/hmac (sync) and ``cryptography`` (AES)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import hmac
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
|
|
9
|
+
from cryptography.hazmat.primitives import padding as symmetric_padding
|
|
10
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
11
|
+
|
|
12
|
+
HashInput = bytes | bytearray | Sequence[bytes | bytearray]
|
|
13
|
+
|
|
14
|
+
_AES_GCM_TAG_LENGTH = 16
|
|
15
|
+
_AES_CBC_BLOCK_SIZE = 16
|
|
16
|
+
_MAX_SAFE_INTEGER = 2**53 - 1
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _as_trailer_length(value: object) -> int:
|
|
20
|
+
if isinstance(value, bool):
|
|
21
|
+
raise ValueError(f"invalid trailer length {value}")
|
|
22
|
+
if isinstance(value, float):
|
|
23
|
+
if (
|
|
24
|
+
value != value
|
|
25
|
+
or value in (float("inf"), float("-inf"))
|
|
26
|
+
or not value.is_integer()
|
|
27
|
+
or abs(value) > _MAX_SAFE_INTEGER
|
|
28
|
+
):
|
|
29
|
+
raise ValueError(f"invalid trailer length {value}")
|
|
30
|
+
return int(value)
|
|
31
|
+
if not isinstance(value, int) or abs(value) > _MAX_SAFE_INTEGER or value < 0:
|
|
32
|
+
raise ValueError(f"invalid trailer length {value}")
|
|
33
|
+
return value
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _feed(data: HashInput) -> list[bytes]:
|
|
37
|
+
if isinstance(data, (bytes, bytearray)):
|
|
38
|
+
return [bytes(data)]
|
|
39
|
+
return [bytes(chunk) for chunk in data]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def sha1(value: HashInput) -> bytes:
|
|
43
|
+
"""SHA-1 digest of one chunk or a chunk sequence."""
|
|
44
|
+
digest = hashlib.sha1()
|
|
45
|
+
for chunk in _feed(value):
|
|
46
|
+
digest.update(chunk)
|
|
47
|
+
return digest.digest()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def sha256(value: HashInput) -> bytes:
|
|
51
|
+
"""SHA-256 digest of one chunk or a chunk sequence."""
|
|
52
|
+
digest = hashlib.sha256()
|
|
53
|
+
for chunk in _feed(value):
|
|
54
|
+
digest.update(chunk)
|
|
55
|
+
return digest.digest()
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def sha512(value: HashInput) -> bytes:
|
|
59
|
+
"""SHA-512 digest of one chunk or a chunk sequence."""
|
|
60
|
+
digest = hashlib.sha512()
|
|
61
|
+
for chunk in _feed(value):
|
|
62
|
+
digest.update(chunk)
|
|
63
|
+
return digest.digest()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def md5_bytes(data: str | HashInput) -> bytes:
|
|
67
|
+
"""MD5 digest of a string or chunk sequence."""
|
|
68
|
+
digest = hashlib.md5()
|
|
69
|
+
if isinstance(data, str):
|
|
70
|
+
digest.update(data.encode("utf-8"))
|
|
71
|
+
else:
|
|
72
|
+
for chunk in _feed(data):
|
|
73
|
+
digest.update(chunk)
|
|
74
|
+
return digest.digest()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def aes_gcm_encrypt(
|
|
78
|
+
key: bytes | bytearray,
|
|
79
|
+
nonce: bytes | bytearray,
|
|
80
|
+
plaintext: bytes | bytearray,
|
|
81
|
+
aad: bytes | bytearray = b"",
|
|
82
|
+
) -> bytes:
|
|
83
|
+
"""AES-256-GCM encrypt with the 16-byte tag appended. Binds ``aad`` when given."""
|
|
84
|
+
encryptor = Cipher(algorithms.AES(bytes(key)), modes.GCM(bytes(nonce))).encryptor()
|
|
85
|
+
if aad:
|
|
86
|
+
encryptor.authenticate_additional_data(bytes(aad))
|
|
87
|
+
return encryptor.update(bytes(plaintext)) + encryptor.finalize() + encryptor.tag
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def aes_gcm_decrypt(
|
|
91
|
+
key: bytes | bytearray,
|
|
92
|
+
nonce: bytes | bytearray,
|
|
93
|
+
ciphertext: bytes | bytearray,
|
|
94
|
+
aad: bytes | bytearray = b"",
|
|
95
|
+
) -> bytes:
|
|
96
|
+
"""AES-256-GCM decrypt (tag appended). Throws on auth failure."""
|
|
97
|
+
raw = bytes(ciphertext)
|
|
98
|
+
tag = raw[-_AES_GCM_TAG_LENGTH:]
|
|
99
|
+
body = raw[:-_AES_GCM_TAG_LENGTH]
|
|
100
|
+
algorithm = algorithms.AES(bytes(key))
|
|
101
|
+
decryptor = Cipher(algorithm, modes.GCM(bytes(nonce), tag)).decryptor()
|
|
102
|
+
if aad:
|
|
103
|
+
decryptor.authenticate_additional_data(bytes(aad))
|
|
104
|
+
return decryptor.update(body) + decryptor.finalize()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def aes_cbc_encrypt(
|
|
108
|
+
key: bytes | bytearray, iv: bytes | bytearray, plaintext: bytes | bytearray
|
|
109
|
+
) -> bytes:
|
|
110
|
+
"""AES-256-CBC encrypt with PKCS#7 padding."""
|
|
111
|
+
encryptor = Cipher(algorithms.AES(bytes(key)), modes.CBC(bytes(iv))).encryptor()
|
|
112
|
+
padder = symmetric_padding.PKCS7(_AES_CBC_BLOCK_SIZE * 8).padder()
|
|
113
|
+
padded = padder.update(bytes(plaintext)) + padder.finalize()
|
|
114
|
+
return encryptor.update(padded) + encryptor.finalize()
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def aes_cbc_encrypt_with_trailer(
|
|
118
|
+
key: bytes | bytearray,
|
|
119
|
+
iv: bytes | bytearray,
|
|
120
|
+
plaintext: bytes | bytearray,
|
|
121
|
+
trailer_bytes: int | float,
|
|
122
|
+
) -> bytes:
|
|
123
|
+
"""CBC encrypt plus ``trailer_bytes`` of zeroed spare room at the end."""
|
|
124
|
+
trailer = _as_trailer_length(trailer_bytes)
|
|
125
|
+
ciphertext = aes_cbc_encrypt(key, iv, plaintext)
|
|
126
|
+
return ciphertext + b"\x00" * trailer
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def aes_cbc_decrypt(
|
|
130
|
+
key: bytes | bytearray, iv: bytes | bytearray, ciphertext: bytes | bytearray
|
|
131
|
+
) -> bytes:
|
|
132
|
+
"""AES-256-CBC decrypt, removing PKCS#7 padding."""
|
|
133
|
+
decryptor = Cipher(algorithms.AES(bytes(key)), modes.CBC(bytes(iv))).decryptor()
|
|
134
|
+
padded = decryptor.update(bytes(ciphertext)) + decryptor.finalize()
|
|
135
|
+
unpadder = symmetric_padding.PKCS7(_AES_CBC_BLOCK_SIZE * 8).unpadder()
|
|
136
|
+
return unpadder.update(padded) + unpadder.finalize()
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def aes_ctr_encrypt(
|
|
140
|
+
key: bytes | bytearray, counter: bytes | bytearray, plaintext: bytes | bytearray
|
|
141
|
+
) -> bytes:
|
|
142
|
+
"""AES-256-CTR encrypt with the given 16-byte counter."""
|
|
143
|
+
algorithm = algorithms.AES(bytes(key))
|
|
144
|
+
encryptor = Cipher(algorithm, modes.CTR(bytes(counter))).encryptor()
|
|
145
|
+
return encryptor.update(bytes(plaintext)) + encryptor.finalize()
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def aes_ctr_decrypt(
|
|
149
|
+
key: bytes | bytearray, counter: bytes | bytearray, ciphertext: bytes | bytearray
|
|
150
|
+
) -> bytes:
|
|
151
|
+
"""AES-256-CTR decrypt with the given 16-byte counter."""
|
|
152
|
+
algorithm = algorithms.AES(bytes(key))
|
|
153
|
+
decryptor = Cipher(algorithm, modes.CTR(bytes(counter))).decryptor()
|
|
154
|
+
return decryptor.update(bytes(ciphertext)) + decryptor.finalize()
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def hmac_sha256_sign(key: bytes | bytearray, data: HashInput) -> bytes:
|
|
158
|
+
"""HMAC-SHA-256 MAC of ``data`` keyed by ``key``."""
|
|
159
|
+
mac = hmac.new(bytes(key), digestmod=hashlib.sha256)
|
|
160
|
+
for chunk in _feed(data):
|
|
161
|
+
mac.update(chunk)
|
|
162
|
+
return mac.digest()
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def hmac_sha512_sign(key: bytes | bytearray, data: HashInput) -> bytes:
|
|
166
|
+
"""HMAC-SHA-512 MAC of ``data`` keyed by ``key``."""
|
|
167
|
+
mac = hmac.new(bytes(key), digestmod=hashlib.sha512)
|
|
168
|
+
for chunk in _feed(data):
|
|
169
|
+
mac.update(chunk)
|
|
170
|
+
return mac.digest()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def pbkdf2_sha256(
|
|
174
|
+
password: bytes | bytearray, salt: bytes | bytearray, iterations: int, length: int
|
|
175
|
+
) -> bytes:
|
|
176
|
+
"""PBKDF2-HMAC-SHA-256 (synchronous: fast, no thread pool)."""
|
|
177
|
+
return hashlib.pbkdf2_hmac(
|
|
178
|
+
"sha256", bytes(password), bytes(salt), iterations, length
|
|
179
|
+
)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Synchronous cryptographic randomness.
|
|
2
|
+
|
|
3
|
+
# DEVIATION: the TS ``random*Async`` functions are async only to push Node
|
|
4
|
+
crypto onto the thread pool. Per the porting rules they are synchronous here
|
|
5
|
+
(``secrets`` is fast), with the ``_async`` suffix dropped from the names.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import secrets
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def random_fill(
|
|
14
|
+
target: bytearray, offset: int = 0, size: int | None = None
|
|
15
|
+
) -> bytearray:
|
|
16
|
+
"""Fill ``target[offset:offset+size]`` (default: to the end) with random bytes.
|
|
17
|
+
|
|
18
|
+
Returns the same buffer for chaining.
|
|
19
|
+
"""
|
|
20
|
+
end = len(target) if size is None else offset + size
|
|
21
|
+
target[offset:end] = secrets.token_bytes(end - offset)
|
|
22
|
+
return target
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def random_int(min_value: int, max_value: int) -> int:
|
|
26
|
+
"""A random integer in ``[min_value, max_value)``."""
|
|
27
|
+
return secrets.randbelow(max_value - min_value) + min_value
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def random_bytes(size: int) -> bytes:
|
|
31
|
+
"""``size`` cryptographically secure random bytes."""
|
|
32
|
+
return secrets.token_bytes(size)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""XEdDSA sign/verify over X25519 keys (pure-Python Edwards math, no accelerator).
|
|
2
|
+
|
|
3
|
+
The TS selects an optional NAPI/WASM backend; Python always uses this
|
|
4
|
+
implementation (see ``zapy._crypto.native_backend``).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from zapy._crypto.core.primitives import sha512
|
|
10
|
+
from zapy._crypto.core.random import random_bytes
|
|
11
|
+
from zapy._crypto.curves.ed25519 import Ed25519
|
|
12
|
+
from zapy._crypto.curves.x25519 import (
|
|
13
|
+
clamp_curve_private_key_in_place,
|
|
14
|
+
montgomery_to_edwards_public,
|
|
15
|
+
)
|
|
16
|
+
from zapy._crypto.math.edwards import encode_extended_point, scalar_mult_base
|
|
17
|
+
from zapy._crypto.math.le import big_int_to_bytes_le, bytes_to_big_int_le
|
|
18
|
+
from zapy._crypto.math.mod import mod_group
|
|
19
|
+
from zapy._util.bytes import assert_byte_length, concat_bytes
|
|
20
|
+
|
|
21
|
+
_PREFIX_SIGNATURE_RANDOM = bytes(
|
|
22
|
+
[0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
|
|
23
|
+
+ [0xFF] * 20
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def xeddsa_verify(
|
|
28
|
+
curve_public_key: bytes | bytearray,
|
|
29
|
+
message: bytes | bytearray,
|
|
30
|
+
signature: bytes | bytearray,
|
|
31
|
+
) -> bool:
|
|
32
|
+
"""Verify an XEdDSA signature. ``False`` on shape mismatch, never throws."""
|
|
33
|
+
sig = bytearray(signature)
|
|
34
|
+
if len(sig) != 64:
|
|
35
|
+
return False
|
|
36
|
+
if sig[63] & 0x60 != 0:
|
|
37
|
+
return False
|
|
38
|
+
|
|
39
|
+
sign_bit = sig[63] & 0x80
|
|
40
|
+
sig[63] &= 0x7F
|
|
41
|
+
try:
|
|
42
|
+
ed_public = montgomery_to_edwards_public(curve_public_key, sign_bit)
|
|
43
|
+
return Ed25519.verify(message, bytes(sig), ed_public)
|
|
44
|
+
except ValueError:
|
|
45
|
+
return False
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def xeddsa_sign(private_key: bytes | bytearray, message: bytes | bytearray) -> bytes:
|
|
49
|
+
"""Sign with a 32-byte X25519 private key. Randomized (64 fresh bytes)."""
|
|
50
|
+
raw = bytes(private_key)
|
|
51
|
+
assert_byte_length(raw, 32, f"invalid curve25519 private key length {len(raw)}")
|
|
52
|
+
|
|
53
|
+
# DEVIATION: TS clamps the caller's buffer in place (observable aliasing
|
|
54
|
+
# side effect). Python ``bytes`` are immutable, so we clamp a copy; the
|
|
55
|
+
# derived scalar and signature bytes are identical.
|
|
56
|
+
clamped = clamp_curve_private_key_in_place(bytearray(raw))
|
|
57
|
+
private_scalar = bytes_to_big_int_le(bytes(clamped))
|
|
58
|
+
encoded_public = encode_extended_point(scalar_mult_base(private_scalar))
|
|
59
|
+
pub_key_sign_bit = encoded_public[31] & 0x80
|
|
60
|
+
|
|
61
|
+
msg = bytes(message)
|
|
62
|
+
random_suffix = random_bytes(64)
|
|
63
|
+
pre_hash = sha512([_PREFIX_SIGNATURE_RANDOM, bytes(clamped), msg, random_suffix])
|
|
64
|
+
r = mod_group(bytes_to_big_int_le(pre_hash))
|
|
65
|
+
encoded_r = encode_extended_point(scalar_mult_base(r))
|
|
66
|
+
|
|
67
|
+
h = mod_group(bytes_to_big_int_le(sha512([encoded_r, encoded_public, msg])))
|
|
68
|
+
s = mod_group(r + h * private_scalar)
|
|
69
|
+
|
|
70
|
+
encoded_s = bytearray(big_int_to_bytes_le(s, 32))
|
|
71
|
+
encoded_s[31] = (encoded_s[31] & 0x7F) | pub_key_sign_bit
|
|
72
|
+
return concat_bytes([encoded_r, bytes(encoded_s)])
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""DER prefixes for native key import (copied verbatim from the oracle)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from zapy._util.bytes import hex_to_bytes
|
|
6
|
+
|
|
7
|
+
X25519_PKCS8_PREFIX: bytes = hex_to_bytes("302e020100300506032b656e04220420")
|
|
8
|
+
X25519_SPKI_PREFIX: bytes = hex_to_bytes("302a300506032b656e032100")
|
|
9
|
+
ED25519_PKCS8_PREFIX: bytes = hex_to_bytes("302e020100300506032b657004220420")
|
|
10
|
+
ED25519_SPKI_PREFIX: bytes = hex_to_bytes("302a300506032b6570032100")
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Ed25519 sign/verify backed by ``cryptography`` (sync, like the TS native path)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
from cryptography.exceptions import InvalidKey, InvalidSignature
|
|
8
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
|
|
9
|
+
Ed25519PrivateKey,
|
|
10
|
+
Ed25519PublicKey,
|
|
11
|
+
)
|
|
12
|
+
from cryptography.hazmat.primitives.serialization import (
|
|
13
|
+
Encoding,
|
|
14
|
+
NoEncryption,
|
|
15
|
+
PrivateFormat,
|
|
16
|
+
PublicFormat,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
from zapy._crypto.curves.types import SignalKeyPair
|
|
20
|
+
from zapy._util.bytes import assert_byte_length
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger("zapy._crypto.ed25519")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Ed25519:
|
|
26
|
+
"""Ed25519 sign/verify. All operations are synchronous (fast, no thread pool)."""
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def generate_key_pair() -> SignalKeyPair:
|
|
30
|
+
"""Generate a fresh Ed25519 key pair."""
|
|
31
|
+
private = Ed25519PrivateKey.generate()
|
|
32
|
+
public = private.public_key()
|
|
33
|
+
return SignalKeyPair(
|
|
34
|
+
pub_key=public.public_bytes(Encoding.Raw, PublicFormat.Raw),
|
|
35
|
+
priv_key=private.private_bytes(
|
|
36
|
+
Encoding.Raw, PrivateFormat.Raw, NoEncryption()
|
|
37
|
+
),
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def sign(message: bytes | bytearray, priv_key: bytes | bytearray) -> bytes:
|
|
42
|
+
"""Sign ``message`` with a 32-byte private key. Returns a 64-byte signature."""
|
|
43
|
+
raw = bytes(priv_key)
|
|
44
|
+
assert_byte_length(raw, 32, "ed25519 private key must be 32 bytes")
|
|
45
|
+
return Ed25519PrivateKey.from_private_bytes(raw).sign(bytes(message))
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def verify(
|
|
49
|
+
message: bytes | bytearray,
|
|
50
|
+
signature: bytes | bytearray,
|
|
51
|
+
pub_key: bytes | bytearray,
|
|
52
|
+
) -> bool:
|
|
53
|
+
"""Verify a signature. Returns ``False`` instead of throwing on failure."""
|
|
54
|
+
public_raw = bytes(pub_key)
|
|
55
|
+
assert_byte_length(public_raw, 32, "ed25519 public key must be 32 bytes")
|
|
56
|
+
try:
|
|
57
|
+
public = Ed25519PublicKey.from_public_bytes(public_raw)
|
|
58
|
+
public.verify(bytes(signature), bytes(message))
|
|
59
|
+
except (InvalidSignature, InvalidKey, ValueError):
|
|
60
|
+
return False
|
|
61
|
+
return True
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Key-pair container and PKCS#8 helpers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True, slots=True)
|
|
9
|
+
class SignalKeyPair:
|
|
10
|
+
"""An X25519/Ed25519 key pair: 32-byte public key, 32-byte private key."""
|
|
11
|
+
|
|
12
|
+
pub_key: bytes
|
|
13
|
+
priv_key: bytes
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def pkcs8_from_raw_private(prefix: bytes | bytearray, raw: bytes | bytearray) -> bytes:
|
|
17
|
+
"""Concatenate a DER PKCS#8 prefix with the raw private key."""
|
|
18
|
+
return bytes(prefix) + bytes(raw)
|