Skip to content

Commit 7d3ef83

Browse files
rusty1968FerralCoder
authored andcommitted
security: eliminate unsafe array indexing in cipher HAL
Replace all direct array indexing with safe .get() and .get_mut() methods in the BlockAligned container implementation to prevent potential panics and meet strict security requirements. **Production code fixes:** - from_slice_padded(): Use get_mut(i) instead of blocks[i] - push_block(): Use get_mut(block_count) instead of blocks[block_count] - get_block(): Use get(index) instead of &blocks[index] **Test code fixes:** - Replace blocks[0] and blocks[1] with safe .get() calls - Replace third_block[0] with safe .get() access **Security improvements:** - Zero panic risk: All array access now bounds-checked - Proper error handling: Failed access returns errors instead of panicking - Compliance: Follows security guidelines forbidding direct indexing All tests pass and clippy indexing warnings are eliminated while maintaining full functionality and performance.
1 parent 7bf676c commit 7d3ef83

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

hal/blocking/src/cipher.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,15 @@ impl<const BLOCK_SIZE: usize, const MAX_BLOCKS: usize> BlockAligned<BLOCK_SIZE,
314314

315315
// Fill complete blocks
316316
for (i, chunk) in data.chunks(BLOCK_SIZE).enumerate() {
317-
result.blocks[i].fill(padding_byte);
318-
result.blocks[i][..chunk.len()].copy_from_slice(chunk);
317+
let block = result
318+
.blocks
319+
.get_mut(i)
320+
.ok_or(BlockAlignedError::DataTooLarge)?;
321+
block.fill(padding_byte);
322+
let slice = block
323+
.get_mut(..chunk.len())
324+
.ok_or(BlockAlignedError::DataTooLarge)?;
325+
slice.copy_from_slice(chunk);
319326
}
320327

321328
Ok(result)
@@ -334,7 +341,11 @@ impl<const BLOCK_SIZE: usize, const MAX_BLOCKS: usize> BlockAligned<BLOCK_SIZE,
334341
return Err(BlockAlignedError::CapacityExceeded);
335342
}
336343

337-
self.blocks[self.block_count] = block;
344+
let block_slot = self
345+
.blocks
346+
.get_mut(self.block_count)
347+
.ok_or(BlockAlignedError::CapacityExceeded)?;
348+
*block_slot = block;
338349
self.block_count += 1;
339350
Ok(())
340351
}
@@ -367,7 +378,7 @@ impl<const BLOCK_SIZE: usize, const MAX_BLOCKS: usize> BlockAligned<BLOCK_SIZE,
367378
/// Get a specific block by index.
368379
pub fn get_block(&self, index: usize) -> Option<&[u8; BLOCK_SIZE]> {
369380
if index < self.block_count {
370-
Some(&self.blocks[index])
381+
self.blocks.get(index)
371382
} else {
372383
None
373384
}
@@ -729,8 +740,8 @@ mod tests {
729740

730741
let blocks = container.blocks();
731742
assert_eq!(blocks.len(), 2);
732-
assert_eq!(blocks[0], block1);
733-
assert_eq!(blocks[1], block2);
743+
assert_eq!(blocks.get(0).unwrap(), &block1);
744+
assert_eq!(blocks.get(1).unwrap(), &block2);
734745
}
735746

736747
#[test]
@@ -797,7 +808,7 @@ mod tests {
797808

798809
// Third block should have one byte of data and 15 bytes of padding
799810
let third_block = container.get_block(2).unwrap();
800-
assert_eq!(third_block[0], 0x42);
811+
assert_eq!(third_block.get(0).unwrap(), &0x42);
801812
assert_eq!(&third_block[1..], &[0xFF; 15]);
802813
}
803814

0 commit comments

Comments
 (0)