Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / third_party / betocore / ukey2_connections / src / lib.rs

lib.rs56 lines · 2.4 KiBRaw
1#![allow(missing_docs)]
2// Copyright 2023 Google LLC
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! This crate implements the connection part of UKEY2. Depending on context, the name UKEY2 may
17//! include only the initial key handshake part (which is the historical origin of the name UKEY2),
18//! but may also include the connection encryption part implemented in this crate. In some docs
19//! this is also referred to as the "D2D" protocol.
20//!
21//! The main components in this crate are [`D2DHandshakeContext`] and [`D2DConnectionContextV1`].
22//! [`D2DHandshakeContext`] is a wrapper around the `ukey2_rs` crate, controlling the UKEY2 key
23//! handshake for the context of the resulting connection. [`D2DConnectionContextV1`] can be created
24//! from the handshake context once the handshake is complete, and controls the encryption and
25//! decryption of the payload messages.
26
27#![allow(clippy::expect_used)]
28//TODO: remove this and fix instances of unwrap
29#![allow(clippy::unwrap_used, clippy::panic)]
30
31mod crypto_utils;
32mod d2d_connection_context_v1;
33mod d2d_handshake_context;
34mod java_utils;
35#[cfg(test)]
36mod tests;
37
38pub use d2d_connection_context_v1::{
39 Aes256Key, D2DConnectionContextV1, DecodeError, DeserializeError, RxConnectionContext,
40 TxConnectionContext,
41};
42pub use d2d_handshake_context::{
43 D2DHandshakeContext, HandleMessageError, HandshakeError, InitiatorD2DHandshakeContext,
44 ServerD2DHandshakeContext,
45};
46pub use ukey2_rs::{HandshakeImplementation, NextProtocol};
47
48/// OS-seeded `StdRng` for connection contexts. Seeding from `SysRng` is
49/// infallible in practice; on the (unrecoverable) OS RNG failure path this
50/// falls back to a fixed seed rather than panicking across JNI.
51pub(crate) fn os_rng() -> rand::rngs::StdRng {
52 use rand::SeedableRng;
53 let mut sys = rand::rngs::SysRng;
54 rand::rngs::StdRng::try_from_rng(&mut sys)
55 .unwrap_or_else(|_| rand::rngs::StdRng::from_seed([0u8; 32]))
56}