Skip to content

Commit eda7147

Browse files
author
gefeili
committed
Introduce Friend between PhotonBeetleDigest and PhotonBeetleEngine
1 parent cc39f0b commit eda7147

File tree

2 files changed

+34
-108
lines changed

2 files changed

+34
-108
lines changed
Lines changed: 11 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bouncycastle.crypto.digests;
22

3+
import org.bouncycastle.crypto.engines.PhotonBeetleEngine;
34
import org.bouncycastle.util.Arrays;
45
import org.bouncycastle.util.Bytes;
56

@@ -13,33 +14,16 @@
1314
public class PhotonBeetleDigest
1415
extends BufferBaseDigest
1516
{
17+
public static class Friend
18+
{
19+
private static final Friend INSTANCE = new Friend();
20+
private Friend() {}
21+
}
1622
private final byte[] state;
1723
private final byte[][] state_2d;
1824
private final int STATE_INBYTES = 32;
19-
private final int D = 8;
25+
private static final int D = 8;
2026
private int blockCount;
21-
private static final byte[][] RC = {//[D][12]
22-
{1, 3, 7, 14, 13, 11, 6, 12, 9, 2, 5, 10},
23-
{0, 2, 6, 15, 12, 10, 7, 13, 8, 3, 4, 11},
24-
{2, 0, 4, 13, 14, 8, 5, 15, 10, 1, 6, 9},
25-
{6, 4, 0, 9, 10, 12, 1, 11, 14, 5, 2, 13},
26-
{14, 12, 8, 1, 2, 4, 9, 3, 6, 13, 10, 5},
27-
{15, 13, 9, 0, 3, 5, 8, 2, 7, 12, 11, 4},
28-
{13, 15, 11, 2, 1, 7, 10, 0, 5, 14, 9, 6},
29-
{9, 11, 15, 6, 5, 3, 14, 4, 1, 10, 13, 2}
30-
};
31-
private static final byte[][] MixColMatrix = { //[D][D]
32-
{2, 4, 2, 11, 2, 8, 5, 6},
33-
{12, 9, 8, 13, 7, 7, 5, 2},
34-
{4, 4, 13, 13, 9, 4, 13, 9},
35-
{1, 6, 5, 1, 12, 13, 15, 14},
36-
{15, 12, 9, 13, 14, 5, 14, 13},
37-
{9, 14, 5, 15, 4, 12, 9, 6},
38-
{12, 2, 2, 10, 3, 1, 1, 14},
39-
{15, 1, 13, 10, 5, 10, 2, 3}
40-
};
41-
42-
private static final byte[] sbox = {12, 5, 6, 11, 9, 0, 10, 13, 3, 14, 15, 8, 4, 7, 1, 2};
4327

4428
public PhotonBeetleDigest()
4529
{
@@ -60,7 +44,7 @@ protected void processBytes(byte[] input, int inOff)
6044
}
6145
else
6246
{
63-
PHOTON_Permutation();
47+
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
6448
Bytes.xorTo(BlockSize, input, inOff, state, 0);
6549
}
6650
blockCount++;
@@ -86,18 +70,18 @@ else if (blockCount == 4 && m_bufPos == 0)
8670
}
8771
else
8872
{
89-
PHOTON_Permutation();
73+
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
9074
Bytes.xorTo(m_bufPos, m_buf, 0, state, 0);
9175
if (m_bufPos < BlockSize)
9276
{
9377
state[m_bufPos] ^= 0x01; // ozs
9478
}
9579
state[STATE_INBYTES - 1] ^= (m_bufPos % BlockSize == 0 ? (byte)1 : (byte)2) << LAST_THREE_BITS_OFFSET;
9680
}
97-
PHOTON_Permutation();
81+
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
9882
int SQUEEZE_RATE_INBYTES = 16;
9983
System.arraycopy(state, 0, output, outOff, SQUEEZE_RATE_INBYTES);
100-
PHOTON_Permutation();
84+
PhotonBeetleEngine.PhotonPermutation(Friend.INSTANCE, state_2d, state);
10185
System.arraycopy(state, 0, output, outOff + SQUEEZE_RATE_INBYTES, DigestSize - SQUEEZE_RATE_INBYTES);
10286
}
10387

@@ -108,74 +92,4 @@ public void reset()
10892
Arrays.fill(state, (byte)0);
10993
blockCount = 0;
11094
}
111-
112-
void PHOTON_Permutation()
113-
{
114-
int i, j, k;
115-
int DSquare = 64;
116-
int dr = 7;
117-
int dq = 3;
118-
for (i = 0; i < DSquare; i++)
119-
{
120-
state_2d[i >>> dq][i & dr] = (byte)(((state[i >> 1] & 0xFF) >>> (4 * (i & 1))) & 0xf);
121-
}
122-
int ROUND = 12;
123-
for (int round = 0; round < ROUND; round++)
124-
{
125-
//AddKey
126-
for (i = 0; i < D; i++)
127-
{
128-
state_2d[i][0] ^= RC[i][round];
129-
}
130-
//SubCell
131-
for (i = 0; i < D; i++)
132-
{
133-
for (j = 0; j < D; j++)
134-
{
135-
state_2d[i][j] = sbox[state_2d[i][j]];
136-
}
137-
}
138-
//ShiftRow
139-
for (i = 1; i < D; i++)
140-
{
141-
System.arraycopy(state_2d[i], 0, state, 0, D);
142-
System.arraycopy(state, i, state_2d[i], 0, D - i);
143-
System.arraycopy(state, 0, state_2d[i], D - i, i);
144-
}
145-
//MixColumn
146-
for (j = 0; j < D; j++)
147-
{
148-
for (i = 0; i < D; i++)
149-
{
150-
int sum = 0;
151-
152-
for (k = 0; k < D; k++)
153-
{
154-
int x = MixColMatrix[i][k], b = state_2d[k][j];
155-
156-
sum ^= x * (b & 1);
157-
sum ^= x * (b & 2);
158-
sum ^= x * (b & 4);
159-
sum ^= x * (b & 8);
160-
}
161-
162-
int t0 = sum >>> 4;
163-
sum = (sum & 15) ^ t0 ^ (t0 << 1);
164-
165-
int t1 = sum >>> 4;
166-
sum = (sum & 15) ^ t1 ^ (t1 << 1);
167-
168-
state[i] = (byte)sum;
169-
}
170-
for (i = 0; i < D; i++)
171-
{
172-
state_2d[i][j] = state[i];
173-
}
174-
}
175-
}
176-
for (i = 0; i < DSquare; i += 2)
177-
{
178-
state[i >>> 1] = (byte)(((state_2d[i >>> dq][i & dr] & 0xf)) | ((state_2d[i >>> dq][(i + 1) & dr] & 0xf) << 4));
179-
}
180-
}
18195
}

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

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

3+
import org.bouncycastle.crypto.digests.PhotonBeetleDigest;
4+
35
/**
46
* Photon-Beetle, <a href="https://www.isical.ac.in/~lightweight/beetle/"></a>
57
* https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-spec-doc/photon-beetle-spec-final.pdf
@@ -25,9 +27,9 @@ public enum PhotonBeetleParameters
2527
private final int RATE_INBYTES_HALF;
2628
private final int STATE_INBYTES;
2729
private final int LAST_THREE_BITS_OFFSET;
28-
private final int D = 8;
30+
private static final int D = 8;
2931
private boolean aadFinished;
30-
private final byte[][] RC = {
32+
private static final byte[][] RC = {
3133
{1, 3, 7, 14, 13, 11, 6, 12, 9, 2, 5, 10},
3234
{0, 2, 6, 15, 12, 10, 7, 13, 8, 3, 4, 11},
3335
{2, 0, 4, 13, 14, 8, 5, 15, 10, 1, 6, 9},
@@ -37,7 +39,7 @@ public enum PhotonBeetleParameters
3739
{13, 15, 11, 2, 1, 7, 10, 0, 5, 14, 9, 6},
3840
{9, 11, 15, 6, 5, 3, 14, 4, 1, 10, 13, 2}
3941
};
40-
private final byte[][] MixColMatrix = {
42+
private static final byte[][] MixColMatrix = {
4143
{2, 4, 2, 11, 2, 8, 5, 6},
4244
{12, 9, 8, 13, 7, 7, 5, 2},
4345
{4, 4, 13, 13, 9, 4, 13, 9},
@@ -48,7 +50,7 @@ public enum PhotonBeetleParameters
4850
{15, 1, 13, 10, 5, 10, 2, 3}
4951
};
5052

51-
private final byte[] sbox = {12, 5, 6, 11, 9, 0, 10, 13, 3, 14, 15, 8, 4, 7, 1, 2};
53+
private static final byte[] sbox = {12, 5, 6, 11, 9, 0, 10, 13, 3, 14, 15, 8, 4, 7, 1, 2};
5254

5355
public PhotonBeetleEngine(PhotonBeetleParameters pbp)
5456
{
@@ -93,7 +95,7 @@ protected void init(byte[] key, byte[] iv)
9395

9496
protected void processBufferAAD(byte[] input, int inOff)
9597
{
96-
PHOTON_Permutation();
98+
PhotonPermutation(state_2d, state);
9799
XOR(input, inOff, BlockSize);
98100
}
99101

@@ -106,7 +108,7 @@ public void processFinalAAD()
106108
{
107109
if (m_aadPos != 0)
108110
{
109-
PHOTON_Permutation();
111+
PhotonPermutation(state_2d, state);
110112
XOR(m_aad, 0, m_aadPos);
111113
if (m_aadPos < BlockSize)
112114
{
@@ -123,14 +125,14 @@ public void processFinalAAD()
123125

124126
protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
125127
{
126-
PHOTON_Permutation();
128+
PhotonPermutation(state_2d, state);
127129
rhoohr(output, outOff, input, inOff, BlockSize);
128130
XOR(input, inOff, BlockSize);
129131
}
130132

131133
protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
132134
{
133-
PHOTON_Permutation();
135+
PhotonPermutation(state_2d, state);
134136
rhoohr(output, outOff, input, inOff, BlockSize);
135137
XOR(output, outOff, BlockSize);
136138
}
@@ -151,7 +153,7 @@ protected void processFinalBlock(byte[] output, int outOff)
151153
{
152154
if (bufferLen != 0)
153155
{
154-
PHOTON_Permutation();
156+
PhotonPermutation(state_2d, state);
155157
rhoohr(output, outOff, m_buf, 0, bufferLen);
156158
if (forEncryption)
157159
{
@@ -172,7 +174,7 @@ protected void processFinalBlock(byte[] output, int outOff)
172174
{
173175
state[STATE_INBYTES - 1] ^= 1 << LAST_THREE_BITS_OFFSET;
174176
}
175-
PHOTON_Permutation();
177+
PhotonPermutation(state_2d, state);
176178
mac = new byte[MAC_SIZE];
177179
System.arraycopy(state, 0, mac, 0, MAC_SIZE);
178180
}
@@ -188,7 +190,7 @@ protected void reset(boolean clearMac)
188190
super.reset(clearMac);
189191
}
190192

191-
private void PHOTON_Permutation()
193+
private static void PhotonPermutation(byte[][] state_2d, byte[] state)
192194
{
193195
int i, j, k;
194196
int dq = 3;
@@ -302,4 +304,14 @@ private void XOR(byte[] in_right, int rOff, int iolen_inbytes)
302304
state[i] ^= in_right[rOff++];
303305
}
304306
}
307+
308+
public static void PhotonPermutation(PhotonBeetleDigest.Friend friend, byte[][] state_2d, byte[] state)
309+
{
310+
if (null == friend)
311+
{
312+
throw new NullPointerException("This method is only for use by PhotonBeetleDigest");
313+
}
314+
315+
PhotonPermutation(state_2d, state);
316+
}
305317
}

0 commit comments

Comments
 (0)