Skip to content

Commit 6cc4a16

Browse files
committed
Constant-time GF multiplication
1 parent 50d6d0e commit 6cc4a16

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-39
lines changed

crypto/src/crypto/digests/DSTU7564Digest.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace Org.BouncyCastle.Crypto.Digests
1717
public class Dstu7564Digest : IDigest, IMemoable
1818
{
1919
private const int ROWS = 8;
20-
private const int REDUCTION_POLYNOMIAL = 0x011d;
2120
private const int BITS_IN_BYTE = 8;
2221

2322
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)
316315

317316
private static byte MultiplyGF(byte x, byte y)
318317
{
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 */
328319

329-
hbit = (byte)(x & 0x80);
320+
uint u = x, v = y;
321+
uint r = u & (0U - (v & 1));
330322

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+
}
332329

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);
337334

338-
y >>= 1;
339-
}
340-
return r;
335+
return (byte)r;
341336
}
342337

343338
private void MixColumns(byte[][] state)

crypto/src/crypto/engines/Dstu7624Engine.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public class Dstu7624Engine
1616
private static readonly int BITS_IN_WORD = 64;
1717
private static readonly int BITS_IN_BYTE = 8;
1818

19-
private static readonly int REDUCTION_POLYNOMIAL = 0x011d; /* x^8 + x^4 + x^3 + x^2 + 1 */
20-
2119
private ulong[] internalState;
2220
private ulong[] workingKey;
2321
private ulong[][] roundKeys;
@@ -495,29 +493,26 @@ private void MatrixMultiply(byte[][] matrix)
495493
}
496494
}
497495

498-
private byte MultiplyGF(byte x, byte y)
496+
private static byte MultiplyGF(byte x, byte y)
499497
{
500-
byte r = 0;
501-
byte hbit = 0;
498+
// REDUCTION_POLYNOMIAL = 0x011d; /* x^8 + x^4 + x^3 + x^2 + 1 */
502499

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));
509502

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+
}
511509

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);
513514

514-
if (hbit == 0x80)
515-
{
516-
x = (byte)((int)x ^ REDUCTION_POLYNOMIAL);
517-
}
518-
y >>= 1;
519-
}
520-
return r;
515+
return (byte)r;
521516
}
522517

523518
private void SubBytes()

0 commit comments

Comments
 (0)