Skip to content

Commit f12f73b

Browse files
author
gefeili
committed
Refactor in XoodyakEngine
1 parent 38a07e4 commit f12f73b

File tree

1 file changed

+26
-50
lines changed

1 file changed

+26
-50
lines changed

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

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public XoodyakEngine()
3939
MAC_SIZE = 16;
4040
BlockSize = 24;
4141
AADBufferSize = 44;
42-
setInnerMembers(ProcessingBufferType.Buffered, AADOperatorType.Default, DataOperatorType.Counter);
42+
setInnerMembers(ProcessingBufferType.Immediate, AADOperatorType.Default, DataOperatorType.Counter);
4343
}
4444

4545
@Override
@@ -62,7 +62,6 @@ protected void processBufferAAD(byte[] input, int inOff)
6262
protected void processFinalAAD()
6363
{
6464
AbsorbAny(m_aad, 0, m_aadPos, aadcd);
65-
m_aadPos = 0;
6665
}
6766

6867
@Override
@@ -89,67 +88,44 @@ protected void finishAAD(State nextState, boolean isDoFinal)
8988

9089
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
9190
{
92-
encrypt(input, inOff, BlockSize, output, outOff);
91+
int Cu = encrypted ? 0 : 0x80;
92+
up(mode, state, Cu); /* Up without extract */
93+
/* Extract from Up and Add */
94+
Bytes.xor(BlockSize, state, input, inOff, output, outOff);
95+
down(mode, state, input, inOff, BlockSize, 0x00);
96+
phase = PhaseDown;
97+
encrypted = true;
9398
}
9499

95100
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
96101
{
97-
decrypt(input, inOff, BlockSize, output, outOff);
98-
}
99-
100-
private void encrypt(byte[] input, int inOff, int len, byte[] output, int outOff)
101-
{
102-
int splitLen;
103-
byte[] P = new byte[BlockSize];
104102
int Cu = encrypted ? 0 : 0x80;
105-
while (len != 0 || !encrypted)
106-
{
107-
splitLen = Math.min(len, BlockSize); /* use Rkout instead of Rsqueeze, this function is only called in keyed mode */
108-
System.arraycopy(input, inOff, P, 0, splitLen);
109-
up(mode, state, Cu); /* Up without extract */
110-
/* Extract from Up and Add */
111-
Bytes.xor(splitLen, state, input, inOff, output, outOff);
112-
inOff += splitLen;
113-
down(mode, state, P, 0, splitLen, 0x00);
114-
phase = PhaseDown;
115-
Cu = 0x00;
116-
outOff += splitLen;
117-
len -= splitLen;
118-
encrypted = true;
119-
}
103+
up(mode, state, Cu); /* Up without extract */
104+
/* Extract from Up and Add */
105+
Bytes.xor(BlockSize, state, input, inOff, output, outOff);
106+
down(mode, state, output, outOff, BlockSize, 0x00);
107+
phase = PhaseDown;
108+
encrypted = true;
120109
}
121110

122-
private void decrypt(byte[] input, int inOff, int len, byte[] output, int outOff)
111+
@Override
112+
protected void processFinalBlock(byte[] output, int outOff)
123113
{
124-
int splitLen;
125114
int Cu = encrypted ? 0 : 0x80;
126-
while (len != 0 || !encrypted)
115+
if (m_bufPos != 0 || !encrypted)
127116
{
128-
splitLen = Math.min(len, BlockSize); /* use Rkout instead of Rsqueeze, this function is only called in keyed mode */
129117
up(mode, state, Cu); /* Up without extract */
130118
/* Extract from Up and Add */
131-
Bytes.xor(splitLen, state, input, inOff, output, outOff);
132-
inOff += splitLen;
133-
down(mode, state, output, outOff, splitLen, 0x00);
119+
Bytes.xor(m_bufPos, state, m_buf, 0, output, outOff);
120+
if (forEncryption)
121+
{
122+
down(mode, state, m_buf, 0, m_bufPos, 0x00);
123+
}
124+
else
125+
{
126+
down(mode, state, output, outOff, m_bufPos, 0x00);
127+
}
134128
phase = PhaseDown;
135-
Cu = 0x00;
136-
outOff += splitLen;
137-
len -= splitLen;
138-
encrypted = true;
139-
}
140-
}
141-
142-
@Override
143-
protected void processFinalBlock(byte[] output, int outOff)
144-
{
145-
if (forEncryption)
146-
{
147-
Arrays.fill(m_buf, m_bufPos, BlockSize, (byte)0);
148-
encrypt(m_buf, 0, m_bufPos, output, outOff);
149-
}
150-
else
151-
{
152-
decrypt(m_buf, 0, m_bufPos, output, outOff);
153129
}
154130
up(mode, state, 0x40);
155131
System.arraycopy(state, 0, mac, 0, MAC_SIZE);

0 commit comments

Comments
 (0)