-
Notifications
You must be signed in to change notification settings - Fork 122
Remove use of BouncyCastle for HMAC key derivation #130
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
171 changes: 171 additions & 0 deletions
171
src/main/java/com/amazonaws/encryptionsdk/internal/HmacKeyDerivationFunction.java
This file contains hidden or 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,171 @@ | ||
/* | ||
* Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazonaws.encryptionsdk.internal; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Provider; | ||
import java.util.Arrays; | ||
|
||
import javax.crypto.Mac; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import static org.apache.commons.lang3.Validate.isTrue; | ||
|
||
/** | ||
* HMAC-based Key Derivation Function. | ||
* Adapted from Hkdf.java in aws-dynamodb-encryption-java | ||
* | ||
* @see <a href="http://tools.ietf.org/html/rfc5869">RFC 5869</a> | ||
*/ | ||
public final class HmacKeyDerivationFunction { | ||
private static final byte[] EMPTY_ARRAY = new byte[0]; | ||
private final String algorithm; | ||
private final Provider provider; | ||
private SecretKey prk = null; | ||
|
||
/** | ||
* Returns an <code>Hkdf</code> object using the specified algorithm. | ||
* | ||
* @param algorithm the standard name of the requested MAC algorithm. See the Mac | ||
* section in the <a href= | ||
* "http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Mac" | ||
* > Java Cryptography Architecture Standard Algorithm Name | ||
* Documentation</a> for information about standard algorithm | ||
* names. | ||
* @return the new <code>Hkdf</code> object | ||
* @throws NoSuchAlgorithmException if no Provider supports a MacSpi implementation for the | ||
* specified algorithm. | ||
*/ | ||
public static HmacKeyDerivationFunction getInstance(final String algorithm) | ||
throws NoSuchAlgorithmException { | ||
// Constructed specifically to sanity-test arguments. | ||
Mac mac = Mac.getInstance(algorithm); | ||
return new HmacKeyDerivationFunction(algorithm, mac.getProvider()); | ||
} | ||
|
||
/** | ||
* Initializes this Hkdf with input keying material. A default salt of | ||
* HashLen zeros will be used (where HashLen is the length of the return | ||
* value of the supplied algorithm). | ||
* | ||
* @param ikm the Input Keying Material | ||
*/ | ||
public void init(final byte[] ikm) { | ||
init(ikm, null); | ||
} | ||
|
||
/** | ||
* Initializes this Hkdf with input keying material and a salt. If <code> | ||
* salt</code> is <code>null</code> or of length 0, then a default salt of | ||
* HashLen zeros will be used (where HashLen is the length of the return | ||
* value of the supplied algorithm). | ||
* | ||
* @param salt the salt used for key extraction (optional) | ||
* @param ikm the Input Keying Material | ||
*/ | ||
public void init(final byte[] ikm, final byte[] salt) { | ||
byte[] realSalt = (salt == null) ? EMPTY_ARRAY : salt.clone(); | ||
byte[] rawKeyMaterial = EMPTY_ARRAY; | ||
try { | ||
Mac extractionMac = Mac.getInstance(algorithm, provider); | ||
if (realSalt.length == 0) { | ||
realSalt = new byte[extractionMac.getMacLength()]; | ||
Arrays.fill(realSalt, (byte) 0); | ||
} | ||
extractionMac.init(new SecretKeySpec(realSalt, algorithm)); | ||
rawKeyMaterial = extractionMac.doFinal(ikm); | ||
this.prk = new SecretKeySpec(rawKeyMaterial, algorithm); | ||
} catch (GeneralSecurityException e) { | ||
// We've already checked all of the parameters so no exceptions | ||
// should be possible here. | ||
throw new RuntimeException("Unexpected exception", e); | ||
} finally { | ||
Arrays.fill(rawKeyMaterial, (byte) 0); // Zeroize temporary array | ||
} | ||
} | ||
|
||
private HmacKeyDerivationFunction(final String algorithm, final Provider provider) { | ||
isTrue(algorithm.startsWith("Hmac"), "Invalid algorithm " + algorithm | ||
+ ". Hkdf may only be used with Hmac algorithms."); | ||
this.algorithm = algorithm; | ||
this.provider = provider; | ||
} | ||
|
||
/** | ||
* Returns a pseudorandom key of <code>length</code> bytes. | ||
* | ||
* @param info optional context and application specific information (can be | ||
* a zero-length array). | ||
* @param length the length of the output key in bytes | ||
* @return a pseudorandom key of <code>length</code> bytes. | ||
* @throws IllegalStateException if this object has not been initialized | ||
*/ | ||
public byte[] deriveKey(final byte[] info, final int length) throws IllegalStateException { | ||
isTrue(length >= 0, "Length must be a non-negative value."); | ||
assertInitialized(); | ||
final byte[] result = new byte[length]; | ||
Mac mac = createMac(); | ||
|
||
isTrue(length <= 255 * mac.getMacLength(), | ||
"Requested keys may not be longer than 255 times the underlying HMAC length."); | ||
|
||
byte[] t = EMPTY_ARRAY; | ||
try { | ||
int loc = 0; | ||
byte i = 1; | ||
while (loc < length) { | ||
mac.update(t); | ||
mac.update(info); | ||
mac.update(i); | ||
t = mac.doFinal(); | ||
|
||
for (int x = 0; x < t.length && loc < length; x++, loc++) { | ||
result[loc] = t[x]; | ||
} | ||
|
||
i++; | ||
} | ||
} finally { | ||
Arrays.fill(t, (byte) 0); // Zeroize temporary array | ||
} | ||
return result; | ||
} | ||
|
||
private Mac createMac() { | ||
try { | ||
Mac mac = Mac.getInstance(algorithm, provider); | ||
mac.init(prk); | ||
return mac; | ||
} catch (NoSuchAlgorithmException | InvalidKeyException ex) { | ||
// We've already validated that this algorithm/key is correct. | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
/** | ||
* Throws an <code>IllegalStateException</code> if this object has not been | ||
* initialized. | ||
* | ||
* @throws IllegalStateException if this object has not been initialized | ||
*/ | ||
private void assertInitialized() throws IllegalStateException { | ||
if (prk == null) { | ||
throw new IllegalStateException("Hkdf has not been initialized"); | ||
} | ||
} | ||
} |
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 update date