-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8325448: Hybrid Public Key Encryption #18411
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
Open
wangweij
wants to merge
64
commits into
openjdk:master
Choose a base branch
from
wangweij:8325448
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
0cbd952
the fix
wangweij 652d63f
no export, params from record to class
wangweij 36993a3
Merge branch 'master' into 8325448
wangweij 4419200
disabled algorithms
wangweij 95f658c
remove debug lines
wangweij ed9f5d0
Merge branch 'master' into 8325448
wangweij 80a188a
spec update
wangweij e7db8b8
Merge branch 'master' into 8325448
wangweij 6d4bbfa
no scope for td
wangweij d77a1b2
Use KDF API
wangweij 412efec
Merge branch 'master' into 8325448
wangweij 5877364
add @since 25
wangweij 7e525f1
example and KAT
wangweij 8ae9a52
kevin comments
wangweij 9271265
renames, precise req, more spec, wrappings
wangweij 8342e7d
typo, and other cleanup
wangweij 1839c73
unspecified id is -1 now
wangweij 5b0f319
add mode_auth and mode_auth_psk, tiny spec change
wangweij 9360dfd
of(kdf_id, aead_id) and constants
wangweij 4bb1750
switch to Asserts.assertThrows in test; use traditional javadoc style
wangweij 18e0538
getParameters
wangweij 25d2fb1
remove disabled identifiers check
wangweij 5d15f9d
remove unused imports
wangweij c578ef5
Merge branch 'master' into 8325448
wangweij 30cbd4d
receiver must specify all algorithm identifiers
wangweij 0796a42
put encapsulation in params from getParameters
wangweij 17dceaa
toString, exportData, spec in HPKEParameters must have algorithm iden…
wangweij fff8e32
address Sean's comments
wangweij 33a8e13
Merge branch 'master' into 8325448
wangweij 15d0b85
change argument order for exporters, reject null exporter_context
wangweij a4f59e3
engineGetBlockSize and engineGetOutputSize returns 0 when not initial…
wangweij f819de2
merge
wangweij 3906aa0
extract and expand
wangweij 30c7994
no more of()
wangweij 787702d
some spec change
wangweij 653b56c
after initialization
wangweij e83a4a1
finetune the spec
wangweij 8b13da1
test on SliceableSecretKey; fixed snippet comment
wangweij 5a84603
Merge branch 'master' into 8325448
wangweij 2c86d90
test fix
wangweij a67d06d
rename variables in a test
wangweij 56730ee
more complete test
wangweij 04a45d4
remove HPKEParameters
wangweij 5b4767b
remove unused imports
wangweij 008a5a6
HPKEParameterSpec.toString with human-readable info
wangweij 2d7631e
put comments in correct places
wangweij 31c3f69
cleaner comments
wangweij fba0da9
8325448: Hybrid Public Key Encryption
wangweij b42a855
address Sean's comments
wangweij 4772004
more spec change
wangweij 29cb254
use different exception type
wangweij 8dbc050
receiver to recipient; different to specified
wangweij cbce830
duplicated "value" words
wangweij d4c237f
withMethods
wangweij bdd00bd
algorithm identifier
wangweij cef5557
cannot reset with withMethods
wangweij a6949f5
about transformation
wangweij 1ec31cf
Merge branch 'master' into 8325448
wangweij b86797a
more key checks; some small spec change
wangweij cca3c8d
Tony's comment
wangweij 8c6cffa
Merge branch 'master' into 8325448
wangweij b511215
merge
wangweij 4d5b15f
check null and tests
wangweij 52c4d84
java style method names
wangweij 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
361 changes: 223 additions & 138 deletions
361
src/java.base/share/classes/com/sun/crypto/provider/DHKEM.java
Large diffs are not rendered by default.
Oops, something went wrong.
588 changes: 588 additions & 0 deletions
588
src/java.base/share/classes/com/sun/crypto/provider/HPKE.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
443 changes: 443 additions & 0 deletions
443
src/java.base/share/classes/javax/crypto/spec/HPKEParameterSpec.java
Large diffs are not rendered by default.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
src/java.base/share/classes/javax/crypto/spec/snippet-files/PackageSnippets.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,76 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
| import javax.crypto.Cipher; | ||
| import javax.crypto.spec.HPKEParameterSpec; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.KeyPair; | ||
| import java.security.KeyPairGenerator; | ||
| import java.util.Arrays; | ||
| import java.util.HexFormat; | ||
|
|
||
| class PackageSnippets { | ||
| public static void main(String[] args) throws Exception { | ||
|
|
||
| // @start region="hpke-spec-example" | ||
| // Recipient key pair generation | ||
| KeyPairGenerator g = KeyPairGenerator.getInstance("X25519"); | ||
| KeyPair kp = g.generateKeyPair(); | ||
|
|
||
| // The HPKE sender cipher is initialized with the recipient's public | ||
| // key and an HPKEParameterSpec using specified algorithm identifiers | ||
| // and application-supplied info. | ||
| Cipher senderCipher = Cipher.getInstance("HPKE"); | ||
| HPKEParameterSpec ps = HPKEParameterSpec.of( | ||
| HPKEParameterSpec.KEM_DHKEM_X25519_HKDF_SHA256, | ||
| HPKEParameterSpec.KDF_HKDF_SHA256, | ||
| HPKEParameterSpec.AEAD_AES_128_GCM) | ||
| .withInfo(HexFormat.of().parseHex("010203040506")); | ||
| senderCipher.init(Cipher.ENCRYPT_MODE, kp.getPublic(), ps); | ||
|
|
||
| // Retrieve the key encapsulation message (from the KEM step) from | ||
| // the sender. | ||
| byte[] kemEncap = senderCipher.getIV(); | ||
|
|
||
| // The HPKE recipient cipher is initialized with its own private key, | ||
| // an HPKEParameterSpec using the same algorithm identifiers as used by | ||
| // the sender, and the key encapsulation message from the sender. | ||
| Cipher recipientCipher = Cipher.getInstance("HPKE"); | ||
| HPKEParameterSpec pr = HPKEParameterSpec.of( | ||
| HPKEParameterSpec.KEM_DHKEM_X25519_HKDF_SHA256, | ||
| HPKEParameterSpec.KDF_HKDF_SHA256, | ||
| HPKEParameterSpec.AEAD_AES_128_GCM) | ||
| .withInfo(HexFormat.of().parseHex("010203040506")) | ||
| .withEncapsulation(kemEncap); | ||
| recipientCipher.init(Cipher.DECRYPT_MODE, kp.getPrivate(), pr); | ||
|
|
||
| // Encryption and decryption | ||
| byte[] msg = "Hello World".getBytes(StandardCharsets.UTF_8); | ||
| byte[] ct = senderCipher.doFinal(msg); | ||
| byte[] pt = recipientCipher.doFinal(ct); | ||
|
|
||
| assert Arrays.equals(msg, pt); | ||
| // @end | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
src/java.base/share/classes/sun/security/util/SliceableSecretKey.java
wangweij marked this conversation as resolved.
Show resolved
Hide resolved
|
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,51 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
| package sun.security.util; | ||
|
|
||
| import javax.crypto.SecretKey; | ||
|
|
||
| /** | ||
| * An interface for <code>SecretKey</code>s that support using its slice as a new | ||
| * <code>SecretKey</code>. | ||
| * <p> | ||
| * This is mainly used by PKCS #11 implementations that support the | ||
| * EXTRACT_KEY_FROM_KEY mechanism even if the key itself is sensitive | ||
| * and non-extractable. | ||
| */ | ||
| public interface SliceableSecretKey { | ||
|
|
||
| /** | ||
| * Returns a slice as a new <code>SecretKey</code>. | ||
| * | ||
| * @param alg the new algorithm name | ||
| * @param from the byte offset of the new key in the full key | ||
| * @param to the to offset (exclusive) of the new key in the full key | ||
| * @return the new key | ||
| * @throws ArrayIndexOutOfBoundsException for improper <code>from</code> | ||
| * and <code>to</code> values | ||
| * @throws UnsupportedOperationException if slicing is not supported | ||
| */ | ||
| SecretKey slice(String alg, int from, int to); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.