@@ -340,8 +340,7 @@ int decrypt_file_AesGCM(const char *in_file, const char *out_file,
340340 byte iv [AES_IV_SIZE ];
341341 byte wolf_magic [strlen (WOLFCRYPT_MAGIC )];
342342 byte key [AES_KEY_SIZE ];
343- byte tag_dec [AESGCM_TAG_SIZE ];
344- byte tag_enc [AESGCM_TAG_SIZE ];
343+ byte tag [AESGCM_TAG_SIZE ];
345344 Aes gcm ;
346345
347346 if (!in_file || !out_file || !key_str ) {
@@ -396,8 +395,7 @@ int decrypt_file_AesGCM(const char *in_file, const char *out_file,
396395 memset (& gcm , 0 , sizeof (Aes ));
397396 memset (iv , 0 , AES_IV_SIZE );
398397 memset (key , 0 , AES_KEY_SIZE );
399- memset (tag_dec , 0 , AESGCM_TAG_SIZE );
400- memset (tag_enc , 0 , AESGCM_TAG_SIZE );
398+ memset (tag , 0 , AESGCM_TAG_SIZE );
401399 strncpy ((char * )key , key_str , AES_KEY_SIZE );
402400
403401 /* Extract a WOLFCRYPT MAGIC | TAG | IV from the cipher file */
@@ -412,12 +410,13 @@ int decrypt_file_AesGCM(const char *in_file, const char *out_file,
412410 ret = AES_GCM_AUTH_E ;
413411 goto exit ;
414412 }
415- read_size = read (in_fd , tag_enc , AESGCM_TAG_SIZE );
413+ read_size = read (in_fd , tag , AESGCM_TAG_SIZE );
416414 if (read_size != AESGCM_TAG_SIZE ) {
417415 perror ("read" );
418416 ret = -1 ;
419417 goto exit ;
420418 }
419+
421420 read_size = read (in_fd , iv , AES_IV_SIZE );
422421 if (read_size != AES_IV_SIZE ) {
423422 perror ("read" );
@@ -443,12 +442,9 @@ int decrypt_file_AesGCM(const char *in_file, const char *out_file,
443442 }
444443
445444 if (ret == 0 ) {
446- ret = wc_AesGcmEncryptFinal (& gcm , tag_dec , AESGCM_TAG_SIZE );
447- if (ret == 0 && (memcmp (tag_enc , tag_dec , AESGCM_TAG_SIZE ) != 0 )) {
448- perror ("TAG didn't match\n" );
449- ret = AES_GCM_AUTH_E ;
450- goto exit ;
451- }
445+ /* The tag param is used to compare to the
446+ calculated tag during decryption */
447+ ret = wc_AesGcmDecryptFinal (& gcm , tag , AESGCM_TAG_SIZE );
452448 }
453449exit :
454450 free (in_buf );
0 commit comments