|
| 1 | +package org.bouncycastle.crypto.test; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | + |
| 5 | +import org.bouncycastle.crypto.AsymmetricCipherKeyPair; |
| 6 | +import org.bouncycastle.crypto.InvalidCipherTextException; |
| 7 | +import org.bouncycastle.crypto.engines.DESedeWrapEngine; |
| 8 | +import org.bouncycastle.crypto.engines.DSTU7624WrapEngine; |
| 9 | +import org.bouncycastle.crypto.engines.GOST28147WrapEngine; |
| 10 | +import org.bouncycastle.crypto.engines.RC2WrapEngine; |
| 11 | +import org.bouncycastle.crypto.engines.SM2Engine; |
| 12 | +import org.bouncycastle.crypto.generators.ECKeyPairGenerator; |
| 13 | +import org.bouncycastle.crypto.params.ECDomainParameters; |
| 14 | +import org.bouncycastle.crypto.params.ECKeyGenerationParameters; |
| 15 | +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; |
| 16 | +import org.bouncycastle.crypto.params.KeyParameter; |
| 17 | +import org.bouncycastle.crypto.params.ParametersWithUKM; |
| 18 | +import org.bouncycastle.crypto.params.RC2Parameters; |
| 19 | +import org.bouncycastle.math.ec.ECConstants; |
| 20 | +import org.bouncycastle.math.ec.ECCurve; |
| 21 | +import org.bouncycastle.math.ec.ECPoint; |
| 22 | +import org.bouncycastle.util.encoders.Hex; |
| 23 | +import org.bouncycastle.util.test.SimpleTest; |
| 24 | +import org.bouncycastle.util.test.TestRandomBigInteger; |
| 25 | + |
| 26 | +/** |
| 27 | + * A ciphertext / wrapped-key shorter than the fixed overhead the engine strips |
| 28 | + * must be rejected with an {@link InvalidCipherTextException}, not an escaping |
| 29 | + * {@code NegativeArraySizeException} / {@code ArrayIndexOutOfBoundsException} |
| 30 | + * from the {@code new byte[inLen - overhead]} allocation. |
| 31 | + */ |
| 32 | +public class ShortWrapCipherTextTest |
| 33 | + extends SimpleTest |
| 34 | +{ |
| 35 | + public String getName() |
| 36 | + { |
| 37 | + return "ShortWrapCipherText"; |
| 38 | + } |
| 39 | + |
| 40 | + private void sm2() |
| 41 | + throws Exception |
| 42 | + { |
| 43 | + BigInteger p = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", 16); |
| 44 | + BigInteger a = new BigInteger("787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", 16); |
| 45 | + BigInteger b = new BigInteger("63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", 16); |
| 46 | + BigInteger n = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", 16); |
| 47 | + BigInteger gx = new BigInteger("421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", 16); |
| 48 | + BigInteger gy = new BigInteger("0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", 16); |
| 49 | + |
| 50 | + ECCurve curve = new ECCurve.Fp(p, a, b, n, ECConstants.ONE); |
| 51 | + ECPoint g = curve.createPoint(gx, gy); |
| 52 | + ECDomainParameters domainParams = new ECDomainParameters(curve, g, n); |
| 53 | + |
| 54 | + ECKeyPairGenerator kpg = new ECKeyPairGenerator(); |
| 55 | + kpg.init(new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0", 16))); |
| 56 | + AsymmetricCipherKeyPair kp = kpg.generateKeyPair(); |
| 57 | + |
| 58 | + SM2Engine engine = new SM2Engine(); |
| 59 | + engine.init(false, (ECPrivateKeyParameters)kp.getPrivate()); |
| 60 | + |
| 61 | + // C1 (65) + C3 (32) is 97 octets; anything shorter cannot hold C1 + digest. |
| 62 | + try |
| 63 | + { |
| 64 | + engine.processBlock(new byte[50], 0, 50); |
| 65 | + fail("SM2Engine: no exception on short ciphertext"); |
| 66 | + } |
| 67 | + catch (InvalidCipherTextException e) |
| 68 | + { |
| 69 | + isEquals("ciphertext too short", e.getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void gost28147Wrap() |
| 74 | + throws Exception |
| 75 | + { |
| 76 | + GOST28147WrapEngine engine = new GOST28147WrapEngine(); |
| 77 | + engine.init(false, new ParametersWithUKM(new KeyParameter(new byte[32]), new byte[8])); |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + engine.unwrap(new byte[16], 0, 16); |
| 82 | + fail("GOST28147WrapEngine: no exception on short input"); |
| 83 | + } |
| 84 | + catch (InvalidCipherTextException e) |
| 85 | + { |
| 86 | + isEquals("unwrap data too short", e.getMessage()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void dstu7624Wrap() |
| 91 | + throws Exception |
| 92 | + { |
| 93 | + DSTU7624WrapEngine engine = new DSTU7624WrapEngine(128); |
| 94 | + engine.init(false, new KeyParameter(new byte[16])); |
| 95 | + |
| 96 | + try |
| 97 | + { |
| 98 | + engine.unwrap(new byte[0], 0, 0); |
| 99 | + fail("DSTU7624WrapEngine: no exception on empty input"); |
| 100 | + } |
| 101 | + catch (InvalidCipherTextException e) |
| 102 | + { |
| 103 | + isEquals("unwrap data too short", e.getMessage()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void desedeWrap() |
| 108 | + throws Exception |
| 109 | + { |
| 110 | + DESedeWrapEngine engine = new DESedeWrapEngine(); |
| 111 | + engine.init(false, new KeyParameter(Hex.decode("0123456789abcdeffedcba987654321089abcdef01234567"))); |
| 112 | + |
| 113 | + try |
| 114 | + { |
| 115 | + engine.unwrap(new byte[8], 0, 8); |
| 116 | + fail("DESedeWrapEngine: no exception on short input"); |
| 117 | + } |
| 118 | + catch (InvalidCipherTextException e) |
| 119 | + { |
| 120 | + isEquals("unwrap data too short", e.getMessage()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void rc2Wrap() |
| 125 | + throws Exception |
| 126 | + { |
| 127 | + RC2WrapEngine engine = new RC2WrapEngine(); |
| 128 | + engine.init(false, new RC2Parameters(new byte[16])); |
| 129 | + |
| 130 | + try |
| 131 | + { |
| 132 | + engine.unwrap(new byte[8], 0, 8); |
| 133 | + fail("RC2WrapEngine: no exception on short input"); |
| 134 | + } |
| 135 | + catch (InvalidCipherTextException e) |
| 136 | + { |
| 137 | + isEquals("unwrap data too short", e.getMessage()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public void performTest() |
| 142 | + throws Exception |
| 143 | + { |
| 144 | + sm2(); |
| 145 | + gost28147Wrap(); |
| 146 | + dstu7624Wrap(); |
| 147 | + desedeWrap(); |
| 148 | + rc2Wrap(); |
| 149 | + } |
| 150 | + |
| 151 | + public static void main(String[] args) |
| 152 | + { |
| 153 | + runTest(new ShortWrapCipherTextTest()); |
| 154 | + } |
| 155 | +} |
0 commit comments