Skip to content

Commit 6cfa1c2

Browse files
author
gefeili
committed
Refactor in Engines
1 parent 476db85 commit 6cfa1c2

File tree

3 files changed

+34
-52
lines changed

3 files changed

+34
-52
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.bouncycastle.util.Arrays;
44
import org.bouncycastle.util.Bytes;
5-
import org.bouncycastle.util.Longs;
65
import org.bouncycastle.util.Pack;
76

87
/**

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

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ else if (mlen == 0)
182182
while (xlen > 0)
183183
{
184184
offset = mOff;
185-
xlen = ad_encryption(m, mOff, mac_s, k, xlen, mac_CNT, (byte)44);
185+
xlen = ad_encryption(m, mOff, mac_s, k, xlen, mac_CNT);
186186
mOff = offset;
187187
}
188-
nonce_encryption(npub, mac_CNT, mac_s, k, w);
188+
block_cipher(mac_s, k, npub, 0, mac_CNT, w);
189189
// Tag generation
190190
g8A(mac_s, mac, 0);
191191
mOff -= mlen;
@@ -198,15 +198,15 @@ else if (mlen == 0)
198198
System.arraycopy(mac, 0, s, 0, AD_BLK_LEN_HALF);
199199
if (mlen > 0)
200200
{
201-
nonce_encryption(npub, CNT, s, k, (byte)36);
201+
block_cipher(s, k, npub, 0, CNT, (byte)36);
202202
while (mlen > AD_BLK_LEN_HALF)
203203
{
204204
mlen = mlen - AD_BLK_LEN_HALF;
205205
rho(m, mOff, output, outOff, s, AD_BLK_LEN_HALF);
206206
outOff += AD_BLK_LEN_HALF;
207207
mOff += AD_BLK_LEN_HALF;
208208
lfsr_gf56(CNT);
209-
nonce_encryption(npub, CNT, s, k, (byte)36);
209+
block_cipher(s, k, npub, 0, CNT, (byte)36);
210210
}
211211
rho(m, mOff, output, outOff, s, mlen);
212212
}
@@ -229,18 +229,18 @@ else if (mlen == 0)
229229
while (xlen > 0)
230230
{
231231
offset = mauth;
232-
xlen = ad_encryption(output, mauth, mac_s, k, xlen, mac_CNT, (byte)44);
232+
xlen = ad_encryption(output, mauth, mac_s, k, xlen, mac_CNT);
233233
mauth = offset;
234234
}
235-
nonce_encryption(npub, mac_CNT, mac_s, k, w);
235+
block_cipher(mac_s, k, npub, 0, mac_CNT, w);
236236
// Tag generation
237237
g8A(mac_s, mac, 0);
238238
System.arraycopy(m, dataOperator.getLen() - MAC_SIZE, m_buf, 0, MAC_SIZE);
239239
m_bufPos = 0;
240240
}
241241
}
242242

243-
int ad_encryption(byte[] A, int AOff, byte[] s, byte[] k, int adlen, byte[] CNT, byte D)
243+
int ad_encryption(byte[] A, int AOff, byte[] s, byte[] k, int adlen, byte[] CNT)
244244
{
245245
byte[] T = new byte[16];
246246
byte[] mp = new byte[16];
@@ -259,7 +259,7 @@ int ad_encryption(byte[] A, int AOff, byte[] s, byte[] k, int adlen, byte[] CNT,
259259
adlen -= len8;
260260
pad(A, AOff, T, n, len8);
261261
offset = AOff + len8;
262-
block_cipher(s, k, T, 0, CNT, D);
262+
block_cipher(s, k, T, 0, CNT, (byte)44);
263263
lfsr_gf56(CNT);
264264
}
265265
return adlen;
@@ -345,14 +345,14 @@ public void processFinalBlock(byte[] output, int outOff)
345345
if (messegeLen == 0)
346346
{
347347
lfsr_gf56(CNT);
348-
nonce_encryption(npub, CNT, s, k, (byte)0x15);
348+
block_cipher(s, k, npub, 0, CNT, (byte)0x15);
349349
}
350350
else if (m_bufPos != 0)
351351
{
352352
int len8 = Math.min(m_bufPos, AD_BLK_LEN_HALF);
353353
rho(m_buf, 0, output, outOff, s, len8);
354354
lfsr_gf56(CNT);
355-
nonce_encryption(npub, CNT, s, k, m_bufPos == AD_BLK_LEN_HALF ? (byte)0x14 : (byte)0x15);
355+
block_cipher(s, k, npub, 0, CNT, m_bufPos == AD_BLK_LEN_HALF ? (byte)0x14 : (byte)0x15);
356356
}
357357
g8A(s, mac, 0);
358358
}
@@ -393,15 +393,15 @@ public void processFinalAAD()
393393
if (aadOperator.getLen() == 0)
394394
{
395395
lfsr_gf56(CNT);
396-
nonce_encryption(npub, CNT, s, k, (byte)0x1a);
396+
block_cipher(s, k, npub, 0, CNT, (byte)0x1a);
397397
}
398398
else if ((m_aadPos & 15) != 0)
399399
{
400-
nonce_encryption(npub, CNT, s, k, (byte)0x1a);
400+
block_cipher(s, k, npub, 0, CNT, (byte)0x1a);
401401
}
402402
else
403403
{
404-
nonce_encryption(npub, CNT, s, k, (byte)0x18);
404+
block_cipher(s, k, npub, 0, CNT, (byte)0x18);
405405
}
406406
reset_lfsr_gf56(CNT);
407407
}
@@ -416,7 +416,7 @@ public void processBufferEncrypt(byte[] input, int inOff, byte[] output, int out
416416
output[i + outOff] ^= input[i + inOff];
417417
}
418418
lfsr_gf56(CNT);
419-
nonce_encryption(npub, CNT, s, k, (byte)0x04);
419+
block_cipher(s, k, npub, 0, CNT, (byte)0x04);
420420
}
421421

422422
@Override
@@ -425,12 +425,11 @@ public void processBufferDecrypt(byte[] input, int inOff, byte[] output, int out
425425
g8A(s, output, outOff);
426426
for (int i = 0; i < AD_BLK_LEN_HALF; i++)
427427
{
428-
s[i] ^= input[i + inOff];
429-
s[i] ^= output[i + outOff];
430428
output[i + outOff] ^= input[i + inOff];
429+
s[i] ^= output[i + outOff];
431430
}
432431
lfsr_gf56(CNT);
433-
nonce_encryption(npub, CNT, s, k, (byte)0x04);
432+
block_cipher(s, k, npub, 0, CNT, (byte)0x04);
434433
}
435434

436435
@Override
@@ -562,8 +561,7 @@ else if ((m_aadPos >= 0) && (aadOperator.getLen() != 0))
562561
}
563562
}
564563

565-
@Override
566-
public void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
564+
private void processBuffer(byte[] input, int inOff, byte[] output, int outOff)
567565
{
568566
System.arraycopy(npub, 0, S, 0, 16);
569567
block_cipher(S, Z, T, 0, CNT, (byte)64);
@@ -572,44 +570,38 @@ public void processBufferEncrypt(byte[] input, int inOff, byte[] output, int out
572570
block_cipher(S, Z, T, 0, CNT, (byte)65);
573571
System.arraycopy(S, 0, Z, 0, 16);
574572
lfsr_gf56(CNT);
575-
// ipad_256(ipad*_128(A)||ipad*_128(C)||N|| CNT
576-
System.arraycopy(output, outOff, m_aad, m_aadPos, BlockSize);
573+
}
574+
575+
private void processAfterAbsorbCiphertext()
576+
{
577577
if (m_aadPos == BlockSize)
578578
{
579579
hirose_128_128_256(h, g, m_aad, 0);
580580
m_aadPos = 0;
581-
lfsr_gf56(CNT_Z);
582581
}
583582
else
584583
{
585584
m_aadPos = BlockSize;
586-
lfsr_gf56(CNT_Z);
587585
}
586+
lfsr_gf56(CNT_Z);
587+
}
588+
589+
@Override
590+
public void processBufferEncrypt(byte[] input, int inOff, byte[] output, int outOff)
591+
{
592+
processBuffer(input, inOff, output, outOff);
593+
// ipad_256(ipad*_128(A)||ipad*_128(C)||N|| CNT
594+
System.arraycopy(output, outOff, m_aad, m_aadPos, BlockSize);
595+
processAfterAbsorbCiphertext();
588596
}
589597

590598
@Override
591599
public void processBufferDecrypt(byte[] input, int inOff, byte[] output, int outOff)
592600
{
593-
System.arraycopy(npub, 0, S, 0, 16);
594-
block_cipher(S, Z, T, 0, CNT, (byte)64);
595-
Bytes.xor(AD_BLK_LEN_HALF, S, input, inOff, output, outOff);
596-
System.arraycopy(npub, 0, S, 0, 16);
597-
block_cipher(S, Z, T, 0, CNT, (byte)65);
598-
System.arraycopy(S, 0, Z, 0, 16);
599-
lfsr_gf56(CNT);
601+
processBuffer(input, inOff, output, outOff);
600602
// ipad_256(ipad*_128(A)||ipad*_128(C)||N|| CNT
601603
System.arraycopy(input, inOff, m_aad, m_aadPos, BlockSize);
602-
if (m_aadPos == BlockSize)
603-
{
604-
hirose_128_128_256(h, g, m_aad, 0);
605-
m_aadPos = 0;
606-
lfsr_gf56(CNT_Z);
607-
}
608-
else
609-
{
610-
m_aadPos = BlockSize;
611-
lfsr_gf56(CNT_Z);
612-
}
604+
processAfterAbsorbCiphertext();
613605
}
614606

615607
@Override
@@ -825,18 +817,10 @@ void block_cipher(byte[] s, byte[] K, byte[] T, int tOff, byte[] CNT, byte D)
825817
skinny_128_384_plus_enc(s, KT);
826818
}
827819

828-
// Calls the TBC using the nonce as part of the tweakey
829-
void nonce_encryption(byte[] N, byte[] CNT, byte[] s, byte[] k, byte D)
830-
{
831-
byte[] T = new byte[16];
832-
System.arraycopy(N, 0, T, 0, 16);
833-
block_cipher(s, k, T, 0, CNT, D);
834-
}
835-
836820
private void reset_lfsr_gf56(byte[] CNT)
837821
{
838822
CNT[0] = 0x01;
839-
Arrays.fill(CNT, 1, 7, (byte) 0);
823+
Arrays.fill(CNT, 1, 7, (byte)0);
840824
}
841825

842826
public static void hirose_128_128_256(RomulusDigest.Friend friend, byte[] h, byte[] g, byte[] m, int mOff)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.bouncycastle.crypto.digests.SparkleDigest;
44
import org.bouncycastle.util.Arrays;
5-
import org.bouncycastle.util.Bytes;
65
import org.bouncycastle.util.Integers;
76
import org.bouncycastle.util.Pack;
87

0 commit comments

Comments
 (0)