Skip to content
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

Iterate on HpkeSuite. #1188

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions common/src/main/java/org/conscrypt/HpkeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ public void engineInitSender(PublicKey recipientKey, byte[] info, PrivateKey sen
byte[] psk, byte[] psk_id) throws InvalidKeyException {
checkNotInitialised();
checkArgumentsForBaseModeOnly(senderKey, psk, psk_id);
final byte[] pk = hpkeSuite.getKem().validatePublicKeyTypeAndGetRawKey(recipientKey);
if (recipientKey == null) {
throw new InvalidKeyException("null recipient key");
} else if (!(recipientKey instanceof OpenSSLX25519PublicKey)) {
throw new InvalidKeyException("Unsupported recipient key class: " + recipientKey.getClass());
}
final byte[] recipientKeyBytes = ((OpenSSLX25519PublicKey) recipientKey).getU();

final Object[] result = NativeCrypto.EVP_HPKE_CTX_setup_base_mode_sender(
hpkeSuite, pk, info);
hpkeSuite, recipientKeyBytes, info);
ctx = (NativeRef.EVP_HPKE_CTX) result[0];
encapsulated = (byte[]) result[1];
}
Expand All @@ -63,10 +68,15 @@ public void engineInitSenderForTesting(PublicKey recipientKey, byte[] info,
checkNotInitialised();
Objects.requireNonNull(sKe);
checkArgumentsForBaseModeOnly(senderKey, psk, psk_id);
final byte[] pk = hpkeSuite.getKem().validatePublicKeyTypeAndGetRawKey(recipientKey);
if (recipientKey == null) {
throw new InvalidKeyException("null recipient key");
} else if (!(recipientKey instanceof OpenSSLX25519PublicKey)) {
throw new InvalidKeyException("Unsupported recipient key class: " + recipientKey.getClass());
}
final byte[] recipientKeyBytes = ((OpenSSLX25519PublicKey) recipientKey).getU();

final Object[] result = NativeCrypto.EVP_HPKE_CTX_setup_base_mode_sender_with_seed_for_testing(
hpkeSuite, pk, info, sKe);
hpkeSuite, recipientKeyBytes, info, sKe);
ctx = (NativeRef.EVP_HPKE_CTX) result[0];
encapsulated = (byte[]) result[1];
}
Expand All @@ -76,11 +86,20 @@ public void engineInitRecipient(byte[] encapsulated, PrivateKey recipientKey,
byte[] info, PublicKey senderKey, byte[] psk, byte[] psk_id) throws InvalidKeyException {
checkNotInitialised();
checkArgumentsForBaseModeOnly(senderKey, psk, psk_id);
hpkeSuite.getKem().validateEncapsulatedLength(encapsulated);
final byte[] sk = hpkeSuite.getKem().validatePrivateKeyTypeAndGetRawKey(recipientKey);
Preconditions.checkNotNull(encapsulated, "null encapsulated data");
if (encapsulated.length != hpkeSuite.getKem().getEncapsulatedLength()) {
throw new InvalidKeyException("Invalid encapsulated length: " + encapsulated.length);
}

if (recipientKey == null) {
throw new InvalidKeyException("null recipient key");
} else if (!(recipientKey instanceof OpenSSLX25519PrivateKey)) {
throw new InvalidKeyException("Unsupported recipient key class: " + recipientKey.getClass());
}
final byte[] recipientKeyBytes = ((OpenSSLX25519PrivateKey) recipientKey).getU();

ctx = (NativeRef.EVP_HPKE_CTX) NativeCrypto.EVP_HPKE_CTX_setup_base_mode_recipient(
hpkeSuite, sk, encapsulated, info);
hpkeSuite, recipientKeyBytes, encapsulated, info);
}

private void checkArgumentsForBaseModeOnly(Key senderKey, byte[] psk, byte[] psk_id) {
Expand All @@ -105,7 +124,11 @@ public byte[] engineSeal(byte[] plaintext, byte[] aad) {
@Override
public byte[] engineExport(int length, byte[] exporterContext) {
checkInitialised();
hpkeSuite.getKdf().validateExportLength(length);
long maxLength = hpkeSuite.getKdf().maxExportLength();
if (length < 0 || length > maxLength) {
throw new IllegalArgumentException("Export length must be between 0 and "
+ maxLength + ", but was " + length);
}
return NativeCrypto.EVP_HPKE_CTX_export(ctx, exporterContext, length);
}

Expand Down
Loading
Loading