Skip to content

Commit b3f40e3

Browse files
author
gefeili
committed
Merge aadData and message of XoodyakEngine into one byte array
1 parent d40db8a commit b3f40e3

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

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

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ public class XoodyakEngine
3232
private boolean aadFinished;
3333
private boolean encrypted;
3434
private boolean initialised = false;
35-
private final byte[] aadData = new byte[Rkin];
36-
private byte[] message;
37-
private int messageOff;
38-
private int aadOff;
35+
private final byte[] buffer = new byte[Rkin];
36+
private int bufferOff;
3937
private byte aadcd;
4038

4139
enum MODE
@@ -62,8 +60,6 @@ public void init(boolean forEncryption, CipherParameters params)
6260
state = new byte[48];
6361
mac = new byte[MAC_SIZE];
6462
initialised = true;
65-
message = new byte[forEncryption ? Rkout : Rkout + MAC_SIZE];
66-
messageOff = 0;
6763
reset();
6864
}
6965

@@ -75,13 +71,13 @@ public void processAADByte(byte input)
7571
throw new IllegalArgumentException("AAD cannot be added after reading a full block(" + Rkout +
7672
" bytes) of input for " + (forEncryption ? "encryption" : "decryption"));
7773
}
78-
if (aadOff >= aadData.length)
74+
if (bufferOff >= Rkin)
7975
{
80-
AbsorbAny(aadData, 0, aadData.length, aadcd);
76+
AbsorbAny(buffer, 0, Rkin, aadcd);
8177
aadcd = 0;
82-
aadOff = 0;
78+
bufferOff = 0;
8379
}
84-
aadData[aadOff++] = input;
80+
buffer[bufferOff++] = input;
8581
}
8682

8783
@Override
@@ -97,15 +93,15 @@ public void processAADBytes(byte[] input, int inOff, int len)
9793
throw new DataLengthException("input buffer too short");
9894
}
9995
int tmp;
100-
if (aadOff + len >= Rkin)
96+
if (bufferOff + len >= Rkin)
10197
{
102-
tmp = Rkin - aadOff;
103-
System.arraycopy(input, inOff, aadData, aadOff, tmp);
104-
AbsorbAny(aadData, 0, aadData.length, aadcd);
98+
tmp = Rkin - bufferOff;
99+
System.arraycopy(input, inOff, buffer, bufferOff, tmp);
100+
AbsorbAny(buffer, 0, buffer.length, aadcd);
105101
aadcd = 0;
106102
inOff += tmp;
107103
len -= tmp;
108-
aadOff = 0;
104+
bufferOff = 0;
109105
}
110106
tmp = len / Rkin;
111107
if (tmp > 0)
@@ -115,16 +111,17 @@ public void processAADBytes(byte[] input, int inOff, int len)
115111
inOff += tmp;
116112
len -= tmp;
117113
}
118-
System.arraycopy(input, inOff, aadData, aadOff, len);
119-
aadOff += len;
114+
System.arraycopy(input, inOff, buffer, bufferOff, len);
115+
bufferOff += len;
120116
}
121117

122118
private void processAAD()
123119
{
124120
if (!aadFinished)
125121
{
126-
AbsorbAny(aadData, 0, aadOff, aadcd);
122+
AbsorbAny(buffer, 0, bufferOff, aadcd);
127123
aadFinished = true;
124+
bufferOff = 0;
128125
}
129126
}
130127

@@ -144,7 +141,8 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
144141
{
145142
throw new DataLengthException("input buffer too short");
146143
}
147-
int blockLen = len + messageOff - (forEncryption ? 0 : MAC_SIZE);
144+
processAAD();
145+
int blockLen = len + bufferOff - (forEncryption ? 0 : MAC_SIZE);
148146
if (blockLen / Rkout * Rkout + outOff > output.length)
149147
{
150148
throw new OutputLengthException("output buffer is too short");
@@ -153,27 +151,26 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
153151
int originalInOff = inOff;
154152
while (blockLen >= Rkout)
155153
{
156-
int copyLen = Math.min(len, Math.max(Rkout - messageOff, 0));
157-
System.arraycopy(input, inOff, message, messageOff, copyLen);
158-
processAAD();
159-
encrypt(message, Rkout, output, outOff);
160-
if (!forEncryption && Rkout < messageOff)
154+
int copyLen = Math.min(len, Math.max(Rkout - bufferOff, 0));
155+
System.arraycopy(input, inOff, buffer, bufferOff, copyLen);
156+
encrypt(buffer, Rkout, output, outOff);
157+
if (!forEncryption && Rkout < bufferOff)
161158
{
162-
System.arraycopy(message, Rkout, message, 0, messageOff - Rkout);
163-
messageOff -= Rkout;
159+
System.arraycopy(buffer, Rkout, buffer, 0, bufferOff - Rkout);
160+
bufferOff -= Rkout;
164161
}
165162
else
166163
{
167-
messageOff = 0;
164+
bufferOff = 0;
168165
}
169166
outOff += Rkout;
170167
rv += Rkout;
171168
blockLen -= Rkout;
172169
inOff += copyLen;
173170
}
174171
len -= inOff - originalInOff;
175-
System.arraycopy(input, inOff, message, messageOff, len);
176-
messageOff += len;
172+
System.arraycopy(input, inOff, buffer, bufferOff, len);
173+
bufferOff += len;
177174
return rv;
178175
}
179176

@@ -220,18 +217,18 @@ public int doFinal(byte[] output, int outOff)
220217
{
221218
throw new IllegalArgumentException("Need call init function before encryption/decryption");
222219
}
223-
byte[] blocks = message;
224-
Arrays.fill(blocks, messageOff, message.length, (byte)0);
225-
int len = messageOff;
220+
processAAD();
221+
int len = bufferOff;
226222
if ((forEncryption && len + MAC_SIZE + outOff > output.length) || (!forEncryption && len - MAC_SIZE + outOff > output.length))
227223
{
228224
throw new OutputLengthException("output buffer too short");
229225
}
230-
processAAD();
226+
231227
int rv = 0;
232228
if (forEncryption)
233229
{
234-
encrypt(blocks, len, output, outOff);
230+
Arrays.fill(buffer, bufferOff, Rkout, (byte)0);
231+
encrypt(buffer, len, output, outOff);
235232
outOff += len;
236233
mac = new byte[MAC_SIZE];
237234
Up(mac, MAC_SIZE, 0x40);
@@ -245,14 +242,14 @@ public int doFinal(byte[] output, int outOff)
245242
{
246243
inOff = len - MAC_SIZE;
247244
rv = inOff;
248-
encrypt(blocks, inOff, output, outOff);
245+
encrypt(buffer, inOff, output, outOff);
249246
}
250247

251248
mac = new byte[MAC_SIZE];
252249
Up(mac, MAC_SIZE, 0x40);
253250
for (int i = 0; i < MAC_SIZE; ++i)
254251
{
255-
if (mac[i] != blocks[inOff++])
252+
if (mac[i] != buffer[inOff++])
256253
{
257254
throw new IllegalArgumentException("Mac does not match");
258255
}
@@ -265,14 +262,29 @@ public int doFinal(byte[] output, int outOff)
265262
@Override
266263
public int getUpdateOutputSize(int len)
267264
{
268-
int total = Math.max(0, len + messageOff + (forEncryption ? 0 : -MAC_SIZE));
265+
int total;
266+
if (aadFinished)
267+
{
268+
total = Math.max(0, len + bufferOff + (forEncryption ? 0 : -MAC_SIZE));
269+
}
270+
else
271+
{
272+
total = Math.max(0, len + (forEncryption ? 0 : -MAC_SIZE));
273+
}
269274
return total - total % Rkout;
270275
}
271276

272277
@Override
273278
public int getOutputSize(int len)
274279
{
275-
return Math.max(0, len + messageOff + (forEncryption ? MAC_SIZE : -MAC_SIZE));
280+
if (aadFinished)
281+
{
282+
return Math.max(0, len + bufferOff + (forEncryption ? MAC_SIZE : -MAC_SIZE));
283+
}
284+
else
285+
{
286+
return Math.max(0, len + (forEncryption ? MAC_SIZE : -MAC_SIZE));
287+
}
276288
}
277289

278290
@Override
@@ -291,10 +303,8 @@ protected void reset(boolean clearMac)
291303
aadFinished = false;
292304
encrypted = false;
293305
phase = PhaseUp;
294-
Arrays.fill(message, (byte)0);
295-
messageOff = 0;
296-
Arrays.fill(aadData, (byte)0);
297-
aadOff = 0;
306+
Arrays.fill(buffer, (byte)0);
307+
bufferOff = 0;
298308
aadcd = (byte)0x03;
299309
//Absorb key
300310
int KLen = K.length;

0 commit comments

Comments
 (0)