Skip to content

Commit a274997

Browse files
committed
refactor/update
1 parent 626e58b commit a274997

File tree

11 files changed

+567
-436
lines changed

11 files changed

+567
-436
lines changed

core/src/main/java/org/bouncycastle/crypto/engines/GOST3412_2015Engine.java

Lines changed: 108 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
import org.bouncycastle.crypto.params.KeyParameter;
88
import org.bouncycastle.util.Arrays;
99

10-
11-
public class GOST3412_2015Engine implements BlockCipher {
10+
/**
11+
* Implementation of GOST 3412 2015 (aka "Kuznyechik") RFC 7801, GOST 3412
12+
*/
13+
public class GOST3412_2015Engine
14+
implements BlockCipher
15+
{
1216

1317
private static final byte[] PI = new byte[]
1418
{
@@ -54,77 +58,99 @@ public class GOST3412_2015Engine implements BlockCipher {
5458
private byte[][] _gf_mul = init_gf256_mul_table();
5559

5660

57-
private static byte[][] init_gf256_mul_table() {
61+
private static byte[][] init_gf256_mul_table()
62+
{
5863
byte[][] mul_table = new byte[256][];
59-
for (int x = 0; x < 256; x++) {
64+
for (int x = 0; x < 256; x++)
65+
{
6066
mul_table[x] = new byte[256];
61-
for (int y = 0; y < 256; y++) {
62-
mul_table[x][y] = kuz_mul_gf256_slow((byte) x, (byte) y);
67+
for (int y = 0; y < 256; y++)
68+
{
69+
mul_table[x][y] = kuz_mul_gf256_slow((byte)x, (byte)y);
6370
}
6471
}
6572
return mul_table;
6673
}
6774

68-
private static byte kuz_mul_gf256_slow(byte a, byte b) {
75+
private static byte kuz_mul_gf256_slow(byte a, byte b)
76+
{
6977
byte p = 0;
7078
byte counter;
7179
byte hi_bit_set;
72-
for (counter = 0; counter < 8 && a != 0 && b != 0; counter++) {
80+
for (counter = 0; counter < 8 && a != 0 && b != 0; counter++)
81+
{
7382
if ((b & 1) != 0)
83+
{
7484
p ^= a;
75-
hi_bit_set = (byte) (a & 0x80);
85+
}
86+
hi_bit_set = (byte)(a & 0x80);
7687
a <<= 1;
7788
if (hi_bit_set != 0)
89+
{
7890
a ^= 0xc3; /* x^8 + x^7 + x^6 + x + 1 */
91+
}
7992
b >>= 1;
8093
}
8194
return p;
8295
}
8396

84-
public String getAlgorithmName() {
97+
public String getAlgorithmName()
98+
{
8599
return "GOST3412_2015";
86100
}
87101

88-
public int getBlockSize() {
102+
public int getBlockSize()
103+
{
89104
return BLOCK_SIZE;
90105
}
91106

92-
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException {
107+
public void init(boolean forEncryption, CipherParameters params)
108+
throws IllegalArgumentException
109+
{
93110

94-
if (params instanceof KeyParameter) {
111+
if (params instanceof KeyParameter)
112+
{
95113
this.forEncryption = forEncryption;
96-
generateSubKeys(((KeyParameter) params).getKey());
97-
} else if (params != null) {
114+
generateSubKeys(((KeyParameter)params).getKey());
115+
}
116+
else if (params != null)
117+
{
98118
throw new IllegalArgumentException("invalid parameter passed to GOST3412_2015 init - " + params.getClass().getName());
99119
}
100120
}
101121

102122
private void generateSubKeys(
103-
byte[] userKey) {
123+
byte[] userKey)
124+
{
104125

105-
if (userKey.length != KEY_LENGTH) {
126+
if (userKey.length != KEY_LENGTH)
127+
{
106128
throw new IllegalArgumentException("Key length invalid. Key needs to be 64 byte - 512 bit!!!");
107129
}
108130

109131
subKeys = new byte[10][];
110-
for (int i = 0; i < 10; i++) {
132+
for (int i = 0; i < 10; i++)
133+
{
111134
subKeys[i] = new byte[SUB_LENGTH];
112135
}
113136

114137
byte[] x = new byte[SUB_LENGTH];
115138
byte[] y = new byte[SUB_LENGTH];
116139

117140

118-
for (int i = 0; i < SUB_LENGTH; i++) {
141+
for (int i = 0; i < SUB_LENGTH; i++)
142+
{
119143
subKeys[0][i] = x[i] = userKey[i];
120144
subKeys[1][i] = y[i] = userKey[i + SUB_LENGTH];
121145
}
122146

123147
byte[] c = new byte[SUB_LENGTH];
124148

125-
for (int k = 1; k < 5; k++) {
149+
for (int k = 1; k < 5; k++)
150+
{
126151

127-
for (int j = 1; j <= 8; j++) {
152+
for (int j = 1; j <= 8; j++)
153+
{
128154
C(c, 8 * (k - 1) + j);
129155
F(c, x, y);
130156
}
@@ -135,15 +161,17 @@ private void generateSubKeys(
135161
}
136162

137163

138-
private void C(byte[] c, int i) {
164+
private void C(byte[] c, int i)
165+
{
139166

140167
Arrays.clear(c);
141-
c[15] = (byte) i;
168+
c[15] = (byte)i;
142169
L(c);
143170
}
144171

145172

146-
private void F(byte[] k, byte[] a1, byte[] a0) {
173+
private void F(byte[] k, byte[] a1, byte[] a0)
174+
{
147175

148176
byte[] temp = LSX(k, a1);
149177
X(temp, a0);
@@ -153,17 +181,22 @@ private void F(byte[] k, byte[] a1, byte[] a0) {
153181

154182
}
155183

156-
public int processBlock(byte[] in, int inOff, byte[] out, int outOff) throws DataLengthException, IllegalStateException {
184+
public int processBlock(byte[] in, int inOff, byte[] out, int outOff)
185+
throws DataLengthException, IllegalStateException
186+
{
157187

158-
if (subKeys == null) {
188+
if (subKeys == null)
189+
{
159190
throw new IllegalStateException("GOST3412_2015 engine not initialised");
160191
}
161192

162-
if ((inOff + BLOCK_SIZE) > in.length) {
193+
if ((inOff + BLOCK_SIZE) > in.length)
194+
{
163195
throw new DataLengthException("input buffer too short");
164196
}
165197

166-
if ((outOff + BLOCK_SIZE) > out.length) {
198+
if ((outOff + BLOCK_SIZE) > out.length)
199+
{
167200
throw new OutputLengthException("output buffer too short");
168201
}
169202

@@ -177,23 +210,29 @@ private void GOST3412_2015Func(
177210
byte[] in,
178211
int inOff,
179212
byte[] out,
180-
int outOff) {
213+
int outOff)
214+
{
181215

182216
byte[] block = new byte[BLOCK_SIZE];
183217
System.arraycopy(in, inOff, block, 0, BLOCK_SIZE);
184218

185-
if (forEncryption) {
219+
if (forEncryption)
220+
{
186221

187-
for (int i = 0; i < 9; i++) {
222+
for (int i = 0; i < 9; i++)
223+
{
188224

189225
byte[] temp = LSX(subKeys[i], block);
190226
block = Arrays.copyOf(temp, BLOCK_SIZE);
191227
}
192228

193229
X(block, subKeys[9]);
194-
} else {
230+
}
231+
else
232+
{
195233

196-
for (int i = 9; i > 0; i--) {
234+
for (int i = 9; i > 0; i--)
235+
{
197236

198237
byte[] temp = XSL(subKeys[i], block);
199238
block = Arrays.copyOf(temp, BLOCK_SIZE);
@@ -205,7 +244,8 @@ private void GOST3412_2015Func(
205244
System.arraycopy(block, 0, out, outOff, BLOCK_SIZE);
206245
}
207246

208-
private byte[] LSX(byte[] k, byte[] a) {
247+
private byte[] LSX(byte[] k, byte[] a)
248+
{
209249

210250
byte[] result = Arrays.copyOf(k, k.length);
211251
X(result, a);
@@ -214,56 +254,70 @@ private byte[] LSX(byte[] k, byte[] a) {
214254
return result;
215255
}
216256

217-
private byte[] XSL(byte[] k, byte[] a) {
257+
private byte[] XSL(byte[] k, byte[] a)
258+
{
218259
byte[] result = Arrays.copyOf(k, k.length);
219260
X(result, a);
220261
inverseL(result);
221262
inverseS(result);
222263
return result;
223264
}
224265

225-
private void X(byte[] result, byte[] data) {
226-
for (int i = 0; i < result.length; i++) {
266+
private void X(byte[] result, byte[] data)
267+
{
268+
for (int i = 0; i < result.length; i++)
269+
{
227270
result[i] ^= data[i];
228271
}
229272
}
230273

231-
private void S(byte[] data) {
232-
for (int i = 0; i < data.length; i++) {
274+
private void S(byte[] data)
275+
{
276+
for (int i = 0; i < data.length; i++)
277+
{
233278
data[i] = PI[unsignedByte(data[i])];
234279
}
235280
}
236281

237-
private void inverseS(byte[] data) {
238-
for (int i = 0; i < data.length; i++) {
282+
private void inverseS(byte[] data)
283+
{
284+
for (int i = 0; i < data.length; i++)
285+
{
239286
data[i] = inversePI[unsignedByte(data[i])];
240287
}
241288
}
242289

243-
private int unsignedByte(byte b) {
290+
private int unsignedByte(byte b)
291+
{
244292
return b & 0xFF;
245293
}
246294

247-
private void L(byte[] data) {
248-
for (int i = 0; i < 16; i++) {
295+
private void L(byte[] data)
296+
{
297+
for (int i = 0; i < 16; i++)
298+
{
249299
R(data);
250300
}
251301
}
252302

253-
private void inverseL(byte[] data) {
254-
for (int i = 0; i < 16; i++) {
303+
private void inverseL(byte[] data)
304+
{
305+
for (int i = 0; i < 16; i++)
306+
{
255307
inverseR(data);
256308
}
257309
}
258310

259311

260-
private void R(byte[] data) {
312+
private void R(byte[] data)
313+
{
261314
byte z = l(data);
262315
System.arraycopy(data, 0, data, 1, 15);
263316
data[0] = z;
264317
}
265318

266-
private void inverseR(byte[] data) {
319+
private void inverseR(byte[] data)
320+
{
267321
byte[] temp = new byte[16];
268322
System.arraycopy(data, 1, temp, 0, 15);
269323
temp[15] = data[0];
@@ -273,15 +327,18 @@ private void inverseR(byte[] data) {
273327
}
274328

275329

276-
private byte l(byte[] data) {
330+
private byte l(byte[] data)
331+
{
277332
byte x = data[15];
278-
for (int i = 14; i >= 0; i--) {
333+
for (int i = 14; i >= 0; i--)
334+
{
279335
x ^= _gf_mul[unsignedByte(data[i])][unsignedByte(lFactors[i])];
280336
}
281337
return x;
282338
}
283339

284-
public void reset() {
340+
public void reset()
341+
{
285342

286343
}
287344
}

0 commit comments

Comments
 (0)