-
Notifications
You must be signed in to change notification settings - Fork 56
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
[PQ] Add experimental support for HPKE #398
Merged
amirhosv
merged 26 commits into
corretto:experimental-pq-hybrid
from
sgmenda-aws:experimental-pq-hybrid
Oct 24, 2024
Merged
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
efa2feb
import aws-lc with experimental-pq-hybrid-with-hpke
sgmenda-aws 8cd1de6
implement cpp for hpke keygen
sgmenda-aws a16bf79
implement hpke keygen
sgmenda-aws 89d4f3f
implement hpke cipher
sgmenda-aws 75bda35
update aws-lc
sgmenda-aws c624dac
fix typo
sgmenda-aws 750165c
add submodule sync to build
sgmenda-aws cc825c9
do submodule update
sgmenda-aws 2d6834f
another attempt
sgmenda-aws 20f7118
yet another attempt
sgmenda-aws c265fbc
one more attempt at ci
sgmenda-aws c7023b4
finally resolved github ci bug
sgmenda-aws 4766d4e
update build script
sgmenda-aws 8aac3df
address review comments
sgmenda-aws c48a866
add some javadocs
sgmenda-aws 79375c3
implement aad and one-shot encrypt and decrypt
sgmenda-aws f2706ba
make test parameterized
sgmenda-aws abca1ba
add hpke to readme
sgmenda-aws f4629d6
only use aes256 for pq ciphersuites
sgmenda-aws 3be5985
update docs
sgmenda-aws 82eabd1
support overlapping buffers
sgmenda-aws b4b8d88
fix compiler warnings
sgmenda-aws 7aea9f2
update javadoc
sgmenda-aws 30a7287
fix another compiler warning
sgmenda-aws 97e7b7f
switch from fork to upstream aws-lc
sgmenda-aws 3963ea6
use HEAD for FIPS build
sgmenda-aws 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[submodule "aws-lc"] | ||
path = aws-lc | ||
url = https://github.com/awslabs/aws-lc | ||
url = https://github.com/sgmenda-aws/aws-lc | ||
branch = experimental-pq-hybrid-with-hpke | ||
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
geedo0 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,158 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#include "auto_free.h" | ||
#include "bn.h" | ||
#include "buffer.h" | ||
#include "env.h" | ||
#include "generated-headers.h" | ||
#include "keyutils.h" | ||
#include "util.h" | ||
#include <openssl/evp.h> | ||
#include <openssl/hpke.h> | ||
#include <openssl/nid.h> | ||
|
||
using namespace AmazonCorrettoCryptoProvider; | ||
|
||
/* | ||
* Class: com_amazon_corretto_crypto_provider_HpkeCipher | ||
* Method: hpkeWrap | ||
* Signature: (J[BIIII[BI)I | ||
*/ | ||
JNIEXPORT jint JNICALL Java_com_amazon_corretto_crypto_provider_HpkeCipher_hpkeCipher(JNIEnv* pEnv, | ||
jclass, | ||
jlong keyHandle, | ||
jint javaCipherMode, | ||
jint kemId, | ||
jint kdfId, | ||
jint aeadId, | ||
jbyteArray input, | ||
jint inputOffset, | ||
jint inputLen, | ||
jbyteArray output, | ||
jint outputOffset) | ||
{ | ||
try { | ||
raii_env env(pEnv); | ||
|
||
if (!input) { | ||
throw_java_ex(EX_NPE, "Empty input array"); | ||
} | ||
if (!output) { | ||
throw_java_ex(EX_NPE, "Empty output array"); | ||
} | ||
|
||
const EVP_HPKE_KEY* key = reinterpret_cast<EVP_HPKE_KEY*>(keyHandle); | ||
const auto kem = EVP_HPKE_KEM_find_by_id(kemId); | ||
const auto kdf = EVP_HPKE_KDF_find_by_id(kdfId); | ||
const auto aead = EVP_HPKE_AEAD_find_by_id(aeadId); | ||
const auto aead_overhead = EVP_AEAD_max_overhead(EVP_HPKE_AEAD_aead(aead)); | ||
|
||
if (kemId != EVP_HPKE_KEM_id(EVP_HPKE_KEY_kem(key))) { | ||
throw_java_ex(EX_RUNTIME_CRYPTO, "KEM in the key does not match the param"); | ||
} | ||
|
||
// FIXME: support setting these values | ||
std::vector<uint8_t> info(0); | ||
std::vector<uint8_t> ad(0); | ||
|
||
size_t result = -1; | ||
|
||
if (javaCipherMode == 3 /* Wrap */) { | ||
// Serialize public key | ||
std::vector<uint8_t> public_key_r(EVP_HPKE_KEM_public_key_len(kem)); | ||
size_t public_key_r_len; | ||
CHECK_OPENSSL(EVP_HPKE_KEY_public_key(key, public_key_r.data(), &public_key_r_len, public_key_r.size())); | ||
|
||
// The input is the plaintext message | ||
java_buffer msgBuf = java_buffer::from_array(env, input, inputOffset, inputLen); | ||
|
||
// We write the enc and the ciphertext to the output buffer | ||
const auto encBufLen = EVP_HPKE_KEM_enc_len(kem); | ||
const auto ctBufLen = inputLen + aead_overhead; | ||
const auto outBufLen = encBufLen + ctBufLen; | ||
java_buffer encBuf = java_buffer::from_array(env, output, outputOffset, encBufLen); | ||
java_buffer ctBuf = java_buffer::from_array(env, output, outputOffset + encBufLen, ctBufLen); | ||
size_t enc_len = 0; | ||
size_t ct_len = 0; | ||
|
||
{ | ||
jni_borrow msg(env, msgBuf, "input msg"); | ||
jni_borrow enc(env, encBuf, "output enc"); | ||
jni_borrow ct(env, ctBuf, "output ciphertext"); | ||
|
||
CHECK_OPENSSL(EVP_HPKE_seal(enc.data(), &enc_len, enc.len(), ct.data(), &ct_len, ct.len(), kem, kdf, | ||
aead, public_key_r.data(), public_key_r_len, info.data(), info.size(), msg.data(), msg.len(), | ||
ad.data(), ad.size())); | ||
if (enc_len != encBufLen) { | ||
throw_java_ex(EX_RUNTIME_CRYPTO, "Unexpected error, enc buffer length is wrong!"); | ||
} | ||
if (ct_len != ctBufLen) { | ||
throw_java_ex(EX_RUNTIME_CRYPTO, "Unexpected error, ciphertext buffer length is wrong!"); | ||
} | ||
result = outBufLen; | ||
} | ||
} else if (javaCipherMode == 4 /* Unwrap */) { | ||
// The input the enc and the ciphertext | ||
const auto encBufLen = EVP_HPKE_KEM_enc_len(kem); | ||
if (inputLen < (encBufLen + aead_overhead)) { | ||
throw_java_ex(EX_RUNTIME_CRYPTO, "input too short to unwrap with HPKE"); | ||
} | ||
const auto ctBufLen = inputLen - encBufLen; | ||
java_buffer encBuf = java_buffer::from_array(env, input, inputOffset, encBufLen); | ||
java_buffer ctBuf = java_buffer::from_array(env, input, inputOffset + encBufLen, ctBufLen); | ||
|
||
// We write the plaintext message to the output buffer | ||
java_buffer msgBuf = java_buffer::from_array(env, output, outputOffset); | ||
size_t msg_len = 0; | ||
{ | ||
jni_borrow msg(env, msgBuf, "output msg"); | ||
jni_borrow enc(env, encBuf, "input enc"); | ||
jni_borrow ct(env, ctBuf, "input ciphertext"); | ||
|
||
CHECK_OPENSSL(EVP_HPKE_open(msg.data(), &msg_len, msg.len(), key, kdf, aead, enc.data(), enc.len(), | ||
info.data(), info.size(), ct.data(), ct.len(), ad.data(), ad.size())) | ||
result = msg_len; | ||
} | ||
} else { | ||
throw_java_ex(EX_RUNTIME_CRYPTO, "Unsupported cipher mode"); | ||
} | ||
return (jint)result; | ||
} catch (java_ex& ex) { | ||
ex.throw_to_java(pEnv); | ||
return -1; | ||
} | ||
} | ||
|
||
/* | ||
* Class: com_amazon_corretto_crypto_provider_HpkeCipher | ||
* Method: hpkeOutputSize | ||
* Signature: (IIIII)I | ||
*/ | ||
JNIEXPORT jint JNICALL Java_com_amazon_corretto_crypto_provider_HpkeCipher_hpkeOutputSize( | ||
JNIEnv* pEnv, jclass, jint javaCipherMode, jint kemId, jint kdfId, jint aeadId, jint inputLen) | ||
{ | ||
const auto kem = EVP_HPKE_KEM_find_by_id(kemId); | ||
const auto aead = EVP_HPKE_AEAD_find_by_id(aeadId); | ||
const auto aead_overhead = EVP_AEAD_max_overhead(EVP_HPKE_AEAD_aead(aead)); | ||
const auto enc_len = EVP_HPKE_KEM_enc_len(kem); | ||
|
||
try { | ||
raii_env env(pEnv); | ||
|
||
if (javaCipherMode == 3 /* Wrap */) { | ||
// We write the enc and the ciphertext to the output buffer | ||
return (inputLen + enc_len + aead_overhead); | ||
} else if (javaCipherMode == 4 /* Unwrap */) { | ||
// We write the plaintext to the output buffer | ||
if (inputLen < (enc_len + aead_overhead)) { | ||
throw_java_ex(EX_RUNTIME_CRYPTO, "input too short to unwrap with HPKE"); | ||
} | ||
return (inputLen - enc_len - aead_overhead); | ||
} else { | ||
throw_java_ex(EX_RUNTIME_CRYPTO, "Unsupported cipher mode"); | ||
} | ||
} catch (java_ex& ex) { | ||
ex.throw_to_java(pEnv); | ||
return -1; | ||
} | ||
} |
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,36 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#include "auto_free.h" | ||
#include "bn.h" | ||
#include "buffer.h" | ||
#include "env.h" | ||
#include "generated-headers.h" | ||
#include "keyutils.h" | ||
#include "util.h" | ||
#include <openssl/evp.h> | ||
#include <openssl/hpke.h> | ||
#include <openssl/nid.h> | ||
|
||
using namespace AmazonCorrettoCryptoProvider; | ||
|
||
/* | ||
* Class: com_amazon_corretto_crypto_provider_HpkeGen | ||
* Method: generateEvpHpkeKemKeyFromSpec | ||
* Signature: (I)J | ||
*/ | ||
JNIEXPORT jlong JNICALL Java_com_amazon_corretto_crypto_provider_HpkeGen_generateEvpHpkeKemKeyFromSpec( | ||
JNIEnv* pEnv, jclass, jint hpke_kem_id) | ||
{ | ||
EVP_HPKE_KEY_auto key; | ||
try { | ||
raii_env env(pEnv); | ||
key.set(EVP_HPKE_KEY_new()); | ||
const EVP_HPKE_KEM* kem = EVP_HPKE_KEM_find_by_id(hpke_kem_id); | ||
geedo0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CHECK_OPENSSL(kem != NULL); | ||
CHECK_OPENSSL(EVP_HPKE_KEY_generate(key, kem)); | ||
return reinterpret_cast<jlong>(key.take()); | ||
} catch (java_ex& ex) { | ||
ex.throw_to_java(pEnv); | ||
return 0; | ||
} | ||
} |
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,18 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.corretto.crypto.provider; | ||
|
||
class EvpHpkeKey extends EvpKey { | ||
private static final long serialVersionUID = 1; | ||
|
||
final HpkeParameterSpec spec; | ||
|
||
EvpHpkeKey(final InternalKey key, final boolean isPublicKey, HpkeParameterSpec spec) { | ||
super(key, EvpKeyType.HPKE, isPublicKey); | ||
this.spec = spec; | ||
} | ||
|
||
public HpkeParameterSpec getSpec() { | ||
return spec; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/com/amazon/corretto/crypto/provider/EvpHpkePrivateKey.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,29 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.corretto.crypto.provider; | ||
|
||
import com.amazon.corretto.crypto.provider.EvpKey.CanDerivePublicKey; | ||
import java.security.PrivateKey; | ||
|
||
public class EvpHpkePrivateKey extends EvpHpkeKey | ||
implements PrivateKey, CanDerivePublicKey<EvpHpkePublicKey> { | ||
private static final long serialVersionUID = 1; | ||
|
||
EvpHpkePrivateKey(InternalKey key, HpkeParameterSpec spec) { | ||
super(key, false, spec); | ||
} | ||
|
||
EvpHpkePrivateKey(final long ptr, HpkeParameterSpec spec) { | ||
this(new InternalKey(ptr), spec); | ||
} | ||
|
||
@Override | ||
public EvpHpkePublicKey getPublicKey() { | ||
// Once our internal key could be elsewhere, we can no longer safely release it when done | ||
ephemeral = false; | ||
sharedKey = true; | ||
final EvpHpkePublicKey result = new EvpHpkePublicKey(internalKey, spec); | ||
result.sharedKey = true; | ||
return result; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/com/amazon/corretto/crypto/provider/EvpHpkePublicKey.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,13 @@ | ||
// 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.PublicKey; | ||
|
||
public class EvpHpkePublicKey extends EvpHpkeKey implements PublicKey { | ||
private static final long serialVersionUID = 1; | ||
|
||
EvpHpkePublicKey(InternalKey key, HpkeParameterSpec spec) { | ||
super(key, true, spec); | ||
} | ||
} |
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.
What's the story here? Is this temporary? Will this branch exist in upstream AWS-LC? We can't be referencing submodules in personal forks.
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.
Yes, this is temporary till aws/aws-lc#1777 is merged.