Package org.bouncycastle.util.test

Examples of org.bouncycastle.util.test.SimpleTestResult


        if (!result.isSuccessful())
        {
            return result;
        }
   
        return new SimpleTestResult(true, "OAEP: Okay");
    }
View Full Code Here


            {
                pubStruct = new RSAPublicKeyStructure((ASN1Sequence)new SubjectPublicKeyInfo((ASN1Sequence)dIn.readObject()).getPublicKey());
            }
            catch (Exception e)
            {
                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
            }
   
            bIn = new ByteArrayInputStream(privKeyEnc);
            dIn = new ASN1InputStream(bIn);
   
            //
            // extract the private key info.
            //
            RSAPrivateKeyStructure privStruct;
   
            try
            {
                privStruct = new RSAPrivateKeyStructure((ASN1Sequence)(new PrivateKeyInfo((ASN1Sequence)dIn.readObject()).getPrivateKey()));
            }
            catch (Exception e)
            {
                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
            }
   
            RSAKeyParameters    pubParameters = new RSAKeyParameters(
                                                        false,
                                                        pubStruct.getModulus(),
                                                        pubStruct.getPublicExponent());
   
            RSAKeyParameters    privParameters = new RSAPrivateCrtKeyParameters(
                                                        privStruct.getModulus(),
                                                        privStruct.getPublicExponent(),
                                                        privStruct.getPrivateExponent(),
                                                        privStruct.getPrime1(),
                                                        privStruct.getPrime2(),
                                                        privStruct.getExponent1(),
                                                        privStruct.getExponent2(),
                                                        privStruct.getCoefficient());
   
            AsymmetricBlockCipher   cipher = new OAEPEncoding(new RSAEngine());
   
            cipher.init(true, new ParametersWithRandom(pubParameters, new Rand()));
   
            byte[]  out;
            byte[]  input = new byte[]
                { (byte)0x54, (byte)0x85, (byte)0x9b, (byte)0x34, (byte)0x2c, (byte)0x49, (byte)0xea, (byte)0x2a };
   
            try
            {
                out = cipher.processBlock(input, 0, input.length);
            }
            catch (Exception e)
            {
                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
            }
   
   
            for (int i = 0; i != output.length; i++)
            {
                if (out[i] != output[i])
                {
                    return new SimpleTestResult(false, getName() + ": failed encryption");
                }
            }
   
            cipher.init(false, privParameters);
            try
            {
                out = cipher.processBlock(output, 0, output.length);
            }
            catch (Exception e)
            {
                return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
            }
   
            for (int i = 0; i != input.length; i++)
            {
                if (out[i] != input[i])
                {
                    return new SimpleTestResult(false, getName() + ": failed decoding");
                }
            }
   
            return new SimpleTestResult(true, getName() + ": Okay");
        }
View Full Code Here

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec1.equals(resStr))
        {
            return new SimpleTestResult(false,
                "MD2 failing standard vector test 1"
                + System.getProperty("line.separator")
                + "    expected: " + resVec1
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        //
        // test 2
        //
        bytes = Hex.decode(testVec2);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec2.equals(resStr))
        {
            return new SimpleTestResult(false,
                "MD2 failing standard vector test 2"
                + System.getProperty("line.separator")
                + "    expected: " + resVec2
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        //
        // test 3
        //
        bytes = Hex.decode(testVec3);
        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec3.equals(resStr))
        {
            return new SimpleTestResult(false,
                "MD2 failing standard vector test 3"
                + System.getProperty("line.separator")
                + "    expected: " + resVec3
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        //
        // test 4
        //

        bytes = Hex.decode(testVec4);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec4.equals(resStr))
        {
            return new SimpleTestResult(false,
                "MD2 failing standard vector test 4"
                + System.getProperty("line.separator")
                + "    expected: " + resVec4
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }
        //
        // test 5
        //
        bytes = Hex.decode(testVec5);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec5.equals(resStr))
        {
            return new SimpleTestResult(false,
          //System.err.println(
                "MD2 failing standard vector test 5"
                + System.getProperty("line.separator")
                + "    expected: " + resVec5
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }
        //
        // test 6
        //
        bytes = Hex.decode(testVec6);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec6.equals(resStr))
        {
            return new SimpleTestResult(false,
                "MD2 failing standard vector test 6"
                + System.getProperty("line.separator")
                + "    expected: " + resVec6
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }
        //
        // test 7
        //
        bytes = Hex.decode(testVec7);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec7.equals(resStr))
        {
            return new SimpleTestResult(false,
                "MD2 failing standard vector test 7"
                + System.getProperty("line.separator")
                + "    expected: " + resVec7
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }
View Full Code Here

            byte[]                    bytes = bOut.toByteArray();

            if (bytes.length != req.length)
            {
                return new SimpleTestResult(false, getName() + ": " + testName + " failed length test");
            }

            for (int i = 0; i != req.length; i++)
            {
                if (bytes[i] != req[i])
                {
                    return new SimpleTestResult(false, getName() + ": " + testName + " failed comparison test");
                }
            }
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, getName() + ": Exception - " + testName + " " + e.toString());
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }
View Full Code Here

            BigInteger   k1 = e1.calculateAgreement(p2.getPublic());
            BigInteger   k2 = e2.calculateAgreement(p1.getPublic());

            if (!k1.equals(k2))
            {
                return new SimpleTestResult(false, this.getName() + ": calculated agreement test failed");
            }

            //
            // two way
            //
            e1 = new ECDHCBasicAgreement();
            e2 = new ECDHCBasicAgreement();

            e1.init(p1.getPrivate());
            e2.init(p2.getPrivate());

            k1 = e1.calculateAgreement(p2.getPublic());
            k2 = e2.calculateAgreement(p1.getPublic());

            if (!k1.equals(k2))
            {
                return new SimpleTestResult(false, this.getName() + ": calculated agreement test failed");
            }

            return new SimpleTestResult(true, this.getName() + ": Okay");
        }
View Full Code Here

            digest.update(m, 0, m.length);
            digest.doFinal(resBuf, 0);

            if (!arraysEqual(resBuf, Hex.decode(digests[i])))
            {
                return new SimpleTestResult(false, getName() + ": Vector " + i + " failed");
            }
        }

        //
        // test 2
        //
        byte[] m = messages[messages.length-1].getBytes();

        digest.update(m, 0, m.length/2);

        // clone the Digest
        Digest d = new RIPEMD320Digest((RIPEMD320Digest)digest);

        digest.update(m, m.length/2, m.length - m.length/2);
        digest.doFinal(resBuf, 0);

        if (!arraysEqual(resBuf, Hex.decode(digests[digests.length-1])))
        {
            return new SimpleTestResult(false,
                "RIPEMD320 failing clone test"
                + System.getProperty("line.separator")
                + "    expected: " + digests[digests.length-1]
                + System.getProperty("line.separator")
                + "    got     : " + new String(Hex.encode(resBuf)));
        }

        d.update(m, m.length/2, m.length - m.length/2);
        d.doFinal(resBuf, 0);

        if (!arraysEqual(resBuf, Hex.decode(digests[digests.length-1])))
        {
            return new SimpleTestResult(false,
                "RIPEMD320 failing clone test - part 2"
                + System.getProperty("line.separator")
                + "    expected: " +  digests[digests.length-1]
                + System.getProperty("line.separator")
                + "    got     : " + new String(Hex.encode(resBuf)));
        }

        for (int i = 0; i < 1000000; i++)
        {
            digest.update((byte)'a');
        }
        digest.doFinal(resBuf, 0);

        if (!arraysEqual(resBuf, Hex.decode(million_a_digest)))
        {
            return new SimpleTestResult(false, getName() + ": Million a's failed");
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }
View Full Code Here

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec1.equals(resStr))
        {
            return new SimpleTestResult(false,
                "SHA-384 failing standard vector test 1"
                + System.getProperty("line.separator")
                + "    expected: " + resVec1
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        //
        // test 2
        //
        byte[]  bytes = Hex.decode(testVec2);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec2.equals(resStr))
        {
            return new SimpleTestResult(false,
                "SHA-384 failing standard vector test 2"
                + System.getProperty("line.separator")
                + "    expected: " + resVec2
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        //
        // test 3
        //
        bytes = Hex.decode(testVec3);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec3.equals(resStr))
        {
            return new SimpleTestResult(false,
                "SHA-384 failing standard vector test 3"
                + System.getProperty("line.separator")
                + "    expected: " + resVec3
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        //
        // test 4
        //
        bytes = Hex.decode(testVec4);

        digest.update(bytes, 0, bytes.length);

        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec4.equals(resStr))
        {
            return new SimpleTestResult(false,
                "SHA-384 failing standard vector test 4"
                + System.getProperty("line.separator")
                + "    expected: " + resVec4
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        //
        // test 5
        //
        bytes = Hex.decode(testVec4);

        digest.update(bytes, 0, bytes.length/2);

        // clone the Digest
        Digest d = new SHA384Digest((SHA384Digest)digest);

        digest.update(bytes, bytes.length/2, bytes.length - bytes.length/2);
        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec4.equals(resStr))
        {
            return new SimpleTestResult(false,
                "SHA384 failing standard vector test 5"
                + System.getProperty("line.separator")
                + "    expected: " + resVec4
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        d.update(bytes, bytes.length/2, bytes.length - bytes.length/2);
        d.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec4.equals(resStr))
        {
            return new SimpleTestResult(false,
                "SHA384 failing standard vector test 5"
                + System.getProperty("line.separator")
                + "    expected: " + resVec4
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        // test 6
        bytes = Hex.decode(testVec5);
        for ( int i = 0; i < 100000; i++ )
        {
            digest.update(bytes, 0, bytes.length);
        }
        digest.doFinal(resBuf, 0);

        resStr = new String(Hex.encode(resBuf));
        if (!resVec5.equals(resStr))
        {
            return new SimpleTestResult(false,
                "SHA-384 failing standard vector test 5"
                + System.getProperty("line.separator")
                + "    expected: " + resVec5
                + System.getProperty("line.separator")
                + "    got     : " + resStr);
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }
View Full Code Here

        X509Name    name1 = new X509Name(attrs);

        if (!name1.equals(name1))
        {
            return new SimpleTestResult(false, getName() + ": Failed same object test");
        }

        X509Name    name2 = new X509Name(attrs);

        if (!name1.equals(name2))
        {
            return new SimpleTestResult(false, getName() + ": Failed same name test");
        }

        Vector  ord1 = new Vector();

        ord1.addElement(X509Name.C);
        ord1.addElement(X509Name.O);
        ord1.addElement(X509Name.L);
        ord1.addElement(X509Name.ST);
        ord1.addElement(X509Name.E);

        Vector  ord2 = new Vector();

        ord2.addElement(X509Name.E);
        ord2.addElement(X509Name.ST);
        ord2.addElement(X509Name.L);
        ord2.addElement(X509Name.O);
        ord2.addElement(X509Name.C);

        name1 = new X509Name(ord1, attrs);
        name2 = new X509Name(ord2, attrs);

        if (!name1.equals(name2))
        {
            return new SimpleTestResult(false, getName() + ": Failed reverse name test");
        }

        ord2 = new Vector();

        ord2.addElement(X509Name.ST);
        ord2.addElement(X509Name.ST);
        ord2.addElement(X509Name.L);
        ord2.addElement(X509Name.O);
        ord2.addElement(X509Name.C);

        name1 = new X509Name(ord1, attrs);
        name2 = new X509Name(ord2, attrs);

        if (name1.equals(name2))
        {
            return new SimpleTestResult(false, getName() + ": Failed different name test");
        }

        ord2 = new Vector();

        ord2.addElement(X509Name.ST);
        ord2.addElement(X509Name.L);
        ord2.addElement(X509Name.O);
        ord2.addElement(X509Name.C);

        name1 = new X509Name(ord1, attrs);
        name2 = new X509Name(ord2, attrs);

        if (name1.equals(name2))
        {
            return new SimpleTestResult(false, getName() + ": Failed subset name test");
        }
       
        //
        // composite test
        //
        try
        {
            byte[]  enc = Hex.decode("305e310b300906035504061302415531283026060355040a131f546865204c6567696f6e206f662074686520426f756e637920436173746c6531253010060355040713094d656c626f75726e653011060355040b130a4173636f742056616c65");
           
            ASN1InputStream aIn = new ASN1InputStream(new ByteArrayInputStream(enc));
           
            X509Name    n = X509Name.getInstance(aIn.readObject());
           
            if (!n.toString().equals("C=AU,O=The Legion of the Bouncy Castle,L=Melbourne+OU=Ascot Vale"))
            {
                return new SimpleTestResult(false, getName() + ": Failed composite to string test");
            }
           
            n = new X509Name("C=AU, O=The Legion of the Bouncy Castle, L=Melbourne + OU=Ascot Vale");
           
            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
            ASN1OutputStream        aOut = new ASN1OutputStream(bOut);
           
            aOut.writeObject(n);
           
            byte[]  enc2 = bOut.toByteArray();
           
            if (enc.length != enc2.length)
            {
                return new SimpleTestResult(false, getName() + ": Failed composite string to encoding test");
            }
           
            for (int i = 0; i != enc.length; i++)
            {
                if (enc[i] != enc2[i])
                {
                    return new SimpleTestResult(false, getName() + ": Failed composite string to encoding test");
                }
            }

            //
            // general subjects test
            //
            for (int i = 0; i != subjects.length; i++)
            {
                X509Name    name = new X509Name(subjects[i]);

                bOut = new ByteArrayOutputStream();
                aOut = new ASN1OutputStream(bOut);
           
                aOut.writeObject(name);

                aIn = new ASN1InputStream(new ByteArrayInputStream(bOut.toByteArray()));

                name = X509Name.getInstance(aIn.readObject());

                if (!name.toString().equals(subjects[i]))
                {
                    return new SimpleTestResult(false, getName() + ": failed regeneration test " + i);
                }
            }

            //
            // sort test
            //
            X509Name unsorted = new X509Name("SN=BBB + CN=AA");

            if (!fromBytes(unsorted.getEncoded()).toString().equals("CN=AA+SN=BBB"))
            {
                return new SimpleTestResult(false, getName() + ": failed sort test 1");
            }

            unsorted = new X509Name("CN=AA + SN=BBB");

            if (!fromBytes(unsorted.getEncoded()).toString().equals("CN=AA+SN=BBB"))
            {
                return new SimpleTestResult(false, getName() + ": failed sort test 2");
            }

            unsorted = new X509Name("SN=B + CN=AA");

            if (!fromBytes(unsorted.getEncoded()).toString().equals("SN=B+CN=AA"))
            {
                return new SimpleTestResult(false, getName() + ": failed sort test 3");
            }
           
            unsorted = new X509Name("CN=AA + SN=B");

            if (!fromBytes(unsorted.getEncoded()).toString().equals("SN=B+CN=AA"))
            {
                return new SimpleTestResult(false, getName() + ": failed sort test 4");
            }
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, getName() + ": exception " + e.getMessage(), e);
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }
View Full Code Here

        {
            data = eng.processBlock(msg1, 0, msg1.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "ISO9796: failed - exception " + e.toString());
        }

        eng.init(false, pubParameters);

        if (!isSameAs(sig1, 0, data))
        {
            return new SimpleTestResult(false, "ISO9796: failed ISO9796-1 generation Test 1");
        }

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "ISO9796: failed - exception " + e.toString());
        }

        if (!isSameAs(msg1, 0, data))
        {
            return new SimpleTestResult(false, "ISO9796: failed ISO9796-1 retrieve Test 1");
        }

        return new SimpleTestResult(true, "ISO9796: Okay");
    }
View Full Code Here

        {
            data = eng.processBlock(msg2, 0, msg2.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "ISO9796: failed - exception " + e.toString());
        }

        eng.init(false, pubParameters);

        if (!isSameAs(data, 1, sig2))
        {
            return new SimpleTestResult(false, "ISO9796: failed ISO9796-1 generation Test 2");
        }

        try
        {
            data = eng.processBlock(data, 0, data.length);
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, "ISO9796: failed - exception " + e.toString());
        }

        if (!isSameAs(msg2, 0, data))
        {
            return new SimpleTestResult(false, "ISO9796: failed ISO9796-1 retrieve Test 2");
        }

        return new SimpleTestResult(true, "ISO9796: Okay");
    }
View Full Code Here

TOP

Related Classes of org.bouncycastle.util.test.SimpleTestResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.