beaconcrypt 0.4.0__tar.gz → 0.4.3__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 (71) hide show
  1. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/.github/workflows/release.yml +16 -6
  2. beaconcrypt-0.4.3/.gitmodules +6 -0
  3. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/Cargo.lock +1 -1
  4. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/Cargo.toml +11 -7
  5. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/PKG-INFO +1 -1
  6. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/README.md +16 -0
  7. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/beaconcrypt.go +2 -28
  8. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/beaconcrypt.pyi +1 -3
  9. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/beaconcrypt_test.go +111 -47
  10. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/bindings.h +7 -13
  11. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/doc/multi-opening-fixture.md +30 -29
  12. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/doc/persistence.md +13 -10
  13. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/doc/protocol.md +26 -1
  14. beaconcrypt-0.4.3/doc/rooterberg.md +59 -0
  15. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/doc/wycheproof.md +3 -2
  16. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/c/main.c +0 -2
  17. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/python/pyproject.toml +0 -2
  18. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/scripts/derive_multi_opening.py +50 -25
  19. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/scripts/generate_kat_vectors.go +12 -8
  20. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/scripts/generate_kat_vectors.py +12 -8
  21. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/cbinds.rs +2 -28
  22. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/deser.rs +33 -4
  23. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/pqxdh.rs +13 -19
  24. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/pybinds.rs +3 -14
  25. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/ser.rs +48 -2
  26. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/server.rs +6 -9
  27. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/shared.rs +83 -32
  28. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/tests/protocol.rs +1 -4
  29. beaconcrypt-0.4.3/tests/rooterberg.rs +426 -0
  30. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/tests/server.rs +91 -23
  31. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/tests/test_encryption.py +2 -4
  32. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/tests/test_registration.py +1 -1
  33. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/tests/test_server_state.py +53 -25
  34. beaconcrypt-0.4.0/.gitmodules +0 -3
  35. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/.cargo/config.toml +0 -0
  36. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/.github/workflows/codeql.yml +0 -0
  37. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/.github/workflows/go.yml +0 -0
  38. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/.github/workflows/python.yml +0 -0
  39. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/.github/workflows/rust.yml +0 -0
  40. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/.gitignore +0 -0
  41. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/LICENSE +0 -0
  42. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/build.rs +0 -0
  43. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/clippy.toml +0 -0
  44. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/doc/rationale.md +0 -0
  45. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/doc/threat_model.md +0 -0
  46. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/c/CMakeLists.txt +0 -0
  47. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/c/README.md +0 -0
  48. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/go/.gitignore +0 -0
  49. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/go/README.md +0 -0
  50. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/go/go.mod +0 -0
  51. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/go/go.sum +0 -0
  52. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/go/main.go +0 -0
  53. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/python/.gitignore +0 -0
  54. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/python/README.md +0 -0
  55. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/python/main.py +0 -0
  56. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/rust/README.md +0 -0
  57. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/examples/rust/main.rs +0 -0
  58. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/go.mod +0 -0
  59. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/go.sum +0 -0
  60. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/pyproject.toml +0 -0
  61. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/rustfmt.toml +0 -0
  62. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/scripts/generate_python_stubs.py +0 -0
  63. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/beacon.rs +0 -0
  64. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/error.rs +0 -0
  65. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/lib.rs +0 -0
  66. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/schema/cryptoframe.capnp +0 -0
  67. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/schema/phase1.capnp +0 -0
  68. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/src/schema/phase2.capnp +0 -0
  69. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/tests/beacon.rs +0 -0
  70. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/tests/wycheproof.rs +0 -0
  71. {beaconcrypt-0.4.0 → beaconcrypt-0.4.3}/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
@@ -0,0 +1,6 @@
1
+ [submodule "wycheproof"]
2
+ path = wycheproof
3
+ url = https://github.com/C2SP/wycheproof.git
4
+ [submodule "rooterberg"]
5
+ path = rooterberg
6
+ url = https://github.com/bleichenbacher-daniel/Rooterberg.git
@@ -78,7 +78,7 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
78
78
 
79
79
  [[package]]
80
80
  name = "beaconcrypt"
81
- version = "0.4.0"
81
+ version = "0.4.3"
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.3"
4
4
  edition = "2024"
5
5
  license-file = "LICENSE"
6
6
  readme = "README.md"
@@ -8,13 +8,8 @@ description = "Cryptographic transport for C2 channels"
8
8
  rust-version = "1.96"
9
9
  repository = "https://github.com/0xd6cb6d73/beaconcrypt"
10
10
  exclude = [
11
+ "rooterberg/**",
11
12
  "wycheproof/**",
12
- "!wycheproof/testvectors_v1/chacha20_poly1305_test.json",
13
- "!wycheproof/testvectors_v1/ed25519_test.json",
14
- "!wycheproof/testvectors_v1/hkdf_sha512_test.json",
15
- "!wycheproof/testvectors_v1/mlkem_768_encaps_test.json",
16
- "!wycheproof/testvectors_v1/mlkem_768_test.json",
17
- "!wycheproof/testvectors_v1/x25519_test.json",
18
13
  ]
19
14
 
20
15
  [lib]
@@ -36,6 +31,11 @@ name = "protocol"
36
31
  path = "tests/protocol.rs"
37
32
  required-features = ["pqxdh", "beacon", "server"]
38
33
 
34
+ [[test]]
35
+ name = "rooterberg"
36
+ path = "tests/rooterberg.rs"
37
+ required-features = ["pqxdh"]
38
+
39
39
  [[test]]
40
40
  name = "server"
41
41
  path = "tests/server.rs"
@@ -50,6 +50,10 @@ required-features = ["pqxdh"]
50
50
  capnpc = "0.26"
51
51
  cbindgen = "0.29"
52
52
 
53
+ [dev-dependencies]
54
+ serde = { version = "1", features = ["derive"] }
55
+ serde_json = "1"
56
+
53
57
  [dependencies]
54
58
  capnp = "0.26"
55
59
  libsodium-rs = "0.2"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: beaconcrypt
3
- Version: 0.4.0
3
+ Version: 0.4.3
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -97,6 +97,22 @@ cargo test
97
97
  See [the Wycheproof test documentation](doc/wycheproof.md) for the coverage
98
98
  matrix, result semantics and update procedure.
99
99
 
100
+ ### Rooterberg
101
+
102
+ The test suite also runs pinned
103
+ [Rooterberg](https://github.com/bleichenbacher-daniel/Rooterberg) vectors for
104
+ X25519, Ed25519 signing and verification, ChaCha20-Poly1305-IETF,
105
+ HKDF-SHA-512 and BLAKE2b-512. Rooterberg is included as a Git submodule and is
106
+ initialized by the same command used for Wycheproof:
107
+
108
+ ```bash
109
+ git submodule update --init --recursive
110
+ cargo test
111
+ ```
112
+
113
+ See [the Rooterberg test documentation](doc/rooterberg.md) for the coverage
114
+ matrix, upstream format caveats and update procedure.
115
+
100
116
  ## Usage
101
117
  The reference implementation is a library that can currently be used either from rust, through C FFI, go and python bindings. The C interface is currently only tested through the go bindings. Note that 0-length messages are explicitly disallowed by the reference implementation, as my feeling is that such messages have no purpose except testing parser edge cases. The library also doesn't handle chnking of any kind and will try to process entire messages at once in memory. It is expected that the caller should handle chunking itself if that is required.
102
118
 
@@ -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;
@@ -32,7 +30,7 @@ typedef struct {
32
30
  void beaconcrypt_free_buffer(beaconcrypt_buffer buffer);
33
31
  void *beaconcrypt_server_new(uint64_t server_kid);
34
32
  void *beaconcrypt_server_new_from_seed(uint64_t server_kid, const uint8_t *seed_ptr, uintptr_t seed_len);
35
- void *beaconcrypt_server_new_from_state(uint64_t server_kid, const uint8_t *seed_ptr, uintptr_t seed_len, const uint8_t *state_ptr, uintptr_t state_len);
33
+ void *beaconcrypt_server_new_from_state(const uint8_t *state_ptr, uintptr_t state_len);
36
34
  void *beaconcrypt_beacon_new(uint64_t server_kid, const uint8_t *server_pk_ptr, uintptr_t server_pk_len);
37
35
  void beaconcrypt_free(void *handle);
38
36
  beaconcrypt_buffer beaconcrypt_identity_pk(const void *handle);
@@ -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
@@ -121,22 +116,14 @@ func NewServerFromSeed(serverKID uint64, seed []byte) (*Server, error) {
121
116
  return server, nil
122
117
  }
123
118
 
124
- func NewServerFromState(serverKID uint64, seed []byte, state string) (*Server, error) {
125
- if len(seed) != 0 && len(seed) != 32 {
126
- return nil, ErrSeedSize
127
- }
119
+ func NewServerFromState(state string) (*Server, error) {
128
120
  if len(state) == 0 {
129
121
  return nil, ErrEmptyData
130
122
  }
131
- seedPtr, seedFree := cBytes(seed)
132
- defer seedFree()
133
123
  stateBytes := []byte(state)
134
124
  statePtr, stateFree := cBytes(stateBytes)
135
125
  defer stateFree()
136
126
  handle := C.beaconcrypt_server_new_from_state(
137
- C.uint64_t(serverKID),
138
- seedPtr,
139
- C.uintptr_t(len(seed)),
140
127
  statePtr,
141
128
  C.uintptr_t(len(stateBytes)),
142
129
  )
@@ -210,17 +197,11 @@ func (s *Server) RegisterBeacon(registration, initialMessage []byte) (*Registrat
210
197
  C.uintptr_t(len(initialMessage)),
211
198
  )
212
199
  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
200
  if err != nil {
219
201
  return nil, err
220
202
  }
221
203
  return &RegistrationResponse{
222
204
  Serialized: serialized,
223
- BeaconPK: beaconPK,
224
205
  KeyID: uint64(response.key_id),
225
206
  }, nil
226
207
  })
@@ -400,12 +381,6 @@ func callStateUpdate(data []byte, call func(*C.uint8_t, C.uintptr_t) C.beaconcry
400
381
  defer free()
401
382
  state := call(ptr, C.uintptr_t(len(data)))
402
383
  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
384
  if err != nil {
410
385
  C.beaconcrypt_free_buffer(state.state)
411
386
  return nil, err
@@ -416,7 +391,6 @@ func callStateUpdate(data []byte, call func(*C.uint8_t, C.uintptr_t) C.beaconcry
416
391
  }
417
392
  return &EncryptState{
418
393
  Data: output,
419
- Key: key,
420
394
  State: string(serializedState),
421
395
  KeyID: uint64(state.key_id),
422
396
  Seq: uint64(state.seq),
@@ -27,14 +27,13 @@ class BeaconCryptServer:
27
27
  def encrypt_to_beacon(self, /, data: Sequence[int], kid: int) -> bytes |None: ...
28
28
  def export_state(self, /) -> str |None: ...
29
29
  @staticmethod
30
- def from_state(kid: int, id_seed: bytes |None, server_state: str) -> BeaconCryptServer: ...
30
+ def from_state(server_state: str) -> BeaconCryptServer: ...
31
31
  def id_pk(self, /) -> bytes: ...
32
32
  def register_beacon(self, /, reg_buffer: bytes, initial_message: bytes |None) -> RegResponse |None: ...
33
33
 
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: ...
@@ -13,9 +13,11 @@ import (
13
13
  )
14
14
 
15
15
  const (
16
+ ed25519SystemID = 1
16
17
  kdfStateSystemID = 6
17
18
  chainSendKeyRole = 8
18
19
  chainReceiveRole = 9
20
+ identityKeyRole = 14
19
21
  ratchetStateBytes = 32
20
22
  )
21
23
 
@@ -162,9 +164,6 @@ func registerBeaconWithInitial(t *testing.T, server *Server, beacon *Beacon, mes
162
164
  if regOut.KeyID == 0 {
163
165
  t.Fatal("expected non-zero beacon key id")
164
166
  }
165
- if len(regOut.BeaconPK) != 32 {
166
- t.Fatalf("beacon public key length mismatch: got %d want 32", len(regOut.BeaconPK))
167
- }
168
167
  return regOut
169
168
  }
170
169
 
@@ -599,9 +598,6 @@ func TestServerEncryptAndUpdateReturnsRatchetState(t *testing.T) {
599
598
  if update.Seq != 2 {
600
599
  t.Fatalf("state sequence mismatch: got %d want 2", update.Seq)
601
600
  }
602
- if len(update.Key) != 32 {
603
- t.Fatalf("state key length mismatch: got %d want 32", len(update.Key))
604
- }
605
601
  system, role, key := decodeJSONRatchetKey(t, json.RawMessage(update.State), "send_key")
606
602
  if system != kdfStateSystemID || role != chainSendKeyRole {
607
603
  t.Fatalf(
@@ -612,8 +608,8 @@ func TestServerEncryptAndUpdateReturnsRatchetState(t *testing.T) {
612
608
  chainSendKeyRole,
613
609
  )
614
610
  }
615
- if !bytes.Equal(update.Key, key) {
616
- t.Fatalf("directional key does not match complete state: got %x want %x", update.Key, key)
611
+ if len(key) != ratchetStateBytes {
612
+ t.Fatalf("state key length mismatch: got %d want %d", len(key), ratchetStateBytes)
617
613
  }
618
614
  plaintext, err := beacon.DecryptServerMessage(update.Data)
619
615
  if err != nil {
@@ -648,9 +644,6 @@ func TestServerDecryptAndUpdateReturnsRatchetState(t *testing.T) {
648
644
  if update.Seq != 1 {
649
645
  t.Fatalf("state sequence mismatch: got %d want 1", update.Seq)
650
646
  }
651
- if len(update.Key) != 32 {
652
- t.Fatalf("state key length mismatch: got %d want 32", len(update.Key))
653
- }
654
647
  system, role, key := decodeJSONRatchetKey(t, json.RawMessage(update.State), "recv_key")
655
648
  if system != kdfStateSystemID || role != chainReceiveRole {
656
649
  t.Fatalf(
@@ -661,8 +654,8 @@ func TestServerDecryptAndUpdateReturnsRatchetState(t *testing.T) {
661
654
  chainReceiveRole,
662
655
  )
663
656
  }
664
- if !bytes.Equal(update.Key, key) {
665
- t.Fatalf("directional key does not match complete state: got %x want %x", update.Key, key)
657
+ if len(key) != ratchetStateBytes {
658
+ t.Fatalf("state key length mismatch: got %d want %d", len(key), ratchetStateBytes)
666
659
  }
667
660
  if !bytes.Equal(update.Data, message) {
668
661
  t.Fatalf("beacon-to-server decrypt mismatch: got %q want %q", update.Data, message)
@@ -687,12 +680,12 @@ func TestStructuredAndJSONUpdatesMatchAcrossTheCGoBoundary(t *testing.T) {
687
680
  t.Fatal(err)
688
681
  }
689
682
 
690
- structuredServer, err := NewServerFromState(0, seed, initialState)
683
+ structuredServer, err := NewServerFromState(initialState)
691
684
  if err != nil {
692
685
  t.Fatal(err)
693
686
  }
694
687
  t.Cleanup(structuredServer.Close)
695
- jsonServer, err := NewServerFromState(0, seed, initialState)
688
+ jsonServer, err := NewServerFromState(initialState)
696
689
  if err != nil {
697
690
  t.Fatal(err)
698
691
  }
@@ -720,9 +713,6 @@ func TestStructuredAndJSONUpdatesMatchAcrossTheCGoBoundary(t *testing.T) {
720
713
  if !bytes.Equal(structuredSend.Data, jsonSend.Data) {
721
714
  t.Fatalf("send update data mismatch: got %x want %x", structuredSend.Data, jsonSend.Data)
722
715
  }
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
716
  assertJSONEqual(t, structuredSend.State, string(jsonSend.State))
727
717
 
728
718
  inbound := []byte("compare structured and JSON decryption updates")
@@ -751,9 +741,6 @@ func TestStructuredAndJSONUpdatesMatchAcrossTheCGoBoundary(t *testing.T) {
751
741
  if !bytes.Equal(structuredRecv.Data, jsonRecv.Data) {
752
742
  t.Fatalf("receive update data mismatch: got %x want %x", structuredRecv.Data, jsonRecv.Data)
753
743
  }
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
744
  assertJSONEqual(t, structuredRecv.State, string(jsonRecv.State))
758
745
  }
759
746
 
@@ -918,7 +905,7 @@ func TestServerStateRoundTripContinuesSessionsAndKeyIDs(t *testing.T) {
918
905
  }
919
906
  server.Close()
920
907
 
921
- restored, err := NewServerFromState(0, seed, state)
908
+ restored, err := NewServerFromState(state)
922
909
  if err != nil {
923
910
  t.Fatal(err)
924
911
  }
@@ -1002,20 +989,22 @@ func TestServerStateRoundTripPreservesCachedOutOfOrderReceiveKeys(t *testing.T)
1002
989
  if err != nil {
1003
990
  t.Fatal(err)
1004
991
  }
1005
- var encoded map[string]struct {
1006
- Ratchet struct {
1007
- RecvPast map[string]json.RawMessage `json:"recv_past"`
1008
- } `json:"ratchet"`
992
+ var encoded struct {
993
+ KnownIDs map[string]struct {
994
+ Ratchet struct {
995
+ RecvPast map[string]json.RawMessage `json:"recv_past"`
996
+ } `json:"ratchet"`
997
+ } `json:"known_ids"`
1009
998
  }
1010
999
  if err := json.Unmarshal([]byte(state), &encoded); err != nil {
1011
1000
  t.Fatal(err)
1012
1001
  }
1013
- cached := encoded[strconv.FormatUint(registration.KeyID, 10)].Ratchet.RecvPast
1002
+ cached := encoded.KnownIDs[strconv.FormatUint(registration.KeyID, 10)].Ratchet.RecvPast
1014
1003
  if len(cached) != 2 || cached["1"] == nil || cached["2"] == nil {
1015
1004
  t.Fatalf("cached receive keys mismatch: got keys %v want [1 2]", reflect.ValueOf(cached).MapKeys())
1016
1005
  }
1017
1006
 
1018
- restored, err := NewServerFromState(0, seed, state)
1007
+ restored, err := NewServerFromState(state)
1019
1008
  if err != nil {
1020
1009
  t.Fatal(err)
1021
1010
  }
@@ -1036,43 +1025,87 @@ func TestServerStateRoundTripPreservesCachedOutOfOrderReceiveKeys(t *testing.T)
1036
1025
  }
1037
1026
  }
1038
1027
 
1039
- func TestServerFromEmptyStateAllowsNilSeed(t *testing.T) {
1028
+ func TestServerStateWithoutKnownIDsRestoresIdentityAndCounter(t *testing.T) {
1040
1029
  const serverKID = 7
1041
1030
 
1042
- server, err := NewServerFromState(serverKID, nil, "{}")
1031
+ server, err := NewServer(serverKID)
1043
1032
  if err != nil {
1044
1033
  t.Fatal(err)
1045
1034
  }
1046
1035
  t.Cleanup(server.Close)
1036
+ serverPK, err := server.IdentityPK()
1037
+ if err != nil {
1038
+ t.Fatal(err)
1039
+ }
1047
1040
  state, err := server.ExportState()
1048
1041
  if err != nil {
1049
1042
  t.Fatal(err)
1050
1043
  }
1051
- if state != "{}" {
1052
- t.Fatalf("empty state mismatch: got %q want %q", state, "{}")
1044
+
1045
+ var encoded struct {
1046
+ IdentityKey []json.RawMessage `json:"identity_key"`
1047
+ IdentityKeyKID uint64 `json:"identity_key_kid"`
1048
+ ServerKID uint64 `json:"server_kid"`
1049
+ KnownIDs map[string]json.RawMessage `json:"known_ids"`
1050
+ }
1051
+ if err := json.Unmarshal([]byte(state), &encoded); err != nil {
1052
+ t.Fatal(err)
1053
+ }
1054
+ if len(encoded.IdentityKey) != 3 ||
1055
+ encoded.IdentityKeyKID != serverKID ||
1056
+ encoded.ServerKID != serverKID ||
1057
+ len(encoded.KnownIDs) != 0 {
1058
+ t.Fatalf("incomplete exported state: %+v", encoded)
1059
+ }
1060
+ var identitySystem uint8
1061
+ var identityRole uint8
1062
+ var identitySeed []byte
1063
+ if err := json.Unmarshal(encoded.IdentityKey[0], &identitySystem); err != nil {
1064
+ t.Fatal(err)
1065
+ }
1066
+ if err := json.Unmarshal(encoded.IdentityKey[1], &identityRole); err != nil {
1067
+ t.Fatal(err)
1068
+ }
1069
+ if err := json.Unmarshal(encoded.IdentityKey[2], &identitySeed); err != nil {
1070
+ t.Fatal(err)
1071
+ }
1072
+ if identitySystem != ed25519SystemID || identityRole != identityKeyRole || len(identitySeed) != 32 {
1073
+ t.Fatalf(
1074
+ "identity key type mismatch: got [%d,%d,%d bytes] want [%d,%d,32 bytes]",
1075
+ identitySystem,
1076
+ identityRole,
1077
+ len(identitySeed),
1078
+ ed25519SystemID,
1079
+ identityKeyRole,
1080
+ )
1053
1081
  }
1054
1082
 
1055
- serverPK, err := server.IdentityPK()
1083
+ restored, err := NewServerFromState(state)
1084
+ if err != nil {
1085
+ t.Fatal(err)
1086
+ }
1087
+ t.Cleanup(restored.Close)
1088
+ restoredPK, err := restored.IdentityPK()
1056
1089
  if err != nil {
1057
1090
  t.Fatal(err)
1058
1091
  }
1059
- beacon, err := NewBeacon(serverKID, serverPK)
1092
+ if !bytes.Equal(restoredPK, serverPK) {
1093
+ t.Fatalf("restored server identity mismatch: got %x want %x", restoredPK, serverPK)
1094
+ }
1095
+
1096
+ beacon, err := NewBeacon(serverKID, restoredPK)
1060
1097
  if err != nil {
1061
1098
  t.Fatal(err)
1062
1099
  }
1063
1100
  t.Cleanup(beacon.Close)
1064
- registration := registerBeacon(t, server, beacon)
1101
+ registration := registerBeacon(t, restored, beacon)
1065
1102
  if registration.KeyID != serverKID+1 {
1066
1103
  t.Fatalf("first key ID mismatch: got %d want %d", registration.KeyID, serverKID+1)
1067
1104
  }
1068
1105
  }
1069
1106
 
1070
1107
  func TestNewServerFromStateRejectsInvalidInput(t *testing.T) {
1071
- seed := bytes.Repeat([]byte{0x61}, 32)
1072
- if _, err := NewServerFromState(0, seed[:31], "{}"); !errors.Is(err, ErrSeedSize) {
1073
- t.Fatalf("invalid seed error mismatch: got %v want %v", err, ErrSeedSize)
1074
- }
1075
- if _, err := NewServerFromState(0, seed, ""); !errors.Is(err, ErrEmptyData) {
1108
+ if _, err := NewServerFromState(""); !errors.Is(err, ErrEmptyData) {
1076
1109
  t.Fatalf("empty state error mismatch: got %v want %v", err, ErrEmptyData)
1077
1110
  }
1078
1111
 
@@ -1082,7 +1115,7 @@ func TestNewServerFromStateRejectsInvalidInput(t *testing.T) {
1082
1115
  `{"1":{"pk":[],"ratchet":{}}}`,
1083
1116
  }
1084
1117
  for _, state := range invalidStates {
1085
- if server, err := NewServerFromState(0, seed, state); !errors.Is(err, ErrCrypto) {
1118
+ if server, err := NewServerFromState(state); !errors.Is(err, ErrCrypto) {
1086
1119
  if server != nil {
1087
1120
  server.Close()
1088
1121
  }
@@ -1110,28 +1143,59 @@ func TestNewServerFromStateRejectsTamperedExportedState(t *testing.T) {
1110
1143
  }
1111
1144
 
1112
1145
  wrongKeyType := mutateJSONObject(t, state, func(value map[string]any) {
1113
- value["1"].(map[string]any)["pk"].([]any)[0] = float64(2)
1146
+ value["known_ids"].(map[string]any)["1"].(map[string]any)["pk"].([]any)[0] = float64(2)
1114
1147
  })
1115
1148
  shortKey := mutateJSONObject(t, state, func(value map[string]any) {
1116
- principal := value["1"].(map[string]any)
1149
+ principal := value["known_ids"].(map[string]any)["1"].(map[string]any)
1117
1150
  publicKey := principal["pk"].([]any)
1118
1151
  principal["pk"] = publicKey[:len(publicKey)-1]
1119
1152
  })
1120
1153
  malformedRatchet := mutateJSONObject(t, state, func(value map[string]any) {
1121
- principal := value["1"].(map[string]any)
1154
+ principal := value["known_ids"].(map[string]any)["1"].(map[string]any)
1122
1155
  ratchet := principal["ratchet"].(map[string]any)
1123
1156
  ratchet["send_key"].([]any)[0] = float64(0)
1124
1157
  })
1125
- entry := state[1 : len(state)-1]
1126
- duplicateKID := "{" + entry + "," + entry + "}"
1158
+ malformedIdentity := mutateJSONObject(t, state, func(value map[string]any) {
1159
+ identityKey := value["identity_key"].([]any)
1160
+ value["identity_key"] = identityKey[:len(identityKey)-1]
1161
+ })
1162
+ wrongIdentitySystem := mutateJSONObject(t, state, func(value map[string]any) {
1163
+ value["identity_key"].([]any)[0] = float64(0)
1164
+ })
1165
+ wrongIdentityRole := mutateJSONObject(t, state, func(value map[string]any) {
1166
+ value["identity_key"].([]any)[1] = float64(chainSendKeyRole)
1167
+ })
1168
+ invalidIdentityKID := mutateJSONObject(t, state, func(value map[string]any) {
1169
+ value["identity_key_kid"] = value["server_kid"].(float64) + 1
1170
+ })
1171
+ regressedServerKID := mutateJSONObject(t, state, func(value map[string]any) {
1172
+ value["server_kid"] = float64(0)
1173
+ })
1174
+ var envelope map[string]json.RawMessage
1175
+ if err := json.Unmarshal([]byte(state), &envelope); err != nil {
1176
+ t.Fatal(err)
1177
+ }
1178
+ var knownIDs map[string]json.RawMessage
1179
+ if err := json.Unmarshal(envelope["known_ids"], &knownIDs); err != nil {
1180
+ t.Fatal(err)
1181
+ }
1182
+ principal := string(knownIDs["1"])
1183
+ duplicateKID := `{"identity_key":` + string(envelope["identity_key"]) +
1184
+ `,"identity_key_kid":0,"server_kid":1,"known_ids":{"1":` +
1185
+ principal + `,"1":` + principal + `}}`
1127
1186
 
1128
1187
  for _, malformed := range []string{
1129
1188
  wrongKeyType,
1130
1189
  shortKey,
1131
1190
  malformedRatchet,
1191
+ malformedIdentity,
1192
+ wrongIdentitySystem,
1193
+ wrongIdentityRole,
1194
+ invalidIdentityKID,
1195
+ regressedServerKID,
1132
1196
  duplicateKID,
1133
1197
  } {
1134
- restored, err := NewServerFromState(0, seed, malformed)
1198
+ restored, err := NewServerFromState(malformed)
1135
1199
  if restored != nil {
1136
1200
  restored.Close()
1137
1201
  }
@@ -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
  */
@@ -82,10 +79,7 @@ struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_server_new_from_seed(uint64_t s
82
79
  const uint8_t *seed_ptr,
83
80
  uintptr_t seed_len);
84
81
 
85
- struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_server_new_from_state(uint64_t server_kid,
86
- const uint8_t *seed_ptr,
87
- uintptr_t seed_len,
88
- const uint8_t *state_ptr,
82
+ struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_server_new_from_state(const uint8_t *state_ptr,
89
83
  uintptr_t state_len);
90
84
 
91
85
  struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_beacon_new(uint64_t server_kid,