@@ -211,6 +211,121 @@ fn test_ph_sign_verify() {
211211 assert ! ( signature_valid) ;
212212}
213213
214+ // The following tests exercise the Ok(false) return path (a well-formed but
215+ // invalid signature) of every Ed25519 verify function. Each test signs a
216+ // message, then flips a bit in the R portion (first byte) of the signature.
217+ // This keeps the signature structurally valid (the S portion remains less
218+ // than the group order) but makes the verification comparison fail, which is
219+ // expected to produce Ok(false).
220+ //
221+ // The underlying wolfCrypt verify functions return SIG_VERIFY_E for a
222+ // well-formed but invalid signature instead of returning 0 with the result
223+ // flag cleared. The Rust wrappers map SIG_VERIFY_E to Ok(false) so that an
224+ // invalid signature is reported as Ok(false) rather than an error.
225+
226+ #[ test]
227+ #[ cfg( all( ed25519_sign, ed25519_verify) ) ]
228+ fn test_verify_msg_bad_sig ( ) {
229+ common:: setup ( ) ;
230+
231+ let mut rng = RNG :: new ( ) . expect ( "Error creating RNG" ) ;
232+ let mut ed = Ed25519 :: generate ( & mut rng) . expect ( "Error with generate()" ) ;
233+
234+ let message = [ 0x42u8 , 33 , 55 , 66 ] ;
235+ let mut signature = [ 0u8 ; Ed25519 :: SIG_SIZE ] ;
236+ ed. sign_msg ( & message, & mut signature) . expect ( "Error with sign_msg()" ) ;
237+ signature[ 0 ] ^= 0x01 ;
238+
239+ assert_eq ! ( ed. verify_msg( & signature, & message) , Ok ( false ) ) ;
240+ }
241+
242+ #[ test]
243+ #[ cfg( all( ed25519_sign, ed25519_verify) ) ]
244+ fn test_verify_msg_ex_bad_sig ( ) {
245+ common:: setup ( ) ;
246+
247+ let mut rng = RNG :: new ( ) . expect ( "Error creating RNG" ) ;
248+ let mut ed = Ed25519 :: generate ( & mut rng) . expect ( "Error with generate()" ) ;
249+
250+ let message = [ 0x42u8 , 33 , 55 , 66 ] ;
251+ let mut signature = [ 0u8 ; Ed25519 :: SIG_SIZE ] ;
252+ ed. sign_msg_ex ( & message, None , Ed25519 :: ED25519 , & mut signature) . expect ( "Error with sign_msg_ex()" ) ;
253+ signature[ 0 ] ^= 0x01 ;
254+
255+ assert_eq ! ( ed. verify_msg_ex( & signature, & message, None , Ed25519 :: ED25519 ) , Ok ( false ) ) ;
256+ }
257+
258+ #[ test]
259+ #[ cfg( all( ed25519_sign, ed25519_verify) ) ]
260+ fn test_verify_msg_ctx_bad_sig ( ) {
261+ common:: setup ( ) ;
262+
263+ let mut rng = RNG :: new ( ) . expect ( "Error creating RNG" ) ;
264+ let mut ed = Ed25519 :: generate ( & mut rng) . expect ( "Error with generate()" ) ;
265+
266+ let message = [ 0x42u8 , 33 , 55 , 66 ] ;
267+ let context = b"context" ;
268+ let mut signature = [ 0u8 ; Ed25519 :: SIG_SIZE ] ;
269+ ed. sign_msg_ctx ( & message, context, & mut signature) . expect ( "Error with sign_msg_ctx()" ) ;
270+ signature[ 0 ] ^= 0x01 ;
271+
272+ assert_eq ! ( ed. verify_msg_ctx( & signature, & message, context) , Ok ( false ) ) ;
273+ }
274+
275+ #[ test]
276+ #[ cfg( all( ed25519_sign, ed25519_verify) ) ]
277+ fn test_verify_msg_ph_bad_sig ( ) {
278+ common:: setup ( ) ;
279+
280+ let mut rng = RNG :: new ( ) . expect ( "Error creating RNG" ) ;
281+ let mut ed = Ed25519 :: generate ( & mut rng) . expect ( "Error with generate()" ) ;
282+
283+ let message = [ 0x42u8 , 33 , 55 , 66 ] ;
284+ let context = b"context" ;
285+ let mut signature = [ 0u8 ; Ed25519 :: SIG_SIZE ] ;
286+ ed. sign_msg_ph ( & message, Some ( context) , & mut signature) . expect ( "Error with sign_msg_ph()" ) ;
287+ signature[ 0 ] ^= 0x01 ;
288+
289+ assert_eq ! ( ed. verify_msg_ph( & signature, & message, Some ( context) ) , Ok ( false ) ) ;
290+ }
291+
292+ #[ test]
293+ #[ cfg( all( ed25519_sign, ed25519_verify) ) ]
294+ fn test_verify_hash_ph_bad_sig ( ) {
295+ common:: setup ( ) ;
296+
297+ let mut rng = RNG :: new ( ) . expect ( "Error creating RNG" ) ;
298+ let mut ed = Ed25519 :: generate ( & mut rng) . expect ( "Error with generate()" ) ;
299+
300+ let hash = [ 0x55u8 ; 64 ] ;
301+ let context = b"context" ;
302+ let mut signature = [ 0u8 ; Ed25519 :: SIG_SIZE ] ;
303+ ed. sign_hash_ph ( & hash, Some ( context) , & mut signature) . expect ( "Error with sign_hash_ph()" ) ;
304+ signature[ 0 ] ^= 0x01 ;
305+
306+ assert_eq ! ( ed. verify_hash_ph( & signature, & hash, Some ( context) ) , Ok ( false ) ) ;
307+ }
308+
309+ #[ test]
310+ #[ cfg( all( ed25519_sign, ed25519_streaming_verify) ) ]
311+ fn test_verify_msg_final_bad_sig ( ) {
312+ common:: setup ( ) ;
313+
314+ let mut rng = RNG :: new ( ) . expect ( "Error creating RNG" ) ;
315+ let mut ed = Ed25519 :: generate ( & mut rng) . expect ( "Error with generate()" ) ;
316+
317+ let message = [ 0x42u8 , 33 , 55 , 66 ] ;
318+ let mut signature = [ 0u8 ; Ed25519 :: SIG_SIZE ] ;
319+ ed. sign_msg ( & message, & mut signature) . expect ( "Error with sign_msg()" ) ;
320+ signature[ 0 ] ^= 0x01 ;
321+
322+ ed. verify_msg_init ( & signature, None , Ed25519 :: ED25519 ) . expect ( "Error with verify_msg_init()" ) ;
323+ ed. verify_msg_update ( & message[ 0 ..2 ] ) . expect ( "Error with verify_msg_update()" ) ;
324+ ed. verify_msg_update ( & message[ 2 ..4 ] ) . expect ( "Error with verify_msg_update()" ) ;
325+
326+ assert_eq ! ( ed. verify_msg_final( & signature) , Ok ( false ) ) ;
327+ }
328+
214329#[ test]
215330#[ cfg( all( ed25519_import, ed25519_export) ) ]
216331fn test_import_export ( ) {
0 commit comments