title: AEAD Decryption with OpenSSL in C
body: <p>I have the following code to decrypt some AEAD encrypted password:</p>
  
  <pre class="lang-c prettyprint-override"><code>int aead_decrypt(char *cipher_password, int len_cipher_password, char *tag, char *key, char *iv, int len_iv, char **plaintext_password) {
      EVP_CIPHER_CTX *ctx;
      int len;
      int plaintext_len;
  
      // Cipher_password len always greater or equal to plaintext
      *plaintext_password = (unsigned char *)malloc(len_cipher_password);
      if(*plaintext_password == 0) {
          fprintf(stderr, "malloc() failure\n");
          free(*plaintext_password);
          return -1;
      }
  
      if(!(ctx = EVP_CIPHER_CTX_new())) {
          fprintf(stderr, "EVP_CIPHER_CTX_new() failure\n");
          ERR_print_errors_fp(stderr);
          return -1;
      }
  
      if(!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL)) {
          fprintf(stderr, "EVP_DecryptInit_ex() failure\n");
          ERR_print_errors_fp(stderr);
          return -1;
      }
  
      if(!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv)) {
          fprintf(stderr, "EVP_DecryptInit_ex() failure\n");
          ERR_print_errors_fp(stderr);
          return -1;
      }
  
      if(!EVP_DecryptUpdate(ctx, *plaintext_password, &amp;len, cipher_password, len_cipher_password)) {
          //if(!EVP_DecryptUpdate(ctx, *plaintext_password, &amp;len, padded_cipher_password, len_padded_cipher_password)) {
          fprintf(stderr, "EVP_DecryptUpdate() failure\n");
          ERR_print_errors_fp(stderr);
          return -1;
      }
  
      if(1!=EVP_DecryptFinal_ex(ctx, *plaintext_password+len, &amp;len)) {
          fprintf(stderr, "EVP_DecryptFinal_ex() failure\n");
          ERR_print_errors_fp(stderr);
          ERR_print_errors_fp(stdout);
          return -1;
      }
  
      plaintext_len += len;
      (*plaintext_password)[plaintext_len] = '\0';
      EVP_CIPHER_CTX_free(ctx);
  
      return 1;
  }
  </code></pre>
  
  <p>My problem is that the <code>EVP_DecryptFinal_ex()</code> function always fail but without printing any errors. 
  My <code>plaintext_password</code> comes out decrypted but with 16 bytes of garbage at the end because the <code>EVP_DecryptUpdate()</code> function doesn't return the good <code>plaintext_password_len</code>. 
  I thought at first it was because of padding, my cipher_password is often 24-25 bytes long, so i tried adding some as we can see in the different comments but it did not worked out. 
  (Also i know i pass some parameters i don't use but that's not what's important here).
  I don't know where the problem could be and i'm not that familiar with the OpenSSL library.</p>