11using System ;
2+ using System . Diagnostics ;
23
34using Org . BouncyCastle . Crypto . Digests ;
45using Org . BouncyCastle . Crypto . Parameters ;
@@ -40,15 +41,15 @@ public virtual void Init(ICipherParameters privParam)
4041 {
4142 SM2KeyExchangePrivateParameters baseParam ;
4243
43- if ( privParam is ParametersWithID )
44+ if ( privParam is ParametersWithID withID )
4445 {
45- baseParam = ( SM2KeyExchangePrivateParameters ) ( ( ParametersWithID ) privParam ) . Parameters ;
46- mUserID = ( ( ParametersWithID ) privParam ) . GetID ( ) ;
46+ baseParam = ( SM2KeyExchangePrivateParameters ) withID . Parameters ;
47+ mUserID = CheckUserID ( withID . GetID ( ) ) ;
4748 }
4849 else
4950 {
5051 baseParam = ( SM2KeyExchangePrivateParameters ) privParam ;
51- mUserID = new byte [ 0 ] ;
52+ mUserID = Array . Empty < byte > ( ) ;
5253 }
5354
5455 mInitiator = baseParam . IsInitiator ;
@@ -65,15 +66,15 @@ public virtual byte[] CalculateKey(int kLen, ICipherParameters pubParam)
6566 SM2KeyExchangePublicParameters otherPub ;
6667 byte [ ] otherUserID ;
6768
68- if ( pubParam is ParametersWithID )
69+ if ( pubParam is ParametersWithID withID )
6970 {
70- otherPub = ( SM2KeyExchangePublicParameters ) ( ( ParametersWithID ) pubParam ) . Parameters ;
71- otherUserID = ( ( ParametersWithID ) pubParam ) . GetID ( ) ;
71+ otherPub = ( SM2KeyExchangePublicParameters ) withID . Parameters ;
72+ otherUserID = CheckUserID ( withID . GetID ( ) ) ;
7273 }
7374 else
7475 {
7576 otherPub = ( SM2KeyExchangePublicParameters ) pubParam ;
76- otherUserID = new byte [ 0 ] ;
77+ otherUserID = Array . Empty < byte > ( ) ;
7778 }
7879
7980 byte [ ] za = GetZ ( mDigest , mUserID , mStaticPubPoint ) ;
@@ -99,15 +100,15 @@ public virtual byte[][] CalculateKeyWithConfirmation(int kLen, byte[] confirmati
99100 SM2KeyExchangePublicParameters otherPub ;
100101 byte [ ] otherUserID ;
101102
102- if ( pubParam is ParametersWithID )
103+ if ( pubParam is ParametersWithID withID )
103104 {
104- otherPub = ( SM2KeyExchangePublicParameters ) ( ( ParametersWithID ) pubParam ) . Parameters ;
105- otherUserID = ( ( ParametersWithID ) pubParam ) . GetID ( ) ;
105+ otherPub = ( SM2KeyExchangePublicParameters ) withID . Parameters ;
106+ otherUserID = CheckUserID ( withID . GetID ( ) ) ;
106107 }
107108 else
108109 {
109110 otherPub = ( SM2KeyExchangePublicParameters ) pubParam ;
110- otherUserID = new byte [ 0 ] ;
111+ otherUserID = Array . Empty < byte > ( ) ;
111112 }
112113
113114 if ( mInitiator && confirmationTag == null )
@@ -256,19 +257,29 @@ private byte[] GetZ(IDigest digest, byte[] userID, ECPoint pubPoint)
256257 return DigestUtilities . DoFinal ( digest ) ;
257258 }
258259
259- private void AddUserID ( IDigest digest , byte [ ] userID )
260+ private static void AddUserID ( IDigest digest , byte [ ] userID )
260261 {
261262 uint len = ( uint ) ( userID . Length * 8 ) ;
263+ Debug . Assert ( len >> 16 == 0 ) ;
262264
263265 digest . Update ( ( byte ) ( len >> 8 ) ) ;
264266 digest . Update ( ( byte ) len ) ;
265267 digest . BlockUpdate ( userID , 0 , userID . Length ) ;
266268 }
267269
268- private void AddFieldElement ( IDigest digest , ECFieldElement v )
270+ private static void AddFieldElement ( IDigest digest , ECFieldElement v )
269271 {
270272 byte [ ] p = v . GetEncoded ( ) ;
271273 digest . BlockUpdate ( p , 0 , p . Length ) ;
272274 }
275+
276+ private static byte [ ] CheckUserID ( byte [ ] userID )
277+ {
278+ // The length in bits must be expressible in two bytes
279+ if ( userID . Length >= 8192 )
280+ throw new ArgumentException ( "SM2 user ID must be less than 2^16 bits long" ) ;
281+
282+ return userID ;
283+ }
273284 }
274285}
0 commit comments