public @NonNull AbstractPrivateKey getPrivateKey() { return new RSAOAEPPrivateKey(e, p, q, HashType.SHA1, HashType.SHA1, getRandSeed()); } }
public @NonNull AbstractPublicKey getPublicKey() { return new RSAOAEPPublicKey(n, e, HashType.SHA1, HashType.SHA1, getRandSeed()); }
/** * Perform a test to check whether we should use an optimized native implementation or default Java one. */ static boolean whetherShouldUseNative() { // Sometimes NativeRSAEngine does not work; e.g. when GMP native binary is not available. // Test once if it will work; if it doesn't, fallback to use default RSAEngine. final RSAOAEPTestVectors oaepSpec = new RSAOAEPTestVectors(); final ParametersWithRandom param = new ParametersWithRandom(oaepSpec.pubParameters, oaepSpec.getRandSeed()); boolean errorOccured; try { final NativeRSAEngine nativeRSAEngine = new NativeRSAEngine(); nativeRSAEngine.init(true, param); errorOccured = false; } catch (Throwable e) { errorOccured = true; } return !errorOccured; } }
/** * Make sure the SpongyCastle OAEPEncoding encodes the block according * to the test vectors from RSA OAEP specification. * * @throws Exception */ @Test public void encodeBlock() throws Exception { AsymmetricBlockCipher encoder = new OAEPEncoding(RSAEngineFactory.make()); encoder.init(true, new ParametersWithRandom(oaepSpec.pubParameters, oaepSpec.getRandSeed())); byte[] encoded = encoder.processBlock(oaepSpec.M, 0, oaepSpec.M.length); assertArrayEquals(encoded, oaepSpec.C); }
/** * Test {@link RSAOAEPPublicKey#updateFromHash} on success scenarios, * using RSA spec vectors. */ @Test public void updateFromHashGoodRSASpec() throws Exception { Map mapRSA = new HashMap<String, Object>() {{ put("n", oaepSpec.n); put("e", oaepSpec.e); put("hash", "SHA-1"); put("mgf1Hash", "SHA-1"); }}; AbstractPublicKey rsaPublicKey = new RSAOAEPPublicKey(); rsaPublicKey.updateFromHash(mapRSA); // We've read the public key from hash; // but, to test it over the RSA spec vectors, // we need to hack into the state.rng (and substitute it with our custom one), // even though it is declared as final. RSAOAEPPublicKey.State state = ((RSAOAEPPublicKey) rsaPublicKey).state; Field rngField = RSAOAEPPublicKey.State.class.getDeclaredField("rng"); rngField.setAccessible(true); rngField.set(state, oaepSpec.getRandSeed()); // Now we can even test the encryption on the RSA spec vectors. assertTrue(rsaPublicKey.isInitialized()); assertEquals(rsaPublicKey.getBitStrength(), 1024); ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor(); assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C); }
Field publicRngField = RSAOAEPPublicKey.State.class.getDeclaredField("rng"); publicRngField.setAccessible(true); publicRngField.set(publicState, oaepSpec.getRandSeed());