Android Open Source - conceal Native G C M Cipher Output Stream Test






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./*from  ww  w .ja  v  a2s .  c  o m*/
 *  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;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Arrays;

import com.facebook.crypto.cipher.NativeGCMCipher;
import com.facebook.crypto.keychain.KeyChain;
import com.facebook.crypto.util.NativeCryptoLibrary;
import com.facebook.crypto.util.SystemNativeCryptoLibrary;

import android.annotation.TargetApi;
import android.os.Build;
import android.test.InstrumentationTestCase;
import android.util.Base64;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class NativeGCMCipherOutputStreamTest extends InstrumentationTestCase {
  private Crypto mCrypto;
  private NativeCryptoLibrary mNativeCryptoLibrary;
  private byte[] mData;
  private byte[] mIV;
  private byte[] mKey;
  private ByteArrayOutputStream mCipherOutputStream;

  protected void setUp() throws Exception {
    super.setUp();
    mNativeCryptoLibrary = new SystemNativeCryptoLibrary();
    KeyChain keyChain = new FakeKeyChain();
    mKey = keyChain.getCipherKey();
    mIV = keyChain.getNewIV();
    mCrypto = new Crypto(keyChain, mNativeCryptoLibrary);
    mData = new byte[CryptoTestUtils.NUM_DATA_BYTES];
    mCipherOutputStream = new ByteArrayOutputStream();
  }

  public void testCompatibleWithBouncycastle() throws Exception {
    OutputStream outputStream = mCrypto.getCipherOutputStream(
        mCipherOutputStream,
        new Entity(CryptoTestUtils.ENTITY_NAME));
    outputStream.write(mData);
    outputStream.close();
    byte[] opensslEncrypted = mCipherOutputStream.toByteArray();

    Entity entity = new Entity(CryptoTestUtils.ENTITY_NAME);
    byte[] aadData = CryptoSerializerHelper.computeBytesToAuthenticate(
        entity.getBytes(),
        VersionCodes.CIPHER_SERALIZATION_VERSION,
        VersionCodes.CIPHER_ID);
    BouncyCastleHelper.Result result = BouncyCastleHelper.bouncyCastleEncrypt(mData,
        mKey,
        mIV,
        aadData);

    byte[] opensslTag = CryptoSerializerHelper.tag(opensslEncrypted);
    byte[] opensslCipherText = CryptoSerializerHelper.cipherText(opensslEncrypted);

    assertTrue(
        CryptoTestUtils.ENCRYPTED_DATA_DOES_NOT_MATCH,
        Arrays.equals(result.cipherText, opensslCipherText)
    );
    assertTrue(CryptoTestUtils.TAG_DOES_NOT_MATCH, Arrays.equals(result.tag, opensslTag));
  }

  public void testWriteData() throws Exception {
    OutputStream outputStream = mCrypto.getCipherOutputStream(
        mCipherOutputStream,
        new Entity(CryptoTestUtils.ENTITY_NAME));
    outputStream.write(mData);
    outputStream.close();
    byte[] encryptedData = CryptoSerializerHelper.cipherText(mCipherOutputStream.toByteArray());

    assertTrue(CryptoTestUtils.ENCRYPTED_DATA_NULL, encryptedData != null);
    assertTrue(CryptoTestUtils.ENCRYPTED_DATA_OF_DIFFERENT_LENGTH,
        encryptedData.length == mData.length);
    assertTrue(CryptoTestUtils.DATA_IS_NOT_ENCRYPTED, !Arrays.equals(mData, encryptedData));
  }

  public void testWriteDataUsingOffsets() throws Exception {
    OutputStream outputStream = mCrypto.getCipherOutputStream(
        mCipherOutputStream,
        new Entity(CryptoTestUtils.ENTITY_NAME));
    outputStream.write(mData, 0, mData.length / 2);
    outputStream.write(mData, mData.length / 2, mData.length / 2 + mData.length % 2);
    outputStream.close();
    byte[] encryptedData = CryptoSerializerHelper.cipherText(mCipherOutputStream.toByteArray());

    assertTrue(CryptoTestUtils.ENCRYPTED_DATA_NULL, encryptedData != null);
    assertTrue(CryptoTestUtils.ENCRYPTED_DATA_OF_DIFFERENT_LENGTH,
        encryptedData.length == mData.length);
    assertTrue(CryptoTestUtils.DATA_IS_NOT_ENCRYPTED, !Arrays.equals(mData, encryptedData));
  }

  public void testEncryptedDataIsExpected() throws Exception {
    String dataToEncrypt = "data to encrypt";
    String expectedEncryptedString = "69VhniqXP+xA0CcKJFx5";
    OutputStream outputStream = mCrypto.getCipherOutputStream(
        mCipherOutputStream,
        new Entity(CryptoTestUtils.ENTITY_NAME));
    outputStream.write(dataToEncrypt.getBytes("UTF-8"));
    outputStream.close();
    byte[] encryptedData = CryptoSerializerHelper.cipherText(mCipherOutputStream.toByteArray());

    String encryptedString = Base64.encodeToString(encryptedData, Base64.DEFAULT).trim();
    assertEquals(CryptoTestUtils.ENCRYPTED_DATA_IS_DIFFERENT,
        expectedEncryptedString,
        encryptedString);
  }

  public void testEncryptedDataIsExpectedWhenWrittenOneByteAtATime() throws Exception {
    String dataToEncrypt = "data to encrypt";
    String expectedEncryptedString = "69VhniqXP+xA0CcKJFx5";
    OutputStream outputStream = mCrypto.getCipherOutputStream(
      mCipherOutputStream,
      new Entity(CryptoTestUtils.ENTITY_NAME));

    byte[] inputData = dataToEncrypt.getBytes("UTF-8");
    for (byte inputByte : inputData) {
      outputStream.write(inputByte);
    }
    outputStream.close();
    byte[] encryptedData = CryptoSerializerHelper.cipherText(mCipherOutputStream.toByteArray());

    String encryptedString = Base64.encodeToString(encryptedData, Base64.DEFAULT).trim();
    assertEquals(CryptoTestUtils.ENCRYPTED_DATA_IS_DIFFERENT,
      expectedEncryptedString,
      encryptedString);
  }
}




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