Skip to content

Commit 1e94064

Browse files
authored
refactor: remove redundant heap allocation in OAEP decrypt_inner (#659)
`decrypt_inner` took `em` by reference but cloned it via `em.to_vec()` into the returned `CtOption<(Vec<u8>, u32)>`. The caller then allocated again with `out[index..].to_vec()`. Two heap allocations per decryption instead of one. Unlike PKCS#1 v1.5 which takes `Vec<u8>` by value, OAEP was forced to clone due to borrowing. The clone was useless—the caller already had `em`. Changed return type from `CtOption<(Vec<u8>, u32)>` to `CtOption<u32>` (index only). Callers now use `em` directly. One allocation removed; behavior and constant-time properties unchanged.
1 parent 16d8eaf commit 1e94064

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

src/algorithms/oaep.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ where
162162
return Err(Error::Decryption);
163163
}
164164

165-
let (out, index) = res.unwrap();
165+
let index = res.unwrap();
166166

167-
Ok(out[index as usize..].to_vec())
167+
Ok(em[index as usize..].to_vec())
168168
}
169169

170170
///Decrypts OAEP padding.
@@ -205,9 +205,9 @@ where
205205
return Err(Error::Decryption);
206206
}
207207

208-
let (out, index) = res.unwrap();
208+
let index = res.unwrap();
209209

210-
Ok(out[index as usize..].to_vec())
210+
Ok(em[index as usize..].to_vec())
211211
}
212212

213213
/// Decrypts OAEP padding. It returns one or zero in valid that indicates whether the
@@ -219,7 +219,7 @@ fn decrypt_inner<MGF: FnMut(&mut [u8], &mut [u8])>(
219219
expected_p_hash: &[u8],
220220
k: usize,
221221
mut mgf: MGF,
222-
) -> Result<CtOption<(Vec<u8>, u32)>> {
222+
) -> Result<CtOption<u32>> {
223223
if k < 11 {
224224
return Err(Error::Decryption);
225225
}
@@ -256,8 +256,5 @@ fn decrypt_inner<MGF: FnMut(&mut [u8], &mut [u8])>(
256256

257257
let valid = first_byte_is_zero & hash_are_equal & !nonzero_before_one & !looking_for_index;
258258

259-
Ok(CtOption::new(
260-
(em.to_vec(), index + 2 + (h_size * 2) as u32),
261-
valid,
262-
))
259+
Ok(CtOption::new(index + 2 + (h_size * 2) as u32, valid))
263260
}

0 commit comments

Comments
 (0)