veilid-core 0.5.3

Core library used to create a Veilid node and operate it as part of an application
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
pub mod sizes;

use super::*;
use argon2::password_hash::Salt;
use data_encoding::BASE64URL_NOPAD;
use digest::rand_core::RngCore;
use digest::Digest;
const NONE_AEAD_OVERHEAD: usize = NONE_PUBLIC_KEY_LENGTH;
pub const CRYPTO_KIND_NONE: CryptoKind = CryptoKind::new(*b"NONE");
pub const CRYPTO_KIND_NONE_FOURCC: u32 = u32::from_be_bytes(*b"NONE");
pub use sizes::*;

pub fn none_generate_keypair() -> KeyPair {
    let mut csprng = VeilidRng {};
    let mut pub_bytes = [0u8; NONE_PUBLIC_KEY_LENGTH];
    let mut sec_bytes = [0u8; NONE_SECRET_KEY_LENGTH];
    csprng.fill_bytes(&mut pub_bytes);
    for n in 0..NONE_PUBLIC_KEY_LENGTH {
        sec_bytes[n] = !pub_bytes[n];
    }
    let dht_key = BarePublicKey::new(&pub_bytes);
    let dht_key_secret = BareSecretKey::new(&sec_bytes);
    KeyPair::new(CRYPTO_KIND_NONE, BareKeyPair::new(dht_key, dht_key_secret))
}

fn do_xor_32(a: &[u8], b: &[u8]) -> VeilidAPIResult<[u8; 32]> {
    if a.len() != 32 || b.len() != 32 {
        apibail_generic!("wrong key length");
    }
    let mut out = [0u8; 32];
    for n in 0..32 {
        out[n] = a[n] ^ b[n];
    }
    Ok(out)
}

fn do_xor_inplace(a: &mut [u8], key: &[u8]) -> VeilidAPIResult<()> {
    if a.len() != 32 || key.is_empty() {
        apibail_generic!("wrong key length");
    }
    for n in 0..a.len() {
        a[n] ^= key[n % key.len()];
    }

    Ok(())
}

fn do_xor_b2b(a: &[u8], b: &mut [u8], key: &[u8]) -> VeilidAPIResult<()> {
    if a.len() != 32 || b.len() != 32 || key.is_empty() {
        apibail_generic!("wrong key length");
    }

    for n in 0..a.len() {
        b[n] = a[n] ^ key[n % key.len()];
    }

    Ok(())
}

fn is_bytes_eq_32(a: &[u8], v: u8) -> VeilidAPIResult<bool> {
    if a.len() != 32 {
        apibail_generic!("wrong key length");
    }

    for n in 0..32 {
        if a[n] != v {
            return Ok(false);
        }
    }
    Ok(true)
}

/// None CryptoSystem
pub(crate) struct CryptoSystemNONE {
    registry: VeilidComponentRegistry,
}

impl CryptoSystemNONE {
    #[must_use]
    pub(crate) fn new(registry: VeilidComponentRegistry) -> Self {
        Self { registry }
    }
}

impl CryptoSystem for CryptoSystemNONE {
    // Accessors
    fn kind(&self) -> CryptoKind {
        CRYPTO_KIND_NONE
    }

    fn crypto(&self) -> VeilidComponentGuard<'_, Crypto> {
        self.registry.lookup::<Crypto>().unwrap_or_log()
    }

    // Cached Operations
    fn cached_dh(&self, key: &PublicKey, secret: &SecretKey) -> VeilidAPIResult<SharedSecret> {
        self.crypto()
            .cached_dh_internal::<CryptoSystemNONE>(self, key, secret)
    }

    // Generation
    fn random_bytes(&self, len: usize) -> Vec<u8> {
        let mut bytes = unsafe { unaligned_u8_vec_uninit(len) };
        random_bytes(bytes.as_mut());
        bytes
    }
    fn hash_password(&self, password: &[u8], salt: &[u8]) -> VeilidAPIResult<String> {
        if salt.len() < Salt::MIN_LENGTH || salt.len() > Salt::MAX_LENGTH {
            apibail_generic!("invalid salt length");
        }
        Ok(format!(
            "{}:{}",
            BASE64URL_NOPAD.encode(salt),
            BASE64URL_NOPAD.encode(password)
        ))
    }
    fn verify_password(&self, password: &[u8], password_hash: &str) -> VeilidAPIResult<bool> {
        let Some((salt, _)) = password_hash.split_once(":") else {
            apibail_generic!("invalid format");
        };
        let Ok(salt) = BASE64URL_NOPAD.decode(salt.as_bytes()) else {
            apibail_generic!("invalid salt");
        };
        Ok(self.hash_password(password, &salt)? == password_hash)
    }

    fn derive_shared_secret(&self, password: &[u8], salt: &[u8]) -> VeilidAPIResult<SharedSecret> {
        if salt.len() < Salt::MIN_LENGTH || salt.len() > Salt::MAX_LENGTH {
            apibail_generic!("invalid salt length");
        }
        Ok(SharedSecret::new(
            CRYPTO_KIND_NONE,
            BareSharedSecret::new(
                blake3::hash(self.hash_password(password, salt)?.as_bytes()).as_bytes(),
            ),
        ))
    }

    fn random_nonce(&self) -> Nonce {
        let mut nonce = [0u8; NONE_NONCE_LENGTH];
        random_bytes(&mut nonce);
        Nonce::new(&nonce)
    }

    fn random_shared_secret(&self) -> SharedSecret {
        let mut s = [0u8; NONE_SHARED_SECRET_LENGTH];
        random_bytes(&mut s);
        SharedSecret::new(CRYPTO_KIND_NONE, BareSharedSecret::new(&s))
    }
    fn compute_dh(&self, key: &PublicKey, secret: &SecretKey) -> VeilidAPIResult<SharedSecret> {
        let s = do_xor_32(key.ref_value(), secret.ref_value())?;
        Ok(SharedSecret::new(
            CRYPTO_KIND_NONE,
            BareSharedSecret::new(&s),
        ))
    }
    fn generate_keypair(&self) -> KeyPair {
        none_generate_keypair()
    }
    fn generate_hash(&self, data: &[u8]) -> HashDigest {
        HashDigest::new(
            CRYPTO_KIND_NONE,
            BareHashDigest::new(blake3::hash(data).as_bytes()),
        )
    }
    fn generate_hash_reader(&self, reader: &mut dyn std::io::Read) -> VeilidAPIResult<PublicKey> {
        let mut hasher = blake3::Hasher::new();
        std::io::copy(reader, &mut hasher).map_err(VeilidAPIError::generic)?;
        Ok(PublicKey::new(
            CRYPTO_KIND_NONE,
            BarePublicKey::new(hasher.finalize().as_bytes()),
        ))
    }

    // Validation
    fn default_salt_length(&self) -> usize {
        4
    }
    fn shared_secret_length(&self) -> usize {
        NONE_SHARED_SECRET_LENGTH
    }
    fn nonce_length(&self) -> usize {
        NONE_NONCE_LENGTH
    }
    fn hash_digest_length(&self) -> usize {
        NONE_HASH_DIGEST_LENGTH
    }
    fn aead_overhead(&self) -> usize {
        NONE_AEAD_OVERHEAD
    }
    fn public_key_length(&self) -> usize {
        NONE_PUBLIC_KEY_LENGTH
    }
    fn secret_key_length(&self) -> usize {
        NONE_SECRET_KEY_LENGTH
    }
    fn signature_length(&self) -> usize {
        NONE_SIGNATURE_LENGTH
    }

    fn validate_keypair(
        &self,
        public_key: &PublicKey,
        secret_key: &SecretKey,
    ) -> VeilidAPIResult<bool> {
        self.check_public_key(public_key)?;
        self.check_secret_key(secret_key)?;

        let data = vec![0u8; 512];
        let Ok(sig) = self.sign(public_key, secret_key, &data) else {
            return Ok(false);
        };
        let Ok(v) = self.verify(public_key, &data, &sig) else {
            return Ok(false);
        };
        Ok(v)
    }
    fn validate_hash(&self, data: &[u8], hash_digest: &HashDigest) -> VeilidAPIResult<bool> {
        self.check_hash_digest(hash_digest)?;
        let out_hash = blake3::hash(data);
        let bytes = out_hash.as_bytes();
        Ok(*bytes == **hash_digest.ref_value())
    }
    fn validate_hash_reader(
        &self,
        reader: &mut dyn std::io::Read,
        hash_digest: &HashDigest,
    ) -> VeilidAPIResult<bool> {
        self.check_hash_digest(hash_digest)?;
        let mut hasher = blake3::Hasher::new();
        std::io::copy(reader, &mut hasher).map_err(VeilidAPIError::generic)?;
        let out_hash = hasher.finalize();
        let bytes = out_hash.as_bytes();
        Ok(*bytes == **hash_digest.ref_value())
    }

    // Authentication
    fn sign(
        &self,
        public_key: &PublicKey,
        secret_key: &SecretKey,
        data: &[u8],
    ) -> VeilidAPIResult<Signature> {
        self.check_public_key(public_key)?;
        self.check_secret_key(secret_key)?;

        if !is_bytes_eq_32(
            &do_xor_32(public_key.ref_value(), secret_key.ref_value())?,
            0xFFu8,
        )? {
            return Err(VeilidAPIError::parse_error(
                "Keypair is invalid",
                "invalid keys",
            ));
        }

        let mut dig = Blake3Digest512::new();
        dig.update(data);
        let sig = dig.finalize();
        let in_sig_bytes: [u8; NONE_SIGNATURE_LENGTH] = sig.into();
        let mut sig_bytes = [0u8; NONE_SIGNATURE_LENGTH];
        sig_bytes[0..32].copy_from_slice(&in_sig_bytes[0..32]);
        sig_bytes[32..64]
            .copy_from_slice(&do_xor_32(&in_sig_bytes[32..64], secret_key.ref_value())?);
        let none_sig = Signature::new(CRYPTO_KIND_NONE, BareSignature::new(&sig_bytes));
        Ok(none_sig)
    }

    fn sign_in_place(
        &self,
        public_key: &PublicKey,
        secret_key: &SecretKey,
        data: &mut [u8],
        range: Range<usize>,
        sig_idx: usize,
    ) -> VeilidAPIResult<()> {
        self.check_public_key(public_key)?;
        self.check_secret_key(secret_key)?;

        let mut dig = Blake3Digest512::new();
        dig.update(
            data.get(range)
                .ok_or_else(|| VeilidAPIError::internal("range is out of bounds"))?,
        );
        let sig = dig.finalize();
        let in_sig_bytes: [u8; NONE_SIGNATURE_LENGTH] = sig.into();

        data[sig_idx..sig_idx + 32].copy_from_slice(&in_sig_bytes[0..32]);
        data[sig_idx + 32..sig_idx + 64]
            .copy_from_slice(&do_xor_32(&in_sig_bytes[32..64], secret_key.ref_value())?);

        Ok(())
    }

    fn verify(
        &self,
        public_key: &PublicKey,
        data: &[u8],
        signature: &Signature,
    ) -> VeilidAPIResult<bool> {
        self.check_public_key(public_key)?;
        self.check_signature(signature)?;

        let mut dig = Blake3Digest512::new();
        dig.update(data);
        let sig = dig.finalize();
        let in_sig_bytes: [u8; NONE_SIGNATURE_LENGTH] = sig.into();
        let mut verify_bytes = [0u8; NONE_SIGNATURE_LENGTH];
        verify_bytes[0..32].copy_from_slice(&do_xor_32(
            &in_sig_bytes[0..32],
            &signature.ref_value()[0..32],
        )?);
        verify_bytes[32..64].copy_from_slice(&do_xor_32(
            &in_sig_bytes[32..64],
            &signature.ref_value()[32..64],
        )?);

        if !is_bytes_eq_32(&verify_bytes[0..32], 0u8)? {
            return Ok(false);
        }
        if !is_bytes_eq_32(
            &do_xor_32(&verify_bytes[32..64], public_key.ref_value())?,
            0xFFu8,
        )? {
            return Ok(false);
        }

        Ok(true)
    }

    fn verify_in_place(
        &self,
        public_key: &PublicKey,
        data: &[u8],
        range: Range<usize>,
        sig_idx: usize,
    ) -> VeilidAPIResult<bool> {
        self.check_public_key(public_key)?;

        let mut dig = Blake3Digest512::new();
        dig.update(
            data.get(range)
                .ok_or_else(|| VeilidAPIError::internal("range is out of bounds"))?,
        );
        let sig = dig.finalize();
        let in_sig_bytes: [u8; NONE_SIGNATURE_LENGTH] = sig.into();
        let mut verify_bytes = [0u8; NONE_SIGNATURE_LENGTH];
        let signature = data
            .get(sig_idx..sig_idx + NONE_SIGNATURE_LENGTH)
            .ok_or_else(|| VeilidAPIError::internal("signature index is out of bounds"))?;
        verify_bytes[0..32].copy_from_slice(&do_xor_32(&in_sig_bytes[0..32], &signature[0..32])?);
        verify_bytes[32..64]
            .copy_from_slice(&do_xor_32(&in_sig_bytes[32..64], &signature[32..64])?);

        if !is_bytes_eq_32(&verify_bytes[0..32], 0u8)? {
            return Ok(false);
        }
        if !is_bytes_eq_32(
            &do_xor_32(&verify_bytes[32..64], public_key.ref_value())?,
            0xFFu8,
        )? {
            return Ok(false);
        }

        Ok(true)
    }

    // AEAD Encrypt/Decrypt
    fn decrypt_in_place_aead(
        &self,
        body: &mut dyn CryptoSystemBuffer,
        nonce: &Nonce,
        shared_secret: &SharedSecret,
        _associated_data: Option<&[u8]>,
    ) -> VeilidAPIResult<()> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut blob = nonce.to_vec();
        blob.extend_from_slice(&[0u8; 8]);
        let blob = do_xor_32(&blob, shared_secret.ref_value())?;

        if body.len() < NONE_AEAD_OVERHEAD {
            return Err(VeilidAPIError::generic("invalid length"));
        }
        if body.as_ref()[body.len() - NONE_AEAD_OVERHEAD..] != blob {
            return Err(VeilidAPIError::generic("invalid keyblob"));
        }
        body.truncate(body.len() - NONE_AEAD_OVERHEAD);
        do_xor_inplace(body.as_mut(), &blob)
    }

    fn decrypt_aead(
        &self,
        body: &[u8],
        nonce: &Nonce,
        shared_secret: &SharedSecret,
        associated_data: Option<&[u8]>,
    ) -> VeilidAPIResult<Vec<u8>> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut out = body.to_vec();
        self.decrypt_in_place_aead(&mut out, nonce, shared_secret, associated_data)
            .map_err(map_to_string)
            .map_err(VeilidAPIError::generic)?;
        Ok(out)
    }

    fn encrypt_in_place_aead(
        &self,
        body: &mut dyn CryptoSystemBuffer,
        nonce: &Nonce,
        shared_secret: &SharedSecret,
        _associated_data: Option<&[u8]>,
    ) -> VeilidAPIResult<()> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut blob = nonce.to_vec();
        blob.extend_from_slice(&[0u8; 8]);
        let blob = do_xor_32(&blob, shared_secret.ref_value())?;
        do_xor_inplace(body.as_mut(), &blob)?;
        body.extend_from_slice(&blob);
        Ok(())
    }

    fn encrypt_aead(
        &self,
        body: &[u8],
        nonce: &Nonce,
        shared_secret: &SharedSecret,
        associated_data: Option<&[u8]>,
    ) -> VeilidAPIResult<Vec<u8>> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut out = body.to_vec();
        self.encrypt_in_place_aead(&mut out, nonce, shared_secret, associated_data)
            .map_err(map_to_string)
            .map_err(VeilidAPIError::generic)?;
        Ok(out)
    }

    // NoAuth Encrypt/Decrypt
    fn crypt_in_place_no_auth(
        &self,
        body: &mut [u8],
        nonce: &Nonce,
        shared_secret: &SharedSecret,
    ) -> VeilidAPIResult<()> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut blob = nonce.to_vec();
        blob.extend_from_slice(&[0u8; 8]);
        let blob = do_xor_32(&blob, shared_secret.ref_value())?;
        do_xor_inplace(body, &blob)
    }

    fn crypt_b2b_no_auth(
        &self,
        in_buf: &[u8],
        out_buf: &mut [u8],
        nonce: &Nonce,
        shared_secret: &SharedSecret,
    ) -> VeilidAPIResult<()> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut blob = nonce.to_vec();
        blob.extend_from_slice(&[0u8; 8]);
        let blob = do_xor_32(&blob, shared_secret.ref_value())?;
        do_xor_b2b(in_buf, out_buf, &blob)
    }

    fn crypt_no_auth_aligned_8(
        &self,
        in_buf: &[u8],
        nonce: &Nonce,
        shared_secret: &SharedSecret,
    ) -> VeilidAPIResult<Vec<u8>> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut out_buf = unsafe { aligned_8_u8_vec_uninit(in_buf.len()) };
        self.crypt_b2b_no_auth(in_buf, &mut out_buf, nonce, shared_secret)?;
        Ok(out_buf)
    }

    fn crypt_no_auth_unaligned(
        &self,
        in_buf: &[u8],
        nonce: &Nonce,
        shared_secret: &SharedSecret,
    ) -> VeilidAPIResult<Vec<u8>> {
        self.check_nonce(nonce)?;
        self.check_shared_secret(shared_secret)?;

        let mut out_buf = unsafe { unaligned_u8_vec_uninit(in_buf.len()) };
        self.crypt_b2b_no_auth(in_buf, &mut out_buf, nonce, shared_secret)?;
        Ok(out_buf)
    }
}