/** * returns the length of the IV for a PCFB created with a specific cipher. */ public static int lengthIV(BlockCipher c) { return c.getBlockSize() >> 3; }
protected PCFBMode(BlockCipher c) { this.c = c; feedback_register = new byte[c.getBlockSize() >> 3]; registerPointer = feedback_register.length; }
public int getBlockSize() { return cipher.getBlockSize(); }
/** * Basic constructor. * * @param c the block cipher to be used. */ public CTRBlockCipher(BlockCipher c) { this.cipher = c; this.blockSize = cipher.getBlockSize()/8; this.IV = new byte[blockSize]; this.counter = new byte[blockSize]; this.counterOut = new byte[blockSize]; this.blockOffset = IV.length; }
private void generator_init(String cipher) { cipher_ctx = Util.getCipherByName(cipher); output_buffer = new byte[cipher_ctx.getBlockSize() / 8]; counter = new byte[cipher_ctx.getBlockSize() / 8]; allZeroString = new byte[cipher_ctx.getBlockSize() / 8]; tmp = new byte[cipher_ctx.getKeySize() / 8]; fetch_counter = output_buffer.length; }
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; }
/** 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; }
byte[] IV = new byte[ivCipher.getBlockSize() / 8]; System.arraycopy(sessionKey.ivNonce, 0, IV, 0, IV.length); System.arraycopy(data, HMAC_LENGTH, IV, IV.length - 4, 4);
(c.getBlockSize() >> 3) + // IV
(c.getBlockSize() >> 3) + // IV
(c.getBlockSize() >> 3) + // IV