Skip to content

Commit 76020c6

Browse files
committed
org.bouncycastle.openpgp.api.DoubleBufferedInputStream, which no class in the library referenced and whose read() returned the buffered byte unmasked so that every byte from 0x80 upwards came back negative and 0xFF came back as the -1 a caller reads as end of stream, has been removed along with its test rather than repaired, its withholding also being one read of the underlying stream rather than the configured buffer size, and OpenPGPMessageInputStream now documents that what is read from it is not covered by an integrity check until close() has returned, relates to github #2424.
1 parent b2f6375 commit 76020c6

5 files changed

Lines changed: 17 additions & 359 deletions

File tree

‎docs/releasenotes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ Date: 2026, TBD
170170
- Every classical signer assembled the signature component s with plain BigInteger arithmetic over the signing key: ECDSASigner formed k^-1 \* (e + d \* r) mod n, SM2Signer (1 + d)^-1 \* (k - r \* d) mod n, DSASigner k^-1 \* (m + x \* r) mod q, GOST3410Signer k \* m + x \* r mod q, ECGOST3410Signer k \* e + d \* r mod n, ECNRSigner u - r \* x mod n, BIP340Signer k + e \* d mod n and DSTU4145Signer r \* d + e mod n, and while the inverse in each had already been moved to the constant-time BigIntegers.modOddInverse, the multiplications and reductions around it had not. BigInteger.mod does an amount of work set by the quotient, so the cost of forming s answered a question about the long-lived signing key once per signature, and since r and e (SM2: r and k) vary publicly from one signature to the next those answers combine over a run of signatures; the same reasoning moved the ECCSI and SAKKE arithmetic in this release. All eight now go through BigIntegers.modMult, modAdd and the new modSubtract (see Additional Features below), which need their operands already reduced. The five elliptic-curve signers take their key as an ECPrivateKeyParameters, so the signing key is in [1, n-1] by ECDomainParameters.validatePrivateScalar, and each nonce and each r is reduced where it is produced - EC-NR's ephemeral comes from a generated key pair, BIP 340's nonce and challenge are reduced and its two conditional negations leave both operands in range, and DSTU 4145 draws its e in [1, n-1] and truncates r below 2^(n.bitLength()-1). The hash-derived value is the one that needed handling in four of them: ECDSA's e, DSA's m, and the GOST 3410 and ECGOST 3410 digests can all sit at or past the order, and all four are public, so each is reduced on the way in at no cost. DSA and GOST 3410 needed one further thing. Unlike ECPrivateKeyParameters, neither DSAPrivateKeyParameters nor GOST3410PrivateKeyParameters checked the range of x, so a key outside [1, q-1] - which FIPS 186-4 sec. 4.1 does not permit - was silently reduced by the arithmetic that formed s. Both now reject such a key when it is constructed, which is where the check belongs: unlike the ECCSI signing key, which does not carry its curve and so has to be checked at the signer, a DSA or GOST key carries the parameters that bound it. A key with no parameters attached is left alone, as it cannot sign in any case. Signatures are unchanged: the RFC 6979 deterministic ECDSA vectors, the SM2 GM/T 0003.2 vectors, the BIP-340 vectors and the GOST R 34.10 fixed-nonce vectors reproduce byte for byte, EC-NR reproduces its fixed-nonce expected values, and DSTU 4145 still verifies what it signs. One behavioural edge: modMult requires an odd modulus, so a caller-built curve whose order is even - which no standard curve has, and which the group structure does not admit - is now refused rather than signed with. The substitutions preserve the result and so cannot be caught by any signing or verification test, and are covered instead by the class-file scan in ConstantTimeUsageTest, which gains rows for all eight signers.
171171
- The XDH KeyFactory did not accept the JDK's own X25519 / X448 key specs. On JDK 11 and later BC's X25519 and X448 keys implement java.security.interfaces.XECPublicKey and XECPrivateKey, so a caller can read the u-coordinate and the scalar through the standard interfaces, but the reverse direction was missing: KeyFactory.getInstance("X25519", "BC").generatePublic(new XECPublicKeySpec(NamedParameterSpec.X25519, u)) failed with InvalidKeySpecException("key spec not recognized"), as did XECPrivateKeySpec and both of them for X448, and asking for either spec back through KeyFactory.getKeySpec() reached the "not implemented yet" fall-through in BaseKeyFactorySpi. A key could therefore be exported through the standard interface but not re-imported through the standard spec, which breaks code that moves a key between BC and another provider by coordinates rather than by encoding. Both specs are now accepted by generatePublic / generatePrivate and returned by getKeySpec, through the same jdk1.11 multi-release hook (XDHKeys) that produces the XEC-implementing key classes; the Edwards side has done this since the JDK 15 EdEC specs, and this is the X25519 / X448 analogue of it. The u-coordinate arrives as a BigInteger and is reduced modulo the field prime before being encoded little-endian into 32 or 56 octets, which RFC 7748 sec. 5 requires - "Implementations MUST accept non-canonical values and process them as if they had been reduced modulo the field prime" - and which the JDK's own provider also does; the reduction went into the conversion both entry points share, so an incoming third-party XECPublicKey carrying a non-canonical or over-wide u is now reduced rather than kept as it stands or rejected. A negative u is still refused, as it already was on the key path. Note that BC and the JDK provider agree on the coordinate but not always on the whole SubjectPublicKeyInfo: SunEC on JDK 11 writes an explicit NULL in the AlgorithmIdentifier parameters where RFC 8410 sec. 3 requires them absent ("For all of the OIDs, the parameters MUST be absent"), which BC does not follow and later JDKs no longer do. Because only the jdk1.11 copy of the hook implements any of this, the coverage is a new multi-release test, prov/src/test/jdk1.11 XDHKeySpecMRTest, registered in AllTests11 and run by the test11 task.
172172
- The GOST R 34.10-2001 / 2012 signer (org.bouncycastle.crypto.signers.ECGOST3410Signer, and ECGOST3410_2012Signer which extends it) drew its per-signature nonce uniformly over the bit length of the group order and rejected only a draw of zero, where the GOST R 34.10-94 signer beside it already redraws anything at or past the order. A draw that landed at or past n was therefore not discarded but folded to k mod n by the reductions that followed, which makes the bottom of the range likelier than the top. On most of the named curves that costs nothing - the order is either just below the next power of two, so such a draw essentially never happens, or close to half of it, so the fold covers the range exactly twice and the result stays uniform - but on the three whose order is about 0.61 of 2^256 (GostR3410-2001-CryptoPro-C, GostR3410-2001-CryptoPro-XchB and Tc26-Gost-3410-12-256-paramSetD) the bottom 64.5% of the range takes 78.4% of the draws, so a nonce fell below n/2 about 61% of the time rather than 50%. A nonce that is not uniform over [1, n-1] is the input a lattice attack on the signing key consumes, and while no key recovery is demonstrated here, the scheme's security argument assumes a uniform one. The nonce is now redrawn until it is below the order, as the GOST R 34.10-94 signer does. That signer had the mirror of the same gap and is corrected with it: it redrew a nonce at or past q but accepted one of zero, which is the single value that gives the signing key away outright - the blinded exponent becomes a multiple of q, so r is 1 and s = k * m + x * r collapses to x. It is a 2^-256 draw, but free to close, and it is now redrawn like any other unusable value. A caller supplying a deterministic stream of test randomness will see further bytes requested where a draw is out of range and previously was not redrawn; the GOST R 34.10 vectors all draw in range and are unaffected. Note the redraw is also what the constant-time assembly above depends on, since that rejects rather than reduces an operand at or past the modulus.
173+
- org.bouncycastle.openpgp.api.DoubleBufferedInputStream has been removed. The class has been present since 1.81, to withhold a trailing window of decrypted data so that unauthenticated plaintext is not emitted, but it was never referenced by anything in the library - no class in any source tree constructed one, and no policy or setting connected it to the decryption pipeline - and it did not work. Its read() returned the buffered byte unmasked, so every byte from 0x80 upwards came back negative and 0xFF came back as -1, which a caller reads as end of stream: data was silently corrupted and then silently truncated. Reading an OpenPGP message through one failed on the first packet, the new-format tag octet 0xCB being read as -53. Its read(byte[], int, int) also returned 0 where the underlying stream returned 0, which the InputStream contract does not allow. Beyond that it never filled its buffers, taking whatever a single read of the underlying stream returned, so the amount withheld was one read rather than the configured buffer size - for a tampered five megabyte message a corrected version of the class still released all but about 1.7 kilobytes of the plaintext with a one megabyte buffer. It is removed rather than repaired: it was unusable as shipped, nothing in the library depended on it, and the withholding it offered was not the guarantee its documentation described. This is a removal from the exported org.bouncycastle.openpgp.api package, so it is a public API change, but no caller can have been reading data through one successfully. Note that the high-level decryption API still hands version 1 (MDC) integrity-protected plaintext to a streaming caller before the integrity check runs at end of stream, so callers must not act on data read from an OpenPGPMessageInputStream until close() has returned without error; version 2 (AEAD) messages are unaffected, each chunk being authenticated before it is released (github #2424).
174+
173175

174176
### 2.1.3 Additional Features and Functionality
175177

‎pg/src/main/java/org/bouncycastle/openpgp/api/DoubleBufferedInputStream.java‎

Lines changed: 0 additions & 194 deletions
This file was deleted.

‎pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPMessageInputStream.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@
3333
* You can get information about the message (signatures, encryption method, message metadata)
3434
* by reading ALL data from the stream, closing it with {@link #close()} and then retrieving a {@link Result} object
3535
* by calling {@link #getResult()}.
36+
* <p>
37+
* <b>Unauthenticated plaintext note:</b> data read from this stream has not yet been checked for integrity.
38+
* Decryption is performed as the stream is read, and the integrity of the message is only established once
39+
* {@link #close()} has returned without throwing. For a version 1 Symmetrically Encrypted and Integrity Protected
40+
* Data packet, the Modification Detection Code covers the whole message and is verified at the end of the data,
41+
* so every plaintext byte is emitted before the check runs; a tampered message is reported by an
42+
* {@link IOException} from the final {@link #read()} or from {@link #close()}, after the plaintext has already
43+
* been handed over. A caller should therefore treat everything read from this stream as unverified, and should not
44+
* act on it, pass it on or parse it further until {@link #close()} has completed normally. RFC 9580 sec. 13.7
45+
* discusses what releasing decrypted data before confirming its integrity can leak.
46+
* <p>
47+
* A version 2 packet is not affected in the same way: it is encrypted with an AEAD algorithm in chunks, and each
48+
* chunk's authentication tag is verified before that chunk's plaintext is emitted, so the unverified window is
49+
* bounded by one chunk. Signature verification is separate from either and is only complete once
50+
* {@link #close()} has returned and {@link #getResult()} has been consulted.
3651
*/
3752
public class OpenPGPMessageInputStream
3853
extends InputStream

0 commit comments

Comments
 (0)