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

[zk-token-sdk] Limit max seed length for key derivations #33700

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions zk-token-sdk/src/encryption/auth_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub enum AuthenticatedEncryptionError {
DerivationMethodNotSupported,
#[error("seed length too short for derivation")]
SeedLengthTooShort,
#[error("seed length too long for derivation")]
SeedLengthTooLarge,
samkim-crypto marked this conversation as resolved.
Show resolved Hide resolved
}

struct AuthenticatedEncryption;
Expand Down Expand Up @@ -172,10 +174,14 @@ impl EncodableKey for AeKey {
impl SeedDerivable for AeKey {
fn from_seed(seed: &[u8]) -> Result<Self, Box<dyn error::Error>> {
const MINIMUM_SEED_LEN: usize = AE_KEY_LEN;
const MAXIMUM_SEED_LEN: usize = 65535;

if seed.len() < MINIMUM_SEED_LEN {
return Err(AuthenticatedEncryptionError::SeedLengthTooShort.into());
}
if seed.len() > MAXIMUM_SEED_LEN {
return Err(AuthenticatedEncryptionError::SeedLengthTooLarge.into());
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh sorry, you'll also need this!

Suggested change
return Err(AuthenticatedEncryptionError::SeedLengthTooLarge.into());
return Err(AuthenticatedEncryptionError::SeedLengthTooLong.into());

}

let mut hasher = Sha3_512::new();
hasher.update(seed);
Expand Down Expand Up @@ -278,4 +284,16 @@ mod tests {
let null_signer = NullSigner::new(&Pubkey::default());
assert!(AeKey::new_from_signer(&null_signer, Pubkey::default().as_ref()).is_err());
}

#[test]
fn test_aes_key_from_seed() {
let good_seed = vec![0; 32];
assert!(AeKey::from_seed(&good_seed).is_ok());

let too_short_seed = vec![0; 15];
assert!(AeKey::from_seed(&too_short_seed).is_err());

let too_long_seed = vec![0; 65536];
assert!(AeKey::from_seed(&too_long_seed).is_err());
}
}
9 changes: 9 additions & 0 deletions zk-token-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub enum ElGamalError {
DerivationMethodNotSupported,
#[error("seed length too short for derivation")]
SeedLengthTooShort,
#[error("seed length too long for derivation")]
SeedLengthTooLarge,
samkim-crypto marked this conversation as resolved.
Show resolved Hide resolved
}

/// Algorithm handle for the twisted ElGamal encryption scheme
Expand Down Expand Up @@ -449,10 +451,14 @@ impl ElGamalSecretKey {
/// Derive an ElGamal secret key from an entropy seed.
pub fn from_seed(seed: &[u8]) -> Result<Self, ElGamalError> {
const MINIMUM_SEED_LEN: usize = ELGAMAL_SECRET_KEY_LEN;
const MAXIMUM_SEED_LEN: usize = 65535;

if seed.len() < MINIMUM_SEED_LEN {
return Err(ElGamalError::SeedLengthTooShort);
}
if seed.len() > MAXIMUM_SEED_LEN {
return Err(ElGamalError::SeedLengthTooLarge);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return Err(ElGamalError::SeedLengthTooLarge);
return Err(ElGamalError::SeedLengthTooLong);

}
Ok(ElGamalSecretKey(Scalar::hash_from_bytes::<Sha3_512>(seed)))
}

Expand Down Expand Up @@ -1026,6 +1032,9 @@ mod tests {

let too_short_seed = vec![0; 31];
assert!(ElGamalKeypair::from_seed(&too_short_seed).is_err());

let too_long_seed = vec![0; 65536];
assert!(ElGamalKeypair::from_seed(&too_long_seed).is_err());
}

#[test]
Expand Down