@@ -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