Example usage for org.bouncycastle.crypto.agreement.jpake JPAKERound3Payload JPAKERound3Payload

List of usage examples for org.bouncycastle.crypto.agreement.jpake JPAKERound3Payload JPAKERound3Payload

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.agreement.jpake JPAKERound3Payload JPAKERound3Payload.

Prototype

public JPAKERound3Payload(String participantId, BigInteger magTag) 

Source Link

Usage

From source file:com.distrimind.util.crypto.P2PJPAKESecretMessageExchanger.java

License:Open Source License

@Override
protected void receiveData(int stepNumber, byte[] dataReceived) throws CryptoException {
    valid = false;//from w w  w  .j  a  v  a  2s .c  om
    switch (stepNumber) {
    case 0:
        try (ByteArrayInputStream bais = new ByteArrayInputStream(dataReceived)) {
            try (DataInputStream ois = new DataInputStream(bais)) {
                JPAKERound1Payload r1;
                try {
                    short s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    byte[] tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger gx1 = new BigInteger(tab);

                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger gx2 = new BigInteger(tab);

                    int size = ois.readInt();
                    BigInteger[] knowledgeProofForX1 = null;
                    if (size > 0) {
                        if (size > 100)
                            throw new CryptoException("illegal argument exception");
                        knowledgeProofForX1 = new BigInteger[size];
                        for (int i = 0; i < size; i++) {
                            s = ois.readShort();
                            if (s <= 0)
                                throw new IOException();
                            tab = new byte[s];
                            ois.readFully(tab);
                            knowledgeProofForX1[i] = new BigInteger(tab);
                        }
                    }
                    size = ois.readInt();
                    BigInteger[] knowledgeProofForX2 = null;
                    if (size > 0) {
                        if (size > 100)
                            throw new CryptoException("illegal argument exception");
                        knowledgeProofForX2 = new BigInteger[size];
                        for (int i = 0; i < size; i++) {
                            s = ois.readShort();
                            if (s <= 0)
                                throw new IOException();
                            tab = new byte[s];
                            ois.readFully(tab);
                            knowledgeProofForX2[i] = new BigInteger(tab);
                        }
                    }
                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    String pid = new String(tab, Charset.forName("utf-8"));
                    if (knowledgeProofForX1 == null)
                        throw new IOException();
                    if (knowledgeProofForX2 == null)
                        throw new IOException();
                    r1 = new JPAKERound1Payload(pid, gx1, gx2, knowledgeProofForX1, knowledgeProofForX2);
                } catch (Exception e) {
                    valid = false;
                    throw new CryptoException("data received is not a valid instance of JPAKERound1Payload", e);
                }
                jpake.validateRound1PayloadReceived(r1);
            }
        } catch (Exception e) {
            valid = false;
            if (e instanceof CryptoException)
                throw (CryptoException) e;
            else
                throw new CryptoException("", e);
        }
        break;
    case 1:
        try (ByteArrayInputStream bais = new ByteArrayInputStream(dataReceived)) {
            try (DataInputStream ois = new DataInputStream(bais)) {
                JPAKERound2Payload r2;
                try {
                    short s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    byte[] tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger A = new BigInteger(tab);

                    int size = ois.readInt();
                    BigInteger[] knowledgeProofForX2s = null;
                    if (size > 0) {
                        if (size > 100)
                            throw new CryptoException("illegal argument exception");
                        knowledgeProofForX2s = new BigInteger[size];
                        for (int i = 0; i < size; i++) {
                            s = ois.readShort();
                            if (s <= 0)
                                throw new IOException();
                            tab = new byte[s];
                            ois.readFully(tab);
                            knowledgeProofForX2s[i] = new BigInteger(tab);
                        }
                    }
                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    String pid = new String(tab, Charset.forName("utf-8"));

                    if (knowledgeProofForX2s == null)
                        throw new IOException();

                    r2 = new JPAKERound2Payload(pid, A, knowledgeProofForX2s);
                } catch (Exception e) {
                    valid = false;
                    throw new CryptoException("data received is not a valid instance of JPAKERound2Payload", e);
                }
                jpake.validateRound2PayloadReceived(r2);
            }
        } catch (Exception e) {
            valid = false;
            if (e instanceof CryptoException)
                throw (CryptoException) e;
            else
                throw new CryptoException("", e);
        }
        break;
    case 2:
        try (ByteArrayInputStream bais = new ByteArrayInputStream(dataReceived)) {
            try (DataInputStream ois = new DataInputStream(bais)) {
                JPAKERound3Payload r3;
                try {
                    short s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    byte[] tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger magTag = new BigInteger(tab);

                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    String pid = new String(tab, Charset.forName("utf-8"));

                    r3 = new JPAKERound3Payload(pid, magTag);
                } catch (Exception e) {
                    valid = false;
                    throw new CryptoException("data received is not a valid instance of JPAKERound2Payload", e);
                }

                jpake.validateRound3PayloadReceived(r3, keyMaterial);
            }
        } catch (Exception e) {
            valid = false;
            if (e instanceof CryptoException)
                throw (CryptoException) e;
            else
                throw new CryptoException("", e);
        }
        break;
    default:
        throw new IllegalAccessError();

    }
    valid = true;
}