Enforce zeroing of serialized proof excess bits

This commit is contained in:
Ignotus Peverell 2019-01-08 19:18:44 +00:00
parent 27801f6a93
commit 5caddc01db
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211

View file

@ -395,10 +395,14 @@ impl Readable for Proof {
return Err(ser::Error::CorruptedData); return Err(ser::Error::CorruptedData);
} }
// prepare nonces and read the right number of bytes
let mut nonces = Vec::with_capacity(global::proofsize()); let mut nonces = Vec::with_capacity(global::proofsize());
let nonce_bits = edge_bits as usize; let nonce_bits = edge_bits as usize;
let bytes_len = BitVec::bytes_len(nonce_bits * global::proofsize()); let bits_len = nonce_bits * global::proofsize();
let bytes_len = BitVec::bytes_len(bits_len);
let bits = reader.read_fixed_bytes(bytes_len)?; let bits = reader.read_fixed_bytes(bytes_len)?;
// set our nonces from what we read in the bitvec
let bitvec = BitVec { bits }; let bitvec = BitVec { bits };
for n in 0..global::proofsize() { for n in 0..global::proofsize() {
let mut nonce = 0; let mut nonce = 0;
@ -409,6 +413,15 @@ impl Readable for Proof {
} }
nonces.push(nonce); nonces.push(nonce);
} }
// check the last bits of the last byte are zeroed, we don't use them but
// still better to enforce to avoid any malleability
for n in (bits_len+1)..(bytes_len*8) {
if bitvec.bit_at(n) {
return Err(ser::Error::CorruptedData);
}
}
Ok(Proof { edge_bits, nonces }) Ok(Proof { edge_bits, nonces })
} }
} }