Skip to content

Commit 2c9faf6

Browse files
committed
Fix GCFB reset after large data
1 parent d20a0d6 commit 2c9faf6

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class GCFBBlockCipher
2525

2626
private final CFBBlockCipher cfbEngine;
2727

28+
private ParametersWithIV initParams;
29+
2830
private KeyParameter key;
2931
private long counter = 0;
3032
private boolean forEncryption;
@@ -41,12 +43,15 @@ public void init(boolean forEncryption, CipherParameters params)
4143
{
4244
counter = 0;
4345
cfbEngine.init(forEncryption, params);
44-
46+
byte[] iv = null;
47+
4548
this.forEncryption = forEncryption;
4649

4750
if (params instanceof ParametersWithIV)
4851
{
49-
params = ((ParametersWithIV)params).getParameters();
52+
ParametersWithIV ivParams = (ParametersWithIV) params;
53+
params = ivParams.getParameters();
54+
iv = ivParams.getIV();
5055
}
5156

5257
if (params instanceof ParametersWithRandom)
@@ -60,6 +65,23 @@ public void init(boolean forEncryption, CipherParameters params)
6065
}
6166

6267
key = (KeyParameter)params;
68+
69+
/* Pick up key/IV from parameters or most recent parameters */
70+
if (key == null && initParams != null)
71+
{
72+
key = (KeyParameter) initParams.getParameters();
73+
}
74+
if (iv == null && initParams != null)
75+
{
76+
iv = initParams.getIV();
77+
}
78+
else
79+
{
80+
iv = cfbEngine.getCurrentIV();
81+
}
82+
83+
/* Save the initParameters */
84+
initParams = new ParametersWithIV(key, iv);
6385
}
6486

6587
public String getAlgorithmName()
@@ -115,6 +137,14 @@ protected byte calculateByte(byte b)
115137
public void reset()
116138
{
117139
counter = 0;
118-
cfbEngine.reset();
140+
if (initParams != null)
141+
{
142+
key = (KeyParameter) initParams.getParameters();
143+
cfbEngine.init(forEncryption, initParams);
144+
}
145+
else
146+
{
147+
cfbEngine.reset();
148+
}
119149
}
120150
}

0 commit comments

Comments
 (0)