| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// Copyright by contributors to this project. |
| 3 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 4 |
|
| 5 |
use mls_rs_core::crypto::CipherSuite; |
| 6 |
|
| 7 |
use crate::SamplingMethod; |
| 8 |
|
| 9 |
/// Elliptic curve types |
| 10 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 11 |
#[repr(u8)] |
| 12 |
#[non_exhaustive] |
| 13 |
pub enum Curve { |
| 14 |
/// NIST Curve-P256 |
| 15 |
P256, |
| 16 |
/// NIST Curve-P384 |
| 17 |
P384, |
| 18 |
/// NIST Curve-P521 |
| 19 |
P521, |
| 20 |
/// Elliptic-curve Diffie-Hellman key exchange Curve25519 |
| 21 |
X25519, |
| 22 |
/// Edwards-curve Digital Signature Algorithm Curve25519 |
| 23 |
Ed25519, |
| 24 |
/// Elliptic-curve Diffie-Hellman key exchange Curve448 |
| 25 |
X448, |
| 26 |
/// Edwards-curve Digital Signature Algorithm Curve448 |
| 27 |
Ed448, |
| 28 |
} |
| 29 |
|
| 30 |
impl Curve { |
| 31 |
/// Returns the amount of bytes of a secret key using this curve |
| 32 |
#[inline(always)] |
| 33 |
pub fn secret_key_size(&self) -> usize { |
| 34 |
match self { |
| 35 |
Curve::P256 => 32, |
| 36 |
Curve::P384 => 48, |
| 37 |
Curve::P521 => 66, |
| 38 |
Curve::X25519 => 32, |
| 39 |
Curve::Ed25519 => 64, |
| 40 |
Curve::X448 => 56, |
| 41 |
Curve::Ed448 => 114, |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[inline(always)] |
| 46 |
pub fn public_key_size(&self) -> usize { |
| 47 |
match self { |
| 48 |
Curve::P256 | Curve::P384 | Curve::P521 => 2 * self.secret_key_size() + 1, |
| 49 |
Curve::X25519 | Curve::Ed25519 | Curve::X448 | Curve::Ed448 => self.secret_key_size(), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
pub fn from_ciphersuite(cipher_suite: CipherSuite, for_sig: bool) -> Option<Self> { |
| 54 |
match cipher_suite { |
| 55 |
CipherSuite::P256_AES128 => Some(Curve::P256), |
| 56 |
CipherSuite::P384_AES256 => Some(Curve::P384), |
| 57 |
CipherSuite::P521_AES256 => Some(Curve::P521), |
| 58 |
CipherSuite::CURVE25519_AES128 | CipherSuite::CURVE25519_CHACHA if for_sig => { |
| 59 |
Some(Curve::Ed25519) |
| 60 |
} |
| 61 |
CipherSuite::CURVE25519_AES128 | CipherSuite::CURVE25519_CHACHA => Some(Curve::X25519), |
| 62 |
CipherSuite::CURVE448_AES256 | CipherSuite::CURVE448_CHACHA if for_sig => { |
| 63 |
Some(Curve::Ed448) |
| 64 |
} |
| 65 |
CipherSuite::CURVE448_AES256 | CipherSuite::CURVE448_CHACHA => Some(Curve::X448), |
| 66 |
_ => None, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
#[inline(always)] |
| 71 |
pub fn hpke_sampling_method(&self) -> SamplingMethod { |
| 72 |
match self { |
| 73 |
Curve::P256 | Curve::P384 => SamplingMethod::HpkeWithBitmask(0xFF), |
| 74 |
Curve::P521 => SamplingMethod::HpkeWithBitmask(0x01), |
| 75 |
_ => SamplingMethod::HpkeWithoutBitmask, |
| 76 |
} |
| 77 |
} |
| 78 |
} |