From 240e9d212a05c11f38b12184fd2a504e6061e651 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 3 Nov 2015 16:45:48 +0700 Subject: Perform counter increment without branches --- crypto/src/crypto/modes/GCMBlockCipher.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/crypto/modes/GCMBlockCipher.cs b/crypto/src/crypto/modes/GCMBlockCipher.cs index 8e6120eef..ecebc3de9 100644 --- a/crypto/src/crypto/modes/GCMBlockCipher.cs +++ b/crypto/src/crypto/modes/GCMBlockCipher.cs @@ -513,10 +513,11 @@ namespace Org.BouncyCastle.Crypto.Modes private byte[] GetNextCounterBlock() { - for (int i = 15; i >= 12; --i) - { - if (++counter[i] != 0) break; - } + uint c = 1; + c += counter[15]; counter[15] = (byte)c; c >>= 8; + c += counter[14]; counter[14] = (byte)c; c >>= 8; + c += counter[13]; counter[13] = (byte)c; c >>= 8; + c += counter[12]; counter[12] = (byte)c; byte[] tmp = new byte[BlockSize]; // TODO Sure would be nice if ciphers could operate on int[] -- cgit 1.5.1