Android Open Source - conceal Crypto Serializer Helper






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.//  www. j  ava  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;

import com.facebook.crypto.cipher.NativeGCMCipher;

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

/**
 * Helper functions for tests to serialize and de-serialize crypto data.
 */
public class CryptoSerializerHelper {

  public static byte[] cipherText(byte[] cipheredData) {
    return Arrays.copyOfRange(cipheredData,
        NativeGCMCipher.IV_LENGTH + 2,
        cipheredData.length - NativeGCMCipher.TAG_LENGTH);
  }

  public static byte[] tag(byte[] cipheredData) {
    return Arrays.copyOfRange(cipheredData,
        cipheredData.length - NativeGCMCipher.TAG_LENGTH,
        cipheredData.length);
  }

  public static byte[] createCipheredData(byte[] iv, byte[] cipherText, byte[] tag) throws IOException {
    ByteArrayOutputStream cipheredData = new ByteArrayOutputStream();
    cipheredData.write(VersionCodes.CIPHER_SERALIZATION_VERSION);
    cipheredData.write(VersionCodes.CIPHER_ID);
    cipheredData.write(iv);
    cipheredData.write(cipherText);
    cipheredData.write(tag);
    return cipheredData.toByteArray();
  }

  public static byte[] createMacData(byte[] data, byte[] macBytes) throws IOException {
    ByteArrayOutputStream dataWithMac = new ByteArrayOutputStream();
    dataWithMac.write(VersionCodes.MAC_SERIALIZATION_VERSION);
    dataWithMac.write(VersionCodes.MAC_ID);
    dataWithMac.write(data);
    dataWithMac.write(macBytes);
    return  dataWithMac.toByteArray();
  }

  public static byte[] getMacTag(byte[] macData, int macLength) {
    return Arrays.copyOfRange(macData, macData.length - macLength, macData.length);
  }

  public static byte[] getOriginalDataFromMacData(byte[] macData, int macLength) {
    return Arrays.copyOfRange(macData, 2, macData.length - macLength);
  }

  /**
   * This method mixes in the crypto serialization version as well as the ID of either the cipher or mac
   * into the authenticated bytes to prevent cross-protocol attacks, i.e. if we don't authenticate
   * this data, we could be forced to use a construction using the parameters of some other
   * construction.
   */
  public static byte[] computeBytesToAuthenticate(byte[] entityBytes, byte cryptoVersion, byte cryptoId) {
    int entityLength = entityBytes.length;
    byte[] aadBytes = new byte[entityLength + 2];
    aadBytes[0] = cryptoVersion;
    aadBytes[1] = cryptoId;
    System.arraycopy(entityBytes, 0, aadBytes, 2, entityLength);
    return aadBytes;
  }
}




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