private void process(ByteBuffer inBuffer, ByteBuffer outBuffer) throws IOException { try { int inputSize = inBuffer.remaining(); // OpensslCipher#update will maintain crypto context. int n = cipher.update(inBuffer, outBuffer); if (n < inputSize) { /** * Typically code will not get here. OpensslCipher#update will * consume all input data and put result in outBuffer. * OpensslCipher#doFinal will reset the crypto context. */ contextReset = true; cipher.doFinal(outBuffer); } } catch (Exception e) { throw new IOException(e); } }
public OpensslAesCtrCipher(int mode) throws GeneralSecurityException { this.mode = mode; cipher = OpensslCipher.getInstance(SUITE.getName()); }
openSslDetail = OpensslCipher.getLoadingFailureReason(); if (openSslDetail != null) { openSslLoaded = false; } else { openSslDetail = OpensslCipher.getLibraryName(); openSslLoaded = true;
/** * Return an <code>OpensslCipher<code> object that implements the specified * transformation. * * @param transformation the name of the transformation, e.g., * AES/CTR/NoPadding. * @return OpensslCipher an <code>OpensslCipher<code> object * @throws NoSuchAlgorithmException if <code>transformation</code> is null, * empty, in an invalid format, or if Openssl doesn't implement the * specified algorithm. * @throws NoSuchPaddingException if <code>transformation</code> contains * a padding scheme that is not available. */ public static final OpensslCipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException { Transform transform = tokenizeTransformation(transformation); int algMode = AlgMode.get(transform.alg, transform.mode); int padding = Padding.get(transform.padding); long context = initContext(algMode, padding); return new OpensslCipher(context, algMode, padding); }
checkState(); Preconditions.checkArgument(output.isDirect(), "Direct buffer is required."); int len = doFinal(context, output, output.position(), output.remaining()); output.position(output.position() + len); return len;
@Test(timeout=120000) public void testDoFinalArguments() throws Exception { Assume.assumeTrue(OpensslCipher.getLoadingFailureReason() == null); OpensslCipher cipher = OpensslCipher.getInstance("AES/CTR/NoPadding"); Assert.assertTrue(cipher != null); cipher.init(OpensslCipher.ENCRYPT_MODE, key, iv); // Require direct buffer ByteBuffer output = ByteBuffer.allocate(1024); try { cipher.doFinal(output); Assert.fail("Output buffer should be direct buffer."); } catch (IllegalArgumentException e) { GenericTestUtils.assertExceptionContains( "Direct buffer is required", e); } } }
@Test(timeout=120000) public void testUpdateArguments() throws Exception { Assume.assumeTrue(OpensslCipher.getLoadingFailureReason() == null); OpensslCipher cipher = OpensslCipher.getInstance("AES/CTR/NoPadding"); Assert.assertTrue(cipher != null); cipher.init(OpensslCipher.ENCRYPT_MODE, key, iv); cipher.update(input, output); Assert.fail("Input and output buffer should be direct buffer."); } catch (IllegalArgumentException e) { output = ByteBuffer.allocateDirect(1000); try { cipher.update(input, output); Assert.fail("Output buffer length should be sufficient " + "to store output data");
checkState(); Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required."); int len = update(context, input, input.position(), input.remaining(), output, output.position(), output.remaining()); input.position(input.limit());
/** * Initialize this cipher with a key and IV. * * @param mode {@link #ENCRYPT_MODE} or {@link #DECRYPT_MODE} * @param key crypto key * @param iv crypto iv */ public void init(int mode, byte[] key, byte[] iv) { context = init(context, mode, alg, padding, key, iv); }
public OpensslAesCtrCryptoCodec() { String loadingFailureReason = OpensslCipher.getLoadingFailureReason(); if (loadingFailureReason != null) { throw new RuntimeException(loadingFailureReason); } }
@Test(timeout=120000) public void testGetInstance() throws Exception { Assume.assumeTrue(OpensslCipher.getLoadingFailureReason() == null); OpensslCipher cipher = OpensslCipher.getInstance("AES/CTR/NoPadding"); Assert.assertTrue(cipher != null); try { cipher = OpensslCipher.getInstance("AES2/CTR/NoPadding"); Assert.fail("Should specify correct algorithm."); } catch (NoSuchAlgorithmException e) { // Expect NoSuchAlgorithmException } try { cipher = OpensslCipher.getInstance("AES/CTR/NoPadding2"); Assert.fail("Should specify correct padding."); } catch (NoSuchPaddingException e) { // Expect NoSuchPaddingException } }
/** Forcibly clean the context. */ public void clean() { if (context != 0) { clean(context); context = 0; } }
@Test public void testNativeCodeLoaded() { if (requireTestJni() == false) { LOG.info("TestNativeCodeLoader: libhadoop.so testing is not required."); return; } if (!NativeCodeLoader.isNativeCodeLoaded()) { fail("TestNativeCodeLoader: libhadoop.so testing was required, but " + "libhadoop.so was not loaded."); } assertFalse(NativeCodeLoader.getLibraryName().isEmpty()); // library names are depended on platform and build envs // so just check names are available assertFalse(ZlibFactory.getLibraryName().isEmpty()); if (NativeCodeLoader.buildSupportsSnappy()) { assertFalse(SnappyCodec.getLibraryName().isEmpty()); } if (NativeCodeLoader.buildSupportsOpenssl()) { assertFalse(OpensslCipher.getLibraryName().isEmpty()); } assertFalse(Lz4Codec.getLibraryName().isEmpty()); LOG.info("TestNativeCodeLoader: libhadoop.so is loaded."); } }
@Test(timeout=120000) public void testDoFinalArguments() throws Exception { Assume.assumeTrue(OpensslCipher.getLoadingFailureReason() == null); OpensslCipher cipher = OpensslCipher.getInstance("AES/CTR/NoPadding"); Assert.assertTrue(cipher != null); cipher.init(OpensslCipher.ENCRYPT_MODE, key, iv); // Require direct buffer ByteBuffer output = ByteBuffer.allocate(1024); try { cipher.doFinal(output); Assert.fail("Output buffer should be direct buffer."); } catch (IllegalArgumentException e) { GenericTestUtils.assertExceptionContains( "Direct buffer is required", e); } } }
@Test(timeout=120000) public void testUpdateArguments() throws Exception { Assume.assumeTrue(OpensslCipher.getLoadingFailureReason() == null); OpensslCipher cipher = OpensslCipher.getInstance("AES/CTR/NoPadding"); Assert.assertTrue(cipher != null); cipher.init(OpensslCipher.ENCRYPT_MODE, key, iv); cipher.update(input, output); Assert.fail("Input and output buffer should be direct buffer."); } catch (IllegalArgumentException e) { output = ByteBuffer.allocateDirect(1000); try { cipher.update(input, output); Assert.fail("Output buffer length should be sufficient " + "to store output data");
/** * Return an <code>OpensslCipher<code> object that implements the specified * transformation. * * @param transformation the name of the transformation, e.g., * AES/CTR/NoPadding. * @return OpensslCipher an <code>OpensslCipher<code> object * @throws NoSuchAlgorithmException if <code>transformation</code> is null, * empty, in an invalid format, or if Openssl doesn't implement the * specified algorithm. * @throws NoSuchPaddingException if <code>transformation</code> contains * a padding scheme that is not available. */ public static final OpensslCipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException { Transform transform = tokenizeTransformation(transformation); int algMode = AlgMode.get(transform.alg, transform.mode); int padding = Padding.get(transform.padding); long context = initContext(algMode, padding); return new OpensslCipher(context, algMode, padding); }
checkState(); Preconditions.checkArgument(output.isDirect(), "Direct buffer is required."); int len = doFinal(context, output, output.position(), output.remaining()); output.position(output.position() + len); return len;
checkState(); Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required."); int len = update(context, input, input.position(), input.remaining(), output, output.position(), output.remaining()); input.position(input.limit());
@Override public void init(byte[] key, byte[] iv) throws IOException { Preconditions.checkNotNull(key); Preconditions.checkNotNull(iv); contextReset = false; cipher.init(mode, key, iv); }
public OpensslAesCtrCryptoCodec() { String loadingFailureReason = OpensslCipher.getLoadingFailureReason(); if (loadingFailureReason != null) { throw new RuntimeException(loadingFailureReason); } }