@@ -363,6 +363,105 @@ static int test_aes_tag_fixed_enc(const EVP_CIPHER *cipher,
363363 return err ;
364364}
365365
366+ static int test_aes_tag_enc_ossh (const EVP_CIPHER * cipher ,
367+ unsigned char * key , unsigned char * iv ,
368+ unsigned char * aad , unsigned char * msg , int len , unsigned char * enc ,
369+ unsigned char * tag )
370+ {
371+ int err ;
372+ EVP_CIPHER_CTX * encCtx ;
373+ unsigned int tagLen = 16 ;
374+ char lastiv [1 ];
375+
376+ /* Test encryption flow used by openSSH */
377+ err = (encCtx = EVP_CIPHER_CTX_new ()) == NULL ;
378+ if (err == 0 ) {
379+ err = EVP_CipherInit (encCtx , cipher , NULL , iv , 1 ) != 1 ;
380+ }
381+ if (err == 0 ) {
382+ err = EVP_CIPHER_CTX_ctrl (encCtx , EVP_CTRL_GCM_SET_IV_FIXED , -1 ,
383+ iv ) != 1 ;
384+ }
385+ if (err == 0 ) {
386+ err = EVP_CipherInit (encCtx , NULL , key , NULL , -1 ) != 1 ;
387+ }
388+ if (err == 0 ) {
389+ err = EVP_CIPHER_CTX_ctrl (encCtx , EVP_CTRL_GCM_IV_GEN , 1 ,
390+ lastiv ) != 1 ;
391+ }
392+ if (err == 0 ) {
393+ err = EVP_Cipher (encCtx , NULL , aad , (int )strlen ((char * )aad )) < 0 ;
394+ }
395+ if (err == 0 ) {
396+ err = EVP_Cipher (encCtx , enc , msg , len ) < 0 ;
397+ }
398+ if (err == 0 ) {
399+ err = EVP_Cipher (encCtx , NULL , NULL , 0 ) < 0 ;
400+ }
401+ if (err == 0 ) {
402+ err = EVP_CIPHER_CTX_ctrl (encCtx , EVP_CTRL_GCM_GET_TAG , tagLen ,
403+ tag ) != 1 ;
404+ }
405+ if (err == 0 ) {
406+ PRINT_BUFFER ("Encrypted" , enc , len );
407+ PRINT_BUFFER ("Tag" , tag , 16 );
408+ }
409+
410+ EVP_CIPHER_CTX_free (encCtx );
411+ return err ;
412+ }
413+
414+ static int test_aes_tag_dec_ossh (const EVP_CIPHER * cipher ,
415+ unsigned char * key , unsigned char * iv ,
416+ unsigned char * aad , unsigned char * msg , int len , unsigned char * enc ,
417+ unsigned char * tag , unsigned char * dec )
418+ {
419+ int err ;
420+ EVP_CIPHER_CTX * decCtx ;
421+ unsigned int tagLen = 16 ;
422+ char lastiv [1 ];
423+
424+ /* Test decryption flow used by openSSH */
425+ err = (decCtx = EVP_CIPHER_CTX_new ()) == NULL ;
426+ if (err == 0 ) {
427+ err = EVP_CipherInit (decCtx , cipher , NULL , iv , 0 ) != 1 ;
428+ }
429+ if (err == 0 ) {
430+ err = EVP_CIPHER_CTX_ctrl (decCtx , EVP_CTRL_GCM_SET_IV_FIXED , -1 ,
431+ iv ) != 1 ;
432+ }
433+ if (err == 0 ) {
434+ err = EVP_CipherInit (decCtx , NULL , key , NULL , -1 ) != 1 ;
435+ }
436+ if (err == 0 ) {
437+ err = EVP_CIPHER_CTX_ctrl (decCtx , EVP_CTRL_GCM_IV_GEN , 1 ,
438+ lastiv ) != 1 ;
439+ }
440+ if (err == 0 ) {
441+ err = EVP_CIPHER_CTX_ctrl (decCtx , EVP_CTRL_GCM_SET_TAG , tagLen ,
442+ tag ) != 1 ;
443+ }
444+ if (err == 0 ) {
445+ err = EVP_Cipher (decCtx , NULL , aad , (int )strlen ((char * )aad )) < 0 ;
446+ }
447+ if (err == 0 ) {
448+ err = EVP_Cipher (decCtx , dec , enc , len ) < 0 ;
449+ }
450+ if (err == 0 ) {
451+ err = EVP_Cipher (decCtx , NULL , NULL , 0 ) < 0 ;
452+ }
453+ if (err == 0 && dec != NULL && msg != NULL ) {
454+ PRINT_BUFFER ("Decrypted" , dec , len );
455+
456+ if (memcmp (dec , msg , len ) != 0 ) {
457+ err = 1 ;
458+ }
459+ }
460+
461+ EVP_CIPHER_CTX_free (decCtx );
462+ return err ;
463+ }
464+
366465static int test_aes_tag_fixed (void * data , const char * cipher ,
367466 int keyLen , int ivFixedLen , int ivLen )
368467{
@@ -418,6 +517,26 @@ static int test_aes_tag_fixed(void *data, const char *cipher,
418517 err = test_aes_tag_dec (ocipher , key , iv , ivLen , aad , msg ,
419518 sizeof (msg ), enc , tag , dec , 0 , 0 );
420519 }
520+ if (err == 0 ) {
521+ PRINT_MSG ("Encrypt with wolfprovider" );
522+ test_aes_tag_enc_ossh (wcipher , key , iv ,
523+ aad , msg , sizeof (msg ), enc , tag );
524+ }
525+ if (err == 0 ) {
526+ PRINT_MSG ("Decrypt with OpenSSL" );
527+ test_aes_tag_dec_ossh (ocipher , key , iv ,
528+ aad , msg , sizeof (msg ), enc , tag , dec );
529+ }
530+ if (err == 0 ) {
531+ PRINT_MSG ("Encrypt with OpenSSL" );
532+ test_aes_tag_enc_ossh (ocipher , key , iv ,
533+ aad , msg , sizeof (msg ), enc , tag );
534+ }
535+ if (err == 0 ) {
536+ PRINT_MSG ("Decrypt with wolfprovider" );
537+ test_aes_tag_dec_ossh (wcipher , key , iv ,
538+ aad , msg , sizeof (msg ), enc , tag , dec );
539+ }
421540
422541 EVP_CIPHER_free (wcipher );
423542 EVP_CIPHER_free (ocipher );
0 commit comments