Android Open Source - conceal Cipher Read Benchmark






From Project

Back to project page conceal.

License

The source code is released under:

BSD License For Conceal software Copyright (c) 2014, Facebook, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that...

If you think the Android project conceal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 *  Copyright (c) 2014, Facebook, Inc./*w  w  w .  j  a v a  2 s  .  c  om*/
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */

package com.facebook.crypto.benchmarks;

import javax.crypto.Mac;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Random;

import com.facebook.crypto.benchmarks.cipher.AESCipher;
import com.facebook.crypto.benchmarks.cipher.BaseCipher;
import com.facebook.crypto.benchmarks.cipher.BouncyCastleGCMCipher;
import com.facebook.crypto.benchmarks.cipher.NativeGCMCipherHelper;
import com.facebook.crypto.benchmarks.mac.HMAC;
import com.facebook.crypto.benchmarks.mac.NativeMacHelper;
import com.facebook.crypto.benchmarks.mac.streams.MacLayeredInputStream;

import com.google.caliper.Param;
import com.google.caliper.SimpleBenchmark;
import com.google.common.io.NullOutputStream;
import org.spongycastle.jce.provider.BouncyCastleProvider;

public class CipherReadBenchmark extends SimpleBenchmark {

  private byte[] mData;

  private byte[] mReadBuffer;

  private byte[] mNativeGCMCipheredData;
  private byte[] mBCGCMCipheredData;
  private byte[] mAESCipherText;

  private HMAC mHMAC;
  private NativeGCMCipherHelper mNativeGCMCipherHelper;

  // This field is purposely here so that we can get a
  // reference to NativeMac which is necessary for
  // library initialization.
  private NativeMacHelper mNativeMacHelper;

  private AESCipher mAESCipher;
  private BouncyCastleGCMCipher mBCGCMCipher;

  @Param({"102400"})
  int size;

  @Override
  public void setUp() throws Exception {
    // Initialize the buffers
    Random random = new Random();
    mData = new byte[size];
    random.nextBytes(mData);
    mReadBuffer = new byte[1024];

    // Initialize the ciphers and Macs.
    mHMAC = HMAC.getInstance();
    mNativeGCMCipherHelper = NativeGCMCipherHelper.getInstance();
    mAESCipher = AESCipher.getInstance();
    Security.addProvider(new BouncyCastleProvider());
    mBCGCMCipher = BouncyCastleGCMCipher.getInstance();

    // Initialize the ciphered outputs.
    mNativeGCMCipheredData = generateCipherText(mNativeGCMCipherHelper);
    mBCGCMCipheredData = generateCipherText(mBCGCMCipher);
    mAESCipherText = generateCipherText(mAESCipher);
  }

  private byte[] generateCipherText(BaseCipher cipher) throws Exception {
    ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
    OutputStream out = cipher.getOutputStream(cipherText);
    out.write(mData);
    out.close();
    return cipherText.toByteArray();
  }

  private byte[] generateCipherText(NativeGCMCipherHelper gcmHelper)
      throws Exception {
    ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
    OutputStream out = gcmHelper.getOutputStream(cipherText);
    out.write(mData);
    out.close();
    return cipherText.toByteArray();
  }

  public void timeBouncyCastleGCMRead(int reps) throws Exception {
    for (int i = 0; i < reps; ++i) {
      ByteArrayInputStream cipheredInput = new ByteArrayInputStream(mBCGCMCipheredData);
      InputStream input = mBCGCMCipher.getInputStream(cipheredInput);
      readFully(input, new NullOutputStream());
      // Not closing the bouncy castle stream on purpose because of a bug in
      // bouncycastle.
      //input.close();
    }
  }

  public void timeNativeGCMRead(int reps) throws Exception {
    for (int i = 0; i < reps; ++i) {
      ByteArrayInputStream cipheredInput = new ByteArrayInputStream(mNativeGCMCipheredData);
      InputStream input = mNativeGCMCipherHelper.getInputStream(cipheredInput);
      readFully(input, new NullOutputStream());
      input.close();
    }
  }

  public void timeAESWithHmacRead(int reps) throws Exception {
    for (int i = 0; i < reps; ++i) {
      ByteArrayInputStream cipheredInput = new ByteArrayInputStream(mAESCipherText);
      Mac mac = mHMAC.getMac();
      InputStream macStream = new MacLayeredInputStream(mac, cipheredInput);
      InputStream input = mAESCipher.getInputStream(macStream);
      readFully(input, new NullOutputStream());
      mac.doFinal();
      input.close();
    }
  }

  private void readFully(InputStream inputStream, OutputStream out) throws Exception {
    int read;
    while ((read = inputStream.read(mReadBuffer)) != -1) {
      out.write(mReadBuffer, 0, read);
    }
  }
}




Java Source Code List

com.facebook.android.crypto.keychain.SecureRandomFix.java
com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChainTest.java
com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain.java
com.facebook.crypto.BouncyCastleHelper.java
com.facebook.crypto.CipherHelper.java
com.facebook.crypto.CryptoSerializerHelper.java
com.facebook.crypto.CryptoTestUtils.java
com.facebook.crypto.Crypto.java
com.facebook.crypto.Entity.java
com.facebook.crypto.FakeKeyChain.java
com.facebook.crypto.FbInstrumentationTestRunner.java
com.facebook.crypto.NativeGCMCipherInputStreamTest.java
com.facebook.crypto.NativeGCMCipherOutputStreamTest.java
com.facebook.crypto.NativeMacLayeredInputStreamTest.java
com.facebook.crypto.NativeMacLayeredOutputStreamTest.java
com.facebook.crypto.SimpleDecryptTest.java
com.facebook.crypto.SimpleEncryptTest.java
com.facebook.crypto.VersionCodes.java
com.facebook.crypto.benchmarks.BenchmarkNativeCryptoLibrary.java
com.facebook.crypto.benchmarks.CipherReadBenchmark.java
com.facebook.crypto.benchmarks.CipherWriteBenchmark.java
com.facebook.crypto.benchmarks.MacBenchmark.java
com.facebook.crypto.benchmarks.cipher.AESCipher.java
com.facebook.crypto.benchmarks.cipher.BaseCipher.java
com.facebook.crypto.benchmarks.cipher.BouncyCastleCCMCipher.java
com.facebook.crypto.benchmarks.cipher.BouncyCastleGCMCipher.java
com.facebook.crypto.benchmarks.cipher.NativeGCMCipherHelper.java
com.facebook.crypto.benchmarks.mac.BaseMac.java
com.facebook.crypto.benchmarks.mac.HMAC.java
com.facebook.crypto.benchmarks.mac.NativeMacHelper.java
com.facebook.crypto.benchmarks.mac.streams.MacLayeredInputStream.java
com.facebook.crypto.benchmarks.mac.streams.MacLayeredOutputStream.java
com.facebook.crypto.cipher.NativeGCMCipherException.java
com.facebook.crypto.cipher.NativeGCMCipher.java
com.facebook.crypto.exception.CryptoInitializationException.java
com.facebook.crypto.exception.KeyChainException.java
com.facebook.crypto.keychain.KeyChain.java
com.facebook.crypto.mac.NativeMac.java
com.facebook.crypto.streams.BetterCipherInputStreamTest.java
com.facebook.crypto.streams.BetterCipherInputStream.java
com.facebook.crypto.streams.FixedSizeByteArrayOutputStream.java
com.facebook.crypto.streams.NativeGCMCipherInputStream.java
com.facebook.crypto.streams.NativeGCMCipherOutputStream.java
com.facebook.crypto.streams.NativeMacLayeredInputStream.java
com.facebook.crypto.streams.NativeMacLayeredOutputStream.java
com.facebook.crypto.streams.TailBufferHelper.java
com.facebook.crypto.streams.TailInputStreamTest.java
com.facebook.crypto.streams.TailInputStream.java
com.facebook.crypto.util.Assertions.java
com.facebook.crypto.util.NativeCryptoLibrary.java
com.facebook.crypto.util.SystemNativeCryptoLibrary.java
com.facebook.proguard.annotations.DoNotStrip.java
com.facebook.proguard.annotations.InternalBuildOnly.java
com.facebook.proguard.annotations.KeepGettersAndSetters.java