revault_lockbox_api 0.0.59

reVault lockbox API to create and manage lockboxes
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use ed25519_dalek::{
    Signature as Ed25519Signature, Signer as Ed25519Signer, SigningKey as Ed25519SigningKey,
    Verifier as Ed25519Verifier, VerifyingKey as Ed25519VerifyingKey,
};
use ml_dsa::{
    Keypair, MlDsa65, Signature as MlDsaSignature, SignatureEncoding,
    SigningKey as MlDsaSigningKey, VerifyingKey as MlDsaVerifyingKey,
};
use std::fmt;
use zeroize::Zeroizing;

use crate::commit_auth::{
    CommitSignature, SIGNATURE_ALGORITHM_ED25519, SIGNATURE_ALGORITHM_ML_DSA_65,
};
use crate::secret_vec::SecretVec;
use crate::{Error, Result};

const SIGNING_PUBLIC_MAGIC: &[u8; 8] = b"LBX1SPUB";
const SIGNING_PRIVATE_MAGIC: &[u8; 8] = b"LBX1SPRV";
const SIGNING_KEY_VERSION: u16 = 1;
const SIGNING_ALGORITHM_ED25519_MLDSA65: u16 = 1;
const ED25519_SEED_LEN: usize = 32;
const ML_DSA_SEED_LEN: usize = 32;

/// Hybrid owner signing keypair used to authenticate writable commits.
///
/// The private record uses secure memory. Expanded signing keys use the
/// cryptographic libraries' zeroizing owners and are omitted from `Debug`.
pub struct OwnerSigningKeyPair {
    private_key_bytes: SecretVec,
    public_key: OwnerSigningPublicKey,
    ed25519: Ed25519SigningKey,
    ml_dsa65: MlDsaSigningKey<MlDsa65>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// Public verification key for owner-signed lockbox commits.
pub struct OwnerSigningPublicKey {
    ed25519_public_key: [u8; 32],
    ml_dsa65_public_key: Vec<u8>,
}

impl OwnerSigningKeyPair {
    /// Signs an application message using both algorithms. Callers must include
    /// their protocol domain, version and context in the message.
    #[must_use]
    pub fn sign_detached(&self, message: &[u8]) -> Vec<u8> {
        let mut out = Vec::new();
        for signature in self.sign(message) {
            put_bytes(&mut out, &signature.signature);
        }
        out
    }
    /// Stable public fingerprint used to identify the established archive owner.
    pub fn fingerprint(&self) -> Result<String> {
        crate::lockbox::owner_signature_fingerprint(&self.empty_signatures())
    }
    /// Generates a fresh Ed25519 and ML-DSA-65 hybrid signing keypair.
    pub fn generate() -> Result<Self> {
        let mut ed25519_seed = Zeroizing::new([0_u8; ED25519_SEED_LEN]);
        let mut ml_dsa65_seed = Zeroizing::new([0_u8; ML_DSA_SEED_LEN]);
        getrandom::fill(&mut *ed25519_seed).map_err(|err| Error::Io(err.to_string()))?;
        getrandom::fill(&mut *ml_dsa65_seed).map_err(|err| Error::Io(err.to_string()))?;
        Self::from_seeds(ed25519_seed, ml_dsa65_seed)
    }

    /// Restores a keypair from its secure private-key record.
    pub fn from_private_key_record(private_key_bytes: SecretVec) -> Result<Self> {
        let (ed25519_seed, ml_dsa65_seed) =
            private_key_bytes.with_bytes(decode_private_key_record)??;
        let mut keypair = Self::from_seeds(ed25519_seed, ml_dsa65_seed)?;
        keypair.private_key_bytes = private_key_bytes;
        Ok(keypair)
    }

    /// Exports an independent secure copy of the private-key record.
    pub fn private_key_record(&self) -> Result<SecretVec> {
        self.private_key_bytes.try_clone().map_err(Into::into)
    }

    /// Returns the public verification half of this keypair.
    pub fn public_key(&self) -> OwnerSigningPublicKey {
        self.public_key.clone()
    }

    /// Clone this signing keypair into a new secure allocation.
    pub fn try_clone(&self) -> Result<Self> {
        Self::from_private_key_record(self.private_key_record()?)
    }

    fn from_seeds(
        ed25519_seed: Zeroizing<[u8; ED25519_SEED_LEN]>,
        ml_dsa65_seed: Zeroizing<[u8; ML_DSA_SEED_LEN]>,
    ) -> Result<Self> {
        let ed25519 = Ed25519SigningKey::from_bytes(&ed25519_seed);
        let ml_dsa65_seed = Zeroizing::new(ml_dsa::Seed::from(*ml_dsa65_seed));
        let ml_dsa65 = MlDsaSigningKey::<MlDsa65>::from_seed(&ml_dsa65_seed);
        let public_key = OwnerSigningPublicKey {
            ed25519_public_key: ed25519.verifying_key().to_bytes(),
            ml_dsa65_public_key: ml_dsa65.verifying_key().encode().to_vec(),
        };
        let private_key_bytes = encode_private_key_record(&ed25519_seed, ml_dsa65_seed.as_ref())?;
        Ok(Self {
            private_key_bytes,
            public_key,
            ed25519,
            ml_dsa65,
        })
    }

    pub(crate) fn empty_signatures(&self) -> Vec<CommitSignature> {
        vec![
            CommitSignature {
                algorithm: SIGNATURE_ALGORITHM_ED25519,
                public_key: self.public_key.ed25519_public_key.to_vec(),
                signature: Vec::new(),
            },
            CommitSignature {
                algorithm: SIGNATURE_ALGORITHM_ML_DSA_65,
                public_key: self.public_key.ml_dsa65_public_key.clone(),
                signature: Vec::new(),
            },
        ]
    }

    pub(crate) fn sign(&self, message: &[u8]) -> Vec<CommitSignature> {
        let ed25519_signature = self.ed25519.sign(message);
        let ml_dsa65_signature = self.ml_dsa65.sign(message);
        vec![
            CommitSignature {
                algorithm: SIGNATURE_ALGORITHM_ED25519,
                public_key: self.public_key.ed25519_public_key.to_vec(),
                signature: ed25519_signature.to_bytes().to_vec(),
            },
            CommitSignature {
                algorithm: SIGNATURE_ALGORITHM_ML_DSA_65,
                public_key: self.public_key.ml_dsa65_public_key.clone(),
                signature: ml_dsa65_signature.to_bytes().to_vec(),
            },
        ]
    }
}

impl fmt::Debug for OwnerSigningKeyPair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("OwnerSigningKeyPair")
            .field("public_key", &self.public_key)
            .finish_non_exhaustive()
    }
}

impl OwnerSigningPublicKey {
    pub(crate) fn matches_signatures(&self, signatures: &[CommitSignature]) -> bool {
        signatures.len() == 2
            && signatures.iter().any(|s| {
                s.algorithm == SIGNATURE_ALGORITHM_ED25519
                    && s.public_key == self.ed25519_public_key
            })
            && signatures.iter().any(|s| {
                s.algorithm == SIGNATURE_ALGORITHM_ML_DSA_65
                    && s.public_key == self.ml_dsa65_public_key
            })
    }

    /// Verifies both signatures against this exact key, rejecting trailing bytes.
    ///
    /// # Errors
    /// Returns an error for malformed signatures or failure of either algorithm.
    pub fn verify_detached(&self, message: &[u8], signature: &[u8]) -> Result<()> {
        if signature.len() > 8192 {
            return Err(Error::CorruptRecord);
        }
        let mut reader = Reader::new(signature);
        let ed25519 = reader.bytes()?;
        let ml_dsa65 = reader.bytes()?;
        reader.done()?;
        verify_commit_signatures(
            message,
            &[
                CommitSignature {
                    algorithm: SIGNATURE_ALGORITHM_ED25519,
                    public_key: self.ed25519_public_key.to_vec(),
                    signature: ed25519,
                },
                CommitSignature {
                    algorithm: SIGNATURE_ALGORITHM_ML_DSA_65,
                    public_key: self.ml_dsa65_public_key.clone(),
                    signature: ml_dsa65,
                },
            ],
        )
    }
    #[cfg(test)]
    pub(crate) fn verify_publication_signatures(
        &self,
        message: &[u8],
        signatures: &[CommitSignature],
    ) -> Result<()> {
        for signature in signatures {
            let matches = match signature.algorithm {
                SIGNATURE_ALGORITHM_ED25519 => signature.public_key == self.ed25519_public_key,
                SIGNATURE_ALGORITHM_ML_DSA_65 => signature.public_key == self.ml_dsa65_public_key,
                _ => false,
            };
            if !matches {
                return Err(Error::CorruptRecord);
            }
        }
        verify_commit_signatures(message, signatures)
    }

    /// Decodes a versioned owner signing public-key record.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        decode_public_key(bytes)
    }

    /// Encodes this key as a versioned public-key record.
    pub fn to_bytes(&self) -> Vec<u8> {
        encode_public_key(self)
    }
}

pub(crate) fn verify_commit_signatures(
    message: &[u8],
    signatures: &[CommitSignature],
) -> Result<()> {
    if signatures.len() != 2 {
        return Err(Error::CorruptRecord);
    }
    let mut saw_ed25519 = false;
    let mut saw_ml_dsa65 = false;
    for signature in signatures {
        match signature.algorithm {
            SIGNATURE_ALGORITHM_ED25519 => {
                if saw_ed25519 {
                    return Err(Error::CorruptRecord);
                }
                let public_key = Ed25519VerifyingKey::from_bytes(
                    signature
                        .public_key
                        .as_slice()
                        .try_into()
                        .map_err(|_| Error::CorruptRecord)?,
                )
                .map_err(|_| Error::CorruptRecord)?;
                let signature = Ed25519Signature::from_slice(&signature.signature)
                    .map_err(|_| Error::CorruptRecord)?;
                public_key
                    .verify(message, &signature)
                    .map_err(|_| Error::CorruptRecord)?;
                saw_ed25519 = true;
            }
            SIGNATURE_ALGORITHM_ML_DSA_65 => {
                if saw_ml_dsa65 {
                    return Err(Error::CorruptRecord);
                }
                let public_key = MlDsaVerifyingKey::<MlDsa65>::decode(
                    &signature
                        .public_key
                        .as_slice()
                        .try_into()
                        .map_err(|_| Error::CorruptRecord)?,
                );
                let signature = MlDsaSignature::<MlDsa65>::try_from(signature.signature.as_slice())
                    .map_err(|_| Error::CorruptRecord)?;
                public_key
                    .verify(message, &signature)
                    .map_err(|_| Error::CorruptRecord)?;
                saw_ml_dsa65 = true;
            }
            _ => return Err(Error::CorruptRecord),
        }
    }
    if saw_ed25519 && saw_ml_dsa65 {
        Ok(())
    } else {
        Err(Error::CorruptRecord)
    }
}

pub(crate) fn commit_signature_keys_match(
    left: &[CommitSignature],
    right: &[CommitSignature],
) -> bool {
    left.len() == 2
        && right.len() == 2
        && left.iter().all(|signature| {
            right.iter().any(|candidate| {
                candidate.algorithm == signature.algorithm
                    && candidate.public_key == signature.public_key
            })
        })
}

pub(crate) fn commit_signatures_match_keypair(
    signatures: &[CommitSignature],
    keypair: &OwnerSigningKeyPair,
) -> bool {
    commit_signature_keys_match(signatures, &keypair.empty_signatures())
}

fn encode_public_key(key: &OwnerSigningPublicKey) -> Vec<u8> {
    let mut out = Vec::new();
    out.extend_from_slice(SIGNING_PUBLIC_MAGIC);
    put_u16(&mut out, SIGNING_KEY_VERSION);
    put_u16(&mut out, SIGNING_ALGORITHM_ED25519_MLDSA65);
    out.extend_from_slice(&key.ed25519_public_key);
    put_bytes(&mut out, &key.ml_dsa65_public_key);
    out
}

fn decode_public_key(bytes: &[u8]) -> Result<OwnerSigningPublicKey> {
    let mut reader = Reader::new(bytes);
    reader.magic(SIGNING_PUBLIC_MAGIC)?;
    reader.version()?;
    reader.algorithm()?;
    let ed25519_public_key = reader.array32()?;
    let ml_dsa65_public_key = reader.bytes()?;
    reader.done()?;
    Ok(OwnerSigningPublicKey {
        ed25519_public_key,
        ml_dsa65_public_key,
    })
}

fn encode_private_key_record(
    ed25519_seed: &[u8; ED25519_SEED_LEN],
    ml_dsa65_seed: &[u8],
) -> Result<SecretVec> {
    let mut out = SecretVec::new();
    out.try_extend_from_slice(SIGNING_PRIVATE_MAGIC)?;
    out.try_extend_from_slice(&SIGNING_KEY_VERSION.to_le_bytes())?;
    out.try_extend_from_slice(&SIGNING_ALGORITHM_ED25519_MLDSA65.to_le_bytes())?;
    out.try_extend_from_slice(ed25519_seed)?;
    out.try_extend_from_slice(&(ml_dsa65_seed.len() as u32).to_le_bytes())?;
    out.try_extend_from_slice(ml_dsa65_seed)?;
    Ok(out)
}

type SigningSeeds = (
    Zeroizing<[u8; ED25519_SEED_LEN]>,
    Zeroizing<[u8; ML_DSA_SEED_LEN]>,
);

fn decode_private_key_record(bytes: &[u8]) -> Result<SigningSeeds> {
    let mut reader = Reader::new(bytes);
    reader.magic(SIGNING_PRIVATE_MAGIC)?;
    reader.version()?;
    reader.algorithm()?;
    let ed25519_seed = Zeroizing::new(reader.array32()?);
    let ml_dsa65_seed = Zeroizing::new(reader.array32_bytes()?);
    reader.done()?;
    Ok((ed25519_seed, ml_dsa65_seed))
}

fn put_u16(out: &mut Vec<u8>, value: u16) {
    out.extend_from_slice(&value.to_le_bytes());
}

fn put_bytes(out: &mut Vec<u8>, bytes: &[u8]) {
    out.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
    out.extend_from_slice(bytes);
}

struct Reader<'a> {
    bytes: &'a [u8],
    offset: usize,
}

impl<'a> Reader<'a> {
    fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, offset: 0 }
    }

    fn magic(&mut self, expected: &[u8]) -> Result<()> {
        if self.bytes.get(self.offset..self.offset + expected.len()) != Some(expected) {
            return Err(Error::InvalidKeyMaterial(
                "owner signing key has invalid magic".to_string(),
            ));
        }
        self.offset += expected.len();
        Ok(())
    }

    fn version(&mut self) -> Result<()> {
        let version = self.u16()?;
        if version != SIGNING_KEY_VERSION {
            return Err(Error::InvalidKeyMaterial(format!(
                "owner signing key version {version} is not supported"
            )));
        }
        Ok(())
    }

    fn algorithm(&mut self) -> Result<()> {
        let algorithm = self.u16()?;
        if algorithm != SIGNING_ALGORITHM_ED25519_MLDSA65 {
            return Err(Error::InvalidKeyMaterial(format!(
                "owner signing key algorithm {algorithm} is not supported"
            )));
        }
        Ok(())
    }

    fn u16(&mut self) -> Result<u16> {
        let bytes = self.take(2)?;
        Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
    }

    fn bytes(&mut self) -> Result<Vec<u8>> {
        let bytes = self.take(4)?;
        let len = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize;
        Ok(self.take(len)?.to_vec())
    }

    fn array32(&mut self) -> Result<[u8; 32]> {
        let bytes = self.take(32)?;
        let mut out = [0_u8; 32];
        out.copy_from_slice(bytes);
        Ok(out)
    }

    fn array32_bytes(&mut self) -> Result<[u8; 32]> {
        let bytes = self.bytes()?;
        bytes.try_into().map_err(|_| {
            Error::InvalidKeyMaterial("owner signing key seed has the wrong length".to_string())
        })
    }

    fn take(&mut self, len: usize) -> Result<&'a [u8]> {
        let end = self.offset.checked_add(len).ok_or_else(|| {
            Error::InvalidKeyMaterial("owner signing key length overflow".to_string())
        })?;
        let slice = self.bytes.get(self.offset..end).ok_or_else(|| {
            Error::InvalidKeyMaterial("owner signing key is truncated".to_string())
        })?;
        self.offset = end;
        Ok(slice)
    }

    fn done(&self) -> Result<()> {
        if self.offset == self.bytes.len() {
            Ok(())
        } else {
            Err(Error::InvalidKeyMaterial(
                "owner signing key has trailing bytes".to_string(),
            ))
        }
    }
}

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

    #[test]
    fn hybrid_signature_verification_rejects_missing_algorithm() {
        let signer = OwnerSigningKeyPair::generate().unwrap();
        let mut signatures = signer.sign(b"commit");
        signatures.pop();

        assert!(matches!(
            verify_commit_signatures(b"commit", &signatures),
            Err(Error::CorruptRecord)
        ));
    }

    #[test]
    fn hybrid_signature_verification_rejects_duplicate_algorithm() {
        let signer = OwnerSigningKeyPair::generate().unwrap();
        let mut signatures = signer.sign(b"commit");
        signatures[1] = signatures[0].clone();

        assert!(matches!(
            verify_commit_signatures(b"commit", &signatures),
            Err(Error::CorruptRecord)
        ));
    }
}