use of com.google.crypto.tink.prf.Prf in project tink by google.
the class HkdfStreamingPrfTest method testPrfUniformity.
@Test
public void testPrfUniformity() throws Exception {
for (int i = 0; i < HashType.values().length; i++) {
byte[] ikm = Random.randBytes(128);
byte[] salt = Random.randBytes(128);
byte[] message = Random.randBytes(1024);
Prf prf = PrfImpl.wrap(new HkdfStreamingPrf(HashType.SHA256, ikm, salt));
byte[] prBytes = prf.compute(message, message.length);
TestUtil.ztestUniformString(prBytes);
TestUtil.ztestAutocorrelationUniformString(prBytes);
TestUtil.ztestCrossCorrelationUniformStrings(prBytes, message);
}
}
use of com.google.crypto.tink.prf.Prf in project tink by google.
the class PrfHmacJceTest method testPrfPrefixOfMac.
@Test
public void testPrfPrefixOfMac() throws Exception {
Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
for (MacTestVector t : HMAC_TEST_VECTORS) {
Prf prf = new PrfHmacJce(t.algName, new SecretKeySpec(t.key, "HMAC"));
Mac mac = new PrfMac(prf, t.tag.length);
byte[] prBytes = prf.compute(t.message, t.tag.length - 1);
byte[] tag = mac.computeMac(t.message);
assertEquals(prBytes.length, t.tag.length - 1);
assertArrayEquals(prBytes, Arrays.copyOf(tag, prBytes.length));
}
}
use of com.google.crypto.tink.prf.Prf in project tink by google.
the class PrfHmacJceTest method testPrfThrowsExceptionIfTagSizeIsTooLarge.
public void testPrfThrowsExceptionIfTagSizeIsTooLarge(String algoName, int tagSize) throws Exception {
try {
Prf r = new PrfHmacJce(algoName, new SecretKeySpec(Random.randBytes(16), "HMAC"));
r.compute(new byte[30], tagSize);
fail("Expected InvalidAlgorithmParameterException");
} catch (InvalidAlgorithmParameterException ex) {
// expected.
}
}
use of com.google.crypto.tink.prf.Prf in project tink by google.
the class PrfSetServiceImpl method compute.
/**
* Computes the output of one PRF.
*/
@Override
public void compute(PrfSetComputeRequest request, StreamObserver<PrfSetComputeResponse> responseObserver) {
PrfSetComputeResponse response;
try {
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
PrfSet prfSet = keysetHandle.getPrimitive(PrfSet.class);
Map<Integer, Prf> prfs = prfSet.getPrfs();
if (!prfs.containsKey(request.getKeyId())) {
response = PrfSetComputeResponse.newBuilder().setErr("Unknown Key ID.").build();
} else {
byte[] output = prfs.get(request.getKeyId()).compute(request.getInputData().toByteArray(), request.getOutputLength());
response = PrfSetComputeResponse.newBuilder().setOutput(ByteString.copyFrom(output)).build();
}
} catch (GeneralSecurityException | InvalidProtocolBufferException e) {
response = PrfSetComputeResponse.newBuilder().setErr(e.toString()).build();
} catch (IOException e) {
responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
return;
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
use of com.google.crypto.tink.prf.Prf in project tink by google.
the class PrfHmacJceTest method testPrfUniformity.
@Test
public void testPrfUniformity() throws GeneralSecurityException {
Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
for (MacTestVector t : HMAC_TEST_VECTORS) {
Prf prf = new PrfHmacJce(t.algName, new SecretKeySpec(t.key, "HMAC"));
// We need a string of bytes identical in size to the tag output size for the given algorithm
// so we can test cross correlation. We're not actually validating the output contents of the
// HMAC in this function. Therefore - just feed the test tag into the HMAC.
byte[] prBytes = prf.compute(t.tag, t.tag.length);
TestUtil.ztestUniformString(prBytes);
TestUtil.ztestAutocorrelationUniformString(prBytes);
TestUtil.ztestCrossCorrelationUniformStrings(prBytes, t.tag);
}
}
Aggregations