Skip to content

Commit 8c39ea1

Browse files
author
gefeili
committed
Refactor on PhotonBeetleEngine and PhotonBeetleDigest
1 parent eda7147 commit 8c39ea1

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/PhotonBeetleDigest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ public class PhotonBeetleDigest
1717
public static class Friend
1818
{
1919
private static final Friend INSTANCE = new Friend();
20-
private Friend() {}
20+
21+
private Friend()
22+
{
23+
}
2124
}
25+
2226
private final byte[] state;
2327
private final byte[][] state_2d;
24-
private final int STATE_INBYTES = 32;
28+
private static final int STATE_INBYTES = 32;
2529
private static final int D = 8;
2630
private int blockCount;
2731

@@ -45,7 +49,7 @@ protected void processBytes(byte[] input, int inOff)
4549
else
4650
{
4751
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
48-
Bytes.xorTo(BlockSize, input, inOff, state, 0);
52+
Bytes.xorTo(BlockSize, input, inOff, state);
4953
}
5054
blockCount++;
5155
}
@@ -71,7 +75,7 @@ else if (blockCount == 4 && m_bufPos == 0)
7175
else
7276
{
7377
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
74-
Bytes.xorTo(m_bufPos, m_buf, 0, state, 0);
78+
Bytes.xorTo(m_bufPos, m_buf, state);
7579
if (m_bufPos < BlockSize)
7680
{
7781
state[m_bufPos] ^= 0x01; // ozs

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.crypto.engines;
22

33
import org.bouncycastle.crypto.digests.PhotonBeetleDigest;
4+
import org.bouncycastle.util.Bytes;
45

56
/**
67
* Photon-Beetle, <a href="https://www.isical.ac.in/~lightweight/beetle/"></a>
@@ -96,7 +97,7 @@ protected void init(byte[] key, byte[] iv)
9697
protected void processBufferAAD(byte[] input, int inOff)
9798
{
9899
PhotonPermutation(state_2d, state);
99-
XOR(input, inOff, BlockSize);
100+
Bytes.xorTo(BlockSize, input, inOff, state);
100101
}
101102

102103
public void processFinalAAD()
@@ -109,7 +110,7 @@ public void processFinalAAD()
109110
if (m_aadPos != 0)
110111
{
111112
PhotonPermutation(state_2d, state);
112-
XOR(m_aad, 0, m_aadPos);
113+
Bytes.xorTo(m_aadPos, m_aad, state);
113114
if (m_aadPos < BlockSize)
114115
{
115116
state[m_aadPos] ^= 0x01; // ozs
@@ -127,14 +128,14 @@ protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int
127128
{
128129
PhotonPermutation(state_2d, state);
129130
rhoohr(output, outOff, input, inOff, BlockSize);
130-
XOR(input, inOff, BlockSize);
131+
Bytes.xorTo(BlockSize, input, inOff, state);
131132
}
132133

133134
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
134135
{
135136
PhotonPermutation(state_2d, state);
136137
rhoohr(output, outOff, input, inOff, BlockSize);
137-
XOR(output, outOff, BlockSize);
138+
Bytes.xorTo(BlockSize, output, outOff, state);
138139
}
139140

140141
@Override
@@ -157,11 +158,11 @@ protected void processFinalBlock(byte[] output, int outOff)
157158
rhoohr(output, outOff, m_buf, 0, bufferLen);
158159
if (forEncryption)
159160
{
160-
XOR(m_buf, 0, bufferLen);
161+
Bytes.xorTo(bufferLen, m_buf, state);
161162
}
162163
else
163164
{
164-
XOR(output, outOff, bufferLen);
165+
Bytes.xorTo(bufferLen, output, outOff, state);
165166
}
166167
if (bufferLen < BlockSize)
167168
{
@@ -297,14 +298,6 @@ private void rhoohr(byte[] ciphertext, int outOff, byte[] plaintext, int inOff,
297298
}
298299
}
299300

300-
private void XOR(byte[] in_right, int rOff, int iolen_inbytes)
301-
{
302-
for (int i = 0; i < iolen_inbytes; i++)
303-
{
304-
state[i] ^= in_right[rOff++];
305-
}
306-
}
307-
308301
public static void PhotonPermutation(PhotonBeetleDigest.Friend friend, byte[][] state_2d, byte[] state)
309302
{
310303
if (null == friend)

core/src/main/java/org/bouncycastle/util/Bytes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public static void xorTo(int len, byte[] x, byte[] z)
3232
}
3333
}
3434

35+
public static void xorTo(int len, byte[] x, int xOff, byte[] z)
36+
{
37+
for (int i = 0; i < len; ++i)
38+
{
39+
z[i] ^= x[xOff++];
40+
}
41+
}
42+
3543
public static void xorTo(int len, byte[] x, int xOff, byte[] z, int zOff)
3644
{
3745
for (int i = 0; i < len; ++i)

0 commit comments

Comments
 (0)