Skip to content

Commit 81e40ca

Browse files
author
gefeili
committed
Fix the issue related to Grain128AEADEngine
1 parent f8c09c9 commit 81e40ca

File tree

4 files changed

+224
-227
lines changed

4 files changed

+224
-227
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ protected void setInnerMembers(ProcessingBufferType type, AADOperatorType aadOpe
100100
dataOperator = new StreamDataOperator();
101101
break;
102102
case StreamCipher:
103+
BlockSize = 0;
103104
m_buf = new byte[m_bufferSizeDecrypt];
104105
dataOperator = new StreamCipherOperator();
105106
break;
@@ -377,28 +378,51 @@ protected class StreamCipherOperator
377378
@Override
378379
public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff)
379380
{
380-
this.len = len;
381+
boolean forEncryption = checkData(false);
381382
if (forEncryption)
382383
{
384+
this.len = len;
383385
processBufferEncrypt(input, inOff, output, outOff);
386+
return len;
384387
}
385388
else
386389
{
387-
processBufferDecrypt(input, inOff, output, outOff);
390+
// keep last mac size bytes
391+
int available = Math.max(m_bufPos + len - MAC_SIZE, 0);
392+
int rlt = 0;
393+
if (m_bufPos > 0)
394+
{
395+
this.len = Math.min(available, m_bufPos);
396+
rlt = this.len;
397+
processBufferDecrypt(m_buf, 0, output, outOff);
398+
available -= rlt;
399+
m_bufPos -= rlt;
400+
System.arraycopy(m_buf, rlt, m_buf, 0, m_bufPos);
401+
}
402+
if (available > 0)
403+
{
404+
this.len = available;
405+
processBufferDecrypt(input, inOff, output, outOff);
406+
rlt += available;
407+
len -= available;
408+
inOff += available;
409+
}
410+
411+
System.arraycopy(input, inOff, m_buf, m_bufPos, len);
412+
m_bufPos += len;
413+
return rlt;
388414
}
389-
return len;
390415
}
391416

392417
@Override
393418
public int getLen()
394419
{
395-
return 0;
420+
return len;
396421
}
397422

398423
@Override
399424
public void reset()
400425
{
401-
402426
}
403427
}
404428

0 commit comments

Comments
 (0)