[ https://issues.apache.org/jira/browse/SSHD-506?focusedWorklogId=436573&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-436573 ]
ASF GitHub Bot logged work on SSHD-506: --------------------------------------- Author: ASF GitHub Bot Created on: 22/May/20 15:58 Start Date: 22/May/20 15:58 Worklog Time Spent: 10m Work Description: lgoldstein commented on a change in pull request #132: URL: https://github.com/apache/mina-sshd/pull/132#discussion_r429330029 ########## File path: sshd-common/src/main/java/org/apache/sshd/common/cipher/BaseGCMCipher.java ########## @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sshd.common.cipher; + +import java.security.spec.AlgorithmParameterSpec; + +import javax.crypto.Cipher; +import javax.crypto.spec.GCMParameterSpec; + +import org.apache.sshd.common.util.buffer.Buffer; +import org.apache.sshd.common.util.buffer.ByteArrayBuffer; + +public class BaseGCMCipher extends BaseAEADCipher { + + public BaseGCMCipher( + int ivsize, int authSize, int kdfSize, String algorithm, int keySize, String transformation, + int blkSize) { + super(ivsize, authSize, kdfSize, algorithm, keySize, transformation, blkSize); + } + + @Override + protected AlgorithmParameterSpec initializeAlgorithmParameters(byte[] iv) { + Buffer buffer = new ByteArrayBuffer(iv); + buffer.rpos(Integer.BYTES); + long ic = buffer.getLong(); + // decrement IV as it will be incremented back to initial value on first call to updateWithAAD + ic = (ic - 1) & 0x0ffffffffL; + buffer.wpos(Integer.BYTES); + buffer.putLong(ic); + return new GCMParameterSpec(getAuthenticationTagSize() * Byte.SIZE, buffer.array()); + } + + @Override + protected AlgorithmParameterSpec getNextAlgorithmParameters() { + Cipher cipher = getCipherInstance(); + Buffer iv = new ByteArrayBuffer(cipher.getIV()); + iv.rpos(Integer.BYTES); + long ic = iv.getLong(); + ic = (ic + 1) & 0x0ffffffffL; + iv.wpos(Integer.BYTES); + iv.putLong(ic); + return new GCMParameterSpec(getAuthenticationTagSize() * Byte.SIZE, iv.array()); + } Review comment: I believe we can avoid creating the `Buffer` since all the code does is replace an 8-byte section of the IV at offset 4: ```java byte[] iv = cipher.getIV(); // Need to write decodeLong but it should be similar to decodeInt in the same class // can use the code of Buffer#getLong as base long ic = KeyEntryResolver.decodeLong(iv, Integer.BYTES, iv.length - Integer.BYTES); ic = (ic + delta) & 0x0ffffffffL; // Need to write it - can use the code of Buffer#putLong as base KeyEntryResolver.encodeLong(iv, ic, Integer.BYTES, iv.length - Integer.BYTES); ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 436573) Time Spent: 2h 20m (was: 2h 10m) > Add support for aes128/256-gcm ciphers > -------------------------------------- > > Key: SSHD-506 > URL: https://issues.apache.org/jira/browse/SSHD-506 > Project: MINA SSHD > Issue Type: Improvement > Reporter: Lyor Goldstein > Priority: Major > Time Spent: 2h 20m > Remaining Estimate: 0h > > See: > * [rfc5647|https://tools.ietf.org/html/rfc5647] > * > [draft-igoe-secsh-aes-gcm-01|https://tools.ietf.org/html/draft-igoe-secsh-aes-gcm-01] > * [OpenSSH v6.2|http://www.openssh.com/txt/release-6.2] > * [JAVA AES 256 GCM encrypt/decrypt > example|https://javainterviewpoint.com/java-aes-256-gcm-encryption-and-decryption/] > - especially the usage of {{GCMParameterSpec}} to initialize the cipher > * [OpenJDK 8 AESCipher.java source > code|https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/com/sun/crypto/provider/AESCipher.java] > ** See also > [CipherCore.java|https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/com/sun/crypto/provider/CipherCore.java], > > [FeedbackCipher.java|https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/com/sun/crypto/provider/FeedbackCipher.java], > > [GaloisCounterMode.java|https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/com/sun/crypto/provider/GaloisCounterMode.java] -- This message was sent by Atlassian Jira (v8.3.4#803005) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@mina.apache.org For additional commands, e-mail: dev-h...@mina.apache.org