@@ -344,6 +344,92 @@ static int test_stream_enc_dec(void *data, const char *cipher, int keyLen,
344344 return err ;
345345}
346346
347+ static int test_cipher_null_zero_ex (void * data , const char * cipher , int keyLen ,
348+ int ivLen )
349+ {
350+ int err = 0 ;
351+ unsigned char msg [16 ] = "Test pattern" ;
352+ unsigned char key [32 ];
353+ unsigned char iv [16 ];
354+ unsigned char enc [sizeof (msg ) + 16 ];
355+ EVP_CIPHER * ocipher ;
356+ EVP_CIPHER * wcipher ;
357+ EVP_CIPHER_CTX * ctx ;
358+
359+ (void )data ;
360+
361+ ocipher = EVP_CIPHER_fetch (osslLibCtx , cipher , "" );
362+ wcipher = EVP_CIPHER_fetch (wpLibCtx , cipher , "" );
363+
364+ if (RAND_bytes (key , keyLen ) != 1 ) {
365+ err = 1 ;
366+ }
367+ if (err == 0 ) {
368+ if (RAND_bytes (iv , ivLen ) != 1 ) {
369+ err = 1 ;
370+ }
371+ }
372+
373+ /* Test that a final call with NULL/NULL/0 yields the correct return
374+ * value, flow mimics that of libssh2 */
375+ err = (ctx = EVP_CIPHER_CTX_new ()) == NULL ;
376+ if (err == 0 ) {
377+ err = EVP_CipherInit (ctx , ocipher , key , iv , 1 ) != 1 ;
378+ }
379+ if (err == 0 ) {
380+ err = EVP_Cipher (ctx , enc , msg , sizeof (msg )) <= 0 ;
381+ }
382+ /* Return is 0, not negative value for NULL/NULL/0 input */
383+ if (err == 0 ) {
384+ err = EVP_Cipher (ctx , NULL , NULL , 0 ) != 0 ;
385+ }
386+ EVP_CIPHER_CTX_free (ctx );
387+
388+ err = (ctx = EVP_CIPHER_CTX_new ()) == NULL ;
389+ if (err == 0 ) {
390+ err = EVP_CipherInit (ctx , wcipher , key , iv , 1 ) != 1 ;
391+ }
392+ if (err == 0 ) {
393+ err = EVP_Cipher (ctx , enc , msg , sizeof (msg )) <= 0 ;
394+ }
395+ /* Return is 0, not negative value for NULL/NULL/0 input */
396+ if (err == 0 ) {
397+ err = EVP_Cipher (ctx , NULL , NULL , 0 ) != 0 ;
398+ }
399+ EVP_CIPHER_CTX_free (ctx );
400+
401+ EVP_CIPHER_free (wcipher );
402+ EVP_CIPHER_free (ocipher );
403+
404+ return err ;
405+ }
406+
407+ int test_cipher_null_zero (void * data )
408+ {
409+ int err = 0 ;
410+
411+ #ifdef WP_HAVE_AESECB
412+ err = test_cipher_null_zero_ex (data , "AES-256-ECB" , 32 , 16 );
413+ #endif
414+ #ifdef WP_HAVE_AESCBC
415+ if (err == 0 ) {
416+ err = test_cipher_null_zero_ex (data , "AES-256-CBC" , 32 , 16 );
417+ }
418+ #endif
419+ #ifdef WP_HAVE_AESCTR
420+ if (err == 0 ) {
421+ err = test_cipher_null_zero_ex (data , "AES-256-CTR" , 32 , 16 );
422+ }
423+ #endif
424+ #ifdef WP_HAVE_AESCFB
425+ if (err == 0 ) {
426+ err = test_cipher_null_zero_ex (data , "AES-256-CFB" , 32 , 16 );
427+ }
428+ #endif
429+
430+ return err ;
431+ }
432+
347433#endif /* WP_HAVE_DES3CBC || WP_HAVE_AESCBC */
348434
349435/******************************************************************************/
0 commit comments