@@ -52,16 +52,15 @@ public ISAPEngine(IsapType isapType)
5252 algorithmName = "ISAP-K-128 AEAD" ;
5353 break ;
5454 }
55- aadData = new byte [ISAP_rH_SZ ];
55+ buffer = new byte [ISAP_rH_SZ ];
5656 }
5757
5858 private boolean initialised ;
5959 final int ISAP_STATE_SZ = 40 ;
6060 private byte [] k ;
6161 private byte [] npub ;
62- //private final ByteArrayOutputStream aadData = new ByteArrayOutputStream();
63- private final byte [] aadData ;
64- private int aadOff ;
62+ private final byte [] buffer ;
63+ private int bufferOff ;
6564 private final ByteArrayOutputStream message = new ByteArrayOutputStream ();
6665 private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
6766 private int ISAP_rH ;
@@ -81,6 +80,8 @@ private interface ISAP_AEAD
8180
8281 void absorbMacBlock (byte [] input , int inOff );
8382
83+ void absorbFinalAADBlock ();
84+
8485 void swapInternalState ();
8586 }
8687
@@ -158,18 +159,19 @@ protected void ABSORB_MAC(byte[] src, int len)
158159 P12 ();
159160 }
160161
161- public void isap_mac ( byte [] ad , int adlen , byte [] c , int clen , byte [] tag )
162+ public void absorbFinalAADBlock ( )
162163 {
163- // Init State
164- // x0 = npub64[0];
165- // x1 = npub64[1];
166- // x2 = ISAP_IV1_64;
167- // x3 = x4 = 0;
168- // P12();
169-
170- ABSORB_MAC (ad , adlen );
171- // Domain seperation
164+ for (int i = 0 ; i < bufferOff ; ++i )
165+ {
166+ x0 ^= (buffer [i ] & 0xFFL ) << ((7 - i ) << 3 );
167+ }
168+ x0 ^= 0x80L << ((7 - bufferOff ) << 3 );
169+ P12 ();
172170 x4 ^= 1L ;
171+ }
172+
173+ public void isap_mac (byte [] ad , int adlen , byte [] c , int clen , byte [] tag )
174+ {
173175 ABSORB_MAC (c , clen );
174176 // Derive K*
175177 Pack .longToBigEndian (x0 , tag , 0 );
@@ -378,7 +380,7 @@ public void reset()
378380 PermuteRoundsKX (SX , E , C );
379381 // Init state for mac
380382 swapInternalState ();
381- Arrays .fill (SX , 12 , 25 , (short ) 0 );
383+ Arrays .fill (SX , 12 , 25 , (short )0 );
382384 System .arraycopy (iv16 , 0 , SX , 0 , 8 );
383385 System .arraycopy (ISAP_IV1_16 , 0 , SX , 8 , 4 );
384386 PermuteRoundsHX (SX , E , C );
@@ -443,6 +445,19 @@ else if (rem_bytes == ISAP_rH_SZ)
443445 }
444446 }
445447
448+ public void absorbFinalAADBlock ()
449+ {
450+ for (int i = 0 ; i < bufferOff ; i ++)
451+ {
452+ SX [i >> 1 ] ^= (buffer [i ] & 0xFF ) << ((i & 1 ) << 3 );
453+ }
454+ SX [bufferOff >> 1 ] ^= 0x80 << ((bufferOff & 1 ) << 3 );
455+ PermuteRoundsHX (SX , E , C );
456+
457+ // Domain seperation
458+ SX [24 ] ^= 0x0100 ;
459+ }
460+
446461 public void isap_rk (short [] iv16 , byte [] y , int ylen , short [] out16 , int outlen , short [] C )
447462 {
448463 // Init state
@@ -465,10 +480,6 @@ public void isap_rk(short[] iv16, byte[] y, int ylen, short[] out16, int outlen,
465480
466481 public void isap_mac (byte [] ad , int adlen , byte [] c , int clen , byte [] tag )
467482 {
468- // Absorb AD
469- ABSORB_MAC (SX , ad , adlen , E , C );
470- // Domain seperation
471- SX [24 ] ^= 0x0100 ;
472483 // Absorb C
473484 ABSORB_MAC (SX , c , clen , E , C );
474485 // Derive K*
@@ -853,12 +864,12 @@ public void init(boolean forEncryption, CipherParameters params)
853864 @ Override
854865 public void processAADByte (byte in )
855866 {
856- if (aadOff >= aadData .length )
867+ if (bufferOff >= buffer .length )
857868 {
858- aadOff = 0 ;
859- ISAPAEAD .absorbMacBlock (aadData , 0 );
869+ bufferOff = 0 ;
870+ ISAPAEAD .absorbMacBlock (buffer , 0 );
860871 }
861- aadData [ aadOff ++] = in ;
872+ buffer [ bufferOff ++] = in ;
862873 }
863874
864875 @ Override
@@ -869,23 +880,23 @@ public void processAADBytes(byte[] in, int inOff, int len)
869880 throw new DataLengthException ("input buffer too short" + (forEncryption ? "encryption" : "decryption" ));
870881 }
871882 int tmp ;
872- if (aadOff + len >= ISAP_rH_SZ )
883+ if (bufferOff + len >= ISAP_rH_SZ )
873884 {
874- tmp = ISAP_rH_SZ - aadOff ;
875- System .arraycopy (in , inOff , aadData , aadOff , tmp );
876- ISAPAEAD .absorbMacBlock (aadData , 0 );
885+ tmp = ISAP_rH_SZ - bufferOff ;
886+ System .arraycopy (in , inOff , buffer , bufferOff , tmp );
887+ ISAPAEAD .absorbMacBlock (buffer , 0 );
877888 inOff += tmp ;
878889 len -= tmp ;
879- aadOff = 0 ;
890+ bufferOff = 0 ;
880891 }
881- while (len > ISAP_rH_SZ )
892+ while (len >= ISAP_rH_SZ )
882893 {
883894 ISAPAEAD .absorbMacBlock (in , inOff );
884895 inOff += ISAP_rH_SZ ;
885896 len -= ISAP_rH_SZ ;
886897 }
887- System .arraycopy (in , inOff , aadData , aadOff , len );
888- aadOff += len ;
898+ System .arraycopy (in , inOff , buffer , bufferOff , len );
899+ bufferOff += len ;
889900 }
890901
891902 @ Override
@@ -900,9 +911,11 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
900911 {
901912 throw new DataLengthException ("input buffer too short" );
902913 }
903- if (!aadFinished )
914+ if (!aadFinished )
904915 {
916+ ISAPAEAD .absorbFinalAADBlock ();
905917 ISAPAEAD .swapInternalState ();
918+ bufferOff = 0 ;
906919 aadFinished = true ;
907920 }
908921 message .write (input , inOff , len );
@@ -952,7 +965,7 @@ public int doFinal(byte[] output, int outOff)
952965 c = outputStream .toByteArray ();
953966 mac = new byte [MAC_SIZE ];
954967 ISAPAEAD .swapInternalState ();
955- ISAPAEAD .isap_mac (aadData , aadOff , c , c .length , mac );
968+ ISAPAEAD .isap_mac (buffer , bufferOff , c , c .length , mac );
956969 System .arraycopy (mac , 0 , output , outOff , 16 );
957970 len += 16 ;
958971 }
@@ -966,7 +979,7 @@ public int doFinal(byte[] output, int outOff)
966979 throw new OutputLengthException ("output buffer is too short" );
967980 }
968981 ISAPAEAD .swapInternalState ();
969- ISAPAEAD .isap_mac (aadData , aadOff , c , len , mac );
982+ ISAPAEAD .isap_mac (buffer , bufferOff , c , len , mac );
970983 ISAPAEAD .reset ();
971984 for (int i = 0 ; i < 16 ; ++i )
972985 {
@@ -1000,11 +1013,11 @@ protected void reset(boolean clearMac)
10001013 {
10011014 throw new IllegalArgumentException ("Need call init function before encryption/decryption" );
10021015 }
1003- Arrays .fill (aadData , (byte )0 );
1016+ Arrays .fill (buffer , (byte )0 );
10041017 ISAPAEAD .reset ();
10051018 message .reset ();
10061019 outputStream .reset ();
1007- aadOff = 0 ;
1020+ bufferOff = 0 ;
10081021 aadFinished = false ;
10091022 super .reset (clearMac );
10101023
0 commit comments