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



##########
File path: 
sshd-common/src/main/java/org/apache/sshd/common/cipher/BaseAEADCipher.java
##########
@@ -0,0 +1,94 @@
+/*
+ * 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 org.apache.sshd.common.util.security.SecurityUtils;
+
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.spec.AlgorithmParameterSpec;
+
+public abstract class BaseAEADCipher extends BaseCipher {
+
+    private Mode mode;
+    private SecretKey secretKey;
+    private AlgorithmParameterSpec params;
+    private boolean aadWritten;
+
+    public BaseAEADCipher(
+            int ivsize, int authSize, int kdfSize, String algorithm, int 
keySize, String transformation, int blkSize) {
+        super(ivsize, authSize, kdfSize, algorithm, keySize, transformation, 
blkSize);
+    }
+
+    @Override
+    protected byte[] initializeIVData(final Mode mode, final byte[] iv, final 
int reqLen) {
+        byte[] data = super.initializeIVData(mode, iv, reqLen);
+        params = initializeAlgorithmParameters(data);
+        return data;
+    }
+
+    @Override
+    protected byte[] initializeKeyData(final Mode mode, final byte[] key, 
final int reqLen) {
+        byte[] keyData = super.initializeKeyData(mode, key, reqLen);
+        secretKey = new SecretKeySpec(keyData, getAlgorithm());
+        return keyData;
+    }
+
+    @Override
+    protected Cipher createCipherInstance(final Mode mode, final byte[] key, 
final byte[] iv) throws Exception {
+        this.mode = mode;
+        Cipher instance = SecurityUtils.getCipher(getTransformation());
+        init(instance);
+        return instance;
+    }
+
+    private void init(Cipher cipher) throws InvalidKeyException, 
InvalidAlgorithmParameterException {

Review comment:
       Probably makes sense. I was trying to refactor this class a little so 
that supporting ChaCha20-Poly1305 later would be simpler since it's another 
AEAD algorithm, though it uses a slightly different key derivation algorithm in 
order to encrypt the packet length as well (wonder if an update or extension to 
AES/GCM can be made to do the same eventually).




----------------------------------------------------------------
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



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

Reply via email to