Skip to content

Commit 45a2fd3

Browse files
committed
8325448: Hybrid Public Key Encryption
Reviewed-by: mullan, ascarpino, abarashev
1 parent b9ee954 commit 45a2fd3

File tree

12 files changed

+2120
-230
lines changed

12 files changed

+2120
-230
lines changed

src/java.base/share/classes/com/sun/crypto/provider/DHKEM.java

Lines changed: 223 additions & 138 deletions
Large diffs are not rendered by default.

src/java.base/share/classes/com/sun/crypto/provider/HPKE.java

Lines changed: 588 additions & 0 deletions
Large diffs are not rendered by default.

src/java.base/share/classes/com/sun/crypto/provider/SunJCE.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ void putEntries() {
371371
ps("Cipher", "PBEWithHmacSHA512/256AndAES_256",
372372
"com.sun.crypto.provider.PBES2Core$HmacSHA512_256AndAES_256");
373373

374+
ps("Cipher", "HPKE", "com.sun.crypto.provider.HPKE");
375+
374376
/*
375377
* Key(pair) Generator engines
376378
*/

src/java.base/share/classes/javax/crypto/spec/HPKEParameterSpec.java

Lines changed: 443 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
import javax.crypto.Cipher;
26+
import javax.crypto.spec.HPKEParameterSpec;
27+
import java.nio.charset.StandardCharsets;
28+
import java.security.KeyPair;
29+
import java.security.KeyPairGenerator;
30+
import java.util.Arrays;
31+
import java.util.HexFormat;
32+
33+
class PackageSnippets {
34+
public static void main(String[] args) throws Exception {
35+
36+
// @start region="hpke-spec-example"
37+
// Recipient key pair generation
38+
KeyPairGenerator g = KeyPairGenerator.getInstance("X25519");
39+
KeyPair kp = g.generateKeyPair();
40+
41+
// The HPKE sender cipher is initialized with the recipient's public
42+
// key and an HPKEParameterSpec using specified algorithm identifiers
43+
// and application-supplied info.
44+
Cipher senderCipher = Cipher.getInstance("HPKE");
45+
HPKEParameterSpec ps = HPKEParameterSpec.of(
46+
HPKEParameterSpec.KEM_DHKEM_X25519_HKDF_SHA256,
47+
HPKEParameterSpec.KDF_HKDF_SHA256,
48+
HPKEParameterSpec.AEAD_AES_128_GCM)
49+
.withInfo(HexFormat.of().parseHex("010203040506"));
50+
senderCipher.init(Cipher.ENCRYPT_MODE, kp.getPublic(), ps);
51+
52+
// Retrieve the key encapsulation message (from the KEM step) from
53+
// the sender.
54+
byte[] kemEncap = senderCipher.getIV();
55+
56+
// The HPKE recipient cipher is initialized with its own private key,
57+
// an HPKEParameterSpec using the same algorithm identifiers as used by
58+
// the sender, and the key encapsulation message from the sender.
59+
Cipher recipientCipher = Cipher.getInstance("HPKE");
60+
HPKEParameterSpec pr = HPKEParameterSpec.of(
61+
HPKEParameterSpec.KEM_DHKEM_X25519_HKDF_SHA256,
62+
HPKEParameterSpec.KDF_HKDF_SHA256,
63+
HPKEParameterSpec.AEAD_AES_128_GCM)
64+
.withInfo(HexFormat.of().parseHex("010203040506"))
65+
.withEncapsulation(kemEncap);
66+
recipientCipher.init(Cipher.DECRYPT_MODE, kp.getPrivate(), pr);
67+
68+
// Encryption and decryption
69+
byte[] msg = "Hello World".getBytes(StandardCharsets.UTF_8);
70+
byte[] ct = senderCipher.doFinal(msg);
71+
byte[] pt = recipientCipher.doFinal(ct);
72+
73+
assert Arrays.equals(msg, pt);
74+
// @end
75+
}
76+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package sun.security.util;
26+
27+
import javax.crypto.SecretKey;
28+
29+
/**
30+
* An interface for <code>SecretKey</code>s that support using its slice as a new
31+
* <code>SecretKey</code>.
32+
* <p>
33+
* This is mainly used by PKCS #11 implementations that support the
34+
* EXTRACT_KEY_FROM_KEY mechanism even if the key itself is sensitive
35+
* and non-extractable.
36+
*/
37+
public interface SliceableSecretKey {
38+
39+
/**
40+
* Returns a slice as a new <code>SecretKey</code>.
41+
*
42+
* @param alg the new algorithm name
43+
* @param from the byte offset of the new key in the full key
44+
* @param to the to offset (exclusive) of the new key in the full key
45+
* @return the new key
46+
* @throws ArrayIndexOutOfBoundsException for improper <code>from</code>
47+
* and <code>to</code> values
48+
* @throws UnsupportedOperationException if slicing is not supported
49+
*/
50+
SecretKey slice(String alg, int from, int to);
51+
}

0 commit comments

Comments
 (0)