Search in sources :

Example 1 with TweakableBlockCipherParameters

use of com.github.zhenwei.core.crypto.params.TweakableBlockCipherParameters in project LinLong-Java by zhenwei1108.

the class ThreefishEngine method init.

/**
 * Initialise the engine.
 *
 * @param params an instance of {@link TweakableBlockCipherParameters}, or {@link KeyParameter}
 *               (to use a 0 tweak)
 */
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
    final byte[] keyBytes;
    final byte[] tweakBytes;
    if (params instanceof TweakableBlockCipherParameters) {
        TweakableBlockCipherParameters tParams = (TweakableBlockCipherParameters) params;
        keyBytes = tParams.getKey().getKey();
        tweakBytes = tParams.getTweak();
    } else if (params instanceof KeyParameter) {
        keyBytes = ((KeyParameter) params).getKey();
        tweakBytes = null;
    } else {
        throw new IllegalArgumentException("Invalid parameter passed to Threefish init - " + params.getClass().getName());
    }
    long[] keyWords = null;
    long[] tweakWords = null;
    if (keyBytes != null) {
        if (keyBytes.length != this.blocksizeBytes) {
            throw new IllegalArgumentException("Threefish key must be same size as block (" + blocksizeBytes + " bytes)");
        }
        keyWords = new long[blocksizeWords];
        for (int i = 0; i < keyWords.length; i++) {
            keyWords[i] = bytesToWord(keyBytes, i * 8);
        }
    }
    if (tweakBytes != null) {
        if (tweakBytes.length != TWEAK_SIZE_BYTES) {
            throw new IllegalArgumentException("Threefish tweak must be " + TWEAK_SIZE_BYTES + " bytes");
        }
        tweakWords = new long[] { bytesToWord(tweakBytes, 0), bytesToWord(tweakBytes, 8) };
    }
    init(forEncryption, keyWords, tweakWords);
}
Also used : TweakableBlockCipherParameters(com.github.zhenwei.core.crypto.params.TweakableBlockCipherParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Aggregations

KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)1 TweakableBlockCipherParameters (com.github.zhenwei.core.crypto.params.TweakableBlockCipherParameters)1