beaconcrypt 0.4.0__tar.gz → 0.4.2__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.
Files changed (68) hide show
  1. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.github/workflows/release.yml +16 -6
  2. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/Cargo.lock +1 -1
  3. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/Cargo.toml +1 -1
  4. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/PKG-INFO +1 -1
  5. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/beaconcrypt.go +0 -18
  6. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/beaconcrypt.pyi +0 -2
  7. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/beaconcrypt_test.go +4 -19
  8. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/bindings.h +6 -9
  9. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/doc/multi-opening-fixture.md +30 -29
  10. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/doc/persistence.md +1 -6
  11. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/doc/protocol.md +26 -1
  12. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/c/main.c +0 -2
  13. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/python/pyproject.toml +0 -2
  14. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/scripts/derive_multi_opening.py +50 -25
  15. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/scripts/generate_kat_vectors.go +12 -8
  16. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/scripts/generate_kat_vectors.py +12 -8
  17. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/cbinds.rs +0 -11
  18. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/pqxdh.rs +0 -2
  19. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/pybinds.rs +0 -11
  20. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/server.rs +0 -2
  21. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/shared.rs +60 -30
  22. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/tests/protocol.rs +1 -4
  23. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/tests/test_encryption.py +2 -4
  24. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/tests/test_registration.py +1 -1
  25. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/tests/test_server_state.py +1 -3
  26. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.cargo/config.toml +0 -0
  27. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.github/workflows/codeql.yml +0 -0
  28. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.github/workflows/go.yml +0 -0
  29. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.github/workflows/python.yml +0 -0
  30. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.github/workflows/rust.yml +0 -0
  31. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.gitignore +0 -0
  32. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/.gitmodules +0 -0
  33. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/LICENSE +0 -0
  34. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/README.md +0 -0
  35. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/build.rs +0 -0
  36. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/clippy.toml +0 -0
  37. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/doc/rationale.md +0 -0
  38. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/doc/threat_model.md +0 -0
  39. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/doc/wycheproof.md +0 -0
  40. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/c/CMakeLists.txt +0 -0
  41. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/c/README.md +0 -0
  42. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/go/.gitignore +0 -0
  43. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/go/README.md +0 -0
  44. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/go/go.mod +0 -0
  45. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/go/go.sum +0 -0
  46. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/go/main.go +0 -0
  47. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/python/.gitignore +0 -0
  48. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/python/README.md +0 -0
  49. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/python/main.py +0 -0
  50. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/rust/README.md +0 -0
  51. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/examples/rust/main.rs +0 -0
  52. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/go.mod +0 -0
  53. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/go.sum +0 -0
  54. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/pyproject.toml +0 -0
  55. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/rustfmt.toml +0 -0
  56. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/scripts/generate_python_stubs.py +0 -0
  57. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/beacon.rs +0 -0
  58. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/deser.rs +0 -0
  59. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/error.rs +0 -0
  60. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/lib.rs +0 -0
  61. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/schema/cryptoframe.capnp +0 -0
  62. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/schema/phase1.capnp +0 -0
  63. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/schema/phase2.capnp +0 -0
  64. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/src/ser.rs +0 -0
  65. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/tests/beacon.rs +0 -0
  66. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/tests/server.rs +0 -0
  67. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/tests/wycheproof.rs +0 -0
  68. {beaconcrypt-0.4.0 → beaconcrypt-0.4.2}/uv.lock +0 -0
@@ -5,6 +5,7 @@ on:
5
5
 
6
6
  env:
7
7
  CARGO_TERM_COLOR: always
8
+ CAPNP_VERSION: 1.5.0
8
9
 
9
10
  jobs:
10
11
  unix-build:
@@ -19,14 +20,23 @@ jobs:
19
20
  - uses: actions/checkout@v7
20
21
  with:
21
22
  submodules: recursive
22
- - name: Install cap'n proto
23
+ - name: Restore cached cap'n proto
24
+ id: cache-capnproto
25
+ uses: actions/cache@v6
26
+ with:
27
+ path: ~/.local/capnproto
28
+ key: capnproto-build-v1-${{ runner.os }}-${{ runner.arch }}-${{ env.CAPNP_VERSION }}
29
+ - name: Build cap'n proto
30
+ if: steps.cache-capnproto.outputs.cache-hit != 'true'
23
31
  run: |
24
- curl -O https://capnproto.org/capnproto-c++-1.5.0.tar.gz
25
- tar zxf capnproto-c++-1.5.0.tar.gz
26
- cd capnproto-c++-1.5.0
27
- ./configure
32
+ curl --fail --location --output "capnproto-c++-${CAPNP_VERSION}.tar.gz" "https://capnproto.org/capnproto-c++-${CAPNP_VERSION}.tar.gz"
33
+ tar zxf "capnproto-c++-${CAPNP_VERSION}.tar.gz"
34
+ cd "capnproto-c++-${CAPNP_VERSION}"
35
+ ./configure --prefix="$HOME/.local/capnproto"
28
36
  make -j4 check
29
- sudo make install
37
+ make install
38
+ - name: Add cap'n proto to PATH
39
+ run: echo "$HOME/.local/capnproto/bin" >> "$GITHUB_PATH"
30
40
  - name: Build
31
41
  run: |
32
42
  rustup component add rust-src --toolchain nightly
@@ -78,7 +78,7 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
78
78
 
79
79
  [[package]]
80
80
  name = "beaconcrypt"
81
- version = "0.4.0"
81
+ version = "0.4.2"
82
82
  dependencies = [
83
83
  "capnp",
84
84
  "capnpc",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "beaconcrypt"
3
- version = "0.4.0"
3
+ version = "0.4.2"
4
4
  edition = "2024"
5
5
  license-file = "LICENSE"
6
6
  readme = "README.md"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: beaconcrypt
3
- Version: 0.4.0
3
+ Version: 0.4.2
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -17,13 +17,11 @@ typedef struct {
17
17
 
18
18
  typedef struct {
19
19
  beaconcrypt_buffer response;
20
- beaconcrypt_buffer beacon_pk;
21
20
  uint64_t key_id;
22
21
  } beaconcrypt_registration_response;
23
22
 
24
23
  typedef struct {
25
24
  beaconcrypt_buffer data;
26
- beaconcrypt_buffer key;
27
25
  beaconcrypt_buffer state;
28
26
  uint64_t key_id;
29
27
  uint64_t seq;
@@ -84,14 +82,11 @@ type Beacon struct {
84
82
 
85
83
  type RegistrationResponse struct {
86
84
  Serialized []byte
87
- BeaconPK []byte
88
85
  KeyID uint64
89
86
  }
90
87
 
91
88
  type EncryptState struct {
92
89
  Data []byte
93
- // Key is the current directional KDF state.
94
- Key []byte
95
90
  // State is the complete ratchet state serialized as JSON.
96
91
  State string
97
92
  KeyID uint64
@@ -210,17 +205,11 @@ func (s *Server) RegisterBeacon(registration, initialMessage []byte) (*Registrat
210
205
  C.uintptr_t(len(initialMessage)),
211
206
  )
212
207
  serialized, err := copyBuffer(response.response)
213
- if err != nil {
214
- C.beaconcrypt_free_buffer(response.beacon_pk)
215
- return nil, err
216
- }
217
- beaconPK, err := copyBuffer(response.beacon_pk)
218
208
  if err != nil {
219
209
  return nil, err
220
210
  }
221
211
  return &RegistrationResponse{
222
212
  Serialized: serialized,
223
- BeaconPK: beaconPK,
224
213
  KeyID: uint64(response.key_id),
225
214
  }, nil
226
215
  })
@@ -400,12 +389,6 @@ func callStateUpdate(data []byte, call func(*C.uint8_t, C.uintptr_t) C.beaconcry
400
389
  defer free()
401
390
  state := call(ptr, C.uintptr_t(len(data)))
402
391
  output, err := copyBuffer(state.data)
403
- if err != nil {
404
- C.beaconcrypt_free_buffer(state.key)
405
- C.beaconcrypt_free_buffer(state.state)
406
- return nil, err
407
- }
408
- key, err := copyBuffer(state.key)
409
392
  if err != nil {
410
393
  C.beaconcrypt_free_buffer(state.state)
411
394
  return nil, err
@@ -416,7 +399,6 @@ func callStateUpdate(data []byte, call func(*C.uint8_t, C.uintptr_t) C.beaconcry
416
399
  }
417
400
  return &EncryptState{
418
401
  Data: output,
419
- Key: key,
420
402
  State: string(serializedState),
421
403
  KeyID: uint64(state.key_id),
422
404
  Seq: uint64(state.seq),
@@ -34,7 +34,6 @@ class BeaconCryptServer:
34
34
  @final
35
35
  class EncryptState:
36
36
  def data(self, /) -> bytes: ...
37
- def key(self, /) -> bytes: ...
38
37
  def key_id(self, /) -> int: ...
39
38
  def seq(self, /) -> int: ...
40
39
  def state(self, /) -> str:
@@ -44,6 +43,5 @@ class EncryptState:
44
43
 
45
44
  @final
46
45
  class RegResponse:
47
- def beacon_pk(self, /) -> bytes: ...
48
46
  def key_id(self, /) -> int: ...
49
47
  def serialized(self, /) -> bytes: ...
@@ -162,9 +162,6 @@ func registerBeaconWithInitial(t *testing.T, server *Server, beacon *Beacon, mes
162
162
  if regOut.KeyID == 0 {
163
163
  t.Fatal("expected non-zero beacon key id")
164
164
  }
165
- if len(regOut.BeaconPK) != 32 {
166
- t.Fatalf("beacon public key length mismatch: got %d want 32", len(regOut.BeaconPK))
167
- }
168
165
  return regOut
169
166
  }
170
167
 
@@ -599,9 +596,6 @@ func TestServerEncryptAndUpdateReturnsRatchetState(t *testing.T) {
599
596
  if update.Seq != 2 {
600
597
  t.Fatalf("state sequence mismatch: got %d want 2", update.Seq)
601
598
  }
602
- if len(update.Key) != 32 {
603
- t.Fatalf("state key length mismatch: got %d want 32", len(update.Key))
604
- }
605
599
  system, role, key := decodeJSONRatchetKey(t, json.RawMessage(update.State), "send_key")
606
600
  if system != kdfStateSystemID || role != chainSendKeyRole {
607
601
  t.Fatalf(
@@ -612,8 +606,8 @@ func TestServerEncryptAndUpdateReturnsRatchetState(t *testing.T) {
612
606
  chainSendKeyRole,
613
607
  )
614
608
  }
615
- if !bytes.Equal(update.Key, key) {
616
- t.Fatalf("directional key does not match complete state: got %x want %x", update.Key, key)
609
+ if len(key) != ratchetStateBytes {
610
+ t.Fatalf("state key length mismatch: got %d want %d", len(key), ratchetStateBytes)
617
611
  }
618
612
  plaintext, err := beacon.DecryptServerMessage(update.Data)
619
613
  if err != nil {
@@ -648,9 +642,6 @@ func TestServerDecryptAndUpdateReturnsRatchetState(t *testing.T) {
648
642
  if update.Seq != 1 {
649
643
  t.Fatalf("state sequence mismatch: got %d want 1", update.Seq)
650
644
  }
651
- if len(update.Key) != 32 {
652
- t.Fatalf("state key length mismatch: got %d want 32", len(update.Key))
653
- }
654
645
  system, role, key := decodeJSONRatchetKey(t, json.RawMessage(update.State), "recv_key")
655
646
  if system != kdfStateSystemID || role != chainReceiveRole {
656
647
  t.Fatalf(
@@ -661,8 +652,8 @@ func TestServerDecryptAndUpdateReturnsRatchetState(t *testing.T) {
661
652
  chainReceiveRole,
662
653
  )
663
654
  }
664
- if !bytes.Equal(update.Key, key) {
665
- t.Fatalf("directional key does not match complete state: got %x want %x", update.Key, key)
655
+ if len(key) != ratchetStateBytes {
656
+ t.Fatalf("state key length mismatch: got %d want %d", len(key), ratchetStateBytes)
666
657
  }
667
658
  if !bytes.Equal(update.Data, message) {
668
659
  t.Fatalf("beacon-to-server decrypt mismatch: got %q want %q", update.Data, message)
@@ -720,9 +711,6 @@ func TestStructuredAndJSONUpdatesMatchAcrossTheCGoBoundary(t *testing.T) {
720
711
  if !bytes.Equal(structuredSend.Data, jsonSend.Data) {
721
712
  t.Fatalf("send update data mismatch: got %x want %x", structuredSend.Data, jsonSend.Data)
722
713
  }
723
- if !bytes.Equal(structuredSend.Key, jsonSend.Key) {
724
- t.Fatalf("send update key mismatch: got %x want %x", structuredSend.Key, jsonSend.Key)
725
- }
726
714
  assertJSONEqual(t, structuredSend.State, string(jsonSend.State))
727
715
 
728
716
  inbound := []byte("compare structured and JSON decryption updates")
@@ -751,9 +739,6 @@ func TestStructuredAndJSONUpdatesMatchAcrossTheCGoBoundary(t *testing.T) {
751
739
  if !bytes.Equal(structuredRecv.Data, jsonRecv.Data) {
752
740
  t.Fatalf("receive update data mismatch: got %x want %x", structuredRecv.Data, jsonRecv.Data)
753
741
  }
754
- if !bytes.Equal(structuredRecv.Key, jsonRecv.Key) {
755
- t.Fatalf("receive update key mismatch: got %x want %x", structuredRecv.Key, jsonRecv.Key)
756
- }
757
742
  assertJSONEqual(t, structuredRecv.State, string(jsonRecv.State))
758
743
  }
759
744
 
@@ -24,6 +24,11 @@
24
24
  */
25
25
  #define beaconcrypt_AEAD_NONCE_LEN 12
26
26
 
27
+ /**
28
+ * crypto_aead::chacha20poly1305_ietf::ABYTES
29
+ */
30
+ #define beaconcrypt_AEAD_TAG_LEN 16
31
+
27
32
  #define beaconcrypt_KDF_RATCHET_OUTPUT_LEN ((beaconcrypt_AEAD_KEY_LEN + beaconcrypt_KDF_STATE_SIZE) + beaconcrypt_AEAD_NONCE_LEN)
28
33
 
29
34
  /**
@@ -37,10 +42,7 @@
37
42
 
38
43
  #define beaconcrypt_COMMITMENT_SIZE 64
39
44
 
40
- /**
41
- * crypto_aead::chacha20poly1305_ietf::ABYTES
42
- */
43
- #define beaconcrypt_MESSAGE_OVERHEAD (beaconcrypt_COMMITMENT_SIZE + 16)
45
+ #define beaconcrypt_MESSAGE_OVERHEAD (beaconcrypt_COMMITMENT_SIZE + beaconcrypt_AEAD_TAG_LEN)
44
46
 
45
47
  typedef struct beaconcrypt_BeaconCryptPqxdh beaconcrypt_BeaconCryptPqxdh;
46
48
 
@@ -52,16 +54,11 @@ typedef struct beaconcrypt_Buffer {
52
54
 
53
55
  typedef struct beaconcrypt_RegistrationResponse {
54
56
  struct beaconcrypt_Buffer response;
55
- struct beaconcrypt_Buffer beacon_pk;
56
57
  uint64_t key_id;
57
58
  } beaconcrypt_RegistrationResponse;
58
59
 
59
60
  typedef struct beaconcrypt_EncryptState {
60
61
  struct beaconcrypt_Buffer data;
61
- /**
62
- * Current directional KDF state, retained as a convenience value.
63
- */
64
- struct beaconcrypt_Buffer key;
65
62
  /**
66
63
  * Complete RatchetManager serialized as JSON.
67
64
  */
@@ -28,7 +28,7 @@ The construction begins with deterministic, non-secret values:
28
28
  | --- | --- |
29
29
  | `K1` | `000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f` |
30
30
  | `N` | `000102030405060708090a0b` |
31
- | `A1` | `f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff` |
31
+ | `A1` | The 153-byte sequence `00 01 ... 98` |
32
32
  | `C` | `00112233445566778899aabbccddeeff` |
33
33
 
34
34
  The nonce is shared across different keys, which does not violate
@@ -56,23 +56,25 @@ block `B`, define:
56
56
  m(B) = LE128(B) + 2^128
57
57
  ```
58
58
 
59
- Both associated data and ciphertext are exactly 16 bytes, so neither needs
60
- padding. The third block is the RFC 8439 length block:
59
+ The 153-byte associated data is padded with seven zero bytes and encoded as ten
60
+ Poly1305 blocks. The 16-byte ciphertext contributes one block, followed by the
61
+ RFC 8439 length block:
61
62
 
62
63
  ```text
63
64
  L = LE64(len(A)) || LE64(len(C))
64
- = LE64(16) || LE64(16)
65
+ = LE64(153) || LE64(16)
65
66
  ```
66
67
 
67
- The Poly1305 accumulator for these three blocks is therefore:
68
+ Call these twelve blocks `B0 ... B11`, where `B0 ... B9` encode the padded
69
+ associated data, `B10` is the ciphertext, and `B11` is `L`. The accumulator is:
68
70
 
69
71
  ```text
70
- acc = (m(A) * r^3 + m(C) * r^2 + m(L) * r) mod p
72
+ acc = sum(m(Bi) * r^(12-i), i=0..11) mod p
71
73
  T = LE128((acc + s) mod 2^128)
72
74
  ```
73
75
 
74
- This form makes the associated-data block a single unknown in a linear
75
- equation modulo `p`.
76
+ The construction keeps `B1 ... B11` fixed and solves `B0`, leaving 137 of the
77
+ 153 associated-data bytes unchanged.
76
78
 
77
79
  ## Constructing the second opening
78
80
 
@@ -97,31 +99,30 @@ target_acc(carry) = base + carry * 2^128
97
99
  Only candidates below `p` are possible, so checking `carry` values `0..3`
98
100
  exhausts the accumulator representatives.
99
101
 
100
- For each candidate accumulator, solve for the encoded associated-data block:
102
+ For each candidate accumulator, let `F` be the fixed contribution from blocks
103
+ `B1 ... B11` and solve for the first associated-data block:
101
104
 
102
105
  ```text
103
- m(A2) =
104
- (target_acc
105
- - m(C) * r2^2
106
- - m(L) * r2)
107
- * inverse(r2^3, p)
106
+ m(B0) =
107
+ (target_acc - F)
108
+ * inverse(r2^12, p)
108
109
  mod p
109
110
  ```
110
111
 
111
112
  A solution encodes one full 16-byte Poly1305 block exactly when:
112
113
 
113
114
  ```text
114
- 2^128 <= m(A2) < 2^129
115
+ 2^128 <= m(B0) < 2^129
115
116
  ```
116
117
 
117
- In that case:
118
+ In that case, the first 16 bytes of `A2` are:
118
119
 
119
120
  ```text
120
- A2 = LE128^-1(m(A2) - 2^128)
121
+ A2[0..16] = LE128^-1(m(B0) - 2^128)
121
122
  ```
122
123
 
123
- The first candidate key (`attempt = 1`) and first accumulator representative
124
- (`carry = 0`) satisfy this condition. Finally:
124
+ The first candidate key (`attempt = 1`) and third accumulator representative
125
+ (`carry = 2`) satisfy this condition. Finally:
125
126
 
126
127
  ```text
127
128
  P2 = C XOR ChaCha20_Stream(K2, N, counter=1)
@@ -134,22 +135,22 @@ rather than their little-endian byte encoding, are:
134
135
  | --- | --- |
135
136
  | `r2` after clamping | `0x0dfd7a580bc3b19401df45ac0f2d360c` |
136
137
  | `s2` | `0xc9bc67ea96e7232f597298455a365448` |
137
- | `target_acc(0)` | `0x4b760f693e2a23eb331c7a4b262a1340` |
138
- | `m(A2)` | `0x1036298f11d35130fa072f6dac3ee093a` |
138
+ | `target_acc(2)` | `0x24a4b9e9cc92bad92bd6360b1d13ac85a` |
139
+ | `m(B0)` | `0x1247bdf3560ca14b16e6e6f2fa48c2ed6` |
139
140
 
140
141
  ## Derived fixture
141
142
 
142
143
  | Name | Value |
143
144
  | --- | --- |
144
145
  | `attempt` | `1` |
145
- | `carry` | `0` |
146
+ | `carry` | `2` |
146
147
  | `K1` | `000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f` |
147
148
  | `K2` | `967712731b5091e4e42b5fa6241e3b02108fedc55c561d80af04c2095d3edbe7` |
148
149
  | `N` | `000102030405060708090a0b` |
149
- | `A1` | `f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff` |
150
- | `A2` | `3a09eec3daf672a00f13351df1986203` |
150
+ | `A1` | The 153-byte sequence `00 01 ... 98` |
151
+ | `A2` | `d62e8ca42f6f6e6eb114ca6035df7b24 || 10 11 ... 98` (153 bytes) |
151
152
  | `C` | `00112233445566778899aabbccddeeff` |
152
- | `T` | `8867608090128f8c1a4711d553773215` |
153
+ | `T` | `a21c712bf7f8d516c2d0126087060814` |
153
154
  | `P1` | `89ea2a336d42c3373f1a954854c0e09c` |
154
155
  | `P2` | `3c6ab3eb035de373e2b5d4a81a3cd13f` |
155
156
 
@@ -173,11 +174,11 @@ CTX = BLAKE2b-512(
173
174
  This produces:
174
175
 
175
176
  ```text
176
- CTX1 = 0573b9e328176e47de0251b211aa5347c72a61abf8e095bc7ac854982711f135
177
- 25c0741341ac59f7db41163fba77aadf8592df71b25a3b02099b6b4b00a3c403
177
+ CTX1 = 9cda090561a1140c1e7fdee457c9057be213b87a65e895078564be7fa13360df
178
+ fb48bab92db64d3800fd90ba2fc4d8c174add55cbbf0b0bff98eb74b32c1e06e
178
179
 
179
- CTX2 = 322268a07252f76c4e894cab1e124db622ecf299f5050ed23768dd79b9e804ad
180
- 22c48e36ff3b0e3e1c6984ee81d96c9d2900672298c6350d8413dbb49b5dcdd1
180
+ CTX2 = f092a14b1da49bad4c64f3bacc67480d7dd367f5ba90fa3f79492aea29cd7707
181
+ 49b0aefdc03fa3736dd11ee3c79038a4a4d10a64959042dd97007690a7506bb2
181
182
  ```
182
183
 
183
184
  This is the property under test: the base AEAD has two valid openings, while
@@ -66,12 +66,7 @@ fn encrypt_and_update_json(&mut self, bytes: &[u8], kid: u64) -> Option<String>;
66
66
  fn decrypt_and_update_json(&mut self, bytes: &[u8]) -> Option<String>;
67
67
  ```
68
68
 
69
- The Python and Go structured wrappers expose the complete `RatchetManager` as
70
- JSON through `EncryptState.state()` and `EncryptState.State`, respectively.
71
- Their existing `key()` and `Key` members remain available as directional
72
- convenience values and must equal the `send_key` or `recv_key` contained in the
73
- complete state. The binding test suites compare these structured values with
74
- the JSON interface from identical server snapshots.
69
+ The Python and Go wrappers expose the complete `RatchetManager` as JSON through `EncryptState.state()` and `EncryptState.State`, respectively.
75
70
 
76
71
  The JSON output looks like this:
77
72
  ```json
@@ -36,6 +36,31 @@ The beacon receives the initial message and uses the `phase2` bundle to perform
36
36
  # Message encryption
37
37
  Every time something is encrypted, it is wrapped in a Cap’n Proto message called a `CryptoFrame`. This embeds minor metadata that allows managing out-of order messages. Every `CryptoFrame` is encrypted under a distinct key, obtained from the principal's `send` KDF chain, which is then ratcheted forward. Nonces are also obtained from the KDF chain, because keys are only used once there is no possible reuse. Keys are deleted once they have been used, this is always immediate for keys on the `send` chains. Keys on the `receive` chains might need to be kept for longer in cases where multiple messages are recieved out of order, in which case the implementation needs to ratchet forward to the required sequence nmuber. All the intermediate keys need to be saved by the implementation, such that they can be used to decrypt the other messages when they are processed. This skipping must be constrained to some gap between the current known counter and the target, to avoid potential unbounded memory consumption. The encryption uses an AEAD scheme, so the plaintext is authenticated. The `CryptoFrame` metadata is bound to the message by the commitment described below: `seq` prevents replay and supports out-of-order delivery, while `keyId` identifies the sender and selects the corresponding receive ratchet.
38
38
 
39
+ ## Commitment
40
+ Beaconcrypt uses a commitment scheme based on [`CTX` by Chan and Rogaway](https://eprint.iacr.org/2022/1260). This intends to both provide protection against key substitution attacks and protect metadata. However, because of this additional metadata being included in the `CTX` transcript, it might be susceptible to canonicalization attacks. Therefore, it is critical that all elements included in the `CTX` transcript be of fixed size, such that no confusion is possible. For the reference `beaconcrypt` implementation these are:
41
+ ```
42
+ -----------------------------------------------------
43
+ | ChaCha20-Poly1305 IETF Key (K) - 32 bytes |
44
+ -----------------------------------------------------
45
+ | ChaCha20-Poly1305 IETF Nonce (N) - 12 bytes |
46
+ -----------------------------------------------------
47
+ | Beaconcrypt PQXDH associated data (A) - 153 bytes |
48
+ -----------------------------------------------------
49
+ | ChaCha20-Poly1305 IETF Tag (T) - 16 bytes |
50
+ -----------------------------------------------------
51
+ | Seq for K as little-endian (S) - 8 bytes |
52
+ -----------------------------------------------------
53
+ | Principal ID as little-endian (I) - 8 bytes |
54
+ -----------------------------------------------------
55
+ ```
56
+
57
+ The commitment itself, which is 64 bytes, is then computed using the following, where `H` is the unkeyed BLAKE2b hash function with 512 bits of output:
58
+ ```
59
+ H(K, N, A, T, S, I)
60
+ ```
61
+
62
+ A canonicalization scheme would have to be used to encode the various elements being included within the `CTX` transcript if the fixed-size assumption were to become false.
63
+
39
64
  # Protocol message
40
65
  ## CryptoFrame
41
66
  This is the most basic framing for an encrypted message within beaconcrypt. It is defined in [cryptoframe.capnp](../src/schema/cryptoframe.capnp). It carries a key identifier (`seq`), a key identifier `keyId`, and the encrypted data under `cipherText`. These messages are closely tied to the ratcheting mechanism. To create such a message, the writer must:
@@ -103,7 +128,7 @@ This message enables the beacon to obtain the elements it needs to derive the sh
103
128
  - Create the associated data byte string by concatenating the encoded server identity key, encoded beacon identity key and the PQXDH and symmetric ratchet protocol strings
104
129
  - Encrypt the first message if there is one, otherwise encrypt a single `0xFF` byte using a `CryptoFrame` and set `appCipherText` to its value
105
130
  - Delete the ephemeral key pair
106
- - Return the beacon's public key and key ID to the caller so it can register it as required
131
+ - Return the serialized registration response and key ID to the caller
107
132
 
108
133
  Upon reception, the beacon must process this message as follows:
109
134
  - Check that `identityKey` contains the same key as the compiled-in server key
@@ -32,7 +32,6 @@ static void
32
32
  free_registration_response(beaconcrypt_RegistrationResponse *response) {
33
33
  if (response != NULL) {
34
34
  free_buffer(&response->response);
35
- free_buffer(&response->beacon_pk);
36
35
  response->key_id = 0;
37
36
  }
38
37
  }
@@ -40,7 +39,6 @@ free_registration_response(beaconcrypt_RegistrationResponse *response) {
40
39
  static void free_encrypt_state(beaconcrypt_EncryptState *state) {
41
40
  if (state != NULL) {
42
41
  free_buffer(&state->data);
43
- free_buffer(&state->key);
44
42
  free_buffer(&state->state);
45
43
  state->key_id = 0;
46
44
  }
@@ -9,5 +9,3 @@ dependencies = [
9
9
  "pynacl>=1.6.2",
10
10
  ]
11
11
 
12
- [tool.uv.sources]
13
- beaconcrypt = { path = "../../target/wheels/beaconcrypt-0.4.0-cp311-abi3-win_amd64.whl" }
@@ -23,6 +23,7 @@ POLY1305_P = (1 << 130) - 5
23
23
  UINT128_MODULUS = 1 << 128
24
24
  UINT32_MASK = (1 << 32) - 1
25
25
  POLY1305_R_MASK = 0x0FFFFFFC0FFFFFFC0FFFFFFC0FFFFFFF
26
+ AD_SIZE = 153
26
27
 
27
28
 
28
29
  def rotate_left_32(value: int, shift: int) -> int:
@@ -97,33 +98,53 @@ def poly1305_block(block: bytes) -> int:
97
98
  def aead_tag(
98
99
  key: bytes, nonce: bytes, associated_data: bytes, ciphertext: bytes
99
100
  ) -> bytes:
100
- """Compute the RFC 8439 tag for the fixture's two full data blocks."""
101
+ """Compute the RFC 8439 AEAD tag."""
101
102
 
102
- assert len(associated_data) == 16
103
103
  assert len(ciphertext) == 16
104
104
  r, s = poly1305_parameters(key, nonce)
105
- lengths = len(associated_data).to_bytes(8, "little") + len(ciphertext).to_bytes(
106
- 8, "little"
105
+ padded_data = (
106
+ associated_data
107
+ + bytes(-len(associated_data) % 16)
108
+ + ciphertext
109
+ + bytes(-len(ciphertext) % 16)
110
+ + len(associated_data).to_bytes(8, "little")
111
+ + len(ciphertext).to_bytes(8, "little")
107
112
  )
108
- accumulator = (
109
- poly1305_block(associated_data) * pow(r, 3, POLY1305_P)
110
- + poly1305_block(ciphertext) * pow(r, 2, POLY1305_P)
111
- + poly1305_block(lengths) * r
112
- ) % POLY1305_P
113
+ accumulator = 0
114
+ for offset in range(0, len(padded_data), 16):
115
+ block = padded_data[offset : offset + 16]
116
+ accumulator = ((accumulator + poly1305_block(block)) * r) % POLY1305_P
113
117
  return ((accumulator + s) % UINT128_MODULUS).to_bytes(16, "little")
114
118
 
115
119
 
116
120
  def solve_associated_data(
117
- key: bytes, nonce: bytes, ciphertext: bytes, desired_tag: bytes
121
+ key: bytes,
122
+ nonce: bytes,
123
+ ciphertext: bytes,
124
+ desired_tag: bytes,
125
+ associated_data_template: bytes,
118
126
  ) -> tuple[int, bytes] | None:
119
127
  """Solve the first Poly1305 block for a second valid AEAD opening."""
120
128
 
129
+ assert len(associated_data_template) == AD_SIZE
121
130
  r, s = poly1305_parameters(key, nonce)
122
- lengths = (16).to_bytes(8, "little") + len(ciphertext).to_bytes(8, "little")
123
- fixed_terms = (
124
- poly1305_block(ciphertext) * pow(r, 2, POLY1305_P) + poly1305_block(lengths) * r
131
+ padded_data = (
132
+ associated_data_template
133
+ + bytes(-len(associated_data_template) % 16)
134
+ + ciphertext
135
+ + bytes(-len(ciphertext) % 16)
136
+ + len(associated_data_template).to_bytes(8, "little")
137
+ + len(ciphertext).to_bytes(8, "little")
125
138
  )
126
- inverse_r_cubed = pow(pow(r, 3, POLY1305_P), -1, POLY1305_P)
139
+ blocks = [
140
+ padded_data[offset : offset + 16] for offset in range(0, len(padded_data), 16)
141
+ ]
142
+ fixed_terms = sum(
143
+ poly1305_block(block) * pow(r, len(blocks) - index, POLY1305_P)
144
+ for index, block in enumerate(blocks)
145
+ if index != 0
146
+ )
147
+ inverse_unknown_coefficient = pow(pow(r, len(blocks), POLY1305_P), -1, POLY1305_P)
127
148
  tag_value = int.from_bytes(desired_tag, "little")
128
149
 
129
150
  # The final 128-bit tag discards the high bits of accumulator + s.
@@ -135,14 +156,15 @@ def solve_associated_data(
135
156
  if target_accumulator >= POLY1305_P:
136
157
  continue
137
158
  associated_data_block = (
138
- (target_accumulator - fixed_terms) * inverse_r_cubed
159
+ (target_accumulator - fixed_terms) * inverse_unknown_coefficient
139
160
  ) % POLY1305_P
140
161
  if UINT128_MODULUS <= associated_data_block < 2 * UINT128_MODULUS:
141
- associated_data = (associated_data_block - UINT128_MODULUS).to_bytes(
162
+ associated_data = bytearray(associated_data_template)
163
+ associated_data[:16] = (associated_data_block - UINT128_MODULUS).to_bytes(
142
164
  16, "little"
143
165
  )
144
- if aead_tag(key, nonce, associated_data, ciphertext) == desired_tag:
145
- return carry, associated_data
166
+ if aead_tag(key, nonce, bytes(associated_data), ciphertext) == desired_tag:
167
+ return carry, bytes(associated_data)
146
168
  return None
147
169
 
148
170
 
@@ -179,20 +201,22 @@ def print_integer(name: str, value: int) -> None:
179
201
  def main() -> None:
180
202
  key_one = bytes(range(32))
181
203
  nonce = bytes(range(12))
182
- associated_data_one = bytes(range(0xF0, 0x100))
204
+ associated_data_one = bytes(range(AD_SIZE))
183
205
  ciphertext = bytes.fromhex("00112233445566778899aabbccddeeff")
184
206
  plaintext_one = chacha20_xor(ciphertext, key_one, nonce)
185
207
  tag = aead_tag(key_one, nonce, associated_data_one, ciphertext)
186
208
 
187
209
  # Deterministically enumerate unrelated second keys until the Poly1305
188
- # equation has a solution that encodes as one full 16-byte AD block.
210
+ # equation has a solution that encodes as the first full AD block.
189
211
  attempt = 0
190
212
  while True:
191
213
  attempt += 1
192
214
  key_two = hashlib.sha256(
193
215
  b"beaconcrypt-ctx-fixture-" + attempt.to_bytes(4, "little")
194
216
  ).digest()
195
- solution = solve_associated_data(key_two, nonce, ciphertext, tag)
217
+ solution = solve_associated_data(
218
+ key_two, nonce, ciphertext, tag, associated_data_one
219
+ )
196
220
  if solution is not None:
197
221
  carry, associated_data_two = solution
198
222
  break
@@ -204,7 +228,7 @@ def main() -> None:
204
228
  target_accumulator = (
205
229
  (int.from_bytes(tag, "little") - s_two) % UINT128_MODULUS
206
230
  ) + carry * UINT128_MODULUS
207
- encoded_associated_data_two = poly1305_block(associated_data_two)
231
+ encoded_associated_data_two = poly1305_block(associated_data_two[:16])
208
232
  commitment_one = commitment(
209
233
  key_one, nonce, associated_data_one, tag, sequence=1, key_id=7
210
234
  )
@@ -215,11 +239,12 @@ def main() -> None:
215
239
 
216
240
  # Detect accidental drift from the checked-in Rust fixture.
217
241
  assert attempt == 1
218
- assert carry == 0
219
- assert associated_data_two.hex() == "3a09eec3daf672a00f13351df1986203"
242
+ assert carry == 2
243
+ assert associated_data_two[:16].hex() == "d62e8ca42f6f6e6eb114ca6035df7b24"
244
+ assert associated_data_two[16:] == associated_data_one[16:]
220
245
  assert plaintext_one.hex() == "89ea2a336d42c3373f1a954854c0e09c"
221
246
  assert plaintext_two.hex() == "3c6ab3eb035de373e2b5d4a81a3cd13f"
222
- assert tag.hex() == "8867608090128f8c1a4711d553773215"
247
+ assert tag.hex() == "a21c712bf7f8d516c2d0126087060814"
223
248
 
224
249
  print("[chacha20poly1305-multi-opening-derivation]")
225
250
  print_value("attempt", attempt)