2
2
3
3
import java .math .BigInteger ;
4
4
5
- import org .bouncycastle .asn1 .nist .NISTNamedCurves ;
6
5
import org .bouncycastle .crypto .Digest ;
6
+ import org .bouncycastle .crypto .ec .CustomNamedCurves ;
7
7
import org .bouncycastle .crypto .prng .EntropySource ;
8
8
import org .bouncycastle .math .ec .ECCurve ;
9
9
import org .bouncycastle .math .ec .ECMultiplier ;
@@ -36,23 +36,18 @@ public class DualECSP800DRBG
36
36
private static final BigInteger p521_Qx = new BigInteger ("1b9fa3e518d683c6b65763694ac8efbaec6fab44f2276171a42726507dd08add4c3b3f4c1ebc5b1222ddba077f722943b24c3edfa0f85fe24d0c8c01591f0be6f63" , 16 );
37
37
private static final BigInteger p521_Qy = new BigInteger ("1f3bdba585295d9a1110d1df1f9430ef8442c5018976ff3437ef91b81dc0b8132c8d5c39c32d0e004a3092b7d327c0e7a4d26d2c7b69b58f9066652911e457779de" , 16 );
38
38
39
- private static final DualECPoints [] nistPoints ;
40
-
41
- static
39
+ private static final DualECPoints [] nistPoints = new DualECPoints []
42
40
{
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
+ };
52
45
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 );
56
51
}
57
52
58
53
@@ -67,7 +62,6 @@ public class DualECSP800DRBG
67
62
private int _securityStrength ;
68
63
private int _seedlen ;
69
64
private int _outlen ;
70
- private ECCurve .Fp _curve ;
71
65
private ECPoint _P ;
72
66
private ECPoint _Q ;
73
67
private byte [] _s ;
@@ -210,11 +204,9 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
210
204
{
211
205
s = getScalarMultipleXCoord (_P , s );
212
206
213
- //System.err.println("S: " + new String(Hex.encode(_s)));
214
-
215
207
byte [] r = getScalarMultipleXCoord (_Q , s ).toByteArray ();
216
208
217
- if (r .length > _outlen )
209
+ if (r .length >= _outlen )
218
210
{
219
211
System .arraycopy (r , r .length - _outlen , output , outOffset , _outlen );
220
212
}
@@ -223,7 +215,6 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
223
215
System .arraycopy (r , 0 , output , outOffset + (_outlen - r .length ), r .length );
224
216
}
225
217
226
- //System.err.println("R: " + new String(Hex.encode(r)));
227
218
outOffset += _outlen ;
228
219
229
220
_reseedCounter ++;
@@ -237,13 +228,17 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
237
228
238
229
int required = output .length - outOffset ;
239
230
240
- if (r .length > _outlen )
231
+ if (r .length >= _outlen )
241
232
{
242
233
System .arraycopy (r , r .length - _outlen , output , outOffset , required );
243
234
}
244
235
else
245
236
{
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
+ }
247
242
}
248
243
249
244
_reseedCounter ++;
0 commit comments