-
Notifications
You must be signed in to change notification settings - Fork 273
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
[COMPRESS-633] Add encryption support for SevenZ #332
Merged
garydgregory
merged 11 commits into
apache:master
from
Dougniel:seven-z-password-encryption-support
Dec 10, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bb3e29a
feat: Encyrption support for Seven7
Dougniel e857bda
feat: Encyrption support for Seven7 without `AES/CBC/PKCS5Padding`
Dougniel 92c4db1
feat: Encyrption support for SevenZ
Dougniel 0f36b34
feat: Encyrption support for SevenZ
Dougniel 2debc4e
feat: Encyrption support for SevenZ
Dougniel c2d1d1d
feat: Encyrption support for SevenZ
Dougniel c7dafb3
Fix spelling
garydgregory a577c1a
Update super class from master
garydgregory 51aca67
Merge branch 'master' into seven-z-password-encryption-support
garydgregory 26dc4af
AES256Options does not need to be public
garydgregory f22b216
Fix spelling in Javadoc
garydgregory 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
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ target | |
.classpath | ||
.settings | ||
.idea | ||
.vscode | ||
*.iml | ||
*~ | ||
/.externalToolBuilders/ | ||
|
100 changes: 100 additions & 0 deletions
100
src/main/java/org/apache/commons/compress/archivers/sevenz/AES256Options.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,100 @@ | ||
/* | ||
* 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.commons.compress.archivers.sevenz; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
/** | ||
* Options for {@link SevenZMethod#AES256SHA256} encoder | ||
* | ||
* @since 1.23 | ||
* @see AES256SHA256Decoder | ||
*/ | ||
class AES256Options { | ||
|
||
private final byte[] salt; | ||
private final byte[] iv; | ||
private final int numCyclesPower; | ||
private final Cipher cipher; | ||
|
||
/** | ||
* @param password password used for encryption | ||
*/ | ||
public AES256Options(char[] password) { | ||
this(password, new byte[0], randomBytes(16), 19); | ||
} | ||
|
||
/** | ||
* @param password password used for encryption | ||
* @param salt for password hash salting (enforce password security) | ||
* @param iv Initialization Vector (IV) used by cipher algorithm | ||
* @param numCyclesPower another password security enforcer parameter that controls the cycles of password hashing. More the | ||
* this number is high, more security you'll have but also high CPU usage | ||
*/ | ||
public AES256Options(char[] password, byte[] salt, byte[] iv, int numCyclesPower) { | ||
this.salt = salt; | ||
this.iv = iv; | ||
this.numCyclesPower = numCyclesPower; | ||
|
||
// NOTE: for security purposes, password is wrapped in a Cipher as soon as possible to not stay in memory | ||
final byte[] aesKeyBytes = AES256SHA256Decoder.sha256Password(password, numCyclesPower, salt); | ||
final SecretKey aesKey = new SecretKeySpec(aesKeyBytes, "AES"); | ||
|
||
try { | ||
cipher = Cipher.getInstance("AES/CBC/NoPadding"); | ||
cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(iv)); | ||
} catch (final GeneralSecurityException generalSecurityException) { | ||
throw new IllegalStateException( | ||
"Encryption error (do you have the JCE Unlimited Strength Jurisdiction Policy Files installed?)", | ||
generalSecurityException | ||
); | ||
} | ||
} | ||
|
||
byte[] getIv() { | ||
return iv; | ||
} | ||
|
||
int getNumCyclesPower() { | ||
return numCyclesPower; | ||
} | ||
|
||
byte[] getSalt() { | ||
return salt; | ||
} | ||
|
||
Cipher getCipher() { | ||
return cipher; | ||
} | ||
|
||
private static byte[] randomBytes(int size) { | ||
byte[] bytes = new byte[size]; | ||
try { | ||
SecureRandom.getInstanceStrong().nextBytes(bytes); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException("No strong secure random available to generate strong AES key", e); | ||
} | ||
return bytes; | ||
} | ||
} |
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.
Please complete the Javadoc comments. You need a starting sentence.