@Test public void testEncryptTls10WithEncryptThenMacServer() throws NoSuchAlgorithmException, CryptoException { context.setConnection(new InboundConnection()); context.setSelectedCipherSuite(CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA); context.setSelectedProtocolVersion(ProtocolVersion.TLS10); context.addNegotiatedExtension(ExtensionType.ENCRYPT_THEN_MAC); byte[] data = ArrayConverter .hexStringToByteArray("1400000CCE92FBEC9131F48A63FED31F71573F726479AA9108FB86A4FA16BC1D5CB5753003030303"); KeySet keySet = new KeySet(); keySet.setServerWriteKey(ArrayConverter .hexStringToByteArray("65B7DA726864D4184D75A549BF5C06AB20867846AF4434CC")); keySet.setServerWriteMacSecret(ArrayConverter.hexStringToByteArray("183612323C5507EDAA5BF0DE71272A2EA87B1165")); keySet.setServerWriteIv(ArrayConverter.hexStringToByteArray("1ACF314DA7208EB8")); keySet.setClientWriteIv(new byte[8]); keySet.setClientWriteKey(new byte[24]); keySet.setClientWriteMacSecret(new byte[20]); cipher = new RecordBlockCipher(context, keySet); Record record = new Record(); record.setContentType(ProtocolMessageType.HANDSHAKE.getValue()); record.prepareComputations(); record.setSequenceNumber(new BigInteger("0")); record.setCleanProtocolMessageBytes(data); record.setProtocolVersion(ProtocolVersion.TLS10.getValue()); cipher.encrypt(record); assertNull(record.getComputations().getExplicitNonce()); assertNull(record.getComputations().getAeadSalt()); assertNull(record.getComputations().getGcmNonce()); assertNull(record.getComputations().getAuthenticationTag()); assertNull(record.getComputations().getAuthenticationTagValid()); assertArrayEquals( ArrayConverter .hexStringToByteArray("C34B06D54CDE2A5AF25EE0AE1896F6F149720FA9EC205C6629B2C7F52A7F3A72931E351D4AD26E23A5B8C98C4E2DE362"), record.getComputations().getCiphertext().getValue()); assertArrayEquals(ArrayConverter.hexStringToByteArray("00000000000000001603010030"), record.getComputations() .getAuthenticatedMetaData().getValue()); assertArrayEquals( ArrayConverter .hexStringToByteArray("C34B06D54CDE2A5AF25EE0AE1896F6F149720FA9EC205C6629B2C7F52A7F3A72931E351D4AD26E23A5B8C98C4E2DE362"), record.getComputations().getAuthenticatedNonMetaData().getValue()); assertArrayEquals(ArrayConverter.hexStringToByteArray("1ACF314DA7208EB8"), record.getComputations() .getCbcInitialisationVector().getValue()); assertArrayEquals(ArrayConverter.hexStringToByteArray("65B7DA726864D4184D75A549BF5C06AB20867846AF4434CC"), record.getComputations().getCipherKey().getValue()); assertArrayEquals(ArrayConverter.hexStringToByteArray("183612323C5507EDAA5BF0DE71272A2EA87B1165"), record .getComputations().getMacKey().getValue()); assertArrayEquals(ArrayConverter.hexStringToByteArray("1A9BCCDD712329663F4065FA0E178F7A434676BE"), record .getComputations().getMac().getValue()); assertArrayEquals(ArrayConverter.hexStringToByteArray("0707070707070707"), record.getComputations() .getPadding().getValue()); assertArrayEquals( ArrayConverter .hexStringToByteArray("1400000CCE92FBEC9131F48A63FED31F71573F726479AA9108FB86A4FA16BC1D5CB57530030303030707070707070707"), record.getComputations().getPlainRecordBytes().getValue()); assertArrayEquals( ArrayConverter .hexStringToByteArray("C34B06D54CDE2A5AF25EE0AE1896F6F149720FA9EC205C6629B2C7F52A7F3A72931E351D4AD26E23A5B8C98C4E2DE3621A9BCCDD712329663F4065FA0E178F7A434676BE"), record.getProtocolMessageBytes().getValue()); assertTrue(record.getComputations().getMacValid()); assertTrue(record.getComputations().getPaddingValid()); }
private byte[] encrypt(byte[] plaintext, byte[] iv) throws CryptoException { byte[] expandedPlaintext = this.expandToBlocksize(plaintext); byte[] ciphertext = encryptCipher.encrypt(iv, expandedPlaintext); if (!useExplicitIv) { encryptCipher.setIv(extractNextEncryptIv(ciphertext)); } LOGGER.debug("EncryptIv: " + ArrayConverter.bytesToHexString(encryptCipher.getIv())); return ciphertext; }
RecordBlockCipher extends RecordCipher { private byte[] encrypt(byte[] plaintext, byte[] iv) throws CryptoException { byte[] expandedPlaintext = this.expandToBlocksize(plaintext); byte[] ciphertext = encryptCipher.encrypt(iv, expandedPlaintext); if (!useExplicitIv) { encryptCipher.setIv(extractNextEncryptIv(ciphertext)); } LOGGER.debug("EncryptIv: " + ArrayConverter.bytesToHexString(encryptCipher.getIv())); return ciphertext; } }
RecordBlockCipher extends RecordCipher { private byte[] encrypt(byte[] plaintext, byte[] iv) throws CryptoException { byte[] expandedPlaintext = this.expandToBlocksize(plaintext); byte[] ciphertext = encryptCipher.encrypt(iv, expandedPlaintext); if (!useExplicitIv) { encryptCipher.setIv(extractNextEncryptIv(ciphertext)); } LOGGER.debug("EncryptIv: " + ArrayConverter.bytesToHexString(encryptCipher.getIv())); return ciphertext; } RecordBlockCipher(TlsContext context, KeySet keySet); }
RecordBlockCipher extends RecordCipher { private byte[] encrypt(byte[] plaintext, byte[] iv) throws CryptoException { byte[] expandedPlaintext = this.expandToBlocksize(plaintext); byte[] ciphertext = encryptCipher.encrypt(iv, expandedPlaintext); if (!useExplicitIv) { encryptCipher.setIv(extractNextEncryptIv(ciphertext)); } LOGGER.debug("EncryptIv: " + ArrayConverter.bytesToHexString(encryptCipher.getIv())); return ciphertext; } RecordBlockCipher(TlsContext context, KeySet keySet); byte[] calculatePadding(int paddingLength); int calculatePaddingLength(Record record, int dataLength); byte[] getEncryptionIV(); @Override void encrypt(Record record); @Override void decrypt(Record record); @Override void encrypt(BlobRecord br); @Override void decrypt(BlobRecord br); }
RecordBlockCipher extends RecordCipher { private byte[] encrypt(byte[] plaintext, byte[] iv) throws CryptoException { byte[] expandedPlaintext = this.expandToBlocksize(plaintext); byte[] ciphertext = encryptCipher.encrypt(iv, expandedPlaintext); if (!useExplicitIv) { encryptCipher.setIv(extractNextEncryptIv(ciphertext)); } LOGGER.debug("EncryptIv: " + ArrayConverter.bytesToHexString(encryptCipher.getIv())); return ciphertext; } RecordBlockCipher(TlsContext context, KeySet keySet); byte[] calculatePadding(int paddingLength); int calculatePaddingLength(Record record, int dataLength); byte[] getEncryptionIV(); @Override void encrypt(Record record); @Override void decrypt(Record record); @Override void encrypt(BlobRecord br); @Override void decrypt(BlobRecord br);  }