Skip to content

Commit 6397af9

Browse files
committed
further work on constraints.
1 parent 44fe07b commit 6397af9

File tree

5 files changed

+140
-13
lines changed

5 files changed

+140
-13
lines changed

core/src/main/java/org/bouncycastle/crypto/engines/ISAACEngine.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.bouncycastle.crypto.engines;
22

33
import org.bouncycastle.crypto.CipherParameters;
4+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
45
import org.bouncycastle.crypto.DataLengthException;
56
import org.bouncycastle.crypto.OutputLengthException;
67
import org.bouncycastle.crypto.StreamCipher;
8+
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
79
import org.bouncycastle.crypto.params.KeyParameter;
810
import org.bouncycastle.util.Pack;
911

@@ -51,9 +53,10 @@ public void init(
5153
* irrelevant.
5254
*/
5355
KeyParameter p = (KeyParameter)params;
54-
setKey(p.getKey());
55-
56-
return;
56+
byte[] key = p.getKey();
57+
setKey(key);
58+
59+
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(getAlgorithmName(), key.length < 32 ? key.length * 8 : 256, params, Utils.getPurpose(forEncryption)));
5760
}
5861

5962
public byte returnByte(byte in)

core/src/main/java/org/bouncycastle/crypto/engines/SEEDEngine.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import org.bouncycastle.crypto.BlockCipher;
44
import org.bouncycastle.crypto.CipherParameters;
5+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
56
import org.bouncycastle.crypto.DataLengthException;
67
import org.bouncycastle.crypto.OutputLengthException;
78
import org.bouncycastle.crypto.StatelessProcessing;
9+
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
810
import org.bouncycastle.crypto.params.KeyParameter;
911

1012
/**
@@ -179,6 +181,8 @@ public void init(boolean forEncryption, CipherParameters params) throws IllegalA
179181
{
180182
this.forEncryption = forEncryption;
181183
wKey = createWorkingKey(((KeyParameter)params).getKey());
184+
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
185+
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
182186
}
183187

184188
public String getAlgorithmName()

core/src/main/java/org/bouncycastle/crypto/engines/Shacal2Engine.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.bouncycastle.crypto.BlockCipher;
44
import org.bouncycastle.crypto.CipherParameters;
5+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
56
import org.bouncycastle.crypto.DataLengthException;
67
import org.bouncycastle.crypto.OutputLengthException;
8+
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
79
import org.bouncycastle.crypto.params.KeyParameter;
810

911
/**
@@ -65,7 +67,11 @@ public void init(boolean _forEncryption, CipherParameters params)
6567
}
6668
this.forEncryption = _forEncryption;
6769
workingKey = new int[64];
68-
setKey( ((KeyParameter)params).getKey() );
70+
byte[] key = ((KeyParameter)params).getKey();
71+
setKey(key);
72+
int keyBits = key.length * 8;
73+
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
74+
this.getAlgorithmName(), keyBits < 256 ? keyBits : 256, params, Utils.getPurpose(forEncryption)));
6975
}
7076

7177
public void setKey(byte[] kb)

core/src/main/java/org/bouncycastle/crypto/engines/XTEAEngine.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.bouncycastle.crypto.BlockCipher;
44
import org.bouncycastle.crypto.CipherParameters;
5+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
56
import org.bouncycastle.crypto.DataLengthException;
67
import org.bouncycastle.crypto.OutputLengthException;
8+
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
79
import org.bouncycastle.crypto.params.KeyParameter;
810

911
/**
@@ -68,6 +70,8 @@ public void init(
6870
KeyParameter p = (KeyParameter)params;
6971

7072
setKey(p.getKey());
73+
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
74+
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
7175
}
7276

7377
public int processBlock(

core/src/test/java/org/bouncycastle/crypto/test/CryptoServiceConstraintsTest.java

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.bouncycastle.crypto.engines.AESLightEngine;
5151
import org.bouncycastle.crypto.engines.BlowfishEngine;
5252
import org.bouncycastle.crypto.engines.CAST5Engine;
53+
import org.bouncycastle.crypto.engines.CAST6Engine;
5354
import org.bouncycastle.crypto.engines.CamelliaEngine;
5455
import org.bouncycastle.crypto.engines.CamelliaLightEngine;
5556
import org.bouncycastle.crypto.engines.ChaCha7539Engine;
@@ -60,15 +61,18 @@
6061
import org.bouncycastle.crypto.engines.HC128Engine;
6162
import org.bouncycastle.crypto.engines.HC256Engine;
6263
import org.bouncycastle.crypto.engines.IDEAEngine;
64+
import org.bouncycastle.crypto.engines.ISAACEngine;
6365
import org.bouncycastle.crypto.engines.RC4Engine;
6466
import org.bouncycastle.crypto.engines.RC532Engine;
6567
import org.bouncycastle.crypto.engines.RC564Engine;
6668
import org.bouncycastle.crypto.engines.RSAEngine;
6769
import org.bouncycastle.crypto.engines.RijndaelEngine;
70+
import org.bouncycastle.crypto.engines.SEEDEngine;
6871
import org.bouncycastle.crypto.engines.SM2Engine;
6972
import org.bouncycastle.crypto.engines.SM4Engine;
7073
import org.bouncycastle.crypto.engines.Salsa20Engine;
7174
import org.bouncycastle.crypto.engines.SerpentEngine;
75+
import org.bouncycastle.crypto.engines.Shacal2Engine;
7276
import org.bouncycastle.crypto.engines.SkipjackEngine;
7377
import org.bouncycastle.crypto.engines.TEAEngine;
7478
import org.bouncycastle.crypto.engines.ThreefishEngine;
@@ -190,6 +194,7 @@ public void performTest()
190194
testARIA();
191195
testIDEA();
192196
testCAST5();
197+
testCAST6();
193198
testCamelliaLight();
194199
testCamellia();
195200
testBlowfish();
@@ -205,13 +210,17 @@ public void performTest()
205210
testSM2Cipher();
206211
testSM4();
207212
testTEA();
213+
testXTEA();
208214
testThreefish();
209215
testSalsa20AndXSalsa20AndChaCha();
210216
testZuc128AndZuc256();
211217
testVMPCAndVMPCKSA();
212218
testRC532AndRC564();
213219
testRijndael();
214220
testHC128AndHC256();
221+
testSEED();
222+
testISAAC();
223+
testShacal2();
215224
}
216225

217226
private void test112bits()
@@ -716,6 +725,24 @@ private void testCAST5()
716725
isEquals("service does not provide 256 bits of security only 128", e.getMessage());
717726
}
718727

728+
engine.init(false, new KeyParameter(new byte[16]));
729+
730+
CryptoServicesRegistrar.setServicesConstraints(null);
731+
}
732+
733+
private void testSEED()
734+
{
735+
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(256));
736+
SEEDEngine engine = new SEEDEngine();
737+
try
738+
{
739+
engine.init(true, new KeyParameter(new byte[16]));
740+
fail("no exception!");
741+
}
742+
catch (CryptoServiceConstraintsException e)
743+
{
744+
isEquals("service does not provide 256 bits of security only 128", e.getMessage());
745+
}
719746

720747
engine.init(false, new KeyParameter(new byte[16]));
721748

@@ -1617,12 +1644,12 @@ private void testECIESKEM()
16171644
ecKp.init(new ECKeyGenerationParameters(new ECDomainParameters(X962NamedCurves.getByName("prime192v1")), random));
16181645
AsymmetricCipherKeyPair kp = ecKp.generateKeyPair();
16191646

1620-
byte[] out = new byte[49];
1647+
byte[] out = new byte[49];
16211648
ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(kdf, random);
16221649

16231650
kem.init(kp.getPublic());
16241651
KeyParameter key1 = (KeyParameter)kem.encrypt(out, 128);
1625-
1652+
16261653
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(128, 80));
16271654

16281655

@@ -1686,6 +1713,88 @@ private void testTEA()
16861713
CryptoServicesRegistrar.setServicesConstraints(null);
16871714
}
16881715

1716+
private void testXTEA()
1717+
{
1718+
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(256, 128));
1719+
TEAEngine engine = new TEAEngine();
1720+
try
1721+
{
1722+
engine.init(true, new KeyParameter(new byte[16]));
1723+
fail("no exception!");
1724+
}
1725+
catch (CryptoServiceConstraintsException e)
1726+
{
1727+
isEquals("service does not provide 256 bits of security only 128", e.getMessage());
1728+
}
1729+
1730+
engine.init(false, new KeyParameter(new byte[16]));
1731+
1732+
CryptoServicesRegistrar.setServicesConstraints(null);
1733+
}
1734+
1735+
private void testCAST6()
1736+
{
1737+
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(256, 128));
1738+
CAST6Engine engine = new CAST6Engine();
1739+
try
1740+
{
1741+
engine.init(true, new KeyParameter(new byte[16]));
1742+
fail("no exception!");
1743+
}
1744+
catch (CryptoServiceConstraintsException e)
1745+
{
1746+
isEquals("service does not provide 256 bits of security only 128", e.getMessage());
1747+
}
1748+
1749+
engine.init(false, new KeyParameter(new byte[16])); // should work
1750+
1751+
engine.init(true, new KeyParameter(new byte[32])); // should work
1752+
1753+
CryptoServicesRegistrar.setServicesConstraints(null);
1754+
}
1755+
1756+
private void testISAAC()
1757+
{
1758+
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(256, 128));
1759+
ISAACEngine engine = new ISAACEngine();
1760+
try
1761+
{
1762+
engine.init(true, new KeyParameter(new byte[16]));
1763+
fail("no exception!");
1764+
}
1765+
catch (CryptoServiceConstraintsException e)
1766+
{
1767+
isEquals("service does not provide 256 bits of security only 128", e.getMessage());
1768+
}
1769+
1770+
engine.init(false, new KeyParameter(new byte[16])); // should work
1771+
1772+
engine.init(true, new KeyParameter(new byte[32])); // should work
1773+
1774+
CryptoServicesRegistrar.setServicesConstraints(null);
1775+
}
1776+
1777+
private void testShacal2()
1778+
{
1779+
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(256, 128));
1780+
Shacal2Engine engine = new Shacal2Engine();
1781+
try
1782+
{
1783+
engine.init(true, new KeyParameter(new byte[16]));
1784+
fail("no exception!");
1785+
}
1786+
catch (CryptoServiceConstraintsException e)
1787+
{
1788+
isEquals("service does not provide 256 bits of security only 128", e.getMessage());
1789+
}
1790+
1791+
engine.init(false, new KeyParameter(new byte[16])); // should work
1792+
1793+
engine.init(true, new KeyParameter(new byte[32])); // should work
1794+
1795+
CryptoServicesRegistrar.setServicesConstraints(null);
1796+
}
1797+
16891798
private void testThreefish()
16901799
{
16911800
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(384, 256));
@@ -1778,7 +1887,7 @@ private void testZuc128AndZuc256()
17781887
}
17791888
catch (CryptoServiceConstraintsException e)
17801889
{
1781-
isEquals(e.getMessage(),"service does not provide 256 bits of security only 128", e.getMessage());
1890+
isEquals(e.getMessage(), "service does not provide 256 bits of security only 128", e.getMessage());
17821891
}
17831892

17841893
xengine.init(true, new ParametersWithIV(new KeyParameter(new byte[32]), new byte[25]));
@@ -1800,7 +1909,7 @@ private void testVMPCAndVMPCKSA()
18001909
}
18011910
catch (CryptoServiceConstraintsException e)
18021911
{
1803-
isEquals(e.getMessage(),"service does not provide 256 bits of security only 128", e.getMessage());
1912+
isEquals(e.getMessage(), "service does not provide 256 bits of security only 128", e.getMessage());
18041913
}
18051914

18061915
xengine.init(true, new ParametersWithIV(new KeyParameter(new byte[32]), new byte[25]));
@@ -1822,7 +1931,7 @@ private void testRC532AndRC564()
18221931
}
18231932
catch (CryptoServiceConstraintsException e)
18241933
{
1825-
isEquals(e.getMessage(),"service does not provide 256 bits of security only 128", e.getMessage());
1934+
isEquals(e.getMessage(), "service does not provide 256 bits of security only 128", e.getMessage());
18261935
}
18271936

18281937
try
@@ -1832,7 +1941,7 @@ private void testRC532AndRC564()
18321941
}
18331942
catch (CryptoServiceConstraintsException e)
18341943
{
1835-
isEquals(e.getMessage(),"service does not provide 256 bits of security only 128", e.getMessage());
1944+
isEquals(e.getMessage(), "service does not provide 256 bits of security only 128", e.getMessage());
18361945
}
18371946

18381947
xengine.init(true, new RC5Parameters(new byte[32], 12));
@@ -1861,6 +1970,7 @@ private void testRijndael()
18611970

18621971
CryptoServicesRegistrar.setServicesConstraints(null);
18631972
}
1973+
18641974
private void testHC128AndHC256()
18651975
{
18661976
CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(256, 128));
@@ -1873,7 +1983,7 @@ private void testHC128AndHC256()
18731983
}
18741984
catch (CryptoServiceConstraintsException e)
18751985
{
1876-
isEquals(e.getMessage(),"service does not provide 256 bits of security only 128", e.getMessage());
1986+
isEquals(e.getMessage(), "service does not provide 256 bits of security only 128", e.getMessage());
18771987
}
18781988

18791989
xengine.init(false, new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16]));
@@ -1905,12 +2015,12 @@ private void testSM2Cipher()
19052015
}
19062016
catch (CryptoServiceConstraintsException e)
19072017
{
1908-
isEquals(e.getMessage(),"service does not provide 256 bits of security only 128", e.getMessage());
2018+
isEquals(e.getMessage(), "service does not provide 256 bits of security only 128", e.getMessage());
19092019
}
19102020

19112021
// decryption should be okay
19122022
engine.init(false, kp.getPrivate());
1913-
2023+
19142024
CryptoServicesRegistrar.setServicesConstraints(null);
19152025
}
19162026

0 commit comments

Comments
 (0)