peat-btle 0.3.3

Bluetooth Low Energy mesh transport for Peat Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright (c) 2025-2026 (r)evolve - Revolve Team LLC
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Signed payload utilities for transport-agnostic message authentication
//!
//! Provides helpers for encoding and verifying signed messages that work
//! across transport layers (BLE, WiFi, IP-based networks).
//!
//! # Wire Format
//!
//! ```text
//! [marker:1][payload:N][signature:64]
//! ```
//!
//! - **marker**: Single byte identifying the message type
//! - **payload**: Variable-length application data (type-specific)
//! - **signature**: Ed25519 signature over (marker || payload)
//!
//! The signature covers both the marker and payload, binding the message
//! type to the content to prevent cross-protocol attacks.
//!
//! # Example
//!
//! ```
//! use peat_btle::security::{DeviceIdentity, SignedPayload};
//!
//! let identity = DeviceIdentity::generate();
//!
//! // Encode a signed message
//! let marker = 0xAF;
//! let payload = [0x01, 0x02, 0x03, 0x04];
//! let wire = SignedPayload::encode(marker, &payload, &identity);
//!
//! // Decode and verify
//! let pubkey = identity.public_key();
//! assert!(SignedPayload::verify(&wire, &pubkey));
//!
//! // Extract components
//! let decoded = SignedPayload::decode(&wire).unwrap();
//! assert_eq!(decoded.marker, marker);
//! assert_eq!(decoded.payload, &payload);
//! ```

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use super::identity::{verify_signature, DeviceIdentity};

/// Signature size in bytes (Ed25519)
pub const SIGNATURE_SIZE: usize = 64;

/// Minimum wire size: marker (1) + signature (64)
pub const MIN_WIRE_SIZE: usize = 1 + SIGNATURE_SIZE;

/// Decoded signed payload
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodedPayload<'a> {
    /// Message type marker
    pub marker: u8,
    /// Payload bytes (between marker and signature)
    pub payload: &'a [u8],
    /// Ed25519 signature
    pub signature: &'a [u8; 64],
}

/// Signed payload encoding and verification utilities
///
/// Transport-agnostic helpers for creating and verifying signed messages.
/// Used by peat-lite CannedMessage and Peat protocol messages.
pub struct SignedPayload;

impl SignedPayload {
    /// Encode a signed payload
    ///
    /// Creates wire format: `[marker:1][payload:N][signature:64]`
    ///
    /// The signature covers `marker || payload`, binding the message type
    /// to the content.
    ///
    /// # Arguments
    /// * `marker` - Message type identifier
    /// * `payload` - Application data to sign
    /// * `identity` - Signer's identity (holds private key)
    ///
    /// # Returns
    /// Wire bytes ready for transmission
    pub fn encode(marker: u8, payload: &[u8], identity: &DeviceIdentity) -> Vec<u8> {
        // Build message to sign: marker || payload
        let mut to_sign = Vec::with_capacity(1 + payload.len());
        to_sign.push(marker);
        to_sign.extend_from_slice(payload);

        // Sign
        let signature = identity.sign(&to_sign);

        // Build wire: marker || payload || signature
        let mut wire = Vec::with_capacity(1 + payload.len() + SIGNATURE_SIZE);
        wire.push(marker);
        wire.extend_from_slice(payload);
        wire.extend_from_slice(&signature);

        wire
    }

    /// Encode with pre-computed signature
    ///
    /// Use when the signature is computed externally (e.g., by secure enclave).
    ///
    /// # Arguments
    /// * `marker` - Message type identifier
    /// * `payload` - Application data
    /// * `signature` - Pre-computed Ed25519 signature over (marker || payload)
    pub fn encode_with_signature(marker: u8, payload: &[u8], signature: &[u8; 64]) -> Vec<u8> {
        let mut wire = Vec::with_capacity(1 + payload.len() + SIGNATURE_SIZE);
        wire.push(marker);
        wire.extend_from_slice(payload);
        wire.extend_from_slice(signature);
        wire
    }

    /// Decode a signed payload without verification
    ///
    /// Extracts marker, payload, and signature from wire format.
    /// Does NOT verify the signature - call `verify()` separately.
    ///
    /// # Arguments
    /// * `wire` - Wire bytes in format `[marker:1][payload:N][signature:64]`
    ///
    /// # Returns
    /// `Some(DecodedPayload)` if wire is at least 65 bytes, `None` otherwise
    pub fn decode(wire: &[u8]) -> Option<DecodedPayload<'_>> {
        if wire.len() < MIN_WIRE_SIZE {
            return None;
        }

        let marker = wire[0];
        let payload_end = wire.len() - SIGNATURE_SIZE;
        let payload = &wire[1..payload_end];

        // Safe because we checked length above
        let signature: &[u8; 64] = wire[payload_end..].try_into().ok()?;

        Some(DecodedPayload {
            marker,
            payload,
            signature,
        })
    }

    /// Verify a signed payload
    ///
    /// Checks that the signature is valid for the given public key.
    ///
    /// # Arguments
    /// * `wire` - Wire bytes in format `[marker:1][payload:N][signature:64]`
    /// * `public_key` - Signer's Ed25519 public key
    ///
    /// # Returns
    /// `true` if signature is valid, `false` otherwise
    pub fn verify(wire: &[u8], public_key: &[u8; 32]) -> bool {
        let Some(decoded) = Self::decode(wire) else {
            return false;
        };

        // Reconstruct signed message: marker || payload
        let signed_len = wire.len() - SIGNATURE_SIZE;
        let to_verify = &wire[..signed_len];

        verify_signature(public_key, to_verify, decoded.signature)
    }

    /// Decode and verify in one step
    ///
    /// Convenience method that decodes and verifies, returning the decoded
    /// payload only if verification succeeds.
    ///
    /// # Arguments
    /// * `wire` - Wire bytes
    /// * `public_key` - Expected signer's public key
    ///
    /// # Returns
    /// `Some(DecodedPayload)` if valid, `None` if malformed or signature invalid
    pub fn decode_verified<'a>(
        wire: &'a [u8],
        public_key: &[u8; 32],
    ) -> Option<DecodedPayload<'a>> {
        if !Self::verify(wire, public_key) {
            return None;
        }
        Self::decode(wire)
    }

    /// Get the payload size from total wire size
    ///
    /// Useful for pre-allocating buffers.
    #[inline]
    pub const fn payload_size(wire_size: usize) -> usize {
        wire_size.saturating_sub(MIN_WIRE_SIZE)
    }

    /// Get the wire size from payload size
    ///
    /// Useful for pre-allocating buffers.
    #[inline]
    pub const fn wire_size(payload_size: usize) -> usize {
        1 + payload_size + SIGNATURE_SIZE
    }

    /// Extract the marker byte without full decode
    ///
    /// Quick check for message type routing.
    #[inline]
    pub fn peek_marker(wire: &[u8]) -> Option<u8> {
        wire.first().copied()
    }

    /// Extract signature bytes without full verification
    ///
    /// Useful for caching or deferred verification.
    pub fn extract_signature(wire: &[u8]) -> Option<&[u8; 64]> {
        if wire.len() < MIN_WIRE_SIZE {
            return None;
        }
        let sig_start = wire.len() - SIGNATURE_SIZE;
        wire[sig_start..].try_into().ok()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_encode_decode_roundtrip() {
        let identity = DeviceIdentity::generate();
        let marker = 0xAF;
        let payload = [0x01, 0x02, 0x03, 0x04, 0x05];

        let wire = SignedPayload::encode(marker, &payload, &identity);

        // Check wire size
        assert_eq!(wire.len(), SignedPayload::wire_size(payload.len()));
        assert_eq!(wire.len(), 1 + 5 + 64);

        // Decode
        let decoded = SignedPayload::decode(&wire).unwrap();
        assert_eq!(decoded.marker, marker);
        assert_eq!(decoded.payload, &payload);
    }

    #[test]
    fn test_verify_valid_signature() {
        let identity = DeviceIdentity::generate();
        let marker = 0xAF;
        let payload = b"Hello, mesh!";

        let wire = SignedPayload::encode(marker, payload, &identity);
        let pubkey = identity.public_key();

        assert!(SignedPayload::verify(&wire, &pubkey));
    }

    #[test]
    fn test_verify_wrong_pubkey() {
        let identity1 = DeviceIdentity::generate();
        let identity2 = DeviceIdentity::generate();

        let wire = SignedPayload::encode(0xAF, b"test", &identity1);
        let wrong_pubkey = identity2.public_key();

        assert!(!SignedPayload::verify(&wire, &wrong_pubkey));
    }

    #[test]
    fn test_verify_tampered_payload() {
        let identity = DeviceIdentity::generate();
        let mut wire = SignedPayload::encode(0xAF, b"original", &identity);
        let pubkey = identity.public_key();

        // Tamper with payload
        wire[1] ^= 0xFF;

        assert!(!SignedPayload::verify(&wire, &pubkey));
    }

    #[test]
    fn test_verify_tampered_marker() {
        let identity = DeviceIdentity::generate();
        let mut wire = SignedPayload::encode(0xAF, b"test", &identity);
        let pubkey = identity.public_key();

        // Change marker
        wire[0] = 0xBF;

        assert!(!SignedPayload::verify(&wire, &pubkey));
    }

    #[test]
    fn test_decode_verified() {
        let identity = DeviceIdentity::generate();
        let marker = 0xAF;
        let payload = b"verified content";

        let wire = SignedPayload::encode(marker, payload, &identity);
        let pubkey = identity.public_key();

        let decoded = SignedPayload::decode_verified(&wire, &pubkey).unwrap();
        assert_eq!(decoded.marker, marker);
        assert_eq!(decoded.payload, payload);
    }

    #[test]
    fn test_decode_verified_fails_bad_sig() {
        let identity1 = DeviceIdentity::generate();
        let identity2 = DeviceIdentity::generate();

        let wire = SignedPayload::encode(0xAF, b"test", &identity1);
        let wrong_pubkey = identity2.public_key();

        assert!(SignedPayload::decode_verified(&wire, &wrong_pubkey).is_none());
    }

    #[test]
    fn test_empty_payload() {
        let identity = DeviceIdentity::generate();
        let marker = 0x00;
        let payload: &[u8] = &[];

        let wire = SignedPayload::encode(marker, payload, &identity);
        assert_eq!(wire.len(), MIN_WIRE_SIZE);

        let decoded = SignedPayload::decode(&wire).unwrap();
        assert_eq!(decoded.marker, marker);
        assert!(decoded.payload.is_empty());

        assert!(SignedPayload::verify(&wire, &identity.public_key()));
    }

    #[test]
    fn test_peek_marker() {
        let identity = DeviceIdentity::generate();
        let wire = SignedPayload::encode(0xAB, b"test", &identity);

        assert_eq!(SignedPayload::peek_marker(&wire), Some(0xAB));
        assert_eq!(SignedPayload::peek_marker(&[]), None);
    }

    #[test]
    fn test_extract_signature() {
        let identity = DeviceIdentity::generate();
        let wire = SignedPayload::encode(0xAF, b"test", &identity);

        let sig = SignedPayload::extract_signature(&wire).unwrap();
        assert_eq!(sig.len(), 64);

        // Too short
        assert!(SignedPayload::extract_signature(&[0x01; 10]).is_none());
    }

    #[test]
    fn test_encode_with_signature() {
        let identity = DeviceIdentity::generate();
        let marker = 0xAF;
        let payload = b"external sig";

        // Compute signature externally
        let mut to_sign = Vec::new();
        to_sign.push(marker);
        to_sign.extend_from_slice(payload);
        let signature = identity.sign(&to_sign);

        // Encode with pre-computed signature
        let wire = SignedPayload::encode_with_signature(marker, payload, &signature);

        // Should verify
        assert!(SignedPayload::verify(&wire, &identity.public_key()));
    }

    #[test]
    fn test_wire_size_calculation() {
        assert_eq!(SignedPayload::wire_size(0), 65);
        assert_eq!(SignedPayload::wire_size(21), 86); // CannedMessage size
        assert_eq!(SignedPayload::wire_size(100), 165);

        assert_eq!(SignedPayload::payload_size(65), 0);
        assert_eq!(SignedPayload::payload_size(86), 21);
        assert_eq!(SignedPayload::payload_size(165), 100);
    }

    #[test]
    fn test_canned_message_size() {
        // CannedMessage: [0xAF][msg_code:1][src:4][tgt:4][timestamp:8][seq:4] = 22 bytes unsigned
        // Signed: [0xAF][msg_code:1][src:4][tgt:4][timestamp:8][seq:4][signature:64] = 86 bytes
        let payload_size = 1 + 4 + 4 + 8 + 4; // 21 bytes (without marker)
        assert_eq!(SignedPayload::wire_size(payload_size), 86);
    }
}