@@ -116,36 +116,32 @@ private void giftb128(byte[] P, byte[] K, byte[] C)
116116 C [15 ] = (byte )(S [3 ]);
117117 }
118118
119- private void xor_topbar_block (byte [] d , byte [] s1 , byte [] s2 )
119+ private void double_half_block (byte [] s )
120120 {
121- Bytes .xor (8 , s1 , s2 , d );
122- System .arraycopy (s1 , 8 , d , 8 , 8 );
121+ int mask = ((s [0 ] & 0xFF ) >>> 7 ) * 27 ;
122+ /*x^{64} + x^4 + x^3 + x + 1*/
123+ for (int i = 0 ; i < 7 ; i ++)
124+ {
125+ s [i ] = (byte )(((s [i ] & 0xFF ) << 1 ) | ((s [i + 1 ] & 0xFF ) >>> 7 ));
126+ }
127+ s [7 ] = (byte )(((s [7 ] & 0xFF ) << 1 ) ^ mask );
123128 }
124129
125- private void double_half_block ( byte [] d , byte [] s )
130+ private void triple_half_block ( byte [] s )
126131 {
127- int i ;
128132 byte [] tmp = new byte [8 ];
129133 /*x^{64} + x^4 + x^3 + x + 1*/
130- for (i = 0 ; i < 7 ; i ++)
134+ for (int i = 0 ; i < 7 ; i ++)
131135 {
132136 tmp [i ] = (byte )(((s [i ] & 0xFF ) << 1 ) | ((s [i + 1 ] & 0xFF ) >>> 7 ));
133137 }
134138 tmp [7 ] = (byte )(((s [7 ] & 0xFF ) << 1 ) ^ (((s [0 ] & 0xFF ) >>> 7 ) * 27 ));
135- System .arraycopy (tmp , 0 , d , 0 , 8 );
136- }
137-
138- private void triple_half_block (byte [] d , byte [] s )
139- {
140- byte [] tmp = new byte [8 ];
141- double_half_block (tmp , s );
142- Bytes .xor (8 , s , tmp , d );
139+ Bytes .xorTo (8 , tmp , s );
143140 }
144141
145142 private void pho1 (byte [] d , byte [] Y , byte [] M , int mOff , int no_of_bytes )
146143 {
147144 byte [] tmpM = new byte [16 ];
148- //padding(tmpM, M, mOff, no_of_bytes);
149145 byte [] tmp = new byte [16 ];
150146 if (no_of_bytes == 0 )
151147 {
@@ -160,11 +156,10 @@ else if (no_of_bytes < 16)
160156 {
161157 System .arraycopy (M , mOff , tmpM , 0 , no_of_bytes );
162158 }
163- int i ;
164159 //G(Y, Y);
165160 /*Y[1],Y[2] -> Y[2],Y[1]<<<1*/
166161 System .arraycopy (Y , 8 , tmp , 0 , 8 );
167- for (i = 0 ; i < 7 ; i ++)
162+ for (int i = 0 ; i < 7 ; i ++)
168163 {
169164 tmp [i + 8 ] = (byte )((Y [i ] & 0xFF ) << 1 | (Y [i + 1 ] & 0xFF ) >>> 7 );
170165 }
@@ -173,25 +168,13 @@ else if (no_of_bytes < 16)
173168 Bytes .xor (16 , Y , tmpM , d );
174169 }
175170
176- private void pho (byte [] Y , byte [] M , int mOff , byte [] X , byte [] C , int cOff , int no_of_bytes )
177- {
178- Bytes .xor (no_of_bytes , Y , M , mOff , C , cOff );
179- pho1 (X , Y , M , mOff , no_of_bytes );
180- }
181-
182- private void phoprime (byte [] Y , byte [] C , int cOff , byte [] X , byte [] M , int mOff , int no_of_bytes )
183- {
184- Bytes .xor (no_of_bytes , Y , C , cOff , M , mOff );
185- pho1 (X , Y , M , mOff , no_of_bytes );
186- }
187-
188171 @ Override
189172 protected void processBufferAAD (byte [] in , int inOff )
190173 {
191174 pho1 (input , Y , in , inOff , 16 );
192175 /* offset = 2*offset */
193- double_half_block (offset , offset );
194- xor_topbar_block ( input , input , offset );
176+ double_half_block (offset );
177+ Bytes . xorTo ( 8 , offset , input );
195178 /* Y[i] = E(X[i]) */
196179 giftb128 (input , k , Y );
197180 }
@@ -203,20 +186,20 @@ protected void processFinalAAD()
203186 /* last byte[] */
204187 /* full byte[]: offset = 3*offset */
205188 /* partial byte[]: offset = 3^2*offset */
206- triple_half_block (offset , offset );
189+ triple_half_block (offset );
207190 if (((m_aadPos & 15 ) != 0 ) || m_state == State .DecInit || m_state == State .EncInit )
208191 {
209- triple_half_block (offset , offset );
192+ triple_half_block (offset );
210193 }
211194 if (len == 0 )
212195 {
213196 /* empty M: offset = 3^2*offset */
214- triple_half_block (offset , offset );
215- triple_half_block (offset , offset );
197+ triple_half_block (offset );
198+ triple_half_block (offset );
216199 }
217200 /* X[i] = (pad(A[i]) + G(Y[i-1])) + offset */
218201 pho1 (input , Y , m_aad , 0 , m_aadPos );
219- xor_topbar_block ( input , input , offset );
202+ Bytes . xorTo ( 8 , offset , input );
220203 /* Y[a] = E(X[a]) */
221204 giftb128 (input , k , Y );
222205 }
@@ -255,50 +238,49 @@ protected void init(byte[] key, byte[] iv)
255238 reset (false );
256239 }
257240
258-
259241 @ Override
260242 protected void processFinalBlock (byte [] output , int outOff )
261243 {
262- int inOff = 0 ;
263244 int len = dataOperator .getLen () - (forEncryption ? 0 : MAC_SIZE );
264245 if (len != 0 )
265246 {
266247 /* full block: offset = 3*offset */
267248 /* empty data / partial block: offset = 3^2*offset */
268- triple_half_block (offset , offset );
249+ triple_half_block (offset );
269250 if ((len & 15 ) != 0 )
270251 {
271- triple_half_block (offset , offset );
252+ triple_half_block (offset );
272253 }
273254 /* last block */
274255 /* C[m] = Y[m+a-1] + M[m]*/
275256 /* X[a+m] = M[m] + G(Y[m+a-1]) + offset */
257+ Bytes .xor (m_bufPos , Y , m_buf , 0 , output , outOff );
276258 if (forEncryption )
277259 {
278- pho ( Y , m_buf , inOff , input , output , outOff , m_bufPos );
260+ pho1 ( input , Y , m_buf , 0 , m_bufPos );
279261 }
280262 else
281263 {
282- phoprime ( Y , m_buf , inOff , input , output , outOff , m_bufPos );
264+ pho1 ( input , Y , output , outOff , m_bufPos );
283265 }
284- xor_topbar_block ( input , input , offset );
266+ Bytes . xorTo ( 8 , offset , input );
285267 /* T = E(X[m+a]) */
286268 giftb128 (input , k , Y );
287269 }
288270 System .arraycopy (Y , 0 , mac , 0 , BlockSize );
289271 }
290272
291-
292273 @ Override
293274 protected void processBufferEncrypt (byte [] inputM , int inOff , byte [] output , int outOff )
294275 {
295276 /* Process M */
296277 /* full byte[]s */
297- double_half_block (offset , offset );
278+ double_half_block (offset );
298279 /* C[i] = Y[i+a-1] + M[i]*/
299280 /* X[i] = M[i] + G(Y[i+a-1]) + offset */
300- pho (Y , inputM , inOff , input , output , outOff , BlockSize );
301- xor_topbar_block (input , input , offset );
281+ Bytes .xor (BlockSize , Y , inputM , inOff , output , outOff );
282+ pho1 (input , Y , inputM , inOff , BlockSize );
283+ Bytes .xorTo (8 , offset , input );
302284 /* Y[i] = E(X[i+a]) */
303285 giftb128 (input , k , Y );
304286 }
@@ -308,11 +290,12 @@ protected void processBufferDecrypt(byte[] inputM, int inOff, byte[] output, int
308290 {
309291 /* Process M */
310292 /* full byte[]s */
311- double_half_block (offset , offset );
293+ double_half_block (offset );
312294 /* C[i] = Y[i+a-1] + M[i]*/
313295 /* X[i] = M[i] + G(Y[i+a-1]) + offset */
314- phoprime (Y , inputM , inOff , input , output , outOff , BlockSize );
315- xor_topbar_block (input , input , offset );
296+ Bytes .xor (BlockSize , Y , inputM , inOff , output , outOff );
297+ pho1 (input , Y , output , outOff , BlockSize );
298+ Bytes .xorTo (8 , offset , input );
316299 /* Y[i] = E(X[i+a]) */
317300 giftb128 (input , k , Y );
318301 }
0 commit comments