public FrameCodec(EncryptionHandshake.Secrets secrets) { this.mac = secrets.mac; BlockCipher cipher; enc = new SICBlockCipher(cipher = new AESEngine()); enc.init(true, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[cipher.getBlockSize()])); dec = new SICBlockCipher(cipher = new AESEngine()); dec.init(false, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[cipher.getBlockSize()])); egressMac = secrets.egressMac; ingressMac = secrets.ingressMac; }
System.arraycopy(headerData, 0, headBuffer, 3, headerData.length); enc.processBytes(headBuffer, 0, 16, headBuffer, 0); enc.processBytes(ptype, 0, ptype.length, buff, 0); out.write(buff, 0, ptype.length); egressMac.update(buff, 0, ptype.length); int n = frame.payload.read(buff); if (n <= 0) break; enc.processBytes(buff, 0, n, buff, 0); egressMac.update(buff, 0, n); out.write(buff, 0, n); byte[] pad = new byte[16]; if (padding < 16) { enc.processBytes(pad, 0, padding, buff, 0); egressMac.update(buff, 0, padding); out.write(buff, 0, padding);
static void prg(byte[] r, int rOff, long rlen, byte[] key, int keyOff) { byte[] nonce = new byte[8]; StreamCipher cipher = new ChaChaEngine(12); cipher.init(true, new ParametersWithIV(new KeyParameter(key, keyOff, 32), nonce)); cipher.processBytes(r, rOff, (int)rlen, r, rOff); //crypto_stream_chacha12(r, rlen, nonce, key); } }
protected int engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) { if (inputLen != 0) { cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); } cipher.reset(); return inputLen; }
param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName()); pbeSpec = (PBEParameterSpec)params; cipher.init(true, param); break; case Cipher.DECRYPT_MODE: case Cipher.UNWRAP_MODE: cipher.init(false, param); break; default:
/** * Writes the specified byte to this output stream. * * @param b the <code>byte</code>. * @throws java.io.IOException if an I/O error occurs. */ public void write( int b) throws IOException { oneByte[0] = (byte)b; if (streamCipher != null) { out.write(streamCipher.returnByte((byte)b)); } else { write(oneByte, 0, 1); } }
public void doChaCha(boolean encrypt, InputStream is, OutputStream os, byte[] key, byte[] iv) throws IOException { CipherParameters cp = new KeyParameter(key); ParametersWithIV params = new ParametersWithIV(cp, iv); StreamCipher engine = new ChaChaEngine(); engine.init(encrypt, params); byte in[] = new byte[8192]; byte out[] = new byte[8192]; int len = 0; while(-1 != (len = is.read(in))) { len = engine.processBytes(in, 0 , len, out, 0); os.write(out, 0, len); } } public void encChaCha(InputStream is, OutputStream os, byte[] key, byte[] iv) throws IOException { doChaCha(true, is, os, key, iv); } public void decChaCha(InputStream is, OutputStream os, byte[] key, byte[] iv) throws IOException { doChaCha(false, is, os, key, iv); }
protected int engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) { if (inputLen != 0) { cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); } cipher.reset(); return inputLen; } }
param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName()); pbeSpec = (PBEParameterSpec)params; cipher.init(true, param); break; case Cipher.DECRYPT_MODE: case Cipher.UNWRAP_MODE: cipher.init(false, param); break; default:
/** * Writes the specified byte to this output stream. * * @param b the <code>byte</code>. * @exception java.io.IOException if an I/O error occurs. */ public void write( int b) throws IOException { oneByte[0] = (byte)b; if (bufferedBlockCipher != null) { int len = bufferedBlockCipher.processBytes(oneByte, 0, 1, buf, 0); if (len != 0) { out.write(buf, 0, len); } } else { out.write(streamCipher.returnByte((byte)b)); } }
dec.processBytes(headBuffer, 0, 16, headBuffer, 0); totalBodySize = headBuffer[0] & 0xFF; totalBodySize = (totalBodySize << 8) + (headBuffer[1] & 0xFF); dec.processBytes(buffer, 0, frameSize, buffer, 0); int pos = 0; long type = RLP.decodeLong(buffer, pos);
protected void updateIV(StreamCipher cipher, boolean forEncryption, long seqNo) { byte[] nonce = new byte[8]; TlsUtils.writeUint64(seqNo, nonce, 0); cipher.init(forEncryption, new ParametersWithIV(null, nonce)); } }
protected int engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException { if (outputOffset + inputLen > output.length) { throw new ShortBufferException("output buffer too short for input."); } if (inputLen != 0) { cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); } cipher.reset(); return inputLen; } }
param = PBE.Util.makePBEParameters(k.getEncoded(), PKCS12, digest, keySizeInBits, ivLength * 8, pbeSpec, cipher.getAlgorithmName()); param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName()); pbeSpec = (PBEParameterSpec)params; cipher.init(true, param); break; case Cipher.DECRYPT_MODE: case Cipher.UNWRAP_MODE: cipher.init(false, param); break; default:
public byte[] encodePlaintext(long seqNo, short type, byte[] plaintext, int offset, int len) { if (usesNonce) { updateIV(encryptCipher, true, seqNo); } byte[] outBuf = new byte[len + writeMac.getSize()]; encryptCipher.processBytes(plaintext, offset, len, outBuf, 0); byte[] mac = writeMac.calculateMac(seqNo, type, plaintext, offset, len); encryptCipher.processBytes(mac, 0, mac.length, outBuf, len); return outBuf; }
protected KeyParameter initRecord(StreamCipher cipher, boolean forEncryption, long seqNo, byte[] iv) { byte[] nonce = calculateNonce(seqNo, iv); cipher.init(forEncryption, new ParametersWithIV(null, nonce)); return generateRecordMACKey(cipher); }
streamCipher.reset();