@@ -14,6 +14,7 @@ use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
1414use curve25519_dalek:: scalar:: Scalar ;
1515use curve25519_dalek:: traits:: MultiscalarMul ;
1616
17+ use clear_on_drop:: clear:: Clear ;
1718use errors:: MPCError ;
1819use generators:: { BulletproofGens , PedersenGens } ;
1920use rand;
@@ -129,6 +130,14 @@ impl<'a> PartyAwaitingPosition<'a> {
129130 }
130131}
131132
133+ /// Overwrite secrets with null bytes when they go out of scope.
134+ impl < ' a > Drop for PartyAwaitingPosition < ' a > {
135+ fn drop ( & mut self ) {
136+ self . v . clear ( ) ;
137+ self . v_blinding . clear ( ) ;
138+ }
139+ }
140+
132141/// A party which has committed to the bits of its value
133142/// and is waiting for the aggregated value challenge from the dealer.
134143pub struct PartyAwaitingBitChallenge < ' a > {
@@ -206,6 +215,28 @@ impl<'a> PartyAwaitingBitChallenge<'a> {
206215 }
207216}
208217
218+ /// Overwrite secrets with null bytes when they go out of scope.
219+ impl < ' a > Drop for PartyAwaitingBitChallenge < ' a > {
220+ fn drop ( & mut self ) {
221+ self . v . clear ( ) ;
222+ self . v_blinding . clear ( ) ;
223+ self . a_blinding . clear ( ) ;
224+ self . s_blinding . clear ( ) ;
225+
226+ // Important: due to how ClearOnDrop auto-implements InitializableFromZeroed
227+ // for T: Default, calling .clear() on Vec compiles, but does not
228+ // clear the content. Instead, it only clears the Vec's header.
229+ // Clearing the underlying buffer item-by-item will do the job, but will
230+ // keep the header as-is, which is fine since the header does not contain secrets.
231+ for e in self . s_L . iter_mut ( ) {
232+ e. clear ( ) ;
233+ }
234+ for e in self . s_R . iter_mut ( ) {
235+ e. clear ( ) ;
236+ }
237+ }
238+ }
239+
209240/// A party which has committed to their polynomial coefficents
210241/// and is waiting for the polynomial challenge from the dealer.
211242pub struct PartyAwaitingPolyChallenge {
@@ -252,3 +283,17 @@ impl PartyAwaitingPolyChallenge {
252283 } )
253284 }
254285}
286+
287+ /// Overwrite secrets with null bytes when they go out of scope.
288+ impl Drop for PartyAwaitingPolyChallenge {
289+ fn drop ( & mut self ) {
290+ self . v_blinding . clear ( ) ;
291+ self . a_blinding . clear ( ) ;
292+ self . s_blinding . clear ( ) ;
293+ self . t_1_blinding . clear ( ) ;
294+ self . t_2_blinding . clear ( ) ;
295+
296+ // Note: polynomials r_poly, l_poly and t_poly
297+ // are cleared within their own Drop impls.
298+ }
299+ }
0 commit comments