outputOffset += cipher.doFinal(output, outputOffset);
outputOffset += cipher.doFinal(output, outputOffset);
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { // Impossible in encrypt mode throw new IllegalStateException(e.toString()); } }
public int doFinal(byte[] out, int outOff) throws DataLengthException, IllegalStateException { try { return cipher.doFinal(out, outOff); } catch (InvalidCipherTextException e) { // Impossible in encrypt mode throw new IllegalStateException(e.toString()); } }
bytesEncrypted = realCipher.doFinal(encBuffer, 0); out.write(encBuffer, 0, bytesEncrypted);
public static byte[] decrypt(byte[] cipheredApdu, byte[] systemTitle, SecuritySuite securitySuite) throws IOException { byte[] iv = ByteBuffer.allocate(IV_LENGTH).put(systemTitle).put(cipheredApdu, 1, FC_LENGTH).array(); byte[] additionalAssociationData = createAadIfExists(securitySuite); AEADParameters parameters = new AEADParameters(new KeyParameter(securitySuite.getGlobalUnicastEncryptionKey()), IV_LENGTH_BITS, iv, additionalAssociationData); GCMBlockCipher decCipher = createBlockCipher(false, parameters); byte[] dec = new byte[decCipher.getOutputSize(cipheredApdu.length - 5)]; int length = decCipher.processBytes(cipheredApdu, 5, cipheredApdu.length - 5, dec, 0); try { decCipher.doFinal(dec, length); } catch (IllegalStateException | InvalidCipherTextException e) { throw new IOException("Unable to decipher/decrypt xDLMS pdu", e); } return dec; }
outputOffset += cipher.doFinal(output, outputOffset); } catch (InvalidCipherTextException e) { throw new RuntimeException("Couldn't validate GCM authentication tag: " + e.getMessage(), e);
outputOffset += cipher.doFinal(output, outputOffset); } catch (InvalidCipherTextException e) { throw new RuntimeException("Couldn't validate GCM authentication tag: " + e.getMessage(), e);
public static byte[] processPlain(byte[] plaintext, int off, int len, byte[] systemTitle, int frameCounter, SecuritySuite securitySuite, byte apduTag) throws IOException { byte[] frameCounterBytes = ByteBuffer.allocate(4).putInt(frameCounter).array(); byte[] iv = ByteBuffer.allocate(systemTitle.length + 4).put(systemTitle).putInt(frameCounter).array(); byte[] additionalAssociationData = createAadIfExists(securitySuite); AEADParameters parameters = new AEADParameters(new KeyParameter(securitySuite.getGlobalUnicastEncryptionKey()), IV_LENGTH_BITS, iv, additionalAssociationData); GCMBlockCipher encCipher = createBlockCipher(true, parameters); byte[] chipherText = new byte[encCipher.getOutputSize(len)]; int length = encCipher.processBytes(plaintext, off, len, chipherText, 0); try { encCipher.doFinal(chipherText, length); } catch (IllegalStateException | InvalidCipherTextException e) { throw new IOException("Unable to cipher/encrypt xDLMS APDU", e); } int chipherTextLength = chipherText.length; if (!securitySuite.getSecurityPolicy().isAuthenticated()) { chipherTextLength -= 12; } byte[] lengthBytes = AxdrLength.encodeLength(chipherTextLength + 5); final int cipheredApduLength = chipherTextLength + LENGTH_FC_TAG_SC + lengthBytes.length; byte controlByte = securityControlByteFrom(securitySuite); return ByteBuffer.allocate(cipheredApduLength) .put(apduTag) // COSEM PDU type. e.g. GLO_GET_REQUEST .put(lengthBytes) .put(controlByte) .put(frameCounterBytes) .put(chipherText, 0, chipherTextLength) .array(); }
outputOffset += cipher.doFinal(output, outputOffset); } catch (InvalidCipherTextException e) { throw new RuntimeException("Couldn't generate GCM authentication tag: " + e.getMessage(), e);
outputOffset += cipher.doFinal(output, outputOffset); } catch (InvalidCipherTextException e) { throw new RuntimeException("Couldn't generate GCM authentication tag: " + e.getMessage(), e);
outputOffset += cipher.doFinal(output, outputOffset);
outputOffset += cipher.doFinal(output, outputOffset);
outputOffset += cipher.doFinal(output, outputOffset);
outputOffset += cipher.doFinal(output, outputOffset);
public static byte[] decrypt(byte[] key, byte[] data) { // TODO utilize GCMAES#decrypt method try { if (data.length < NONCE_LENGTH + TAG_LENGTH) { throw new IllegalArgumentException("data packet too short"); } int cipherTextLength = data.length - NONCE_LENGTH - TAG_LENGTH; byte[] nonce = Arrays.copyOf(data, NONCE_LENGTH); GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(key), TAG_LENGTH * 8, nonce); cipher.init(false, parameters); byte[] out = new byte[cipher.getOutputSize(cipherTextLength + TAG_LENGTH)]; int pos = cipher.processBytes(data, NONCE_LENGTH, data.length - NONCE_LENGTH, out, 0); pos += cipher.doFinal(out, pos); return Arrays.copyOf(out, pos); } catch (IllegalStateException | InvalidCipherTextException ex) { throw new IllegalArgumentException(ex); } } }
pos += cipher.doFinal(out, pos);