package org.bouncycastle.crypto.generators; import io.grpc.internal.GrpcUtil; import java.io.PrintStream; import java.math.BigInteger; import java.security.SecureRandom; import java.util.Vector; import okhttp3.internal.http.StatusLine; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator; import org.bouncycastle.crypto.KeyGenerationParameters; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.params.NaccacheSternKeyGenerationParameters; import org.bouncycastle.crypto.params.NaccacheSternKeyParameters; import org.bouncycastle.crypto.params.NaccacheSternPrivateKeyParameters; import org.bouncycastle.crypto.tls.CipherSuite; import org.bouncycastle.math.Primes; import org.jmrtd.cbeff.ISO781611; /* loaded from: classes6.dex */ public class NaccacheSternKeyPairGenerator implements AsymmetricCipherKeyPairGenerator { private NaccacheSternKeyGenerationParameters param; private static int[] smallPrimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, ISO781611.CREATION_DATE_AND_TIME_TAG, CipherSuite.TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA, CipherSuite.TLS_PSK_WITH_3DES_EDE_CBC_SHA, CipherSuite.TLS_RSA_PSK_WITH_AES_256_CBC_SHA, 151, CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384, CipherSuite.TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, CipherSuite.TLS_DH_anon_WITH_AES_256_GCM_SHA384, CipherSuite.TLS_RSA_PSK_WITH_AES_256_GCM_SHA384, CipherSuite.TLS_DHE_PSK_WITH_AES_256_CBC_SHA384, CipherSuite.TLS_DHE_PSK_WITH_NULL_SHA384, CipherSuite.TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256, CipherSuite.TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256, CipherSuite.TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256, 199, Primes.SMALL_FACTOR_LIMIT, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, StatusLine.HTTP_MISDIRECTED_REQUEST, 431, 433, 439, GrpcUtil.DEFAULT_PORT_SSL, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557}; private static final BigInteger ONE = BigInteger.valueOf(1); @Override // org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator public void init(KeyGenerationParameters keyGenerationParameters) { this.param = (NaccacheSternKeyGenerationParameters) keyGenerationParameters; } @Override // org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator public AsymmetricCipherKeyPair generateKeyPair() { long j; BigInteger generatePrime; BigInteger add; BigInteger generatePrime2; BigInteger bigInteger; BigInteger bigInteger2; BigInteger add2; BigInteger bigInteger3; BigInteger bigInteger4; BigInteger bigInteger5; BigInteger bigInteger6; BigInteger bigInteger7; BigInteger bigInteger8; BigInteger bigInteger9; PrintStream printStream; StringBuilder sb; long j2; BigInteger bigInteger10; int i; int strength = this.param.getStrength(); SecureRandom random = this.param.getRandom(); int certainty = this.param.getCertainty(); boolean isDebug = this.param.isDebug(); if (isDebug) { PrintStream printStream2 = System.out; StringBuilder sb2 = new StringBuilder("Fetching first "); sb2.append(this.param.getCntSmallPrimes()); sb2.append(" primes."); printStream2.println(sb2.toString()); } Vector permuteList = permuteList(findFirstPrimes(this.param.getCntSmallPrimes()), random); BigInteger bigInteger11 = ONE; BigInteger bigInteger12 = bigInteger11; for (int i2 = 0; i2 < permuteList.size() / 2; i2++) { bigInteger12 = bigInteger12.multiply((BigInteger) permuteList.elementAt(i2)); } for (int size = permuteList.size() / 2; size < permuteList.size(); size++) { bigInteger11 = bigInteger11.multiply((BigInteger) permuteList.elementAt(size)); } BigInteger multiply = bigInteger12.multiply(bigInteger11); int bitLength = (((strength - multiply.bitLength()) - 48) / 2) + 1; BigInteger generatePrime3 = generatePrime(bitLength, certainty, random); BigInteger generatePrime4 = generatePrime(bitLength, certainty, random); if (isDebug) { System.out.println("generating p and q"); } BigInteger shiftLeft = generatePrime3.multiply(bigInteger12).shiftLeft(1); BigInteger shiftLeft2 = generatePrime4.multiply(bigInteger11).shiftLeft(1); long j3 = 0; while (true) { j = j3 + 1; generatePrime = generatePrime(24, certainty, random); add = generatePrime.multiply(shiftLeft).add(ONE); if (add.isProbablePrime(certainty)) { while (true) { do { generatePrime2 = generatePrime(24, certainty, random); } while (generatePrime.equals(generatePrime2)); BigInteger multiply2 = generatePrime2.multiply(shiftLeft2); bigInteger = shiftLeft2; bigInteger2 = ONE; add2 = multiply2.add(bigInteger2); if (add2.isProbablePrime(certainty)) { break; } shiftLeft2 = bigInteger; } bigInteger3 = shiftLeft; if (!multiply.gcd(generatePrime.multiply(generatePrime2)).equals(bigInteger2)) { continue; } else { if (add.multiply(add2).bitLength() >= strength) { break; } if (isDebug) { PrintStream printStream3 = System.out; StringBuilder sb3 = new StringBuilder("key size too small. Should be "); sb3.append(strength); sb3.append(" but is actually "); sb3.append(add.multiply(add2).bitLength()); printStream3.println(sb3.toString()); } } } else { bigInteger = shiftLeft2; bigInteger3 = shiftLeft; } j3 = j; shiftLeft2 = bigInteger; shiftLeft = bigInteger3; } BigInteger bigInteger13 = generatePrime4; if (isDebug) { PrintStream printStream4 = System.out; bigInteger4 = generatePrime3; StringBuilder sb4 = new StringBuilder("needed "); sb4.append(j); sb4.append(" tries to generate p and q."); printStream4.println(sb4.toString()); } else { bigInteger4 = generatePrime3; } BigInteger multiply3 = add.multiply(add2); BigInteger multiply4 = add.subtract(bigInteger2).multiply(add2.subtract(bigInteger2)); if (isDebug) { System.out.println("generating g"); } long j4 = 0; while (true) { Vector vector = new Vector(); bigInteger5 = add; bigInteger6 = add2; int i3 = 0; while (i3 != permuteList.size()) { BigInteger divide = multiply4.divide((BigInteger) permuteList.elementAt(i3)); while (true) { j2 = j4 + 1; bigInteger10 = new BigInteger(strength, certainty, random); i = strength; if (!bigInteger10.modPow(divide, multiply3).equals(ONE)) { break; } j4 = j2; strength = i; } vector.addElement(bigInteger10); i3++; j4 = j2; strength = i; } int i4 = strength; bigInteger7 = ONE; int i5 = 0; while (i5 < permuteList.size()) { bigInteger7 = bigInteger7.multiply(((BigInteger) vector.elementAt(i5)).modPow(multiply.divide((BigInteger) permuteList.elementAt(i5)), multiply3)).mod(multiply3); i5++; random = random; } SecureRandom secureRandom = random; int i6 = 0; while (true) { if (i6 >= permuteList.size()) { BigInteger modPow = bigInteger7.modPow(multiply4.divide(BigInteger.valueOf(4L)), multiply3); BigInteger bigInteger14 = ONE; if (!modPow.equals(bigInteger14)) { if (!bigInteger7.modPow(multiply4.divide(generatePrime), multiply3).equals(bigInteger14)) { if (!bigInteger7.modPow(multiply4.divide(generatePrime2), multiply3).equals(bigInteger14)) { bigInteger8 = bigInteger4; if (!bigInteger7.modPow(multiply4.divide(bigInteger8), multiply3).equals(bigInteger14)) { bigInteger9 = bigInteger13; if (!bigInteger7.modPow(multiply4.divide(bigInteger9), multiply3).equals(bigInteger14)) { break; } if (isDebug) { System.out.println("g has order phi(n)/b\n g: ".concat(String.valueOf(bigInteger7))); } } else { if (isDebug) { System.out.println("g has order phi(n)/a\n g: ".concat(String.valueOf(bigInteger7))); } bigInteger9 = bigInteger13; } } else if (isDebug) { printStream = System.out; sb = new StringBuilder("g has order phi(n)/q'\n g: "); sb.append(bigInteger7); printStream.println(sb.toString()); } } else if (isDebug) { printStream = System.out; sb = new StringBuilder("g has order phi(n)/p'\n g: "); sb.append(bigInteger7); printStream.println(sb.toString()); } } else if (isDebug) { printStream = System.out; sb = new StringBuilder("g has order phi(n)/4\n g:"); sb.append(bigInteger7); printStream.println(sb.toString()); } } else if (!bigInteger7.modPow(multiply4.divide((BigInteger) permuteList.elementAt(i6)), multiply3).equals(ONE)) { i6++; } else if (isDebug) { PrintStream printStream5 = System.out; StringBuilder sb5 = new StringBuilder("g has order phi(n)/"); sb5.append(permuteList.elementAt(i6)); sb5.append("\n g: "); sb5.append(bigInteger7); printStream5.println(sb5.toString()); } } bigInteger9 = bigInteger13; bigInteger8 = bigInteger4; bigInteger4 = bigInteger8; bigInteger13 = bigInteger9; add2 = bigInteger6; add = bigInteger5; strength = i4; random = secureRandom; } if (isDebug) { PrintStream printStream6 = System.out; StringBuilder sb6 = new StringBuilder("needed "); sb6.append(j4); sb6.append(" tries to generate g"); printStream6.println(sb6.toString()); System.out.println(); System.out.println("found new NaccacheStern cipher variables:"); System.out.println("smallPrimes: ".concat(String.valueOf(permuteList))); PrintStream printStream7 = System.out; StringBuilder sb7 = new StringBuilder("sigma:...... "); sb7.append(multiply); sb7.append(" ("); sb7.append(multiply.bitLength()); sb7.append(" bits)"); printStream7.println(sb7.toString()); System.out.println("a:.......... ".concat(String.valueOf(bigInteger8))); System.out.println("b:.......... ".concat(String.valueOf(bigInteger9))); System.out.println("p':......... ".concat(String.valueOf(generatePrime))); System.out.println("q':......... ".concat(String.valueOf(generatePrime2))); System.out.println("p:.......... ".concat(String.valueOf(bigInteger5))); System.out.println("q:.......... ".concat(String.valueOf(bigInteger6))); System.out.println("n:.......... ".concat(String.valueOf(multiply3))); System.out.println("phi(n):..... ".concat(String.valueOf(multiply4))); System.out.println("g:.......... ".concat(String.valueOf(bigInteger7))); System.out.println(); } return new AsymmetricCipherKeyPair((AsymmetricKeyParameter) new NaccacheSternKeyParameters(false, bigInteger7, multiply3, multiply.bitLength()), (AsymmetricKeyParameter) new NaccacheSternPrivateKeyParameters(bigInteger7, multiply3, multiply.bitLength(), permuteList, multiply4)); } private static Vector permuteList(Vector vector, SecureRandom secureRandom) { Vector vector2 = new Vector(); Vector vector3 = new Vector(); for (int i = 0; i < vector.size(); i++) { vector3.addElement(vector.elementAt(i)); } vector2.addElement(vector3.elementAt(0)); while (true) { vector3.removeElementAt(0); if (vector3.size() == 0) { return vector2; } vector2.insertElementAt(vector3.elementAt(0), getInt(secureRandom, vector2.size() + 1)); } } private static int getInt(SecureRandom secureRandom, int i) { int nextInt; int i2; if (((-i) & i) == i) { return (int) ((i * (secureRandom.nextInt() & Integer.MAX_VALUE)) >> 31); } do { nextInt = secureRandom.nextInt() & Integer.MAX_VALUE; i2 = nextInt % i; } while ((nextInt - i2) + (i - 1) < 0); return i2; } private static BigInteger generatePrime(int i, int i2, SecureRandom secureRandom) { BigInteger bigInteger = new BigInteger(i, i2, secureRandom); while (bigInteger.bitLength() != i) { bigInteger = new BigInteger(i, i2, secureRandom); } return bigInteger; } private static Vector findFirstPrimes(int i) { Vector vector = new Vector(i); for (int i2 = 0; i2 != i; i2++) { vector.addElement(BigInteger.valueOf(smallPrimes[i2])); } return vector; } }