pinenut-log 0.0.2

An extremely high performance logging system for clients (iOS, Android, Desktop), written in Rust.
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
//! Encryption & Decryption.

use thiserror::Error;

use crate::Sealed;

/// Errors that can be occurred during encryption or decryption.
#[derive(Error, Clone, Debug)]
pub enum Error {
    /// An error that occurs during padding or unpadding.
    #[error("padding error")]
    Padding,
    /// An error that occurs during ECDH.
    #[error("ECDH error")]
    Ecdh,
}

/// Errors that can be occurred during encryption.
pub type EncryptionError = Error;

/// Errors that can be occurred during decryption.
pub type DecryptionError = Error;

/// Represents the type of encryption keys.
///
/// `Pinenut` uses encryption keys of length 16 bytes (128 bits).
pub type EncryptionKey = [u8; 16];

/// Represents the length of the public key.
///
/// A public key is a compressed elliptic curve point.
/// With length: 1 byte (encoding tag) + 32 bytes (256 bits).
pub const PUBLIC_KEY_LEN: usize = 33;

/// Operation of encryption. Different values are used according to different flush
/// dimensions.
#[derive(Debug, Clone, Copy)]
pub(crate) enum EncryptOp<'a> {
    Input(&'a [u8]),
    Flush,
}

/// Represents a target for encrypted data or decrypted data.
pub(crate) trait Sink = crate::Sink<Error>;

/// Represents a block encryptor that encrypts data to its target (`Sink`).
pub(crate) trait Encryptor: Sealed {
    fn encrypt<S>(&mut self, operation: EncryptOp, sink: &mut S) -> Result<(), S::Error>
    where
        S: Sink;
}

/// Represents a block decryptor that decrypts data to its target (`Sink`).
pub(crate) trait Decryptor: Sealed {
    fn decrypt<S>(
        &mut self,
        input: &[u8],
        reached_to_end: bool,
        sink: &mut S,
    ) -> Result<(), S::Error>
    where
        S: Sink;
}

pub use ecdh::{gen_echd_key_pair, PublicKey, SecretKey};

/// `Elliptic Curve Diffie–Hellman (ECDH)` Support.
///
/// Using the NIST P-256 (a.k.a. secp256r1, prime256v1) elliptic curve.
pub(crate) mod ecdh {
    use std::mem;

    use p256::{ecdh::diffie_hellman, elliptic_curve};
    use rand_core::OsRng;

    use crate::encrypt::{EncryptionKey, Error, PUBLIC_KEY_LEN};

    /// Represents the type of secret keys.
    ///
    /// With length: 32 bytes (256 bits).
    pub type SecretKey = [u8; 32];

    /// Represents the type of public keys.
    ///
    /// A public key is a compressed elliptic curve point.
    /// With length: 1 byte (encoding tag) + 32 bytes (256 bits).
    pub type PublicKey = [u8; 33];

    /// The empty public key, it means no encryption.
    pub(crate) const EMPTY_PUBLIC_KEY: PublicKey = [0; PUBLIC_KEY_LEN];

    impl From<elliptic_curve::Error> for Error {
        #[inline]
        fn from(_: elliptic_curve::Error) -> Self {
            Self::Ecdh
        }
    }

    /// Generates the ECHD key pair.
    #[inline]
    pub fn gen_echd_key_pair() -> (SecretKey, PublicKey) {
        let secret_key = p256::SecretKey::random(&mut OsRng);
        let public_key = p256::EncodedPoint::from(secret_key.public_key()).compress();
        (secret_key.to_bytes().into(), public_key.as_bytes().try_into().unwrap())
    }

    /// Represents the public and symmetric keys generated in the initialization of
    /// the logger.
    pub(crate) struct Keys {
        /// Represents the public key passed to the consumer (viewer).
        pub(crate) public_key: PublicKey,
        /// Represents the symmetric key during log record encryption.
        pub(crate) encryption_key: EncryptionKey,
    }

    impl Keys {
        /// Constructs the `Keys` via Elliptic Curve Diffie-Hellman (ECDH).
        pub(crate) fn new(public_key: &PublicKey) -> Result<Self, Error> {
            let public_key = p256::PublicKey::from_sec1_bytes(public_key.as_ref())?;
            let secret_key = p256::SecretKey::random(&mut OsRng);

            let encryption_key =
                diffie_hellman(secret_key.to_nonzero_scalar(), public_key.as_affine());
            let encryption_key = encryption_key.raw_secret_bytes().as_slice()
                [..mem::size_of::<EncryptionKey>()]
                .try_into()
                .map_err(|_| Error::Ecdh)?;

            let public_key = p256::EncodedPoint::from(secret_key.public_key()).compress();
            let public_key = public_key.as_bytes().try_into().map_err(|_| Error::Ecdh)?;

            Ok(Self { public_key, encryption_key })
        }
    }

    /// Negotiates the symmetric key during log record encryption via Elliptic Curve
    /// Diffie-Hellman (ECDH).
    #[inline]
    pub(crate) fn ecdh_encryption_key(
        secret_key: &SecretKey,
        public_key: &PublicKey,
    ) -> Result<EncryptionKey, Error> {
        let secret_key = p256::SecretKey::from_slice(secret_key.as_ref())?;
        let public_key = p256::PublicKey::from_sec1_bytes(public_key.as_ref())?;

        let encryption_key = diffie_hellman(secret_key.to_nonzero_scalar(), public_key.as_affine());
        encryption_key.raw_secret_bytes().as_slice()[..mem::size_of::<EncryptionKey>()]
            .try_into()
            .map_err(|_| Error::Ecdh)
    }
}

pub(crate) use aes::{Decryptor as AesDecryptor, Encryptor as AesEncryptor};

/// `Encryptor` and `Decryptor` for the `AES 128` encryption, with `ECB` mode and
/// `PKCS#7` padding.
///
/// In this situation, we consider using `ECB` mode to trade off between performance
/// and security. Because the bytes of data are compressed before they are encrypted.
pub(crate) mod aes {
    use aes::{Aes128Dec, Aes128Enc};
    use cipher::{
        block_padding::{NoPadding, Pkcs7, UnpadError},
        inout::PadError,
        BlockDecrypt, BlockEncrypt, KeyInit,
    };

    use crate::{
        common::BytesBuf,
        encrypt::{
            Decryptor as DecryptorTrait, EncryptOp, EncryptionKey, Encryptor as EncryptorTrait,
            Error, Sink,
        },
        Sealed,
    };

    /// 128-bit AES block.
    const BLOCK_SIZE: usize = 16;

    impl From<PadError> for Error {
        #[inline]
        fn from(_: PadError) -> Self {
            Self::Padding
        }
    }

    impl From<UnpadError> for Error {
        #[inline]
        fn from(_: UnpadError) -> Self {
            Self::Padding
        }
    }

    /// The `AES` encryptor.
    pub(crate) struct Encryptor {
        inner: Aes128Enc,
        buffer: BytesBuf,
    }

    impl Encryptor {
        /// Should not be less than `BLOCK_SIZE`.
        ///
        /// Buffer of 256 bytes should be sufficient for encryption of a log.
        const BUFFER_LEN: usize = 16 * BLOCK_SIZE;

        /// Constructs a new `Encryptor` with encryption key.
        #[inline]
        pub(crate) fn new(key: &EncryptionKey) -> Self {
            let inner = Aes128Enc::new(key.into());
            let buffer = BytesBuf::with_capacity(Self::BUFFER_LEN);
            Self { inner, buffer }
        }
    }

    impl EncryptorTrait for Encryptor {
        fn encrypt<S>(&mut self, operation: EncryptOp, sink: &mut S) -> Result<(), S::Error>
        where
            S: Sink,
        {
            match operation {
                EncryptOp::Input(mut input) => {
                    while !input.is_empty() {
                        let buffered = self.buffer.buffer(input);
                        debug_assert_ne!(
                            buffered, 0,
                            "the size of buffer needs to be greater than or equal to `BLOCK_SIZE`"
                        );

                        self.buffer.sink(sink, false, |buf, len| {
                            self.inner.encrypt_padded::<NoPadding>(buf, len)
                        })?;

                        // The remaining input.
                        input = &input[buffered..];
                    }
                    Ok(())
                }
                EncryptOp::Flush => self
                    .buffer
                    .sink(sink, true, |buf, len| self.inner.encrypt_padded::<Pkcs7>(buf, len)),
            }
        }
    }

    impl Sealed for Encryptor {}

    /// The `AES` decryptor.
    pub(crate) struct Decryptor {
        inner: Aes128Dec,
        buffer: BytesBuf,
    }

    impl Decryptor {
        /// Should not be less than `BLOCK_SIZE`.
        ///
        /// Uses 1KB as the buffer length for decryption.
        const BUFFER_LEN: usize = 64 * BLOCK_SIZE;

        /// Constructs a new `Decryptor` with encryption key.
        #[inline]
        pub(crate) fn new(key: &EncryptionKey) -> Self {
            let inner = Aes128Dec::new(key.into());
            let buffer = BytesBuf::with_capacity(Self::BUFFER_LEN);
            Self { inner, buffer }
        }
    }

    impl DecryptorTrait for Decryptor {
        fn decrypt<S>(
            &mut self,
            mut input: &[u8],
            reached_to_end: bool,
            sink: &mut S,
        ) -> Result<(), S::Error>
        where
            S: Sink,
        {
            while !input.is_empty() {
                let buffered = self.buffer.buffer(input);
                debug_assert_ne!(
                    buffered, 0,
                    "the size of buffer needs to be greater than or equal to `BLOCK_SIZE`"
                );

                let reached_to_end = reached_to_end && buffered == input.len();
                self.buffer.sink(sink, reached_to_end, |buf, len| {
                    let buf = &mut buf[..len];
                    if reached_to_end {
                        self.inner.decrypt_padded::<Pkcs7>(buf)
                    } else {
                        self.inner.decrypt_padded::<NoPadding>(buf)
                    }
                })?;

                // The remaining input.
                input = &input[buffered..];
            }
            Ok(())
        }
    }

    impl Sealed for Decryptor {}

    impl BytesBuf {
        /// Handle the bytes of specified length, then writes them to the sink, and
        /// finally drains the buffer.
        fn sink<S, E>(
            &mut self,
            sink: &mut S,
            pad: bool,
            handle: impl FnOnce(&mut [u8], usize) -> Result<&[u8], E>,
        ) -> Result<(), S::Error>
        where
            S: Sink,
            E: Into<Error>,
        {
            let len = if pad { self.len() } else { self.len() / BLOCK_SIZE * BLOCK_SIZE };
            let buffer = self.as_buffer_mut_slice();

            let bytes = handle(buffer, len).map_err(Into::into)?;
            if !bytes.is_empty() {
                sink.sink(bytes)?;
            }
            self.drain(len);

            Ok(())
        }
    }
}

impl<T> Encryptor for Option<T>
where
    T: Encryptor,
{
    #[inline]
    fn encrypt<S>(&mut self, operation: EncryptOp, sink: &mut S) -> Result<(), S::Error>
    where
        S: Sink,
    {
        match self {
            Some(encryptor) => encryptor.encrypt(operation, sink),
            // Just writes its all input to the sink directly.
            None => match operation {
                EncryptOp::Input(bytes) => sink.sink(bytes),
                _ => Ok(()),
            },
        }
    }
}

impl<T> Decryptor for Option<T>
where
    T: Decryptor,
{
    #[inline]
    fn decrypt<S>(
        &mut self,
        input: &[u8],
        reached_to_end: bool,
        sink: &mut S,
    ) -> Result<(), S::Error>
    where
        S: Sink,
    {
        match self {
            Some(decryptor) => decryptor.decrypt(input, reached_to_end, sink),
            // Just writes its all input to the sink directly.
            None => sink.sink(input),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::slice;

    use crate::encrypt::{
        AesDecryptor, AesEncryptor, Decryptor, EncryptOp, EncryptionKey, Encryptor,
    };

    const KEY: EncryptionKey = [0x23; 16];

    fn aes_encrypt(input: &[u8]) -> Vec<u8> {
        let mut encryptor = AesEncryptor::new(&KEY);
        let mut sink = Vec::new();
        let mut sink_mul = Vec::new();

        // One time.
        encryptor.encrypt(EncryptOp::Input(input), &mut sink).unwrap();
        encryptor.encrypt(EncryptOp::Flush, &mut sink).unwrap();

        // Multiple times.
        for byte in input {
            encryptor.encrypt(EncryptOp::Input(slice::from_ref(byte)), &mut sink_mul).unwrap();
        }
        encryptor.encrypt(EncryptOp::Flush, &mut sink_mul).unwrap();

        assert_eq!(sink, sink_mul);
        sink
    }

    fn aes_decrypt(input: &[u8]) -> Vec<u8> {
        let mut decryptor = AesDecryptor::new(&KEY);
        let mut sink = Vec::new();
        let mut sink_mul = Vec::new();

        // One time.
        decryptor.decrypt(input, true, &mut sink).unwrap();

        // Multiple times.
        for (idx, byte) in input.iter().enumerate() {
            decryptor
                .decrypt(slice::from_ref(byte), idx == input.len() - 1, &mut sink_mul)
                .unwrap();
        }

        assert_eq!(sink, sink_mul);
        sink
    }

    #[test]
    fn test_aes() {
        // Short data
        let data = b"Hello World";
        assert_eq!(aes_decrypt(&aes_encrypt(data)), data);

        // 16 bytes data
        let data = b"123456789ABCDEFG";
        assert_eq!(aes_decrypt(&aes_encrypt(data)), data);

        // Long data
        let data = b"Hello, I'm Tangent, nice to meet you.";
        assert_eq!(aes_decrypt(&aes_encrypt(data)), data);
    }
}