1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-02-22 19:58:29 +01:00

gpg: Reformat parts of decrypt-data.c

* g10/decrypt-data.c (struct decode_filter_context_s): Rename 'defer'
to 'holdback' and 'defer_filled' to 'holdbacklen'.  Increase size of
holdback to allow for future AEAD decryption.  Turn 'partial' and
'eof_seen' into bit fields.
(decrypt_data): Replace write_status_text by write_Status_printf.
Indent parts of the code.
--

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2020-04-15 20:00:34 +02:00
parent ddc74f50d4
commit 2f39e00b6b
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -1,6 +1,7 @@
/* decrypt-data.c - Decrypt an encrypted data packet /* decrypt-data.c - Decrypt an encrypted data packet
* Copyright (C) 1998, 1999, 2000, 2001, 2005, * Copyright (C) 1998-2001, 2005-2006, 2009 Free Software Foundation, Inc.
* 2006, 2009 Free Software Foundation, Inc. * Copyright (C) 1998-2001, 2005-2006, 2009, 2018 Werner Koch
* Copyright (C) 2020 g10 Code GmbH
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -16,6 +17,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-3.0-or-later
*/ */
#include <config.h> #include <config.h>
@ -37,17 +39,40 @@ static int mdc_decode_filter ( void *opaque, int control, IOBUF a,
static int decode_filter ( void *opaque, int control, IOBUF a, static int decode_filter ( void *opaque, int control, IOBUF a,
byte *buf, size_t *ret_len); byte *buf, size_t *ret_len);
typedef struct decode_filter_context_s /* Our context object. */
struct decode_filter_context_s
{ {
gcry_cipher_hd_t cipher_hd; /* Recounter (max value is 2). We need it because we do not know
gcry_md_hd_t mdc_hash; * whether the iobuf or the outer control code frees this object
char defer[22]; * first. */
int defer_filled;
int eof_seen;
int refcount; int refcount;
int partial; /* Working on a partial length packet. */
size_t length; /* If !partial: Remaining bytes in the packet. */ /* The cipher handle. */
} *decode_filter_ctx_t; gcry_cipher_hd_t cipher_hd;
/* The hash handle for use in MDC mode. */
gcry_md_hd_t mdc_hash;
/* The holdback buffer and its used length. For AEAD we need 32+1
* bytes but we use 48 byte. For MDC we need 22 bytes; here
* holdbacklen will either 0 or 22. */
char holdback[48];
unsigned int holdbacklen;
/* Working on a partial length packet. */
unsigned int partial : 1;
/* EOF indicator with these true values:
* 1 = normal EOF
* 2 = premature EOF (tag or hash incomplete)
* 3 = premature EOF (general) */
unsigned int eof_seen : 2;
/* Remaining bytes in the packet according to the packet header.
* Not used if PARTIAL is true. */
size_t length;
};
typedef struct decode_filter_context_s *decode_filter_ctx_t;
/* Helper to release the decode context. */ /* Helper to release the decode context. */
@ -109,12 +134,8 @@ decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
goto leave; goto leave;
} }
{ write_status_printf (STATUS_DECRYPTION_INFO, "%d %d %d",
char buf[20]; ed->mdc_method, dek->algo, 0);
snprintf (buf, sizeof buf, "%d %d", ed->mdc_method, dek->algo);
write_status_text (STATUS_DECRYPTION_INFO, buf);
}
if (opt.show_session_key) if (opt.show_session_key)
{ {
@ -139,11 +160,13 @@ decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
blocksize = openpgp_cipher_get_algo_blklen (dek->algo); blocksize = openpgp_cipher_get_algo_blklen (dek->algo);
if ( !blocksize || blocksize > 16 ) if ( !blocksize || blocksize > 16 )
log_fatal ("unsupported blocksize %u\n", blocksize ); log_fatal ("unsupported blocksize %u\n", blocksize );
if (1)
{
nprefix = blocksize; nprefix = blocksize;
if ( ed->len && ed->len < (nprefix+2) ) if ( ed->len && ed->len < (nprefix+2) )
{ {
/* An invalid message. We can't check that during parsing /* An invalid message. We can't check that during parsing
because we may not know the used cipher then. */ * because we may not know the used cipher then. */
rc = gpg_error (GPG_ERR_INV_PACKET); rc = gpg_error (GPG_ERR_INV_PACKET);
goto leave; goto leave;
} }
@ -223,6 +246,7 @@ decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
if ( dfx->mdc_hash ) if ( dfx->mdc_hash )
gcry_md_write (dfx->mdc_hash, temp, nprefix+2); gcry_md_write (dfx->mdc_hash, temp, nprefix+2);
}
dfx->refcount++; dfx->refcount++;
dfx->partial = ed->is_partial; dfx->partial = ed->is_partial;
@ -287,16 +311,16 @@ decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
log_assert (dfx->cipher_hd); log_assert (dfx->cipher_hd);
log_assert (dfx->mdc_hash); log_assert (dfx->mdc_hash);
gcry_cipher_decrypt (dfx->cipher_hd, dfx->defer, 22, NULL, 0); gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 22, NULL, 0);
gcry_md_write (dfx->mdc_hash, dfx->defer, 2); gcry_md_write (dfx->mdc_hash, dfx->holdback, 2);
gcry_md_final (dfx->mdc_hash); gcry_md_final (dfx->mdc_hash);
if ( dfx->defer[0] != '\xd3' if ( dfx->holdback[0] != '\xd3'
|| dfx->defer[1] != '\x14' || dfx->holdback[1] != '\x14'
|| datalen != 20 || datalen != 20
|| memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->defer+2, datalen)) || memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->holdback+2, datalen))
rc = gpg_error (GPG_ERR_BAD_SIGNATURE); rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
/* log_printhex("MDC message:", dfx->defer, 22); */ /* log_printhex("MDC message:", dfx->holdback, 22); */
/* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */ /* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */
} }
@ -358,14 +382,14 @@ mdc_decode_filter (void *opaque, int control, IOBUF a,
if (n == 44) if (n == 44)
{ {
/* We have enough stuff - flush the deferred stuff. */ /* We have enough stuff - flush the deferred stuff. */
if ( !dfx->defer_filled ) /* First time. */ if ( !dfx->holdbacklen ) /* First time. */
{ {
memcpy (buf, buf+22, 22); memcpy (buf, buf+22, 22);
n = 22; n = 22;
} }
else else
{ {
memcpy (buf, dfx->defer, 22); memcpy (buf, dfx->holdback, 22);
} }
/* Fill up the buffer. */ /* Fill up the buffer. */
if (dfx->partial) if (dfx->partial)
@ -399,10 +423,10 @@ mdc_decode_filter (void *opaque, int control, IOBUF a,
/* Move the trailing 22 bytes back to the defer buffer. We /* Move the trailing 22 bytes back to the defer buffer. We
have at least 44 bytes thus a memmove is not needed. */ have at least 44 bytes thus a memmove is not needed. */
n -= 22; n -= 22;
memcpy (dfx->defer, buf+n, 22 ); memcpy (dfx->holdback, buf+n, 22 );
dfx->defer_filled = 1; dfx->holdbacklen = 22;
} }
else if ( !dfx->defer_filled ) /* EOF seen but empty defer buffer. */ else if ( !dfx->holdbacklen ) /* EOF seen but empty holdback buffer. */
{ {
/* This is bad because it means an incomplete hash. */ /* This is bad because it means an incomplete hash. */
n -= 22; n -= 22;
@ -411,9 +435,9 @@ mdc_decode_filter (void *opaque, int control, IOBUF a,
} }
else /* EOF seen (i.e. read less than 22 bytes). */ else /* EOF seen (i.e. read less than 22 bytes). */
{ {
memcpy (buf, dfx->defer, 22 ); memcpy (buf, dfx->holdback, 22 );
n -= 22; n -= 22;
memcpy (dfx->defer, buf+n, 22 ); memcpy (dfx->holdback, buf+n, 22 );
dfx->eof_seen = 1; /* Normal EOF. */ dfx->eof_seen = 1; /* Normal EOF. */
} }