sheave_core/readers/
encryption_algorithm.rs

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
use std::{
    future::Future,
    io::Result as IOResult,
    pin::Pin,
    task::{
        Context as FutureContext,
        Poll
    }
};
use futures::ready;
use tokio::io::{
    AsyncRead,
    ReadBuf
};
use crate::handshake::EncryptionAlgorithm;

#[doc(hidden)]
#[derive(Debug)]
pub struct EncryptionAlgorithmReader<'a, R: AsyncRead> {
    reader: Pin<&'a mut R>
}

#[doc(hidden)]
impl<R: AsyncRead> Future for EncryptionAlgorithmReader<'_, R> {
    type Output = IOResult<EncryptionAlgorithm>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut FutureContext<'_>) -> Poll<Self::Output> {
        let mut encryption_algorithm_byte: [u8; 1] = [0; 1];
        let mut buf = ReadBuf::new(&mut encryption_algorithm_byte);
        ready!(self.reader.as_mut().poll_read(cx, &mut buf))?;
        Poll::Ready(Ok(u8::from_be_bytes(encryption_algorithm_byte).into()))
    }
}

/// Reads one byte to indicate the encryption algorithm from streams.
///
/// # Examples
///
/// ```rust
/// use std::{
///     io::Result as IOResult,
///     pin::pin
/// };
/// use sheave_core::{
///     handshake::EncryptionAlgorithm::*,
///     readers::read_encryption_algorithm
/// };
///
/// #[tokio::main]
/// async fn main() -> IOResult<()> {
///     let reader: [u8; 1] = [3; 1];
///     let result = read_encryption_algorithm(pin!(reader.as_slice())).await?;
///     assert_eq!(NotEncrypted, result);
///     Ok(())
/// }
/// ```
pub fn read_encryption_algorithm<R: AsyncRead>(reader: Pin<&mut R>) -> EncryptionAlgorithmReader<'_, R> {
    EncryptionAlgorithmReader { reader }
}

#[cfg(test)]
mod tests {
    use std::pin::pin;
    use crate::handshake::EncryptionAlgorithm::*;
    use super::*;

    #[tokio::test]
    async fn read_not_encrypted() {
        let reader: [u8; 1] = [3];
        let result = read_encryption_algorithm(pin!(reader.as_slice())).await;
        assert!(result.is_ok());
        assert_eq!(NotEncrypted, result.unwrap())
    }

    #[tokio::test]
    async fn read_diffie_hellman() {
        let reader: [u8; 1] = [6];
        let result = read_encryption_algorithm(pin!(reader.as_slice())).await;
        assert!(result.is_ok());
        assert_eq!(DiffieHellman, result.unwrap())
    }

    #[tokio::test]
    async fn read_xtea() {
        let reader: [u8; 1] = [8];
        let result = read_encryption_algorithm(pin!(reader.as_slice())).await;
        assert!(result.is_ok());
        assert_eq!(Xtea, result.unwrap())
    }

    #[tokio::test]
    async fn read_blowfish() {
        let reader: [u8; 1] = [9];
        let result = read_encryption_algorithm(pin!(reader.as_slice())).await;
        assert!(result.is_ok());
        assert_eq!(Blowfish, result.unwrap())
    }

    #[tokio::test]
    async fn read_other() {
        let reader: [u8; 1] = [0];
        let result = read_encryption_algorithm(pin!(reader.as_slice())).await;
        assert!(result.is_ok());
        assert_eq!(Other, result.unwrap())
    }
}