File tree Expand file tree Collapse file tree 2 files changed +29
-39
lines changed Expand file tree Collapse file tree 2 files changed +29
-39
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,6 @@ namespace Org.BouncyCastle.Crypto.Digests
17
17
public class Dstu7564Digest : IDigest , IMemoable
18
18
{
19
19
private const int ROWS = 8 ;
20
- private const int REDUCTION_POLYNOMIAL = 0x011d ;
21
20
private const int BITS_IN_BYTE = 8 ;
22
21
23
22
private const int NB_512 = 8 ; //Number of 8-byte words in state for <=256-bit hash code.
@@ -316,28 +315,24 @@ private void ShiftBytes(byte[][] state)
316
315
317
316
private static byte MultiplyGF ( byte x , byte y )
318
317
{
319
- int i ;
320
- byte r = 0 ;
321
- byte hbit = 0 ;
322
- for ( i = 0 ; i < BITS_IN_BYTE ; ++ i )
323
- {
324
- if ( ( y & 0x1 ) == 1 )
325
- {
326
- r ^= x ;
327
- }
318
+ // REDUCTION_POLYNOMIAL = 0x011d; /* x^8 + x^4 + x^3 + x^2 + 1 */
328
319
329
- hbit = ( byte ) ( x & 0x80 ) ;
320
+ uint u = x , v = y ;
321
+ uint r = u & ( 0U - ( v & 1 ) ) ;
330
322
331
- x <<= 1 ;
323
+ for ( int i = 1 ; i < BITS_IN_BYTE ; i ++ )
324
+ {
325
+ u <<= 1 ;
326
+ v >>= 1 ;
327
+ r ^= u & ( 0U - ( v & 1 ) ) ;
328
+ }
332
329
333
- if ( hbit == 0x80 )
334
- {
335
- x = ( byte ) ( ( int ) x ^ REDUCTION_POLYNOMIAL ) ;
336
- }
330
+ uint hi = r & 0xFF00U ;
331
+ r ^= hi ^ ( hi >> 4 ) ^ ( hi >> 5 ) ^ ( hi >> 6 ) ^ ( hi >> 8 ) ;
332
+ hi = r & 0x0F00U ;
333
+ r ^= hi ^ ( hi >> 4 ) ^ ( hi >> 5 ) ^ ( hi >> 6 ) ^ ( hi >> 8 ) ;
337
334
338
- y >>= 1 ;
339
- }
340
- return r ;
335
+ return ( byte ) r ;
341
336
}
342
337
343
338
private void MixColumns ( byte [ ] [ ] state )
Original file line number Diff line number Diff line change @@ -16,8 +16,6 @@ public class Dstu7624Engine
16
16
private static readonly int BITS_IN_WORD = 64 ;
17
17
private static readonly int BITS_IN_BYTE = 8 ;
18
18
19
- private static readonly int REDUCTION_POLYNOMIAL = 0x011d ; /* x^8 + x^4 + x^3 + x^2 + 1 */
20
-
21
19
private ulong [ ] internalState ;
22
20
private ulong [ ] workingKey ;
23
21
private ulong [ ] [ ] roundKeys ;
@@ -495,29 +493,26 @@ private void MatrixMultiply(byte[][] matrix)
495
493
}
496
494
}
497
495
498
- private byte MultiplyGF ( byte x , byte y )
496
+ private static byte MultiplyGF ( byte x , byte y )
499
497
{
500
- byte r = 0 ;
501
- byte hbit = 0 ;
498
+ // REDUCTION_POLYNOMIAL = 0x011d; /* x^8 + x^4 + x^3 + x^2 + 1 */
502
499
503
- for ( int i = 0 ; i < BITS_IN_BYTE ; i ++ )
504
- {
505
- if ( ( y & 0x01 ) == 1 )
506
- {
507
- r ^= x ;
508
- }
500
+ uint u = x , v = y ;
501
+ uint r = u & ( 0U - ( v & 1 ) ) ;
509
502
510
- hbit = ( byte ) ( x & 0x80 ) ;
503
+ for ( int i = 1 ; i < BITS_IN_BYTE ; i ++ )
504
+ {
505
+ u <<= 1 ;
506
+ v >>= 1 ;
507
+ r ^= u & ( 0U - ( v & 1 ) ) ;
508
+ }
511
509
512
- x <<= 1 ;
510
+ uint hi = r & 0xFF00U ;
511
+ r ^= hi ^ ( hi >> 4 ) ^ ( hi >> 5 ) ^ ( hi >> 6 ) ^ ( hi >> 8 ) ;
512
+ hi = r & 0x0F00U ;
513
+ r ^= hi ^ ( hi >> 4 ) ^ ( hi >> 5 ) ^ ( hi >> 6 ) ^ ( hi >> 8 ) ;
513
514
514
- if ( hbit == 0x80 )
515
- {
516
- x = ( byte ) ( ( int ) x ^ REDUCTION_POLYNOMIAL ) ;
517
- }
518
- y >>= 1 ;
519
- }
520
- return r ;
515
+ return ( byte ) r ;
521
516
}
522
517
523
518
private void SubBytes ( )
You can’t perform that action at this time.
0 commit comments