@@ -51,7 +51,7 @@ public TwofishCipher(byte[] key, CipherMode mode, CipherPadding padding)
51
51
gMDS3 [ i ] = mX [ P_30 ] | m1 [ P_30 ] << 8 | mY [ P_30 ] << 16 | mX [ P_30 ] << 24 ;
52
52
}
53
53
54
- k64Cnt = key . Length / 8 ; // pre-padded ?
54
+ _k64Cnt = key . Length / 8 ; // pre-padded ?
55
55
SetKey ( key ) ;
56
56
}
57
57
@@ -68,17 +68,16 @@ public TwofishCipher(byte[] key, CipherMode mode, CipherPadding padding)
68
68
/// </returns>
69
69
public override int EncryptBlock ( byte [ ] inputBuffer , int inputOffset , int inputCount , byte [ ] outputBuffer , int outputOffset )
70
70
{
71
- int x0 = BytesTo32Bits ( inputBuffer , inputOffset ) ^ gSubKeys [ INPUT_WHITEN ] ;
72
- int x1 = BytesTo32Bits ( inputBuffer , inputOffset + 4 ) ^ gSubKeys [ INPUT_WHITEN + 1 ] ;
73
- int x2 = BytesTo32Bits ( inputBuffer , inputOffset + 8 ) ^ gSubKeys [ INPUT_WHITEN + 2 ] ;
74
- int x3 = BytesTo32Bits ( inputBuffer , inputOffset + 12 ) ^ gSubKeys [ INPUT_WHITEN + 3 ] ;
75
-
76
- int k = ROUND_SUBKEYS ;
77
- int t0 , t1 ;
78
- for ( int r = 0 ; r < ROUNDS ; r += 2 )
71
+ var x0 = BytesTo32Bits ( inputBuffer , inputOffset ) ^ gSubKeys [ INPUT_WHITEN ] ;
72
+ var x1 = BytesTo32Bits ( inputBuffer , inputOffset + 4 ) ^ gSubKeys [ INPUT_WHITEN + 1 ] ;
73
+ var x2 = BytesTo32Bits ( inputBuffer , inputOffset + 8 ) ^ gSubKeys [ INPUT_WHITEN + 2 ] ;
74
+ var x3 = BytesTo32Bits ( inputBuffer , inputOffset + 12 ) ^ gSubKeys [ INPUT_WHITEN + 3 ] ;
75
+
76
+ var k = ROUND_SUBKEYS ;
77
+ for ( var r = 0 ; r < ROUNDS ; r += 2 )
79
78
{
80
- t0 = Fe32_0 ( gSBox , x0 ) ;
81
- t1 = Fe32_3 ( gSBox , x1 ) ;
79
+ var t0 = Fe32_0 ( gSBox , x0 ) ;
80
+ var t1 = Fe32_3 ( gSBox , x1 ) ;
82
81
x2 ^= t0 + t1 + gSubKeys [ k ++ ] ;
83
82
x2 = ( int ) ( ( uint ) x2 >> 1 ) | x2 << 31 ;
84
83
x3 = ( x3 << 1 | ( int ) ( ( uint ) x3 >> 31 ) ) ^ ( t0 + 2 * t1 + gSubKeys [ k ++ ] ) ;
@@ -345,7 +344,7 @@ public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputC
345
344
private int [ ] gSubKeys ;
346
345
private int [ ] gSBox ;
347
346
348
- private int k64Cnt ;
347
+ private readonly int _k64Cnt ;
349
348
350
349
private void SetKey ( byte [ ] key )
351
350
{
@@ -355,12 +354,12 @@ private void SetKey(byte[] key)
355
354
var sBoxKeys = new int [ MAX_KEY_BITS / 64 ] ; // 4
356
355
gSubKeys = new int [ TOTAL_SUBKEYS ] ;
357
356
358
- if ( k64Cnt < 1 )
357
+ if ( _k64Cnt < 1 )
359
358
{
360
359
throw new ArgumentException ( "Key size less than 64 bits" ) ;
361
360
}
362
361
363
- if ( k64Cnt > 4 )
362
+ if ( _k64Cnt > 4 )
364
363
{
365
364
throw new ArgumentException ( "Key size larger than 256 bits" ) ;
366
365
}
@@ -371,26 +370,26 @@ private void SetKey(byte[] key)
371
370
* maximum of 32 bytes ( 256 bits ), so the range
372
371
* for k64Cnt is 1..4
373
372
*/
374
- for ( int i = 0 ; i < k64Cnt ; i ++ )
373
+ for ( var i = 0 ; i < _k64Cnt ; i ++ )
375
374
{
376
375
var p = i * 8 ;
377
376
378
377
k32e [ i ] = BytesTo32Bits ( key , p ) ;
379
378
k32o [ i ] = BytesTo32Bits ( key , p + 4 ) ;
380
379
381
- sBoxKeys [ k64Cnt - 1 - i ] = RS_MDS_Encode ( k32e [ i ] , k32o [ i ] ) ;
380
+ sBoxKeys [ _k64Cnt - 1 - i ] = RS_MDS_Encode ( k32e [ i ] , k32o [ i ] ) ;
382
381
}
383
382
384
- for ( int i = 0 ; i < TOTAL_SUBKEYS / 2 ; i ++ )
383
+ for ( var i = 0 ; i < TOTAL_SUBKEYS / 2 ; i ++ )
385
384
{
386
385
var q = i * SK_STEP ;
387
- var A = F32 ( q , k32e ) ;
388
- var B = F32 ( q + SK_BUMP , k32o ) ;
389
- B = B << 8 | ( int ) ( ( uint ) B >> 24 ) ;
390
- A += B ;
391
- gSubKeys [ i * 2 ] = A ;
392
- A += B ;
393
- gSubKeys [ i * 2 + 1 ] = A << SK_ROTL | ( int ) ( ( uint ) A >> ( 32 - SK_ROTL ) ) ;
386
+ var a = F32 ( q , k32e ) ;
387
+ var b = F32 ( q + SK_BUMP , k32o ) ;
388
+ b = b << 8 | ( int ) ( ( uint ) b >> 24 ) ;
389
+ a += b ;
390
+ gSubKeys [ i * 2 ] = a ;
391
+ a += b ;
392
+ gSubKeys [ i * 2 + 1 ] = a << SK_ROTL | ( int ) ( ( uint ) a >> ( 32 - SK_ROTL ) ) ;
394
393
}
395
394
396
395
/*
@@ -405,7 +404,7 @@ private void SetKey(byte[] key)
405
404
{
406
405
int b1 , b2 , b3 ;
407
406
var b0 = b1 = b2 = b3 = i ;
408
- switch ( k64Cnt & 3 )
407
+ switch ( _k64Cnt & 3 )
409
408
{
410
409
case 1 :
411
410
gSBox [ i * 2 ] = gMDS0 [ ( P [ P_01 * 256 + b0 ] & 0xff ) ^ M_b0 ( k0 ) ] ;
@@ -447,17 +446,17 @@ private void SetKey(byte[] key)
447
446
*/
448
447
private int F32 ( int x , int [ ] k32 )
449
448
{
450
- int b0 = M_b0 ( x ) ;
451
- int b1 = M_b1 ( x ) ;
452
- int b2 = M_b2 ( x ) ;
453
- int b3 = M_b3 ( x ) ;
454
- int k0 = k32 [ 0 ] ;
455
- int k1 = k32 [ 1 ] ;
456
- int k2 = k32 [ 2 ] ;
457
- int k3 = k32 [ 3 ] ;
458
-
459
- int result = 0 ;
460
- switch ( k64Cnt & 3 )
449
+ var b0 = M_b0 ( x ) ;
450
+ var b1 = M_b1 ( x ) ;
451
+ var b2 = M_b2 ( x ) ;
452
+ var b3 = M_b3 ( x ) ;
453
+ var k0 = k32 [ 0 ] ;
454
+ var k1 = k32 [ 1 ] ;
455
+ var k2 = k32 [ 2 ] ;
456
+ var k3 = k32 [ 3 ] ;
457
+
458
+ var result = 0 ;
459
+ switch ( _k64Cnt & 3 )
461
460
{
462
461
case 1 :
463
462
result = gMDS0 [ ( P [ P_01 * 256 + b0 ] & 0xff ) ^ M_b0 ( k0 ) ] ^
@@ -499,7 +498,7 @@ private int F32(int x, int[] k32)
499
498
*/
500
499
private static int RS_MDS_Encode ( int k0 , int k1 )
501
500
{
502
- int r = k1 ;
501
+ var r = k1 ;
503
502
// shift 1 byte at a time
504
503
r = RS_rem ( r ) ;
505
504
r = RS_rem ( r ) ;
@@ -525,10 +524,10 @@ private static int RS_MDS_Encode(int k0, int k1)
525
524
*/
526
525
private static int RS_rem ( int x )
527
526
{
528
- int b = ( int ) ( ( ( uint ) x >> 24 ) & 0xff ) ;
529
- int g2 = ( ( b << 1 ) ^
527
+ var b = ( int ) ( ( ( uint ) x >> 24 ) & 0xff ) ;
528
+ var g2 = ( ( b << 1 ) ^
530
529
( ( b & 0x80 ) != 0 ? RS_GF_FDBK : 0 ) ) & 0xff ;
531
- int g3 = ( ( int ) ( ( uint ) b >> 1 ) ^
530
+ var g3 = ( ( int ) ( ( uint ) b >> 1 ) ^
532
531
( ( b & 0x01 ) != 0 ? ( int ) ( ( uint ) RS_GF_FDBK >> 1 ) : 0 ) ) ^ g2 ;
533
532
return ( ( x << 8 ) ^ ( g3 << 24 ) ^ ( g2 << 16 ) ^ ( g3 << 8 ) ^ b ) ;
534
533
}
0 commit comments