-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a trait for crypto, porting EC first (#606)
* Adds a trait for crypto, porting EC first * Moves crypto implementation next to its trait * Renames constants and types
- Loading branch information
1 parent
80b82ff
commit c168141
Showing
25 changed files
with
880 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use super::EC_FIELD_SIZE; | ||
use rng256::Rng256; | ||
|
||
/// Container for all ECDH cryptographic material. | ||
pub trait Ecdh { | ||
type SecretKey: SecretKey<PublicKey = Self::PublicKey, SharedSecret = Self::SharedSecret>; | ||
type PublicKey: PublicKey; | ||
type SharedSecret: SharedSecret; | ||
} | ||
|
||
/// ECDH ephemeral key. | ||
pub trait SecretKey { | ||
type PublicKey: PublicKey; | ||
type SharedSecret: SharedSecret; | ||
|
||
/// Generates a new random secret key. | ||
fn random(rng: &mut impl Rng256) -> Self; | ||
|
||
/// Computes the corresponding public key for this private key. | ||
fn public_key(&self) -> Self::PublicKey; | ||
|
||
/// Computes the shared secret when using Elliptic-curve Diffie–Hellman. | ||
fn diffie_hellman(&self, public_key: &Self::PublicKey) -> Self::SharedSecret; | ||
} | ||
|
||
/// ECDH public key. | ||
pub trait PublicKey: Sized { | ||
/// Creates a public key from its coordinates. | ||
fn from_coordinates(x: &[u8; EC_FIELD_SIZE], y: &[u8; EC_FIELD_SIZE]) -> Option<Self>; | ||
|
||
/// Writes the public key coordinates into the passed in parameters. | ||
fn to_coordinates(&self, x: &mut [u8; EC_FIELD_SIZE], y: &mut [u8; EC_FIELD_SIZE]); | ||
} | ||
|
||
/// ECDH shared secret. | ||
pub trait SharedSecret { | ||
/// Exports the x component of the point computed by Diffie–Hellman. | ||
fn raw_secret_bytes(&self) -> [u8; EC_FIELD_SIZE]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use super::{EC_FIELD_SIZE, EC_SIGNATURE_SIZE}; | ||
use alloc::vec::Vec; | ||
use rng256::Rng256; | ||
|
||
/// Container for all ECDSA cryptographic material. | ||
pub trait Ecdsa { | ||
type SecretKey: SecretKey<PublicKey = Self::PublicKey, Signature = Self::Signature>; | ||
type PublicKey: PublicKey<Signature = Self::Signature>; | ||
type Signature: Signature; | ||
} | ||
|
||
/// ECDSA signing key. | ||
pub trait SecretKey: Sized { | ||
type PublicKey: PublicKey; | ||
type Signature: Signature; | ||
|
||
/// Generates a new random secret key. | ||
fn random(rng: &mut impl Rng256) -> Self; | ||
|
||
/// Creates a signing key from its representation in bytes. | ||
fn from_slice(bytes: &[u8; EC_FIELD_SIZE]) -> Option<Self>; | ||
|
||
/// Computes the corresponding public key for this private key. | ||
fn public_key(&self) -> Self::PublicKey; | ||
|
||
/// Signs the message. | ||
/// | ||
/// For hashing, SHA256 is used implicitly. | ||
fn sign(&self, message: &[u8]) -> Self::Signature; | ||
|
||
/// Writes the signing key bytes into the passed in parameter. | ||
fn to_slice(&self, bytes: &mut [u8; EC_FIELD_SIZE]); | ||
} | ||
|
||
/// ECDSA verifying key. | ||
pub trait PublicKey: Sized { | ||
type Signature: Signature; | ||
|
||
/// Creates a public key from its coordinates. | ||
fn from_coordinates(x: &[u8; EC_FIELD_SIZE], y: &[u8; EC_FIELD_SIZE]) -> Option<Self>; | ||
|
||
/// Verifies if the signature matches the message. | ||
/// | ||
/// For hashing, SHA256 is used implicitly. | ||
fn verify(&self, message: &[u8], signature: &Self::Signature) -> bool; | ||
|
||
/// Writes the public key coordinates into the passed in parameters. | ||
fn to_coordinates(&self, x: &mut [u8; EC_FIELD_SIZE], y: &mut [u8; EC_FIELD_SIZE]); | ||
} | ||
|
||
/// ECDSA signature. | ||
pub trait Signature: Sized { | ||
/// Creates a signature from its affine coordinates, represented as concatenated bytes. | ||
fn from_slice(bytes: &[u8; EC_SIGNATURE_SIZE]) -> Option<Self>; | ||
|
||
/// Encodes the signatures as ASN1 DER. | ||
fn to_der(&self) -> Vec<u8>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
pub mod ecdh; | ||
pub mod ecdsa; | ||
#[cfg(feature = "rust_crypto")] | ||
pub mod rust_crypto; | ||
#[cfg(not(feature = "rust_crypto"))] | ||
pub mod software_crypto; | ||
#[cfg(feature = "rust_crypto")] | ||
pub use rust_crypto as software_crypto; | ||
|
||
use self::ecdh::Ecdh; | ||
use self::ecdsa::Ecdsa; | ||
|
||
/// The size of field elements in the elliptic curve P256. | ||
pub const EC_FIELD_SIZE: usize = 32; | ||
|
||
/// The size of a serialized ECDSA signature. | ||
pub const EC_SIGNATURE_SIZE: usize = 2 * EC_FIELD_SIZE; | ||
|
||
/// Necessary cryptographic primitives for CTAP. | ||
pub trait Crypto { | ||
type Ecdh: Ecdh; | ||
type Ecdsa: Ecdsa; | ||
} |
Oops, something went wrong.