Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / crypto_provider_rustcrypto / src / ecdsa.rs
ecdsa.rs147 lines · 4.5 KiBRaw
| 1 | // Copyright 2025 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // |
| 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | // |
| 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | // |
| 17 | // See the License for the specific language governing permissions and |
| 18 | // |
| 19 | // limitations under the License. |
| 20 | // |
| 21 | // |
| 22 | |
| 23 | use crypto_provider::ecdsa::{ |
| 24 | InvalidSignatureBytes, P256EcdsaProvider, VerifyError, PUBLIC_KEY_LENGTH, |
| 25 | }; |
| 26 | use p256::ecdsa::signature::{Signer, Verifier}; |
| 27 | #[cfg(feature = "std")] |
| 28 | use p256::elliptic_curve::Generate; |
| 29 | |
| 30 | pub struct P256Ecdsa; |
| 31 | |
| 32 | impl P256EcdsaProvider for P256Ecdsa { |
| 33 | type P256KeyPair = P256SigningKey; |
| 34 | |
| 35 | type P256PublicKey = P256VerifyingKey; |
| 36 | |
| 37 | type P256Signature = P256Signature; |
| 38 | } |
| 39 | |
| 40 | pub struct P256SigningKey(p256::ecdsa::SigningKey); |
| 41 | |
| 42 | impl crypto_provider::ecdsa::KeyPair for P256SigningKey { |
| 43 | type P256PublicKey = P256VerifyingKey; |
| 44 | |
| 45 | type P256Signature = P256Signature; |
| 46 | |
| 47 | fn raw_private_key( |
| 48 | &self, |
| 49 | _permit: &crypto_provider::ecdsa::RawPrivateKeyPermit, |
| 50 | ) -> crypto_provider::ecdsa::RawP256PrivateKey { |
| 51 | self.0.to_bytes().into() |
| 52 | } |
| 53 | |
| 54 | #[allow(clippy::unwrap_used)] |
| 55 | fn from_raw_private_key( |
| 56 | bytes: &crypto_provider::ecdsa::RawP256PrivateKey, |
| 57 | _permit: &crypto_provider::ecdsa::RawPrivateKeyPermit, |
| 58 | ) -> Self |
| 59 | where |
| 60 | Self: Sized, |
| 61 | { |
| 62 | // Unwrap is safe here, as the bytes for a private key can be anything. |
| 63 | // We size check the raw bytes beforehand, so there is no failure. |
| 64 | let signing_key = p256::ecdsa::SigningKey::from_slice(bytes).unwrap(); |
| 65 | Self(signing_key) |
| 66 | } |
| 67 | |
| 68 | fn sign(&self, msg: &[u8]) -> Self::P256Signature { |
| 69 | P256Signature(self.0.sign(msg)) |
| 70 | } |
| 71 | |
| 72 | #[cfg(feature = "std")] |
| 73 | fn generate() -> Self { |
| 74 | let mut csprng = rand::rngs::ThreadRng::default(); |
| 75 | Self(p256::ecdsa::SigningKey::generate_from_rng(&mut csprng)) |
| 76 | } |
| 77 | |
| 78 | #[allow(clippy::clone_on_copy)] |
| 79 | fn public_key(&self) -> Self::P256PublicKey { |
| 80 | P256VerifyingKey(*self.0.verifying_key()) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub struct P256VerifyingKey(p256::ecdsa::VerifyingKey); |
| 85 | |
| 86 | impl crypto_provider::ecdsa::PublicKey for P256VerifyingKey { |
| 87 | type Signature = P256Signature; |
| 88 | |
| 89 | #[allow(clippy::unwrap_used)] |
| 90 | fn from_bytes( |
| 91 | bytes: &crypto_provider::ecdsa::RawP256PublicKey, |
| 92 | ) -> Result<Self, crypto_provider::ecdsa::InvalidPublicKeyBytes> |
| 93 | where |
| 94 | Self: Sized, |
| 95 | { |
| 96 | let _encoded_point = p256::Sec1Point::from_bytes(bytes) |
| 97 | .map_err(|_| crypto_provider::ecdsa::InvalidPublicKeyBytes)?; |
| 98 | // Unwrap is safe here, as we've verified during construction that the public key is |
| 99 | // of the correct size and that it is a point on the curve, so this cannot fail. |
| 100 | let verifying_key = p256::ecdsa::VerifyingKey::from_sec1_bytes(bytes.as_ref()).unwrap(); |
| 101 | Ok(Self(verifying_key)) |
| 102 | } |
| 103 | |
| 104 | fn to_bytes(&self) -> crypto_provider::ecdsa::RawP256PublicKey { |
| 105 | let mut public_key = [0u8; PUBLIC_KEY_LENGTH]; |
| 106 | let point = self.0.to_sec1_point(false); |
| 107 | public_key.copy_from_slice(point.as_bytes()); |
| 108 | public_key |
| 109 | } |
| 110 | |
| 111 | fn verify_strict( |
| 112 | &self, |
| 113 | message: &[u8], |
| 114 | signature: &Self::Signature, |
| 115 | ) -> Result<(), VerifyError> { |
| 116 | self.0.verify(message, &signature.0).map_err(|_| VerifyError) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | pub struct P256Signature(p256::ecdsa::Signature); |
| 121 | |
| 122 | impl crypto_provider::ecdsa::Signature for P256Signature { |
| 123 | fn from_bytes( |
| 124 | bytes: &crypto_provider::ecdsa::RawP256Signature, |
| 125 | ) -> Result<Self, InvalidSignatureBytes> { |
| 126 | p256::ecdsa::Signature::from_der(bytes).map(Self).map_err(|_| InvalidSignatureBytes) |
| 127 | } |
| 128 | |
| 129 | #[allow(clippy::unwrap_used)] |
| 130 | fn to_bytes(&self) -> crypto_provider::ecdsa::RawP256Signature { |
| 131 | // Unwrap is safe here as the maximum size of a DER-serialized key is 72 bytes. |
| 132 | // Since we check the size of the signature on initialization from bytes, we don't |
| 133 | // need a check here and this cannot fail. |
| 134 | self.0.to_der().as_bytes().try_into().unwrap() |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #[cfg(test)] |
| 139 | mod test { |
| 140 | use super::P256Ecdsa; |
| 141 | use crypto_provider_test::ecdsa::*; |
| 142 | |
| 143 | #[test] |
| 144 | fn p256_ecdsa_tests() { |
| 145 | run_wycheproof_test_vectors::<P256Ecdsa>(EcdsaTestName::EcdsaSecp256r1Sha256); |
| 146 | } |
| 147 | } |