Skip to content

Commit 1a0e7d0

Browse files
author
gefeili
committed
Refactor on XoodyakEngine and XoodyakDigest, refactor on ISAPDigest
1 parent 1dc34f1 commit 1a0e7d0

File tree

6 files changed

+63
-187
lines changed

6 files changed

+63
-187
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,19 @@ protected void processBytes(byte[] input, int inOff)
6262
protected void finish(byte[] output, int outOff)
6363
{
6464
/* absorb final input block */
65-
int idx;
6665
p.x0 ^= 0x80L << ((7 - m_bufPos) << 3);
6766
while (m_bufPos > 0)
6867
{
6968
p.x0 ^= (m_buf[--m_bufPos] & 0xFFL) << ((7 - m_bufPos) << 3);
7069
}
71-
p.p(12);
7270
// squeeze
7371
long[] out64 = new long[4];
74-
for (idx = 0; idx < 3; ++idx)
72+
for (int i = 0; i < 4; ++i)
7573
{
76-
out64[idx] = U64BIG(p.x0);
7774
p.p(12);
75+
out64[i] = U64BIG(p.x0);
7876
}
7977
/* squeeze final output block */
80-
out64[idx] = U64BIG(p.x0);
8178
Pack.longToLittleEndian(out64, output, outOff);
8279
}
8380

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ private Friend()
2121
}
2222
}
2323

24-
byte[] h = new byte[16];
25-
byte[] g = new byte[16];
24+
private final byte[] h = new byte[16];
25+
private final byte[] g = new byte[16];
2626
/*
2727
* This file includes only the encryption function of SKINNY-128-384+ as required by Romulus-v1.3
2828
*/
Lines changed: 20 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.bouncycastle.crypto.digests;
22

3+
import org.bouncycastle.crypto.engines.XoodyakEngine;
34
import org.bouncycastle.util.Arrays;
4-
import org.bouncycastle.util.Integers;
5-
import org.bouncycastle.util.Pack;
65

76
/**
87
* Xoodyak v1, https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-spec-doc/xoodyak-spec-final.pdf
@@ -14,20 +13,21 @@
1413
public class XoodyakDigest
1514
extends BufferBaseDigest
1615
{
16+
public static class Friend
17+
{
18+
private static final Friend INSTANCE = new Friend();
19+
20+
private Friend()
21+
{
22+
}
23+
}
24+
1725
private final byte[] state;
1826
private int phase;
19-
private MODE mode;
20-
private final int f_bPrime = 48;
21-
private final int PhaseUp = 2;
27+
private static final int mode = 1; // set as ModeHash
28+
private static final int PhaseUp = 2;
29+
private static final int TAGLEN = 16;
2230
private int Cd;
23-
private final int[] RC = {0x00000058, 0x00000038, 0x000003C0, 0x000000D0, 0x00000120, 0x00000014, 0x00000060,
24-
0x0000002C, 0x00000380, 0x000000F0, 0x000001A0, 0x00000012};
25-
26-
enum MODE
27-
{
28-
ModeHash,
29-
ModeKeyed
30-
}
3131

3232
public XoodyakDigest()
3333
{
@@ -43,9 +43,9 @@ protected void processBytes(byte[] input, int inOff)
4343
{
4444
if (phase != PhaseUp)
4545
{
46-
Up(null, 0, 0, 0);
46+
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, null, 0, 0, 0);
4747
}
48-
Down(input, inOff, BlockSize, Cd);
48+
phase = XoodyakEngine.down(Friend.INSTANCE, mode, state, input, inOff, BlockSize, Cd);
4949
Cd = 0;
5050
}
5151

@@ -56,14 +56,13 @@ protected void finish(byte[] output, int outOff)
5656
{
5757
if (phase != PhaseUp)
5858
{
59-
Up(null, 0, 0, 0);
59+
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, null, 0, 0, 0);
6060
}
61-
Down(m_buf, 0, m_bufPos, Cd);
61+
phase = XoodyakEngine.down(Friend.INSTANCE, mode, state, m_buf, 0, m_bufPos, Cd);
6262
}
63-
int TAGLEN = 16;
64-
Up(output, outOff, TAGLEN, 0x40);
65-
Down(null, 0, 0, 0);
66-
Up(output, outOff + TAGLEN, TAGLEN, 0);
63+
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, output, outOff, TAGLEN, 0x40);
64+
phase = XoodyakEngine.down(Friend.INSTANCE, mode, state, null, 0, 0, 0);
65+
phase = XoodyakEngine.up(Friend.INSTANCE, mode, state, output, outOff + TAGLEN, TAGLEN, 0);
6766
}
6867

6968
@Override
@@ -72,135 +71,6 @@ public void reset()
7271
super.reset();
7372
Arrays.fill(state, (byte)0);
7473
phase = PhaseUp;
75-
mode = MODE.ModeHash;
7674
Cd = 0x03;
7775
}
78-
79-
private void Up(byte[] Yi, int YiOff, int YiLen, int Cu)
80-
{
81-
if (mode != MODE.ModeHash)
82-
{
83-
state[f_bPrime - 1] ^= Cu;
84-
}
85-
86-
int a0 = Pack.littleEndianToInt(state, 0);
87-
int a1 = Pack.littleEndianToInt(state, 4);
88-
int a2 = Pack.littleEndianToInt(state, 8);
89-
int a3 = Pack.littleEndianToInt(state, 12);
90-
int a4 = Pack.littleEndianToInt(state, 16);
91-
int a5 = Pack.littleEndianToInt(state, 20);
92-
int a6 = Pack.littleEndianToInt(state, 24);
93-
int a7 = Pack.littleEndianToInt(state, 28);
94-
int a8 = Pack.littleEndianToInt(state, 32);
95-
int a9 = Pack.littleEndianToInt(state, 36);
96-
int a10 = Pack.littleEndianToInt(state, 40);
97-
int a11 = Pack.littleEndianToInt(state, 44);
98-
99-
int MAXROUNDS = 12;
100-
for (int i = 0; i < MAXROUNDS; ++i)
101-
{
102-
/* Theta: Column Parity Mixer */
103-
int p0 = a0 ^ a4 ^ a8;
104-
int p1 = a1 ^ a5 ^ a9;
105-
int p2 = a2 ^ a6 ^ a10;
106-
int p3 = a3 ^ a7 ^ a11;
107-
108-
int e0 = Integers.rotateLeft(p3, 5) ^ Integers.rotateLeft(p3, 14);
109-
int e1 = Integers.rotateLeft(p0, 5) ^ Integers.rotateLeft(p0, 14);
110-
int e2 = Integers.rotateLeft(p1, 5) ^ Integers.rotateLeft(p1, 14);
111-
int e3 = Integers.rotateLeft(p2, 5) ^ Integers.rotateLeft(p2, 14);
112-
113-
a0 ^= e0;
114-
a4 ^= e0;
115-
a8 ^= e0;
116-
117-
a1 ^= e1;
118-
a5 ^= e1;
119-
a9 ^= e1;
120-
121-
a2 ^= e2;
122-
a6 ^= e2;
123-
a10 ^= e2;
124-
125-
a3 ^= e3;
126-
a7 ^= e3;
127-
a11 ^= e3;
128-
129-
/* Rho-west: plane shift */
130-
int b0 = a0;
131-
int b1 = a1;
132-
int b2 = a2;
133-
int b3 = a3;
134-
135-
int b4 = a7;
136-
int b5 = a4;
137-
int b6 = a5;
138-
int b7 = a6;
139-
140-
int b8 = Integers.rotateLeft(a8, 11);
141-
int b9 = Integers.rotateLeft(a9, 11);
142-
int b10 = Integers.rotateLeft(a10, 11);
143-
int b11 = Integers.rotateLeft(a11, 11);
144-
145-
/* Iota: round ant */
146-
b0 ^= RC[i];
147-
148-
/* Chi: non linear layer */
149-
a0 = b0 ^ (~b4 & b8);
150-
a1 = b1 ^ (~b5 & b9);
151-
a2 = b2 ^ (~b6 & b10);
152-
a3 = b3 ^ (~b7 & b11);
153-
154-
a4 = b4 ^ (~b8 & b0);
155-
a5 = b5 ^ (~b9 & b1);
156-
a6 = b6 ^ (~b10 & b2);
157-
a7 = b7 ^ (~b11 & b3);
158-
159-
b8 ^= (~b0 & b4);
160-
b9 ^= (~b1 & b5);
161-
b10 ^= (~b2 & b6);
162-
b11 ^= (~b3 & b7);
163-
164-
/* Rho-east: plane shift */
165-
a4 = Integers.rotateLeft(a4, 1);
166-
a5 = Integers.rotateLeft(a5, 1);
167-
a6 = Integers.rotateLeft(a6, 1);
168-
a7 = Integers.rotateLeft(a7, 1);
169-
170-
a8 = Integers.rotateLeft(b10, 8);
171-
a9 = Integers.rotateLeft(b11, 8);
172-
a10 = Integers.rotateLeft(b8, 8);
173-
a11 = Integers.rotateLeft(b9, 8);
174-
}
175-
176-
Pack.intToLittleEndian(a0, state, 0);
177-
Pack.intToLittleEndian(a1, state, 4);
178-
Pack.intToLittleEndian(a2, state, 8);
179-
Pack.intToLittleEndian(a3, state, 12);
180-
Pack.intToLittleEndian(a4, state, 16);
181-
Pack.intToLittleEndian(a5, state, 20);
182-
Pack.intToLittleEndian(a6, state, 24);
183-
Pack.intToLittleEndian(a7, state, 28);
184-
Pack.intToLittleEndian(a8, state, 32);
185-
Pack.intToLittleEndian(a9, state, 36);
186-
Pack.intToLittleEndian(a10, state, 40);
187-
Pack.intToLittleEndian(a11, state, 44);
188-
189-
phase = PhaseUp;
190-
if (Yi != null)
191-
{
192-
System.arraycopy(state, 0, Yi, YiOff, YiLen);
193-
}
194-
}
195-
196-
void Down(byte[] Xi, int XiOff, int XiLen, int Cd)
197-
{
198-
for (int i = 0; i < XiLen; i++)
199-
{
200-
state[i] ^= Xi[XiOff++];
201-
}
202-
state[XiLen] ^= 0x01;
203-
state[f_bPrime - 1] ^= (mode == MODE.ModeHash) ? (Cd & 0x01) : Cd;
204-
phase = 1;
205-
}
20676
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,6 @@ protected void init(byte[] key, byte[] iv)
739739
{
740740
npub = iv;
741741
k = key;
742-
m_buf = new byte[BlockSize + (forEncryption ? 0 : MAC_SIZE)];
743742
ISAPAEAD.init();
744743
m_state = forEncryption ? State.EncInit : State.DecInit;
745744
reset();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ protected void init(byte[] key, byte[] iv)
8888
N = iv;
8989
state = new byte[STATE_INBYTES];
9090
state_2d = new byte[D][D];
91-
mac = new byte[MAC_SIZE];
9291
m_state = forEncryption ? State.EncInit : State.DecInit;
9392
reset(false);
9493
}

0 commit comments

Comments
 (0)