Search in sources :

Example 1 with AesCtrKey

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

the class TestUtil method createAesCtrHmacAeadKeyData.

/**
 * @return a {@code KeyData} containing a {@code AesCtrHmacAeadKey}.
 */
public static KeyData createAesCtrHmacAeadKeyData(byte[] aesCtrKeyValue, int ivSize, byte[] hmacKeyValue, int tagSize) throws Exception {
    AesCtrKey aesCtrKey = createAesCtrKey(aesCtrKeyValue, ivSize);
    HmacKey hmacKey = createHmacKey(hmacKeyValue, tagSize);
    AesCtrHmacAeadKey keyProto = AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).build();
    return createKeyData(keyProto, AeadConfig.AES_CTR_HMAC_AEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) HmacKey(com.google.crypto.tink.proto.HmacKey) AesCtrHmacAeadKey(com.google.crypto.tink.proto.AesCtrHmacAeadKey)

Example 2 with AesCtrKey

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

the class AesCtrKeyManagerTest method getPrimitive.

@Test
public void getPrimitive() throws Exception {
    AesCtrKey key = factory.createKey(createFormat(14, 32));
    IndCpaCipher managerCipher = manager.getPrimitive(key, IndCpaCipher.class);
    IndCpaCipher directCipher = new AesCtrJceCipher(key.getKeyValue().toByteArray(), 14);
    byte[] plaintext = Random.randBytes(20);
    assertThat(directCipher.decrypt(managerCipher.encrypt(plaintext))).isEqualTo(plaintext);
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) IndCpaCipher(com.google.crypto.tink.subtle.IndCpaCipher) AesCtrJceCipher(com.google.crypto.tink.subtle.AesCtrJceCipher) Test(org.junit.Test)

Example 3 with AesCtrKey

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

the class AesCtrKeyManager method getPrimitive.

/**
 * @param key {@code AesCtrKey} proto
 */
@Override
public AesCtrJceCipher getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof AesCtrKey)) {
        throw new GeneralSecurityException("expected AesCtrKey proto");
    }
    AesCtrKey keyProto = (AesCtrKey) key;
    validate(keyProto);
    return new AesCtrJceCipher(keyProto.getKeyValue().toByteArray(), keyProto.getParams().getIvSize());
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) GeneralSecurityException(java.security.GeneralSecurityException) AesCtrJceCipher(com.google.crypto.tink.subtle.AesCtrJceCipher)

Example 4 with AesCtrKey

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

the class AesCtrKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    AesCtrKeyFormat ctrKeyFormat = AesCtrKeyFormat.newBuilder().setParams(AesCtrParams.newBuilder().setIvSize(16).build()).setKeySize(16).build();
    ByteString serialized = ByteString.copyFrom(ctrKeyFormat.toByteArray());
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesCtrKeyManager.TYPE_URL).setValue(serialized).build();
    AesCtrKeyManager keyManager = new AesCtrKeyManager();
    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++) {
        AesCtrKey key = (AesCtrKey) keyManager.newKey(ctrKeyFormat);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        key = (AesCtrKey) keyManager.newKey(serialized);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = AesCtrKey.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) AesCtrKeyFormat(com.google.crypto.tink.proto.AesCtrKeyFormat) ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 5 with AesCtrKey

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

the class AesCtrHmacAeadKeyManager method newKey.

/**
 * @param keyFormat  {@code AesCtrHmacAeadKeyFormat} proto
 * @return new {@code AesCtrHmacAeadKey} proto
 */
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
    if (!(keyFormat instanceof AesCtrHmacAeadKeyFormat)) {
        throw new GeneralSecurityException("expected AesCtrHmacAeadKeyFormat proto");
    }
    AesCtrHmacAeadKeyFormat format = (AesCtrHmacAeadKeyFormat) keyFormat;
    AesCtrKey aesCtrKey = (AesCtrKey) Registry.newKey(AesCtrKeyManager.TYPE_URL, format.getAesCtrKeyFormat());
    HmacKey hmacKey = (HmacKey) Registry.newKey(MacConfig.HMAC_TYPE_URL, format.getHmacKeyFormat());
    return AesCtrHmacAeadKey.newBuilder().setAesCtrKey(aesCtrKey).setHmacKey(hmacKey).setVersion(VERSION).build();
}
Also used : AesCtrKey(com.google.crypto.tink.proto.AesCtrKey) GeneralSecurityException(java.security.GeneralSecurityException) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) HmacKey(com.google.crypto.tink.proto.HmacKey)

Aggregations

AesCtrKey (com.google.crypto.tink.proto.AesCtrKey)7 HmacKey (com.google.crypto.tink.proto.HmacKey)4 AesCtrHmacAeadKey (com.google.crypto.tink.proto.AesCtrHmacAeadKey)2 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)2 AesCtrKeyFormat (com.google.crypto.tink.proto.AesCtrKeyFormat)2 AesCtrJceCipher (com.google.crypto.tink.subtle.AesCtrJceCipher)2 ByteString (com.google.protobuf.ByteString)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Test (org.junit.Test)2 HmacKeyManager (com.google.crypto.tink.mac.HmacKeyManager)1 HmacKeyFormat (com.google.crypto.tink.proto.HmacKeyFormat)1 KeyData (com.google.crypto.tink.proto.KeyData)1 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)1 IndCpaCipher (com.google.crypto.tink.subtle.IndCpaCipher)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1