Search in sources :

Example 1 with AesCtrHmacStreamingKey

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

the class AesCtrHmacStreamingKeyManager method getPrimitive.

/**
 * @param key {@code AesCtrHmacStreamingKey} proto
 */
@Override
public StreamingAead getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof AesCtrHmacStreamingKey)) {
        throw new GeneralSecurityException("expected AesCtrHmacStreamingKey proto");
    }
    AesCtrHmacStreamingKey keyProto = (AesCtrHmacStreamingKey) key;
    validate(keyProto);
    return new AesCtrHmacStreaming(keyProto.getKeyValue().toByteArray(), StreamingAeadUtil.toHmacAlgo(keyProto.getParams().getHkdfHashType()), keyProto.getParams().getDerivedKeySize(), StreamingAeadUtil.toHmacAlgo(keyProto.getParams().getHmacParams().getHash()), keyProto.getParams().getHmacParams().getTagSize(), keyProto.getParams().getCiphertextSegmentSize(), /* firstSegmentOffset= */
    0);
}
Also used : AesCtrHmacStreaming(com.google.crypto.tink.subtle.AesCtrHmacStreaming) AesCtrHmacStreamingKey(com.google.crypto.tink.proto.AesCtrHmacStreamingKey) GeneralSecurityException(java.security.GeneralSecurityException)

Example 2 with AesCtrHmacStreamingKey

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

the class AesCtrHmacStreamingKeyManagerTest method testNewKeyWithBadFormat.

@Test
public void testNewKeyWithBadFormat() throws Exception {
    // key_size too small.
    AesCtrHmacStreamingKeyFormat keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(15).build();
    testNewKeyWithBadFormat(keyFormat);
    // Unknown HKDF HashType.
    AesCtrHmacStreamingParams badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(AES_KEY_SIZE).build();
    testNewKeyWithBadFormat(badKeyParams);
    // derived_key_size too small.
    badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(10).setHkdfHashType(HashType.SHA256).build();
    testNewKeyWithBadFormat(badKeyParams);
    // ciphertext_segment_size too small.
    badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(15).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
    testNewKeyWithBadFormat(badKeyParams);
    // No HmacParams.
    badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
    testNewKeyWithBadFormat(badKeyParams);
    // Unknown HmacParams.hash.
    badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(HmacParams.newBuilder().build()).build();
    testNewKeyWithBadFormat(badKeyParams);
    // tag size too small.
    badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(9).build()).build();
    testNewKeyWithBadFormat(badKeyParams);
    // tag size too big.
    badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(33).build()).build();
    testNewKeyWithBadFormat(badKeyParams);
    // All params good.
    AesCtrHmacStreamingParams goodKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(hmacParams).build();
    keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(goodKeyParams).setKeySize(16).build();
    ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    AesCtrHmacStreamingKey unusedKey = (AesCtrHmacStreamingKey) keyManager.newKey(keyFormat);
    unusedKey = (AesCtrHmacStreamingKey) keyManager.newKey(serializedKeyFormat);
}
Also used : AesCtrHmacStreamingParams(com.google.crypto.tink.proto.AesCtrHmacStreamingParams) ByteString(com.google.protobuf.ByteString) AesCtrHmacStreamingKey(com.google.crypto.tink.proto.AesCtrHmacStreamingKey) AesCtrHmacStreamingKeyFormat(com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat) Test(org.junit.Test)

Example 3 with AesCtrHmacStreamingKey

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

the class AesCtrHmacStreamingKeyManagerTest method testBasic.

@Test
public void testBasic() throws Exception {
    // Create primitive from a given key.
    AesCtrHmacStreamingKey key = AesCtrHmacStreamingKey.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.
    AesCtrHmacStreamingKeyFormat keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
    ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    key = (AesCtrHmacStreamingKey) keyManager.newKey(serializedKeyFormat);
    streamingAead = keyManager.getPrimitive(key);
    StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
Also used : AesCtrHmacStreamingKey(com.google.crypto.tink.proto.AesCtrHmacStreamingKey) ByteString(com.google.protobuf.ByteString) AesCtrHmacStreamingKeyFormat(com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat) StreamingAead(com.google.crypto.tink.StreamingAead) Test(org.junit.Test)

Example 4 with AesCtrHmacStreamingKey

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

the class AesCtrHmacStreamingKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    AesCtrHmacStreamingKeyFormat keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
    ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
    Set<String> keys = new TreeSet<String>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 27;
    for (int i = 0; i < numTests / 3; i++) {
        AesCtrHmacStreamingKey key = (AesCtrHmacStreamingKey) keyManager.newKey(keyFormat);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        key = (AesCtrHmacStreamingKey) keyManager.newKey(serializedKeyFormat);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(serializedKeyFormat);
        key = AesCtrHmacStreamingKey.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) AesCtrHmacStreamingKey(com.google.crypto.tink.proto.AesCtrHmacStreamingKey) AesCtrHmacStreamingKeyFormat(com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat) ByteString(com.google.protobuf.ByteString) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 5 with AesCtrHmacStreamingKey

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

the class TestUtil method createAesCtrHmacStreamingKeyData.

/**
 * @return a {@code KeyData} containing a {@code AesCtrHmacStreamingKey}.
 */
public static KeyData createAesCtrHmacStreamingKeyData(byte[] keyValue, int derivedKeySize, int ciphertextSegmentSize) throws Exception {
    HmacParams hmacParams = HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(16).build();
    AesCtrHmacStreamingParams keyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(ciphertextSegmentSize).setDerivedKeySize(derivedKeySize).setHkdfHashType(HashType.SHA256).setHmacParams(hmacParams).build();
    AesCtrHmacStreamingKey keyProto = AesCtrHmacStreamingKey.newBuilder().setVersion(0).setKeyValue(ByteString.copyFrom(keyValue)).setParams(keyParams).build();
    return createKeyData(keyProto, StreamingAeadConfig.AES_CTR_HMAC_STREAMINGAEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
Also used : AesCtrHmacStreamingParams(com.google.crypto.tink.proto.AesCtrHmacStreamingParams) AesCtrHmacStreamingKey(com.google.crypto.tink.proto.AesCtrHmacStreamingKey) HmacParams(com.google.crypto.tink.proto.HmacParams)

Aggregations

AesCtrHmacStreamingKey (com.google.crypto.tink.proto.AesCtrHmacStreamingKey)5 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)3 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.Test)3 AesCtrHmacStreamingParams (com.google.crypto.tink.proto.AesCtrHmacStreamingParams)2 StreamingAead (com.google.crypto.tink.StreamingAead)1 HmacParams (com.google.crypto.tink.proto.HmacParams)1 KeyData (com.google.crypto.tink.proto.KeyData)1 AesCtrHmacStreaming (com.google.crypto.tink.subtle.AesCtrHmacStreaming)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1