Skip to content

Commit 9b0dbdb

Browse files
authoredNov 14, 2024
Porting combined errors from mbedtls changes (#372)
Due to downgrade back to v2.28 (from ~3.X), the enhancement in PR #271 was lost in the mbedtls 2.8 branch being used. we need to port these combined errors from mbedtls changes back to the mbedtls 2.8 branch and build with the latest upgrade in rust toolchain version to apply the enhancement. This PR ports said changes to a latest branch forked from mbedtls master and builds the same on latest rustc 1.83.0-nightly (26d8e9255 2024-10-11) version.
1 parent 0e5891d commit 9b0dbdb

27 files changed

+586
-450
lines changed
 

‎Cargo.lock

+42-77
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
@@ -2,7 +2,7 @@
22
name = "mbedtls"
33
# We jumped from v0.9 to v0.12 because v0.10 and v0.11 were based on mbedtls 3.X, which
44
# we decided not to support.
5-
version = "0.12.3"
5+
version = "0.13.0"
66
authors = ["Jethro Beekman <jethro@fortanix.com>"]
77
build = "build.rs"
88
edition = "2018"
@@ -24,8 +24,8 @@ features = ["x509", "ssl"]
2424

2525
[dependencies]
2626
bitflags = "1"
27-
serde = { version = "1.0.7", default-features = false, features = ["alloc"] }
28-
serde_derive = "1.0.7"
27+
serde = { version = "1.0.214", default-features = false, features = ["alloc"] }
28+
serde_derive = "1.0.214"
2929
byteorder = { version = "1.0.0", default-features = false }
3030
yasna = { version = "0.2", optional = true, features = [
3131
"num-bigint",

‎mbedtls/src/bignum/mod.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* option. This file may not be copied, modified, or distributed except
77
* according to those terms. */
88

9-
use crate::error::{Error, IntoResult, Result};
9+
use crate::error::Error;
10+
use crate::error::{codes, IntoResult, Result};
1011
use mbedtls_sys::*;
1112

1213
#[cfg(not(feature = "std"))]
@@ -161,7 +162,7 @@ impl Mpi {
161162
pub fn as_u32(&self) -> Result<u32> {
162163
if self.bit_length()? > 32 {
163164
// Not exactly correct but close enough
164-
return Err(Error::MpiBufferTooSmall);
165+
return Err(codes::MpiBufferTooSmall.into());
165166
}
166167

167168
Ok(self.get_limb(0) as u32)
@@ -183,7 +184,7 @@ impl Mpi {
183184
let r = unsafe { mpi_write_string(&self.inner, radix, ::core::ptr::null_mut(), 0, &mut olen) };
184185

185186
if r != ERR_MPI_BUFFER_TOO_SMALL {
186-
return Err(Error::from_mbedtls_code(r));
187+
return Err(r.into());
187188
}
188189

189190
let mut buf = vec![0u8; olen];
@@ -264,7 +265,7 @@ impl Mpi {
264265
let zero = Mpi::new(0)?;
265266

266267
if self < &zero || self >= p {
267-
return Err(Error::MpiBadInputData);
268+
return Err(codes::MpiBadInputData.into());
268269
}
269270
if self == &zero {
270271
return Ok(zero);
@@ -273,12 +274,12 @@ impl Mpi {
273274
// This ignores p=2 (for which this algorithm is valid), as not
274275
// cryptographically interesting.
275276
if p.get_bit(0) == false || p <= &zero {
276-
return Err(Error::MpiBadInputData);
277+
return Err(codes::MpiBadInputData.into());
277278
}
278279

279280
if self.jacobi(p)? != 1 {
280281
// a is not a quadratic residue mod p
281-
return Err(Error::MpiBadInputData);
282+
return Err(codes::MpiBadInputData.into());
282283
}
283284

284285
if (p % 4)?.as_u32()? == 3 {
@@ -325,7 +326,7 @@ impl Mpi {
325326
bo = bo.mod_exp(&two, p)?;
326327
m += 1;
327328
if m >= r {
328-
return Err(Error::MpiBadInputData);
329+
return Err(codes::MpiBadInputData.into());
329330
}
330331
}
331332

@@ -358,7 +359,7 @@ impl Mpi {
358359
let one = Mpi::new(1)?;
359360

360361
if self < &zero || n < &zero || n.get_bit(0) == false {
361-
return Err(Error::MpiBadInputData);
362+
return Err(codes::MpiBadInputData.into());
362363
}
363364

364365
let mut x = self.modulo(n)?;
@@ -431,7 +432,7 @@ impl Mpi {
431432
pub(super) fn mpi_inner_eq_const_time(x: &mpi, y: &mpi) -> core::prelude::v1::Result<bool, Error> {
432433
match mpi_inner_cmp_const_time(x, y) {
433434
Ok(order) => Ok(order == Ordering::Equal),
434-
Err(Error::MpiBadInputData) => Ok(false),
435+
Err(e) if e == codes::MpiBadInputData.into() => Ok(false),
435436
Err(e) => Err(e),
436437
}
437438
}
@@ -779,7 +780,7 @@ mod tests {
779780
])
780781
.unwrap();
781782
assert_eq!(mpi3.less_than_const_time(&mpi3), Ok(false));
782-
assert_eq!(mpi2.less_than_const_time(&mpi3), Err(Error::MpiBadInputData));
783+
assert_eq!(mpi2.less_than_const_time(&mpi3), Err(codes::MpiBadInputData.into()));
783784
}
784785

785786
#[test]

0 commit comments

Comments
 (0)
Please sign in to comment.