private void rekey(byte[] key) { cipher_ctx.initialize(key); counter = new byte[allZeroString.length]; cipher_ctx.encipher(allZeroString, counter); Arrays.fill(key, (byte) 0); }
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 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; }
/** * Create PCFBMode object for this key */ PCFBMode makeCipher(byte[] iv, byte[] key) { byte[] iv2 = new byte[0x20]; // 256 bits System.arraycopy(salt, 0, iv2, 0, 0x10); System.arraycopy(iv, 0, iv2, 0x10, 0x10); try { BlockCipher aes = new Rijndael(256, 256); aes.initialize(key); return PCFBMode.create(aes, iv2); } catch (UnsupportedCipherException e) { Logger.error(this, "Rijndael not supported!", e); throw new Error("Rijndael not supported!", e); } }
(c.getBlockSize() >> 3) + // IV inputOffset += HASH_LENGTH; c.initialize(pn.jfkKe); int ivLength = PCFBMode.lengthIV(c); int decypheredPayloadOffset = 0; outgoingCipher.initialize(pn.outgoingKey); incommingCipher.initialize(pn.incommingKey); ivCipher.initialize(pn.ivKey);
throw new Error("Impossible: no Rijndael(256,128): "+e, e); cipher.initialize(masterKey); diskSalt = new byte[0x10]; cipher.encipher(newsalt, diskSalt); if(logDEBUG) Logger.debug(this, "Encrypting with "+HexUtil.bytesToHex(newsalt)+" from "+HexUtil.bytesToHex(diskSalt)); throw new Error("Impossible: no Rijndael(256,128): "+e, e); cipher.initialize(masterKey); salt = new byte[0x10]; cipher.decipher(diskSalt, salt); if(logDEBUG) Logger.debug(this, "Encrypting (new) with "+HexUtil.bytesToHex(salt)+" from "+HexUtil.bytesToHex(diskSalt));
protected void refillBuffer() { // Encrypt feedback into result c.encipher(feedback_register, feedback_register); registerPointer=0; }
protected PCFBMode(BlockCipher c) { this.c = c; feedback_register = new byte[c.getBlockSize() >> 3]; registerPointer = feedback_register.length; }
} else if(type.cipherName.equals("RIJNDAEL")){ if(offset == 0 && len == input.length && outputOffset == 0 && len == output.length) blockCipher.decipher(input, output); else { byte[] result = new byte[len]; blockCipher.decipher(Arrays.copyOfRange(input, offset, offset+len), result); System.arraycopy(result, 0, output, outputOffset, len);
cipher.initialize(outerKey); PCFBMode pcfb = PCFBMode.create(cipher, iv); pcfb.blockEncipher(data, hashedStart, data.length - hashedStart);
(c.getBlockSize() >> 3) + // IV c.initialize(pn.jfkKe); int ivLength = PCFBMode.lengthIV(c); byte[] iv = new byte[ivLength];
/** 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; }
/** * returns the length of the IV for a PCFB created with a specific cipher. */ public static int lengthIV(BlockCipher c) { return c.getBlockSize() >> 3; }
if(type.cipherName.equals("RIJNDAEL")){ blockCipher = new Rijndael(type.keyType.keySize, type.blockSize); blockCipher.initialize(key.getEncoded()); if(type == CryptByteBufferType.RijndaelPCFB){ encryptPCFB = PCFBMode.create(blockCipher, this.iv.getIV());
(c.getBlockSize() >> 3) + // IV Logger.minor(this, "Their initial message ID: "+theirInitialMsgID+" ours "+ourInitialMsgID); c.initialize(Ke); int ivLength = PCFBMode.lengthIV(c); int decypheredPayloadOffset = 0; throw new RuntimeException(e); outgoingCipher.initialize(outgoingKey); incommingCipher.initialize(incommingKey); ivCipher.initialize(ivKey);
/** 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; }
} 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);
public int getBlockSize() { return cipher.getBlockSize(); }
cipher.initialize(outerKey); PCFBMode pcfb = PCFBMode.create(cipher, iv); pcfb.blockDecipher(dataAndHash, 0, dataAndHash.length);
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); ivCipher.encipher(IV, IV);