Skip to content

Commit 8ac0a1d

Browse files
committed
further work on GOST symmetric key standards
1 parent e6d9625 commit 8ac0a1d

File tree

14 files changed

+286
-217
lines changed

14 files changed

+286
-217
lines changed

CONTRIBUTORS.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@
285285
<li>N&uacute;ria Mar&iacute; &lt;numaa&#064hotmail.com&gt; - patch for alternate data type recoginition in CMSSignedDataParser.</li>
286286
<li>Janis Schuller &lt;js&#064tzi.de&gt; - addition of NotationData packets for OpenPGP.</li>
287287
<li>Michael Samblanet &lt;mike&#064samblanet.com&gt; - patches towards improved Sun/default provider support in CMS.</li>
288-
<li>Mike StJohns &lt;mstjohns&#064comcast.net&gt; - patches for supporting empty subject in X.509 certificate generation, noneWithECDSA.</li>
288+
<li>Mike StJohns &lt;mstjohns&#064comcast.net&gt; - patches for supporting empty subject in X.509 certificate generation, noneWithECDSA, updates to KeyPurposeId.</li>
289289
<li>Ramon Keller &lt;ramon.keller&#064gmx.ch&gt; - patch to deal with null revocations return from other CRL in X509V2CRLGenerator.</li>
290290
<li>Mark Nelson &lt;mark&#064nbr.com&gt; - correction to excluded DN in name constraints processing for PKIX processing.</li>
291291
<li>Eugene Golushkov &lt;eugene_gff&#064ukr.net&gt; - mask fix to single byte read in TlsInputStream.</li>
@@ -296,7 +296,6 @@
296296
<li>Tal Yacobi &lt;tal.yacobi&#064octavian-tech.com&gt; - fix for issue in OpenPGP examples [#BJA-55].</li>
297297
<li>Massimiliano Ziccardi &lt;massimiliano.ziccardi&#064gmail.comt&gt; - support for counter signature reading in CMS API, update for multiple counter signature attributes.</li>
298298
<li>Andrey Pavlenko &lt;andrey.a.pavlenko&#064gmail.com&gt; - security manager patch for PKCS1Encoding property check.</li>
299-
<li>Mike StJohns &lt;mstjohns&#064comcast.net&gt; - updates to KeyPurposeId</li>
300299
<li>J Ross Nicoll &lt;jrn&#064jrn.me.uk&gt; - improved exception handling for getInstance() in ASN.1 library.</li>
301300
<li>Matthew Stevenson &lt;mavricknz&#064yahoo.com&gt; - patch to construtor for CRMF CertSequence.</li>
302301
<li>Gabriele Contini &lt;gcontini&#064hotpop.com&gt; - identified a bug in ASN.1 library with handling of unterminated NDEF's.</li>
@@ -451,7 +450,7 @@
451450
<li>Anders Schack-Mulligen &lt;https://github.com/aschackmull&gt; code cleanups for CMSSignedDataParser, BrokenKDF2BytesGenerator.</li>
452451
<li>Sebastian Wolfgang Roland &lt;sebastianwolfgang.roland&#064stud.tu-darmstadt.de&gt; Initial XMSS/XMSS-MT implementation.</li>
453452
<li>didisoft &lt;https://github.com/didisoft&gt; test code for PGP signature removal involving user ids.</li>
454-
<li>Mike Safonov&lt;https://github.com/MikeSafonov&gt; initial implementation of GOST3410-2012 for light weight provider and JCA, parameters patches for ECGOST keys.</li>
453+
<li>Mike Safonov&lt;https://github.com/MikeSafonov&gt; initial implementation of GOST3410-2012 for light weight provider and JCA, parameters patches for ECGOST keys, initial implementation of GOST3412-2015.</li>
455454
<li>Artem Storozhuk &lt;storojs72&#064gmail.com&gt; initial implementation of DSTU7564 (digest) and DSTU7624 (cipher) and their associated modes.</li>
456455
<li>Andreas Glaser &lt;[email protected]&gt; patch to recognise ANSSI curves for PKCS#10 requests.</li>
457456
<li>codeborne &lt;https://github.com/cbxp&gt; patch to correct OIDs used in public key digest parameters for ECGOST-2012.</li>

core/src/main/java/org/bouncycastle/crypto/modes/G3413CBCBlockCipher.java

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.bouncycastle.crypto.BlockCipher;
44
import org.bouncycastle.crypto.CipherParameters;
55
import org.bouncycastle.crypto.DataLengthException;
6-
import org.bouncycastle.crypto.params.GOST3412ParametersWithIV;
76
import org.bouncycastle.crypto.params.ParametersWithIV;
7+
import org.bouncycastle.util.Arrays;
88

99
/**
1010
* An implementation of the CBC mode for GOST 3412 2015 cipher.
@@ -34,39 +34,22 @@ public G3413CBCBlockCipher(BlockCipher cipher)
3434
public void init(boolean forEncryption, CipherParameters params)
3535
throws IllegalArgumentException
3636
{
37-
3837
this.forEncryption = forEncryption;
3938
if (params instanceof ParametersWithIV)
4039
{
4140
ParametersWithIV ivParam = (ParametersWithIV)params;
4241

43-
setupDefaultParams();
44-
45-
initArrays();
46-
47-
R_init = GOST3413CipherUtil.initIV(ivParam.getIV(), m);
48-
System.arraycopy(R_init, 0, R, 0, R_init.length);
49-
42+
byte[] iv = ivParam.getIV();
5043

51-
// if null it's an IV changed only.
52-
if (ivParam.getParameters() != null)
44+
if (iv.length < blockSize)
5345
{
54-
cipher.init(forEncryption, ivParam.getParameters());
46+
throw new IllegalArgumentException("Parameter m must blockSize <= m");
5547
}
56-
57-
58-
}
59-
if (params instanceof GOST3412ParametersWithIV)
60-
{
61-
GOST3412ParametersWithIV ivParam = (GOST3412ParametersWithIV)params;
62-
63-
this.m = ivParam.getM() / 8;
64-
65-
validateParams();
48+
this.m = iv.length;
6649

6750
initArrays();
6851

69-
R_init = GOST3413CipherUtil.initIV(ivParam.getIV(), m);
52+
R_init = Arrays.clone(iv);
7053
System.arraycopy(R_init, 0, R, 0, R_init.length);
7154

7255
// if null it's an IV changed only.
@@ -77,7 +60,6 @@ public void init(boolean forEncryption, CipherParameters params)
7760
}
7861
else
7962
{
80-
8163
setupDefaultParams();
8264

8365
initArrays();
@@ -92,19 +74,7 @@ public void init(boolean forEncryption, CipherParameters params)
9274

9375
initialized = true;
9476
}
95-
96-
private void validateParams()
97-
throws IllegalArgumentException
98-
{
99-
100-
if (m < blockSize)
101-
{
102-
throw new IllegalArgumentException("Parameter m must blockSize <= m");
103-
}
104-
105-
}
106-
107-
77+
10878
/**
10979
* allocate memory for R and R_init arrays
11080
*/
@@ -190,7 +160,6 @@ private int decrypt(byte[] in, int inOff, byte[] out, int outOff)
190160
*/
191161
private void generateR(byte[] C)
192162
{
193-
194163
byte[] buf = GOST3413CipherUtil.LSB(R, m - blockSize);
195164
System.arraycopy(buf, 0, R, 0, buf.length);
196165
System.arraycopy(C, 0, R, buf.length, m - buf.length);

core/src/main/java/org/bouncycastle/crypto/modes/G3413CFBBlockCipher.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.bouncycastle.crypto.CipherParameters;
55
import org.bouncycastle.crypto.DataLengthException;
66
import org.bouncycastle.crypto.StreamBlockCipher;
7-
import org.bouncycastle.crypto.params.GOST3412ParametersWithIV;
87
import org.bouncycastle.crypto.params.ParametersWithIV;
98
import org.bouncycastle.util.Arrays;
109

@@ -73,14 +72,11 @@ public G3413CFBBlockCipher(BlockCipher cipher, int bitBlockSize)
7372
public void init(boolean forEncryption, CipherParameters params)
7473
throws IllegalArgumentException
7574
{
76-
7775
this.forEncryption = forEncryption;
7876
if (params instanceof ParametersWithIV)
7977
{
8078
ParametersWithIV ivParam = (ParametersWithIV)params;
8179

82-
setupDefaultParams();
83-
8480
byte[] iv = ivParam.getIV();
8581

8682
if (iv.length < blockSize)
@@ -91,7 +87,7 @@ public void init(boolean forEncryption, CipherParameters params)
9187

9288
initArrays();
9389

94-
R_init = GOST3413CipherUtil.initIV(ivParam.getIV(), iv.length);
90+
R_init = Arrays.clone(iv);
9591
System.arraycopy(R_init, 0, R, 0, R_init.length);
9692

9793

@@ -101,23 +97,6 @@ public void init(boolean forEncryption, CipherParameters params)
10197
cipher.init(true, ivParam.getParameters());
10298
}
10399
}
104-
else if (params instanceof GOST3412ParametersWithIV)
105-
{
106-
GOST3412ParametersWithIV ivParam = (GOST3412ParametersWithIV)params;
107-
108-
this.m = ivParam.getM() / 8;
109-
110-
initArrays();
111-
112-
R_init = GOST3413CipherUtil.initIV(ivParam.getIV(), m);
113-
System.arraycopy(R_init, 0, R, 0, R_init.length);
114-
System.err.println(ivParam.getIV().length);
115-
// if null it's an IV changed only.
116-
if (ivParam.getParameters() != null)
117-
{
118-
cipher.init(true, ivParam.getParameters());
119-
}
120-
}
121100
else
122101
{
123102
setupDefaultParams();

core/src/main/java/org/bouncycastle/crypto/modes/G3413CTRBlockCipher.java

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import org.bouncycastle.crypto.CipherParameters;
55
import org.bouncycastle.crypto.DataLengthException;
66
import org.bouncycastle.crypto.StreamBlockCipher;
7-
import org.bouncycastle.crypto.params.GOST3412ParametersWithIV;
87
import org.bouncycastle.crypto.params.ParametersWithIV;
8+
import org.bouncycastle.util.Arrays;
99

1010
/**
1111
* implements the GOST 3412 2015 CTR counter mode (GCTR).
@@ -48,6 +48,12 @@ public G3413CTRBlockCipher(BlockCipher cipher, int bitBlockSize)
4848
{
4949
super(cipher);
5050

51+
if (bitBlockSize < 0 || bitBlockSize > cipher.getBlockSize() * 8)
52+
{
53+
throw new IllegalArgumentException("Parameter bitBlockSize must be in range 0 < bitBlockSize <= "
54+
+ cipher.getBlockSize() * 8);
55+
}
56+
5157
this.cipher = cipher;
5258
this.blockSize = cipher.getBlockSize();
5359
this.s = bitBlockSize / 8;
@@ -77,35 +83,19 @@ public void init(
7783

7884
initArrays();
7985

80-
IV = GOST3413CipherUtil.initIV(ivParam.getIV(), IV.length);
81-
System.arraycopy(IV, 0, CTR, 0, IV.length);
82-
for (int i = IV.length; i < blockSize; i++)
83-
{
84-
CTR[i] = 0;
85-
}
86+
IV = Arrays.clone(ivParam.getIV());
8687

87-
// if null it's an IV changed only.
88-
if (ivParam.getParameters() != null)
88+
if (IV.length != blockSize / 2)
8989
{
90-
cipher.init(true, ivParam.getParameters());
90+
throw new IllegalArgumentException("Parameter IV length must be == blockSize/2");
9191
}
92-
}
93-
if (params instanceof GOST3412ParametersWithIV)
94-
{
95-
GOST3412ParametersWithIV ivParam = (GOST3412ParametersWithIV)params;
96-
97-
validateParams(ivParam.getIV().length);
9892

99-
initArrays();
100-
101-
IV = GOST3413CipherUtil.initIV(ivParam.getIV(), IV.length);
10293
System.arraycopy(IV, 0, CTR, 0, IV.length);
10394
for (int i = IV.length; i < blockSize; i++)
10495
{
10596
CTR[i] = 0;
10697
}
10798

108-
10999
// if null it's an IV changed only.
110100
if (ivParam.getParameters() != null)
111101
{
@@ -125,21 +115,7 @@ public void init(
125115

126116
initialized = true;
127117
}
128-
129-
private void validateParams(int viLen)
130-
throws IllegalArgumentException
131-
{
132-
if (s < 0 || s > blockSize)
133-
{
134-
throw new IllegalArgumentException("Parameter s must be in range 0 < s <= blockSize");
135-
}
136-
137-
if (viLen != blockSize / 2)
138-
{
139-
throw new IllegalArgumentException("Parameter IV length must be == blockSize/2");
140-
}
141-
}
142-
118+
143119
private void initArrays()
144120
{
145121
IV = new byte[blockSize / 2];

core/src/main/java/org/bouncycastle/crypto/modes/G3413OFBBlockCipher.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.bouncycastle.crypto.CipherParameters;
55
import org.bouncycastle.crypto.DataLengthException;
66
import org.bouncycastle.crypto.StreamBlockCipher;
7-
import org.bouncycastle.crypto.params.GOST3412ParametersWithIV;
87
import org.bouncycastle.crypto.params.ParametersWithIV;
98
import org.bouncycastle.util.Arrays;
109

@@ -15,7 +14,7 @@
1514
public class G3413OFBBlockCipher
1615
extends StreamBlockCipher
1716
{
18-
// private int s;
17+
// private int s;
1918
private int m;
2019
private int blockSize;
2120
private byte[] R;
@@ -43,40 +42,27 @@ public void init(boolean forEncryption, CipherParameters params)
4342
{
4443
ParametersWithIV ivParam = (ParametersWithIV)params;
4544

46-
setupDefaultParams();
47-
48-
initArrays();
45+
byte[] iv = ivParam.getIV();
4946

50-
R_init = GOST3413CipherUtil.initIV(ivParam.getIV(), m);
51-
System.arraycopy(R_init, 0, R, 0, R_init.length);
52-
53-
54-
// if null it's an IV changed only.
55-
if (ivParam.getParameters() != null)
47+
if (iv.length < blockSize)
5648
{
57-
cipher.init(true, ivParam.getParameters());
49+
throw new IllegalArgumentException("Parameter m must blockSize <= m");
5850
}
59-
60-
61-
}
62-
if (params instanceof GOST3412ParametersWithIV)
63-
{
64-
GOST3412ParametersWithIV ivParam = (GOST3412ParametersWithIV)params;
65-
66-
this.m = ivParam.getM() / 8;
67-
68-
validateParams();
51+
this.m = iv.length;
6952

7053
initArrays();
7154

72-
R_init = GOST3413CipherUtil.initIV(ivParam.getIV(), m);
55+
R_init = Arrays.clone(iv);
7356
System.arraycopy(R_init, 0, R, 0, R_init.length);
7457

58+
7559
// if null it's an IV changed only.
7660
if (ivParam.getParameters() != null)
7761
{
7862
cipher.init(true, ivParam.getParameters());
7963
}
64+
65+
8066
}
8167
else
8268
{
@@ -96,17 +82,6 @@ public void init(boolean forEncryption, CipherParameters params)
9682
initialized = true;
9783
}
9884

99-
private void validateParams()
100-
throws IllegalArgumentException
101-
{
102-
if (m < blockSize)
103-
{
104-
throw new IllegalArgumentException("Parameter m must blockSize <= m");
105-
}
106-
107-
}
108-
109-
11085
/**
11186
* allocate memory for R and R_init arrays
11287
*/

core/src/main/java/org/bouncycastle/crypto/modes/GOST3413CipherUtil.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,6 @@
77
*/
88
class GOST3413CipherUtil
99
{
10-
/**
11-
* init initial value for <b>R1</b>
12-
*
13-
* @param iv
14-
* @param len
15-
* @return R1
16-
*/
17-
public static byte[] initIV(byte[] iv, int len)
18-
{
19-
byte[] R1 = new byte[len];
20-
if (iv.length < len)
21-
{
22-
System.arraycopy(iv, 0, R1, R1.length - iv.length, iv.length);
23-
}
24-
else
25-
{
26-
System.arraycopy(iv, 0, R1, 0, R1.length);
27-
}
28-
return R1;
29-
}
30-
31-
3210
/**
3311
* copy first <b>size</b> elements from <b>from</b>
3412
*

0 commit comments

Comments
 (0)