protected void refillBuffer() { // Encrypt feedback into result c.encipher(feedback_register, feedback_register); registerPointer=0; }
/** Encrypt counter to counterOut, and then increment counter. */ private void processBlock() throws IllegalStateException { // Our ciphers clobber the input array, so it is essential to copy // the counter to counterOut and then encrypt in-place. System.arraycopy(counter, 0, counterOut, 0, counter.length); cipher.encipher(counterOut, counterOut); // Now increment counter. for (int i = counter.length; i-- > 0 && (++counter[i]) == (byte)0;) { /* nothing here */ } blockOffset = 0; }
private void rekey(byte[] key) { cipher_ctx.initialize(key); counter = new byte[allZeroString.length]; cipher_ctx.encipher(allZeroString, counter); Arrays.fill(key, (byte) 0); }
} else if(type.cipherName.equals("RIJNDAEL")){ if(offset == 0 && len == input.length && outputOffset == 0 && len == output.length) blockCipher.encipher(input, output); else { byte[] result = new byte[len]; blockCipher.encipher(Arrays.copyOfRange(input, offset, offset+len), result); System.arraycopy(result, 0, output, outputOffset, len);
static byte[] encryptSequenceNumber(int seqNum, SessionKey sessionKey) { byte[] seqNumBytes = new byte[4]; seqNumBytes[0] = (byte) (seqNum >>> 24); seqNumBytes[1] = (byte) (seqNum >>> 16); seqNumBytes[2] = (byte) (seqNum >>> 8); seqNumBytes[3] = (byte) (seqNum); BlockCipher ivCipher = sessionKey.ivCipher; byte[] IV = new byte[ivCipher.getBlockSize() / 8]; System.arraycopy(sessionKey.ivNonce, 0, IV, 0, IV.length); System.arraycopy(seqNumBytes, 0, IV, IV.length - seqNumBytes.length, seqNumBytes.length); ivCipher.encipher(IV, IV); PCFBMode cipher = PCFBMode.create(sessionKey.incommingCipher, IV); cipher.blockEncipher(seqNumBytes, 0, seqNumBytes.length); return seqNumBytes; }
private void generateOutput() { counterInc(); output_buffer = new byte[counter.length]; cipher_ctx.encipher(counter, output_buffer); if(output_count++ > Pg) { output_count = 0; nextBytes(tmp); rekey(tmp); } }
/** Must NOT modify buf contents. */ private NPFPacket decipherFromSeqnum(byte[] buf, int offset, int length, SessionKey sessionKey, int sequenceNumber) { BlockCipher ivCipher = sessionKey.ivCipher; byte[] IV = new byte[ivCipher.getBlockSize() / 8]; System.arraycopy(sessionKey.ivNonce, 0, IV, 0, IV.length); IV[IV.length - 4] = (byte) (sequenceNumber >>> 24); IV[IV.length - 3] = (byte) (sequenceNumber >>> 16); IV[IV.length - 2] = (byte) (sequenceNumber >>> 8); IV[IV.length - 1] = (byte) (sequenceNumber); ivCipher.encipher(IV, IV); byte[] payload = Arrays.copyOfRange(buf, offset + HMAC_LENGTH, offset + length); byte[] hash = Arrays.copyOfRange(buf, offset, offset + HMAC_LENGTH); byte[] localHash = Arrays.copyOf(HMAC.macWithSHA256(sessionKey.hmacKey, payload), HMAC_LENGTH); if (!MessageDigest.isEqual(hash, localHash)) { if (logMINOR) { Logger.minor(this, "Failed to validate the HMAC using TrackerID="+sessionKey.trackerID); } return null; } PCFBMode payloadCipher = PCFBMode.create(sessionKey.incommingCipher, IV); payloadCipher.blockDecipher(payload, 0, payload.length); NPFPacket p = NPFPacket.create(payload, pn); NewPacketFormatKeyContext keyContext = sessionKey.packetContext; synchronized(this) { if(seqNumGreaterThan(sequenceNumber, keyContext.highestReceivedSeqNum, 31)) { keyContext.highestReceivedSeqNum = sequenceNumber; } } return p; }
cipher.encipher(newsalt, diskSalt); if(logDEBUG) Logger.debug(this, "Encrypting with "+HexUtil.bytesToHex(newsalt)+" from "+HexUtil.bytesToHex(diskSalt));
System.arraycopy(data, HMAC_LENGTH, IV, IV.length - 4, 4); ivCipher.encipher(IV, IV);