Search in sources :

Example 6 with AesGcmHkdfStreamingKeyFormat

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

the class StreamingAeadKeyTemplatesTest method testAES128_GCM_HKDF_4KB.

@Test
public void testAES128_GCM_HKDF_4KB() throws Exception {
    KeyTemplate template = StreamingAeadKeyTemplates.AES128_GCM_HKDF_4KB;
    assertEquals(AesGcmHkdfStreamingKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
    AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue());
    assertEquals(16, format.getKeySize());
    assertEquals(16, format.getParams().getDerivedKeySize());
    assertEquals(HashType.SHA256, format.getParams().getHkdfHashType());
    assertEquals(4096, format.getParams().getCiphertextSegmentSize());
}
Also used : AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 7 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManager method newKey.

/**
 * @param keyFormat {@code AesGcmHkdfStreamingKeyFormat} proto
 * @return new {@code AesGcmHkdfStreamingKey} proto
 */
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
    if (!(keyFormat instanceof AesGcmHkdfStreamingKeyFormat)) {
        throw new GeneralSecurityException("expected AesGcmHkdfStreamingKeyFormat proto");
    }
    AesGcmHkdfStreamingKeyFormat format = (AesGcmHkdfStreamingKeyFormat) keyFormat;
    validate(format);
    return AesGcmHkdfStreamingKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).setParams(format.getParams()).setVersion(VERSION).build();
}
Also used : AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) GeneralSecurityException(java.security.GeneralSecurityException)

Example 8 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testNewKeyWithBadFormat.

@Test
public void testNewKeyWithBadFormat() throws Exception {
    // key_size too small.
    AesGcmHkdfStreamingKeyFormat keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(15).build();
    ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    try {
        keyManager.newKey(keyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    try {
        keyManager.newKeyData(serializedKeyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    // Unknown HKDF HashType.
    AesGcmHkdfStreamingParams badKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(AES_KEY_SIZE).build();
    keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(badKeyParams).setKeySize(16).build();
    serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    try {
        keyManager.newKey(keyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    try {
        keyManager.newKeyData(serializedKeyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    // derived_key_size too small.
    badKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(10).setHkdfHashType(HashType.SHA256).build();
    keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(badKeyParams).setKeySize(16).build();
    serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    try {
        keyManager.newKey(keyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    try {
        keyManager.newKeyData(serializedKeyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    // ciphertext_segment_size too small.
    badKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(15).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
    keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(badKeyParams).setKeySize(16).build();
    serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    try {
        keyManager.newKey(keyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    try {
        keyManager.newKeyData(serializedKeyFormat);
        fail("Bad format, should have thrown exception");
    } catch (GeneralSecurityException expected) {
    // Expected
    }
    // All params good.
    AesGcmHkdfStreamingParams goodKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
    keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(goodKeyParams).setKeySize(16).build();
    serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    AesGcmHkdfStreamingKey unusedKey = (AesGcmHkdfStreamingKey) keyManager.newKey(keyFormat);
    unusedKey = (AesGcmHkdfStreamingKey) keyManager.newKey(serializedKeyFormat);
}
Also used : AesGcmHkdfStreamingKey(com.google.crypto.tink.proto.AesGcmHkdfStreamingKey) AesGcmHkdfStreamingParams(com.google.crypto.tink.proto.AesGcmHkdfStreamingParams) AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) ByteString(com.google.protobuf.ByteString) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Example 9 with AesGcmHkdfStreamingKeyFormat

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

the class AesGcmHkdfStreamingKeyManagerTest method testBasic.

@Test
public void testBasic() throws Exception {
    // Create primitive from a given key.
    AesGcmHkdfStreamingKey key = AesGcmHkdfStreamingKey.newBuilder().setVersion(0).setKeyValue(ByteString.copyFrom(Random.randBytes(20))).setParams(keyParams).build();
    StreamingAead streamingAead = keyManager.getPrimitive(key);
    StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
    // Create a key from KeyFormat, and use the key.
    AesGcmHkdfStreamingKeyFormat keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
    ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    key = (AesGcmHkdfStreamingKey) keyManager.newKey(serializedKeyFormat);
    streamingAead = keyManager.getPrimitive(key);
    StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
Also used : AesGcmHkdfStreamingKey(com.google.crypto.tink.proto.AesGcmHkdfStreamingKey) AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) ByteString(com.google.protobuf.ByteString) StreamingAead(com.google.crypto.tink.StreamingAead) Test(org.junit.Test)

Example 10 with AesGcmHkdfStreamingKeyFormat

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

the class StreamingAeadKeyTemplatesTest method testAES256_GCM_HKDF_4KB.

@Test
public void testAES256_GCM_HKDF_4KB() throws Exception {
    KeyTemplate template = StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB;
    assertEquals(AesGcmHkdfStreamingKeyManager.TYPE_URL, template.getTypeUrl());
    assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
    AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue());
    assertEquals(32, format.getKeySize());
    assertEquals(32, format.getParams().getDerivedKeySize());
    assertEquals(HashType.SHA256, format.getParams().getHkdfHashType());
    assertEquals(4096, format.getParams().getCiphertextSegmentSize());
}
Also used : AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Aggregations

AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)22 Test (org.junit.Test)19 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)7 AesGcmHkdfStreamingKey (com.google.crypto.tink.proto.AesGcmHkdfStreamingKey)5 KeyTemplate (com.google.crypto.tink.KeyTemplate)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteString (com.google.protobuf.ByteString)3 GeneralSecurityException (java.security.GeneralSecurityException)3 AesGcmHkdfStreamingParams (com.google.crypto.tink.proto.AesGcmHkdfStreamingParams)2 StreamingAead (com.google.crypto.tink.StreamingAead)1 HashType (com.google.crypto.tink.proto.HashType)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1