beaconcrypt 0.4.2__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.3/.gitmodules +6 -0
  2. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/Cargo.lock +1 -1
  3. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/Cargo.toml +11 -7
  4. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/PKG-INFO +1 -1
  5. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/README.md +16 -0
  6. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/beaconcrypt.go +2 -10
  7. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/beaconcrypt.pyi +1 -1
  8. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/beaconcrypt_test.go +107 -28
  9. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/bindings.h +1 -4
  10. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/doc/persistence.md +12 -4
  11. beaconcrypt-0.4.3/doc/rooterberg.md +59 -0
  12. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/doc/wycheproof.md +3 -2
  13. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/cbinds.rs +2 -17
  14. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/deser.rs +33 -4
  15. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/pqxdh.rs +13 -17
  16. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/pybinds.rs +3 -3
  17. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/ser.rs +48 -2
  18. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/server.rs +6 -7
  19. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/shared.rs +23 -2
  20. beaconcrypt-0.4.3/tests/rooterberg.rs +426 -0
  21. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/tests/server.rs +91 -23
  22. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/tests/test_server_state.py +52 -22
  23. beaconcrypt-0.4.2/.gitmodules +0 -3
  24. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/.cargo/config.toml +0 -0
  25. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/.github/workflows/codeql.yml +0 -0
  26. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/.github/workflows/go.yml +0 -0
  27. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/.github/workflows/python.yml +0 -0
  28. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/.github/workflows/release.yml +0 -0
  29. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/.github/workflows/rust.yml +0 -0
  30. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/.gitignore +0 -0
  31. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/LICENSE +0 -0
  32. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/build.rs +0 -0
  33. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/clippy.toml +0 -0
  34. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/doc/multi-opening-fixture.md +0 -0
  35. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/doc/protocol.md +0 -0
  36. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/doc/rationale.md +0 -0
  37. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/doc/threat_model.md +0 -0
  38. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/c/CMakeLists.txt +0 -0
  39. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/c/README.md +0 -0
  40. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/c/main.c +0 -0
  41. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/go/.gitignore +0 -0
  42. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/go/README.md +0 -0
  43. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/go/go.mod +0 -0
  44. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/go/go.sum +0 -0
  45. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/go/main.go +0 -0
  46. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/python/.gitignore +0 -0
  47. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/python/README.md +0 -0
  48. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/python/main.py +0 -0
  49. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/python/pyproject.toml +0 -0
  50. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/rust/README.md +0 -0
  51. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/examples/rust/main.rs +0 -0
  52. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/go.mod +0 -0
  53. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/go.sum +0 -0
  54. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/pyproject.toml +0 -0
  55. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/rustfmt.toml +0 -0
  56. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/scripts/derive_multi_opening.py +0 -0
  57. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/scripts/generate_kat_vectors.go +0 -0
  58. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/scripts/generate_kat_vectors.py +0 -0
  59. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/scripts/generate_python_stubs.py +0 -0
  60. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/beacon.rs +0 -0
  61. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/error.rs +0 -0
  62. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/lib.rs +0 -0
  63. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/schema/cryptoframe.capnp +0 -0
  64. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/schema/phase1.capnp +0 -0
  65. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/src/schema/phase2.capnp +0 -0
  66. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/tests/beacon.rs +0 -0
  67. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/tests/protocol.rs +0 -0
  68. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/tests/test_encryption.py +0 -0
  69. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/tests/test_registration.py +0 -0
  70. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/tests/wycheproof.rs +0 -0
  71. {beaconcrypt-0.4.2 → beaconcrypt-0.4.3}/uv.lock +0 -0
@@ -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.2"
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.2"
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.2
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
 
@@ -30,7 +30,7 @@ typedef struct {
30
30
  void beaconcrypt_free_buffer(beaconcrypt_buffer buffer);
31
31
  void *beaconcrypt_server_new(uint64_t server_kid);
32
32
  void *beaconcrypt_server_new_from_seed(uint64_t server_kid, const uint8_t *seed_ptr, uintptr_t seed_len);
33
- 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);
34
34
  void *beaconcrypt_beacon_new(uint64_t server_kid, const uint8_t *server_pk_ptr, uintptr_t server_pk_len);
35
35
  void beaconcrypt_free(void *handle);
36
36
  beaconcrypt_buffer beaconcrypt_identity_pk(const void *handle);
@@ -116,22 +116,14 @@ func NewServerFromSeed(serverKID uint64, seed []byte) (*Server, error) {
116
116
  return server, nil
117
117
  }
118
118
 
119
- func NewServerFromState(serverKID uint64, seed []byte, state string) (*Server, error) {
120
- if len(seed) != 0 && len(seed) != 32 {
121
- return nil, ErrSeedSize
122
- }
119
+ func NewServerFromState(state string) (*Server, error) {
123
120
  if len(state) == 0 {
124
121
  return nil, ErrEmptyData
125
122
  }
126
- seedPtr, seedFree := cBytes(seed)
127
- defer seedFree()
128
123
  stateBytes := []byte(state)
129
124
  statePtr, stateFree := cBytes(stateBytes)
130
125
  defer stateFree()
131
126
  handle := C.beaconcrypt_server_new_from_state(
132
- C.uint64_t(serverKID),
133
- seedPtr,
134
- C.uintptr_t(len(seed)),
135
127
  statePtr,
136
128
  C.uintptr_t(len(stateBytes)),
137
129
  )
@@ -27,7 +27,7 @@ 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
 
@@ -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
 
@@ -678,12 +680,12 @@ func TestStructuredAndJSONUpdatesMatchAcrossTheCGoBoundary(t *testing.T) {
678
680
  t.Fatal(err)
679
681
  }
680
682
 
681
- structuredServer, err := NewServerFromState(0, seed, initialState)
683
+ structuredServer, err := NewServerFromState(initialState)
682
684
  if err != nil {
683
685
  t.Fatal(err)
684
686
  }
685
687
  t.Cleanup(structuredServer.Close)
686
- jsonServer, err := NewServerFromState(0, seed, initialState)
688
+ jsonServer, err := NewServerFromState(initialState)
687
689
  if err != nil {
688
690
  t.Fatal(err)
689
691
  }
@@ -903,7 +905,7 @@ func TestServerStateRoundTripContinuesSessionsAndKeyIDs(t *testing.T) {
903
905
  }
904
906
  server.Close()
905
907
 
906
- restored, err := NewServerFromState(0, seed, state)
908
+ restored, err := NewServerFromState(state)
907
909
  if err != nil {
908
910
  t.Fatal(err)
909
911
  }
@@ -987,20 +989,22 @@ func TestServerStateRoundTripPreservesCachedOutOfOrderReceiveKeys(t *testing.T)
987
989
  if err != nil {
988
990
  t.Fatal(err)
989
991
  }
990
- var encoded map[string]struct {
991
- Ratchet struct {
992
- RecvPast map[string]json.RawMessage `json:"recv_past"`
993
- } `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"`
994
998
  }
995
999
  if err := json.Unmarshal([]byte(state), &encoded); err != nil {
996
1000
  t.Fatal(err)
997
1001
  }
998
- cached := encoded[strconv.FormatUint(registration.KeyID, 10)].Ratchet.RecvPast
1002
+ cached := encoded.KnownIDs[strconv.FormatUint(registration.KeyID, 10)].Ratchet.RecvPast
999
1003
  if len(cached) != 2 || cached["1"] == nil || cached["2"] == nil {
1000
1004
  t.Fatalf("cached receive keys mismatch: got keys %v want [1 2]", reflect.ValueOf(cached).MapKeys())
1001
1005
  }
1002
1006
 
1003
- restored, err := NewServerFromState(0, seed, state)
1007
+ restored, err := NewServerFromState(state)
1004
1008
  if err != nil {
1005
1009
  t.Fatal(err)
1006
1010
  }
@@ -1021,43 +1025,87 @@ func TestServerStateRoundTripPreservesCachedOutOfOrderReceiveKeys(t *testing.T)
1021
1025
  }
1022
1026
  }
1023
1027
 
1024
- func TestServerFromEmptyStateAllowsNilSeed(t *testing.T) {
1028
+ func TestServerStateWithoutKnownIDsRestoresIdentityAndCounter(t *testing.T) {
1025
1029
  const serverKID = 7
1026
1030
 
1027
- server, err := NewServerFromState(serverKID, nil, "{}")
1031
+ server, err := NewServer(serverKID)
1028
1032
  if err != nil {
1029
1033
  t.Fatal(err)
1030
1034
  }
1031
1035
  t.Cleanup(server.Close)
1036
+ serverPK, err := server.IdentityPK()
1037
+ if err != nil {
1038
+ t.Fatal(err)
1039
+ }
1032
1040
  state, err := server.ExportState()
1033
1041
  if err != nil {
1034
1042
  t.Fatal(err)
1035
1043
  }
1036
- if state != "{}" {
1037
- 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
+ )
1038
1081
  }
1039
1082
 
1040
- serverPK, err := server.IdentityPK()
1083
+ restored, err := NewServerFromState(state)
1041
1084
  if err != nil {
1042
1085
  t.Fatal(err)
1043
1086
  }
1044
- beacon, err := NewBeacon(serverKID, serverPK)
1087
+ t.Cleanup(restored.Close)
1088
+ restoredPK, err := restored.IdentityPK()
1089
+ if err != nil {
1090
+ t.Fatal(err)
1091
+ }
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)
1045
1097
  if err != nil {
1046
1098
  t.Fatal(err)
1047
1099
  }
1048
1100
  t.Cleanup(beacon.Close)
1049
- registration := registerBeacon(t, server, beacon)
1101
+ registration := registerBeacon(t, restored, beacon)
1050
1102
  if registration.KeyID != serverKID+1 {
1051
1103
  t.Fatalf("first key ID mismatch: got %d want %d", registration.KeyID, serverKID+1)
1052
1104
  }
1053
1105
  }
1054
1106
 
1055
1107
  func TestNewServerFromStateRejectsInvalidInput(t *testing.T) {
1056
- seed := bytes.Repeat([]byte{0x61}, 32)
1057
- if _, err := NewServerFromState(0, seed[:31], "{}"); !errors.Is(err, ErrSeedSize) {
1058
- t.Fatalf("invalid seed error mismatch: got %v want %v", err, ErrSeedSize)
1059
- }
1060
- if _, err := NewServerFromState(0, seed, ""); !errors.Is(err, ErrEmptyData) {
1108
+ if _, err := NewServerFromState(""); !errors.Is(err, ErrEmptyData) {
1061
1109
  t.Fatalf("empty state error mismatch: got %v want %v", err, ErrEmptyData)
1062
1110
  }
1063
1111
 
@@ -1067,7 +1115,7 @@ func TestNewServerFromStateRejectsInvalidInput(t *testing.T) {
1067
1115
  `{"1":{"pk":[],"ratchet":{}}}`,
1068
1116
  }
1069
1117
  for _, state := range invalidStates {
1070
- if server, err := NewServerFromState(0, seed, state); !errors.Is(err, ErrCrypto) {
1118
+ if server, err := NewServerFromState(state); !errors.Is(err, ErrCrypto) {
1071
1119
  if server != nil {
1072
1120
  server.Close()
1073
1121
  }
@@ -1095,28 +1143,59 @@ func TestNewServerFromStateRejectsTamperedExportedState(t *testing.T) {
1095
1143
  }
1096
1144
 
1097
1145
  wrongKeyType := mutateJSONObject(t, state, func(value map[string]any) {
1098
- 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)
1099
1147
  })
1100
1148
  shortKey := mutateJSONObject(t, state, func(value map[string]any) {
1101
- principal := value["1"].(map[string]any)
1149
+ principal := value["known_ids"].(map[string]any)["1"].(map[string]any)
1102
1150
  publicKey := principal["pk"].([]any)
1103
1151
  principal["pk"] = publicKey[:len(publicKey)-1]
1104
1152
  })
1105
1153
  malformedRatchet := mutateJSONObject(t, state, func(value map[string]any) {
1106
- principal := value["1"].(map[string]any)
1154
+ principal := value["known_ids"].(map[string]any)["1"].(map[string]any)
1107
1155
  ratchet := principal["ratchet"].(map[string]any)
1108
1156
  ratchet["send_key"].([]any)[0] = float64(0)
1109
1157
  })
1110
- entry := state[1 : len(state)-1]
1111
- 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 + `}}`
1112
1186
 
1113
1187
  for _, malformed := range []string{
1114
1188
  wrongKeyType,
1115
1189
  shortKey,
1116
1190
  malformedRatchet,
1191
+ malformedIdentity,
1192
+ wrongIdentitySystem,
1193
+ wrongIdentityRole,
1194
+ invalidIdentityKID,
1195
+ regressedServerKID,
1117
1196
  duplicateKID,
1118
1197
  } {
1119
- restored, err := NewServerFromState(0, seed, malformed)
1198
+ restored, err := NewServerFromState(malformed)
1120
1199
  if restored != nil {
1121
1200
  restored.Close()
1122
1201
  }
@@ -79,10 +79,7 @@ struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_server_new_from_seed(uint64_t s
79
79
  const uint8_t *seed_ptr,
80
80
  uintptr_t seed_len);
81
81
 
82
- struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_server_new_from_state(uint64_t server_kid,
83
- const uint8_t *seed_ptr,
84
- uintptr_t seed_len,
85
- const uint8_t *state_ptr,
82
+ struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_server_new_from_state(const uint8_t *state_ptr,
86
83
  uintptr_t state_len);
87
84
 
88
85
  struct beaconcrypt_BeaconCryptPqxdh *beaconcrypt_beacon_new(uint64_t server_kid,
@@ -2,10 +2,14 @@
2
2
  This document describes the way beaconcrypt exposes state persistence to the server. This breaks forward secrecy, but is required to be useful in a server context. Additionally, the [threat model](threat_model.md) specifies that we already assume that server compromise is game over.
3
3
 
4
4
  # Usage
5
- The server object exposes a `export_state` method, which produces the following JSON output:
5
+ The server object exposes an `export_state` method, which produces the following JSON output:
6
6
  ```json
7
7
  {
8
- "1": {
8
+ "identity_key": [1,14,[65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65]],
9
+ "identity_key_kid": 0,
10
+ "server_kid": 2,
11
+ "known_ids": {
12
+ "1": {
9
13
  "pk": [1,179,176,97,56,138,132,165,3,79,7,190,144,179,105,2,187,3,165,119,87,142,123,131,59,254,117,167,156,41,125,254,56],
10
14
  "ratchet": {
11
15
  "send_key": [6,8,[1,79,112,132,98,47,6,118,212,221,235,120,149,156,87,172,106,157,248,36,135,198,191,207,243,65,83,183,57,110,161,213]],
@@ -16,7 +20,7 @@ The server object exposes a `export_state` method, which produces the following
16
20
  "recv_ctr": 1
17
21
  }
18
22
  },
19
- "2": {
23
+ "2": {
20
24
  "pk": [1,198,247,65,172,172,76,66,155,64,140,90,73,162,40,247,225,134,162,239,15,149,105,64,89,167,171,159,125,28,56,207,166],
21
25
  "ratchet": {
22
26
  "send_key": [6,8,[62,234,153,109,200,24,60,67,197,43,127,175,32,40,158,53,61,233,196,100,226,72,64,51,164,91,186,161,85,15,211,144]],
@@ -26,11 +30,15 @@ The server object exposes a `export_state` method, which produces the following
26
30
  "recv_past": {},
27
31
  "recv_ctr": 1
28
32
  }
33
+ }
29
34
  }
30
35
  }
31
36
  ```
32
37
 
33
- The way to read this is that the server knows about 2 beacons, with keyId 1 and 2. Every object then contains the beacon's public key as well as the full state of their associated ratchets. Note that the keys are strongly-typed arrays, which server code should not try to parse. I expect that this method would be rather slow, as it will extract and serialize the entirety of the beaconcrypt instance's crypto state. Therefore, the server has access to the following interface:
38
+ `identity_key` contains the server's 32-byte Ed25519 seed as a strongly typed array. `identity_key_kid` is the server identity's key ID, `server_kid` is the next-remote-ID counter, and `known_ids` contains the known beacons' ratchet state. In this example the server knows about 2 beacons, with keyId 1 and 2. Every `known_ids` entry contains the beacon's public key as well as the full state of its associated ratchets. Together these fields allow `from_state` to restore the server from the serialized state alone.
39
+
40
+ Note that the ratchet keys are strongly-typed arrays, which server code should not try to parse. I expect that this method would be rather slow, as it will extract and serialize the entirety of the beaconcrypt instance's crypto state. Therefore, the server has access to the following interface:
41
+
34
42
  ```rust
35
43
  /// Encrypt some bytes to `kid` and return the ciphertext, `kid`, consumed key sequence,
36
44
  /// and complete ratchet state for `kid`.
@@ -0,0 +1,59 @@
1
+ # Rooterberg tests
2
+
3
+ Beaconcrypt runs the upstream
4
+ [Rooterberg](https://github.com/bleichenbacher-daniel/Rooterberg) vectors
5
+ against the `libsodium-rs` APIs used by the protocol. Rooterberg is included as
6
+ a pinned Git submodule so test results are reproducible and vector updates are
7
+ explicit.
8
+
9
+ Rooterberg describes itself as experimental: its vectors extend Wycheproof,
10
+ and its format may change. The harness therefore validates the format version,
11
+ test type, algorithm metadata and declared test count before running cases.
12
+
13
+ ## Coverage
14
+
15
+ | Protocol primitive | Rooterberg vector | What is checked |
16
+ | --- | --- | --- |
17
+ | X25519 | `xdh/x25519.json` | Valid shared secrets, twists, non-canonical inputs, arithmetic edge cases and rejection of low-order public keys |
18
+ | Ed25519 | `eddsa/ed25519.json`, `eddsa/ed25519_sign.json` | Deterministic signing KATs, valid signatures and rejection of malformed, non-canonical and forged signatures |
19
+ | ChaCha20-Poly1305-IETF | `aead/chacha20_poly1305.json` | Encryption KATs, decryption, AAD authentication and rejection of invalid tags |
20
+ | HKDF-SHA-512 | `kdf/hkdf_sha512.json` | Extract-and-expand KATs across empty, boundary and pseudorandom inputs |
21
+ | BLAKE2b-512 | `message_digest/blake2b.json` | Unkeyed digest KATs across boundary and pseudorandom message lengths |
22
+
23
+ Rooterberg uses a boolean `valid` result rather than Wycheproof's
24
+ `valid`/`invalid`/`acceptable` result model. The harness follows Rooterberg's
25
+ strict result for every vector. Rooterberg does not currently publish ML-KEM
26
+ vectors, so ML-KEM-768 remains covered by Wycheproof and the protocol tests.
27
+
28
+ Rooterberg does not provide a license at the pinned revision. Its vector files
29
+ therefore remain in the external submodule and are excluded from Beaconcrypt's
30
+ published crate archives. Consequently, this external-vector test target can
31
+ only be run from a repository checkout with initialized submodules.
32
+
33
+ ## Running the tests
34
+
35
+ Initialize the submodules after cloning:
36
+
37
+ ```sh
38
+ git submodule update --init --recursive
39
+ cargo test --test rooterberg
40
+ ```
41
+
42
+ The normal `cargo test` command also runs the suite when the `pqxdh` feature is
43
+ enabled.
44
+
45
+ ## Updating vectors
46
+
47
+ Update the pinned revision deliberately, then run both external-vector and
48
+ protocol suites:
49
+
50
+ ```sh
51
+ git -C rooterberg fetch origin
52
+ git -C rooterberg checkout <reviewed-revision>
53
+ cargo test --test rooterberg
54
+ cargo test --test wycheproof
55
+ cargo test
56
+ ```
57
+
58
+ Review upstream format, algorithm metadata, result semantics and licensing
59
+ changes before accepting an update.
@@ -21,8 +21,9 @@ those two outcomes. A successful operation that produces any other secret
21
21
  fails the test.
22
22
 
23
23
  BLAKE2b is used by the commitment construction, but Wycheproof does not
24
- currently publish BLAKE2b vectors. It remains covered by the independent
25
- known-answer, context-binding and multi-opening tests in `src/shared.rs`.
24
+ currently publish BLAKE2b vectors. It is covered by the Rooterberg suite and
25
+ the independent known-answer, context-binding and multi-opening tests in
26
+ `src/shared.rs`.
26
27
  The protocol-specific use of all primitives is additionally covered by the
27
28
  registration, tampering, ratchet and end-to-end tests.
28
29
 
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: 0BSD
2
2
 
3
3
  use crate::server::{RecvState, SendState};
4
- use crate::{BeaconCryptPqxdh, CryptoProvider, ED25519_SEED_SIZE, ProviderBeacon, ProviderServer};
4
+ use crate::{BeaconCryptPqxdh, CryptoProvider, ProviderBeacon, ProviderServer};
5
5
  use std::mem;
6
6
  use std::slice;
7
7
 
@@ -110,31 +110,16 @@ pub extern "C" fn beaconcrypt_server_new_from_seed(
110
110
 
111
111
  #[unsafe(no_mangle)]
112
112
  pub extern "C" fn beaconcrypt_server_new_from_state(
113
- server_kid: u64,
114
- seed_ptr: *const u8,
115
- seed_len: usize,
116
113
  state_ptr: *const u8,
117
114
  state_len: usize,
118
115
  ) -> *mut BeaconCryptPqxdh {
119
- let seed = match seed_len {
120
- 0 => None,
121
- ED25519_SEED_SIZE => {
122
- let Some(seed) = (unsafe { input(seed_ptr, seed_len) }) else {
123
- return std::ptr::null_mut();
124
- };
125
- Some(seed)
126
- }
127
- _ => return std::ptr::null_mut(),
128
- };
129
116
  let Some(state) = (unsafe { input(state_ptr, state_len) }) else {
130
117
  return std::ptr::null_mut();
131
118
  };
132
119
  let Ok(state) = std::str::from_utf8(state) else {
133
120
  return std::ptr::null_mut();
134
121
  };
135
- let Some(provider) =
136
- <BeaconCryptPqxdh as ProviderServer>::try_from_state(server_kid, seed, state)
137
- else {
122
+ let Some(provider) = <BeaconCryptPqxdh as ProviderServer>::try_from_state(state) else {
138
123
  return std::ptr::null_mut();
139
124
  };
140
125
 
@@ -340,11 +340,38 @@ impl<'de> Deserialize<'de> for RemotePrincipal<crypto_sign::PublicKey> {
340
340
  struct DeserializedKnownIds(HashMap<u64, RemotePrincipal<crypto_sign::PublicKey>>);
341
341
 
342
342
  #[cfg(feature = "server")]
343
- pub(crate) fn deserialize_known_ids(
343
+ #[derive(Deserialize)]
344
+ #[serde(rename = "BeaconCryptPqxdh")]
345
+ struct ServerStateData {
346
+ identity_key: TypedArray<{ crypto_sign::SEEDBYTES }, systems::Ed25519, roles::IdentityKey>,
347
+ identity_key_kid: u64,
348
+ server_kid: u64,
349
+ known_ids: DeserializedKnownIds,
350
+ }
351
+
352
+ #[cfg(feature = "server")]
353
+ pub(crate) fn deserialize_server_state(
344
354
  state: &str,
345
- ) -> Option<HashMap<u64, RemotePrincipal<crypto_sign::PublicKey>>> {
346
- let DeserializedKnownIds(known_ids) = serde_json::from_str(state).ok()?;
347
- Some(known_ids)
355
+ ) -> Option<(
356
+ crypto_sign::KeyPair,
357
+ u64,
358
+ u64,
359
+ HashMap<u64, RemotePrincipal<crypto_sign::PublicKey>>,
360
+ )> {
361
+ let ServerStateData {
362
+ identity_key,
363
+ identity_key_kid,
364
+ server_kid,
365
+ known_ids: DeserializedKnownIds(known_ids),
366
+ } = serde_json::from_str(state).ok()?;
367
+
368
+ if identity_key_kid > server_kid || known_ids.keys().any(|kid| *kid > server_kid) {
369
+ return None;
370
+ }
371
+
372
+ let keypair = crypto_sign::KeyPair::from_seed(identity_key.buffer.0.as_slice()).ok()?;
373
+
374
+ Some((keypair, identity_key_kid, server_kid, known_ids))
348
375
  }
349
376
 
350
377
  #[cfg(feature = "server")]
@@ -690,6 +717,7 @@ mod tests {
690
717
  #[test]
691
718
  fn type_identifiers_follow_the_undefined_zero_convention() {
692
719
  assert_eq!(u8::from(systems::Identifier::Undefined), 0);
720
+ assert_eq!(u8::from(systems::Identifier::Ed25519), 1);
693
721
  assert_eq!(u8::from(systems::Identifier::HkdfSha512), 6);
694
722
  assert_eq!(u8::from(systems::Identifier::Chacha20Poly1305Ietf), 7);
695
723
  assert!(matches!(
@@ -709,6 +737,7 @@ mod tests {
709
737
  (roles::Identifier::EncryptionRecvKey, 11),
710
738
  (roles::Identifier::SendNonce, 12),
711
739
  (roles::Identifier::RecvNonce, 13),
740
+ (roles::Identifier::IdentityKey, 14),
712
741
  ] {
713
742
  assert_eq!(u8::from(identifier), encoded);
714
743
  assert_eq!(roles::Identifier::from(encoded), identifier);
@@ -13,7 +13,8 @@ use crate::{beacon::ProviderBeacon, shared::encode_kem};
13
13
  use crate::{
14
14
  server::{ProviderServer, RegResponse, RegistrationOutput},
15
15
  shared::{
16
- REGISTRATION_WITNESS, decode_kem, decode_sign, deserialize_known_ids, serialize_known_ids,
16
+ REGISTRATION_WITNESS, decode_kem, decode_sign, deserialize_server_state,
17
+ serialize_server_state,
17
18
  },
18
19
  };
19
20
  use capnp::message::{ReaderOptions, TypedBuilder, TypedReader};
@@ -540,28 +541,23 @@ impl ProviderServer for BeaconCryptPqxdh {
540
541
  }
541
542
 
542
543
  fn export_state(&self) -> Option<String> {
543
- serialize_known_ids(&self.known_ids)
544
+ serialize_server_state(
545
+ &self.identity_key,
546
+ self.identity_key_kid,
547
+ self.server_kid,
548
+ &self.known_ids,
549
+ )
544
550
  }
545
551
 
546
- fn try_from_state(server_kid: u64, id_seed: Option<&[u8]>, server_state: &str) -> Option<Self> {
552
+ fn try_from_state(server_state: &str) -> Option<Self> {
547
553
  ensure_init().ok()?;
548
554
 
549
- let id_keypair = if let Some(seed) = id_seed {
550
- crypto_sign::KeyPair::from_seed(seed).ok()?
551
- } else {
552
- crypto_sign::KeyPair::generate().ok()?
553
- };
554
- let known_ids = deserialize_known_ids(server_state)?;
555
- let next_remote_kid = known_ids
556
- .keys()
557
- .max()
558
- .copied()
559
- .map_or(server_kid, |known_kid| server_kid.max(known_kid));
555
+ let (id_keypair, identity_key_kid, server_kid, known_ids) =
556
+ deserialize_server_state(server_state)?;
560
557
 
561
558
  Some(Self {
562
559
  identity_key: id_keypair,
563
- // this will be overwritten when the agent registers
564
- identity_key_kid: server_kid,
560
+ identity_key_kid,
565
561
  // the server doesn't use prekeys
566
562
  prekey: None,
567
563
  // only the beacon uses it, and it generated at registration time
@@ -570,7 +566,7 @@ impl ProviderServer for BeaconCryptPqxdh {
570
566
  pq_key: None,
571
567
  associated_data: None,
572
568
  is_beacon: false,
573
- server_kid: next_remote_kid,
569
+ server_kid,
574
570
  known_ids,
575
571
  })
576
572
  }