what-the-bank/sources/org/bouncycastle/crypto/examples/JPAKEExample.java

197 lines
11 KiB
Java

package org.bouncycastle.crypto.examples;
import com.airbnb.deeplinkdispatch.UrlTreeKt;
import java.io.PrintStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.agreement.jpake.JPAKEParticipant;
import org.bouncycastle.crypto.agreement.jpake.JPAKEPrimeOrderGroup;
import org.bouncycastle.crypto.agreement.jpake.JPAKEPrimeOrderGroups;
import org.bouncycastle.crypto.agreement.jpake.JPAKERound1Payload;
import org.bouncycastle.crypto.agreement.jpake.JPAKERound2Payload;
import org.bouncycastle.crypto.agreement.jpake.JPAKERound3Payload;
import org.bouncycastle.crypto.digests.SHA256Digest;
/* loaded from: classes6.dex */
public class JPAKEExample {
public static void main(String[] strArr) throws CryptoException {
JPAKEPrimeOrderGroup jPAKEPrimeOrderGroup = JPAKEPrimeOrderGroups.NIST_3072;
BigInteger p = jPAKEPrimeOrderGroup.getP();
BigInteger q = jPAKEPrimeOrderGroup.getQ();
BigInteger g = jPAKEPrimeOrderGroup.getG();
System.out.println("********* Initialization **********");
System.out.println("Public parameters for the cyclic group:");
PrintStream printStream = System.out;
StringBuilder sb = new StringBuilder("p (");
sb.append(p.bitLength());
sb.append(" bits): ");
sb.append(p.toString(16));
printStream.println(sb.toString());
PrintStream printStream2 = System.out;
StringBuilder sb2 = new StringBuilder("q (");
sb2.append(q.bitLength());
sb2.append(" bits): ");
sb2.append(q.toString(16));
printStream2.println(sb2.toString());
PrintStream printStream3 = System.out;
StringBuilder sb3 = new StringBuilder("g (");
sb3.append(p.bitLength());
sb3.append(" bits): ");
sb3.append(g.toString(16));
printStream3.println(sb3.toString());
PrintStream printStream4 = System.out;
StringBuilder sb4 = new StringBuilder("p mod q = ");
sb4.append(p.mod(q).toString(16));
printStream4.println(sb4.toString());
PrintStream printStream5 = System.out;
StringBuilder sb5 = new StringBuilder("g^{q} mod p = ");
sb5.append(g.modPow(q, p).toString(16));
printStream5.println(sb5.toString());
System.out.println("");
System.out.println("(Secret passwords used by Alice and Bob: \"password\" and \"password\")\n");
SHA256Digest sHA256Digest = new SHA256Digest();
SecureRandom secureRandom = new SecureRandom();
JPAKEParticipant jPAKEParticipant = new JPAKEParticipant("alice", "password".toCharArray(), jPAKEPrimeOrderGroup, sHA256Digest, secureRandom);
JPAKEParticipant jPAKEParticipant2 = new JPAKEParticipant("bob", "password".toCharArray(), jPAKEPrimeOrderGroup, sHA256Digest, secureRandom);
JPAKERound1Payload createRound1PayloadToSend = jPAKEParticipant.createRound1PayloadToSend();
JPAKERound1Payload createRound1PayloadToSend2 = jPAKEParticipant2.createRound1PayloadToSend();
System.out.println("************ Round 1 **************");
System.out.println("Alice sends to Bob: ");
PrintStream printStream6 = System.out;
StringBuilder sb6 = new StringBuilder("g^{x1}=");
sb6.append(createRound1PayloadToSend.getGx1().toString(16));
printStream6.println(sb6.toString());
PrintStream printStream7 = System.out;
StringBuilder sb7 = new StringBuilder("g^{x2}=");
sb7.append(createRound1PayloadToSend.getGx2().toString(16));
printStream7.println(sb7.toString());
PrintStream printStream8 = System.out;
StringBuilder sb8 = new StringBuilder("KP{x1}={");
sb8.append(createRound1PayloadToSend.getKnowledgeProofForX1()[0].toString(16));
sb8.append("};{");
sb8.append(createRound1PayloadToSend.getKnowledgeProofForX1()[1].toString(16));
sb8.append(UrlTreeKt.componentParamSuffix);
printStream8.println(sb8.toString());
PrintStream printStream9 = System.out;
StringBuilder sb9 = new StringBuilder("KP{x2}={");
sb9.append(createRound1PayloadToSend.getKnowledgeProofForX2()[0].toString(16));
sb9.append("};{");
sb9.append(createRound1PayloadToSend.getKnowledgeProofForX2()[1].toString(16));
sb9.append(UrlTreeKt.componentParamSuffix);
printStream9.println(sb9.toString());
System.out.println("");
System.out.println("Bob sends to Alice: ");
PrintStream printStream10 = System.out;
StringBuilder sb10 = new StringBuilder("g^{x3}=");
sb10.append(createRound1PayloadToSend2.getGx1().toString(16));
printStream10.println(sb10.toString());
PrintStream printStream11 = System.out;
StringBuilder sb11 = new StringBuilder("g^{x4}=");
sb11.append(createRound1PayloadToSend2.getGx2().toString(16));
printStream11.println(sb11.toString());
PrintStream printStream12 = System.out;
StringBuilder sb12 = new StringBuilder("KP{x3}={");
sb12.append(createRound1PayloadToSend2.getKnowledgeProofForX1()[0].toString(16));
sb12.append("};{");
sb12.append(createRound1PayloadToSend2.getKnowledgeProofForX1()[1].toString(16));
sb12.append(UrlTreeKt.componentParamSuffix);
printStream12.println(sb12.toString());
PrintStream printStream13 = System.out;
StringBuilder sb13 = new StringBuilder("KP{x4}={");
sb13.append(createRound1PayloadToSend2.getKnowledgeProofForX2()[0].toString(16));
sb13.append("};{");
sb13.append(createRound1PayloadToSend2.getKnowledgeProofForX2()[1].toString(16));
sb13.append(UrlTreeKt.componentParamSuffix);
printStream13.println(sb13.toString());
System.out.println("");
jPAKEParticipant.validateRound1PayloadReceived(createRound1PayloadToSend2);
System.out.println("Alice checks g^{x4}!=1: OK");
System.out.println("Alice checks KP{x3}: OK");
System.out.println("Alice checks KP{x4}: OK");
System.out.println("");
jPAKEParticipant2.validateRound1PayloadReceived(createRound1PayloadToSend);
System.out.println("Bob checks g^{x2}!=1: OK");
System.out.println("Bob checks KP{x1},: OK");
System.out.println("Bob checks KP{x2},: OK");
System.out.println("");
JPAKERound2Payload createRound2PayloadToSend = jPAKEParticipant.createRound2PayloadToSend();
JPAKERound2Payload createRound2PayloadToSend2 = jPAKEParticipant2.createRound2PayloadToSend();
System.out.println("************ Round 2 **************");
System.out.println("Alice sends to Bob: ");
PrintStream printStream14 = System.out;
StringBuilder sb14 = new StringBuilder("A=");
sb14.append(createRound2PayloadToSend.getA().toString(16));
printStream14.println(sb14.toString());
PrintStream printStream15 = System.out;
StringBuilder sb15 = new StringBuilder("KP{x2*s}={");
sb15.append(createRound2PayloadToSend.getKnowledgeProofForX2s()[0].toString(16));
sb15.append("},{");
sb15.append(createRound2PayloadToSend.getKnowledgeProofForX2s()[1].toString(16));
sb15.append(UrlTreeKt.componentParamSuffix);
printStream15.println(sb15.toString());
System.out.println("");
System.out.println("Bob sends to Alice");
PrintStream printStream16 = System.out;
StringBuilder sb16 = new StringBuilder("B=");
sb16.append(createRound2PayloadToSend2.getA().toString(16));
printStream16.println(sb16.toString());
PrintStream printStream17 = System.out;
StringBuilder sb17 = new StringBuilder("KP{x4*s}={");
sb17.append(createRound2PayloadToSend2.getKnowledgeProofForX2s()[0].toString(16));
sb17.append("},{");
sb17.append(createRound2PayloadToSend2.getKnowledgeProofForX2s()[1].toString(16));
sb17.append(UrlTreeKt.componentParamSuffix);
printStream17.println(sb17.toString());
System.out.println("");
jPAKEParticipant.validateRound2PayloadReceived(createRound2PayloadToSend2);
System.out.println("Alice checks KP{x4*s}: OK\n");
jPAKEParticipant2.validateRound2PayloadReceived(createRound2PayloadToSend);
System.out.println("Bob checks KP{x2*s}: OK\n");
BigInteger calculateKeyingMaterial = jPAKEParticipant.calculateKeyingMaterial();
BigInteger calculateKeyingMaterial2 = jPAKEParticipant2.calculateKeyingMaterial();
System.out.println("********* After round 2 ***********");
PrintStream printStream18 = System.out;
StringBuilder sb18 = new StringBuilder("Alice computes key material \t K=");
sb18.append(calculateKeyingMaterial.toString(16));
printStream18.println(sb18.toString());
PrintStream printStream19 = System.out;
StringBuilder sb19 = new StringBuilder("Bob computes key material \t K=");
sb19.append(calculateKeyingMaterial2.toString(16));
printStream19.println(sb19.toString());
System.out.println();
deriveSessionKey(calculateKeyingMaterial);
deriveSessionKey(calculateKeyingMaterial2);
JPAKERound3Payload createRound3PayloadToSend = jPAKEParticipant.createRound3PayloadToSend(calculateKeyingMaterial);
JPAKERound3Payload createRound3PayloadToSend2 = jPAKEParticipant2.createRound3PayloadToSend(calculateKeyingMaterial2);
System.out.println("************ Round 3 **************");
System.out.println("Alice sends to Bob: ");
PrintStream printStream20 = System.out;
StringBuilder sb20 = new StringBuilder("MacTag=");
sb20.append(createRound3PayloadToSend.getMacTag().toString(16));
printStream20.println(sb20.toString());
System.out.println("");
System.out.println("Bob sends to Alice: ");
PrintStream printStream21 = System.out;
StringBuilder sb21 = new StringBuilder("MacTag=");
sb21.append(createRound3PayloadToSend2.getMacTag().toString(16));
printStream21.println(sb21.toString());
System.out.println("");
jPAKEParticipant.validateRound3PayloadReceived(createRound3PayloadToSend2, calculateKeyingMaterial);
System.out.println("Alice checks MacTag: OK\n");
jPAKEParticipant2.validateRound3PayloadReceived(createRound3PayloadToSend, calculateKeyingMaterial2);
System.out.println("Bob checks MacTag: OK\n");
System.out.println();
System.out.println("MacTags validated, therefore the keying material matches.");
}
private static BigInteger deriveSessionKey(BigInteger bigInteger) {
SHA256Digest sHA256Digest = new SHA256Digest();
byte[] byteArray = bigInteger.toByteArray();
byte[] bArr = new byte[sHA256Digest.getDigestSize()];
sHA256Digest.update(byteArray, 0, byteArray.length);
sHA256Digest.doFinal(bArr, 0);
return new BigInteger(bArr);
}
}