Skip to content

Commit

Permalink
Refactoring some KDF related functions
Browse files Browse the repository at this point in the history
+ The refactored methods are common between HKDF, concatenation KDF, and
  CTR KDF.
  • Loading branch information
amirhosv committed Jul 26, 2024
1 parent 5071483 commit f4c067c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 38 deletions.
17 changes: 0 additions & 17 deletions csrc/hkdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@

using namespace AmazonCorrettoCryptoProvider;

// The possible values of digestCode are defined in HkdfSecretKeyFactorySpi.java
static EVP_MD const* digest_code_to_EVP_MD(int digestCode)
{
switch (digestCode) {
case com_amazon_corretto_crypto_provider_HkdfSecretKeyFactorySpi_SHA1_CODE:
return EVP_sha1();
case com_amazon_corretto_crypto_provider_HkdfSecretKeyFactorySpi_SHA256_CODE:
return EVP_sha256();
case com_amazon_corretto_crypto_provider_HkdfSecretKeyFactorySpi_SHA384_CODE:
return EVP_sha384();
case com_amazon_corretto_crypto_provider_HkdfSecretKeyFactorySpi_SHA512_CODE:
return EVP_sha512();
default:
throw java_ex(EX_ERROR, "THIS SHOULD NOT BE REACHABLE.");
}
}

extern "C" JNIEXPORT void JNICALL Java_com_amazon_corretto_crypto_provider_HkdfSecretKeyFactorySpi_hkdf(JNIEnv* env,
jclass,
jbyteArray jOutput,
Expand Down
16 changes: 16 additions & 0 deletions csrc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ extern "C" JNIEXPORT void JNICALL Java_com_amazon_corretto_crypto_provider_Utils
EVP_CIPHER_CTX_free(reinterpret_cast<EVP_CIPHER_CTX*>(ctxPtr));
}

EVP_MD const* digest_code_to_EVP_MD(int digestCode)
{
switch (digestCode) {
case com_amazon_corretto_crypto_provider_Utils_SHA1_CODE:
return EVP_sha1();
case com_amazon_corretto_crypto_provider_Utils_SHA256_CODE:
return EVP_sha256();
case com_amazon_corretto_crypto_provider_Utils_SHA384_CODE:
return EVP_sha384();
case com_amazon_corretto_crypto_provider_Utils_SHA512_CODE:
return EVP_sha512();
default:
throw java_ex(EX_ERROR, "THIS SHOULD NOT BE REACHABLE.");
}
}

} // namespace
4 changes: 4 additions & 0 deletions csrc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ static inline bool check_bounds(size_t length, size_t offset, size_t range_len)
return remaining >= range_len;
}

// Given the code of a digest, returns its correspodning EVP_MD* object. The possible values of digestCode are defined
// in Utils.java. This method is used by different KDFs that can work with different digest algorithms.
EVP_MD const* digest_code_to_EVP_MD(int digestCode);

} // namespace AmazonCorrettoCryptoProvider

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import java.util.HashMap;
import java.util.Map;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactorySpi;
import javax.crypto.spec.SecretKeySpec;

class HkdfSecretKeyFactorySpi extends SecretKeyFactorySpi {
class HkdfSecretKeyFactorySpi extends KdfSpi {
private final int digestCode;
private final int digestLength;

Expand Down Expand Up @@ -121,16 +120,6 @@ private static native void hkdfExpand(
byte[] jInfo,
int infoLen);

@Override
protected KeySpec engineGetKeySpec(final SecretKey key, final Class<?> keySpec) {
throw new UnsupportedOperationException();
}

@Override
protected SecretKey engineTranslateKey(final SecretKey key) {
throw new UnsupportedOperationException();
}

static final Map<String, HkdfSecretKeyFactorySpi> INSTANCES = getInstances();

private static final String HKDF = "Hkdf";
Expand All @@ -140,25 +129,20 @@ protected SecretKey engineTranslateKey(final SecretKey key) {
static final String HKDF_WITH_SHA384 = HKDF + WITH + "HmacSHA384";
static final String HKDF_WITH_SHA512 = HKDF + WITH + "HmacSHA512";

private static final int SHA1_CODE = 1;
private static final int SHA256_CODE = 2;
private static final int SHA384_CODE = 3;
private static final int SHA512_CODE = 4;

private static Map<String, HkdfSecretKeyFactorySpi> getInstances() {
final Map<String, HkdfSecretKeyFactorySpi> result = new HashMap<>();
result.put(
getSpiFactoryForAlgName(HKDF_WITH_SHA1),
new HkdfSecretKeyFactorySpi(SHA1_CODE, getDigestLength("sha1")));
new HkdfSecretKeyFactorySpi(Utils.SHA1_CODE, getDigestLength("sha1")));
result.put(
getSpiFactoryForAlgName(HKDF_WITH_SHA256),
new HkdfSecretKeyFactorySpi(SHA256_CODE, getDigestLength("sha256")));
new HkdfSecretKeyFactorySpi(Utils.SHA256_CODE, getDigestLength("sha256")));
result.put(
getSpiFactoryForAlgName(HKDF_WITH_SHA384),
new HkdfSecretKeyFactorySpi(SHA384_CODE, getDigestLength("sha384")));
new HkdfSecretKeyFactorySpi(Utils.SHA384_CODE, getDigestLength("sha384")));
result.put(
getSpiFactoryForAlgName(HKDF_WITH_SHA512),
new HkdfSecretKeyFactorySpi(SHA512_CODE, getDigestLength("sha512")));
new HkdfSecretKeyFactorySpi(Utils.SHA512_CODE, getDigestLength("sha512")));
return Collections.unmodifiableMap(result);
}

Expand Down
20 changes: 20 additions & 0 deletions src/com/amazon/corretto/crypto/provider/KdfSpi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactorySpi;

abstract class KdfSpi extends SecretKeyFactorySpi {

@Override
protected KeySpec engineGetKeySpec(final SecretKey key, final Class<?> keySpec) {
throw new UnsupportedOperationException();
}

@Override
protected SecretKey engineTranslateKey(final SecretKey key) {
throw new UnsupportedOperationException();
}
}
4 changes: 4 additions & 0 deletions src/com/amazon/corretto/crypto/provider/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

/** Miscellaneous utility methods. */
final class Utils {
static final int SHA1_CODE = 1;
static final int SHA256_CODE = 2;
static final int SHA384_CODE = 3;
static final int SHA512_CODE = 4;
private static final String PROPERTY_NATIVE_CONTEXT_RELEASE_STRATEGY =
"nativeContextReleaseStrategy";

Expand Down

0 comments on commit f4c067c

Please sign in to comment.