add handlers to check for hardware errors and try to recover

This commit is contained in:
bunnie 2024-03-28 10:20:53 +08:00
parent c0d9b40d9d
commit 4170979f6a
2 changed files with 112 additions and 88 deletions

View file

@ -129,8 +129,8 @@ pub(crate) fn engine(a: &[u8; 32], b: &[u8; 32], op: EngineOp) -> Engine25519 {
)
},
];
match op {
loop {
let prog_len = match op {
EngineOp::Mul => {
let prog = assemble_engine25519!(
start:
@ -141,6 +141,7 @@ pub(crate) fn engine(a: &[u8; 32], b: &[u8; 32], op: EngineOp) -> Engine25519 {
*dest = src;
}
engine.wfo(utra::engine::MPLEN_MPLEN, prog.len() as u32);
prog.len()
}
EngineOp::Add => {
let prog = assemble_engine25519!(
@ -154,6 +155,7 @@ pub(crate) fn engine(a: &[u8; 32], b: &[u8; 32], op: EngineOp) -> Engine25519 {
*dest = src;
}
engine.wfo(utra::engine::MPLEN_MPLEN, prog.len() as u32);
prog.len()
}
EngineOp::Sub => {
let prog = assemble_engine25519!(
@ -168,8 +170,9 @@ pub(crate) fn engine(a: &[u8; 32], b: &[u8; 32], op: EngineOp) -> Engine25519 {
*dest = src;
}
engine.wfo(utra::engine::MPLEN_MPLEN, prog.len() as u32);
prog.len()
}
}
};
// copy a arg
for (src, dst) in a.chunks_exact(4).zip(rf[0].iter_mut()) {
let bytes: [u8; 4] = [src[0], src[1], src[2], src[3]];
@ -191,6 +194,10 @@ pub(crate) fn engine(a: &[u8; 32], b: &[u8; 32], op: EngineOp) -> Engine25519 {
engine.wfo(utra::engine::CONTROL_GO, 1);
while engine.rf(utra::engine::STATUS_RUNNING) != 0 {}
if !was_engine_error(prog_len) {
break;
}
}
// return result, always in reg 2
let mut result: [u8; 32] = [0; 32];

View file

@ -468,10 +468,16 @@ impl ProjectivePoint {
let mut ucode_hw = unsafe { get_ucode() };
let rf_hw = unsafe { get_rf() };
let mut r;
loop {
copy_to_rf(self.U.as_bytes(), 29, rf_hw, 0);
copy_to_rf(self.W.as_bytes(), 30, rf_hw, 0);
let r = MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, 0));
r = MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, 0));
if !was_engine_error(mcode.len()) {
break;
}
}
#[cfg(feature="auto-release")]
free_engine();
r
@ -637,6 +643,7 @@ pub(crate) fn differential_add_and_double(
let mut ucode_hw = unsafe { get_ucode() };
let rf_hw = unsafe { get_rf() };
loop {
// P.U in %20
// P.W in %21
// Q.U in %22
@ -650,6 +657,10 @@ pub(crate) fn differential_add_and_double(
// start the run
run_job(&mut ucode_hw, &rf_hw, &mcode, 0);
if !was_engine_error(mcode.len()) {
break;
}
}
P.U = FieldElement::from_bytes(&copy_from_rf(20, &rf_hw, 0));
P.W = FieldElement::from_bytes(&copy_from_rf(21, &rf_hw, 0));
@ -998,6 +1009,8 @@ impl Mul<&Scalar> for &MontgomeryPoint {
let window = 0;
match ensure_engine() {
Ok(_) => {
let mut r;
loop {
// safety: these were called after ensure_engine()
let mut ucode_hw = unsafe { get_ucode() };
let mut rf_hw = unsafe { get_rf() };
@ -1019,7 +1032,11 @@ impl Mul<&Scalar> for &MontgomeryPoint {
window,
); // 254 as loop counter
let r = MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, window));
r = MontgomeryPoint(run_job(&mut ucode_hw, &rf_hw, &mcode, window));
if !was_engine_error(mcode.len()) {
break;
}
}
#[cfg(feature="auto-release")]
free_engine();
r