|
7 | 7 | import java.util.Map; |
8 | 8 |
|
9 | 9 | import org.bouncycastle.crypto.agreement.SM9KeyExchange; |
| 10 | +import org.bouncycastle.crypto.kems.SM9KEMExtractor; |
10 | 11 | import org.bouncycastle.crypto.params.SM9EncMasterPrivateKeyParameters; |
11 | 12 | import org.bouncycastle.crypto.params.SM9EncPrivateKeyParameters; |
12 | 13 | import org.bouncycastle.crypto.params.SM9EncUserKeyParametersGenerator; |
|
26 | 27 | * crypto/sm9/sm9_keyexchange_hid03.txt); the hid is the KGC's published |
27 | 28 | * choice, taken from the vector file. For each, the master public key, both |
28 | 29 | * 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. |
30 | 33 | */ |
31 | 34 | public class SM9KeyExchangeTest |
32 | 35 | extends SimpleTest |
@@ -106,7 +109,15 @@ private void checkVector(String fileName) |
106 | 109 | isTrue(fileName + " user key deB", Arrays.areEqual( |
107 | 110 | deB.getPrivatePoint().getEncoded(), g2(v, "deB_x_hi", "deB_x_lo", "deB_y_hi", "deB_y_lo"))); |
108 | 111 |
|
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); |
110 | 121 | SM9KeyExchange b = new SM9KeyExchange(deB, identityA, false); |
111 | 122 | ECPoint ra = a.generateEphemeral(new TestRandomBigInteger(256, hex(v, "rA"))); |
112 | 123 | ECPoint rb = b.generateEphemeral(new TestRandomBigInteger(256, hex(v, "rB"))); |
@@ -186,6 +197,36 @@ private void checkHidValidation() |
186 | 197 | { |
187 | 198 | isTrue("SM9 key exchange requires a key-exchange user key from generateExchangeKey".equals(e.getMessage())); |
188 | 199 | } |
| 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 | + } |
189 | 230 | } |
190 | 231 |
|
191 | 232 | private byte[] g2(Map v, String xHi, String xLo, String yHi, String yLo) |
|
0 commit comments