@@ -1078,7 +1078,69 @@ static final byte[][] rsaKey(int len, boolean crt, boolean f4) {
10781078 // instrumented ffdc
10791079 } catch (java .security .NoSuchProviderException e ) {
10801080 // instrumented ffdc
1081+ } catch (java .lang .UnsupportedOperationException uoe ) {
1082+ //This is when hard ware crypto provider is at the top of java.security
1083+ //Using the different key creation routines.
1084+ System .out .println ("DEBUG: UnsupportedOperationException is caught!! Going back to the previous hardware crypto routine for evaluation." );
1085+ BigInteger p , q , n , d ;
1086+ BigInteger e = BigInteger .valueOf (f4 ? 0x10001 : 3 );
1087+ BigInteger one = BigInteger .valueOf (1 ), two = BigInteger .valueOf (2 );
1088+ byte [] b = new byte [(len /= 2 ) + 1 ];
1089+
1090+ for (p = null ;;) {
1091+ for (q = null ;;) {
1092+ if (q == null ) {
1093+ random (b , 1 , len );
1094+ b [1 ] |= 0xC0 ;
1095+ b [len ] |= 1 ;
1096+ q = new BigInteger (b );
1097+ } else {
1098+ q = q .add (two );
1099+ if (q .bitLength () > len * 8 ) {
1100+ q = null ;
1101+ continue ;
1102+ }
1103+ }
1104+
1105+ if (q .isProbablePrime (32 ) && e .gcd (q .subtract (one )).equals (one ))
1106+ break ;
1107+ }
1108+
1109+ if (p == null )
1110+ p = q ;
1111+ else {
1112+ n = p .multiply (q );
1113+ if (n .bitLength () == len * 2 * 8 ) {
1114+
1115+ d = e .modInverse ((p .subtract (one )).multiply (q .subtract (one )));
1116+
1117+ if (((p .modPow (e , n )).modPow (d , n )).equals (p ))
1118+ break ;
1119+ }
1120+ p = null ;
1121+ }
1122+ }
1123+
1124+ key [0 ] = n .toByteArray (); // modulus
1125+ key [1 ] = crt ? null : d .toByteArray (); // private exponent if a CRT key
1126+ key [2 ] = e .toByteArray (); // public exponent
1127+
1128+ if (crt ) {
1129+ if (p .compareTo (q ) < 0 ) {
1130+ e = p ;
1131+ p = q ;
1132+ q = e ;
1133+ }
1134+ key [3 ] = p .toByteArray (); // PrimeP
1135+ key [4 ] = q .toByteArray (); // PrimeQ
1136+ key [5 ] = d .remainder (p .subtract (one )).toByteArray (); // PrimeExponentP \
1137+ key [6 ] = d .remainder (q .subtract (one )).toByteArray (); // PrimeExponentQ - looks like JCE sets these to
1138+ // zero. You could calculate these if you want
1139+ // to.
1140+ key [7 ] = q .modInverse (p ).toByteArray (); // getCrtCoefficient /
1141+ }
10811142 }
1143+
10821144
10831145 return key ;
10841146 }
0 commit comments