Skip to content

Commit d2f95c9

Browse files
author
gefeili
committed
Refactor on AEADBufferBaseEngine, and prepare to make SparkleEngine inherit from AEADBufferBaseEngine.
1 parent bce43d1 commit d2f95c9

File tree

8 files changed

+156
-424
lines changed

8 files changed

+156
-424
lines changed

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ protected enum State
3535
public void processAADByte(byte input)
3636
{
3737
checkAAD();
38-
m_aad[m_aadPos++] = input;
39-
if (m_aadPos >= AADBufferSize)
38+
if (m_aadPos == AADBufferSize)
4039
{
4140
processBufferAAD(m_aad, 0);
4241
m_aadPos = 0;
4342
}
43+
m_aad[m_aadPos++] = input;
4444
}
4545

4646
@Override
@@ -74,7 +74,7 @@ public void processAADBytes(byte[] input, int inOff, int len)
7474
processBufferAAD(m_aad, 0);
7575
m_aadPos = 0;
7676
}
77-
while (len >= AADBufferSize)
77+
while (len > AADBufferSize)
7878
{
7979
processBufferAAD(input, inOff);
8080
inOff += AADBufferSize;
@@ -92,20 +92,17 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
9292
{
9393
throw new DataLengthException("input buffer too short");
9494
}
95-
int blockLen = len + m_bufPos - (forEncryption ? 0 : MAC_SIZE);
96-
if (blockLen / BlockSize * BlockSize + outOff > output.length)
97-
{
98-
throw new OutputLengthException("output buffer is too short");
99-
}
95+
10096
boolean forEncryption = checkData();
97+
10198
int resultLength = 0;
10299

103100
if (forEncryption)
104101
{
105102
if (m_bufPos > 0)
106103
{
107104
int available = BlockSize - m_bufPos;
108-
if (len < available)
105+
if (len <= available)
109106
{
110107
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
111108
m_bufPos += len;
@@ -116,14 +113,14 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
116113
inOff += available;
117114
len -= available;
118115

119-
processBuffer(m_buf, 0, output, outOff);
116+
validateAndProcessBuffer(m_buf, 0, output, outOff);
120117
resultLength = BlockSize;
121118
//m_bufPos = 0;
122119
}
123120

124-
while (len >= BlockSize)
121+
while (len > BlockSize)
125122
{
126-
processBuffer(input, inOff, output, outOff + resultLength);
123+
validateAndProcessBuffer(input, inOff, output, outOff + resultLength);
127124
inOff += BlockSize;
128125
len -= BlockSize;
129126
resultLength += BlockSize;
@@ -132,16 +129,16 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
132129
else
133130
{
134131
int available = BlockSize + MAC_SIZE - m_bufPos;
135-
if (len < available)
132+
if (len <= available)
136133
{
137134
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
138135
m_bufPos += len;
139136
return 0;
140137
}
141138

142-
if (m_bufPos >= BlockSize)
139+
if (m_bufPos > BlockSize)
143140
{
144-
processBuffer(m_buf, 0, output, outOff);
141+
validateAndProcessBuffer(m_buf, 0, output, outOff);
145142
m_bufPos -= BlockSize;
146143
System.arraycopy(m_buf, BlockSize, m_buf, 0, m_bufPos);
147144
resultLength = BlockSize;
@@ -159,13 +156,13 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
159156
System.arraycopy(input, inOff, m_buf, m_bufPos, available);
160157
inOff += available;
161158
len -= available;
162-
processBuffer(m_buf, 0, output, outOff + resultLength);
159+
validateAndProcessBuffer(m_buf, 0, output, outOff + resultLength);
163160
resultLength += BlockSize;
164161
//m_bufPos = 0;
165162

166-
while (len >= BlockSize + MAC_SIZE)
163+
while (len > BlockSize + MAC_SIZE)
167164
{
168-
processBuffer(input, inOff, output, outOff + resultLength);
165+
validateAndProcessBuffer(input, inOff, output, outOff + resultLength);
169166
inOff += BlockSize;
170167
len -= BlockSize;
171168
resultLength += BlockSize;
@@ -182,10 +179,6 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
182179
public int doFinal(byte[] output, int outOff)
183180
throws IllegalStateException, InvalidCipherTextException
184181
{
185-
if (!initialised)
186-
{
187-
throw new IllegalStateException("Need call init function before encryption/decryption");
188-
}
189182
boolean forEncryption = checkData();
190183
int resultLength;
191184
if (forEncryption)
@@ -362,6 +355,15 @@ protected void bufferReset()
362355
}
363356
}
364357

358+
protected void validateAndProcessBuffer(byte[] input, int inOff, byte[] output, int outOff)
359+
{
360+
if (outOff > output.length - BlockSize)
361+
{
362+
throw new OutputLengthException("output buffer too short");
363+
}
364+
processBuffer(input, inOff, output, outOff);
365+
}
366+
365367
protected abstract void processFinalBlock(byte[] output, int outOff);
366368

367369
protected abstract void processBufferAAD(byte[] input, int inOff);

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

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,17 @@ public void absorbMacBlock(byte[] input, int inOff)
131131

132132
public void absorbFinalAADBlock()
133133
{
134-
for (int i = 0; i < m_aadPos; ++i)
134+
if (m_aadPos == AADBufferSize)
135135
{
136-
x0 ^= (m_aad[i] & 0xFFL) << ((7 - i) << 3);
136+
absorbMacBlock(m_aad, 0);
137+
m_aadPos = 0;
138+
}
139+
else
140+
{
141+
for (int i = 0; i < m_aadPos; ++i)
142+
{
143+
x0 ^= (m_aad[i] & 0xFFL) << ((7 - i) << 3);
144+
}
137145
}
138146
x0 ^= 0x80L << ((7 - m_aadPos) << 3);
139147
P12();
@@ -142,9 +150,17 @@ public void absorbFinalAADBlock()
142150

143151
public void processMACFinal(byte[] input, int inOff, int len, byte[] tag)
144152
{
145-
for (int i = 0; i < len; ++i)
153+
if (len == BlockSize)
146154
{
147-
x0 ^= (input[inOff++] & 0xFFL) << ((7 - i) << 3);
155+
absorbMacBlock(input, inOff);
156+
len = 0;
157+
}
158+
else
159+
{
160+
for (int i = 0; i < len; ++i)
161+
{
162+
x0 ^= (input[inOff++] & 0xFFL) << ((7 - i) << 3);
163+
}
148164
}
149165
x0 ^= 0x80L << ((7 - len) << 3);
150166
P12();
@@ -190,12 +206,19 @@ public void processEncBlock(byte[] input, int inOff, byte[] output, int outOff)
190206

191207
public void processEncFinalBlock(byte[] output, int outOff)
192208
{
193-
/* Encrypt final m block */
194-
byte[] xo = Pack.longToLittleEndian(x0);
195-
int mlen = m_bufPos;
196-
while (mlen > 0)
209+
if (m_bufPos == BlockSize)
197210
{
198-
output[outOff + mlen - 1] = (byte)(xo[BlockSize - mlen] ^ m_buf[--mlen]);
211+
processEncBlock(m_buf, 0, output, outOff);
212+
}
213+
else
214+
{
215+
/* Encrypt final m block */
216+
byte[] xo = Pack.longToLittleEndian(x0);
217+
int mlen = m_bufPos;
218+
while (mlen > 0)
219+
{
220+
output[outOff + mlen - 1] = (byte)(xo[BlockSize - mlen] ^ m_buf[--mlen]);
221+
}
199222
}
200223
}
201224

@@ -384,9 +407,17 @@ public void absorbMacBlock(byte[] input, int inOff)
384407

385408
public void absorbFinalAADBlock()
386409
{
387-
for (int i = 0; i < m_aadPos; i++)
410+
if (m_aadPos == AADBufferSize)
411+
{
412+
absorbMacBlock(m_aad, 0);
413+
m_aadPos = 0;
414+
}
415+
else
388416
{
389-
SX[i >> 1] ^= (m_aad[i] & 0xFF) << ((i & 1) << 3);
417+
for (int i = 0; i < m_aadPos; i++)
418+
{
419+
SX[i >> 1] ^= (m_aad[i] & 0xFF) << ((i & 1) << 3);
420+
}
390421
}
391422
SX[m_aadPos >> 1] ^= 0x80 << ((m_aadPos & 1) << 3);
392423
PermuteRoundsHX(SX, E, C);
@@ -417,10 +448,18 @@ public void isap_rk(short[] iv16, byte[] y, int ylen, short[] out16, int outlen,
417448

418449
public void processMACFinal(byte[] input, int inOff, int len, byte[] tag)
419450
{
420-
// Absorb C final block
421-
for (int i = 0; i < len; i++)
451+
if (len == BlockSize)
452+
{
453+
absorbMacBlock(input, inOff);
454+
len = 0;
455+
}
456+
else
422457
{
423-
SX[i >> 1] ^= (input[inOff++] & 0xFF) << ((i & 1) << 3);
458+
// Absorb C final block
459+
for (int i = 0; i < len; i++)
460+
{
461+
SX[i >> 1] ^= (input[inOff++] & 0xFF) << ((i & 1) << 3);
462+
}
424463
}
425464
SX[len >> 1] ^= 0x80 << ((len & 1) << 3);
426465
PermuteRoundsHX(SX, E, C);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ protected void processFinalBlock(byte[] output, int outOff)
146146
{
147147
PHOTON_Permutation();
148148
rhoohr(output, outOff, m_buf, 0, bufferLen);
149-
state[bufferLen] ^= 0x01; // ozs
149+
if(bufferLen < BlockSize)
150+
{
151+
state[bufferLen] ^= 0x01; // ozs
152+
}
150153
}
151154
state[STATE_INBYTES - 1] ^= c1 << LAST_THREE_BITS_OFFSET;
152155
}

0 commit comments

Comments
 (0)