Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions generic-ec/src/encoded.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use core::{fmt, ops};

use zeroize::Zeroize;

use crate::{as_raw::AsRaw, core::ByteArray, Curve};

/// Bytes representation of an elliptic point
#[derive(Zeroize)]
pub struct EncodedPoint<E: Curve>(EncodedPointInner<E>);

impl<E: Curve> EncodedPoint<E> {
Expand Down Expand Up @@ -67,6 +70,24 @@ impl<E: Curve> AsRef<[u8]> for EncodedPoint<E> {
}
}

impl<E: Curve> AsMut<[u8]> for EncodedPoint<E> {
fn as_mut(&mut self) -> &mut [u8] {
match &mut self.0 {
EncodedPointInner::Compressed(a) => a.as_mut(),
EncodedPointInner::Uncompressed(a) => a.as_mut(),
}
}
}

impl<E: Curve> Zeroize for EncodedPointInner<E> {
fn zeroize(&mut self) {
match self {
EncodedPointInner::Compressed(a) => a.as_mut().zeroize(),
EncodedPointInner::Uncompressed(a) => a.as_mut().zeroize(),
}
}
}
Comment on lines +82 to +89

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it ever happen that an elliptic point represents a sensitive info? I don't recall any such case

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this case, we would need a SecretPoint<E>...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, like in ECIES or ECDH when a point is fed to KDF - that's a secret info that we want to zeroize

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right... But I see a little value in adding Zeroize to EncodedPoint, when the point itself is not Zeroize... But making a point Zeroize brings little value without adding SecretPoint...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah the point is Zeroize already! Ok, then let's do Zeroize for EncodedPoint and leave SecretPoint as a thought for the future


/// Bytes representation of a scalar (either in big-endian or in little-endian)
#[derive(Clone)]
pub struct EncodedScalar<E: Curve>(E::ScalarArray);
Expand Down Expand Up @@ -133,3 +154,9 @@ impl<E: Curve> AsRaw for EncodedScalar<E> {
&self.0
}
}

impl<E: Curve> Zeroize for EncodedScalar<E> {
fn zeroize(&mut self) {
self.as_mut().zeroize()
}
}
Comment on lines +158 to +162

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we might wanna add SecretScalar::{to_be_bytes, to_le_bytes} methods that return Zeroizing<Box<EncodedScalar>> (Box cuz otherwise Rust will leave copies of encoded scalar everywhere on the stack)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Interesting idea. Right now I'm thinking about a SecretPoint and it would make a better and safer interface in this case

Loading