You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by GitBox <gi...@apache.org> on 2020/06/14 16:12:25 UTC

[GitHub] [mina-sshd] lgoldstein commented on a change in pull request #132: [SSHD-506] Add support for RFC 5647

lgoldstein commented on a change in pull request #132:
URL: https://github.com/apache/mina-sshd/pull/132#discussion_r439844394



##########
File path: sshd-common/src/main/java/org/apache/sshd/common/cipher/BaseGCMCipher.java
##########
@@ -0,0 +1,104 @@
+/*
+ * 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 javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.GCMParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.apache.sshd.common.util.buffer.BufferUtils;
+import org.apache.sshd.common.util.security.SecurityUtils;
+
+public class BaseGCMCipher extends BaseCipher {
+
+    private Mode mode;
+    private CounterGCMParameterSpec parameters;
+    private SecretKey secretKey;

Review comment:
       Let's make them `protected` in the spirit of open-source and allowing others to extend this class if they want/need to.

##########
File path: sshd-common/src/main/java/org/apache/sshd/common/cipher/BaseGCMCipher.java
##########
@@ -0,0 +1,104 @@
+/*
+ * 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 javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.GCMParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.apache.sshd.common.util.buffer.BufferUtils;
+import org.apache.sshd.common.util.security.SecurityUtils;
+
+public class BaseGCMCipher extends BaseCipher {
+
+    private Mode mode;
+    private CounterGCMParameterSpec parameters;
+    private SecretKey secretKey;
+
+    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 Cipher createCipherInstance(Mode mode, byte[] key, byte[] iv) throws Exception {
+        this.mode = mode;
+        secretKey = new SecretKeySpec(key, getAlgorithm());
+        parameters = new CounterGCMParameterSpec(getAuthenticationTagSize() * Byte.SIZE, iv);
+        parameters.decrementCounter();
+        return SecurityUtils.getCipher(getTransformation());
+    }
+
+    @Override
+    public void update(byte[] input, int inputOffset, int inputLen) throws Exception {
+        updateWithAAD(input, inputOffset, 0, inputLen);
+    }
+
+    @Override
+    public void updateWithAAD(byte[] input, int offset, int aadLen, int inputLen) throws Exception {
+        Cipher cipher = getCipherInstance();
+        parameters.incrementCounter();
+        cipher.init(mode == Mode.Encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey, parameters);
+        if (aadLen > 0) {
+            cipher.updateAAD(input, offset, aadLen);
+        }
+        int inputOffset = offset + aadLen;
+        if (mode == Mode.Decrypt) {
+            inputLen += getAuthenticationTagSize();
+        }
+        cipher.doFinal(input, inputOffset, inputLen, input, inputOffset);
+    }
+
+    /**
+     * Algorithm parameters for AES/GCM that assumes the IV uses an 8-byte counter field as its most significant bytes.
+     */
+    protected static class CounterGCMParameterSpec extends GCMParameterSpec {
+        private final byte[] iv;

Review comment:
       Let's make it`protected` in the spirit of open-source and allowing others to extend this class if they want/need to.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org