Refine search
@Override public byte[] generate() throws LimitException { final byte[] result = new byte[cipher.getBlockSize()]; final byte[] nonce = NonceUtil.randomNonce(result.length); synchronized (cipher) { cipher.init(true, new KeyParameter(key.getEncoded())); cipher.processBlock(nonce, 0, result, 0); cipher.reset(); } return result; }
/** * return the algorithm name and mode. * * @return the name of the underlying algorithm followed by "/CBC". */ public String getAlgorithmName() { return cipher.getAlgorithmName() + "/CBC"; }
public void reset() { if (initialised) { engine.processBlock(this.iv, 0, ofbV, 0); } engine.reset(); byteCount = 0; }
private void ProcessBlock(byte[] input, int inOff, int len, byte[] output, int outOff) { for (int byteIndex = 0; byteIndex < counter.length; byteIndex++) { s[byteIndex] += counter[byteIndex]; } engine.processBlock(s, 0, buffer, 0); for (int byteIndex = 0; byteIndex < engine.getBlockSize(); byteIndex++) { output[outOff + byteIndex] = (byte)(buffer[byteIndex] ^ input[inOff + byteIndex]); } }
private void E(byte[] key, byte[] s, int sOff, byte[] in, int inOff) { cipher.init(true, new KeyParameter(key)); cipher.processBlock(in, inOff, s, sOff); }
private void initCipher(boolean forEncryption, BlockCipher cipher, byte[] key_block, int key_size, int key_offset, int iv_offset) { KeyParameter key_parameter = new KeyParameter(key_block, key_offset, key_size); ParametersWithIV parameters_with_iv = new ParametersWithIV(key_parameter, key_block, iv_offset, cipher.getBlockSize()); cipher.init(forEncryption, parameters_with_iv); }
protected byte calculateByte(byte b) { if (counter > 0 && counter % 1024 == 0) { BlockCipher base = cfbEngine.getUnderlyingCipher(); base.init(false, key); byte[] nextKey = new byte[32]; base.processBlock(C, 0, nextKey, 0); base.processBlock(C, 8, nextKey, 8); base.processBlock(C, 16, nextKey, 16); base.processBlock(C, 24, nextKey, 24); key = new KeyParameter(nextKey); base.init(true, key); byte[] iv = cfbEngine.getCurrentIV(); base.processBlock(iv, 0, iv, 0); cfbEngine.init(forEncryption, new ParametersWithIV(key, iv)); } counter++; return cfbEngine.calculateByte(b); }
/** * {@inheritDoc} */ public void init(byte[] key) { if (key.length != 16 && key.length != 24 && key.length != 32) throw new IllegalArgumentException("Not an AES key length"); cipher.init(true, new KeyParameter(key)); }
public AESCipherCBCnoPad(boolean forEncryption, byte[] key, byte[] initVector) { BlockCipher aes = new AESFastEngine(); cbc = new CBCBlockCipher(aes); KeyParameter kp = new KeyParameter(key); ParametersWithIV piv = new ParametersWithIV(kp, initVector); cbc.init(forEncryption, piv); }
@Override protected BufferedBlockCipherAdapter newCipher(final CiphertextHeader header, final boolean mode) { final BufferedBlockCipher cipher = blockCipherSpec.newInstance(); CipherParameters params = new KeyParameter(lookupKey(header.getKeyName()).getEncoded()); final String algName = cipher.getUnderlyingCipher().getAlgorithmName(); if (algName.endsWith("CBC") || algName.endsWith("OFB") || algName.endsWith("CFB")) { params = new ParametersWithIV(params, header.getNonce()); } cipher.init(mode, params); return new BufferedBlockCipherAdapter(cipher); } }
String decrypt(byte[] cryptoBytes, byte[] key, byte[] iv) { BlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); byte[] out = new byte[cipher.getOutputSize(cryptoBytes.length)]; int offset = cipher.processBytes(cryptoBytes, 0, cryptoBytes.length, out, 0); cipher.doFinal(out, offset); return new String(out); }
@Override public int processBlock(byte[] in, int inOff, byte[] out, int outOff) throws DataLengthException, IllegalStateException { if (key == null) { throw new IllegalStateException("not initialised"); } if (offset == 0) { byte[] iv = ivGenerator.apply(index); ParametersWithIV parameters = new ParametersWithIV(key, iv); cipher.init(forEncryption, parameters); } offset += getBlockSize(); if (offset == blockLength) { index++; offset = 0; } return cipher.processBlock(in, inOff, out, outOff); }
public void init( boolean forEncryption, //ignored by this CTR mode CipherParameters params) throws IllegalArgumentException { if (params instanceof ParametersWithIV) { ParametersWithIV ivParam = (ParametersWithIV)params; this.IV = Arrays.clone(ivParam.getIV()); if (blockSize < IV.length) { throw new IllegalArgumentException("CTR/SIC mode requires IV no greater than: " + blockSize + " bytes."); } int maxCounterSize = (8 > blockSize / 2) ? blockSize / 2 : 8; if (blockSize - IV.length > maxCounterSize) { throw new IllegalArgumentException("CTR/SIC mode requires IV of at least: " + (blockSize - maxCounterSize) + " bytes."); } // if null it's an IV changed only. if (ivParam.getParameters() != null) { cipher.init(true, ivParam.getParameters()); } reset(); } else { throw new IllegalArgumentException("CTR/SIC mode requires ParametersWithIV"); } }
public void init(boolean forEncryption, CipherParameters parameters) { if (!(parameters instanceof ParametersWithIV)) { throw new IllegalArgumentException("Invalid parameters passed"); } ParametersWithIV ivParam = (ParametersWithIV)parameters; parameters = ivParam.getParameters(); byte[] iv = ivParam.getIV(); /* * TODO We need to check what the rule is supposed to be for IVs that aren't exactly one block. * * Given general little-endianness, presumably a short IV should be right-padded with zeroes. */ if (iv.length != blockSize) { throw new IllegalArgumentException("Currently only support IVs of exactly one block"); } byte[] tweak = new byte[blockSize]; System.arraycopy(iv, 0, tweak, 0, blockSize); cipher.init(true, parameters); cipher.processBlock(tweak, 0, tweak, 0); cipher.init(forEncryption, parameters); Pack.littleEndianToLong(tweak, 0, tw_init); System.arraycopy(tw_init, 0, tw_current, 0, tw_init.length); counter = 0; }
/** * Constructs a new big endian SIC seekable block cipher mode. * * @param cipher The underlying block cipher to use. */ public SICSeekableBlockCipher(final BlockCipher cipher) { this.cipher = cipher; this.blockSize = cipher.getBlockSize(); this.IV = new byte[blockSize]; this.cipherIn = new byte[blockSize]; this.cipherOut = new byte[blockSize]; }
protected int processNonce(byte[] N) { byte[] nonce = new byte[16]; System.arraycopy(N, 0, nonce, nonce.length - N.length, N.length); nonce[0] = (byte)(macSize << 4); nonce[15 - N.length] |= 1; int bottom = nonce[15] & 0x3F; nonce[15] &= 0xC0; /* * When used with incrementing nonces, the cipher is only applied once every 64 inits. */ if (KtopInput == null || !Arrays.areEqual(nonce, KtopInput)) { byte[] Ktop = new byte[16]; KtopInput = nonce; hashCipher.processBlock(KtopInput, 0, Ktop, 0); System.arraycopy(Ktop, 0, Stretch, 0, 16); for (int i = 0; i < 8; ++i) { Stretch[16 + i] = (byte)(Ktop[i] ^ Ktop[i + 1]); } } return bottom; }
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException { this.initialised = true; if (params instanceof ParametersWithIV) { ParametersWithIV ivParam = (ParametersWithIV)params; byte[] iv = ivParam.getIV(); int diff = this.iv.length - iv.length; Arrays.fill(this.iv, (byte)0); System.arraycopy(iv, 0, this.iv, diff, iv.length); params = ivParam.getParameters(); } else { throw new IllegalArgumentException("invalid parameter passed"); } if (params != null) { engine.init(true, params); } reset(); }
public void update(byte in) { if (bufOff == buf.length) { cipher.processBlock(buf, 0, mac, 0); bufOff = 0; } buf[bufOff++] = in; }
@Override public void init( boolean forEncryption, // not used for CTR mode CipherParameters params) { ParametersWithIV ivParams = (ParametersWithIV) params; byte[] iv = ivParams.getIV(); System.arraycopy(iv, 0, IV, 0, IV.length); reset(); this.cipher.init(true, ivParams.getParameters()); }