Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ecp and pk module #344

Merged
merged 9 commits into from
Feb 8, 2024
Merged

Enhance ecp and pk module #344

merged 9 commits into from
Feb 8, 2024

Conversation

Taowyoo
Copy link
Collaborator

@Taowyoo Taowyoo commented Feb 5, 2024

What's New

Add functionalities for EcPoint and Pk:

  • Add a function to access Z field of EcPoint.
  • Add a newer version of EcPoint::mul with RNG for blinding.
    • Also marked old version one as deprecated.
  • Add a newer version of Pk::private_from_ec_components with RNG for blinding.
    • Also marked old version one as deprecated.
  • Add a function for performing const time comparison of EcPoint.

Test

  • Update tests to use above new functions with RNG.
  • Add a test for fn eq_const_time
  • Add bench for ensure fn eq_const_time works, see mbedtls/benches/ecp_eq_test.rs:
    EcpPoint X not equal    time:   [6.2940 ns 6.3282 ns 6.3741 ns]
    Found 13 outliers among 100 measurements (13.00%)
      1 (1.00%) high mild
      12 (12.00%) high severe
    
    EcpPoint Y not equal    time:   [9.3641 ns 9.4346 ns 9.5214 ns]
    Found 32 outliers among 100 measurements (32.00%)
      15 (15.00%) low severe
      2 (2.00%) low mild
      6 (6.00%) high mild
      9 (9.00%) high severe
    
    EcpPoint Z not equal    time:   [12.141 ns 12.289 ns 12.477 ns]
    Found 1 outliers among 100 measurements (1.00%)
      1 (1.00%) high severe
    
    EcpPoint X not equal const time
                            time:   [9.7337 ns 9.7690 ns 9.8196 ns]
    Found 19 outliers among 100 measurements (19.00%)
      5 (5.00%) low mild
      2 (2.00%) high mild
      12 (12.00%) high severe
    
    EcpPoint Y not equal const time
                            time:   [9.6957 ns 9.7007 ns 9.7072 ns]
    Found 11 outliers among 100 measurements (11.00%)
      5 (5.00%) high mild
      6 (6.00%) high severe
    
    EcpPoint Z not equal const time
                            time:   [9.7067 ns 9.7381 ns 9.7804 ns]
    Found 16 outliers among 100 measurements (16.00%)
      14 (14.00%) high mild
      2 (2.00%) high severe
    

@Taowyoo Taowyoo requested review from s-arash and zugzwang February 5, 2024 19:24
@Taowyoo Taowyoo self-assigned this Feb 5, 2024
- Add a function to access `Z` field of `EcPoint`.
- Add a newer version of `EcPoint::mul` with RNG for blinding.
  - Also marked old version one as deprecated.
- Add a newer version of `Pk::private_from_ec_components` with RNG for blinding.
  - Also marked old version one as deprecated.
- Update tests to use above new functions.
- Add a function for performing const time comparison of `EcPoint`.
@Taowyoo Taowyoo changed the title Enhance ecp module Enhance ecp and pk module Feb 5, 2024
Copy link
Collaborator

@s-arash s-arash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some nitpicks, otherwise looks good.

mbedtls/src/ecp/mod.rs Outdated Show resolved Hide resolved
mbedtls/src/pk/mod.rs Outdated Show resolved Hide resolved
mbedtls/src/pk/mod.rs Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@Taowyoo Taowyoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better rustdoc

@Taowyoo Taowyoo enabled auto-merge (squash) February 8, 2024 00:57
@Taowyoo Taowyoo merged commit 391b600 into main Feb 8, 2024
11 checks passed
@Taowyoo Taowyoo deleted the yx/enhance_ecp branch February 8, 2024 01:06
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these benches conclusive?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused. How to determine they are conclusive or not?

@@ -309,6 +309,10 @@ impl EcPoint {
Mpi::copy(&self.inner.Y)
}

pub fn z(&self) -> Result<Mpi> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to expose z? Use of Jacobian coordinates is normally internal for efficiency, and APIs should speak Cartesian

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MbedTLS uses Z as an internal value and it ensures that every function accepts/returns Z=0 (infinity point) or Z=1. But we should not expose this function at all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

///
/// This function will return an error if:
///
/// * `k` is not a valid private key, or `self` is not a valid public key.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no notion of "private key" or "public key", just scalars and elliptic curve points. Also, documentation should be a bit more precise (what is "invalid"?)

@@ -379,6 +433,16 @@ impl EcPoint {
}
}

/// This function compares two points in const time.
pub fn eq_const_time(&self, other: &EcPoint) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm this is really constant time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I addedbench for ensure fn eq_const_time works, see mbedtls/benches/ecp_eq_test.rs.
Is the result in the PR description enough?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these conclusive? Timing difference can be as few as some cycles. Instead, it would be preferable to reference MbedTLS C demonstrating constant-time implementation

/// * Fails to genearte `EcPoint` from given EcGroup in `curve`.
/// * The underlying C `mbedtls_pk_setup` function fails to set up the `Pk` context.
/// * The `EcPoint::mul` function fails to generate the public key point.
pub fn private_from_ec_components_with_rng<F: Random>(mut curve: EcGroup, private_key: Mpi, rng: &mut F) -> Result<Pk> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be called private_from_ec_scalar_with_rng .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will create another PR for update these

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants