Skip to content

Commit 26ac00b

Browse files
author
gefeili
committed
Fix issues and use bigEndian to/from long to replace littleEndian in ISAPEngine.
1 parent 54b8279 commit 26ac00b

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ private interface ISAP_AEAD
7070

7171
void init();
7272

73-
void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag, int tagOff);
73+
void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag);
7474

7575
void reset();
7676
}
7777

78-
public abstract class ISAPAEAD_A
78+
private abstract class ISAPAEAD_A
7979
implements ISAP_AEAD
8080
{
8181
protected long[] k64;
@@ -94,13 +94,9 @@ public ISAPAEAD_A()
9494
public void init()
9595
{
9696
npub64 = new long[getLongSize(npub.length)];
97-
Pack.littleEndianToLong(npub, 0, npub64, 0, npub64.length);
98-
npub64[0] = U64BIG(npub64[0]);
99-
npub64[1] = U64BIG(npub64[1]);
10097
k64 = new long[getLongSize(k.length)];
101-
Pack.littleEndianToLong(k, 0, k64, 0, k64.length);
102-
k64[0] = U64BIG(k64[0]);
103-
k64[1] = U64BIG(k64[1]);
98+
Pack.bigEndianToLong(npub, 0, npub64);
99+
Pack.bigEndianToLong(k, 0, k64);
104100
reset();
105101
}
106102

@@ -111,11 +107,11 @@ public void init()
111107
protected void ABSORB_MAC(byte[] src, int len)
112108
{
113109
long[] src64 = new long[src.length >> 3];
114-
Pack.littleEndianToLong(src, 0, src64, 0, src64.length);
110+
Pack.bigEndianToLong(src, 0, src64, 0, src64.length);
115111
int idx = 0;
116112
while (len >= ISAP_rH_SZ)
117113
{
118-
x0 ^= U64BIG(src64[idx++]);
114+
x0 ^= src64[idx++];
119115
P12();
120116
len -= ISAP_rH_SZ;
121117
}
@@ -128,7 +124,7 @@ protected void ABSORB_MAC(byte[] src, int len)
128124
P12();
129125
}
130126

131-
public void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag, int tagOff)
127+
public void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag)
132128
{
133129
// Init State
134130
x0 = npub64[0];
@@ -141,17 +137,17 @@ public void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag, int t
141137
x4 ^= 1L;
142138
ABSORB_MAC(c, clen);
143139
// Derive K*
144-
Pack.longToLittleEndian(U64BIG(x0), tag, 0);
145-
Pack.longToLittleEndian(U64BIG(x1), tag, 8);
140+
Pack.longToBigEndian(x0, tag, 0);
141+
Pack.longToBigEndian(x1, tag, 8);
146142
long tmp_x2 = x2, tmp_x3 = x3, tmp_x4 = x4;
147143
isap_rk(ISAP_IV2_64, tag, KEY_SIZE);
148144
x2 = tmp_x2;
149145
x3 = tmp_x3;
150146
x4 = tmp_x4;
151147
// Squeeze tag
152148
P12();
153-
Pack.longToLittleEndian(U64BIG(x0), tag, tagOff);
154-
Pack.longToLittleEndian(U64BIG(x1), tag, tagOff + 8);
149+
Pack.longToBigEndian(x0, tag, 0);
150+
Pack.longToBigEndian(x1, tag, 8);
155151
}
156152

157153
public void isap_rk(long iv64, byte[] y, int ylen)
@@ -399,7 +395,7 @@ public void isap_rk(short[] iv16, byte[] y, int ylen, short[] out16, int outlen,
399395
System.arraycopy(SX, 0, out16, 0, outlen == ISAP_STATE_SZ_CRYPTO_NPUBBYTES ? 17 : 8);
400396
}
401397

402-
public void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag, int tagOff)
398+
public void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag)
403399
{
404400
SX = new short[25];
405401
// Init state
@@ -413,11 +409,11 @@ public void isap_mac(byte[] ad, int adlen, byte[] c, int clen, byte[] tag, int t
413409
// Absorb C
414410
ABSORB_MAC(SX, c, clen, E, C);
415411
// Derive K*
416-
shortToByte(SX, tag, tagOff);
412+
shortToByte(SX, tag, 0);
417413
isap_rk(ISAP_IV2_16, tag, KEY_SIZE, SX, KEY_SIZE, C);
418414
// Squeeze tag
419415
PermuteRoundsHX(SX, E, C);
420-
shortToByte(SX, tag, tagOff);
416+
shortToByte(SX, tag, 0);
421417
}
422418

423419
public void isap_enc(byte[] m, int mOff, int mlen, byte[] c, int cOff, int clen)
@@ -852,10 +848,6 @@ public int doFinal(byte[] output, int outOff)
852848
int len;
853849
byte[] c;
854850
byte[] ad;
855-
if (mac == null)
856-
{
857-
mac = new byte[MAC_SIZE];
858-
}
859851
if (forEncryption)
860852
{
861853
byte[] enc_input = message.toByteArray();
@@ -869,20 +861,22 @@ public int doFinal(byte[] output, int outOff)
869861
outOff += len;
870862
ad = aadData.toByteArray();
871863
c = outputStream.toByteArray();
872-
ISAPAEAD.isap_mac(ad, ad.length, c, c.length, mac, 0);
864+
mac = new byte[MAC_SIZE];
865+
ISAPAEAD.isap_mac(ad, ad.length, c, c.length, mac);
873866
System.arraycopy(mac, 0, output, outOff, 16);
874867
len += 16;
875868
}
876869
else
877870
{
878871
ad = aadData.toByteArray();
879872
c = message.toByteArray();
873+
mac = new byte[MAC_SIZE];
880874
len = c.length - mac.length;
881875
if (len + outOff > output.length)
882876
{
883877
throw new OutputLengthException("output buffer is too short");
884878
}
885-
ISAPAEAD.isap_mac(ad, ad.length, c, len, mac, 0);
879+
ISAPAEAD.isap_mac(ad, ad.length, c, len, mac);
886880
ISAPAEAD.reset();
887881
for (int i = 0; i < 16; ++i)
888882
{
@@ -893,6 +887,7 @@ public int doFinal(byte[] output, int outOff)
893887
}
894888
ISAPAEAD.isap_enc(c, 0, len, output, outOff, output.length);
895889
}
890+
// reset(false);
896891
return len;
897892
}
898893

core/src/test/java/org/bouncycastle/crypto/test/ISAPTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,42 @@ public void performTest()
5050
testVectors("isapk128av20", IsapType.ISAP_K_128A);
5151
testVectors("isapk128v20", IsapType.ISAP_K_128);
5252
testVectors();
53+
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
54+
{
55+
@Override
56+
public AEADCipher createInstance()
57+
{
58+
return new ISAPEngine(IsapType.ISAP_K_128A);
59+
}
60+
});
61+
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
62+
{
63+
@Override
64+
public AEADCipher createInstance()
65+
{
66+
return new ISAPEngine(IsapType.ISAP_K_128);
67+
}
68+
});
69+
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
70+
{
71+
@Override
72+
public AEADCipher createInstance()
73+
{
74+
return new ISAPEngine(IsapType.ISAP_A_128A);
75+
}
76+
});
77+
CipherTest.checkCipher(32, 16, 100, 128, new CipherTest.Instance()
78+
{
79+
@Override
80+
public AEADCipher createInstance()
81+
{
82+
return new ISAPEngine(IsapType.ISAP_A_128);
83+
}
84+
});
5385
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_K_128A));
5486
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_K_128));
55-
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_A_128A));
56-
CipherTest.checkAEADParemeter(this, 16, 16, 16, 16, new ISAPEngine(IsapType.ISAP_A_128));
87+
CipherTest.checkAEADParemeter(this, 16, 16, 16, 8, new ISAPEngine(IsapType.ISAP_A_128A));
88+
CipherTest.checkAEADParemeter(this, 16, 16, 16, 8, new ISAPEngine(IsapType.ISAP_A_128));
5789
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128A));
5890
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128));
5991
CipherTest.checkAEADCipherOutputSize(this, 16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128A));

0 commit comments

Comments
 (0)