22
33import java .math .BigInteger ;
44
5- import org .bouncycastle .asn1 .nist .NISTNamedCurves ;
65import org .bouncycastle .crypto .Digest ;
6+ import org .bouncycastle .crypto .ec .CustomNamedCurves ;
77import org .bouncycastle .crypto .prng .EntropySource ;
88import org .bouncycastle .math .ec .ECCurve ;
99import org .bouncycastle .math .ec .ECMultiplier ;
@@ -36,23 +36,18 @@ public class DualECSP800DRBG
3636 private static final BigInteger p521_Qx = new BigInteger ("1b9fa3e518d683c6b65763694ac8efbaec6fab44f2276171a42726507dd08add4c3b3f4c1ebc5b1222ddba077f722943b24c3edfa0f85fe24d0c8c01591f0be6f63" , 16 );
3737 private static final BigInteger p521_Qy = new BigInteger ("1f3bdba585295d9a1110d1df1f9430ef8442c5018976ff3437ef91b81dc0b8132c8d5c39c32d0e004a3092b7d327c0e7a4d26d2c7b69b58f9066652911e457779de" , 16 );
3838
39- private static final DualECPoints [] nistPoints ;
40-
41- static
39+ private static final DualECPoints [] nistPoints = new DualECPoints []
4240 {
43- nistPoints = new DualECPoints [3 ];
44-
45- ECCurve .Fp curve = (ECCurve .Fp )NISTNamedCurves .getByNameLazy ("P-256" ).getCurve ();
46-
47- nistPoints [0 ] = new DualECPoints (128 , curve .createPoint (p256_Px , p256_Py ), curve .createPoint (p256_Qx , p256_Qy ), 1 );
48-
49- curve = (ECCurve .Fp )NISTNamedCurves .getByNameLazy ("P-384" ).getCurve ();
50-
51- nistPoints [1 ] = new DualECPoints (192 , curve .createPoint (p384_Px , p384_Py ), curve .createPoint (p384_Qx , p384_Qy ), 1 );
41+ createDualECPoints ("P-256" , 128 , p256_Px , p256_Py , p256_Qx , p256_Qy , 1 ),
42+ createDualECPoints ("P-384" , 192 , p384_Px , p384_Py , p384_Qx , p384_Qy , 1 ),
43+ createDualECPoints ("P-521" , 256 , p521_Px , p521_Py , p521_Qx , p521_Qy , 1 ),
44+ };
5245
53- curve = (ECCurve .Fp )NISTNamedCurves .getByNameLazy ("P-521" ).getCurve ();
54-
55- nistPoints [2 ] = new DualECPoints (256 , curve .createPoint (p521_Px , p521_Py ), curve .createPoint (p521_Qx , p521_Qy ), 1 );
46+ private static DualECPoints createDualECPoints (String curveName , int securityStrength , BigInteger Px ,
47+ BigInteger Py , BigInteger Qx , BigInteger Qy , int cofactor )
48+ {
49+ ECCurve .AbstractFp c = (ECCurve .AbstractFp )CustomNamedCurves .getByNameLazy (curveName ).getCurve ();
50+ return new DualECPoints (securityStrength , c .createPoint (Px , Py ), c .createPoint (Qx , Qy ), cofactor );
5651 }
5752
5853
@@ -67,7 +62,6 @@ public class DualECSP800DRBG
6762 private int _securityStrength ;
6863 private int _seedlen ;
6964 private int _outlen ;
70- private ECCurve .Fp _curve ;
7165 private ECPoint _P ;
7266 private ECPoint _Q ;
7367 private byte [] _s ;
@@ -210,11 +204,9 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
210204 {
211205 s = getScalarMultipleXCoord (_P , s );
212206
213- //System.err.println("S: " + new String(Hex.encode(_s)));
214-
215207 byte [] r = getScalarMultipleXCoord (_Q , s ).toByteArray ();
216208
217- if (r .length > _outlen )
209+ if (r .length >= _outlen )
218210 {
219211 System .arraycopy (r , r .length - _outlen , output , outOffset , _outlen );
220212 }
@@ -223,7 +215,6 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
223215 System .arraycopy (r , 0 , output , outOffset + (_outlen - r .length ), r .length );
224216 }
225217
226- //System.err.println("R: " + new String(Hex.encode(r)));
227218 outOffset += _outlen ;
228219
229220 _reseedCounter ++;
@@ -237,13 +228,17 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
237228
238229 int required = output .length - outOffset ;
239230
240- if (r .length > _outlen )
231+ if (r .length >= _outlen )
241232 {
242233 System .arraycopy (r , r .length - _outlen , output , outOffset , required );
243234 }
244235 else
245236 {
246- System .arraycopy (r , 0 , output , outOffset + (_outlen - r .length ), required );
237+ int outPos = _outlen - r .length ;
238+ if (outPos < required )
239+ {
240+ System .arraycopy (r , 0 , output , outOffset + outPos , required - outPos );
241+ }
247242 }
248243
249244 _reseedCounter ++;
0 commit comments