-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,112 additions
and
12 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#include "buffer.h" | ||
#include "env.h" | ||
#include "generated-headers.h" | ||
#include <openssl/evp.h> | ||
#include <openssl/kdf.h> | ||
|
||
using namespace AmazonCorrettoCryptoProvider; | ||
|
||
extern "C" JNIEXPORT void Java_com_amazon_corretto_crypto_provider_CounterKdfSpi_nKdf(JNIEnv* env, | ||
jclass, | ||
jint digestCode, | ||
jbyteArray jSecret, | ||
jint secretLen, | ||
jbyteArray jInfo, | ||
jint infoLen, | ||
jbyteArray jOutput, | ||
jint outputLen) | ||
{ | ||
try { | ||
EVP_MD const* digest = digest_code_to_EVP_MD(digestCode); | ||
JBinaryBlob secret(env, nullptr, jSecret); | ||
JBinaryBlob info(env, nullptr, jInfo); | ||
JBinaryBlob output(env, nullptr, jOutput); | ||
if (KBKDF_ctr_hmac(output.get(), outputLen, digest, secret.get(), secretLen, info.get(), infoLen) != 1) { | ||
throw_openssl(EX_RUNTIME_CRYPTO, "KBKDF_ctr_hmac failed."); | ||
} | ||
} catch (java_ex& ex) { | ||
ex.throw_to_java(env); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
src/com/amazon/corretto/crypto/provider/CounterKdfSpec.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,54 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.corretto.crypto.provider; | ||
|
||
import java.security.spec.KeySpec; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Represents the inputs to CounterKdfSpec algorithms. | ||
* | ||
* <p>If info is not provided, an empty byte array is used. | ||
* | ||
* <p>The algorithmName is the name of algorithm used to create SecretKeySpec. | ||
*/ | ||
public class CounterKdfSpec implements KeySpec { | ||
private final byte[] secret; | ||
private final byte[] info; | ||
private final int outputLen; | ||
private final String algorithName; | ||
|
||
public CounterKdfSpec( | ||
final byte[] secret, final byte[] info, final int outputLen, final String algorithName) { | ||
this.secret = Objects.requireNonNull(secret); | ||
if (this.secret.length == 0) { | ||
throw new IllegalArgumentException("Secret must be byte array with non-zero length."); | ||
} | ||
this.info = Objects.requireNonNull(info); | ||
if (outputLen <= 0) { | ||
throw new IllegalArgumentException("Output size must be greater than zero."); | ||
} | ||
this.outputLen = outputLen; | ||
this.algorithName = Objects.requireNonNull(algorithName); | ||
} | ||
|
||
public CounterKdfSpec(final byte[] secret, final int outputLen, final String algorithName) { | ||
this(secret, Utils.EMPTY_ARRAY, outputLen, algorithName); | ||
} | ||
|
||
public byte[] getSecret() { | ||
return secret; | ||
} | ||
|
||
public byte[] getInfo() { | ||
return info; | ||
} | ||
|
||
public int getOutputLen() { | ||
return outputLen; | ||
} | ||
|
||
public String getAlgorithName() { | ||
return algorithName; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/com/amazon/corretto/crypto/provider/CounterKdfSpi.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,66 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.corretto.crypto.provider; | ||
|
||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.KeySpec; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
class CounterKdfSpi extends KdfSpi { | ||
private final int digestCode; | ||
|
||
CounterKdfSpi(final int digestCode) { | ||
this.digestCode = digestCode; | ||
} | ||
|
||
@Override | ||
protected SecretKey engineGenerateSecret(final KeySpec keySpec) throws InvalidKeySpecException { | ||
if (!(keySpec instanceof CounterKdfSpec)) { | ||
throw new InvalidKeySpecException("Expected a key spec of type CounterKdfSpec"); | ||
} | ||
final CounterKdfSpec spec = (CounterKdfSpec) keySpec; | ||
|
||
final byte[] secret = spec.getSecret(); | ||
|
||
final byte[] info = spec.getInfo(); | ||
|
||
final byte[] output = new byte[spec.getOutputLen()]; | ||
|
||
nKdf(digestCode, secret, secret.length, info, info.length, output, output.length); | ||
|
||
return new SecretKeySpec(output, spec.getAlgorithName()); | ||
} | ||
|
||
private static native void nKdf( | ||
int digestCode, | ||
byte[] secret, | ||
int secretLen, | ||
byte[] info, | ||
int infoLen, | ||
byte[] output, | ||
int outputLen); | ||
|
||
static final Map<String, CounterKdfSpi> INSTANCES = getInstances(); | ||
|
||
private static final String CNTR_KDF = "CounterKdf"; | ||
private static final String WITH_HMAC = "WithHmac"; | ||
static final String CNTRKDF_WITH_SHA256 = CNTR_KDF + WITH_HMAC + "SHA256"; | ||
static final String CNTRKDF_WITH_SHA384 = CNTR_KDF + WITH_HMAC + "SHA384"; | ||
static final String CNTRKDF_WITH_SHA512 = CNTR_KDF + WITH_HMAC + "SHA512"; | ||
|
||
private static Map<String, CounterKdfSpi> getInstances() { | ||
final Map<String, CounterKdfSpi> kdfs = new HashMap<>(); | ||
kdfs.put(getSpiFactoryForAlgName(CNTRKDF_WITH_SHA256), new CounterKdfSpi(Utils.SHA256_CODE)); | ||
kdfs.put(getSpiFactoryForAlgName(CNTRKDF_WITH_SHA384), new CounterKdfSpi(Utils.SHA384_CODE)); | ||
kdfs.put(getSpiFactoryForAlgName(CNTRKDF_WITH_SHA512), new CounterKdfSpi(Utils.SHA512_CODE)); | ||
return Collections.unmodifiableMap(kdfs); | ||
} | ||
|
||
static String getSpiFactoryForAlgName(final String alg) { | ||
return alg.toUpperCase(); | ||
} | ||
} |
Oops, something went wrong.