Skip to content

Commit

Permalink
Add encryption error
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiono11 committed Apr 29, 2024
1 parent 1d862b8 commit 4c033ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/olaf/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub enum DKGError {
},
/// Decryption error when decrypting an encrypted secret share.
DecryptionError(chacha20poly1305::Error),
/// Encryption error when encrypting the secret share.
EncryptionError(chacha20poly1305::Error),
/// Incorrect number of coefficient commitments.
InvalidSecretPolynomialCommitment {
/// The expected value.
Expand Down
27 changes: 15 additions & 12 deletions src/olaf/simplpedpop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl SecretShare {
decryption_key: &Scalar,
encryption_key: &RistrettoPoint,
context: &[u8],
) -> EncryptedSecretShare {
) -> DKGResult<EncryptedSecretShare> {
let shared_secret = decryption_key * encryption_key;

let mut transcript = Transcript::new(b"encryption");
Expand All @@ -152,9 +152,11 @@ impl SecretShare {
let cipher: ChaCha20Poly1305 = make_aead::<Transcript, ChaCha20Poly1305>(transcript);
let nonce = Nonce::from_slice(&bytes[..]);

let ciphertext: Vec<u8> = cipher.encrypt(nonce, &self.0.as_bytes()[..]).unwrap();
let ciphertext: Vec<u8> = cipher
.encrypt(nonce, &self.0.as_bytes()[..])
.map_err(DKGError::EncryptionError)?;

EncryptedSecretShare(ciphertext)
Ok(EncryptedSecretShare(ciphertext))
}
}

Expand Down Expand Up @@ -397,12 +399,12 @@ pub mod round2 {
deckey: Scalar,
enckey: RistrettoPoint,
context: &[u8],
) -> PrivateMessage {
let encrypted_secret_share = secret_share.encrypt(&deckey, &enckey, context);
) -> DKGResult<PrivateMessage> {
let encrypted_secret_share = secret_share.encrypt(&deckey, &enckey, context)?;

PrivateMessage {
Ok(PrivateMessage {
encrypted_secret_share,
}
})
}
}

Expand Down Expand Up @@ -477,7 +479,7 @@ pub mod round2 {
&round1_private_data.secret_polynomial,
round1_private_data.secret_key,
secret_commitment,
);
)?;

Ok((public_data, messages))
}
Expand Down Expand Up @@ -664,8 +666,9 @@ pub mod round2 {
secret_polynomial: &SecretPolynomial,
secret_key: SecretKey,
secret_commitment: &SecretCommitment,
) -> Messages {
) -> DKGResult<Messages> {
let mut private_messages = BTreeMap::new();

let enc_keys: Vec<RistrettoPoint> = round2_public_data
.round1_public_messages
.values()
Expand All @@ -691,7 +694,7 @@ pub mod round2 {
secret_key.key,
enc_keys[i],
identifier.0.as_bytes(),
),
)?,
);
}

Expand All @@ -704,10 +707,10 @@ pub mod round2 {

let public_message = PublicMessage { certificate };

Messages {
Ok(Messages {
private_messages,
public_message,
}
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/olaf/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ mod tests {
.get_mut(&participants_sets_of_participants[0].own_identifier)
.unwrap();

private_message.encrypted_secret_share = enc_share;
private_message.encrypted_secret_share = enc_share.unwrap();

let result = round3(
&participants_sets_of_participants,
Expand Down Expand Up @@ -956,7 +956,7 @@ mod tests {
let original_share = SecretShare(Scalar::random(&mut rng));

let encrypted_share = original_share.encrypt(&deckey, &enckey, context);
let decrypted_share = encrypted_share.decrypt(&deckey, &enckey, context);
let decrypted_share = encrypted_share.unwrap().decrypt(&deckey, &enckey, context);

assert_eq!(
original_share.0,
Expand Down

0 comments on commit 4c033ef

Please sign in to comment.