Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / crypto_provider_rustcrypto / src / p256.rs
p256.rs143 lines · 4.8 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 | |
| 15 | use crate::RcRng; |
| 16 | use core::marker::PhantomData; |
| 17 | use crypto_provider::{ |
| 18 | elliptic_curve::{EcdhProvider, EphemeralSecret}, |
| 19 | p256::{PointCompression, P256}, |
| 20 | tinyvec::ArrayVec, |
| 21 | }; |
| 22 | use p256::{ |
| 23 | elliptic_curve, |
| 24 | elliptic_curve::sec1::{FromSec1Point, Sec1Point, ToSec1Point}, |
| 25 | elliptic_curve::Generate, |
| 26 | NistP256, |
| 27 | }; |
| 28 | use rand::{Rng, SeedableRng}; |
| 29 | use rand_core::CryptoRng; |
| 30 | |
| 31 | /// Implementation of NIST-P256 using RustCrypto crates. |
| 32 | pub struct P256Ecdh<R> { |
| 33 | _marker: PhantomData<R>, |
| 34 | } |
| 35 | |
| 36 | impl<R: CryptoRng + SeedableRng + Rng + Send> EcdhProvider<P256> for P256Ecdh<R> { |
| 37 | type PublicKey = P256PublicKey; |
| 38 | type EphemeralSecret = P256EphemeralSecret<R>; |
| 39 | type SharedSecret = [u8; 32]; |
| 40 | } |
| 41 | |
| 42 | /// A NIST-P256 public key. |
| 43 | #[derive(Debug, PartialEq, Eq)] |
| 44 | pub struct P256PublicKey(p256::PublicKey); |
| 45 | impl crypto_provider::p256::P256PublicKey for P256PublicKey { |
| 46 | type Error = elliptic_curve::Error; |
| 47 | |
| 48 | fn from_sec1_bytes(bytes: &[u8]) -> Result<Self, Self::Error> { |
| 49 | p256::PublicKey::from_sec1_bytes(bytes).map(Self) |
| 50 | } |
| 51 | |
| 52 | fn to_sec1_bytes(&self, point_compression: PointCompression) -> ArrayVec<[u8; 65]> { |
| 53 | let mut bytes = ArrayVec::<[u8; 65]>::new(); |
| 54 | bytes.extend_from_slice( |
| 55 | self.0.to_sec1_point(point_compression == PointCompression::Compressed).as_bytes(), |
| 56 | ); |
| 57 | bytes |
| 58 | } |
| 59 | |
| 60 | #[allow(clippy::expect_used)] |
| 61 | fn to_affine_coordinates(&self) -> Result<([u8; 32], [u8; 32]), Self::Error> { |
| 62 | let p256_key = self.0.to_sec1_point(false); |
| 63 | let x: &[u8; 32] = |
| 64 | p256_key.x().expect("Generated key should not be on identity point").as_ref(); |
| 65 | let y: &[u8; 32] = |
| 66 | p256_key.y().expect("Generated key should not be on identity point").as_ref(); |
| 67 | Ok((*x, *y)) |
| 68 | } |
| 69 | fn from_affine_coordinates(x: &[u8; 32], y: &[u8; 32]) -> Result<Self, Self::Error> { |
| 70 | let key_option: Option<p256::PublicKey> = p256::PublicKey::from_sec1_point( |
| 71 | &Sec1Point::<NistP256>::from_affine_coordinates(x.into(), y.into(), false), |
| 72 | ) |
| 73 | .into(); |
| 74 | key_option.map(Self).ok_or(elliptic_curve::Error) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Ephemeral secrect for use in a P256 Diffie-Hellman |
| 79 | pub struct P256EphemeralSecret<R: CryptoRng + SeedableRng + Rng> { |
| 80 | secret: p256::ecdh::EphemeralSecret, |
| 81 | _marker: PhantomData<R>, |
| 82 | } |
| 83 | |
| 84 | impl<R: CryptoRng + SeedableRng + Rng + Send> EphemeralSecret<P256> for P256EphemeralSecret<R> { |
| 85 | type Impl = P256Ecdh<R>; |
| 86 | type Error = sec1::Error; |
| 87 | type Rng = RcRng<R>; |
| 88 | type EncodedPublicKey = ArrayVec<[u8; 65]>; |
| 89 | |
| 90 | fn generate_random(rng: &mut Self::Rng) -> Self { |
| 91 | Self { |
| 92 | secret: p256::ecdh::EphemeralSecret::generate_from_rng(&mut rng.0), |
| 93 | _marker: Default::default(), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn public_key_bytes(&self) -> Self::EncodedPublicKey { |
| 98 | let mut bytes = Self::EncodedPublicKey::new(); |
| 99 | bytes.extend_from_slice(self.secret.public_key().to_sec1_point(false).as_bytes()); |
| 100 | bytes |
| 101 | } |
| 102 | |
| 103 | fn diffie_hellman( |
| 104 | self, |
| 105 | other_pub: &P256PublicKey, |
| 106 | ) -> Result<<Self::Impl as EcdhProvider<P256>>::SharedSecret, Self::Error> { |
| 107 | let shared_secret = p256::ecdh::EphemeralSecret::diffie_hellman(&self.secret, &other_pub.0); |
| 108 | let bytes: <Self::Impl as EcdhProvider<P256>>::SharedSecret = |
| 109 | (*shared_secret.raw_secret_bytes()).into(); |
| 110 | Ok(bytes) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[cfg(test)] |
| 115 | impl<R: CryptoRng + SeedableRng + Rng + Send> |
| 116 | crypto_provider_test::elliptic_curve::EphemeralSecretForTesting<P256> |
| 117 | for P256EphemeralSecret<R> |
| 118 | { |
| 119 | fn from_private_components( |
| 120 | private_bytes: &[u8; 32], |
| 121 | _public_key: &P256PublicKey, |
| 122 | ) -> Result<Self, Self::Error> { |
| 123 | Ok(Self { |
| 124 | secret: p256::ecdh::EphemeralSecret::generate_from_rng( |
| 125 | &mut crate::testing::MockCryptoRng { values: private_bytes.iter() }, |
| 126 | ), |
| 127 | _marker: Default::default(), |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #[cfg(test)] |
| 133 | mod tests { |
| 134 | use super::P256Ecdh; |
| 135 | use core::marker::PhantomData; |
| 136 | use crypto_provider_test::p256::*; |
| 137 | use rand::rngs::StdRng; |
| 138 | |
| 139 | #[apply(p256_test_cases)] |
| 140 | fn p256_tests(testcase: CryptoProviderTestCase<P256Ecdh<StdRng>>, _name: &str) { |
| 141 | testcase(PhantomData::<P256Ecdh<StdRng>>) |
| 142 | } |
| 143 | } |