11package org .bouncycastle .jcajce .provider .asymmetric .ec ;
22
3- import java .math .BigInteger ;
43import java .security .InvalidAlgorithmParameterException ;
54import java .security .InvalidKeyException ;
65import java .security .Key ;
98import java .security .SecureRandom ;
109import java .security .spec .AlgorithmParameterSpec ;
1110
12- import org .bouncycastle .asn1 .x9 .X9IntegerConverter ;
13- import org .bouncycastle .crypto .BasicAgreement ;
14- import org .bouncycastle .crypto .CipherParameters ;
15- import org .bouncycastle .crypto .DerivationFunction ;
1611import org .bouncycastle .crypto .agreement .SM2KeyExchange ;
1712import org .bouncycastle .crypto .params .ECPrivateKeyParameters ;
1813import org .bouncycastle .crypto .params .ECPublicKeyParameters ;
2116import org .bouncycastle .crypto .params .SM2KeyExchangePublicParameters ;
2217import org .bouncycastle .jcajce .provider .asymmetric .util .BaseAgreementSpi ;
2318import org .bouncycastle .jcajce .spec .SM2KeyExchangeSpec ;
24- import org .bouncycastle .jce .interfaces .ECPrivateKey ;
2519import org .bouncycastle .util .Arrays ;
2620
2721
2822public class GMKeyExchangeSpi
2923 extends BaseAgreementSpi
3024{
31- private static final X9IntegerConverter converter = new X9IntegerConverter ();
32-
3325 private final String kaAlgorithm ;
34- private ParametersWithID parameters ;
35- private final BasicAgreement agreement ;
26+ private final SM2KeyExchange engine ;
3627 private SM2KeyExchangeSpec spec ;
3728 private byte [] result ;
3829
39- protected GMKeyExchangeSpi (
40- String kaAlgorithm ,
41- BasicAgreement agreement ,
42- DerivationFunction kdf )
30+ protected GMKeyExchangeSpi (String kaAlgorithm )
4331 {
44- super (kaAlgorithm , kdf );
32+ super (kaAlgorithm , null );
4533
4634 this .kaAlgorithm = kaAlgorithm ;
47- this .agreement = agreement ;
48- }
49-
50- protected byte [] bigIntToBytes (
51- BigInteger r , BCECPublicKey key )
52- {
53- return converter .integerToBytes (r , converter .getByteLength (key .engineGetSpec ().getCurve ()));
35+ this .engine = new SM2KeyExchange ();
5436 }
5537
5638 protected Key engineDoPhase (
5739 Key key ,
5840 boolean lastPhase )
5941 throws InvalidKeyException , IllegalStateException
6042 {
61- if (parameters == null )
43+ if (spec == null )
6244 {
6345 throw new IllegalStateException (kaAlgorithm + " not initialised." );
6446 }
@@ -73,16 +55,13 @@ protected Key engineDoPhase(
7355 throw new InvalidKeyException (kaAlgorithm + " key agreement requires "
7456 + getSimpleName (BCECPublicKey .class ) + " for doPhase" );
7557 }
76- BCECPublicKey k = (BCECPublicKey )key ;
77- ECPublicKeyParameters staticKey = (ECPublicKeyParameters )
78- ECUtils .generatePublicKeyParameter ((PublicKey )key );
79- ECPublicKeyParameters ephemKey = (ECPublicKeyParameters )
80- ECUtils .generatePublicKeyParameter (spec .getOtherPartyEphemeralKey ());
58+ ECPublicKeyParameters staticKey = (ECPublicKeyParameters )ECUtils .generatePublicKeyParameter ((PublicKey )key );
59+ ECPublicKeyParameters ephemeralKey = (ECPublicKeyParameters )ECUtils .generatePublicKeyParameter (spec .getOtherPartyEphemeralKey ());
8160
82- ParametersWithID parameters = new ParametersWithID (new SM2KeyExchangePublicParameters (staticKey , ephemKey ),
61+ ParametersWithID parameters = new ParametersWithID (new SM2KeyExchangePublicParameters (staticKey , ephemeralKey ),
8362 spec .getOtherPartyId ());
8463
85- result = bigIntToBytes ( agreement . calculateAgreement ( parameters ), k );
64+ result = engine . calculateKey ( 128 , parameters );
8665
8766 return null ;
8867 }
@@ -98,15 +77,14 @@ protected void doInitFromKey(Key key, AlgorithmParameterSpec parameterSpec, Secu
9877 if (!(key instanceof PrivateKey ))
9978 {
10079 throw new InvalidKeyException (kaAlgorithm + " key agreement requires "
101- + getSimpleName (ECPrivateKey .class ) + " for initialisation" );
80+ + getSimpleName (BCECPrivateKey .class ) + " for initialisation" );
10281 }
10382 spec = (SM2KeyExchangeSpec )parameterSpec ;
104- byte [] id = spec .getId ();
10583
10684 ECPrivateKeyParameters staticKey = (ECPrivateKeyParameters )ECUtils .generatePrivateKeyParameter ((PrivateKey )key );
10785 ECPrivateKeyParameters ephemeralKey = (ECPrivateKeyParameters )ECUtils .generatePrivateKeyParameter (spec .getEphemeralPrivateKey ());
108- this . parameters = new ParametersWithID (new SM2KeyExchangePrivateParameters (spec .isInitiator (), staticKey , ephemeralKey ), id );
109- agreement .init (parameters );
86+ ParametersWithID parameters = new ParametersWithID (new SM2KeyExchangePrivateParameters (spec .isInitiator (), staticKey , ephemeralKey ), spec . getId () );
87+ engine .init (parameters );
11088 }
11189
11290 private static String getSimpleName (Class clazz )
@@ -126,39 +104,7 @@ public static class SM2
126104 {
127105 public SM2 ()
128106 {
129- super ("SM2" , new SM2KeyAgreement (), null );
130- }
131- }
132-
133- private static class SM2KeyAgreement
134- implements BasicAgreement
135- {
136- private final SM2KeyExchange engine ;
137- private int fieldSize ;
138- public SM2KeyAgreement ()
139- {
140- engine = new SM2KeyExchange ();
141- }
142-
143- @ Override
144- public void init (CipherParameters param )
145- {
146- engine .init (param );
147- fieldSize = ((SM2KeyExchangePrivateParameters )((ParametersWithID )param ).getParameters ()).getStaticPrivateKey ()
148- .getParameters ().getCurve ().getFieldElementEncodingLength ();
149- }
150-
151- @ Override
152- public int getFieldSize ()
153- {
154- return fieldSize ;
155- }
156-
157- @ Override
158- public BigInteger calculateAgreement (CipherParameters pubKey )
159- {
160- byte [] rlt = engine .calculateKey (128 , pubKey );
161- return new BigInteger (1 , rlt );
107+ super ("SM2" );
162108 }
163109 }
164110}
0 commit comments