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.
Files changed (345) hide show
  1. shellzapy-0.1.0.dist-info/METADATA +112 -0
  2. shellzapy-0.1.0.dist-info/RECORD +345 -0
  3. shellzapy-0.1.0.dist-info/WHEEL +4 -0
  4. shellzapy-0.1.0.dist-info/licenses/LICENSE +21 -0
  5. shellzapy-0.1.0.dist-info/licenses/NOTICE +9 -0
  6. zapy/__init__.py +58 -0
  7. zapy/_appstate/__init__.py +59 -0
  8. zapy/_appstate/constants.py +82 -0
  9. zapy/_appstate/crypto/__init__.py +17 -0
  10. zapy/_appstate/crypto/app_state_crypto.py +401 -0
  11. zapy/_appstate/parsers/__init__.py +23 -0
  12. zapy/_appstate/parsers/encoding.py +157 -0
  13. zapy/_appstate/parsers/response_parser.py +135 -0
  14. zapy/_appstate/sync/__init__.py +11 -0
  15. zapy/_appstate/sync/sync_client.py +1559 -0
  16. zapy/_appstate/types.py +137 -0
  17. zapy/_appstate/utils.py +100 -0
  18. zapy/_auth/__init__.py +3 -0
  19. zapy/_auth/auth_client.py +618 -0
  20. zapy/_auth/credentials_flow.py +290 -0
  21. zapy/_auth/pairing/__init__.py +3 -0
  22. zapy/_auth/pairing/companion_host.py +239 -0
  23. zapy/_auth/pairing/flow.py +808 -0
  24. zapy/_auth/pairing/pairing_code_crypto.py +143 -0
  25. zapy/_auth/pairing/qr_flow.py +123 -0
  26. zapy/_auth/pairing/shortcake_crypto.py +122 -0
  27. zapy/_auth/pairing/shortcake_flow.py +393 -0
  28. zapy/_auth/types.py +110 -0
  29. zapy/_client/__init__.py +26 -0
  30. zapy/_client/client.py +972 -0
  31. zapy/_client/connection/__init__.py +6 -0
  32. zapy/_client/connection/manager.py +525 -0
  33. zapy/_client/connection/receipt_queue.py +74 -0
  34. zapy/_client/coordinators/__init__.py +81 -0
  35. zapy/_client/coordinators/ab_props.py +260 -0
  36. zapy/_client/coordinators/app_state_mutation.py +2538 -0
  37. zapy/_client/coordinators/bot.py +830 -0
  38. zapy/_client/coordinators/broadcast_list.py +101 -0
  39. zapy/_client/coordinators/business.py +247 -0
  40. zapy/_client/coordinators/email.py +158 -0
  41. zapy/_client/coordinators/group.py +1478 -0
  42. zapy/_client/coordinators/incoming_node.py +758 -0
  43. zapy/_client/coordinators/low_level.py +119 -0
  44. zapy/_client/coordinators/message.py +1153 -0
  45. zapy/_client/coordinators/message_dispatch.py +1637 -0
  46. zapy/_client/coordinators/newsletter.py +110 -0
  47. zapy/_client/coordinators/offline_resume.py +271 -0
  48. zapy/_client/coordinators/passive_tasks.py +447 -0
  49. zapy/_client/coordinators/presence.py +103 -0
  50. zapy/_client/coordinators/privacy.py +623 -0
  51. zapy/_client/coordinators/profile.py +1024 -0
  52. zapy/_client/coordinators/retry.py +1878 -0
  53. zapy/_client/coordinators/status.py +132 -0
  54. zapy/_client/coordinators/stream_control.py +260 -0
  55. zapy/_client/coordinators/trusted_contact_token.py +417 -0
  56. zapy/_client/events/__init__.py +266 -0
  57. zapy/_client/events/abprops.py +58 -0
  58. zapy/_client/events/appstate_mutation.py +248 -0
  59. zapy/_client/events/business.py +565 -0
  60. zapy/_client/events/call.py +144 -0
  61. zapy/_client/events/chatstate.py +41 -0
  62. zapy/_client/events/devices.py +122 -0
  63. zapy/_client/events/dirty.py +384 -0
  64. zapy/_client/events/group.py +655 -0
  65. zapy/_client/events/identity.py +40 -0
  66. zapy/_client/events/incoming.py +1079 -0
  67. zapy/_client/events/mex_notification.py +317 -0
  68. zapy/_client/events/offline.py +123 -0
  69. zapy/_client/events/picture.py +67 -0
  70. zapy/_client/events/presence.py +74 -0
  71. zapy/_client/events/privacy.py +187 -0
  72. zapy/_client/events/privacy_token.py +72 -0
  73. zapy/_client/events/receipt.py +109 -0
  74. zapy/_client/events/registration.py +70 -0
  75. zapy/_client/events/username.py +76 -0
  76. zapy/_client/factory.py +2020 -0
  77. zapy/_client/media.py +829 -0
  78. zapy/_client/newsletter/__init__.py +90 -0
  79. zapy/_client/newsletter/admin.py +323 -0
  80. zapy/_client/newsletter/content.py +499 -0
  81. zapy/_client/newsletter/discovery.py +233 -0
  82. zapy/_client/newsletter/messaging.py +396 -0
  83. zapy/_client/newsletter/mex.py +99 -0
  84. zapy/_client/newsletter/parse.py +406 -0
  85. zapy/_client/newsletter/types.py +393 -0
  86. zapy/_client/persistence/__init__.py +89 -0
  87. zapy/_client/persistence/companion_host.py +133 -0
  88. zapy/_client/persistence/ephemeral_setting.py +245 -0
  89. zapy/_client/persistence/group_history.py +361 -0
  90. zapy/_client/persistence/history_blob.py +169 -0
  91. zapy/_client/persistence/history_sync.py +646 -0
  92. zapy/_client/persistence/mailbox.py +232 -0
  93. zapy/_client/persistence/write_behind_persistence.py +368 -0
  94. zapy/_client/plugins/__init__.py +23 -0
  95. zapy/_client/plugins/define.py +44 -0
  96. zapy/_client/plugins/install.py +254 -0
  97. zapy/_client/plugins/types.py +64 -0
  98. zapy/_client/tokens/__init__.py +21 -0
  99. zapy/_client/tokens/cs_token.py +47 -0
  100. zapy/_client/tokens/tc_token.py +43 -0
  101. zapy/_client/types.py +988 -0
  102. zapy/_crypto/__init__.py +65 -0
  103. zapy/_crypto/core/__init__.py +62 -0
  104. zapy/_crypto/core/hkdf.py +41 -0
  105. zapy/_crypto/core/keys.py +51 -0
  106. zapy/_crypto/core/nonce.py +25 -0
  107. zapy/_crypto/core/primitives.py +179 -0
  108. zapy/_crypto/core/random.py +32 -0
  109. zapy/_crypto/core/xeddsa.py +72 -0
  110. zapy/_crypto/curves/__init__.py +3 -0
  111. zapy/_crypto/curves/constants.py +10 -0
  112. zapy/_crypto/curves/ed25519.py +61 -0
  113. zapy/_crypto/curves/types.py +18 -0
  114. zapy/_crypto/curves/x25519.py +117 -0
  115. zapy/_crypto/math/__init__.py +3 -0
  116. zapy/_crypto/math/constants.py +35 -0
  117. zapy/_crypto/math/edwards.py +174 -0
  118. zapy/_crypto/math/fe.py +120 -0
  119. zapy/_crypto/math/le.py +13 -0
  120. zapy/_crypto/math/mod.py +15 -0
  121. zapy/_crypto/math/prime.py +11 -0
  122. zapy/_crypto/math/types.py +25 -0
  123. zapy/_crypto/native_backend.py +40 -0
  124. zapy/_infra/__init__.py +3 -0
  125. zapy/_infra/log.py +142 -0
  126. zapy/_infra/perf.py +77 -0
  127. zapy/_media/__init__.py +64 -0
  128. zapy/_media/constants.py +133 -0
  129. zapy/_media/container.py +110 -0
  130. zapy/_media/crypto/__init__.py +19 -0
  131. zapy/_media/crypto/crypto.py +633 -0
  132. zapy/_media/crypto/media_retry.py +107 -0
  133. zapy/_media/processor.py +100 -0
  134. zapy/_media/sticker/__init__.py +8 -0
  135. zapy/_media/sticker/pack.py +188 -0
  136. zapy/_media/transfer/__init__.py +15 -0
  137. zapy/_media/transfer/client.py +635 -0
  138. zapy/_media/transfer/conn.py +48 -0
  139. zapy/_media/types.py +169 -0
  140. zapy/_message/__init__.py +121 -0
  141. zapy/_message/addons/__init__.py +5 -0
  142. zapy/_message/addons/link_preview/__init__.py +54 -0
  143. zapy/_message/addons/link_preview/builder.py +56 -0
  144. zapy/_message/addons/link_preview/detect.py +72 -0
  145. zapy/_message/addons/link_preview/fetcher.py +553 -0
  146. zapy/_message/addons/link_preview/types.py +100 -0
  147. zapy/_message/client.py +486 -0
  148. zapy/_message/context_info.py +424 -0
  149. zapy/_message/crypto/__init__.py +71 -0
  150. zapy/_message/crypto/addon_crypto.py +656 -0
  151. zapy/_message/crypto/icdc.py +138 -0
  152. zapy/_message/crypto/phash.py +87 -0
  153. zapy/_message/crypto/reporting_token.py +498 -0
  154. zapy/_message/crypto/use_case_secret.py +113 -0
  155. zapy/_message/encode/__init__.py +84 -0
  156. zapy/_message/encode/content.py +636 -0
  157. zapy/_message/encode/device_sent.py +88 -0
  158. zapy/_message/encode/media_payload.py +122 -0
  159. zapy/_message/encode/padding.py +27 -0
  160. zapy/_message/kinds/__init__.py +1 -0
  161. zapy/_message/kinds/bot.py +179 -0
  162. zapy/_message/kinds/group_history.py +206 -0
  163. zapy/_message/kinds/newsletter.py +388 -0
  164. zapy/_message/kinds/sticker_pack.py +74 -0
  165. zapy/_message/messaging/__init__.py +336 -0
  166. zapy/_message/messaging/companion_host.py +117 -0
  167. zapy/_message/messaging/fanout.py +311 -0
  168. zapy/_message/messaging/group_metadata.py +444 -0
  169. zapy/_message/messaging/ignore_key.py +264 -0
  170. zapy/_message/messaging/key_protocol.py +217 -0
  171. zapy/_message/messaging/link_preview.py +448 -0
  172. zapy/_message/messaging/messages.py +1429 -0
  173. zapy/_message/primitives/__init__.py +1 -0
  174. zapy/_message/primitives/ack.py +232 -0
  175. zapy/_message/primitives/incoming.py +1141 -0
  176. zapy/_message/primitives/media_retry.py +451 -0
  177. zapy/_message/primitives/peer_data_operation.py +271 -0
  178. zapy/_message/types.py +434 -0
  179. zapy/_proto/WAProto_pb2.py +2584 -0
  180. zapy/_proto/__init__.py +1 -0
  181. zapy/_protocol/__init__.py +3 -0
  182. zapy/_protocol/abprops_spec.py +6216 -0
  183. zapy/_protocol/auth.py +17 -0
  184. zapy/_protocol/browser.py +57 -0
  185. zapy/_protocol/defaults.py +38 -0
  186. zapy/_protocol/device_identity.py +54 -0
  187. zapy/_protocol/jid.py +241 -0
  188. zapy/_protocol/message.py +127 -0
  189. zapy/_protocol/nodes.py +144 -0
  190. zapy/_protocol/notification.py +18 -0
  191. zapy/_retry/__init__.py +77 -0
  192. zapy/_retry/codec.py +209 -0
  193. zapy/_retry/constants.py +28 -0
  194. zapy/_retry/parse.py +132 -0
  195. zapy/_retry/reason.py +65 -0
  196. zapy/_retry/replay.py +483 -0
  197. zapy/_retry/tracker.py +161 -0
  198. zapy/_retry/types.py +122 -0
  199. zapy/_signal/__init__.py +171 -0
  200. zapy/_signal/api/__init__.py +133 -0
  201. zapy/_signal/api/codec.py +164 -0
  202. zapy/_signal/api/constants.py +14 -0
  203. zapy/_signal/api/device_sync.py +915 -0
  204. zapy/_signal/api/digest_sync.py +363 -0
  205. zapy/_signal/api/identity_sync.py +250 -0
  206. zapy/_signal/api/missing_pre_keys_sync.py +267 -0
  207. zapy/_signal/api/prekeys.py +191 -0
  208. zapy/_signal/api/result_map.py +40 -0
  209. zapy/_signal/api/rotate_key.py +111 -0
  210. zapy/_signal/api/session_sync.py +297 -0
  211. zapy/_signal/attestation.py +145 -0
  212. zapy/_signal/constants.py +22 -0
  213. zapy/_signal/encoding.py +78 -0
  214. zapy/_signal/group/__init__.py +35 -0
  215. zapy/_signal/group/chain.py +139 -0
  216. zapy/_signal/group/codec.py +78 -0
  217. zapy/_signal/group/encoding.py +166 -0
  218. zapy/_signal/group/manager.py +361 -0
  219. zapy/_signal/registration/__init__.py +44 -0
  220. zapy/_signal/registration/keygen.py +46 -0
  221. zapy/_signal/registration/reg_encoding.py +66 -0
  222. zapy/_signal/registration/reg_utils.py +52 -0
  223. zapy/_signal/session/__init__.py +114 -0
  224. zapy/_signal/session/encoding.py +385 -0
  225. zapy/_signal/session/protocol.py +590 -0
  226. zapy/_signal/session/ratchet.py +381 -0
  227. zapy/_signal/session/resolver.py +630 -0
  228. zapy/_signal/session/serializer.py +96 -0
  229. zapy/_signal/session/session.py +272 -0
  230. zapy/_signal/types.py +214 -0
  231. zapy/_store/__init__.py +118 -0
  232. zapy/_store/cache/__init__.py +166 -0
  233. zapy/_store/cache/identity.py +130 -0
  234. zapy/_store/cache/privacy_token.py +116 -0
  235. zapy/_store/cache/sender_key.py +168 -0
  236. zapy/_store/cache/session.py +142 -0
  237. zapy/_store/contracts/__init__.py +84 -0
  238. zapy/_store/contracts/appstate.py +82 -0
  239. zapy/_store/contracts/auth.py +16 -0
  240. zapy/_store/contracts/chat_metadata.py +29 -0
  241. zapy/_store/contracts/contact.py +31 -0
  242. zapy/_store/contracts/device_list.py +35 -0
  243. zapy/_store/contracts/group_metadata.py +33 -0
  244. zapy/_store/contracts/identity.py +18 -0
  245. zapy/_store/contracts/message.py +36 -0
  246. zapy/_store/contracts/message_secret.py +41 -0
  247. zapy/_store/contracts/pre_key.py +32 -0
  248. zapy/_store/contracts/privacy_token.py +32 -0
  249. zapy/_store/contracts/retry.py +47 -0
  250. zapy/_store/contracts/sender_key.py +37 -0
  251. zapy/_store/contracts/session.py +21 -0
  252. zapy/_store/contracts/signal.py +23 -0
  253. zapy/_store/contracts/thread.py +35 -0
  254. zapy/_store/create_store.py +890 -0
  255. zapy/_store/locks.py +1154 -0
  256. zapy/_store/memory/__init__.py +40 -0
  257. zapy/_store/memory/appstate.py +200 -0
  258. zapy/_store/memory/auth.py +24 -0
  259. zapy/_store/memory/chat_metadata.py +124 -0
  260. zapy/_store/memory/contact.py +125 -0
  261. zapy/_store/memory/device_list.py +161 -0
  262. zapy/_store/memory/group_metadata.py +125 -0
  263. zapy/_store/memory/identity.py +86 -0
  264. zapy/_store/memory/message.py +85 -0
  265. zapy/_store/memory/message_secret.py +145 -0
  266. zapy/_store/memory/pre_key.py +112 -0
  267. zapy/_store/memory/privacy_token.py +103 -0
  268. zapy/_store/memory/retry.py +250 -0
  269. zapy/_store/memory/sender_key.py +162 -0
  270. zapy/_store/memory/session.py +111 -0
  271. zapy/_store/memory/signal.py +45 -0
  272. zapy/_store/memory/thread.py +66 -0
  273. zapy/_store/noop.py +279 -0
  274. zapy/_transport/__init__.py +3 -0
  275. zapy/_transport/binary/__init__.py +77 -0
  276. zapy/_transport/binary/constants.py +80 -0
  277. zapy/_transport/binary/decoder.py +260 -0
  278. zapy/_transport/binary/encoder.py +279 -0
  279. zapy/_transport/binary/tokens.py +1289 -0
  280. zapy/_transport/comms.py +759 -0
  281. zapy/_transport/keepalive/__init__.py +3 -0
  282. zapy/_transport/keepalive/keep_alive.py +252 -0
  283. zapy/_transport/node/__init__.py +3 -0
  284. zapy/_transport/node/builders/__init__.py +9 -0
  285. zapy/_transport/node/builders/abprops.py +33 -0
  286. zapy/_transport/node/builders/account_sync.py +120 -0
  287. zapy/_transport/node/builders/bot.py +56 -0
  288. zapy/_transport/node/builders/business.py +261 -0
  289. zapy/_transport/node/builders/chatstate.py +74 -0
  290. zapy/_transport/node/builders/community.py +116 -0
  291. zapy/_transport/node/builders/device.py +30 -0
  292. zapy/_transport/node/builders/email.py +181 -0
  293. zapy/_transport/node/builders/global_.py +74 -0
  294. zapy/_transport/node/builders/group.py +279 -0
  295. zapy/_transport/node/builders/media.py +21 -0
  296. zapy/_transport/node/builders/message.py +436 -0
  297. zapy/_transport/node/builders/mobile.py +227 -0
  298. zapy/_transport/node/builders/newsletter.py +293 -0
  299. zapy/_transport/node/builders/offline.py +20 -0
  300. zapy/_transport/node/builders/pairing.py +130 -0
  301. zapy/_transport/node/builders/passive.py +28 -0
  302. zapy/_transport/node/builders/prekeys.py +215 -0
  303. zapy/_transport/node/builders/presence.py +100 -0
  304. zapy/_transport/node/builders/privacy.py +243 -0
  305. zapy/_transport/node/builders/privacy_token.py +94 -0
  306. zapy/_transport/node/builders/profile.py +176 -0
  307. zapy/_transport/node/builders/retry.py +156 -0
  308. zapy/_transport/node/builders/shortcake.py +87 -0
  309. zapy/_transport/node/builders/tos.py +92 -0
  310. zapy/_transport/node/builders/usync.py +185 -0
  311. zapy/_transport/node/helpers.py +201 -0
  312. zapy/_transport/node/orchestrator.py +210 -0
  313. zapy/_transport/node/query.py +82 -0
  314. zapy/_transport/node/transport.py +105 -0
  315. zapy/_transport/node/usync.py +33 -0
  316. zapy/_transport/node/xml.py +38 -0
  317. zapy/_transport/noise/__init__.py +7 -0
  318. zapy/_transport/noise/cert.py +85 -0
  319. zapy/_transport/noise/client_payload.py +344 -0
  320. zapy/_transport/noise/constants.py +12 -0
  321. zapy/_transport/noise/frame_codec.py +120 -0
  322. zapy/_transport/noise/handshake.py +92 -0
  323. zapy/_transport/noise/session.py +493 -0
  324. zapy/_transport/noise/socket.py +45 -0
  325. zapy/_transport/noise/types.py +66 -0
  326. zapy/_transport/noise/version.json +3 -0
  327. zapy/_transport/proxy.py +35 -0
  328. zapy/_transport/types.py +19 -0
  329. zapy/_transport/wa_version_fetcher.py +159 -0
  330. zapy/_transport/web_socket.py +602 -0
  331. zapy/_util/__init__.py +3 -0
  332. zapy/_util/bytes.py +131 -0
  333. zapy/_util/coercion.py +29 -0
  334. zapy/_util/collections.py +45 -0
  335. zapy/_util/primitives.py +49 -0
  336. zapy/client.py +840 -0
  337. zapy/errors.py +148 -0
  338. zapy/events.py +564 -0
  339. zapy/jid.py +140 -0
  340. zapy/py.typed +0 -0
  341. zapy/stores/__init__.py +50 -0
  342. zapy/stores/base.py +1627 -0
  343. zapy/stores/json_file.py +359 -0
  344. zapy/stores/memory.py +322 -0
  345. zapy/stores/sqlite.py +343 -0
@@ -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,3 @@
1
+ """Curve backends (mirrors ``src/crypto/curves/``)."""
2
+
3
+ __all__: list[str] = []
@@ -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)