quo-systems 0.1.0__tar.gz
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.
- quo_systems-0.1.0/.gitignore +5 -0
- quo_systems-0.1.0/.python-version +1 -0
- quo_systems-0.1.0/LICENSE +201 -0
- quo_systems-0.1.0/NOTICE +6 -0
- quo_systems-0.1.0/PKG-INFO +8 -0
- quo_systems-0.1.0/README.md +109 -0
- quo_systems-0.1.0/pyproject.toml +13 -0
- quo_systems-0.1.0/src/quo/__init__.py +11 -0
- quo_systems-0.1.0/src/quo/arithmetic.py +212 -0
- quo_systems-0.1.0/src/quo/call.py +99 -0
- quo_systems-0.1.0/src/quo/carriage.py +254 -0
- quo_systems-0.1.0/src/quo/envelope.py +191 -0
- quo_systems-0.1.0/src/quo/line.py +353 -0
- quo_systems-0.1.0/src/quo/notation.py +330 -0
- quo_systems-0.1.0/src/quo/warden.py +975 -0
- quo_systems-0.1.0/src/quo/wire.py +309 -0
- quo_systems-0.1.0/subject.py +686 -0
- quo_systems-0.1.0/tests/test_arithmetic.py +387 -0
- quo_systems-0.1.0/tests/test_carriage.py +257 -0
- quo_systems-0.1.0/tests/test_envelope.py +370 -0
- quo_systems-0.1.0/tests/test_judgment_over_every_road.py +402 -0
- quo_systems-0.1.0/tests/test_line.py +385 -0
- quo_systems-0.1.0/tests/test_notation.py +88 -0
- quo_systems-0.1.0/tests/test_roads.py +133 -0
- quo_systems-0.1.0/tests/test_warden.py +1239 -0
- quo_systems-0.1.0/tests/test_wire.py +243 -0
- quo_systems-0.1.0/uv.lock +177 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.13
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Razvan Gherghina
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
quo_systems-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# The Quo kit in Python
|
|
2
|
+
|
|
3
|
+
Written from [the constitution](../../constitution.md) alone and judged
|
|
4
|
+
against the pinned corpus in [`../js/vectors`](../js/vectors). Today it
|
|
5
|
+
carries a core of five modules and the two roads.
|
|
6
|
+
|
|
7
|
+
- **`quo.notation`** — Article IV. A blueprint's canonical text, its SHA-256
|
|
8
|
+
digest, and every text the notation refuses.
|
|
9
|
+
- **`quo.arithmetic`** — Article VI. Ed25519 signs, X25519 seals, SHA-256
|
|
10
|
+
commits, AES-256-GCM encrypts under a key derived through HKDF-SHA-256.
|
|
11
|
+
- **`quo.wire`** — Article V. The byte encoding of every closed type.
|
|
12
|
+
- **`quo.envelope`** — Article XI. The sealed box and the two records inside it.
|
|
13
|
+
- **`quo.warden`** — Articles IX and XII. The door, and the judgment in order.
|
|
14
|
+
|
|
15
|
+
And the roads, which are not the core:
|
|
16
|
+
|
|
17
|
+
- **`quo.carriage`** — Article III. The common carriage: one POST, bytes in
|
|
18
|
+
and bytes out. The only module that imports `http`.
|
|
19
|
+
- **`quo.line`** — Article III. Framed envelopes over one persistent TCP
|
|
20
|
+
connection. The only module that imports `socket`.
|
|
21
|
+
|
|
22
|
+
Importing `quo` gives the core and pulls in no host; a road is asked for by
|
|
23
|
+
name, and `tests/test_roads.py` asserts that rather than trusting a reader to
|
|
24
|
+
keep noticing it.
|
|
25
|
+
|
|
26
|
+
One third-party import, and it is the whole of it: PyCA
|
|
27
|
+
[`cryptography`](https://cryptography.io/), which carries Ed25519, X25519,
|
|
28
|
+
AES-256-GCM and HKDF. SHA-256 still comes from `hashlib`, because a primitive
|
|
29
|
+
the platform holds is taken from the platform. The core imports no host:
|
|
30
|
+
nothing from `socket`, `http` or `asyncio`.
|
|
31
|
+
|
|
32
|
+
## What it needs
|
|
33
|
+
|
|
34
|
+
Python 3.11 or newer. The kit is developed and tested on CPython 3.13, pinned
|
|
35
|
+
in `.python-version`, with the dependency resolved by the committed
|
|
36
|
+
`uv.lock`.
|
|
37
|
+
|
|
38
|
+
## The interface
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from quo import notation
|
|
42
|
+
|
|
43
|
+
notation.parse(text) # -> Blueprint, or raises NotationError
|
|
44
|
+
notation.render(blueprint) # -> the canonical text
|
|
45
|
+
notation.canonical(text) # -> the canonical bytes, UTF-8
|
|
46
|
+
notation.digest(text) # -> 32 bytes, the blueprint's identity
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
A text is canonical or it is refused. There is no repair: `parse` reads the
|
|
50
|
+
text, then writes the blueprint back and requires the two to be the same
|
|
51
|
+
bytes, so a trailing space or a second blank line is a refusal rather than
|
|
52
|
+
something quietly dropped.
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from quo import arithmetic
|
|
56
|
+
|
|
57
|
+
arithmetic.digest(data) # -> 32 bytes
|
|
58
|
+
arithmetic.commitment(warden, heir) # -> 32 bytes
|
|
59
|
+
arithmetic.signing_public(secret) # -> the Ed25519 pk of that seed
|
|
60
|
+
arithmetic.sign(secret, message) # -> 64 bytes
|
|
61
|
+
arithmetic.verify(voice, message, sig) # -> bool
|
|
62
|
+
arithmetic.sealing_public(secret) # -> the X25519 pk of that key
|
|
63
|
+
arithmetic.agree(secret, public) # -> 32 bytes, or ArithmeticError
|
|
64
|
+
arithmetic.derive(shared) # -> (32-byte key, 12-byte nonce)
|
|
65
|
+
arithmetic.seal(shared, additional, plain) # -> ciphertext then 16-byte tag
|
|
66
|
+
arithmetic.unseal(shared, additional, box) # -> plaintext, or ArithmeticError
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
A key is 32 bytes; anything else is refused before an algorithm sees it. No
|
|
70
|
+
function here mints a key — every draw of randomness is taken as an argument.
|
|
71
|
+
`verify` answers false rather than raising, because a signature that does not
|
|
72
|
+
stand is an ordinary answer; a seal that does not open is an
|
|
73
|
+
`ArithmeticError`. So is a dead agreement — one handing back thirty-two zero
|
|
74
|
+
bytes is refused at the point of agreement, said as this module's own refusal
|
|
75
|
+
rather than as whatever `cryptography` raised underneath.
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from quo import carriage, line
|
|
79
|
+
|
|
80
|
+
carriage.post(hint, message) # -> the sealed answer, or b"" for silence
|
|
81
|
+
carriage.Door(handle, limit=...) # one POST in, one body out
|
|
82
|
+
|
|
83
|
+
line.parse_hint("tcp://host:9000") # -> Road(host, port, cap, declared)
|
|
84
|
+
line.frame(envelope, cap) # -> the length, then the envelope bytes
|
|
85
|
+
line.dial(hint).send(envelope) # the dialing end publishes nothing
|
|
86
|
+
line.Listener(handle, cap=...) # the listening end, and its hint
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The default cap is 16,384 bytes, which a bare `tcp://` hint promises; a door
|
|
90
|
+
with a different appetite declares it with `?cap=` and a dialer reads it before
|
|
91
|
+
connecting. An over-cap send is refused in the sender's own kit. On a line
|
|
92
|
+
silence has no wire form — a refused ask puts no frame out — and only a framing
|
|
93
|
+
fault ends the connection, without a word.
|
|
94
|
+
|
|
95
|
+
## The tests
|
|
96
|
+
|
|
97
|
+
With [uv](https://docs.astral.sh/uv/), from this directory:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
uv run --frozen --python 3.13 python -m unittest discover -s tests -t tests -v
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Every vector in `../js/vectors/notation.json` is reproduced — six accepted
|
|
104
|
+
texts asserted on both their canonical bytes and their digest, and
|
|
105
|
+
twenty-eight refusals asserted as strictly as the acceptances. So is every
|
|
106
|
+
vector in `arithmetic.json`, all sixteen, with the case count asserted
|
|
107
|
+
against the file. `material.json` carries no vectors: instead every one of
|
|
108
|
+
its twenty keys is accounted for, each derived from its own secret or named
|
|
109
|
+
as a root the file does not derive.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "quo-systems"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Quo in Python: the protocol that answers by whose authority."
|
|
5
|
+
requires-python = ">=3.11"
|
|
6
|
+
dependencies = ["cryptography"]
|
|
7
|
+
|
|
8
|
+
[build-system]
|
|
9
|
+
requires = ["hatchling"]
|
|
10
|
+
build-backend = "hatchling.build"
|
|
11
|
+
|
|
12
|
+
[tool.hatch.build.targets.wheel]
|
|
13
|
+
packages = ["src/quo"]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""The Quo kit in Python. The core: the notation, the arithmetic, the wire, the
|
|
2
|
+
envelope and the warden.
|
|
3
|
+
|
|
4
|
+
The three roads — ``quo.carriage``, ``quo.line`` and ``quo.call``, which is
|
|
5
|
+
distance zero — are deliberately absent here. Importing this package pulls in
|
|
6
|
+
no host, so the core stays portable; a road is asked for by name.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from . import arithmetic, envelope, notation, warden, wire
|
|
10
|
+
|
|
11
|
+
__all__ = ["arithmetic", "envelope", "notation", "warden", "wire"]
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""The arithmetic: the four algorithms, named once and never negotiated.
|
|
2
|
+
|
|
3
|
+
Article VI of the constitution is the whole specification. Ed25519 signs,
|
|
4
|
+
X25519 seals, SHA-256 commits, AES-256-GCM encrypts under a key derived
|
|
5
|
+
through HKDF-SHA-256 with the fixed label ``quo-seal``. There is no suite
|
|
6
|
+
identifier and no negotiation.
|
|
7
|
+
|
|
8
|
+
Every draw of randomness is taken as an argument here, never reached for: no
|
|
9
|
+
function in this module mints a key of its own.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
from typing import Tuple
|
|
16
|
+
|
|
17
|
+
from cryptography.exceptions import InvalidSignature, InvalidTag
|
|
18
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
|
|
19
|
+
Ed25519PrivateKey,
|
|
20
|
+
Ed25519PublicKey,
|
|
21
|
+
)
|
|
22
|
+
from cryptography.hazmat.primitives.asymmetric.x25519 import (
|
|
23
|
+
X25519PrivateKey,
|
|
24
|
+
X25519PublicKey,
|
|
25
|
+
)
|
|
26
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
27
|
+
from cryptography.hazmat.primitives.hashes import SHA256
|
|
28
|
+
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
|
29
|
+
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"ArithmeticError",
|
|
33
|
+
"KEY_LENGTH",
|
|
34
|
+
"SIGNATURE_LENGTH",
|
|
35
|
+
"NONCE_LENGTH",
|
|
36
|
+
"TAG_LENGTH",
|
|
37
|
+
"SEAL_INFO",
|
|
38
|
+
"digest",
|
|
39
|
+
"commitment",
|
|
40
|
+
"signing_public",
|
|
41
|
+
"sign",
|
|
42
|
+
"small_order",
|
|
43
|
+
"verify",
|
|
44
|
+
"sealing_public",
|
|
45
|
+
"agree",
|
|
46
|
+
"derive",
|
|
47
|
+
"seal",
|
|
48
|
+
"unseal",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class ArithmeticError(ValueError):
|
|
53
|
+
"""An input the arithmetic refuses, or a seal that does not open."""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
KEY_LENGTH = 32
|
|
57
|
+
SIGNATURE_LENGTH = 64
|
|
58
|
+
NONCE_LENGTH = 12
|
|
59
|
+
TAG_LENGTH = 16
|
|
60
|
+
|
|
61
|
+
SEAL_INFO = b"quo-seal"
|
|
62
|
+
"""The fixed ASCII label. The label and the info are one constant, not two."""
|
|
63
|
+
|
|
64
|
+
_DRAWN = KEY_LENGTH + NONCE_LENGTH
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _key(name: str, value: bytes) -> bytes:
|
|
68
|
+
"""A key is 32 bytes. Anything else is refused before an algorithm sees it."""
|
|
69
|
+
if not isinstance(value, (bytes, bytearray)):
|
|
70
|
+
raise ArithmeticError(f"{name} must be bytes")
|
|
71
|
+
if len(value) != KEY_LENGTH:
|
|
72
|
+
raise ArithmeticError(f"{name} must be {KEY_LENGTH} bytes, got {len(value)}")
|
|
73
|
+
return bytes(value)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def digest(data: bytes) -> bytes:
|
|
77
|
+
"""SHA-256 commits."""
|
|
78
|
+
return hashlib.sha256(data).digest()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def commitment(warden: bytes, heir: bytes) -> bytes:
|
|
82
|
+
"""The pk of the warden the heir would spend at, then the heir's pk, hashed.
|
|
83
|
+
|
|
84
|
+
For a warden's own name that warden is itself.
|
|
85
|
+
"""
|
|
86
|
+
return digest(_key("warden", warden) + _key("heir", heir))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def signing_public(secret: bytes) -> bytes:
|
|
90
|
+
"""The Ed25519 public key of the pair minted from these thirty-two bytes.
|
|
91
|
+
|
|
92
|
+
The bytes are the seed as Ed25519 defines it; the public key follows by the
|
|
93
|
+
algorithm's own rules.
|
|
94
|
+
"""
|
|
95
|
+
private = Ed25519PrivateKey.from_private_bytes(_key("secret", secret))
|
|
96
|
+
return private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def sign(secret: bytes, message: bytes) -> bytes:
|
|
100
|
+
"""Sign a message under the pair minted from this seed."""
|
|
101
|
+
private = Ed25519PrivateKey.from_private_bytes(_key("secret", secret))
|
|
102
|
+
return private.sign(bytes(message))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# The small-order points of Curve25519's group, as Ed25519 public keys. A
|
|
106
|
+
# signature under one binds to no secret, so a voice wearing one is silence.
|
|
107
|
+
_SMALL_ORDER = frozenset(
|
|
108
|
+
bytes.fromhex(point)
|
|
109
|
+
for point in (
|
|
110
|
+
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
111
|
+
"ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
|
112
|
+
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
113
|
+
"0000000000000000000000000000000000000000000000000000000000000080",
|
|
114
|
+
"26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05",
|
|
115
|
+
"c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a",
|
|
116
|
+
"26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85",
|
|
117
|
+
"c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa",
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def small_order(pk: bytes) -> bool:
|
|
123
|
+
"""Whether this public key is one of the curve's small-order points."""
|
|
124
|
+
return bytes(pk) in _SMALL_ORDER
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def verify(voice: bytes, message: bytes, signature: bytes) -> bool:
|
|
128
|
+
"""Whether this signature stands under this voice over these bytes.
|
|
129
|
+
|
|
130
|
+
False rather than an exception: a signature that does not verify is an
|
|
131
|
+
ordinary answer, not a malformed input.
|
|
132
|
+
"""
|
|
133
|
+
try:
|
|
134
|
+
public = Ed25519PublicKey.from_public_bytes(_key("voice", voice))
|
|
135
|
+
except ArithmeticError:
|
|
136
|
+
raise
|
|
137
|
+
if len(signature) != SIGNATURE_LENGTH:
|
|
138
|
+
return False
|
|
139
|
+
if small_order(voice):
|
|
140
|
+
return False
|
|
141
|
+
try:
|
|
142
|
+
public.verify(bytes(signature), bytes(message))
|
|
143
|
+
except InvalidSignature:
|
|
144
|
+
return False
|
|
145
|
+
return True
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def sealing_public(secret: bytes) -> bytes:
|
|
149
|
+
"""The X25519 public key of the pair minted from these thirty-two bytes.
|
|
150
|
+
|
|
151
|
+
The bytes are the private key itself. Clamping happens inside the
|
|
152
|
+
algorithm; nothing is derived from a seed first.
|
|
153
|
+
"""
|
|
154
|
+
private = X25519PrivateKey.from_private_bytes(_key("secret", secret))
|
|
155
|
+
return private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def agree(secret: bytes, public: bytes) -> bytes:
|
|
159
|
+
"""The raw thirty-two-byte X25519 shared secret, exactly as it comes back.
|
|
160
|
+
|
|
161
|
+
**An agreement that hands back thirty-two zero bytes is refused at the
|
|
162
|
+
point of agreement**: the padlock was not a real key, and a seal derived
|
|
163
|
+
from it would protect nothing. ``cryptography`` refuses the degenerate
|
|
164
|
+
output itself, with a ``ValueError`` of its own that is not this module's
|
|
165
|
+
refusal — so it is caught here and said as one, and the zero check stands
|
|
166
|
+
behind it for a platform that would hand the bytes back instead.
|
|
167
|
+
"""
|
|
168
|
+
private = X25519PrivateKey.from_private_bytes(_key("secret", secret))
|
|
169
|
+
other = X25519PublicKey.from_public_bytes(_key("public", public))
|
|
170
|
+
try:
|
|
171
|
+
shared = private.exchange(other)
|
|
172
|
+
except ValueError as bad:
|
|
173
|
+
raise ArithmeticError("a dead agreement") from bad
|
|
174
|
+
if shared == b"\x00" * KEY_LENGTH:
|
|
175
|
+
raise ArithmeticError("a dead agreement")
|
|
176
|
+
return shared
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def derive(shared: bytes) -> Tuple[bytes, bytes]:
|
|
180
|
+
"""Draw forty-four bytes: thirty-two of key, then twelve of nonce.
|
|
181
|
+
|
|
182
|
+
HKDF-SHA-256 used whole, over the raw shared secret with nothing prepended
|
|
183
|
+
and nothing hashed first, under an empty salt — a salt of zero length,
|
|
184
|
+
not a run of zero bytes — and the fixed info ``quo-seal``.
|
|
185
|
+
"""
|
|
186
|
+
drawn = HKDF(
|
|
187
|
+
algorithm=SHA256(),
|
|
188
|
+
length=_DRAWN,
|
|
189
|
+
salt=b"",
|
|
190
|
+
info=SEAL_INFO,
|
|
191
|
+
).derive(_key("shared", shared))
|
|
192
|
+
return drawn[:KEY_LENGTH], drawn[KEY_LENGTH:]
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def seal(shared: bytes, additional: bytes, plaintext: bytes) -> bytes:
|
|
196
|
+
"""AES-256-GCM under the derived key and nonce: ciphertext first, tag after.
|
|
197
|
+
|
|
198
|
+
The additional authenticated data is the ephemeral public key.
|
|
199
|
+
"""
|
|
200
|
+
key, nonce = derive(shared)
|
|
201
|
+
return AESGCM(key).encrypt(nonce, bytes(plaintext), bytes(additional))
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def unseal(shared: bytes, additional: bytes, box: bytes) -> bytes:
|
|
205
|
+
"""Open a box, or refuse it. The last sixteen bytes are the tag."""
|
|
206
|
+
if len(box) < TAG_LENGTH:
|
|
207
|
+
raise ArithmeticError("a box carries at least the sixteen-byte tag")
|
|
208
|
+
key, nonce = derive(shared)
|
|
209
|
+
try:
|
|
210
|
+
return AESGCM(key).decrypt(nonce, bytes(box), bytes(additional))
|
|
211
|
+
except InvalidTag as failure:
|
|
212
|
+
raise ArithmeticError("the seal does not open") from failure
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Distance zero: the carriage is a call.
|
|
2
|
+
|
|
3
|
+
Article III names three carriages. This is the third: two houses in one device
|
|
4
|
+
or one process handing envelope bytes as bytes, a private carriage like any
|
|
5
|
+
other, needing no naming because no wire exists to disagree about. There is no
|
|
6
|
+
hint, because a hint is where to send bytes and here the door itself is the
|
|
7
|
+
address.
|
|
8
|
+
|
|
9
|
+
**Distance zero waives no step of the judgment.** The bytes handed over are the
|
|
10
|
+
same sealed, signed envelope the common carriage would have carried, and the
|
|
11
|
+
door behind this road spends every step of Article XII on them. The seal and
|
|
12
|
+
the signature are what make the two houses two; a ground that strips them for
|
|
13
|
+
being local has rebuilt the ambient permission the law exists to end. So this
|
|
14
|
+
module carries no shortcut of any kind: it is the carriage with the socket
|
|
15
|
+
removed and nothing else removed.
|
|
16
|
+
|
|
17
|
+
Within one house there is no carriage at all, because there are no strangers.
|
|
18
|
+
This road is for two houses that happen to share a process, never for a house
|
|
19
|
+
talking to itself.
|
|
20
|
+
|
|
21
|
+
This module is a road, not the core, and no core module imports it. Unlike the
|
|
22
|
+
other two it imports no host at all — there is nothing to import, which is the
|
|
23
|
+
whole of what distance zero means.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
from typing import Callable, Optional
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"CallError",
|
|
32
|
+
"DEFAULT_LIMIT",
|
|
33
|
+
"Door",
|
|
34
|
+
"post",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class CallError(Exception):
|
|
39
|
+
"""The road failed. Never a word about the message, which the seal owns."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
DEFAULT_LIMIT = 65536
|
|
43
|
+
|
|
44
|
+
#: What a caller may hand over. A handler answers bytes, or ``None`` for silence.
|
|
45
|
+
Handler = Callable[[bytes], Optional[bytes]]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Door:
|
|
49
|
+
"""A warden's door at distance zero: bytes in, bytes out, no wire between.
|
|
50
|
+
|
|
51
|
+
``limit`` is the largest message this door reads, which is the warden's own
|
|
52
|
+
published limit and nothing this road decided. A door that is not open
|
|
53
|
+
carries nothing, the same weather a closed socket is.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(self, handle: Handler, limit: int = DEFAULT_LIMIT) -> None:
|
|
57
|
+
self.handle = handle
|
|
58
|
+
self.limit = limit
|
|
59
|
+
self.open = False
|
|
60
|
+
|
|
61
|
+
def start(self) -> "Door":
|
|
62
|
+
self.open = True
|
|
63
|
+
return self
|
|
64
|
+
|
|
65
|
+
def close(self) -> None:
|
|
66
|
+
self.open = False
|
|
67
|
+
|
|
68
|
+
def __enter__(self) -> "Door":
|
|
69
|
+
return self.start()
|
|
70
|
+
|
|
71
|
+
def __exit__(self, *_exc) -> None:
|
|
72
|
+
self.close()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def post(door: Door, message: bytes) -> bytes:
|
|
76
|
+
"""Hand one message to the door. Returns the answer, empty for silence.
|
|
77
|
+
|
|
78
|
+
The bytes cross as bytes and nothing reads them on the way: no length is
|
|
79
|
+
negotiated, no framing is added, and the copy the door sees is its own, so
|
|
80
|
+
neither house can reach into the other's buffer after the call. A door that
|
|
81
|
+
is not open raises :class:`CallError`, which is weather rather than an
|
|
82
|
+
answer.
|
|
83
|
+
"""
|
|
84
|
+
if not isinstance(door, Door):
|
|
85
|
+
raise CallError("not a door at distance zero")
|
|
86
|
+
if not isinstance(message, (bytes, bytearray)):
|
|
87
|
+
raise CallError("not bytes")
|
|
88
|
+
if not door.open:
|
|
89
|
+
raise CallError("a door that is not open")
|
|
90
|
+
body = bytes(message)
|
|
91
|
+
if len(body) == 0 or len(body) > door.limit:
|
|
92
|
+
# A message the door would not have read off a socket is not read here
|
|
93
|
+
# either, and it meets the same silence.
|
|
94
|
+
return b""
|
|
95
|
+
try:
|
|
96
|
+
answer = door.handle(body)
|
|
97
|
+
except Exception: # a door never says which step it was
|
|
98
|
+
return b""
|
|
99
|
+
return bytes(answer) if answer else b""
|