Search in sources :

Example 1 with Prf

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);
    }
}
Also used : Prf(com.google.crypto.tink.prf.Prf) Test(org.junit.Test)

Example 2 with Prf

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));
    }
}
Also used : Prf(com.google.crypto.tink.prf.Prf) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 3 with Prf

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.
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Prf(com.google.crypto.tink.prf.Prf) SecretKeySpec(javax.crypto.spec.SecretKeySpec)

Example 4 with Prf

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();
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) Prf(com.google.crypto.tink.prf.Prf) PrfSet(com.google.crypto.tink.prf.PrfSet) GeneralSecurityException(java.security.GeneralSecurityException) PrfSetComputeResponse(com.google.crypto.tink.proto.testing.PrfSetComputeResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException)

Example 5 with Prf

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);
    }
}
Also used : Prf(com.google.crypto.tink.prf.Prf) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Test(org.junit.Test)

Aggregations

Prf (com.google.crypto.tink.prf.Prf)5 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 Test (org.junit.Test)3 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 Mac (com.google.crypto.tink.Mac)1 PrfSet (com.google.crypto.tink.prf.PrfSet)1 PrfSetComputeResponse (com.google.crypto.tink.proto.testing.PrfSetComputeResponse)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1