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

Modern-Apps.git / third_party / betocore / crypto_provider_rustcrypto / src / x25519.rs

x25519.rs119 lines · 3.7 KiBRaw
1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::RcRng;
16use core::marker::PhantomData;
17use crypto_provider::elliptic_curve::{EcdhProvider, EphemeralSecret, PublicKey};
18use crypto_provider::x25519::X25519;
19use rand::Rng;
20use rand_core::{CryptoRng, SeedableRng};
21
22/// The RustCrypto implementation of X25519 ECDH.
23pub struct X25519Ecdh<R> {
24 _marker: PhantomData<R>,
25}
26
27impl<R: CryptoRng + Rng + SeedableRng + Send> EcdhProvider<X25519> for X25519Ecdh<R> {
28 type PublicKey = X25519PublicKey;
29 type EphemeralSecret = X25519EphemeralSecret<R>;
30 type SharedSecret = [u8; 32];
31}
32
33/// A X25519 ephemeral secret used for Diffie-Hellman.
34pub struct X25519EphemeralSecret<R: CryptoRng + Rng + SeedableRng> {
35 secret: x25519_dalek::EphemeralSecret,
36 marker: PhantomData<R>,
37}
38
39impl<R: CryptoRng + Rng + SeedableRng + Send> EphemeralSecret<X25519> for X25519EphemeralSecret<R> {
40 type Impl = X25519Ecdh<R>;
41 type Error = Error;
42 type Rng = RcRng<R>;
43 type EncodedPublicKey = [u8; 32];
44
45 fn generate_random(rng: &mut Self::Rng) -> Self {
46 Self {
47 secret: x25519_dalek::EphemeralSecret::random_from_rng(&mut rng.0),
48 marker: Default::default(),
49 }
50 }
51
52 fn public_key_bytes(&self) -> Self::EncodedPublicKey {
53 let pubkey: x25519_dalek::PublicKey = (&self.secret).into();
54 pubkey.to_bytes()
55 }
56
57 fn diffie_hellman(
58 self,
59 other_pub: &<X25519Ecdh<R> as EcdhProvider<X25519>>::PublicKey,
60 ) -> Result<<X25519Ecdh<R> as EcdhProvider<X25519>>::SharedSecret, Self::Error> {
61 Ok(x25519_dalek::EphemeralSecret::diffie_hellman(self.secret, &other_pub.0).to_bytes())
62 }
63}
64
65#[cfg(test)]
66impl<R: CryptoRng + Rng + SeedableRng + Send>
67 crypto_provider_test::elliptic_curve::EphemeralSecretForTesting<X25519>
68 for X25519EphemeralSecret<R>
69{
70 fn from_private_components(
71 private_bytes: &[u8; 32],
72 _public_key: &X25519PublicKey,
73 ) -> Result<Self, Self::Error> {
74 Ok(Self {
75 secret: x25519_dalek::EphemeralSecret::random_from_rng(crate::testing::MockCryptoRng {
76 values: private_bytes.iter(),
77 }),
78 marker: Default::default(),
79 })
80 }
81}
82
83/// A X25519 public key.
84#[derive(Debug, PartialEq, Eq)]
85pub struct X25519PublicKey(x25519_dalek::PublicKey);
86
87impl PublicKey<X25519> for X25519PublicKey {
88 type Error = Error;
89 type EncodedPublicKey = [u8; 32];
90
91 fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Error> {
92 let byte_sized: [u8; 32] = bytes.try_into().map_err(|_| Error::WrongSize)?;
93 Ok(Self(byte_sized.into()))
94 }
95
96 fn to_bytes(&self) -> Self::EncodedPublicKey {
97 self.0.to_bytes()
98 }
99}
100
101/// Error type for the RustCrypto implementation of x25519.
102#[derive(Debug)]
103pub enum Error {
104 /// Unexpected size for the given input.
105 WrongSize,
106}
107
108#[cfg(test)]
109mod tests {
110 use super::X25519Ecdh;
111 use core::marker::PhantomData;
112 use crypto_provider_test::x25519::*;
113 use rand::rngs::StdRng;
114
115 #[apply(x25519_test_cases)]
116 fn x25519_tests(testcase: CryptoProviderTestCase<X25519Ecdh<StdRng>>) {
117 testcase(PhantomData::<X25519Ecdh<StdRng>>)
118 }
119}