Search in sources :

Example 6 with AesGcmSivKeyFormat

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

the class AesGcmSivKeyManagerTest method testRawAes256GcmSivTemplate.

@Test
public void testRawAes256GcmSivTemplate() throws Exception {
    KeyTemplate template = AesGcmSivKeyManager.rawAes256GcmSivTemplate();
    assertEquals(new AesGcmSivKeyManager().getKeyType(), template.getTypeUrl());
    assertEquals(KeyTemplate.OutputPrefixType.RAW, template.getOutputPrefixType());
    AesGcmSivKeyFormat format = AesGcmSivKeyFormat.parseFrom(ByteString.copyFrom(template.getValue()), ExtensionRegistryLite.getEmptyRegistry());
    assertEquals(32, format.getKeySize());
}
Also used : AesGcmSivKeyFormat(com.google.crypto.tink.proto.AesGcmSivKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 7 with AesGcmSivKeyFormat

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

the class AesGcmSivKeyManager method keyFactory.

@Override
public KeyFactory<AesGcmSivKeyFormat, AesGcmSivKey> keyFactory() {
    return new KeyFactory<AesGcmSivKeyFormat, AesGcmSivKey>(AesGcmSivKeyFormat.class) {

        @Override
        public void validateKeyFormat(AesGcmSivKeyFormat format) throws GeneralSecurityException {
            Validators.validateAesKeySize(format.getKeySize());
        }

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

        @Override
        public AesGcmSivKey createKey(AesGcmSivKeyFormat format) {
            return AesGcmSivKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).setVersion(getVersion()).build();
        }

        @Override
        public AesGcmSivKey deriveKey(AesGcmSivKeyFormat 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 AesGcmSivKey.newBuilder().setKeyValue(ByteString.copyFrom(pseudorandomness)).setVersion(getVersion()).build();
            } catch (IOException e) {
                throw new GeneralSecurityException("Reading pseudorandomness failed", e);
            }
        }

        @Override
        public Map<String, KeyFactory.KeyFormat<AesGcmSivKeyFormat>> keyFormats() throws GeneralSecurityException {
            Map<String, KeyFactory.KeyFormat<AesGcmSivKeyFormat>> result = new HashMap<>();
            result.put("AES128_GCM_SIV", createKeyFormat(16, KeyTemplate.OutputPrefixType.TINK));
            result.put("AES128_GCM_SIV_RAW", createKeyFormat(16, KeyTemplate.OutputPrefixType.RAW));
            result.put("AES256_GCM_SIV", createKeyFormat(32, KeyTemplate.OutputPrefixType.TINK));
            result.put("AES256_GCM_SIV_RAW", createKeyFormat(32, KeyTemplate.OutputPrefixType.RAW));
            return Collections.unmodifiableMap(result);
        }
    };
}
Also used : AesGcmSivKeyFormat(com.google.crypto.tink.proto.AesGcmSivKeyFormat) 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) AesGcmSivKeyFormat(com.google.crypto.tink.proto.AesGcmSivKeyFormat)

Example 8 with AesGcmSivKeyFormat

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

the class AesGcmSivKeyManagerTest method testAes256GcmSivTemplate.

@Test
public void testAes256GcmSivTemplate() throws Exception {
    KeyTemplate template = AesGcmSivKeyManager.aes256GcmSivTemplate();
    assertEquals(new AesGcmSivKeyManager().getKeyType(), template.getTypeUrl());
    assertEquals(KeyTemplate.OutputPrefixType.TINK, template.getOutputPrefixType());
    AesGcmSivKeyFormat format = AesGcmSivKeyFormat.parseFrom(ByteString.copyFrom(template.getValue()), ExtensionRegistryLite.getEmptyRegistry());
    assertEquals(32, format.getKeySize());
}
Also used : AesGcmSivKeyFormat(com.google.crypto.tink.proto.AesGcmSivKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Aggregations

AesGcmSivKeyFormat (com.google.crypto.tink.proto.AesGcmSivKeyFormat)8 Test (org.junit.Test)7 KeyTemplate (com.google.crypto.tink.KeyTemplate)4 ByteString (com.google.protobuf.ByteString)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 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