Search in sources :

Example 1 with TwofishEngine

use of org.spongycastle.crypto.engines.TwofishEngine in project SightRemote by TebbeUbben.

the class Cryptograph method produceCCMTag.

public static byte[] produceCCMTag(byte[] nonce, byte[] payload, byte[] header, byte[] key) {
    TwofishEngine engine = new TwofishEngine();
    engine.init(true, new KeyParameter(key));
    byte[] initializationVector = new byte[engine.getBlockSize()];
    engine.processBlock(produceIV(nonce, (short) payload.length), 0, initializationVector, 0);
    CBCBlockCipher cbc = new CBCBlockCipher(new TwofishEngine());
    cbc.init(true, new ParametersWithIV(new KeyParameter(key), initializationVector));
    byte[] processedHeader = blockCipherZeroPad(processHeader(header));
    byte[] processedPayload = blockCipherZeroPad(payload);
    byte[] combine = combine(processedHeader, blockCipherZeroPad(processedPayload));
    byte[] result = new byte[combine.length];
    for (int i = 0; i < combine.length / 16; i++) cbc.processBlock(combine, i * 16, result, i * 16);
    byte[] result2 = new byte[8];
    System.arraycopy(result, result.length - 16, result2, 0, 8);
    byte[] ctr = new byte[engine.getBlockSize()];
    engine.processBlock(produceCTRBlock(nonce, (short) 0), 0, ctr, 0);
    return byteArrayXOR(result2, ctr);
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) AsymmetricKeyParameter(org.spongycastle.crypto.params.AsymmetricKeyParameter) KeyParameter(org.spongycastle.crypto.params.KeyParameter) TwofishEngine(org.spongycastle.crypto.engines.TwofishEngine) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher)

Example 2 with TwofishEngine

use of org.spongycastle.crypto.engines.TwofishEngine in project SightRemote by TebbeUbben.

the class Cryptograph method encryptDataCTR.

public static byte[] encryptDataCTR(byte[] data, byte[] key, byte[] nonce) {
    byte[] padded = blockCipherZeroPad(data);
    int length = padded.length >> 4;
    byte[] result = new byte[length * 16];
    TwofishEngine engine = new TwofishEngine();
    engine.init(true, new KeyParameter(key));
    for (int i = 0; i < length; i++) {
        engine.processBlock(produceCTRBlock(nonce, (short) (i + 1)), 0, result, i * 16);
    }
    byte[] xor = byteArrayXOR(padded, result);
    byte[] copy = new byte[Math.min(data.length, xor.length)];
    System.arraycopy(xor, 0, copy, 0, copy.length);
    return copy;
}
Also used : AsymmetricKeyParameter(org.spongycastle.crypto.params.AsymmetricKeyParameter) KeyParameter(org.spongycastle.crypto.params.KeyParameter) TwofishEngine(org.spongycastle.crypto.engines.TwofishEngine)

Aggregations

TwofishEngine (org.spongycastle.crypto.engines.TwofishEngine)2 AsymmetricKeyParameter (org.spongycastle.crypto.params.AsymmetricKeyParameter)2 KeyParameter (org.spongycastle.crypto.params.KeyParameter)2 CBCBlockCipher (org.spongycastle.crypto.modes.CBCBlockCipher)1 ParametersWithIV (org.spongycastle.crypto.params.ParametersWithIV)1