Skip to content

Commit c3413f8

Browse files
committed
Allow an SM9 key-exchange user key to be rebuilt from its encoding and the master public key, through SM9EncPrivateKeyParameters.fromEncodedExchangeKey and an exchangeKey flag on SM9EncUserPrivateKeySpec, so an exchange party served by the KGC no longer needs the master private key.
1 parent 14b8718 commit c3413f8

6 files changed

Lines changed: 154 additions & 19 deletions

File tree

‎core/src/main/java/org/bouncycastle/crypto/params/SM9EncPrivateKeyParameters.java‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
* security argument assumes is unavailable. Keeping the two usages on separate
2222
* keys (distinct hid, or distinct master keys as the GM/T 0044.5 examples do)
2323
* is what makes sharing the master key sound.
24+
* <p>
25+
* A key rebuilt from its encoding ({@link #fromEncoded} /
26+
* {@link #fromEncodedExchangeKey}) carries the usage the importer names - the
27+
* point encoding itself does not record which usage the KGC derived it for -
28+
* so an importer must claim the usage the key was actually derived under.
2429
*/
2530
public class SM9EncPrivateKeyParameters
2631
extends AsymmetricKeyParameter
@@ -98,9 +103,10 @@ public byte[] getEncoded()
98103
}
99104

100105
/**
101-
* Rebuild a KEM / decryption user key from its bare point encoding. There is
102-
* deliberately no key-exchange counterpart: nothing needs one yet, and an
103-
* exchange user key is obtained from the master key's derivation instead.
106+
* Rebuild a KEM / decryption user key from its bare point encoding. For a key
107+
* the KGC derived for the key exchange use {@link #fromEncodedExchangeKey}
108+
* instead - the usage is the importer's claim (see the class note), and the
109+
* consumers enforce whichever is claimed.
104110
*/
105111
public static SM9EncPrivateKeyParameters fromEncoded(
106112
byte[] enc, SM9EncMasterPublicKeyParameters masterPublicKey, byte[] identity, byte hid)
@@ -109,6 +115,31 @@ public static SM9EncPrivateKeyParameters fromEncoded(
109115
return new SM9EncPrivateKeyParameters(SM9G2Point.decode(enc), masterPublicKey, Arrays.clone(identity), hid, false);
110116
}
111117

118+
/**
119+
* Rebuild a key-exchange user key from its bare point encoding, under
120+
* {@link SM9EncMasterPrivateKeyParameters#HID_EXCHANGE} - the import path for
121+
* an exchange party that received its key from the KGC rather than deriving
122+
* it in-process via
123+
* {@link SM9EncMasterPrivateKeyParameters#generateExchangeKey(byte[])}.
124+
*/
125+
public static SM9EncPrivateKeyParameters fromEncodedExchangeKey(
126+
byte[] enc, SM9EncMasterPublicKeyParameters masterPublicKey, byte[] identity)
127+
{
128+
return fromEncodedExchangeKey(enc, masterPublicKey, identity, SM9EncMasterPrivateKeyParameters.HID_EXCHANGE);
129+
}
130+
131+
/**
132+
* Rebuild a key-exchange user key from its bare point encoding under an
133+
* explicit hid, for a KGC whose published exchange hid is not
134+
* {@link SM9EncMasterPrivateKeyParameters#HID_EXCHANGE} (the official English
135+
* edition's GM/T 0044.5 Annex B example runs the exchange under 0x03).
136+
*/
137+
public static SM9EncPrivateKeyParameters fromEncodedExchangeKey(
138+
byte[] enc, SM9EncMasterPublicKeyParameters masterPublicKey, byte[] identity, byte hid)
139+
{
140+
SM9EncMasterPrivateKeyParameters.checkHid(hid);
141+
return new SM9EncPrivateKeyParameters(SM9G2Point.decode(enc), masterPublicKey, Arrays.clone(identity), hid, true);
142+
}
112143

113144
/**
114145
* Destroy this object, dropping its reference to the private point de and

‎core/src/test/java/org/bouncycastle/crypto/test/SM9KeyExchangeTest.java‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Map;
88

99
import org.bouncycastle.crypto.agreement.SM9KeyExchange;
10+
import org.bouncycastle.crypto.kems.SM9KEMExtractor;
1011
import org.bouncycastle.crypto.params.SM9EncMasterPrivateKeyParameters;
1112
import org.bouncycastle.crypto.params.SM9EncPrivateKeyParameters;
1213
import org.bouncycastle.crypto.params.SM9EncUserKeyParametersGenerator;
@@ -26,7 +27,9 @@
2627
* crypto/sm9/sm9_keyexchange_hid03.txt); the hid is the KGC's published
2728
* choice, taken from the vector file. For each, the master public key, both
2829
* KGC-derived user keys, both parties' ephemeral values, the shared key and the
29-
* key-confirmation tags S_A / S_B are reproduced byte-for-byte.
30+
* key-confirmation tags S_A / S_B are reproduced byte-for-byte - with party A
31+
* running on a key rebuilt through fromEncodedExchangeKey, the import path for
32+
* a party holding its KGC-served key but not the master private key.
3033
*/
3134
public class SM9KeyExchangeTest
3235
extends SimpleTest
@@ -106,7 +109,15 @@ private void checkVector(String fileName)
106109
isTrue(fileName + " user key deB", Arrays.areEqual(
107110
deB.getPrivatePoint().getEncoded(), g2(v, "deB_x_hi", "deB_x_lo", "deB_y_hi", "deB_y_lo")));
108111

109-
SM9KeyExchange a = new SM9KeyExchange(deA, identityB, true);
112+
// party A runs on a key rebuilt from its point encoding - the import path a
113+
// party served by the KGC uses, no master private key involved - and must
114+
// reproduce the exchange byte-for-byte
115+
SM9EncPrivateKeyParameters deAImported = SM9EncPrivateKeyParameters.fromEncodedExchangeKey(
116+
deA.getEncoded(), master.getPublicKeyParameters(), identityA, hid);
117+
isTrue(fileName + " imported deA is an exchange key", deAImported.isExchangeKey());
118+
isTrue(fileName + " imported deA records its hid", deAImported.getHid() == hid);
119+
120+
SM9KeyExchange a = new SM9KeyExchange(deAImported, identityB, true);
110121
SM9KeyExchange b = new SM9KeyExchange(deB, identityA, false);
111122
ECPoint ra = a.generateEphemeral(new TestRandomBigInteger(256, hex(v, "rA")));
112123
ECPoint rb = b.generateEphemeral(new TestRandomBigInteger(256, hex(v, "rB")));
@@ -186,6 +197,36 @@ private void checkHidValidation()
186197
{
187198
isTrue("SM9 key exchange requires a key-exchange user key from generateExchangeKey".equals(e.getMessage()));
188199
}
200+
201+
// an exchange key rebuilt from its encoding carries the exchange usage,
202+
// defaults to the published exchange hid, and is rejected by the KEM side
203+
// exactly like a KGC-derived one
204+
SM9EncPrivateKeyParameters exchKey = master.generateExchangeKey(identity);
205+
SM9EncPrivateKeyParameters imported = SM9EncPrivateKeyParameters.fromEncodedExchangeKey(
206+
exchKey.getEncoded(), master.getPublicKeyParameters(), identity);
207+
isTrue("imported exchange key claims the exchange usage", imported.isExchangeKey());
208+
isTrue("imported exchange key defaults to HID_EXCHANGE",
209+
imported.getHid() == SM9EncMasterPrivateKeyParameters.HID_EXCHANGE);
210+
try
211+
{
212+
new SM9KEMExtractor(imported, 128);
213+
fail("SM9KEMExtractor accepted an imported key-exchange user key");
214+
}
215+
catch (IllegalArgumentException e)
216+
{
217+
isTrue("SM9 KEM decapsulation requires an encryption user key, not a key-exchange key".equals(e.getMessage()));
218+
}
219+
// and the import validates the hid the same way the derivation does
220+
try
221+
{
222+
SM9EncPrivateKeyParameters.fromEncodedExchangeKey(
223+
exchKey.getEncoded(), master.getPublicKeyParameters(), identity, (byte)0x04);
224+
fail("fromEncodedExchangeKey accepted hid 0x04");
225+
}
226+
catch (IllegalArgumentException e)
227+
{
228+
isTrue("hid must be HID (0x03) or HID_EXCHANGE (0x02)".equals(e.getMessage()));
229+
}
189230
}
190231

191232
private byte[] g2(Map v, String xHi, String xLo, String yHi, String yLo)

0 commit comments

Comments
 (0)