Skip to content

Commit a84d701

Browse files
authored
Try #255:
2 parents aa500d0 + 35c78e8 commit a84d701

File tree

9 files changed

+201
-208
lines changed

9 files changed

+201
-208
lines changed

Cargo.lock

+49-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mbedtls/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ byteorder = "1.0.0"
2727
yasna = { version = "0.2", optional = true, features = ["num-bigint", "bit-vec"] }
2828
num-bigint = { version = "0.2", optional = true }
2929
bit-vec = { version = "0.5", optional = true }
30-
block-modes = { version = "0.3", optional = true }
31-
rc2 = { version = "0.3", optional = true }
30+
cbc = { version = "0.1.2", optional = true }
31+
rc2 = { version = "0.8.1", optional = true }
3232
cfg-if = "1.0.0"
3333

3434
[target.x86_64-fortanix-unknown-sgx.dependencies]
@@ -67,7 +67,7 @@ time = ["mbedtls-sys-auto/time"]
6767
padlock = ["mbedtls-sys-auto/padlock"]
6868
dsa = ["std", "yasna", "num-bigint", "bit-vec"]
6969
pkcs12 = ["std", "yasna"]
70-
pkcs12_rc2 = ["pkcs12", "rc2", "block-modes"]
70+
pkcs12_rc2 = ["pkcs12", "rc2", "cbc"]
7171

7272
[[example]]
7373
name = "client"

mbedtls/src/alloc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ impl<T> Drop for Box<T> {
5858
}
5959
}
6060

61+
unsafe impl<T: Send> Send for Box<T> {}
62+
unsafe impl<T: Sync> Sync for Box<T> {}
63+
6164
#[repr(transparent)]
6265
pub struct List<T> {
6366
pub(crate) inner: Option<Box<T>>
6467
}
65-

mbedtls/src/lib.rs

+66
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,69 @@ cfg_if::cfg_if! {
158158
pub unsafe fn set_global_debug_threshold(threshold: i32) {
159159
mbedtls_sys::debug_set_threshold(threshold);
160160
}
161+
162+
#[cfg(test)]
163+
mod tests {
164+
#[allow(dead_code)]
165+
/// Utilities for testing whether types implement certain traits.
166+
///
167+
/// For each trait `Trait` that you want to be able to test, you should
168+
/// implement:
169+
/// ```ignore
170+
/// impl<T: “Trait”> Testable<dyn “Trait”> for T {}
171+
/// ```
172+
///
173+
/// Then, to test whether a type `Type` implements `Trait`, call:
174+
/// ```ignore
175+
/// TestTrait::<dyn “Trait”, “Type”>::new().impls_trait()
176+
/// ```
177+
/// This returns a `bool` indicating whether the trait is implemented.
178+
// This relies on auto-deref to distinguish between types that do and don't
179+
// implement the trait.
180+
mod testtrait {
181+
use core::marker::PhantomData;
182+
183+
pub struct NonImplTrait<T> {
184+
inner: PhantomData<T>
185+
}
186+
187+
pub struct TestTrait<TraitObj: ?Sized, Type> {
188+
non_impl: NonImplTrait<Type>,
189+
phantom: PhantomData<*const TraitObj>,
190+
}
191+
192+
pub trait Testable<T: ?Sized> {}
193+
194+
impl<TraitObj: ?Sized, Type> TestTrait<TraitObj, Type> {
195+
pub fn new() -> Self {
196+
TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData }
197+
}
198+
}
199+
200+
impl<TraitObj: ?Sized, Type: Testable<TraitObj>> TestTrait<TraitObj, Type> {
201+
pub fn impls_trait(&self) -> bool {
202+
true
203+
}
204+
}
205+
206+
impl<T> NonImplTrait<T> {
207+
pub fn impls_trait(&self) -> bool {
208+
false
209+
}
210+
}
211+
212+
impl<TraitObj: ?Sized, Type> core::ops::Deref for TestTrait<TraitObj, Type> {
213+
type Target = NonImplTrait<Type>;
214+
215+
fn deref(&self) -> &NonImplTrait<Type> {
216+
&self.non_impl
217+
}
218+
}
219+
}
220+
221+
pub use testtrait::{TestTrait, Testable};
222+
223+
impl<T: Send> Testable<dyn Send> for T {}
224+
impl<T: Sync> Testable<dyn Sync> for T {}
225+
impl<T: Send + Sync> Testable<dyn Send + Sync> for T {}
226+
}

mbedtls/src/pk/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ extern "C" fn alloc_custom_pk_ctx() -> *mut c_void {
113113
}
114114

115115
unsafe extern "C" fn free_custom_pk_ctx(p: *mut c_void) {
116-
Box::from_raw(p as *mut CustomPkContext);
116+
let _ = Box::from_raw(p as *mut CustomPkContext);
117117
}
118118

119119
extern "C" fn custom_pk_can_do(_t: u32) -> i32 {

mbedtls/src/pkcs12/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -601,17 +601,17 @@ fn decrypt_3des(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> Pkcs12Result<Vec<u8
601601

602602
#[cfg(feature = "pkcs12_rc2")]
603603
fn decrypt_rc2(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> Pkcs12Result<Vec<u8>> {
604-
use block_modes::BlockMode;
604+
use rc2::cipher::{block_padding::Pkcs7, BlockDecryptMut, KeyIvInit};
605605

606606
let cipher =
607-
block_modes::Cbc::<rc2::Rc2, block_modes::block_padding::Pkcs7>::new_var(&key, &iv)
607+
cbc::Decryptor::<rc2::Rc2>::new_from_slices(key, iv)
608608
.map_err(|e| Pkcs12Error::Custom(format!("{:?}", e)))?;
609609

610610
let mut pt = ciphertext.to_vec();
611611
let pt = cipher
612-
.decrypt(&mut pt)
612+
.decrypt_padded_mut::<Pkcs7>(&mut pt)
613613
.map_err(|e| Pkcs12Error::Custom(format!("{:?}", e)))?;
614-
return Ok(pt.to_owned());
614+
Ok(pt.to_owned())
615615
}
616616

617617
#[cfg(not(feature = "pkcs12_rc2"))]

mbedtls/src/wrapper_macros.rs

+1-59
Original file line numberDiff line numberDiff line change
@@ -301,69 +301,11 @@ macro_rules! getter {
301301

302302
#[cfg(test)]
303303
mod tests {
304-
#[allow(dead_code)]
305-
/// Utilities for testing whether types implement certain traits.
306-
///
307-
/// For each trait `Trait` that you want to be able to test, you should
308-
/// implement:
309-
/// ```ignore
310-
/// impl<T: “Trait”> Testable<dyn “Trait”> for T {}
311-
/// ```
312-
///
313-
/// Then, to test whether a type `Type` implements `Trait`, call:
314-
/// ```ignore
315-
/// TestTrait::<dyn “Trait”, “Type”>::new().impls_trait()
316-
/// ```
317-
/// This returns a `bool` indicating whether the trait is implemented.
318-
// This relies on auto-deref to distinguish between types that do and don't
319-
// implement the trait.
320-
mod testtrait {
321-
use core::marker::PhantomData;
322-
323-
pub struct NonImplTrait<T> {
324-
inner: PhantomData<T>
325-
}
326-
327-
pub struct TestTrait<TraitObj: ?Sized, Type> {
328-
non_impl: NonImplTrait<Type>,
329-
phantom: PhantomData<*const TraitObj>,
330-
}
331-
332-
pub trait Testable<T: ?Sized> {}
333-
334-
impl<TraitObj: ?Sized, Type> TestTrait<TraitObj, Type> {
335-
pub fn new() -> Self {
336-
TestTrait { non_impl: NonImplTrait { inner: PhantomData }, phantom: PhantomData }
337-
}
338-
}
339-
340-
impl<TraitObj: ?Sized, Type: Testable<TraitObj>> TestTrait<TraitObj, Type> {
341-
pub fn impls_trait(&self) -> bool {
342-
true
343-
}
344-
}
345-
346-
impl<T> NonImplTrait<T> {
347-
pub fn impls_trait(&self) -> bool {
348-
false
349-
}
350-
}
351-
352-
impl<TraitObj: ?Sized, Type> core::ops::Deref for TestTrait<TraitObj, Type> {
353-
type Target = NonImplTrait<Type>;
354-
355-
fn deref(&self) -> &NonImplTrait<Type> {
356-
&self.non_impl
357-
}
358-
}
359-
}
360-
361-
use testtrait::{TestTrait, Testable};
304+
use crate::tests::{TestTrait, Testable};
362305

363306
callback!(RustTest: Fn() -> ());
364307
callback!(NativeTestMut,NativeTest() -> ());
365308

366-
impl<T: Send + Sync> Testable<dyn Send + Sync> for T {}
367309
impl<T: RustTest> Testable<dyn RustTest> for T {}
368310
impl<T: NativeTest> Testable<dyn NativeTest> for T {}
369311
impl<T: NativeTestMut> Testable<dyn NativeTestMut> for T {}

0 commit comments

Comments
 (0)