Skip to content

Commit aca10f3

Browse files
committed
EC-related updates from bc-java
1 parent 4337e11 commit aca10f3

File tree

67 files changed

+959
-672
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+959
-672
lines changed

crypto/BouncyCastle.Android.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,12 +1259,14 @@
12591259
<Compile Include="src\math\ec\multiplier\FixedPointPreCompInfo.cs" />
12601260
<Compile Include="src\math\ec\multiplier\FixedPointUtilities.cs" />
12611261
<Compile Include="src\math\ec\multiplier\GlvMultiplier.cs" />
1262+
<Compile Include="src\math\ec\multiplier\IPreCompCallback.cs" />
12621263
<Compile Include="src\math\ec\multiplier\MixedNafR2LMultiplier.cs" />
12631264
<Compile Include="src\math\ec\multiplier\MontgomeryLadderMultiplier.cs" />
12641265
<Compile Include="src\math\ec\multiplier\NafL2RMultiplier.cs" />
12651266
<Compile Include="src\math\ec\multiplier\NafR2LMultiplier.cs" />
12661267
<Compile Include="src\math\ec\multiplier\PreCompInfo.cs" />
12671268
<Compile Include="src\math\ec\multiplier\ReferenceMultiplier.cs" />
1269+
<Compile Include="src\math\ec\multiplier\ValidityPreCompInfo.cs" />
12681270
<Compile Include="src\math\ec\multiplier\WNafL2RMultiplier.cs" />
12691271
<Compile Include="src\math\ec\multiplier\WNafPreCompInfo.cs" />
12701272
<Compile Include="src\math\ec\multiplier\WNafUtilities.cs" />

crypto/BouncyCastle.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,12 +1253,14 @@
12531253
<Compile Include="src\math\ec\multiplier\FixedPointPreCompInfo.cs" />
12541254
<Compile Include="src\math\ec\multiplier\FixedPointUtilities.cs" />
12551255
<Compile Include="src\math\ec\multiplier\GlvMultiplier.cs" />
1256+
<Compile Include="src\math\ec\multiplier\IPreCompCallback.cs" />
12561257
<Compile Include="src\math\ec\multiplier\MixedNafR2LMultiplier.cs" />
12571258
<Compile Include="src\math\ec\multiplier\MontgomeryLadderMultiplier.cs" />
12581259
<Compile Include="src\math\ec\multiplier\NafL2RMultiplier.cs" />
12591260
<Compile Include="src\math\ec\multiplier\NafR2LMultiplier.cs" />
12601261
<Compile Include="src\math\ec\multiplier\PreCompInfo.cs" />
12611262
<Compile Include="src\math\ec\multiplier\ReferenceMultiplier.cs" />
1263+
<Compile Include="src\math\ec\multiplier\ValidityPreCompInfo.cs" />
12621264
<Compile Include="src\math\ec\multiplier\WNafL2RMultiplier.cs" />
12631265
<Compile Include="src\math\ec\multiplier\WNafPreCompInfo.cs" />
12641266
<Compile Include="src\math\ec\multiplier\WNafUtilities.cs" />

crypto/BouncyCastle.iOS.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,12 +1254,14 @@
12541254
<Compile Include="src\math\ec\multiplier\FixedPointPreCompInfo.cs" />
12551255
<Compile Include="src\math\ec\multiplier\FixedPointUtilities.cs" />
12561256
<Compile Include="src\math\ec\multiplier\GlvMultiplier.cs" />
1257+
<Compile Include="src\math\ec\multiplier\IPreCompCallback.cs" />
12571258
<Compile Include="src\math\ec\multiplier\MixedNafR2LMultiplier.cs" />
12581259
<Compile Include="src\math\ec\multiplier\MontgomeryLadderMultiplier.cs" />
12591260
<Compile Include="src\math\ec\multiplier\NafL2RMultiplier.cs" />
12601261
<Compile Include="src\math\ec\multiplier\NafR2LMultiplier.cs" />
12611262
<Compile Include="src\math\ec\multiplier\PreCompInfo.cs" />
12621263
<Compile Include="src\math\ec\multiplier\ReferenceMultiplier.cs" />
1264+
<Compile Include="src\math\ec\multiplier\ValidityPreCompInfo.cs" />
12631265
<Compile Include="src\math\ec\multiplier\WNafL2RMultiplier.cs" />
12641266
<Compile Include="src\math\ec\multiplier\WNafPreCompInfo.cs" />
12651267
<Compile Include="src\math\ec\multiplier\WNafUtilities.cs" />

crypto/crypto.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6153,6 +6153,11 @@
61536153
SubType = "Code"
61546154
BuildAction = "Compile"
61556155
/>
6156+
<File
6157+
RelPath = "src\math\ec\multiplier\IPreCompCallback.cs"
6158+
SubType = "Code"
6159+
BuildAction = "Compile"
6160+
/>
61566161
<File
61576162
RelPath = "src\math\ec\multiplier\MixedNafR2LMultiplier.cs"
61586163
SubType = "Code"
@@ -6183,6 +6188,11 @@
61836188
SubType = "Code"
61846189
BuildAction = "Compile"
61856190
/>
6191+
<File
6192+
RelPath = "src\math\ec\multiplier\ValidityPreCompInfo.cs"
6193+
SubType = "Code"
6194+
BuildAction = "Compile"
6195+
/>
61866196
<File
61876197
RelPath = "src\math\ec\multiplier\WNafL2RMultiplier.cs"
61886198
SubType = "Code"

crypto/src/asn1/cryptopro/ECGOST3410NamedCurves.cs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ static ECGost3410NamedCurves()
3232
mod_p, // p
3333
new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639316"), // a
3434
new BigInteger("166"), // b
35-
mod_q,
36-
BigInteger.One);
35+
mod_q, BigInteger.One);
3736

3837
ECDomainParameters ecParams = new ECDomainParameters(
3938
curve,
4039
curve.CreatePoint(
4140
new BigInteger("1"), // x
4241
new BigInteger("64033881142927202683649881450433473985931760268884941288852745803908878638612")), // y
43-
mod_q);
42+
mod_q, BigInteger.One);
4443

4544
parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProA] = ecParams;
4645

@@ -51,15 +50,14 @@ static ECGost3410NamedCurves()
5150
mod_p, // p
5251
new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639316"),
5352
new BigInteger("166"),
54-
mod_q,
55-
BigInteger.One);
53+
mod_q, BigInteger.One);
5654

5755
ecParams = new ECDomainParameters(
5856
curve,
5957
curve.CreatePoint(
6058
new BigInteger("1"), // x
6159
new BigInteger("64033881142927202683649881450433473985931760268884941288852745803908878638612")), // y
62-
mod_q);
60+
mod_q, BigInteger.One);
6361

6462
parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchA] = ecParams;
6563

@@ -70,15 +68,14 @@ static ECGost3410NamedCurves()
7068
mod_p, // p
7169
new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564823190"), // a
7270
new BigInteger("28091019353058090096996979000309560759124368558014865957655842872397301267595"), // b
73-
mod_q,
74-
BigInteger.One);
71+
mod_q, BigInteger.One);
7572

7673
ecParams = new ECDomainParameters(
7774
curve,
7875
curve.CreatePoint(
7976
new BigInteger("1"), // x
8077
new BigInteger("28792665814854611296992347458380284135028636778229113005756334730996303888124")), // y
81-
mod_q); // q
78+
mod_q, BigInteger.One);
8279

8380
parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProB] = ecParams;
8481

@@ -89,15 +86,14 @@ static ECGost3410NamedCurves()
8986
mod_p, // p
9087
new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502616"),
9188
new BigInteger("32858"),
92-
mod_q,
93-
BigInteger.One);
89+
mod_q, BigInteger.One);
9490

9591
ecParams = new ECDomainParameters(
9692
curve,
9793
curve.CreatePoint(
9894
new BigInteger("0"),
9995
new BigInteger("29818893917731240733471273240314769927240550812383695689146495261604565990247")),
100-
mod_q);
96+
mod_q, BigInteger.One);
10197

10298
parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProXchB] = ecParams;
10399

@@ -107,15 +103,14 @@ static ECGost3410NamedCurves()
107103
mod_p, // p
108104
new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502616"), // a
109105
new BigInteger("32858"), // b
110-
mod_q,
111-
BigInteger.One);
106+
mod_q, BigInteger.One);
112107

113108
ecParams = new ECDomainParameters(
114109
curve,
115110
curve.CreatePoint(
116111
new BigInteger("0"), // x
117112
new BigInteger("29818893917731240733471273240314769927240550812383695689146495261604565990247")), // y
118-
mod_q); // q
113+
mod_q, BigInteger.One);
119114

120115
parameters[CryptoProObjectIdentifiers.GostR3410x2001CryptoProC] = ecParams;
121116

@@ -126,15 +121,14 @@ static ECGost3410NamedCurves()
126121
mod_p, // p
127122
new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639316"), // a
128123
new BigInteger("166"), // b
129-
mod_q,
130-
BigInteger.One);
124+
mod_q, BigInteger.One);
131125

132126
ecParams = new ECDomainParameters(
133127
curve,
134128
curve.CreatePoint(
135129
new BigInteger("1"), // x
136130
new BigInteger("64033881142927202683649881450433473985931760268884941288852745803908878638612")), // y
137-
mod_q); // q
131+
mod_q, BigInteger.One);
138132

139133
parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA] = ecParams;
140134

@@ -144,15 +138,14 @@ static ECGost3410NamedCurves()
144138
mod_p, // p
145139
new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDC4",16), // a
146140
new BigInteger("E8C2505DEDFC86DDC1BD0B2B6667F1DA34B82574761CB0E879BD081CFD0B6265EE3CB090F30D27614CB4574010DA90DD862EF9D4EBEE4761503190785A71C760",16), // b
147-
mod_q,
148-
BigInteger.One);
141+
mod_q, BigInteger.One);
149142

150143
ecParams = new ECDomainParameters(
151144
curve,
152145
curve.CreatePoint(
153146
new BigInteger("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003"), // x
154147
new BigInteger("7503CFE87A836AE3A61B8816E25450E6CE5E1C93ACF1ABC1778064FDCBEFA921DF1626BE4FD036E93D75E6A50E3A41E98028FE5FC235F5B889A589CB5215F2A4",16)), // y
155-
mod_q); // q
148+
mod_q, BigInteger.One);
156149

157150
parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA] = ecParams;
158151

@@ -162,15 +155,14 @@ static ECGost3410NamedCurves()
162155
mod_p, // p
163156
new BigInteger("8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006C",16), // a
164157
new BigInteger("687D1B459DC841457E3E06CF6F5E2517B97C7D614AF138BCBF85DC806C4B289F3E965D2DB1416D217F8B276FAD1AB69C50F78BEE1FA3106EFB8CCBC7C5140116",16), // b
165-
mod_q,
166-
BigInteger.One);
158+
mod_q, BigInteger.One);
167159

168160
ecParams = new ECDomainParameters(
169161
curve,
170162
curve.CreatePoint(
171163
new BigInteger("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002"), // x
172164
new BigInteger("1A8F7EDA389B094C2C071E3647A8940F3C123B697578C213BE6DD9E6C8EC7335DCB228FD1EDF4A39152CBCAAF8C0398828041055F94CEEEC7E21340780FE41BD",16)), // y
173-
mod_q); // q
165+
mod_q, BigInteger.One);
174166

175167
parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetB] = ecParams;
176168

@@ -180,15 +172,14 @@ static ECGost3410NamedCurves()
180172
mod_p, // p
181173
new BigInteger("DC9203E514A721875485A529D2C722FB187BC8980EB866644DE41C68E143064546E861C0E2C9EDD92ADE71F46FCF50FF2AD97F951FDA9F2A2EB6546F39689BD3",16), // a
182174
new BigInteger("B4C4EE28CEBC6C2C8AC12952CF37F16AC7EFB6A9F69F4B57FFDA2E4F0DE5ADE038CBC2FFF719D2C18DE0284B8BFEF3B52B8CC7A5F5BF0A3C8D2319A5312557E1",16), // b
183-
mod_q,
184-
BigInteger.One);
175+
mod_q, BigInteger.One);
185176

186177
ecParams = new ECDomainParameters(
187178
curve,
188179
curve.CreatePoint(
189180
new BigInteger("E2E31EDFC23DE7BDEBE241CE593EF5DE2295B7A9CBAEF021D385F7074CEA043AA27272A7AE602BF2A7B9033DB9ED3610C6FB85487EAE97AAC5BC7928C1950148", 16), // x
190181
new BigInteger("F5CE40D95B5EB899ABBCCFF5911CB8577939804D6527378B8C108C3D2090FF9BE18E2D33E3021ED2EF32D85822423B6304F726AA854BAE07D0396E9A9ADDC40F",16)), // y
191-
mod_q); // q
182+
mod_q, BigInteger.One);
192183

193184
parameters[RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetC] = ecParams;
194185

crypto/src/asn1/x9/X962NamedCurves.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected override X9ECParameters CreateParameters()
3131
BigInteger h = BigInteger.One;
3232

3333
ECCurve cFp192v1 = new FpCurve(
34-
new BigInteger("6277101735386680763835789423207666416083908700390324961279"),
34+
new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", 16),
3535
new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
3636
new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16),
3737
n, h);
@@ -58,7 +58,7 @@ protected override X9ECParameters CreateParameters()
5858
BigInteger h = BigInteger.One;
5959

6060
ECCurve cFp192v2 = new FpCurve(
61-
new BigInteger("6277101735386680763835789423207666416083908700390324961279"),
61+
new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", 16),
6262
new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
6363
new BigInteger("cc22d6dfb95c6b25e49c0d6364a4e5980c393aa21668d953", 16),
6464
n, h);
@@ -85,7 +85,7 @@ protected override X9ECParameters CreateParameters()
8585
BigInteger h = BigInteger.One;
8686

8787
ECCurve cFp192v3 = new FpCurve(
88-
new BigInteger("6277101735386680763835789423207666416083908700390324961279"),
88+
new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", 16),
8989
new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
9090
new BigInteger("22123dc2395a05caa7423daeccc94760a7d462256bd56916", 16),
9191
n, h);

crypto/src/asn1/x9/X9Curve.cs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,19 @@ public X9Curve(
4747
}
4848
}
4949

50+
[Obsolete("Use constructor including order/cofactor")]
5051
public X9Curve(
5152
X9FieldID fieldID,
5253
Asn1Sequence seq)
54+
: this(fieldID, null, null, seq)
55+
{
56+
}
57+
58+
public X9Curve(
59+
X9FieldID fieldID,
60+
BigInteger order,
61+
BigInteger cofactor,
62+
Asn1Sequence seq)
5363
{
5464
if (fieldID == null)
5565
throw new ArgumentNullException("fieldID");
@@ -60,47 +70,47 @@ public X9Curve(
6070

6171
if (fieldIdentifier.Equals(X9ObjectIdentifiers.PrimeField))
6272
{
63-
BigInteger q = ((DerInteger) fieldID.Parameters).Value;
64-
X9FieldElement x9A = new X9FieldElement(q, (Asn1OctetString) seq[0]);
65-
X9FieldElement x9B = new X9FieldElement(q, (Asn1OctetString) seq[1]);
66-
curve = new FpCurve(q, x9A.Value.ToBigInteger(), x9B.Value.ToBigInteger());
73+
BigInteger p = ((DerInteger)fieldID.Parameters).Value;
74+
BigInteger A = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets());
75+
BigInteger B = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets());
76+
curve = new FpCurve(p, A, B, order, cofactor);
6777
}
68-
else
78+
else if (fieldIdentifier.Equals(X9ObjectIdentifiers.CharacteristicTwoField))
6979
{
70-
if (fieldIdentifier.Equals(X9ObjectIdentifiers.CharacteristicTwoField))
80+
// Characteristic two field
81+
DerSequence parameters = (DerSequence)fieldID.Parameters;
82+
int m = ((DerInteger)parameters[0]).Value.IntValue;
83+
DerObjectIdentifier representation
84+
= (DerObjectIdentifier)parameters[1];
85+
86+
int k1 = 0;
87+
int k2 = 0;
88+
int k3 = 0;
89+
if (representation.Equals(X9ObjectIdentifiers.TPBasis))
7190
{
72-
// Characteristic two field
73-
DerSequence parameters = (DerSequence)fieldID.Parameters;
74-
int m = ((DerInteger)parameters[0]).Value.IntValue;
75-
DerObjectIdentifier representation
76-
= (DerObjectIdentifier)parameters[1];
77-
78-
int k1 = 0;
79-
int k2 = 0;
80-
int k3 = 0;
81-
if (representation.Equals(X9ObjectIdentifiers.TPBasis))
82-
{
83-
// Trinomial basis representation
84-
k1 = ((DerInteger)parameters[2]).Value.IntValue;
85-
}
86-
else
87-
{
88-
// Pentanomial basis representation
89-
DerSequence pentanomial = (DerSequence) parameters[2];
90-
k1 = ((DerInteger) pentanomial[0]).Value.IntValue;
91-
k2 = ((DerInteger) pentanomial[1]).Value.IntValue;
92-
k3 = ((DerInteger) pentanomial[2]).Value.IntValue;
93-
}
94-
X9FieldElement x9A = new X9FieldElement(m, k1, k2, k3, (Asn1OctetString)seq[0]);
95-
X9FieldElement x9B = new X9FieldElement(m, k1, k2, k3, (Asn1OctetString)seq[1]);
96-
// TODO Is it possible to get the order (n) and cofactor(h) too?
97-
curve = new F2mCurve(m, k1, k2, k3, x9A.Value.ToBigInteger(), x9B.Value.ToBigInteger());
91+
// Trinomial basis representation
92+
k1 = ((DerInteger)parameters[2]).Value.IntValue;
9893
}
94+
else
95+
{
96+
// Pentanomial basis representation
97+
DerSequence pentanomial = (DerSequence) parameters[2];
98+
k1 = ((DerInteger) pentanomial[0]).Value.IntValue;
99+
k2 = ((DerInteger) pentanomial[1]).Value.IntValue;
100+
k3 = ((DerInteger) pentanomial[2]).Value.IntValue;
101+
}
102+
BigInteger A = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets());
103+
BigInteger B = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets());
104+
curve = new F2mCurve(m, k1, k2, k3, A, B, order, cofactor);
105+
}
106+
else
107+
{
108+
throw new ArgumentException("This type of ECCurve is not implemented");
99109
}
100110

101111
if (seq.Count == 3)
102112
{
103-
seed = ((DerBitString) seq[2]).GetBytes();
113+
seed = ((DerBitString)seq[2]).GetBytes();
104114
}
105115
}
106116

0 commit comments

Comments
 (0)