Search in sources :

Example 1 with HmacPrfKeyFormat

use of com.google.crypto.tink.proto.HmacPrfKeyFormat in project tink by google.

the class HmacPrfKeyManager method keyFactory.

@Override
public KeyFactory<HmacPrfKeyFormat, HmacPrfKey> keyFactory() {
    return new KeyFactory<HmacPrfKeyFormat, HmacPrfKey>(HmacPrfKeyFormat.class) {

        @Override
        public void validateKeyFormat(HmacPrfKeyFormat format) throws GeneralSecurityException {
            if (format.getKeySize() < MIN_KEY_SIZE_IN_BYTES) {
                throw new GeneralSecurityException("key too short");
            }
            validateParams(format.getParams());
        }

        @Override
        public HmacPrfKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException {
            return HmacPrfKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
        }

        @Override
        public HmacPrfKey createKey(HmacPrfKeyFormat format) {
            return HmacPrfKey.newBuilder().setVersion(getVersion()).setParams(format.getParams()).setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).build();
        }

        @Override
        public HmacPrfKey deriveKey(HmacPrfKeyFormat format, InputStream inputStream) throws GeneralSecurityException {
            Validators.validateVersion(format.getVersion(), getVersion());
            byte[] pseudorandomness = new byte[format.getKeySize()];
            try {
                int read = inputStream.read(pseudorandomness);
                if (read != format.getKeySize()) {
                    throw new GeneralSecurityException("Not enough pseudorandomness given");
                }
                return HmacPrfKey.newBuilder().setVersion(getVersion()).setParams(format.getParams()).setKeyValue(ByteString.copyFrom(pseudorandomness)).build();
            } catch (IOException e) {
                throw new GeneralSecurityException("Reading pseudorandomness failed", e);
            }
        }

        @Override
        public Map<String, KeyFactory.KeyFormat<HmacPrfKeyFormat>> keyFormats() throws GeneralSecurityException {
            Map<String, KeyFactory.KeyFormat<HmacPrfKeyFormat>> result = new HashMap<>();
            result.put("HMAC_SHA256_PRF", new KeyFactory.KeyFormat<>(HmacPrfKeyFormat.newBuilder().setParams(HmacPrfParams.newBuilder().setHash(HashType.SHA256).build()).setKeySize(32).build(), KeyTemplate.OutputPrefixType.RAW));
            result.put("HMAC_SHA512_PRF", new KeyFactory.KeyFormat<>(HmacPrfKeyFormat.newBuilder().setParams(HmacPrfParams.newBuilder().setHash(HashType.SHA512).build()).setKeySize(64).build(), KeyTemplate.OutputPrefixType.RAW));
            return Collections.unmodifiableMap(result);
        }
    };
}
Also used : HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat)

Example 2 with HmacPrfKeyFormat

use of com.google.crypto.tink.proto.HmacPrfKeyFormat in project tink by google.

the class PrfKeyTemplates method createHmacTemplate.

private static KeyTemplate createHmacTemplate(int keySize, HashType hashType) {
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(hashType).build();
    HmacPrfKeyFormat format = HmacPrfKeyFormat.newBuilder().setParams(params).setKeySize(keySize).build();
    return KeyTemplate.newBuilder().setTypeUrl(new HmacPrfKeyManager().getKeyType()).setValue(format.toByteString()).setOutputPrefixType(OutputPrefixType.RAW).build();
}
Also used : HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams)

Example 3 with HmacPrfKeyFormat

use of com.google.crypto.tink.proto.HmacPrfKeyFormat in project tink by google.

the class HmacPrfKeyManagerTest method createKey_multipleTimes.

@Test
public void createKey_multipleTimes() throws Exception {
    HmacPrfKeyFormat keyFormat = makeHmacPrfKeyFormat(16, HashType.SHA256);
    int numKeys = 100;
    Set<String> keys = new TreeSet<String>();
    for (int i = 0; i < numKeys; ++i) {
        keys.add(TestUtil.hexEncode(factory.createKey(keyFormat).getKeyValue().toByteArray()));
    }
    assertThat(keys).hasSize(numKeys);
}
Also used : TreeSet(java.util.TreeSet) HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 4 with HmacPrfKeyFormat

use of com.google.crypto.tink.proto.HmacPrfKeyFormat in project tink by google.

the class HmacPrfKeyManagerTest method testDeriveKey_notEnoughKeyMaterial_throws.

@Test
public void testDeriveKey_notEnoughKeyMaterial_throws() throws Exception {
    byte[] keyMaterial = Random.randBytes(31);
    HmacPrfParams params = HmacPrfParams.newBuilder().setHash(HashType.SHA256).build();
    HmacPrfKeyFormat format = HmacPrfKeyFormat.newBuilder().setVersion(0).setParams(params).setKeySize(32).build();
    assertThrows(GeneralSecurityException.class, () -> factory.deriveKey(format, new ByteArrayInputStream(keyMaterial)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) HmacPrfParams(com.google.crypto.tink.proto.HmacPrfParams) Test(org.junit.Test)

Example 5 with HmacPrfKeyFormat

use of com.google.crypto.tink.proto.HmacPrfKeyFormat in project tink by google.

the class HmacPrfKeyManagerTest method testHmacSha512Template.

@Test
public void testHmacSha512Template() throws Exception {
    KeyTemplate template = HmacPrfKeyManager.hmacSha512Template();
    assertThat(template.getTypeUrl()).isEqualTo(new HmacPrfKeyManager().getKeyType());
    assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.RAW);
    HmacPrfKeyFormat format = HmacPrfKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertThat(format.getKeySize()).isEqualTo(64);
    assertThat(format.getParams().getHash()).isEqualTo(HashType.SHA512);
}
Also used : HmacPrfKeyFormat(com.google.crypto.tink.proto.HmacPrfKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Aggregations

HmacPrfKeyFormat (com.google.crypto.tink.proto.HmacPrfKeyFormat)9 Test (org.junit.Test)6 HmacPrfParams (com.google.crypto.tink.proto.HmacPrfParams)4 KeyTemplate (com.google.crypto.tink.KeyTemplate)2 ByteString (com.google.protobuf.ByteString)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HmacPrfKey (com.google.crypto.tink.proto.HmacPrfKey)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1