Search in sources :

Example 1 with AesGcmHkdfStreamingParams

use of com.google.crypto.tink.proto.AesGcmHkdfStreamingParams 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 2 with AesGcmHkdfStreamingParams

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

the class TestUtil method createAesGcmHkdfStreamingKeyData.

/**
 * @return a {@code KeyData} containing a {@code AesGcmHkdfStreamingKey}.
 */
public static KeyData createAesGcmHkdfStreamingKeyData(byte[] keyValue, int derivedKeySize, int ciphertextSegmentSize) throws Exception {
    AesGcmHkdfStreamingParams keyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(ciphertextSegmentSize).setDerivedKeySize(derivedKeySize).setHkdfHashType(HashType.SHA256).build();
    AesGcmHkdfStreamingKey keyProto = AesGcmHkdfStreamingKey.newBuilder().setVersion(0).setKeyValue(ByteString.copyFrom(keyValue)).setParams(keyParams).build();
    return createKeyData(keyProto, StreamingAeadConfig.AES_GCM_HKDF_STREAMINGAEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
Also used : AesGcmHkdfStreamingKey(com.google.crypto.tink.proto.AesGcmHkdfStreamingKey) AesGcmHkdfStreamingParams(com.google.crypto.tink.proto.AesGcmHkdfStreamingParams)

Example 3 with AesGcmHkdfStreamingParams

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

the class StreamingAeadKeyTemplates method createAesGcmHkdfStreamingKeyTemplate.

/**
 * @return a {@code KeyTemplate} containing a {@code AesGcmHkdfStreamingKeyFormat}
 *     with some specified parameters.
 */
public static KeyTemplate createAesGcmHkdfStreamingKeyTemplate(int mainKeySize, HashType hkdfHashType, int derivedKeySize, int ciphertextSegmentSize) {
    AesGcmHkdfStreamingParams keyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(ciphertextSegmentSize).setDerivedKeySize(derivedKeySize).setHkdfHashType(hkdfHashType).build();
    AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.newBuilder().setKeySize(mainKeySize).setParams(keyParams).build();
    return KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(AesGcmHkdfStreamingKeyManager.TYPE_URL).setOutputPrefixType(OutputPrefixType.RAW).build();
}
Also used : AesGcmHkdfStreamingParams(com.google.crypto.tink.proto.AesGcmHkdfStreamingParams) AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)

Aggregations

AesGcmHkdfStreamingParams (com.google.crypto.tink.proto.AesGcmHkdfStreamingParams)3 AesGcmHkdfStreamingKey (com.google.crypto.tink.proto.AesGcmHkdfStreamingKey)2 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)2 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Test (org.junit.Test)1