<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi,</p>
<p>I think there is a bug in GnuPG the function g10/build-packet.c:gpg_mpi_write()</p>
<p>The case I observed is an opaque MPI with a leading byte 0x04.
The call to gcry_mpi_get_opaque() already sets the correct bit
length (i.e. accounting for the highest 5 bits to be zero). Then
the subsequent code again subtracts 5 from nbits, effectively
reducing the byte count by one. The written MPI is thus one byte
too short.</p>
<p>- Falko<br>
</p>
<p><tt>gpg_error_t<br>
gpg_mpi_write (iobuf_t out, gcry_mpi_t a, unsigned int
*r_nwritten)<br>
{<br>
gpg_error_t err;<br>
unsigned int nwritten = 0;<br>
<br>
if (gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))<br>
{<br>
unsigned int nbits;<br>
const unsigned char *p;<br>
unsigned char lenhdr[2];<br>
<br>
/* gcry_log_debugmpi ("a", a); */<br>
p = gcry_mpi_get_opaque (a, &nbits);<br>
if (p)<br>
{<br>
/* Strip leading zero bits. */<br>
for (; nbits >= 8 && !*p; p++, nbits -= 8)<br>
;<br>
if (nbits >= 8 && !(*p & 0x80))<br>
if (--nbits >= 7 && !(*p & 0x40))<br>
if (--nbits >= 6 && !(*p & 0x20))<br>
if (--nbits >= 5 && !(*p & 0x10))<br>
if (--nbits >= 4 && !(*p &
0x08))<br>
if (--nbits >= 3 && !(*p &
0x04))<br>
if (--nbits >= 2 && !(*p &
0x02))<br>
if (--nbits >= 1 && !(*p
& 0x01))<br>
--nbits;<br>
</tt><br>
</p>
<div class="moz-signature">-- <br>
<!-- MTG AG HTML signature v.1.0, 2021-02-12 - Author: Andreas Cholet -->
<p style="line-height: 1.5;"><font face="Arial"><span
style="font-size: small; color: rgb(93, 93, 95);">
<strong>MTG AG</strong><br>
Dr. Falko Strenzke<br>
Executive System Architect<br>
<!--up to here--> </span></font></p>
<font face="Arial">
<p>
<span style="font-size: small; color: rgb(93, 93, 95);">
<!--personalize--><span
style="display:inline-block;width:4em">Phone: </span>+49
6151 8000 24<br>
<!--personalize--><span
style="display:inline-block;width:4em">E-Mail: </span><a class="moz-txt-link-abbreviated" href="mailto:falko.strenzke@mtg.de">falko.strenzke@mtg.de</a><br>
<span style="display:inline-block;width:4em">Web: </span><a
href="https://www.mtg.de" title="MTG AG Internet"
target="_blank">mtg.de</a>
</span></p>
<br>
<font face="Arial"> </font>
<p style="line-height: 1.2;"><font face="Arial">
<span style="font-size: x-small; color: rgb(93, 93, 95);">
MTG AG - Dolivostr. 11 - 64293 Darmstadt, Germany<br>
Commercial register: HRB 8901<br>
Register Court: Amtsgericht Darmstadt<br>
Management Board: Jürgen Ruf (CEO), Tamer Kemeröz<br>
Chairman of the Supervisory Board: Dr. Thomas Milde<br>
<br>
This email may contain confidential and/or privileged
information.
If you are not the correct recipient or have received this
email in error,
<br>
please inform the sender immediately and delete this
email.Unauthorised copying or distribution of this email
is not permitted.<br>
<br>
Data protection information: <a
href="https://www.mtg.de/en/privacy-policy"
title="MTG Privacy policy" target="_blank">Privacy
policy</a>
</span></font></p>
</font></div>
</body>
</html>