-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SSHD-506] Add support for RFC 5647 #132
Closed
Closed
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dc39193
[SSHD-506] Add support for RFC 5647
jvz 8b2ef07
Update to almost work
jvz 85f89cf
Remove generated final keywords
jvz bd03dd2
Clean up API updates and add docs
jvz 3500697
Decrement IV before initial increment
jvz 77672f4
Merge branch 'master' into gcm-cipher-SSHD-506
jvz b7a3ec3
Optimize GCM IV usage
jvz 0b12782
Fix iv reuse exception
jvz 2de46fc
Make code less C-like to make PMD happy
jvz f874c3c
Re-add counter decrement
jvz 1de84ef
Fix tests
jvz 2bd1dc9
Merge branch 'master' into gcm-cipher-SSHD-506
jvz 42d4bed
Use safer arithmetic
jvz dc6f7cf
Merge branch 'master' into gcm-cipher-SSHD-506
jvz 0f37aec
Clean up AEAD API and add tests
jvz 953b999
Simplify code
jvz f797955
Make internals protected
jvz b0c814f
Simplify IV management
jvz cc5a53c
Split updateAAD into own method
jvz f4ca814
Merge branch 'master' into gcm-cipher-SSHD-506
jvz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
sshd-common/src/main/java/org/apache/sshd/common/cipher/BaseAEADCipher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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.InvalidAlgorithmParameterException; | ||
import java.security.InvalidKeyException; | ||
import java.security.spec.AlgorithmParameterSpec; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import org.apache.sshd.common.util.security.SecurityUtils; | ||
|
||
/** | ||
* Base cipher for authenticated encryption with associated data implementations. | ||
* | ||
* @see <a href="https://tools.ietf.org/html/rfc5116">RFC 5116</a> | ||
*/ | ||
public abstract class BaseAEADCipher extends BaseCipher { | ||
|
||
private Mode mode; | ||
private SecretKey secretKey; | ||
private AlgorithmParameterSpec params; | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* Initializes this cipher using the given JDK cipher and the previously configured secret key and algorithm | ||
* parameters. | ||
* | ||
* @param cipher the JDK cipher instance to initialize | ||
* @throws InvalidKeyException if this cipher's secret key is incompatible with the given cipher | ||
* @throws InvalidAlgorithmParameterException if this cipher's algorithm parameters are incompatible with the given | ||
* cipher | ||
*/ | ||
protected void init(Cipher cipher) throws InvalidKeyException, InvalidAlgorithmParameterException { | ||
cipher.init(mode == Mode.Encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey, params); | ||
} | ||
|
||
/** | ||
* Creates the specific algorithm parameters for the given input initialization vector. | ||
* | ||
* @param iv initialization vector provided at construction | ||
* @return this cipher's parameters | ||
*/ | ||
protected abstract AlgorithmParameterSpec initializeAlgorithmParameters(byte[] iv); | ||
|
||
/** | ||
* Returns the next algorithm parameters to use for encrypting or decrypting the next | ||
* {@link #updateWithAAD(byte[], int, int, int, int)}} operation. | ||
* | ||
* @return the parameters for the next updateWithAAD operation | ||
*/ | ||
protected abstract AlgorithmParameterSpec getNextAlgorithmParameters(); | ||
|
||
@Override | ||
public void updateWithAAD(byte[] input, int aadOffset, int aadLen, int inputOffset, int inputLen) throws Exception { | ||
Cipher cipher = getCipherInstance(); | ||
params = getNextAlgorithmParameters(); | ||
init(cipher); | ||
cipher.updateAAD(input, aadOffset, aadLen); | ||
cipher.doFinal(input, inputOffset, inputLen, input, inputOffset); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
sshd-common/src/main/java/org/apache/sshd/common/cipher/BaseGCMCipher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty expensive in memory - every time it is called (which is for every packet) a new
byte[]
(cipher.getIv()
returns a clone), newBuffer
and newGCMParameterSpec
instance is created. Can we do something about it ?Moreover, this code is exactly the same as
initializeAlgorithmParameters
except it does+1
instead of-1
- perhaps we could use an internal (protected)updateAlgorithmParameters(byte[] iv, long delta)
and call it once with+1
and once with-1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, nice find. Yes, we just need to update the 8-byte section of the buffer. The update algorithm parameters API idea sounds good to me; it's more in line with how OpenSSH is structured, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the
GCMParameterSpec
based on this link we might be able to execute an ugly hack via reflection API and manipulate the internal IV data directly instead of creating a new instance every time... will need to think about it some more - but only after this code is working.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I came up with a better idea: extend the class and don't clone the IV. Less safe, but doesn't continually create garbage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've mostly optimized away all the garbage here. I do an initial IV clone when calling the public
init
method, but that's the only time. I don't see the IV being cloned further by theCipher
classes that useGCMParameterSpec
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, I optimized it so much, both AES/GCM engines can't tell that the IV changes between invocations and throw an exception. Nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't figure out how to get the engine to consider the IV as different, so I still have some clone calls happening, but much less waste than before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into the internal implementation of the cipher engine, and it makes its own clone regardless of what we do, so this seems to be about as optimal as I can get it without digging into Bouncycastle internals or something similar.