11package org .bouncycastle .crypto .engines ;
22
33import org .bouncycastle .util .Arrays ;
4+ import org .bouncycastle .util .Bytes ;
45import org .bouncycastle .util .Longs ;
56import org .bouncycastle .util .Pack ;
67
@@ -69,8 +70,6 @@ private interface ISAP_AEAD
6970
7071 void absorbFinalAADBlock ();
7172
72- void swapInternalState ();
73-
7473 void processEncBlock (byte [] input , int inOff , byte [] output , int outOff );
7574
7675 void processEncFinalBlock (byte [] output , int outOff );
@@ -87,13 +86,14 @@ private abstract class ISAPAEAD_A
8786 protected long ISAP_IV2_64 ;
8887 protected long ISAP_IV3_64 ;
8988 AsconPermutationFriend .AsconPermutation p ;
90- protected long t0 , t1 , t2 , t3 , t4 , macx0 , macx1 , macx2 , macx3 , macx4 ;
89+ AsconPermutationFriend . AsconPermutation mac ;
9190
9291 public ISAPAEAD_A ()
9392 {
9493 ISAP_rH = 64 ;
9594 BlockSize = (ISAP_rH + 7 ) >> 3 ;
9695 p = new AsconPermutationFriend .AsconPermutation ();
96+ mac = new AsconPermutationFriend .AsconPermutation ();
9797 }
9898
9999 public void init ()
@@ -102,72 +102,52 @@ public void init()
102102 k64 = new long [getLongSize (k .length )];
103103 Pack .bigEndianToLong (npub , 0 , npub64 );
104104 Pack .bigEndianToLong (k , 0 , k64 );
105- //reset();
106105 }
107106
108- protected abstract void PX1 ();
107+ protected abstract void PX1 (AsconPermutationFriend . AsconPermutation p );
109108
110- protected abstract void PX2 ();
111-
112- public void swapInternalState ()
113- {
114- t0 = p .x0 ;
115- t1 = p .x1 ;
116- t2 = p .x2 ;
117- t3 = p .x3 ;
118- t4 = p .x4 ;
119- p .x0 = macx0 ;
120- p .x1 = macx1 ;
121- p .x2 = macx2 ;
122- p .x3 = macx3 ;
123- p .x4 = macx4 ;
124- macx0 = t0 ;
125- macx1 = t1 ;
126- macx2 = t2 ;
127- macx3 = t3 ;
128- macx4 = t4 ;
129- }
109+ protected abstract void PX2 (AsconPermutationFriend .AsconPermutation p );
130110
131111 public void absorbMacBlock (byte [] input , int inOff )
132112 {
133- p .x0 ^= Pack .bigEndianToLong (input , inOff );
134- p .p (12 );
113+ mac .x0 ^= Pack .bigEndianToLong (input , inOff );
114+ mac .p (12 );
135115 }
136116
137117 public void absorbFinalAADBlock ()
138118 {
139119 for (int i = 0 ; i < m_aadPos ; ++i )
140120 {
141- p .x0 ^= (m_aad [i ] & 0xFFL ) << ((7 - i ) << 3 );
121+ mac .x0 ^= (m_aad [i ] & 0xFFL ) << ((7 - i ) << 3 );
142122 }
143- p .x0 ^= 0x80L << ((7 - m_aadPos ) << 3 );
144- p .p (12 );
145- p .x4 ^= 1L ;
123+ mac .x0 ^= 0x80L << ((7 - m_aadPos ) << 3 );
124+ mac .p (12 );
125+ mac .x4 ^= 1L ;
146126 }
147127
148128 public void processMACFinal (byte [] input , int inOff , int len , byte [] tag )
149129 {
150130 for (int i = 0 ; i < len ; ++i )
151131 {
152- p .x0 ^= (input [inOff ++] & 0xFFL ) << ((7 - i ) << 3 );
132+ mac .x0 ^= (input [inOff ++] & 0xFFL ) << ((7 - i ) << 3 );
153133 }
154- p .x0 ^= 0x80L << ((7 - len ) << 3 );
155- p .p (12 );
134+ mac .x0 ^= 0x80L << ((7 - len ) << 3 );
135+ mac .p (12 );
156136 // Derive K*
157- Pack .longToBigEndian (p .x0 , tag , 0 );
158- Pack .longToBigEndian (p .x1 , tag , 8 );
159- long tmp_x2 = p .x2 , tmp_x3 = p .x3 , tmp_x4 = p .x4 ;
160- isap_rk (ISAP_IV2_64 , tag , KEY_SIZE );
161- p .x2 = tmp_x2 ;
162- p .x3 = tmp_x3 ;
163- p .x4 = tmp_x4 ;
137+ Pack .longToBigEndian (mac .x0 , tag , 0 );
138+ Pack .longToBigEndian (mac .x1 , tag , 8 );
139+ long tmp_x2 = mac .x2 , tmp_x3 = mac .x3 , tmp_x4 = mac .x4 ;
140+ isap_rk (mac , ISAP_IV2_64 , tag , KEY_SIZE );
141+ mac .x2 = tmp_x2 ;
142+ mac .x3 = tmp_x3 ;
143+ mac .x4 = tmp_x4 ;
164144 // Squeeze tag
165- p .p (12 );
166- Pack .longToBigEndian (p .x0 , tag , 0 );
167- Pack .longToBigEndian (p .x1 , tag , 8 );
145+ mac .p (12 );
146+ Pack .longToBigEndian (mac .x0 , tag , 0 );
147+ Pack .longToBigEndian (mac .x1 , tag , 8 );
168148 }
169149
170- public void isap_rk (long iv64 , byte [] y , int ylen )
150+ private void isap_rk (AsconPermutationFriend . AsconPermutation p , long iv64 , byte [] y , int ylen )
171151 {
172152 // Init state
173153 p .x0 = k64 [0 ];
@@ -179,7 +159,7 @@ public void isap_rk(long iv64, byte[] y, int ylen)
179159 for (int i = 0 ; i < (ylen << 3 ) - 1 ; i ++)
180160 {
181161 p .x0 ^= ((((y [i >>> 3 ] >>> (7 - (i & 7 ))) & 0x01 ) << 7 ) & 0xFFL ) << 56 ;
182- PX2 ();
162+ PX2 (p );
183163 }
184164 p .x0 ^= (((y [ylen - 1 ]) & 0x01L ) << 7 ) << 56 ;
185165 p .p (12 );
@@ -189,7 +169,7 @@ public void processEncBlock(byte[] input, int inOff, byte[] output, int outOff)
189169 {
190170 long m64 = Pack .littleEndianToLong (input , inOff );
191171 long c64 = U64BIG (p .x0 ) ^ m64 ;
192- PX1 ();
172+ PX1 (p );
193173 Pack .longToLittleEndian (c64 , output , outOff );
194174 }
195175
@@ -198,26 +178,22 @@ public void processEncFinalBlock(byte[] output, int outOff)
198178 /* Encrypt final m block */
199179 byte [] xo = Pack .longToLittleEndian (p .x0 );
200180 int mlen = m_bufPos ;
201- while (mlen > 0 )
202- {
203- output [outOff + mlen - 1 ] = (byte )(xo [BlockSize - mlen ] ^ m_buf [--mlen ]);
204- }
181+ Bytes .xor (mlen , xo , BlockSize - mlen , m_buf , 0 , output , outOff );
205182 }
206183
207184 public void reset ()
208185 {
209186 // Init state
210- isap_rk (ISAP_IV3_64 , npub , IV_SIZE );
187+ isap_rk (p , ISAP_IV3_64 , npub , IV_SIZE );
211188 p .x3 = npub64 [0 ];
212189 p .x4 = npub64 [1 ];
213- PX1 ();
214- swapInternalState ();
190+ PX1 (p );
215191 // Init State for mac
216- p .x0 = npub64 [0 ];
217- p .x1 = npub64 [1 ];
218- p .x2 = ISAP_IV1_64 ;
219- p .x3 = p .x4 = 0 ;
220- p .p (12 );
192+ mac .x0 = npub64 [0 ];
193+ mac .x1 = npub64 [1 ];
194+ mac .x2 = ISAP_IV1_64 ;
195+ mac .x3 = mac .x4 = 0 ;
196+ mac .p (12 );
221197 }
222198
223199 private int getLongSize (int x )
@@ -242,12 +218,12 @@ public ISAPAEAD_A_128A()
242218 ISAP_IV3_64 = 252271952373286412L ;
243219 }
244220
245- protected void PX1 ()
221+ protected void PX1 (AsconPermutationFriend . AsconPermutation p )
246222 {
247223 p .p (6 );
248224 }
249225
250- protected void PX2 ()
226+ protected void PX2 (AsconPermutationFriend . AsconPermutation p )
251227 {
252228 p .round (0x4bL );
253229 }
@@ -263,12 +239,12 @@ public ISAPAEAD_A_128()
263239 ISAP_IV3_64 = 252271952374008844L ;
264240 }
265241
266- protected void PX1 ()
242+ protected void PX1 (AsconPermutationFriend . AsconPermutation p )
267243 {
268244 p .p (12 );
269245 }
270246
271- protected void PX2 ()
247+ protected void PX2 (AsconPermutationFriend . AsconPermutation p )
272248 {
273249 p .p (12 );
274250 }
@@ -315,24 +291,10 @@ public void reset()
315291 System .arraycopy (iv16 , 0 , SX , 17 , 8 );
316292 PermuteRoundsKX (SX , E , C );
317293 // Init state for mac
318- swapInternalState ();
319- Arrays .fill (SX , 12 , 25 , (short )0 );
320- System .arraycopy (iv16 , 0 , SX , 0 , 8 );
321- System .arraycopy (ISAP_IV1_16 , 0 , SX , 8 , 4 );
322- PermuteRoundsHX (SX , E , C );
323- }
324-
325- public void swapInternalState ()
326- {
327- short [] tmp = SX ;
328- SX = macSX ;
329- macSX = tmp ;
330- tmp = E ;
331- E = macE ;
332- macE = tmp ;
333- tmp = C ;
334- C = macC ;
335- macC = tmp ;
294+ Arrays .fill (macSX , 12 , 25 , (short )0 );
295+ System .arraycopy (iv16 , 0 , macSX , 0 , 8 );
296+ System .arraycopy (ISAP_IV1_16 , 0 , macSX , 8 , 4 );
297+ PermuteRoundsHX (macSX , macE , macC );
336298 }
337299
338300 protected abstract void PermuteRoundsHX (short [] SX , short [] E , short [] C );
@@ -343,21 +305,21 @@ public void swapInternalState()
343305
344306 public void absorbMacBlock (byte [] input , int inOff )
345307 {
346- byteToShortXor (input , inOff , SX , BlockSize >> 1 );
347- PermuteRoundsHX (SX , E , C );
308+ byteToShortXor (input , inOff , macSX , BlockSize >> 1 );
309+ PermuteRoundsHX (macSX , macE , macC );
348310 }
349311
350312 public void absorbFinalAADBlock ()
351313 {
352314 for (int i = 0 ; i < m_aadPos ; i ++)
353315 {
354- SX [i >> 1 ] ^= (m_aad [i ] & 0xFF ) << ((i & 1 ) << 3 );
316+ macSX [i >> 1 ] ^= (m_aad [i ] & 0xFF ) << ((i & 1 ) << 3 );
355317 }
356- SX [m_aadPos >> 1 ] ^= 0x80 << ((m_aadPos & 1 ) << 3 );
357- PermuteRoundsHX (SX , E , C );
318+ macSX [m_aadPos >> 1 ] ^= 0x80 << ((m_aadPos & 1 ) << 3 );
319+ PermuteRoundsHX (macSX , macE , macC );
358320
359321 // Domain seperation
360- SX [24 ] ^= 0x0100 ;
322+ macSX [24 ] ^= 0x0100 ;
361323 }
362324
363325 public void isap_rk (short [] iv16 , byte [] y , int ylen , short [] out16 , int outlen , short [] C )
@@ -385,17 +347,17 @@ public void processMACFinal(byte[] input, int inOff, int len, byte[] tag)
385347 // Absorb C final block
386348 for (int i = 0 ; i < len ; i ++)
387349 {
388- SX [i >> 1 ] ^= (input [inOff ++] & 0xFF ) << ((i & 1 ) << 3 );
350+ macSX [i >> 1 ] ^= (input [inOff ++] & 0xFF ) << ((i & 1 ) << 3 );
389351 }
390352
391- SX [len >> 1 ] ^= 0x80 << ((len & 1 ) << 3 );
392- PermuteRoundsHX (SX , E , C );
353+ macSX [len >> 1 ] ^= 0x80 << ((len & 1 ) << 3 );
354+ PermuteRoundsHX (macSX , macE , macC );
393355 // Derive K*
394- Pack .shortToLittleEndian (SX , 0 , 8 , tag , 0 );
395- isap_rk (ISAP_IV2_16 , tag , KEY_SIZE , SX , KEY_SIZE , C );
356+ Pack .shortToLittleEndian (macSX , 0 , 8 , tag , 0 );
357+ isap_rk (ISAP_IV2_16 , tag , KEY_SIZE , macSX , KEY_SIZE , macC );
396358 // Squeeze tag
397- PermuteRoundsHX (SX , E , C );
398- Pack .shortToLittleEndian (SX , 0 , 8 , tag , 0 );
359+ PermuteRoundsHX (macSX , macE , macC );
360+ Pack .shortToLittleEndian (macSX , 0 , 8 , tag , 0 );
399361 }
400362
401363 public void processEncBlock (byte [] input , int inOff , byte [] output , int outOff )
@@ -755,7 +717,6 @@ protected void processFinalAAD()
755717 if (!aadFinished )
756718 {
757719 ISAPAEAD .absorbFinalAADBlock ();
758- ISAPAEAD .swapInternalState ();
759720 m_aadPos = 0 ;
760721 aadFinished = true ;
761722 }
@@ -765,18 +726,14 @@ protected void processBufferEncrypt(byte[] input, int inOff, byte[] output, int
765726 {
766727 processFinalAAD ();
767728 ISAPAEAD .processEncBlock (input , inOff , output , outOff );
768- ISAPAEAD .swapInternalState ();
769729 ISAPAEAD .absorbMacBlock (output , outOff );
770- ISAPAEAD .swapInternalState ();
771730 }
772731
773732 protected void processBufferDecrypt (byte [] input , int inOff , byte [] output , int outOff )
774733 {
775734 processFinalAAD ();
776735 ISAPAEAD .processEncBlock (input , inOff , output , outOff );
777- ISAPAEAD .swapInternalState ();
778736 ISAPAEAD .absorbMacBlock (input , inOff );
779- ISAPAEAD .swapInternalState ();
780737 }
781738
782739 @ Override
@@ -785,7 +742,6 @@ protected void processFinalBlock(byte[] output, int outOff)
785742 processFinalAAD ();
786743 int len = m_bufPos ;
787744 ISAPAEAD .processEncFinalBlock (output , outOff );
788- ISAPAEAD .swapInternalState ();
789745 if (forEncryption )
790746 {
791747 ISAPAEAD .processMACFinal (output , outOff , len , mac );
0 commit comments