aws_lc_rs/aead/
chacha20_poly1305_openssh.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
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
// Copyright 2016 Brian Smith.
// SPDX-License-Identifier: ISC
// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC

//! The [chacha20-poly1305@openssh.com] AEAD-ish construct.
//!
//! This should only be used by SSH implementations. It has a similar, but
//! different API from `aws_lc_rs::aead` because the construct cannot use the same
//! API as `aws_lc_rs::aead` due to the way the construct handles the encrypted
//! packet length.
//!
//! The concatenation of a and b is denoted `a||b`. `K_1` and `K_2` are defined
//! in the [chacha20-poly1305@openssh.com] specification. `packet_length`,
//! `padding_length`, `payload`, and `random padding` are defined in
//! [RFC 4253]. The term `plaintext` is used as a shorthand for
//! `padding_length||payload||random padding`.
//!
//! [chacha20-poly1305@openssh.com]:
//!    http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?annotate=HEAD
//! [RFC 4253]: https://tools.ietf.org/html/rfc4253
//!
//! # FIPS
//! The APIs offered in this module must not be used.

use super::{poly1305, Nonce, Tag};
use crate::cipher::block::BLOCK_LEN;
use crate::cipher::chacha::{self, ChaCha20Key};
use crate::iv::FixedLength;
use crate::{constant_time, endian::BigEndian, error};

/// A key for sealing packets.
pub struct SealingKey {
    key: Key,
}

impl SealingKey {
    /// Constructs a new `SealingKey`.
    #[must_use]
    pub fn new(key_material: &[u8; KEY_LEN]) -> SealingKey {
        SealingKey {
            key: Key::new(key_material),
        }
    }

    /// Seals (encrypts and signs) a packet.
    ///
    /// On input, `plaintext_in_ciphertext_out` must contain the unencrypted
    /// `packet_length||plaintext` where `plaintext` is the
    /// `padding_length||payload||random padding`. It will be overwritten by
    /// `encrypted_packet_length||ciphertext`, where `encrypted_packet_length`
    /// is encrypted with `K_1` and `ciphertext` is encrypted by `K_2`.
    //
    // # FIPS
    // This method must not be used.
    #[inline]
    pub fn seal_in_place(
        &self,
        sequence_number: u32,
        plaintext_in_ciphertext_out: &mut [u8],
        tag_out: &mut [u8; TAG_LEN],
    ) {
        let nonce = make_nonce(sequence_number);
        let poly_key = derive_poly1305_key(&self.key.k_2, Nonce(FixedLength::from(nonce.as_ref())));

        {
            let (len_in_out, data_and_padding_in_out) =
                plaintext_in_ciphertext_out.split_at_mut(PACKET_LENGTH_LEN);

            self.key.k_1.encrypt_in_place(nonce.as_ref(), len_in_out, 0);
            self.key
                .k_2
                .encrypt_in_place(nonce.as_ref(), data_and_padding_in_out, 1);
        }

        let Tag(tag, tag_len) = poly1305::sign(poly_key, plaintext_in_ciphertext_out);
        debug_assert_eq!(TAG_LEN, tag_len);
        tag_out.copy_from_slice(tag.as_ref());
    }
}

/// A key for opening packets.
pub struct OpeningKey {
    key: Key,
}

impl OpeningKey {
    /// Constructs a new `OpeningKey`.
    #[must_use]
    pub fn new(key_material: &[u8; KEY_LEN]) -> OpeningKey {
        OpeningKey {
            key: Key::new(key_material),
        }
    }

    /// Returns the decrypted, but unauthenticated, packet length.
    ///
    /// Importantly, the result won't be authenticated until `open_in_place` is
    /// called.
    //
    // # FIPS
    // This method must not be used.
    #[inline]
    #[must_use]
    pub fn decrypt_packet_length(
        &self,
        sequence_number: u32,
        encrypted_packet_length: [u8; PACKET_LENGTH_LEN],
    ) -> [u8; PACKET_LENGTH_LEN] {
        let mut packet_length = encrypted_packet_length;
        let nonce = make_nonce(sequence_number);
        self.key
            .k_1
            .encrypt_in_place(nonce.as_ref(), &mut packet_length, 0);
        packet_length
    }

    /// Opens (authenticates and decrypts) a packet.
    ///
    /// `ciphertext_in_plaintext_out` must be of the form
    /// `encrypted_packet_length||ciphertext` where `ciphertext` is the
    /// encrypted `plaintext`. When the function succeeds the ciphertext is
    /// replaced by the plaintext and the result is `Ok(plaintext)`, where
    /// `plaintext` is `&ciphertext_in_plaintext_out[PACKET_LENGTH_LEN..]`;
    /// otherwise the contents of `ciphertext_in_plaintext_out` are unspecified
    /// and must not be used.
    ///
    /// # Errors
    /// `error::Unspecified` when ciphertext is invalid
    //
    // # FIPS
    // This method must not be used.
    #[inline]
    pub fn open_in_place<'a>(
        &self,
        sequence_number: u32,
        ciphertext_in_plaintext_out: &'a mut [u8],
        tag: &[u8; TAG_LEN],
    ) -> Result<&'a [u8], error::Unspecified> {
        let nonce = make_nonce(sequence_number);

        // We must verify the tag before decrypting so that
        // `ciphertext_in_plaintext_out` is unmodified if verification fails.
        // This is beyond what we guarantee.
        let poly_key = derive_poly1305_key(&self.key.k_2, Nonce(FixedLength::from(nonce.as_ref())));
        verify(poly_key, ciphertext_in_plaintext_out, tag)?;

        let plaintext_in_ciphertext_out = &mut ciphertext_in_plaintext_out[PACKET_LENGTH_LEN..];
        self.key
            .k_2
            .encrypt_in_place(nonce.as_ref(), plaintext_in_ciphertext_out, 1);

        Ok(plaintext_in_ciphertext_out)
    }
}

struct Key {
    k_1: ChaCha20Key,
    k_2: ChaCha20Key,
}

impl Key {
    fn new(key_material: &[u8; KEY_LEN]) -> Key {
        // The first half becomes K_2 and the second half becomes K_1.
        let (k_2, k_1) = key_material.split_at(chacha::KEY_LEN);
        let k_1: [u8; chacha::KEY_LEN] = k_1.try_into().unwrap();
        let k_2: [u8; chacha::KEY_LEN] = k_2.try_into().unwrap();
        Key {
            k_1: ChaCha20Key::from(k_1),
            k_2: ChaCha20Key::from(k_2),
        }
    }
}

#[inline]
fn make_nonce(sequence_number: u32) -> Nonce {
    Nonce::from(BigEndian::from(sequence_number))
}

/// The length of key.
pub const KEY_LEN: usize = chacha::KEY_LEN * 2;

/// The length in bytes of the `packet_length` field in a SSH packet.
pub const PACKET_LENGTH_LEN: usize = 4; // 32 bits

/// The length in bytes of an authentication tag.
pub const TAG_LEN: usize = BLOCK_LEN;

#[inline]
fn verify(key: poly1305::Key, msg: &[u8], tag: &[u8; TAG_LEN]) -> Result<(), error::Unspecified> {
    let Tag(calculated_tag, _) = poly1305::sign(key, msg);
    constant_time::verify_slices_are_equal(calculated_tag.as_ref(), tag)
}

#[inline]
#[allow(clippy::needless_pass_by_value)]
pub(super) fn derive_poly1305_key(chacha_key: &ChaCha20Key, nonce: Nonce) -> poly1305::Key {
    let mut key_bytes = [0u8; 2 * BLOCK_LEN];
    chacha_key.encrypt_in_place(nonce.as_ref(), &mut key_bytes, 0);
    poly1305::Key::new(key_bytes)
}

#[cfg(test)]
mod tests {
    use crate::aead::chacha20_poly1305_openssh::{
        derive_poly1305_key, OpeningKey, SealingKey, KEY_LEN, TAG_LEN,
    };
    use crate::aead::Nonce;
    use crate::cipher::chacha::ChaCha20Key;
    use crate::endian::{BigEndian, FromArray, LittleEndian};
    use crate::test;

    #[test]
    fn derive_poly1305_test() {
        let chacha_key =
            test::from_hex("98bef1469be7269837a45bfbc92a5a6ac762507cf96443bf33b96b1bd4c6f8f6")
                .unwrap();
        let expected_poly1305_key =
            test::from_hex("759de17d6d6258a436e36ecf75e3f00e4d9133ec05c4c855a9ec1a4e4e873b9d")
                .unwrap();
        let chacha_key = chacha_key.as_slice();
        let chacha_key_bytes: [u8; 32] = <[u8; 32]>::try_from(chacha_key).unwrap();
        let chacha_key = ChaCha20Key::from(chacha_key_bytes);
        {
            let iv = Nonce::from(&[45u32, 897, 4567]);
            let poly1305_key = derive_poly1305_key(&chacha_key, iv);
            assert_eq!(&expected_poly1305_key, &poly1305_key.key_and_nonce);
        }

        {
            let iv = Nonce::from(&LittleEndian::<u32>::from_array(&[45u32, 897, 4567]));
            let poly1305_key = derive_poly1305_key(&chacha_key, iv);
            assert_eq!(&expected_poly1305_key, &poly1305_key.key_and_nonce);
        }

        {
            let iv = Nonce::from(&BigEndian::<u32>::from_array(&[45u32, 897, 4567]));
            let poly1305_key = derive_poly1305_key(&chacha_key, iv);
            assert_ne!(&expected_poly1305_key, &poly1305_key.key_and_nonce);
        }
    }

    #[test]
    #[allow(clippy::cast_possible_truncation)]
    fn test_decrypt_packet_length() {
        let key_bytes: [u8; KEY_LEN] = test::from_dirty_hex("98bef1469be7269837a45bfbc92a5a6ac762\
        507cf96443bf33b96b1bd4c6f8f6759de17d6d6258a436e36ecf75e3f00e4d9133ec05c4c855a9ec1a4e4e873b9d")
            .try_into().unwrap();

        let sealing_key = SealingKey::new(&key_bytes);
        let opening_key = OpeningKey::new(&key_bytes);

        let plaintext = b"Hello World!";
        let packet_length = plaintext.len() as u32;
        let packet_length = packet_length.to_be_bytes();
        let mut in_out = Vec::new();

        in_out.extend_from_slice(&packet_length);
        in_out.extend_from_slice(plaintext);

        let mut tag = [0u8; TAG_LEN];
        sealing_key.seal_in_place(0, &mut in_out, &mut tag);

        let encrypted_length: [u8; 4] = in_out[0..4].to_owned().try_into().unwrap();
        let decrypted_length = opening_key.decrypt_packet_length(0, encrypted_length);
        let decrypted_length = u32::from_be_bytes(decrypted_length);
        assert_eq!(plaintext.len() as u32, decrypted_length);
    }

    #[test]
    fn test_types() {
        test::compile_time_assert_send::<OpeningKey>();
        test::compile_time_assert_sync::<OpeningKey>();

        test::compile_time_assert_send::<SealingKey>();
        test::compile_time_assert_sync::<SealingKey>();
    }
}