1use num_bigint::{BigUint, RandBigInt};
2use std::ops::Shl;
3
4use crate::SshResult;
5
6use super::{HashType, KeyExchange};
7
8#[cfg(feature = "deprecated-dh-group1-sha1")]
9const GROUP1: [u8; 128] = [
10 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0xf, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34,
11 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x2, 0x4e, 0x8, 0x8a, 0x67, 0xcc, 0x74,
12 0x2, 0xb, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x8, 0x79, 0x8e, 0x34, 0x4, 0xdd,
13 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b, 0x30, 0x2b, 0xa, 0x6d, 0xf2, 0x5f, 0x14, 0x37,
14 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45, 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6,
15 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x37, 0xed, 0x6b, 0xb, 0xff, 0x5c, 0xb6, 0xf4, 0x6, 0xb7, 0xed,
16 0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5, 0xae, 0x9f, 0x24, 0x11, 0x7c, 0x4b, 0x1f, 0xe6,
17 0x49, 0x28, 0x66, 0x51, 0xec, 0xe6, 0x53, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18];
19
20const GROUP14: [u8; 256] = [
21 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0xf, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34,
22 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x2, 0x4e, 0x8, 0x8a, 0x67, 0xcc, 0x74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c, 0x52, 0xc9, 0xde, 0x2b, 0xcb, 0xf6, 0x95, 0x58, 0x17, 0x18,
35 0x39, 0x95, 0x49, 0x7c, 0xea, 0x95, 0x6a, 0xe5, 0x15, 0xd2, 0x26, 0x18, 0x98, 0xfa, 0x5, 0x10,
36 0x15, 0x72, 0x8e, 0x5a, 0x8a, 0xac, 0xaa, 0x68, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37];
38
39struct DhGroup {
40 prime: &'static [u8],
41 generator: usize,
42 exp_size: u64,
43}
44
45#[cfg(feature = "deprecated-dh-group1-sha1")]
46const DH_GROUP1: DhGroup = DhGroup {
47 prime: &GROUP1,
48 generator: 2,
49 exp_size: 256,
50};
51
52const DH_GROUP14: DhGroup = DhGroup {
53 prime: &GROUP14,
54 generator: 2,
55 exp_size: 256,
56};
57
58fn biguint_to_mpint(biguint: &BigUint) -> Vec<u8> {
59 let mut mpint = Vec::new();
60 let bytes = biguint.to_bytes_be();
61 if let Some(b) = bytes.first() {
62 if b > &0x7f {
63 mpint.push(0);
64 }
65 }
66 mpint.extend(&bytes);
67 mpint
68}
69
70macro_rules! create_dh_with_group {
71 ($name: ident, $group: expr, $hash: expr) => {
72 pub(super) struct $name {
73 prime: BigUint,
74 private_key: BigUint,
75 public_key: Vec<u8>,
76 }
77
78 impl KeyExchange for $name {
79 fn new() -> SshResult<Self> {
80 let mut rng = rand::thread_rng();
81 let group = &($group);
82 let prime = BigUint::from_bytes_be(&group.prime);
83 let private_key = rng.gen_biguint((group.exp_size * 8) - 2u64).shl(1);
84 let public_key =
85 biguint_to_mpint(&BigUint::from(group.generator).modpow(&private_key, &prime));
86
87 Ok(Self {
88 prime,
89 private_key,
90 public_key,
91 })
92 }
93
94 fn get_public_key(&self) -> &[u8] {
95 &self.public_key
96 }
97
98 fn get_shared_secret(&self, puk: Vec<u8>) -> SshResult<Vec<u8>> {
99 let other_pub = BigUint::from_bytes_be(&puk);
100 let shared_secret = other_pub.modpow(&self.private_key, &self.prime);
101 Ok(biguint_to_mpint(&shared_secret))
102 }
103
104 fn get_hash_type(&self) -> HashType {
105 $hash
106 }
107 }
108 };
109}
110
111#[cfg(feature = "deprecated-dh-group1-sha1")]
112create_dh_with_group!(DiffieHellmanGroup1Sha1, DH_GROUP1, HashType::SHA1);
113create_dh_with_group!(DiffieHellmanGroup14Sha1, DH_GROUP14, HashType::SHA1);
114create_dh_with_group!(DiffieHellmanGroup14Sha256, DH_GROUP14, HashType::SHA256);