Add spongy castle sources to libraries folder

This commit is contained in:
Dominik Schürmann
2014-01-27 14:00:22 +01:00
parent 8ca42b9bf9
commit 5aec25ac05
4258 changed files with 848014 additions and 0 deletions

View File

@@ -0,0 +1,461 @@
package java.math.test;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.spongycastle.util.test.*;
public class BigIntegerTest
extends SimpleTest
{
private static BigInteger VALUE1 = new BigInteger("1234");
private static BigInteger VALUE2 = new BigInteger("1234567890");
private static BigInteger VALUE3 = new BigInteger("12345678901234567890123");
private static BigInteger zero = BigInteger.ZERO;
private static BigInteger one = BigInteger.ONE;
private static BigInteger two = BigInteger.valueOf(2);
public String getName()
{
return "BigInteger";
}
private void clearBitTest()
{
BigInteger value = VALUE1.clearBit(3);
BigInteger result = new BigInteger("1234");
if (!value.equals(result))
{
fail("clearBit - expected: " + result + " got: " + value);
}
value = VALUE2.clearBit(3);
result = new BigInteger("1234567890");
if (!value.equals(result))
{
fail("clearBit - expected: " + result + " got: " + value);
}
value = VALUE3.clearBit(3);
result = new BigInteger("12345678901234567890115");
if (!value.equals(result))
{
fail("clearBit - expected: " + result + " got: " + value);
}
value = VALUE2.clearBit(55);
result = new BigInteger("1234567890");
if (!value.equals(result))
{
fail("clearBit - expected: " + result + " got: " + value);
}
value = VALUE3.clearBit(55);
result = new BigInteger("12345642872437548926155");
if (!value.equals(result))
{
fail("clearBit - expected: " + result + " got: " + value);
}
}
private void flipBitTest()
{
BigInteger value = VALUE1.flipBit(3);
BigInteger result = new BigInteger("1242");
if (!value.equals(result))
{
fail("flipBit - expected: " + result + " got: " + value);
}
value = VALUE2.flipBit(3);
result = new BigInteger("1234567898");
if (!value.equals(result))
{
fail("flipBit - expected: " + result + " got: " + value);
}
value = VALUE3.flipBit(3);
result = new BigInteger("12345678901234567890115");
if (!value.equals(result))
{
fail("flipBit - expected: " + result + " got: " + value);
}
value = VALUE2.flipBit(55);
result = new BigInteger("36028798253531858");
if (!value.equals(result))
{
fail("flipBit - expected: " + result + " got: " + value);
}
value = VALUE3.flipBit(55);
result = new BigInteger("12345642872437548926155");
if (!value.equals(result))
{
fail("flipBit - expected: " + result + " got: " + value);
}
}
private void setBitTest()
{
BigInteger value = VALUE1.setBit(3);
BigInteger result = new BigInteger("1242");
if (!value.equals(result))
{
fail("setBit - expected: " + result + " got: " + value);
}
value = VALUE2.setBit(3);
result = new BigInteger("1234567898");
if (!value.equals(result))
{
fail("setBit - expected: " + result + " got: " + value);
}
value = VALUE3.setBit(3);
result = new BigInteger("12345678901234567890123");
if (!value.equals(result))
{
fail("setBit - expected: " + result + " got: " + value);
}
value = VALUE2.setBit(55);
result = new BigInteger("36028798253531858");
if (!value.equals(result))
{
fail("setBit - expected: " + result + " got: " + value);
}
value = VALUE3.setBit(55);
result = new BigInteger("12345678901234567890123");
if (!value.equals(result))
{
fail("setBit - expected: " + result + " got: " + value);
}
}
private void testDivideAndRemainder()
{
SecureRandom random = new SecureRandom();
BigInteger n = new BigInteger(48, random);
BigInteger[] qr = n.divideAndRemainder(n);
if (!qr[0].equals(one) || !qr[1].equals(zero))
{
fail("testDivideAndRemainder - expected: 1/0 got: " + qr[0] + "/" + qr[1]);
}
qr = n.divideAndRemainder(one);
if (!qr[0].equals(n) || !qr[1].equals(zero))
{
fail("testDivideAndRemainder - expected: " + n + "/0 got: " + qr[0] + "/" + qr[1]);
}
for (int rep = 0; rep < 10; ++rep)
{
BigInteger a = new BigInteger(100 - rep, 0, random);
BigInteger b = new BigInteger(100 + rep, 0, random);
BigInteger c = new BigInteger(10 + rep, 0, random);
BigInteger d = a.multiply(b).add(c);
BigInteger[] es = d.divideAndRemainder(a);
if (!es[0].equals(b) || !es[1].equals(c))
{
fail("testDivideAndRemainder - expected: " + b + "/" + c + " got: " + qr[0] + "/" + qr[1]);
}
}
}
private void testModInverse()
{
SecureRandom random = new SecureRandom();
for (int i = 0; i < 10; ++i)
{
BigInteger p = BigInteger.probablePrime(64, random);
BigInteger q = new BigInteger(63, random).add(one);
BigInteger inv = q.modInverse(p);
BigInteger inv2 = inv.modInverse(p);
if (!q.equals(inv2))
{
fail("testModInverse failed symmetry test");
}
BigInteger check = q.multiply(inv).mod(p);
if (!one.equals(check))
{
fail("testModInverse - expected: 1 got: " + check);
}
}
// ModInverse for powers of 2
for (int i = 1; i <= 128; ++i)
{
BigInteger m = one.shiftLeft(i);
BigInteger d = new BigInteger(i, random).setBit(0);
BigInteger x = d.modInverse(m);
BigInteger check = x.multiply(d).mod(m);
if (!one.equals(check))
{
fail("testModInverse - expected: 1 got: " + check);
}
}
}
private void testNegate()
{
if (!zero.equals(zero.negate()))
{
fail("zero - negate falied");
}
if (!one.equals(one.negate().negate()))
{
fail("one - negate falied");
}
if (!two.equals(two.negate().negate()))
{
fail("two - negate falied");
}
}
private void testNot()
{
for (int i = -10; i <= 10; ++i)
{
if(!BigInteger.valueOf(~i).equals(
BigInteger.valueOf(i).not()))
{
fail("Problem: ~" + i + " should be " + ~i);
}
}
}
private void testOr()
{
for (int i = -10; i <= 10; ++i)
{
for (int j = -10; j <= 10; ++j)
{
if (!BigInteger.valueOf(i | j).equals(
BigInteger.valueOf(i).or(BigInteger.valueOf(j))))
{
fail("Problem: " + i + " OR " + j + " should be " + (i | j));
}
}
}
}
public void testPow()
{
if (!one.equals(zero.pow(0)))
{
fail("one pow equals failed");
}
if (!zero.equals(zero.pow(123)))
{
fail("zero pow equals failed");
}
if (!one.equals(one.pow(0)))
{
fail("one one equals failed");
}
if (!one.equals(one.pow(123)))
{
fail("1 123 equals failed");
}
if (!two.pow(147).equals(one.shiftLeft(147)))
{
fail("2 pow failed");
}
if (!one.shiftLeft(7).pow(11).equals(one.shiftLeft(77)))
{
fail("pow 2 pow failed");
}
BigInteger n = new BigInteger("1234567890987654321");
BigInteger result = one;
for (int i = 0; i < 10; ++i)
{
try
{
BigInteger.valueOf(i).pow(-1);
fail("expected ArithmeticException");
}
catch (ArithmeticException e) {}
if (!result.equals(n.pow(i)))
{
fail("mod pow equals failed");
}
result = result.multiply(n);
}
}
public void testToString()
{
SecureRandom random = new SecureRandom();
int trials = 256;
BigInteger[] tests = new BigInteger[trials];
for (int i = 0; i < trials; ++i)
{
int len = random.nextInt(i + 1);
tests[i] = new BigInteger(len, random);
}
for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; ++radix)
{
for (int i = 0; i < trials; ++i)
{
BigInteger n1 = tests[i];
String s = n1.toString(radix);
BigInteger n2 = new BigInteger(s, radix);
if (!n1.equals(n2))
{
fail("testToStringRadix - radix:" + radix + ", n1:" + n1.toString(16) + ", n2:" + n2.toString(16));
}
}
}
}
private void xorTest()
{
BigInteger value = VALUE1.xor(VALUE2);
BigInteger result = new BigInteger("1234568704");
if (!value.equals(result))
{
fail("xor - expected: " + result + " got: " + value);
}
value = VALUE1.xor(VALUE3);
result = new BigInteger("12345678901234567888921");
if (!value.equals(result))
{
fail("xor - expected: " + result + " got: " + value);
}
value = VALUE3.xor(VALUE1);
result = new BigInteger("12345678901234567888921");
if (!value.equals(result))
{
fail("xor - expected: " + result + " got: " + value);
}
value = VALUE2.xor(new BigInteger("-1"));
result = new BigInteger("-1234567891");
if (!value.equals(result))
{
fail("xor - expected: " + result + " got: " + value);
}
value = VALUE3.xor(VALUE3);
result = new BigInteger("0");
if (!value.equals(result))
{
fail("xor - expected: " + result + " got: " + value);
}
}
public void performTest()
{
clearBitTest();
flipBitTest();
setBitTest();
testDivideAndRemainder();
testModInverse();
testNegate();
testNot();
testOr();
testPow();
testToString();
xorTest();
BigInteger n1, n2, r1;
// test division where the difference in bit length of the dividend and divisor is 32 bits
n1 = new BigInteger("54975581388");
n2 = new BigInteger("10");
r1 = n1.divide(n2);
if (!r1.toString(10).equals("5497558138"))
{
fail("BigInteger: failed Divide Test");
}
// two's complement test
byte[] zeroBytes = BigInteger.ZERO.toByteArray();
byte[] oneBytes = BigInteger.ONE.toByteArray();
byte[] minusOneBytes = BigInteger.ONE.negate().toByteArray();
BigInteger zero = new BigInteger(zeroBytes);
if (!zero.equals(BigInteger.ZERO))
{
fail("Failed constructing zero");
}
BigInteger one = new BigInteger(oneBytes);
if (!one.equals(BigInteger.ONE))
{
fail("Failed constructing one");
}
BigInteger minusOne = new BigInteger(minusOneBytes);
if (!minusOne.equals(BigInteger.ONE.negate()))
{
fail("Failed constructing minus one");
}
SecureRandom random = new SecureRandom();
byte[] randomBytes = new byte[100];
for (int i=0; i < 100; i++)
{
random.nextBytes(randomBytes);
BigInteger bcInt = new BigInteger(randomBytes);
BigInteger bcInt2 = new BigInteger(bcInt.toByteArray());
if (!bcInt.equals(bcInt2))
{
fail("Failed constructing random value " + i);
}
// java.math.BigInteger jdkInt = new java.math.BigInteger(randomBytes);
// byte[] bcBytes = bcInt.toByteArray();
// byte[] jdkBytes = jdkInt.toByteArray();
// if (!arrayEquals(bcBytes, jdkBytes))
// {
// fail(""Failed constructing random value " + i);
// }
}
}
public static void main(
String[] args)
{
runTest(new BigIntegerTest());
}
}

View File

@@ -0,0 +1,21 @@
package java.math.test;
import org.spongycastle.util.test.Test;
import org.spongycastle.util.test.TestResult;
public class RegressionTest
{
public static Test[] tests = {
new BigIntegerTest()
};
public static void main(
String[] args)
{
for (int i = 0; i != tests.length; i++)
{
TestResult result = tests[i].perform();
System.out.println(result);
}
}
}

View File

@@ -0,0 +1,189 @@
package org.spongycastle.asn1.test;
import java.util.Date;
import java.util.TimeZone;
import org.spongycastle.asn1.ASN1GeneralizedTime;
import org.spongycastle.asn1.DERGeneralizedTime;
import org.spongycastle.util.test.SimpleTest;
/**
* X.690 test example
*/
public class GeneralizedTimeTest
extends SimpleTest
{
String[] input =
{
"20020122122220",
"20020122122220Z",
"20020122122220-1000",
"20020122122220+00",
"20020122122220.1",
"20020122122220.1Z",
"20020122122220.1-1000",
"20020122122220.1+00",
"20020122122220.01",
"20020122122220.01Z",
"20020122122220.01-1000",
"20020122122220.01+00",
"20020122122220.001",
"20020122122220.001Z",
"20020122122220.001-1000",
"20020122122220.001+00",
"20020122122220.0001",
"20020122122220.0001Z",
"20020122122220.0001-1000",
"20020122122220.0001+00",
"20020122122220.0001+1000"
};
String[] output = {
"20020122122220",
"20020122122220GMT+00:00",
"20020122122220GMT-10:00",
"20020122122220GMT+00:00",
"20020122122220.1",
"20020122122220.1GMT+00:00",
"20020122122220.1GMT-10:00",
"20020122122220.1GMT+00:00",
"20020122122220.01",
"20020122122220.01GMT+00:00",
"20020122122220.01GMT-10:00",
"20020122122220.01GMT+00:00",
"20020122122220.001",
"20020122122220.001GMT+00:00",
"20020122122220.001GMT-10:00",
"20020122122220.001GMT+00:00",
"20020122122220.0001",
"20020122122220.0001GMT+00:00",
"20020122122220.0001GMT-10:00",
"20020122122220.0001GMT+00:00",
"20020122122220.0001GMT+10:00" };
String[] zOutput = {
"20020122122220Z",
"20020122122220Z",
"20020122222220Z",
"20020122122220Z",
"20020122122220Z",
"20020122122220Z",
"20020122222220Z",
"20020122122220Z",
"20020122122220Z",
"20020122122220Z",
"20020122222220Z",
"20020122122220Z",
"20020122122220Z",
"20020122122220Z",
"20020122222220Z",
"20020122122220Z",
"20020122122220Z",
"20020122122220Z",
"20020122222220Z",
"20020122122220Z",
"20020122022220Z"
};
String[] mzOutput = {
"20020122122220.000Z",
"20020122122220.000Z",
"20020122222220.000Z",
"20020122122220.000Z",
"20020122122220.100Z",
"20020122122220.100Z",
"20020122222220.100Z",
"20020122122220.100Z",
"20020122122220.010Z",
"20020122122220.010Z",
"20020122222220.010Z",
"20020122122220.010Z",
"20020122122220.001Z",
"20020122122220.001Z",
"20020122222220.001Z",
"20020122122220.001Z",
"20020122122220.000Z",
"20020122122220.000Z",
"20020122222220.000Z",
"20020122122220.000Z",
"20020122022220.000Z"
};
public String getName()
{
return "GeneralizedTime";
}
public void performTest()
throws Exception
{
for (int i = 0; i != input.length; i++)
{
DERGeneralizedTime t = new DERGeneralizedTime(input[i]);
if (output[i].indexOf('G') > 0) // don't check local time the same way
{
if (!t.getTime().equals(output[i]))
{
fail("failed conversion test");
}
}
else
{
String offset = calculateGMTOffset(t.getDate());
if (!t.getTime().equals(output[i] + offset))
{
fail("failed conversion test");
}
}
}
for (int i = 0; i != input.length; i++)
{
ASN1GeneralizedTime t = new ASN1GeneralizedTime(mzOutput[i]);
if (!new ASN1GeneralizedTime(t.getDate(), true).getDate().equals(t.getDate()))
{
fail("failed equality test");
}
}
}
private String calculateGMTOffset(Date date)
{
String sign = "+";
TimeZone timeZone = TimeZone.getDefault();
int offset = timeZone.getRawOffset();
if (offset < 0)
{
sign = "-";
offset = -offset;
}
int hours = offset / (60 * 60 * 1000);
int minutes = (offset - (hours * 60 * 60 * 1000)) / (60 * 1000);
// if (timeZone.useDaylightTime() && timeZone.inDaylightTime(date))
// {
// hours += sign.equals("+") ? 1 : -1;
// }
return "GMT" + sign + convert(hours) + ":" + convert(minutes);
}
private String convert(int time)
{
if (time < 10)
{
return "0" + time;
}
return Integer.toString(time);
}
public static void main(
String[] args)
{
runTest(new GeneralizedTimeTest());
}
}

View File

@@ -0,0 +1,63 @@
package org.spongycastle.asn1.test;
import org.spongycastle.util.test.Test;
import org.spongycastle.util.test.TestResult;
public class RegressionTest
{
public static Test[] tests = {
new CertificateTest(),
new CMSTest(),
new OCSPTest(),
new OIDTest(),
new PKCS10Test(),
new PKCS12Test(),
new X509NameTest(),
new X500NameTest(),
new X509ExtensionsTest(),
new BitStringTest(),
new MiscTest(),
new X9Test(),
new EncryptedPrivateKeyInfoTest(),
new StringTest(),
new RequestedCertificateUnitTest(),
new OtherCertIDUnitTest(),
new OtherSigningCertificateUnitTest(),
new ContentHintsUnitTest(),
new CertHashUnitTest(),
new AdditionalInformationSyntaxUnitTest(),
new AdmissionSyntaxUnitTest(),
new AdmissionsUnitTest(),
new DeclarationOfMajorityUnitTest(),
new ProcurationSyntaxUnitTest(),
new ProfessionInfoUnitTest(),
new RestrictionUnitTest(),
new NamingAuthorityUnitTest(),
new MonetaryLimitUnitTest(),
new DERApplicationSpecificTest(),
new IssuingDistributionPointUnitTest(),
new TargetInformationTest(),
new SubjectKeyIdentifierTest(),
new ESSCertIDv2UnitTest(),
new ParsingTest(),
new GeneralNameTest(),
new RFC4519Test()
};
public static void main(
String[] args)
{
for (int i = 0; i != tests.length; i++)
{
TestResult result = tests[i].perform();
if (result.getException() != null)
{
result.getException().printStackTrace();
}
System.out.println(result);
}
}
}

View File

@@ -0,0 +1,98 @@
package org.spongycastle.asn1.test;
import org.spongycastle.asn1.ASN1UTCTime;
import org.spongycastle.asn1.DERUTCTime;
import org.spongycastle.util.test.SimpleTest;
/**
* X.690 test example
*/
public class UTCTimeTest
extends SimpleTest
{
String[] input =
{
"020122122220Z",
"020122122220-1000",
"020122122220+1000",
"020122122220+00",
"0201221222Z",
"0201221222-1000",
"0201221222+1000",
"0201221222+00",
"550122122220Z",
"5501221222Z"
};
String[] output = {
"20020122122220GMT+00:00",
"20020122122220GMT-10:00",
"20020122122220GMT+10:00",
"20020122122220GMT+00:00",
"20020122122200GMT+00:00",
"20020122122200GMT-10:00",
"20020122122200GMT+10:00",
"20020122122200GMT+00:00",
"19550122122220GMT+00:00",
"19550122122200GMT+00:00"
};
String[] zOutput1 = {
"20020122122220Z",
"20020122222220Z",
"20020122022220Z",
"20020122122220Z",
"20020122122200Z",
"20020122222200Z",
"20020122022200Z",
"20020122122200Z",
"19550122122220Z",
"19550122122200Z"
};
String[] zOutput2 = {
"20020122122220Z",
"20020122222220Z",
"20020122022220Z",
"20020122122220Z",
"20020122122200Z",
"20020122222200Z",
"20020122022200Z",
"20020122122200Z",
"19550122122220Z",
"19550122122200Z"
};
public String getName()
{
return "UTCTime";
}
public void performTest()
throws Exception
{
for (int i = 0; i != input.length; i++)
{
DERUTCTime t = new DERUTCTime(input[i]);
if (!t.getAdjustedTime().equals(output[i]))
{
fail("failed conversion test " + i);
}
t = new ASN1UTCTime(zOutput1[i].substring(2));
if (!new ASN1UTCTime(t.getAdjustedDate()).getAdjustedTime().equals(t.getAdjustedTime()))
{
fail("failed equality test");
}
}
}
public static void main(
String[] args)
{
runTest(new UTCTimeTest());
}
}

View File

@@ -0,0 +1,32 @@
package org.spongycastle.crypto.test;
import org.spongycastle.util.test.*;
public class BigIntegerTest
{
public static Test[] tests = {
new DHTest(),
new ElGamalTest(),
new DSATest(),
new ECTest(),
new ECIESTest(),
new RSATest(),
new ISO9796Test(),
new OAEPTest(),
new PSSTest(),
new CTSTest(),
new PKCS5Test(),
new PKCS12Test()
};
public static void main(
String[] args)
{
for (int i = 0; i != tests.length; i++)
{
TestResult result = tests[i].perform();
System.out.println(result);
}
}
}

View File

@@ -0,0 +1,64 @@
package org.spongycastle.crypto.test;
import org.spongycastle.util.test.*;
public class CryptoRegressionTest
{
public static Test[] tests = {
new AESTest(),
new DESTest(),
new DESedeTest(),
new ModeTest(),
new DHTest(),
new ElGamalTest(),
new DSATest(),
new ECTest(),
new ECIESTest(),
new MacTest(),
new RC2Test(),
new RC4Test(),
new RC5Test(),
new RC6Test(),
new RijndaelTest(),
new SerpentTest(),
new SkipjackTest(),
new BlowfishTest(),
new TwofishTest(),
new CAST5Test(),
new CAST6Test(),
new IDEATest(),
new CamelliaTest(),
new RSATest(),
new ISO9796Test(),
new MD2DigestTest(),
new MD4DigestTest(),
new MD5DigestTest(),
new SHA1DigestTest(),
new SHA256DigestTest(),
new SHA384DigestTest(),
new SHA512DigestTest(),
new RIPEMD128DigestTest(),
new RIPEMD160DigestTest(),
new TigerDigestTest(),
new MD5HMacTest(),
new SHA1HMacTest(),
new RIPEMD128HMacTest(),
new RIPEMD160HMacTest(),
/* new OAEPTest() */
new PSSTest(),
new CTSTest(),
/* new PKCS5Test() */
new PKCS12Test()
};
public static void main(
String[] args)
{
for (int i = 0; i != tests.length; i++)
{
TestResult result = tests[i].perform();
System.out.println(result);
}
}
}

View File

@@ -0,0 +1,480 @@
package org.spongycastle.crypto.test;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.spongycastle.crypto.AsymmetricBlockCipher;
import org.spongycastle.crypto.AsymmetricCipherKeyPair;
import org.spongycastle.crypto.CipherParameters;
import org.spongycastle.crypto.InvalidCipherTextException;
import org.spongycastle.crypto.encodings.OAEPEncoding;
import org.spongycastle.crypto.encodings.PKCS1Encoding;
import org.spongycastle.crypto.engines.RSAEngine;
import org.spongycastle.crypto.generators.RSAKeyPairGenerator;
import org.spongycastle.crypto.params.RSAKeyGenerationParameters;
import org.spongycastle.crypto.params.RSAKeyParameters;
import org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.spongycastle.util.Arrays;
import org.spongycastle.util.encoders.Hex;
import org.spongycastle.util.test.SimpleTest;
public class RSATest
extends SimpleTest
{
static BigInteger mod = new BigInteger("b259d2d6e627a768c94be36164c2d9fc79d97aab9253140e5bf17751197731d6f7540d2509e7b9ffee0a70a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf3c298e05842535d9f064e6b0391cb7d306e0a2d20c4dfb4e7b49a9640bdea26c10ad69c3f05007ce2513cee44cfe01998e62b6c3637d3fc0391079b26ee36d5", 16);
static BigInteger pubExp = new BigInteger("11", 16);
static BigInteger privExp = new BigInteger("92e08f83cc9920746989ca5034dcb384a094fb9c5a6288fcc4304424ab8f56388f72652d8fafc65a4b9020896f2cde297080f2a540e7b7ce5af0b3446e1258d1dd7f245cf54124b4c6e17da21b90a0ebd22605e6f45c9f136d7a13eaac1c0f7487de8bd6d924972408ebb58af71e76fd7b012a8d0e165f3ae2e5077a8648e619", 16);
static BigInteger p = new BigInteger("f75e80839b9b9379f1cf1128f321639757dba514642c206bbbd99f9a4846208b3e93fbbe5e0527cc59b1d4b929d9555853004c7c8b30ee6a213c3d1bb7415d03", 16);
static BigInteger q = new BigInteger("b892d9ebdbfc37e397256dd8a5d3123534d1f03726284743ddc6be3a709edb696fc40c7d902ed804c6eee730eee3d5b20bf6bd8d87a296813c87d3b3cc9d7947", 16);
static BigInteger pExp = new BigInteger("1d1a2d3ca8e52068b3094d501c9a842fec37f54db16e9a67070a8b3f53cc03d4257ad252a1a640eadd603724d7bf3737914b544ae332eedf4f34436cac25ceb5", 16);
static BigInteger qExp = new BigInteger("6c929e4e81672fef49d9c825163fec97c4b7ba7acb26c0824638ac22605d7201c94625770984f78a56e6e25904fe7db407099cad9b14588841b94f5ab498dded", 16);
static BigInteger crtCoef = new BigInteger("dae7651ee69ad1d081ec5e7188ae126f6004ff39556bde90e0b870962fa7b926d070686d8244fe5a9aa709a95686a104614834b0ada4b10f53197a5cb4c97339", 16);
static String input = "4e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e";
//
// to check that we handling byte extension by big number correctly.
//
static String edgeInput = "ff6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e";
static byte[] oversizedSig = Hex.decode("01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff004e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e");
static byte[] dudBlock = Hex.decode("000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff004e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e");
static byte[] truncatedDataBlock = Hex.decode("0001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff004e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e");
static byte[] incorrectPadding = Hex.decode("0001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4e6f77206973207468652074696d6520666f7220616c6c20676f6f64206d656e");
static byte[] missingDataBlock = Hex.decode("0001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
public String getName()
{
return "RSA";
}
private void testStrictPKCS1Length(RSAKeyParameters pubParameters, RSAKeyParameters privParameters)
{
AsymmetricBlockCipher eng = new RSAEngine();
eng.init(true, privParameters);
byte[] data = null;
try
{
data = eng.processBlock(oversizedSig, 0, oversizedSig.length);
}
catch (Exception e)
{
fail("RSA: failed - exception " + e.toString(), e);
}
eng = new PKCS1Encoding(eng);
eng.init(false, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
fail("oversized signature block not recognised");
}
catch (InvalidCipherTextException e)
{
if (!e.getMessage().equals("block incorrect size"))
{
fail("RSA: failed - exception " + e.toString(), e);
}
}
}
private void testTruncatedPKCS1Block(RSAKeyParameters pubParameters, RSAKeyParameters privParameters)
{
checkForPKCS1Exception(pubParameters, privParameters, truncatedDataBlock, "block truncated");
}
private void testDudPKCS1Block(RSAKeyParameters pubParameters, RSAKeyParameters privParameters)
{
checkForPKCS1Exception(pubParameters, privParameters, dudBlock, "unknown block type");
}
private void testWrongPaddingPKCS1Block(RSAKeyParameters pubParameters, RSAKeyParameters privParameters)
{
checkForPKCS1Exception(pubParameters, privParameters, incorrectPadding, "block padding incorrect");
}
private void testMissingDataPKCS1Block(RSAKeyParameters pubParameters, RSAKeyParameters privParameters)
{
checkForPKCS1Exception(pubParameters, privParameters, missingDataBlock, "no data in block");
}
private void checkForPKCS1Exception(RSAKeyParameters pubParameters, RSAKeyParameters privParameters, byte[] inputData, String expectedMessage)
{
AsymmetricBlockCipher eng = new RSAEngine();
eng.init(true, privParameters);
byte[] data = null;
try
{
data = eng.processBlock(inputData, 0, inputData.length);
}
catch (Exception e)
{
fail("RSA: failed - exception " + e.toString(), e);
}
eng = new PKCS1Encoding(eng);
eng.init(false, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
fail("missing data block not recognised");
}
catch (InvalidCipherTextException e)
{
if (!e.getMessage().equals(expectedMessage))
{
fail("RSA: failed - exception " + e.toString(), e);
}
}
}
private void testOAEP(RSAKeyParameters pubParameters, RSAKeyParameters privParameters)
{
//
// OAEP - public encrypt, private decrypt
//
AsymmetricBlockCipher eng = new OAEPEncoding(new RSAEngine());
byte[] data = Hex.decode(input);
eng.init(true, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!input.equals(new String(Hex.encode(data))))
{
fail("failed OAEP Test");
}
}
private void zeroBlockTest(CipherParameters encParameters, CipherParameters decParameters)
{
AsymmetricBlockCipher eng = new PKCS1Encoding(new RSAEngine());
eng.init(true, encParameters);
if (eng.getOutputBlockSize() != ((PKCS1Encoding)eng).getUnderlyingCipher().getOutputBlockSize())
{
fail("PKCS1 output block size incorrect");
}
byte[] zero = new byte[0];
byte[] data = null;
try
{
data = eng.processBlock(zero, 0, zero.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
eng.init(false, decParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!Arrays.areEqual(zero, data))
{
fail("failed PKCS1 zero Test");
}
}
public void performTest()
{
RSAKeyParameters pubParameters = new RSAKeyParameters(false, mod, pubExp);
RSAKeyParameters privParameters = new RSAPrivateCrtKeyParameters(mod, pubExp, privExp, p, q, pExp, qExp, crtCoef);
byte[] data = Hex.decode(edgeInput);
//
// RAW
//
AsymmetricBlockCipher eng = new RSAEngine();
eng.init(true, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("RSA: failed - exception " + e.toString(), e);
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!edgeInput.equals(new String(Hex.encode(data))))
{
fail("failed RAW edge Test");
}
data = Hex.decode(input);
eng.init(true, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!input.equals(new String(Hex.encode(data))))
{
fail("failed RAW Test");
}
//
// PKCS1 - public encrypt, private decrypt
//
eng = new PKCS1Encoding(eng);
eng.init(true, pubParameters);
if (eng.getOutputBlockSize() != ((PKCS1Encoding)eng).getUnderlyingCipher().getOutputBlockSize())
{
fail("PKCS1 output block size incorrect");
}
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
eng.init(false, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!input.equals(new String(Hex.encode(data))))
{
fail("failed PKCS1 public/private Test");
}
//
// PKCS1 - private encrypt, public decrypt
//
eng = new PKCS1Encoding(((PKCS1Encoding)eng).getUnderlyingCipher());
eng.init(true, privParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
eng.init(false, pubParameters);
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!input.equals(new String(Hex.encode(data))))
{
fail("failed PKCS1 private/public Test");
}
zeroBlockTest(pubParameters, privParameters);
zeroBlockTest(privParameters, pubParameters);
//
// key generation test
//
RSAKeyPairGenerator pGen = new RSAKeyPairGenerator();
RSAKeyGenerationParameters genParam = new RSAKeyGenerationParameters(
BigInteger.valueOf(0x11), new SecureRandom(), 768, 25);
pGen.init(genParam);
AsymmetricCipherKeyPair pair = pGen.generateKeyPair();
eng = new RSAEngine();
if (((RSAKeyParameters)pair.getPublic()).getModulus().bitLength() < 768)
{
fail("failed key generation (768) length test");
}
eng.init(true, pair.getPublic());
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
eng.init(false, pair.getPrivate());
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!input.equals(new String(Hex.encode(data))))
{
fail("failed key generation (768) Test");
}
genParam = new RSAKeyGenerationParameters(BigInteger.valueOf(0x11), new SecureRandom(), 1024, 25);
pGen.init(genParam);
pair = pGen.generateKeyPair();
eng.init(true, pair.getPublic());
if (((RSAKeyParameters)pair.getPublic()).getModulus().bitLength() < 1024)
{
fail("failed key generation (1024) length test");
}
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
eng.init(false, pair.getPrivate());
try
{
data = eng.processBlock(data, 0, data.length);
}
catch (Exception e)
{
fail("failed - exception " + e.toString(), e);
}
if (!input.equals(new String(Hex.encode(data))))
{
fail("failed key generation (1024) test");
}
genParam = new RSAKeyGenerationParameters(
BigInteger.valueOf(0x11), new SecureRandom(), 16, 25);
pGen.init(genParam);
for (int i = 0; i < 100; ++i)
{
pair = pGen.generateKeyPair();
RSAPrivateCrtKeyParameters privKey = (RSAPrivateCrtKeyParameters) pair.getPrivate();
BigInteger pqDiff = privKey.getP().subtract(privKey.getQ()).abs();
if (pqDiff.bitLength() < 5)
{
fail("P and Q too close in RSA key pair");
}
}
testOAEP(pubParameters, privParameters);
testStrictPKCS1Length(pubParameters, privParameters);
testDudPKCS1Block(pubParameters, privParameters);
testMissingDataPKCS1Block(pubParameters, privParameters);
testTruncatedPKCS1Block(pubParameters, privParameters);
testWrongPaddingPKCS1Block(pubParameters, privParameters);
try
{
new RSAEngine().processBlock(new byte[]{ 1 }, 0, 1);
fail("failed initialisation check");
}
catch (IllegalStateException e)
{
// expected
}
}
public static void main(
String[] args)
{
runTest(new RSATest());
}
}

View File

@@ -0,0 +1,68 @@
package org.spongycastle.crypto.test;
import org.spongycastle.util.test.Test;
import org.spongycastle.util.test.TestResult;
public final class RegressionTest
{
public static Test[] tests = {
new AESTest(),
new DESTest(),
new DESedeTest(),
new ModeTest(),
new DHTest(),
new ElGamalTest(),
new DSATest(),
new ECTest(),
new ECIESTest(),
new MacTest(),
new RC2Test(),
new RC4Test(),
new RC5Test(),
new RC6Test(),
new RijndaelTest(),
new SerpentTest(),
new SkipjackTest(),
new BlowfishTest(),
new TwofishTest(),
new CAST5Test(),
new CAST6Test(),
new IDEATest(),
new CamelliaTest(),
new RSATest(),
new ISO9796Test(),
new MD2DigestTest(),
new MD4DigestTest(),
new MD5DigestTest(),
new SHA1DigestTest(),
new SHA256DigestTest(),
new SHA384DigestTest(),
new SHA512DigestTest(),
new RIPEMD128DigestTest(),
new RIPEMD160DigestTest(),
new TigerDigestTest(),
new MD5HMacTest(),
new SHA1HMacTest(),
new RIPEMD128HMacTest(),
new RIPEMD160HMacTest(),
new OAEPTest(),
new PSSTest(),
new CTSTest(),
new PKCS5Test(),
new PKCS12Test(),
new GOST28147Test(),
new GOST3410Test(),
new WhirlpoolDigestTest()
};
public static void main(
String[] args)
{
for (int i = 0; i != tests.length; i++)
{
TestResult result = tests[i].perform();
System.out.println(result);
}
}
}