Skip to content

Commit f8ea7ff

Browse files
Merge pull request #12212 from gasbytes/rust-crate-fips-v6-fixes
Rust wrapper: FIPS v6 support
2 parents 49b7f79 + de592ef commit f8ea7ff

13 files changed

Lines changed: 385 additions & 23 deletions

File tree

‎wrapper/rust/wolfssl-wolfcrypt/build.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,13 @@ fn scan_cfg() -> Result<()> {
678678
println!("cargo::metadata=FIPS_V5=1");
679679
}
680680

681+
// FIPS v6 accepts a caller supplied seed in wc_RNG_DRBG_Reseed, newer
682+
// modules refuse it, and the bindings look the same either way.
683+
println!("cargo::rustc-check-cfg=cfg(fips_v6)");
684+
if fips && version("HAVE_FIPS_VERSION_MAJOR").or_else(|| version("HAVE_FIPS_VERSION")) == Some(6) {
685+
println!("cargo::rustc-cfg=fips_v6");
686+
}
687+
681688
/* hkdf */
682689
check_cfg(&binding, "wc_HKDF_Extract", "hkdf");
683690
check_cfg(&binding, "wc_HKDF_Extract_ex", "hkdf_ex");
@@ -738,7 +745,12 @@ fn scan_cfg() -> Result<()> {
738745
* unable to do OAEP. Require the OAEP sentinel from headers.h too. */
739746
check_cfg_if(&binding, "wc_RsaPublicEncrypt_ex", "rsa_oaep",
740747
has_symbol(&binding, "WOLFSSL_RUST_HAVE_RSA_OAEP"));
741-
check_cfg(&binding, "wc_RsaSetRNG", "rsa_setrng");
748+
// Builds without RSA blinding, such as FIPS v5 and v6, have no
749+
// wc_RsaSetRNG. Crates that depend on this one see it as
750+
// DEP_WOLFSSL_RSA_SETRNG.
751+
if check_cfg(&binding, "wc_RsaSetRNG", "rsa_setrng") {
752+
println!("cargo::metadata=RSA_SETRNG=1");
753+
}
742754
/* The RSA "only" build options subtract API, so each cfg names what is
743755
* left rather than the C macro: rsa_private is !WOLFSSL_RSA_PUBLIC_ONLY,
744756
* rsa_sign is !WOLFSSL_RSA_VERIFY_ONLY and rsa_ssl_verify is

‎wrapper/rust/wolfssl-wolfcrypt/src/ecc.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,11 @@ impl ECC {
975975
/// # Example
976976
///
977977
/// ```rust
978+
/// #[cfg(fips)]
979+
/// {
980+
/// use wolfssl_wolfcrypt::fips;
981+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
982+
/// }
978983
/// #[cfg(all(ecc_import, random))]
979984
/// {
980985
/// use wolfssl_wolfcrypt::random::RNG;
@@ -1363,6 +1368,11 @@ impl ECC {
13631368
/// # Example
13641369
///
13651370
/// ```rust
1371+
/// #[cfg(fips)]
1372+
/// {
1373+
/// use wolfssl_wolfcrypt::fips;
1374+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
1375+
/// }
13661376
/// #[cfg(all(ecc_export, random))]
13671377
/// {
13681378
/// use wolfssl_wolfcrypt::random::RNG;
@@ -1418,6 +1428,11 @@ impl ECC {
14181428
/// # Example
14191429
///
14201430
/// ```rust
1431+
/// #[cfg(fips)]
1432+
/// {
1433+
/// use wolfssl_wolfcrypt::fips;
1434+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
1435+
/// }
14211436
/// #[cfg(all(ecc_export, random))]
14221437
/// {
14231438
/// use wolfssl_wolfcrypt::random::RNG;
@@ -1474,6 +1489,11 @@ impl ECC {
14741489
/// # Example
14751490
///
14761491
/// ```rust
1492+
/// #[cfg(fips)]
1493+
/// {
1494+
/// use wolfssl_wolfcrypt::fips;
1495+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
1496+
/// }
14771497
/// #[cfg(all(ecc_export, random))]
14781498
/// {
14791499
/// use wolfssl_wolfcrypt::random::RNG;

‎wrapper/rust/wolfssl-wolfcrypt/src/ed25519.rs‎

Lines changed: 116 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ impl Ed25519 {
289289
/// # Example
290290
///
291291
/// ```rust
292+
/// #[cfg(fips)]
293+
/// {
294+
/// use wolfssl_wolfcrypt::fips;
295+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
296+
/// }
292297
/// #[cfg(all(ed25519_make_key, ed25519_export, random))]
293298
/// {
294299
/// use wolfssl_wolfcrypt::random::RNG;
@@ -304,8 +309,16 @@ impl Ed25519 {
304309
pub fn export_key(&self, private: &mut [u8], public: &mut [u8]) -> Result<(usize, usize), i32> {
305310
let mut private_size = crate::buffer_len_to_u32(private.len())?;
306311
let mut public_size = crate::buffer_len_to_u32(public.len())?;
312+
// Older modules such as FIPS v6 export whatever is in memory, so
313+
// refuse a missing part here like newer wolfSSL does, private first.
314+
if self.ws_key.privKeySet() == 0 {
315+
return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
316+
}
317+
if self.ws_key.pubKeySet() == 0 {
318+
return Err(sys::wolfCrypt_ErrorCodes_PUBLIC_KEY_E);
319+
}
307320
let rc = unsafe {
308-
sys::wc_ed25519_export_key(&self.ws_key,
321+
sys::wc_ed25519_export_key(crate::ptr_for_read(&self.ws_key),
309322
private.as_mut_ptr(), &mut private_size,
310323
public.as_mut_ptr(), &mut public_size)
311324
};
@@ -344,8 +357,13 @@ impl Ed25519 {
344357
#[cfg(ed25519_export)]
345358
pub fn export_public(&self, public: &mut [u8]) -> Result<usize, i32> {
346359
let mut public_size = crate::buffer_len_to_u32(public.len())?;
360+
// Older modules such as FIPS v6 export whatever is in memory, so
361+
// refuse a missing part here like newer wolfSSL does.
362+
if self.ws_key.pubKeySet() == 0 {
363+
return Err(sys::wolfCrypt_ErrorCodes_PUBLIC_KEY_E);
364+
}
347365
let rc = unsafe {
348-
sys::wc_ed25519_export_public(&self.ws_key, public.as_mut_ptr(),
366+
sys::wc_ed25519_export_public(crate::ptr_for_read(&self.ws_key), public.as_mut_ptr(),
349367
&mut public_size)
350368
};
351369
if rc != 0 {
@@ -370,6 +388,11 @@ impl Ed25519 {
370388
/// # Example
371389
///
372390
/// ```rust
391+
/// #[cfg(fips)]
392+
/// {
393+
/// use wolfssl_wolfcrypt::fips;
394+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
395+
/// }
373396
/// #[cfg(all(ed25519_make_key, ed25519_export, random))]
374397
/// {
375398
/// use wolfssl_wolfcrypt::random::RNG;
@@ -383,8 +406,13 @@ impl Ed25519 {
383406
#[cfg(ed25519_export)]
384407
pub fn export_private(&self, keyout: &mut [u8]) -> Result<usize, i32> {
385408
let mut keyout_size = crate::buffer_len_to_u32(keyout.len())?;
409+
// Older modules such as FIPS v6 export whatever is in memory, so
410+
// refuse a missing part here like newer wolfSSL does.
411+
if self.ws_key.privKeySet() == 0 {
412+
return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
413+
}
386414
let rc = unsafe {
387-
sys::wc_ed25519_export_private(&self.ws_key, keyout.as_mut_ptr(),
415+
sys::wc_ed25519_export_private(crate::ptr_for_read(&self.ws_key), keyout.as_mut_ptr(),
388416
&mut keyout_size)
389417
};
390418
if rc != 0 {
@@ -409,6 +437,11 @@ impl Ed25519 {
409437
/// # Example
410438
///
411439
/// ```rust
440+
/// #[cfg(fips)]
441+
/// {
442+
/// use wolfssl_wolfcrypt::fips;
443+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
444+
/// }
412445
/// #[cfg(all(ed25519_make_key, ed25519_export, random))]
413446
/// {
414447
/// use wolfssl_wolfcrypt::random::RNG;
@@ -422,8 +455,13 @@ impl Ed25519 {
422455
#[cfg(ed25519_export)]
423456
pub fn export_private_only(&self, private: &mut [u8]) -> Result<usize, i32> {
424457
let mut private_size = crate::buffer_len_to_u32(private.len())?;
458+
// Older modules such as FIPS v6 export whatever is in memory, so
459+
// refuse a missing part here like newer wolfSSL does.
460+
if self.ws_key.privKeySet() == 0 {
461+
return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
462+
}
425463
let rc = unsafe {
426-
sys::wc_ed25519_export_private_only(&self.ws_key,
464+
sys::wc_ed25519_export_private_only(crate::ptr_for_read(&self.ws_key),
427465
private.as_mut_ptr(), &mut private_size)
428466
};
429467
if rc != 0 {
@@ -450,6 +488,11 @@ impl Ed25519 {
450488
/// # Example
451489
///
452490
/// ```rust
491+
/// #[cfg(fips)]
492+
/// {
493+
/// use wolfssl_wolfcrypt::fips;
494+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
495+
/// }
453496
/// #[cfg(all(ed25519_make_key, ed25519_import, ed25519_export, random))]
454497
/// {
455498
/// use wolfssl_wolfcrypt::random::RNG;
@@ -494,6 +537,11 @@ impl Ed25519 {
494537
/// # Example
495538
///
496539
/// ```rust
540+
/// #[cfg(fips)]
541+
/// {
542+
/// use wolfssl_wolfcrypt::fips;
543+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
544+
/// }
497545
/// #[cfg(all(ed25519_make_key, ed25519_import, ed25519_export, random))]
498546
/// {
499547
/// use wolfssl_wolfcrypt::random::RNG;
@@ -534,6 +582,11 @@ impl Ed25519 {
534582
/// # Example
535583
///
536584
/// ```rust
585+
/// #[cfg(fips)]
586+
/// {
587+
/// use wolfssl_wolfcrypt::fips;
588+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
589+
/// }
537590
/// #[cfg(all(ed25519_make_key, ed25519_import, ed25519_export, random))]
538591
/// {
539592
/// use wolfssl_wolfcrypt::random::RNG;
@@ -578,6 +631,11 @@ impl Ed25519 {
578631
/// # Example
579632
///
580633
/// ```rust
634+
/// #[cfg(fips)]
635+
/// {
636+
/// use wolfssl_wolfcrypt::fips;
637+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
638+
/// }
581639
/// #[cfg(all(ed25519_make_key, ed25519_import, ed25519_export, random))]
582640
/// {
583641
/// use wolfssl_wolfcrypt::random::RNG;
@@ -629,6 +687,11 @@ impl Ed25519 {
629687
/// # Example
630688
///
631689
/// ```rust
690+
/// #[cfg(fips)]
691+
/// {
692+
/// use wolfssl_wolfcrypt::fips;
693+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
694+
/// }
632695
/// #[cfg(all(ed25519_make_key, ed25519_import, ed25519_export, random))]
633696
/// {
634697
/// use wolfssl_wolfcrypt::random::RNG;
@@ -678,6 +741,11 @@ impl Ed25519 {
678741
/// # Example
679742
///
680743
/// ```rust
744+
/// #[cfg(fips)]
745+
/// {
746+
/// use wolfssl_wolfcrypt::fips;
747+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
748+
/// }
681749
/// #[cfg(all(ed25519_make_key, ed25519_export, ed25519_import, random))]
682750
/// {
683751
/// use wolfssl_wolfcrypt::random::RNG;
@@ -695,13 +763,25 @@ impl Ed25519 {
695763
#[cfg(ed25519_make_key)]
696764
pub fn make_public(&mut self, pubkey: &mut [u8]) -> Result<(), i32> {
697765
let pubkey_size = crate::buffer_len_to_u32(pubkey.len())?;
766+
// The C code keeps a public half that is already set, so a later
767+
// check_key can still catch a mismatched pair.
768+
// We store the public half here (if present).
769+
let had_public = self.ws_key.pubKeySet() != 0;
698770
let rc = unsafe {
699771
sys::wc_ed25519_make_public(&mut self.ws_key,
700772
pubkey.as_mut_ptr(), pubkey_size)
701773
};
702774
if rc != 0 {
703775
return Err(rc);
704776
}
777+
// Older modules such as FIPS v6 return the public key without storing
778+
// it in the key, so store it here like newer wolfSSL does, but only
779+
// when the key had no public half before.
780+
if !had_public {
781+
let public = &pubkey[..Self::PUB_KEY_SIZE];
782+
self.ws_key.p.copy_from_slice(public);
783+
self.ws_key.k[Self::KEY_SIZE..].copy_from_slice(public);
784+
}
705785
Ok(())
706786
}
707787

@@ -958,6 +1038,10 @@ impl Ed25519 {
9581038
pub fn sign_msg_ex(&mut self, din: &[u8], context: Option<&[u8]>, typ: u8, signature: &mut [u8]) -> Result<usize, i32> {
9591039
let din_size = crate::buffer_len_to_u32(din.len())?;
9601040
let (context_ptr, context_size) = Self::check_variant_context(typ, context)?;
1041+
// Older modules such as FIPS v6 take a prehash of any length.
1042+
if typ == Self::ED25519PH && din.len() != Self::PREHASH_SIZE {
1043+
return Err(sys::wolfCrypt_ErrorCodes_BAD_LENGTH_E);
1044+
}
9611045
let mut signature_size = crate::buffer_len_to_u32(signature.len())?;
9621046
let rc = unsafe {
9631047
sys::wc_ed25519_sign_msg_ex(din.as_ptr(), din_size,
@@ -1253,6 +1337,10 @@ impl Ed25519 {
12531337
let signature_size = crate::buffer_len_to_u32(signature.len())?;
12541338
let din_size = crate::buffer_len_to_u32(din.len())?;
12551339
let (context_ptr, context_size) = Self::check_variant_context(typ, context)?;
1340+
// Older modules such as FIPS v6 take a prehash of any length.
1341+
if typ == Self::ED25519PH && din.len() != Self::PREHASH_SIZE {
1342+
return Err(sys::wolfCrypt_ErrorCodes_BAD_LENGTH_E);
1343+
}
12561344
let mut res = 0i32;
12571345
let rc = unsafe {
12581346
sys::wc_ed25519_verify_msg_ex(signature.as_ptr(), signature_size,
@@ -1468,7 +1556,7 @@ impl Ed25519 {
14681556
/// }
14691557
/// ```
14701558
pub fn size(&self) -> Result<usize, i32> {
1471-
let rc = unsafe { sys::wc_ed25519_size(&self.ws_key) };
1559+
let rc = unsafe { sys::wc_ed25519_size(crate::ptr_for_read(&self.ws_key)) };
14721560
if rc < 0 {
14731561
return Err(rc);
14741562
}
@@ -1496,7 +1584,7 @@ impl Ed25519 {
14961584
/// }
14971585
/// ```
14981586
pub fn priv_size(&self) -> Result<usize, i32> {
1499-
let rc = unsafe { sys::wc_ed25519_priv_size(&self.ws_key) };
1587+
let rc = unsafe { sys::wc_ed25519_priv_size(crate::ptr_for_read(&self.ws_key)) };
15001588
if rc < 0 {
15011589
return Err(rc);
15021590
}
@@ -1524,7 +1612,7 @@ impl Ed25519 {
15241612
/// }
15251613
/// ```
15261614
pub fn pub_size(&self) -> Result<usize, i32> {
1527-
let rc = unsafe { sys::wc_ed25519_pub_size(&self.ws_key) };
1615+
let rc = unsafe { sys::wc_ed25519_pub_size(crate::ptr_for_read(&self.ws_key)) };
15281616
if rc < 0 {
15291617
return Err(rc);
15301618
}
@@ -1552,7 +1640,7 @@ impl Ed25519 {
15521640
/// }
15531641
/// ```
15541642
pub fn sig_size(&self) -> Result<usize, i32> {
1555-
let rc = unsafe { sys::wc_ed25519_sig_size(&self.ws_key) };
1643+
let rc = unsafe { sys::wc_ed25519_sig_size(crate::ptr_for_read(&self.ws_key)) };
15561644
if rc < 0 {
15571645
return Err(rc);
15581646
}
@@ -1693,6 +1781,11 @@ mod signature_impl {
16931781
/// # Example
16941782
///
16951783
/// ```rust
1784+
/// #[cfg(fips)]
1785+
/// {
1786+
/// use wolfssl_wolfcrypt::fips;
1787+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
1788+
/// }
16961789
/// #[cfg(all(feature = "signature", ed25519_make_key, ed25519_sign, ed25519_export, random))]
16971790
/// {
16981791
/// use wolfssl_wolfcrypt::random::RNG;
@@ -1720,6 +1813,11 @@ mod signature_impl {
17201813
/// # Example
17211814
///
17221815
/// ```rust
1816+
/// #[cfg(fips)]
1817+
/// {
1818+
/// use wolfssl_wolfcrypt::fips;
1819+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
1820+
/// }
17231821
/// #[cfg(all(feature = "signature", ed25519_make_key, ed25519_import, ed25519_export, ed25519_sign, random))]
17241822
/// {
17251823
/// use wolfssl_wolfcrypt::random::RNG;
@@ -1757,6 +1855,11 @@ mod signature_impl {
17571855
/// # Example
17581856
///
17591857
/// ```rust
1858+
/// #[cfg(fips)]
1859+
/// {
1860+
/// use wolfssl_wolfcrypt::fips;
1861+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
1862+
/// }
17601863
/// #[cfg(all(feature = "signature", ed25519_make_key, ed25519_import, ed25519_export, ed25519_sign, random))]
17611864
/// {
17621865
/// use wolfssl_wolfcrypt::random::RNG;
@@ -1799,6 +1902,11 @@ mod signature_impl {
17991902
/// # Example
18001903
///
18011904
/// ```rust
1905+
/// #[cfg(fips)]
1906+
/// {
1907+
/// use wolfssl_wolfcrypt::fips;
1908+
/// fips::set_private_key_read_enable(1).expect("Error with set_private_key_read_enable()");
1909+
/// }
18021910
/// #[cfg(all(feature = "signature", ed25519_make_key, ed25519_export, ed25519_sign, random))]
18031911
/// {
18041912
/// use wolfssl_wolfcrypt::random::RNG;

0 commit comments

Comments
 (0)