Hmac-SIV encryption/decryption.

This commit is contained in:
Alan Evans
2020-01-17 13:31:30 -05:00
committed by Greyson Parrelli
parent 3907ec8b51
commit 7d70ea78cd
18 changed files with 462 additions and 28 deletions

View File

@@ -0,0 +1,20 @@
package org.thoughtcrime.securesms.testutil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.thoughtcrime.securesms.util.Hex;
import java.io.IOException;
/**
* Jackson deserializes Json Strings to byte[] using Base64 by default, this allows Base16.
*/
public final class HexDeserializer extends JsonDeserializer<byte[]> {
@Override
public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return Hex.fromStringCondensed(p.getText());
}
}

View File

@@ -0,0 +1,48 @@
package org.thoughtcrime.securesms.testutil;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.security.SecureRandom;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
public final class SecureRandomTestUtil {
private SecureRandomTestUtil() {
}
/**
* Creates a {@link SecureRandom} that returns exactly the {@param returnValue} the first time
* its {@link SecureRandom#nextBytes(byte[])}} method is called.
* <p>
* Any attempt to call with the incorrect length, or a second time will fail.
*/
public static SecureRandom mockRandom(byte[] returnValue) {
SecureRandom mock = mock(SecureRandom.class);
ArgumentCaptor<byte[]> argument = ArgumentCaptor.forClass(byte[].class);
doAnswer(new Answer<Void>() {
private int count;
@Override
public Void answer(InvocationOnMock invocation) {
assertEquals("SecureRandom Mock: nextBytes only expected to be called once", 1, ++count);
byte[] output = argument.getValue();
assertEquals("SecureRandom Mock: nextBytes byte[] length requested does not match byte[] setup", returnValue.length, output.length);
System.arraycopy(returnValue, 0, output, 0, returnValue.length);
return null;
}
}).when(mock).nextBytes(argument.capture());
return mock;
}
}