|
4 | 4 | import java.security.SecureRandom; |
5 | 5 | import java.util.Collections; |
6 | 6 |
|
| 7 | +import org.bouncycastle.asn1.x9.ECNamedCurveTable; |
7 | 8 | import org.bouncycastle.asn1.x9.X962NamedCurves; |
8 | 9 | import org.bouncycastle.crypto.AsymmetricCipherKeyPair; |
9 | 10 | import org.bouncycastle.crypto.BasicAgreement; |
|
44 | 45 | import org.bouncycastle.crypto.digests.SHA3Digest; |
45 | 46 | import org.bouncycastle.crypto.digests.SHA512Digest; |
46 | 47 | import org.bouncycastle.crypto.digests.SHAKEDigest; |
47 | | -import org.bouncycastle.crypto.engines.*; |
| 48 | +import org.bouncycastle.crypto.engines.AESEngine; |
| 49 | +import org.bouncycastle.crypto.engines.AESFastEngine; |
| 50 | +import org.bouncycastle.crypto.engines.AESLightEngine; |
| 51 | +import org.bouncycastle.crypto.engines.BlowfishEngine; |
| 52 | +import org.bouncycastle.crypto.engines.CAST5Engine; |
| 53 | +import org.bouncycastle.crypto.engines.CamelliaEngine; |
| 54 | +import org.bouncycastle.crypto.engines.CamelliaLightEngine; |
| 55 | +import org.bouncycastle.crypto.engines.ChaCha7539Engine; |
| 56 | +import org.bouncycastle.crypto.engines.ChaChaEngine; |
| 57 | +import org.bouncycastle.crypto.engines.DESEngine; |
| 58 | +import org.bouncycastle.crypto.engines.DESedeEngine; |
| 59 | +import org.bouncycastle.crypto.engines.ElGamalEngine; |
| 60 | +import org.bouncycastle.crypto.engines.HC128Engine; |
| 61 | +import org.bouncycastle.crypto.engines.HC256Engine; |
| 62 | +import org.bouncycastle.crypto.engines.IDEAEngine; |
| 63 | +import org.bouncycastle.crypto.engines.RC4Engine; |
| 64 | +import org.bouncycastle.crypto.engines.RC532Engine; |
| 65 | +import org.bouncycastle.crypto.engines.RC564Engine; |
| 66 | +import org.bouncycastle.crypto.engines.RSAEngine; |
| 67 | +import org.bouncycastle.crypto.engines.RijndaelEngine; |
| 68 | +import org.bouncycastle.crypto.engines.SM2Engine; |
| 69 | +import org.bouncycastle.crypto.engines.SM4Engine; |
| 70 | +import org.bouncycastle.crypto.engines.Salsa20Engine; |
| 71 | +import org.bouncycastle.crypto.engines.SerpentEngine; |
| 72 | +import org.bouncycastle.crypto.engines.SkipjackEngine; |
| 73 | +import org.bouncycastle.crypto.engines.TEAEngine; |
| 74 | +import org.bouncycastle.crypto.engines.ThreefishEngine; |
| 75 | +import org.bouncycastle.crypto.engines.TnepresEngine; |
| 76 | +import org.bouncycastle.crypto.engines.TwofishEngine; |
| 77 | +import org.bouncycastle.crypto.engines.VMPCEngine; |
| 78 | +import org.bouncycastle.crypto.engines.VMPCKSA3Engine; |
| 79 | +import org.bouncycastle.crypto.engines.XSalsa20Engine; |
| 80 | +import org.bouncycastle.crypto.engines.Zuc128Engine; |
| 81 | +import org.bouncycastle.crypto.engines.Zuc256Engine; |
48 | 82 | import org.bouncycastle.crypto.generators.DESKeyGenerator; |
49 | 83 | import org.bouncycastle.crypto.generators.DESedeKeyGenerator; |
50 | 84 | import org.bouncycastle.crypto.generators.DHBasicKeyPairGenerator; |
|
90 | 124 | import org.bouncycastle.crypto.params.MQVPrivateParameters; |
91 | 125 | import org.bouncycastle.crypto.params.MQVPublicParameters; |
92 | 126 | import org.bouncycastle.crypto.params.ParametersWithIV; |
| 127 | +import org.bouncycastle.crypto.params.ParametersWithRandom; |
93 | 128 | import org.bouncycastle.crypto.params.ParametersWithUKM; |
94 | 129 | import org.bouncycastle.crypto.params.RC5Parameters; |
95 | 130 | import org.bouncycastle.crypto.params.RSAKeyGenerationParameters; |
@@ -167,6 +202,7 @@ public void performTest() |
167 | 202 | testRSA(); |
168 | 203 | testRsaKEM(); |
169 | 204 | testECIESKEM(); |
| 205 | + testSM2Cipher(); |
170 | 206 | testSM4(); |
171 | 207 | testTEA(); |
172 | 208 | testThreefish(); |
@@ -1851,6 +1887,33 @@ private void testHC128AndHC256() |
1851 | 1887 | CryptoServicesRegistrar.setServicesConstraints(null); |
1852 | 1888 | } |
1853 | 1889 |
|
| 1890 | + private void testSM2Cipher() |
| 1891 | + { |
| 1892 | + SecureRandom random = new SecureRandom(); |
| 1893 | + ECKeyPairGenerator kpGen = new ECKeyPairGenerator(); |
| 1894 | + |
| 1895 | + kpGen.init(new ECKeyGenerationParameters(new ECDomainParameters(ECNamedCurveTable.getByName("P-256")), random)); |
| 1896 | + |
| 1897 | + AsymmetricCipherKeyPair kp = kpGen.generateKeyPair(); |
| 1898 | + |
| 1899 | + CryptoServicesRegistrar.setServicesConstraints(new LegacyBitsOfSecurityConstraint(256, 128)); |
| 1900 | + SM2Engine engine = new SM2Engine(); |
| 1901 | + try |
| 1902 | + { |
| 1903 | + engine.init(true, new ParametersWithRandom(kp.getPublic(), random)); |
| 1904 | + fail("no exception!"); |
| 1905 | + } |
| 1906 | + catch (CryptoServiceConstraintsException e) |
| 1907 | + { |
| 1908 | + isEquals(e.getMessage(),"service does not provide 256 bits of security only 128", e.getMessage()); |
| 1909 | + } |
| 1910 | + |
| 1911 | + // decryption should be okay |
| 1912 | + engine.init(false, kp.getPrivate()); |
| 1913 | + |
| 1914 | + CryptoServicesRegistrar.setServicesConstraints(null); |
| 1915 | + } |
| 1916 | + |
1854 | 1917 | public static void main( |
1855 | 1918 | String[] args) |
1856 | 1919 | { |
|
0 commit comments