Skip to content

Commit

Permalink
Merge pull request #33 from neXenio/feature/32-Constant-Time-Algorith…
Browse files Browse the repository at this point in the history
…m-for-MAC-Verification

Feature/32 constant time algorithm for mac verification
  • Loading branch information
marvinmirtschin authored Aug 16, 2021
2 parents a086049 + 0cf5f6e commit 9ec410b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import javax.crypto.SecretKey;

Expand Down Expand Up @@ -66,7 +65,7 @@ public void sign_validData_emitsSignature() {
public void sign_subsequentCallsWithSameKey_emitsSameSignature() {
Single<byte[]> signSingle = macProvider.sign(MESSAGE, firstSecretKey);

Single.zip(signSingle, signSingle, Arrays::equals)
Single.zip(signSingle, signSingle, BaseMacProvider::isEqual)
.test()
.assertValue(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.nexenio.rxkeystore.RxKeyStore;
import com.nexenio.rxkeystore.provider.BaseCryptoProvider;

import java.util.Arrays;

import javax.crypto.Mac;
import javax.crypto.SecretKey;

Expand Down Expand Up @@ -48,7 +46,7 @@ public Completable verify(@NonNull byte[] data, @NonNull byte[] signature, @NonN
@Override
public Single<Boolean> getVerificationResult(@NonNull byte[] data, @NonNull byte[] signature, @NonNull SecretKey secretKey) {
return sign(data, secretKey)
.map(computedSignature -> Arrays.equals(computedSignature, signature))
.map(computedSignature -> isEqual(computedSignature, signature))
.onErrorReturnItem(false);
}

Expand All @@ -66,6 +64,17 @@ protected Single<Mac> getMacInstance() {
));
}

public static boolean isEqual(byte[] a, byte[] b) {
if (a.length != b.length) {
return false;
}
byte result = 0;
for (byte i = 0; i < a.length; i++) {
result |= (byte) (a[i] ^ b[i]);
}
return result == 0;
}

public String getMacAlgorithm() {
return macAlgorithm;
}
Expand Down

0 comments on commit 9ec410b

Please sign in to comment.