Skip to content

Commit 9385b0e

Browse files
committed
removed support for non-cbc mode ciphers in IES/ECIES
1 parent 7acfa2f commit 9385b0e

File tree

10 files changed

+214
-282
lines changed

10 files changed

+214
-282
lines changed

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/DH.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,14 @@ public void configure(ConfigurableProvider provider)
3838
provider.addAlgorithm("AlgorithmParameterGenerator.DH", PREFIX + "AlgorithmParameterGeneratorSpi");
3939

4040
provider.addAlgorithm("Cipher.IES", PREFIX + "IESCipher$IES");
41-
provider.addAlgorithm("Cipher.IESwithAES", PREFIX + "IESCipher$IESwithAES");
42-
provider.addAlgorithm("Cipher.IESWITHAES", PREFIX + "IESCipher$IESwithAES");
43-
provider.addAlgorithm("Cipher.IESWITHDESEDE", PREFIX + "IESCipher$IESwithDESede");
41+
provider.addAlgorithm("Cipher.IESwithAES-CBC", PREFIX + "IESCipher$IESwithAESCBC");
42+
provider.addAlgorithm("Cipher.IESWITHAES-CBC", PREFIX + "IESCipher$IESwithAESCBC");
43+
provider.addAlgorithm("Cipher.IESWITHDESEDE-CBC", PREFIX + "IESCipher$IESwithDESedeCBC");
4444

4545
provider.addAlgorithm("Cipher.DHIES", PREFIX + "IESCipher$IES");
46-
provider.addAlgorithm("Cipher.DHIESwithAES", PREFIX + "IESCipher$IESwithAES");
47-
provider.addAlgorithm("Cipher.DHIESWITHAES", PREFIX + "IESCipher$IESwithAES");
48-
provider.addAlgorithm("Cipher.DHIESWITHDESEDE", PREFIX + "IESCipher$IESwithDESede");
49-
50-
provider.addAlgorithm("Cipher.OLDDHIES", PREFIX + "IESCipher$OldIES");
51-
provider.addAlgorithm("Cipher.OLDDHIESwithAES", PREFIX + "IESCipher$OldIESwithAES");
52-
provider.addAlgorithm("Cipher.OLDDHIESWITHAES", PREFIX + "IESCipher$OldIESwithAES");
53-
provider.addAlgorithm("Cipher.OLDDHIESWITHDESEDE", PREFIX + "IESCipher$OldIESwithDESede");
46+
provider.addAlgorithm("Cipher.DHIESwithAES-CBC", PREFIX + "IESCipher$IESwithAESCBC");
47+
provider.addAlgorithm("Cipher.DHIESWITHAES-CBC", PREFIX + "IESCipher$IESwithAESCBC");
48+
provider.addAlgorithm("Cipher.DHIESWITHDESEDE-CBC", PREFIX + "IESCipher$IESwithDESedeCBC");
5449

5550
registerOid(provider, PKCSObjectIdentifiers.dhKeyAgreement, "DH", new KeyFactorySpi());
5651
registerOid(provider, X9ObjectIdentifiers.dhpublicnumber, "DH", new KeyFactorySpi());

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/EC.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,12 @@ public void configure(ConfigurableProvider provider)
134134
provider.addAlgorithm("KeyPairGenerator.ECIES", PREFIX + "KeyPairGeneratorSpi$ECDH");
135135

136136
provider.addAlgorithm("Cipher.ECIES", PREFIX + "IESCipher$ECIES");
137-
provider.addAlgorithm("Cipher.ECIESwithAES", PREFIX + "IESCipher$ECIESwithAES");
138-
provider.addAlgorithm("Cipher.ECIESWITHAES", PREFIX + "IESCipher$ECIESwithAES");
139-
provider.addAlgorithm("Cipher.ECIESwithDESEDE", PREFIX + "IESCipher$ECIESwithDESede");
140-
provider.addAlgorithm("Cipher.ECIESWITHDESEDE", PREFIX + "IESCipher$ECIESwithDESede");
137+
141138
provider.addAlgorithm("Cipher.ECIESwithAES-CBC", PREFIX + "IESCipher$ECIESwithAESCBC");
142139
provider.addAlgorithm("Cipher.ECIESWITHAES-CBC", PREFIX + "IESCipher$ECIESwithAESCBC");
143140
provider.addAlgorithm("Cipher.ECIESwithDESEDE-CBC", PREFIX + "IESCipher$ECIESwithDESedeCBC");
144141
provider.addAlgorithm("Cipher.ECIESWITHDESEDE-CBC", PREFIX + "IESCipher$ECIESwithDESedeCBC");
145142

146-
provider.addAlgorithm("Cipher.OldECIES", PREFIX + "IESCipher$OldECIES");
147-
provider.addAlgorithm("Cipher.OldECIESwithAES", PREFIX + "IESCipher$OldECIESwithAES");
148-
provider.addAlgorithm("Cipher.OldECIESWITHAES", PREFIX + "IESCipher$OldECIESwithAES");
149-
provider.addAlgorithm("Cipher.OldECIESwithDESEDE", PREFIX + "IESCipher$OldECIESwithDESede");
150-
provider.addAlgorithm("Cipher.OldECIESWITHDESEDE", PREFIX + "IESCipher$OldECIESwithDESede");
151-
provider.addAlgorithm("Cipher.OldECIESwithAES-CBC", PREFIX + "IESCipher$OldECIESwithAESCBC");
152-
provider.addAlgorithm("Cipher.OldECIESWITHAES-CBC", PREFIX + "IESCipher$OldECIESwithAESCBC");
153-
provider.addAlgorithm("Cipher.OldECIESwithDESEDE-CBC", PREFIX + "IESCipher$OldECIESwithDESedeCBC");
154-
provider.addAlgorithm("Cipher.OldECIESWITHDESEDE-CBC", PREFIX + "IESCipher$OldECIESwithDESedeCBC");
155-
156143
provider.addAlgorithm("Signature.ECDSA", PREFIX + "SignatureSpi$ecDSA");
157144
provider.addAlgorithm("Signature.NONEwithECDSA", PREFIX + "SignatureSpi$ecDSAnone");
158145

prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/dh/IESCipher.java

+39-58
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@
2121
import javax.crypto.interfaces.DHPrivateKey;
2222
import javax.crypto.interfaces.DHPublicKey;
2323

24-
import org.bouncycastle.crypto.BlockCipher;
24+
import org.bouncycastle.crypto.CipherParameters;
2525
import org.bouncycastle.crypto.InvalidCipherTextException;
2626
import org.bouncycastle.crypto.KeyEncoder;
2727
import org.bouncycastle.crypto.agreement.DHBasicAgreement;
2828
import org.bouncycastle.crypto.digests.SHA1Digest;
29-
import org.bouncycastle.crypto.engines.AESEngine;
29+
import org.bouncycastle.crypto.engines.AESFastEngine;
3030
import org.bouncycastle.crypto.engines.DESedeEngine;
3131
import org.bouncycastle.crypto.engines.IESEngine;
32-
import org.bouncycastle.crypto.engines.OldIESEngine;
3332
import org.bouncycastle.crypto.generators.DHKeyPairGenerator;
3433
import org.bouncycastle.crypto.generators.EphemeralKeyPairGenerator;
3534
import org.bouncycastle.crypto.generators.KDF2BytesGenerator;
3635
import org.bouncycastle.crypto.macs.HMac;
36+
import org.bouncycastle.crypto.modes.CBCBlockCipher;
3737
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
3838
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
3939
import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
4040
import org.bouncycastle.crypto.params.DHKeyParameters;
4141
import org.bouncycastle.crypto.params.DHParameters;
4242
import org.bouncycastle.crypto.params.DHPublicKeyParameters;
43-
import org.bouncycastle.crypto.params.IESParameters;
4443
import org.bouncycastle.crypto.params.IESWithCipherParameters;
44+
import org.bouncycastle.crypto.params.ParametersWithIV;
4545
import org.bouncycastle.crypto.parsers.DHIESPublicKeyParser;
4646
import org.bouncycastle.jcajce.provider.asymmetric.util.DHUtil;
4747
import org.bouncycastle.jcajce.provider.asymmetric.util.IESUtil;
@@ -57,6 +57,7 @@ public class IESCipher
5757
extends CipherSpi
5858
{
5959
private final JcaJceHelper helper = new BCJcaJceHelper();
60+
private final int ivLength;
6061

6162
private IESEngine engine;
6263
private int state = -1;
@@ -71,11 +72,13 @@ public class IESCipher
7172
public IESCipher(IESEngine engine)
7273
{
7374
this.engine = engine;
75+
this.ivLength = 0;
7476
}
7577

76-
public IESCipher(OldIESEngine engine)
78+
public IESCipher(IESEngine engine, int ivLength)
7779
{
7880
this.engine = engine;
81+
this.ivLength = ivLength;
7982
}
8083

8184
public int engineGetBlockSize()
@@ -106,6 +109,10 @@ public int engineGetKeySize(Key key)
106109

107110
public byte[] engineGetIV()
108111
{
112+
if (engineSpec != null)
113+
{
114+
return engineSpec.getNonce();
115+
}
109116
return null;
110117
}
111118

@@ -257,7 +264,13 @@ public void engineInit(
257264
// Use default parameters (including cipher key size) if none are specified
258265
if (engineSpec == null)
259266
{
260-
this.engineSpec = IESUtil.guessParameterSpec(engine.getCipher());
267+
byte[] nonce = null;
268+
if (ivLength != 0 && opmode == Cipher.ENCRYPT_MODE)
269+
{
270+
nonce = new byte[ivLength];
271+
random.nextBytes(nonce);
272+
}
273+
this.engineSpec = IESUtil.guessParameterSpec(engine.getCipher(), nonce);
261274
}
262275
else if (engineSpec instanceof IESParameterSpec)
263276
{
@@ -268,6 +281,13 @@ else if (engineSpec instanceof IESParameterSpec)
268281
throw new InvalidAlgorithmParameterException("must be passed IES parameters");
269282
}
270283

284+
byte[] nonce = this.engineSpec.getNonce();
285+
286+
if (ivLength != 0 && (nonce == null || nonce.length != ivLength))
287+
{
288+
throw new InvalidAlgorithmParameterException("NONCE in IES Parameters needs to be " + ivLength + " bytes long");
289+
}
290+
271291
// Parse the recipient's key
272292
if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE)
273293
{
@@ -329,7 +349,7 @@ public void engineInit(
329349
}
330350
catch (InvalidAlgorithmParameterException e)
331351
{
332-
throw new IllegalArgumentException("can't handle supplied parameter spec");
352+
throw new IllegalArgumentException("cannot handle supplied parameter spec: " + e.getMessage());
333353
}
334354

335355
}
@@ -376,11 +396,16 @@ public byte[] engineDoFinal(
376396
buffer.reset();
377397

378398
// Convert parameters for use in IESEngine
379-
IESParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(),
399+
CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(),
380400
engineSpec.getEncodingV(),
381401
engineSpec.getMacKeySize(),
382402
engineSpec.getCipherKeySize());
383403

404+
if (engineSpec.getNonce() != null)
405+
{
406+
params = new ParametersWithIV(params, engineSpec.getNonce());
407+
}
408+
384409
DHParameters dhParams = ((DHKeyParameters)key).getParameters();
385410

386411
byte[] V;
@@ -494,71 +519,27 @@ public IES()
494519
}
495520
}
496521

497-
static public class IESwithDESede
522+
static public class IESwithDESedeCBC
498523
extends IESCipher
499524
{
500-
public IESwithDESede()
525+
public IESwithDESedeCBC()
501526
{
502527
super(new IESEngine(new DHBasicAgreement(),
503528
new KDF2BytesGenerator(new SHA1Digest()),
504529
new HMac(new SHA1Digest()),
505-
new PaddedBufferedBlockCipher(new DESedeEngine())));
530+
new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()))), 8);
506531
}
507532
}
508533

509-
static public class IESwithAES
534+
static public class IESwithAESCBC
510535
extends IESCipher
511536
{
512-
public IESwithAES()
537+
public IESwithAESCBC()
513538
{
514539
super(new IESEngine(new DHBasicAgreement(),
515540
new KDF2BytesGenerator(new SHA1Digest()),
516541
new HMac(new SHA1Digest()),
517-
new PaddedBufferedBlockCipher(new AESEngine())));
518-
}
519-
}
520-
521-
/**
522-
* Backwards compatibility.
523-
*/
524-
static public class OldIESwithCipher
525-
extends IESCipher
526-
{
527-
public OldIESwithCipher(BlockCipher baseCipher)
528-
{
529-
super(new OldIESEngine(new DHBasicAgreement(),
530-
new KDF2BytesGenerator(new SHA1Digest()),
531-
new HMac(new SHA1Digest()),
532-
new PaddedBufferedBlockCipher(baseCipher)));
533-
}
534-
}
535-
536-
static public class OldIES
537-
extends IESCipher
538-
{
539-
public OldIES()
540-
{
541-
super(new OldIESEngine(new DHBasicAgreement(),
542-
new KDF2BytesGenerator(new SHA1Digest()),
543-
new HMac(new SHA1Digest())));
544-
}
545-
}
546-
547-
static public class OldIESwithDESede
548-
extends OldIESwithCipher
549-
{
550-
public OldIESwithDESede()
551-
{
552-
super(new DESedeEngine());
553-
}
554-
}
555-
556-
static public class OldIESwithAES
557-
extends OldIESwithCipher
558-
{
559-
public OldIESwithAES()
560-
{
561-
super(new AESEngine());
542+
new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()))), 16);
562543
}
563544
}
564545
}

0 commit comments

Comments
 (0)