2023-01-01 06:54:18 +00:00
|
|
|
use core::fmt::Debug;
|
2022-12-07 22:20:20 +00:00
|
|
|
use std::{
|
|
|
|
ops::Deref,
|
|
|
|
io::{self, Read, Write},
|
|
|
|
collections::HashMap,
|
|
|
|
};
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2022-12-07 22:20:20 +00:00
|
|
|
use zeroize::{Zeroize, Zeroizing};
|
|
|
|
use rand_core::{RngCore, CryptoRng};
|
|
|
|
|
|
|
|
use chacha20::{
|
|
|
|
cipher::{crypto_common::KeyIvInit, StreamCipher},
|
|
|
|
Key as Cc20Key, Nonce as Cc20Iv, ChaCha20,
|
|
|
|
};
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
use transcript::{Transcript, RecommendedTranscript};
|
2022-12-07 22:20:20 +00:00
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
use group::ff::Field;
|
|
|
|
use group::GroupEncoding;
|
2022-12-07 22:20:20 +00:00
|
|
|
use ciphersuite::Ciphersuite;
|
2023-01-01 06:54:18 +00:00
|
|
|
use multiexp::BatchVerifier;
|
2022-12-07 22:20:20 +00:00
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
use schnorr::SchnorrSignature;
|
|
|
|
use dleq::DLEqProof;
|
2022-12-07 22:20:20 +00:00
|
|
|
|
2023-02-23 11:50:45 +00:00
|
|
|
use crate::{Participant, ThresholdParams};
|
2022-12-07 22:20:20 +00:00
|
|
|
|
|
|
|
pub trait ReadWrite: Sized {
|
|
|
|
fn read<R: Read>(reader: &mut R, params: ThresholdParams) -> io::Result<Self>;
|
|
|
|
fn write<W: Write>(&self, writer: &mut W) -> io::Result<()>;
|
|
|
|
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = vec![];
|
|
|
|
self.write(&mut buf).unwrap();
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Message: Clone + PartialEq + Eq + Debug + Zeroize + ReadWrite {}
|
|
|
|
impl<M: Clone + PartialEq + Eq + Debug + Zeroize + ReadWrite> Message for M {}
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
/// Wraps a message with a key to use for encryption in the future.
|
2022-12-07 22:20:20 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
|
|
pub struct EncryptionKeyMessage<C: Ciphersuite, M: Message> {
|
|
|
|
msg: M,
|
|
|
|
enc_key: C::G,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't impl ReadWrite so that doesn't need to be imported
|
|
|
|
impl<C: Ciphersuite, M: Message> EncryptionKeyMessage<C, M> {
|
|
|
|
pub fn read<R: Read>(reader: &mut R, params: ThresholdParams) -> io::Result<Self> {
|
|
|
|
Ok(Self { msg: M::read(reader, params)?, enc_key: C::read_G(reader)? })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
|
|
|
|
self.msg.write(writer)?;
|
|
|
|
writer.write_all(self.enc_key.to_bytes().as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = vec![];
|
|
|
|
self.write(&mut buf).unwrap();
|
|
|
|
buf
|
|
|
|
}
|
2023-01-01 06:54:18 +00:00
|
|
|
|
|
|
|
// Used by tests
|
|
|
|
pub(crate) fn enc_key(&self) -> C::G {
|
|
|
|
self.enc_key
|
|
|
|
}
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
pub trait Encryptable: Clone + AsRef<[u8]> + AsMut<[u8]> + Zeroize + ReadWrite {}
|
|
|
|
impl<E: Clone + AsRef<[u8]> + AsMut<[u8]> + Zeroize + ReadWrite> Encryptable for E {}
|
|
|
|
|
|
|
|
/// An encrypted message, with a per-message encryption key enabling revealing specific messages
|
|
|
|
/// without side effects.
|
2022-12-07 22:20:20 +00:00
|
|
|
#[derive(Clone, Zeroize)]
|
2023-01-01 06:54:18 +00:00
|
|
|
pub struct EncryptedMessage<C: Ciphersuite, E: Encryptable> {
|
|
|
|
key: C::G,
|
|
|
|
// Also include a proof-of-possession for the key.
|
|
|
|
// If this proof-of-possession wasn't here, Eve could observe Alice encrypt to Bob with key X,
|
|
|
|
// then send Bob a message also claiming to use X.
|
|
|
|
// While Eve's message would fail to meaningfully decrypt, Bob would then use this to create a
|
|
|
|
// blame argument against Eve. When they do, they'd reveal bX, revealing Alice's message to Bob.
|
|
|
|
// This is a massive side effect which could break some protocols, in the worst case.
|
|
|
|
// While Eve can still reuse their own keys, causing Bob to leak all messages by revealing for
|
|
|
|
// any single one, that's effectively Eve revealing themselves, and not considered relevant.
|
|
|
|
pop: SchnorrSignature<C>,
|
|
|
|
msg: Zeroizing<E>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ecdh<C: Ciphersuite>(private: &Zeroizing<C::F>, public: C::G) -> Zeroizing<C::G> {
|
|
|
|
Zeroizing::new(public * private.deref())
|
|
|
|
}
|
|
|
|
|
2023-02-23 09:44:20 +00:00
|
|
|
// Each ecdh must be distinct. Reuse of an ecdh for multiple ciphers will cause the messages to be
|
|
|
|
// leaked.
|
2023-02-23 12:19:55 +00:00
|
|
|
fn cipher<C: Ciphersuite>(context: &str, ecdh: &Zeroizing<C::G>) -> ChaCha20 {
|
2023-01-01 06:54:18 +00:00
|
|
|
// Ideally, we'd box this transcript with ZAlloc, yet that's only possible on nightly
|
|
|
|
// TODO: https://github.com/serai-dex/serai/issues/151
|
|
|
|
let mut transcript = RecommendedTranscript::new(b"DKG Encryption v0.2");
|
2023-02-23 12:19:55 +00:00
|
|
|
transcript.append_message(b"context", context.as_bytes());
|
|
|
|
|
|
|
|
transcript.domain_separate(b"encryption_key");
|
2023-01-01 06:54:18 +00:00
|
|
|
|
|
|
|
let mut ecdh = ecdh.to_bytes();
|
|
|
|
transcript.append_message(b"shared_key", ecdh.as_ref());
|
|
|
|
ecdh.as_mut().zeroize();
|
|
|
|
|
|
|
|
let zeroize = |buf: &mut [u8]| buf.zeroize();
|
|
|
|
|
|
|
|
let mut key = Cc20Key::default();
|
|
|
|
let mut challenge = transcript.challenge(b"key");
|
|
|
|
key.copy_from_slice(&challenge[.. 32]);
|
|
|
|
zeroize(challenge.as_mut());
|
|
|
|
|
2023-02-23 09:44:20 +00:00
|
|
|
// Since the key is single-use, it doesn't matter what we use for the IV
|
|
|
|
// The isssue is key + IV reuse. If we never reuse the key, we can't have the opportunity to
|
|
|
|
// reuse a nonce
|
|
|
|
// Use a static IV in acknowledgement of this
|
2023-01-01 06:54:18 +00:00
|
|
|
let mut iv = Cc20Iv::default();
|
2023-02-23 09:44:20 +00:00
|
|
|
// The \0 is to satisfy the length requirement (12), not to be null terminated
|
|
|
|
iv.copy_from_slice(b"DKG IV v0.2\0");
|
2023-01-01 06:54:18 +00:00
|
|
|
|
2023-02-23 09:44:20 +00:00
|
|
|
// ChaCha20 has the same commentary as the transcript regarding ZAlloc
|
2023-01-01 06:54:18 +00:00
|
|
|
// TODO: https://github.com/serai-dex/serai/issues/151
|
|
|
|
let res = ChaCha20::new(&key, &iv);
|
|
|
|
zeroize(key.as_mut());
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encrypt<R: RngCore + CryptoRng, C: Ciphersuite, E: Encryptable>(
|
|
|
|
rng: &mut R,
|
2023-02-23 12:19:55 +00:00
|
|
|
context: &str,
|
2023-02-23 11:50:45 +00:00
|
|
|
from: Participant,
|
2023-01-01 06:54:18 +00:00
|
|
|
to: C::G,
|
|
|
|
mut msg: Zeroizing<E>,
|
|
|
|
) -> EncryptedMessage<C, E> {
|
|
|
|
/*
|
|
|
|
The following code could be used to replace the requirement on an RNG here.
|
|
|
|
It's just currently not an issue to require taking in an RNG here.
|
|
|
|
let last = self.last_enc_key.to_bytes();
|
|
|
|
self.last_enc_key = C::hash_to_F(b"encryption_base", last.as_ref());
|
|
|
|
let key = C::hash_to_F(b"encryption_key", last.as_ref());
|
|
|
|
last.as_mut().zeroize();
|
|
|
|
*/
|
2022-12-07 22:20:20 +00:00
|
|
|
|
2023-02-23 09:44:20 +00:00
|
|
|
// Generate a new key for this message, satisfying cipher's requirement of distinct keys per
|
|
|
|
// message, and enabling revealing this message without revealing any others
|
2023-01-01 06:54:18 +00:00
|
|
|
let key = Zeroizing::new(C::random_nonzero_F(rng));
|
2023-02-23 12:19:55 +00:00
|
|
|
cipher::<C>(context, &ecdh::<C>(&key, to)).apply_keystream(msg.as_mut().as_mut());
|
2023-01-01 06:54:18 +00:00
|
|
|
|
|
|
|
let pub_key = C::generator() * key.deref();
|
|
|
|
let nonce = Zeroizing::new(C::random_nonzero_F(rng));
|
|
|
|
let pub_nonce = C::generator() * nonce.deref();
|
|
|
|
EncryptedMessage {
|
|
|
|
key: pub_key,
|
|
|
|
pop: SchnorrSignature::sign(
|
|
|
|
&key,
|
|
|
|
nonce,
|
2023-02-23 12:19:55 +00:00
|
|
|
pop_challenge::<C>(context, pub_nonce, pub_key, from, msg.deref().as_ref()),
|
2023-01-01 06:54:18 +00:00
|
|
|
),
|
|
|
|
msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: Ciphersuite, E: Encryptable> EncryptedMessage<C, E> {
|
2022-12-07 22:20:20 +00:00
|
|
|
pub fn read<R: Read>(reader: &mut R, params: ThresholdParams) -> io::Result<Self> {
|
2023-01-01 06:54:18 +00:00
|
|
|
Ok(Self {
|
|
|
|
key: C::read_G(reader)?,
|
|
|
|
pop: SchnorrSignature::<C>::read(reader)?,
|
|
|
|
msg: Zeroizing::new(E::read(reader, params)?),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
|
|
|
|
writer.write_all(self.key.to_bytes().as_ref())?;
|
|
|
|
self.pop.write(writer)?;
|
|
|
|
self.msg.write(writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = vec![];
|
|
|
|
self.write(&mut buf).unwrap();
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn invalidate_pop(&mut self) {
|
|
|
|
self.pop.s += C::F::one();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-02-23 12:19:55 +00:00
|
|
|
pub(crate) fn invalidate_msg<R: RngCore + CryptoRng>(
|
|
|
|
&mut self,
|
|
|
|
rng: &mut R,
|
|
|
|
context: &str,
|
|
|
|
from: Participant,
|
|
|
|
) {
|
2023-01-01 06:54:18 +00:00
|
|
|
// Invalidate the message by specifying a new key/Schnorr PoP
|
|
|
|
// This will cause all initial checks to pass, yet a decrypt to gibberish
|
|
|
|
let key = Zeroizing::new(C::random_nonzero_F(rng));
|
|
|
|
let pub_key = C::generator() * key.deref();
|
|
|
|
let nonce = Zeroizing::new(C::random_nonzero_F(rng));
|
|
|
|
let pub_nonce = C::generator() * nonce.deref();
|
|
|
|
self.key = pub_key;
|
|
|
|
self.pop = SchnorrSignature::sign(
|
|
|
|
&key,
|
|
|
|
nonce,
|
2023-02-23 12:19:55 +00:00
|
|
|
pop_challenge::<C>(context, pub_nonce, pub_key, from, self.msg.deref().as_ref()),
|
2023-01-01 06:54:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes the encrypted message is a secret share.
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn invalidate_share_serialization<R: RngCore + CryptoRng>(
|
|
|
|
&mut self,
|
|
|
|
rng: &mut R,
|
2023-02-23 12:19:55 +00:00
|
|
|
context: &str,
|
2023-02-23 11:50:45 +00:00
|
|
|
from: Participant,
|
2023-01-01 06:54:18 +00:00
|
|
|
to: C::G,
|
|
|
|
) {
|
|
|
|
use group::ff::PrimeField;
|
|
|
|
|
|
|
|
let mut repr = <C::F as PrimeField>::Repr::default();
|
|
|
|
for b in repr.as_mut().iter_mut() {
|
|
|
|
*b = 255;
|
|
|
|
}
|
|
|
|
// Tries to guarantee the above assumption.
|
|
|
|
assert_eq!(repr.as_ref().len(), self.msg.as_ref().len());
|
|
|
|
// Checks that this isn't over a field where this is somehow valid
|
|
|
|
assert!(!bool::from(C::F::from_repr(repr).is_some()));
|
|
|
|
|
|
|
|
self.msg.as_mut().as_mut().copy_from_slice(repr.as_ref());
|
2023-02-23 12:19:55 +00:00
|
|
|
*self = encrypt(rng, context, from, to, self.msg.clone());
|
2023-01-01 06:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes the encrypted message is a secret share.
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn invalidate_share_value<R: RngCore + CryptoRng>(
|
|
|
|
&mut self,
|
|
|
|
rng: &mut R,
|
2023-02-23 12:19:55 +00:00
|
|
|
context: &str,
|
2023-02-23 11:50:45 +00:00
|
|
|
from: Participant,
|
2023-01-01 06:54:18 +00:00
|
|
|
to: C::G,
|
|
|
|
) {
|
|
|
|
use group::ff::PrimeField;
|
|
|
|
|
|
|
|
// Assumes the share isn't randomly 1
|
|
|
|
let repr = C::F::one().to_repr();
|
|
|
|
self.msg.as_mut().as_mut().copy_from_slice(repr.as_ref());
|
2023-02-23 12:19:55 +00:00
|
|
|
*self = encrypt(rng, context, from, to, self.msg.clone());
|
2023-01-01 06:54:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A proof that the provided point is the legitimately derived shared key for some message.
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Zeroize)]
|
|
|
|
pub struct EncryptionKeyProof<C: Ciphersuite> {
|
|
|
|
key: Zeroizing<C::G>,
|
|
|
|
dleq: DLEqProof<C::G>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: Ciphersuite> EncryptionKeyProof<C> {
|
|
|
|
pub fn read<R: Read>(reader: &mut R) -> io::Result<Self> {
|
|
|
|
Ok(Self { key: Zeroizing::new(C::read_G(reader)?), dleq: DLEqProof::read(reader)? })
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write<W: Write>(&self, writer: &mut W) -> io::Result<()> {
|
2023-01-01 06:54:18 +00:00
|
|
|
writer.write_all(self.key.to_bytes().as_ref())?;
|
|
|
|
self.dleq.write(writer)
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = vec![];
|
|
|
|
self.write(&mut buf).unwrap();
|
|
|
|
buf
|
|
|
|
}
|
2023-01-01 06:54:18 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn invalidate_key(&mut self) {
|
|
|
|
*self.key += C::generator();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn invalidate_dleq(&mut self) {
|
|
|
|
let mut buf = vec![];
|
|
|
|
self.dleq.write(&mut buf).unwrap();
|
|
|
|
// Adds one to c since this is serialized c, s
|
|
|
|
// Adding one to c will leave a validly serialized c
|
|
|
|
// Adding one to s may leave an invalidly serialized s
|
|
|
|
buf[0] = buf[0].wrapping_add(1);
|
|
|
|
self.dleq = DLEqProof::read::<&[u8]>(&mut buf.as_ref()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This doesn't need to take the msg. It just doesn't hurt as an extra layer.
|
|
|
|
// This still doesn't mean the DKG offers an authenticated channel. The per-message keys have no
|
|
|
|
// root of trust other than their existence in the assumed-to-exist external authenticated channel.
|
2023-02-23 12:19:55 +00:00
|
|
|
fn pop_challenge<C: Ciphersuite>(
|
|
|
|
context: &str,
|
|
|
|
nonce: C::G,
|
|
|
|
key: C::G,
|
|
|
|
sender: Participant,
|
|
|
|
msg: &[u8],
|
|
|
|
) -> C::F {
|
2023-01-01 06:54:18 +00:00
|
|
|
let mut transcript = RecommendedTranscript::new(b"DKG Encryption Key Proof of Possession v0.2");
|
2023-02-23 12:19:55 +00:00
|
|
|
transcript.append_message(b"context", context.as_bytes());
|
|
|
|
|
|
|
|
transcript.domain_separate(b"proof_of_possession");
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
transcript.append_message(b"nonce", nonce.to_bytes());
|
|
|
|
transcript.append_message(b"key", key.to_bytes());
|
|
|
|
// This is sufficient to prevent the attack this is meant to stop
|
2023-02-23 11:50:45 +00:00
|
|
|
transcript.append_message(b"sender", sender.to_bytes());
|
2023-01-01 06:54:18 +00:00
|
|
|
// This, as written above, doesn't hurt
|
|
|
|
transcript.append_message(b"message", msg);
|
|
|
|
// While this is a PoK and a PoP, it's called a PoP here since the important part is its owner
|
|
|
|
// Elsewhere, where we use the term PoK, the important part is that it isn't some inverse, with
|
|
|
|
// an unknown to anyone discrete log, breaking the system
|
|
|
|
C::hash_to_F(b"DKG-encryption-proof_of_possession", &transcript.challenge(b"schnorr"))
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 12:19:55 +00:00
|
|
|
fn encryption_key_transcript(context: &str) -> RecommendedTranscript {
|
|
|
|
let mut transcript = RecommendedTranscript::new(b"DKG Encryption Key Correctness Proof v0.2");
|
|
|
|
transcript.append_message(b"context", context.as_bytes());
|
|
|
|
transcript
|
2023-01-01 06:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Error)]
|
|
|
|
pub(crate) enum DecryptionError {
|
|
|
|
#[error("accused provided an invalid signature")]
|
|
|
|
InvalidSignature,
|
|
|
|
#[error("accuser provided an invalid decryption key")]
|
|
|
|
InvalidProof,
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple box for managing encryption.
|
2022-12-07 22:20:20 +00:00
|
|
|
#[derive(Clone)]
|
2023-01-01 06:54:18 +00:00
|
|
|
pub(crate) struct Encryption<C: Ciphersuite> {
|
2023-02-23 12:19:55 +00:00
|
|
|
context: String,
|
2023-02-23 11:50:45 +00:00
|
|
|
i: Participant,
|
2022-12-07 22:20:20 +00:00
|
|
|
enc_key: Zeroizing<C::F>,
|
|
|
|
enc_pub_key: C::G,
|
2023-02-23 11:50:45 +00:00
|
|
|
enc_keys: HashMap<Participant, C::G>,
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
impl<C: Ciphersuite> Zeroize for Encryption<C> {
|
2022-12-07 22:20:20 +00:00
|
|
|
fn zeroize(&mut self) {
|
|
|
|
self.enc_key.zeroize();
|
|
|
|
self.enc_pub_key.zeroize();
|
2022-12-07 22:41:07 +00:00
|
|
|
for (_, mut value) in self.enc_keys.drain() {
|
2022-12-07 22:20:20 +00:00
|
|
|
value.zeroize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
impl<C: Ciphersuite> Encryption<C> {
|
2023-02-23 12:19:55 +00:00
|
|
|
pub(crate) fn new<R: RngCore + CryptoRng>(context: String, i: Participant, rng: &mut R) -> Self {
|
2022-12-07 22:20:20 +00:00
|
|
|
let enc_key = Zeroizing::new(C::random_nonzero_F(rng));
|
2023-01-01 06:54:18 +00:00
|
|
|
Self {
|
2023-02-23 12:19:55 +00:00
|
|
|
context,
|
2023-01-01 06:54:18 +00:00
|
|
|
i,
|
|
|
|
enc_pub_key: C::generator() * enc_key.deref(),
|
|
|
|
enc_key,
|
|
|
|
enc_keys: HashMap::new(),
|
|
|
|
}
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn registration<M: Message>(&self, msg: M) -> EncryptionKeyMessage<C, M> {
|
|
|
|
EncryptionKeyMessage { msg, enc_key: self.enc_pub_key }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn register<M: Message>(
|
|
|
|
&mut self,
|
2023-02-23 11:50:45 +00:00
|
|
|
participant: Participant,
|
2022-12-07 22:20:20 +00:00
|
|
|
msg: EncryptionKeyMessage<C, M>,
|
|
|
|
) -> M {
|
|
|
|
if self.enc_keys.contains_key(&participant) {
|
|
|
|
panic!("Re-registering encryption key for a participant");
|
|
|
|
}
|
|
|
|
self.enc_keys.insert(participant, msg.enc_key);
|
|
|
|
msg.msg
|
|
|
|
}
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
pub(crate) fn encrypt<R: RngCore + CryptoRng, E: Encryptable>(
|
|
|
|
&self,
|
|
|
|
rng: &mut R,
|
2023-02-23 11:50:45 +00:00
|
|
|
participant: Participant,
|
2023-01-01 06:54:18 +00:00
|
|
|
msg: Zeroizing<E>,
|
|
|
|
) -> EncryptedMessage<C, E> {
|
2023-02-23 12:19:55 +00:00
|
|
|
encrypt(rng, &self.context, self.i, self.enc_keys[&participant], msg)
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
pub(crate) fn decrypt<R: RngCore + CryptoRng, I: Copy + Zeroize, E: Encryptable>(
|
2022-12-07 22:20:20 +00:00
|
|
|
&self,
|
2023-01-01 06:54:18 +00:00
|
|
|
rng: &mut R,
|
|
|
|
batch: &mut BatchVerifier<I, C::G>,
|
|
|
|
// Uses a distinct batch ID so if this batch verifier is reused, we know its the PoP aspect
|
|
|
|
// which failed, and therefore to use None for the blame
|
|
|
|
batch_id: I,
|
2023-02-23 11:50:45 +00:00
|
|
|
from: Participant,
|
2023-01-01 06:54:18 +00:00
|
|
|
mut msg: EncryptedMessage<C, E>,
|
|
|
|
) -> (Zeroizing<E>, EncryptionKeyProof<C>) {
|
|
|
|
msg.pop.batch_verify(
|
|
|
|
rng,
|
|
|
|
batch,
|
|
|
|
batch_id,
|
|
|
|
msg.key,
|
2023-02-23 12:19:55 +00:00
|
|
|
pop_challenge::<C>(&self.context, msg.pop.R, msg.key, from, msg.msg.deref().as_ref()),
|
2023-01-01 06:54:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let key = ecdh::<C>(&self.enc_key, msg.key);
|
2023-02-23 12:19:55 +00:00
|
|
|
cipher::<C>(&self.context, &key).apply_keystream(msg.msg.as_mut().as_mut());
|
2023-01-01 06:54:18 +00:00
|
|
|
(
|
|
|
|
msg.msg,
|
|
|
|
EncryptionKeyProof {
|
|
|
|
key,
|
|
|
|
dleq: DLEqProof::prove(
|
|
|
|
rng,
|
2023-02-23 12:19:55 +00:00
|
|
|
&mut encryption_key_transcript(&self.context),
|
2023-01-01 06:54:18 +00:00
|
|
|
&[C::generator(), msg.key],
|
|
|
|
&self.enc_key,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-01 06:54:18 +00:00
|
|
|
// Given a message, and the intended decryptor, and a proof for its key, decrypt the message.
|
|
|
|
// Returns None if the key was wrong.
|
|
|
|
pub(crate) fn decrypt_with_proof<E: Encryptable>(
|
2022-12-07 22:20:20 +00:00
|
|
|
&self,
|
2023-02-23 11:50:45 +00:00
|
|
|
from: Participant,
|
|
|
|
decryptor: Participant,
|
2023-01-01 06:54:18 +00:00
|
|
|
mut msg: EncryptedMessage<C, E>,
|
|
|
|
// There's no encryption key proof if the accusation is of an invalid signature
|
|
|
|
proof: Option<EncryptionKeyProof<C>>,
|
|
|
|
) -> Result<Zeroizing<E>, DecryptionError> {
|
2023-02-23 12:19:55 +00:00
|
|
|
if !msg.pop.verify(
|
|
|
|
msg.key,
|
|
|
|
pop_challenge::<C>(&self.context, msg.pop.R, msg.key, from, msg.msg.deref().as_ref()),
|
|
|
|
) {
|
2023-01-01 06:54:18 +00:00
|
|
|
Err(DecryptionError::InvalidSignature)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(proof) = proof {
|
|
|
|
// Verify this is the decryption key for this message
|
|
|
|
proof
|
|
|
|
.dleq
|
|
|
|
.verify(
|
2023-02-23 12:19:55 +00:00
|
|
|
&mut encryption_key_transcript(&self.context),
|
2023-01-01 06:54:18 +00:00
|
|
|
&[C::generator(), msg.key],
|
|
|
|
&[self.enc_keys[&decryptor], *proof.key],
|
|
|
|
)
|
|
|
|
.map_err(|_| DecryptionError::InvalidProof)?;
|
|
|
|
|
2023-02-23 12:19:55 +00:00
|
|
|
cipher::<C>(&self.context, &proof.key).apply_keystream(msg.msg.as_mut().as_mut());
|
2023-01-01 06:54:18 +00:00
|
|
|
Ok(msg.msg)
|
|
|
|
} else {
|
|
|
|
Err(DecryptionError::InvalidProof)
|
|
|
|
}
|
2022-12-07 22:20:20 +00:00
|
|
|
}
|
|
|
|
}
|