Java 类org.bouncycastle.crypto.modes.EAXBlockCipher 实例源码

项目:gwt-crypto    文件:SerpentTest.java   
private void doEax(byte[] key, byte[] iv, byte[] pt, byte[] aad, int tagLength, byte[] expected)
    throws InvalidCipherTextException
{
    EAXBlockCipher c = new EAXBlockCipher(new SerpentEngine());

    c.init(true, new AEADParameters(new KeyParameter(key), tagLength, iv, aad));

    byte[] out = new byte[expected.length];

    int len = c.processBytes(pt, 0, pt.length, out, 0);

    c.doFinal(out, len);

    if (!Arrays.areEqual(expected, out))
    {
        fail("EAX test failed");
    }
}
项目:PasswordSafe    文件:EAXEncryptStream.java   
public EAXEncryptStream(byte[] key, byte[] nonce, byte[] associatedData, OutputStream os)
{
    if(key.length != KEY_LENGTH_BYTES)
    {
        throw new IllegalArgumentException("key must be " + KEY_LENGTH_BYTES*8 + " bits");
    }

    this.cipher = new EAXBlockCipher(new AESEngine());
    this.os = os;

    keyParameter = new KeyParameter(key);
    AEADParameters par = new AEADParameters(keyParameter, EAXDecryptStream.MAC_LEN_BITS, nonce, associatedData);

    cipher.init(true, par);

    out = new byte[BUFFER_SIZE];
}
项目:PasswordSafe    文件:EAXDecryptStream.java   
public EAXDecryptStream(byte[] key, byte[] nonce, byte[] associatedData, InputStream in)
{
    if(key.length != KEY_LENGTH_BYTES)
    {
        throw new IllegalArgumentException("key must be " + KEY_LENGTH_BYTES*8 + " bits");
    }

    this.in = in;
    this.cipher = new EAXBlockCipher(new AESEngine());

    keyParameter = new KeyParameter(key);
    AEADParameters par = new AEADParameters(keyParameter, MAC_LEN_BITS, nonce, associatedData);

    cipher.init(false, par);

    out = new byte[BUFFER_SIZE];
    buf = new byte[BUFFER_SIZE];
}
项目:gwt-crypto    文件:CipherStreamTest.java   
private void testModes(BlockCipher cipher1, BlockCipher cipher2, int keySize)
    throws Exception
{
    final KeyParameter key = new KeyParameter(new byte[keySize]);
    final int blockSize = getBlockSize(cipher1);
    final CipherParameters withIv = new ParametersWithIV(key, new byte[blockSize]);

    if (blockSize > 1)
    {
        testMode(new PaddedBufferedBlockCipher(cipher1, new PKCS7Padding()), key);

        testMode(new PaddedBufferedBlockCipher(new CBCBlockCipher(cipher1), new PKCS7Padding()), withIv);

        testMode(new BufferedBlockCipher(new OFBBlockCipher(cipher1, blockSize)), withIv);
        testMode(new BufferedBlockCipher(new CFBBlockCipher(cipher1, blockSize)), withIv);
        testMode(new BufferedBlockCipher(new SICBlockCipher(cipher1)), withIv);
    }
    // CTS requires at least one block
    if (blockSize <= 16 && streamSize >= blockSize)
    {
        testMode(new CTSBlockCipher(cipher1), key);
    }
    if (blockSize <= 16 && streamSize >= blockSize)
    {
        testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS1, cipher1), key);
        testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS2, cipher1), key);
        testMode(new NISTCTSBlockCipher(NISTCTSBlockCipher.CS3, cipher1), key);
    }
    if (blockSize == 8 || blockSize == 16)
    {
        testMode(new EAXBlockCipher(cipher1), withIv);
    }
    if (blockSize == 16)
    {
        testMode(new CCMBlockCipher(cipher1), new ParametersWithIV(key, new byte[7]));
        testMode(new GCMBlockCipher(cipher1), withIv);
        testMode(new OCBBlockCipher(cipher1, cipher2), new ParametersWithIV(key, new byte[15]));
    }
}
项目:gwt-crypto    文件:EAXTest.java   
private void randomTest(
    SecureRandom srng)
    throws InvalidCipherTextException
{
    int DAT_LEN = srng.nextInt() >>> 22; // Note: JDK1.0 compatibility
    byte[] nonce = new byte[NONCE_LEN];
    byte[] authen = new byte[AUTHEN_LEN];
    byte[] datIn = new byte[DAT_LEN];
    byte[] key = new byte[16];
    srng.nextBytes(nonce);
    srng.nextBytes(authen);
    srng.nextBytes(datIn);
    srng.nextBytes(key);

    AESFastEngine engine = new AESFastEngine();
    KeyParameter sessKey = new KeyParameter(key);
    EAXBlockCipher eaxCipher = new EAXBlockCipher(engine);

    AEADParameters params = new AEADParameters(sessKey, MAC_LEN * 8, nonce, authen);
    eaxCipher.init(true, params);

    byte[] intrDat = new byte[eaxCipher.getOutputSize(datIn.length)];
    int outOff = eaxCipher.processBytes(datIn, 0, DAT_LEN, intrDat, 0);
    outOff += eaxCipher.doFinal(intrDat, outOff);

    eaxCipher.init(false, params);
    byte[] datOut = new byte[eaxCipher.getOutputSize(outOff)];
    int resultLen = eaxCipher.processBytes(intrDat, 0, outOff, datOut, 0);
    eaxCipher.doFinal(datOut, resultLen);

    if (!areEqual(datIn, datOut))
    {
        fail("EAX roundtrip failed to match");
    }
}
项目:PasswordSafe    文件:MemCrypt.java   
public static final byte[] encrypt(byte[] data) throws Exception
{
    EAXBlockCipher cipher = new EAXBlockCipher(new AESEngine());

    byte[] nonce = new byte[NONCE_SIZE_BYTES];
    new SecureRandom().nextBytes(nonce);

    byte[] key = generateKey();
    KeyParameter kp = new KeyParameter(key);
    try
    {
        AEADParameters par = new AEADParameters(kp, MAC_SIZE_BITS, nonce, ZERO_BYTE_ARRAY);
        cipher.init(true, par);

        int sz = cipher.getOutputSize(data.length);
        byte[] out = new byte[NONCE_SIZE_BYTES + sz];
        System.arraycopy(nonce, 0, out, 0, NONCE_SIZE_BYTES);

        int off = NONCE_SIZE_BYTES;
        off += cipher.processBytes(data, 0, data.length, out, off);
        cipher.doFinal(out, off);
        return out;
    }
    finally
    {
        Crypto.zero(kp);
        Crypto.zero(key);
    }
}
项目:PasswordSafe    文件:MemCrypt.java   
public static final byte[] decrypt(byte[] data) throws Exception
{
    EAXBlockCipher cipher = new EAXBlockCipher(new AESEngine());

    byte[] nonce = new byte[NONCE_SIZE_BYTES];
    System.arraycopy(data, 0, nonce, 0, NONCE_SIZE_BYTES);

    byte[] key = generateKey();
    KeyParameter kp = new KeyParameter(key);
    try
    {
        AEADParameters par = new AEADParameters(kp, MAC_SIZE_BITS, nonce, ZERO_BYTE_ARRAY);
        cipher.init(false, par);

        int sz = cipher.getOutputSize(data.length - NONCE_SIZE_BYTES);
        byte[] out = new byte[sz];

        int off = cipher.processBytes(data, NONCE_SIZE_BYTES, data.length - NONCE_SIZE_BYTES, out, 0);
        cipher.doFinal(out, off);
        return out;
    }
    finally
    {
        Crypto.zero(kp);
        Crypto.zero(key);
    }
}
项目:irma_future_id    文件:EAXTest.java   
private void randomTest(
    SecureRandom srng)
    throws InvalidCipherTextException
{
    int DAT_LEN = srng.nextInt() >>> 22; // Note: JDK1.0 compatibility
    byte[] nonce = new byte[NONCE_LEN];
    byte[] authen = new byte[AUTHEN_LEN];
    byte[] datIn = new byte[DAT_LEN];
    byte[] key = new byte[16];
    srng.nextBytes(nonce);
    srng.nextBytes(authen);
    srng.nextBytes(datIn);
    srng.nextBytes(key);

    AESFastEngine engine = new AESFastEngine();
    KeyParameter sessKey = new KeyParameter(key);
    EAXBlockCipher eaxCipher = new EAXBlockCipher(engine);

    AEADParameters params = new AEADParameters(sessKey, MAC_LEN * 8, nonce, authen);
    eaxCipher.init(true, params);

    byte[] intrDat = new byte[eaxCipher.getOutputSize(datIn.length)];
    int outOff = eaxCipher.processBytes(datIn, 0, DAT_LEN, intrDat, 0);
    outOff += eaxCipher.doFinal(intrDat, outOff);

    eaxCipher.init(false, params);
    byte[] datOut = new byte[eaxCipher.getOutputSize(outOff)];
    int resultLen = eaxCipher.processBytes(intrDat, 0, outOff, datOut, 0);
    eaxCipher.doFinal(datOut, resultLen);

    if (!areEqual(datIn, datOut))
    {
        fail("EAX roundtrip failed to match");
    }
}
项目:bc-java    文件:EAXTest.java   
private void randomTest(
    SecureRandom srng)
    throws InvalidCipherTextException
{
    int DAT_LEN = srng.nextInt() >>> 22; // Note: JDK1.0 compatibility
    byte[] nonce = new byte[NONCE_LEN];
    byte[] authen = new byte[AUTHEN_LEN];
    byte[] datIn = new byte[DAT_LEN];
    byte[] key = new byte[16];
    srng.nextBytes(nonce);
    srng.nextBytes(authen);
    srng.nextBytes(datIn);
    srng.nextBytes(key);

    AESFastEngine engine = new AESFastEngine();
    KeyParameter sessKey = new KeyParameter(key);
    EAXBlockCipher eaxCipher = new EAXBlockCipher(engine);

    AEADParameters params = new AEADParameters(sessKey, MAC_LEN * 8, nonce, authen);
    eaxCipher.init(true, params);

    byte[] intrDat = new byte[eaxCipher.getOutputSize(datIn.length)];
    int outOff = eaxCipher.processBytes(datIn, 0, DAT_LEN, intrDat, 0);
    outOff += eaxCipher.doFinal(intrDat, outOff);

    eaxCipher.init(false, params);
    byte[] datOut = new byte[eaxCipher.getOutputSize(outOff)];
    int resultLen = eaxCipher.processBytes(intrDat, 0, outOff, datOut, 0);
    eaxCipher.doFinal(datOut, resultLen);

    if (!areEqual(datIn, datOut))
    {
        fail("EAX roundtrip failed to match");
    }
}
项目:gwt-crypto    文件:EAXTest.java   
private void runCheckVectors(
    int count,
    EAXBlockCipher encEax,
    EAXBlockCipher decEax,
    String additionalDataType,
    byte[] sa,
    byte[] p,
    byte[] t,
    byte[] c)
    throws InvalidCipherTextException
{
    byte[] enc = new byte[c.length];

    if (sa != null)
    {
        encEax.processAADBytes(sa, 0, sa.length);
    }

    int len = encEax.processBytes(p, 0, p.length, enc, 0);

    len += encEax.doFinal(enc, len);

    if (!areEqual(c, enc))
    {
        fail("encrypted stream fails to match in test " + count + " with " + additionalDataType);
    }

    byte[] tmp = new byte[enc.length];

    if (sa != null)
    {
        decEax.processAADBytes(sa, 0, sa.length);
    }

    len = decEax.processBytes(enc, 0, enc.length, tmp, 0);

    len += decEax.doFinal(tmp, len);

    byte[] dec = new byte[len];

    System.arraycopy(tmp, 0, dec, 0, len);

    if (!areEqual(p, dec))
    {
        fail("decrypted stream fails to match in test " + count + " with " + additionalDataType);
    }

    if (!areEqual(t, decEax.getMac()))
    {
        fail("MAC fails to match in test " + count + " with " + additionalDataType);
    }
}
项目:irma_future_id    文件:EAXTest.java   
private void runCheckVectors(
    int count,
    EAXBlockCipher encEax,
    EAXBlockCipher decEax,
    String additionalDataType,
    byte[] sa,
    byte[] p,
    byte[] t,
    byte[] c)
    throws InvalidCipherTextException
{
    byte[] enc = new byte[c.length];

    if (sa != null)
    {
        encEax.processAADBytes(sa, 0, sa.length);
    }

    int len = encEax.processBytes(p, 0, p.length, enc, 0);

    len += encEax.doFinal(enc, len);

    if (!areEqual(c, enc))
    {
        fail("encrypted stream fails to match in test " + count + " with " + additionalDataType);
    }

    byte[] tmp = new byte[enc.length];

    if (sa != null)
    {
        decEax.processAADBytes(sa, 0, sa.length);
    }

    len = decEax.processBytes(enc, 0, enc.length, tmp, 0);

    len += decEax.doFinal(tmp, len);

    byte[] dec = new byte[len];

    System.arraycopy(tmp, 0, dec, 0, len);

    if (!areEqual(p, dec))
    {
        fail("decrypted stream fails to match in test " + count + " with " + additionalDataType);
    }

    if (!areEqual(t, decEax.getMac()))
    {
        fail("MAC fails to match in test " + count + " with " + additionalDataType);
    }
}
项目:bc-java    文件:EAXTest.java   
private void runCheckVectors(
    int count,
    EAXBlockCipher encEax,
    EAXBlockCipher decEax,
    String additionalDataType,
    byte[] sa,
    byte[] p,
    byte[] t,
    byte[] c)
    throws InvalidCipherTextException
{
    byte[] enc = new byte[c.length];

    if (sa != null)
    {
        encEax.processAADBytes(sa, 0, sa.length);
    }

    int len = encEax.processBytes(p, 0, p.length, enc, 0);

    len += encEax.doFinal(enc, len);

    if (!areEqual(c, enc))
    {
        fail("encrypted stream fails to match in test " + count + " with " + additionalDataType);
    }

    byte[] tmp = new byte[enc.length];

    if (sa != null)
    {
        decEax.processAADBytes(sa, 0, sa.length);
    }

    len = decEax.processBytes(enc, 0, enc.length, tmp, 0);

    len += decEax.doFinal(tmp, len);

    byte[] dec = new byte[len];

    System.arraycopy(tmp, 0, dec, 0, len);

    if (!areEqual(p, dec))
    {
        fail("decrypted stream fails to match in test " + count + " with " + additionalDataType);
    }

    if (!areEqual(t, decEax.getMac()))
    {
        fail("MAC fails to match in test " + count + " with " + additionalDataType);
    }
}
项目:toffi    文件:BcEaxAesEncoder.java   
@Override
protected AEADBlockCipher createAEADBlockCipher() {
    return new EAXBlockCipher(new AESEngine());
}
项目:toffi    文件:BcEaxAesDecoder.java   
@Override
protected AEADBlockCipher createAEADBlockCipher() {
    return new EAXBlockCipher(new AESEngine());
}