Search in sources :

Example 1 with AesCtrKeyFormat

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

the class AesCtrKeyManager method newKey.

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

Example 2 with AesCtrKeyFormat

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

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

the class AeadKeyTemplates method createAesCtrHmacAeadKeyTemplate.

/**
 * @return a {@link KeyTemplate} containing a {@link AesCtrHmacAeadKeyFormat} with some specific
 *     parameters.
 */
public static KeyTemplate createAesCtrHmacAeadKeyTemplate(int aesKeySize, int ivSize, int hmacKeySize, int tagSize, HashType hashType) {
    AesCtrKeyFormat aesCtrKeyFormat = AesCtrKeyFormat.newBuilder().setParams(AesCtrParams.newBuilder().setIvSize(ivSize).build()).setKeySize(aesKeySize).build();
    HmacKeyFormat hmacKeyFormat = HmacKeyFormat.newBuilder().setParams(HmacParams.newBuilder().setHash(hashType).setTagSize(tagSize).build()).setKeySize(hmacKeySize).build();
    AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.newBuilder().setAesCtrKeyFormat(aesCtrKeyFormat).setHmacKeyFormat(hmacKeyFormat).build();
    return KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(AesCtrHmacAeadKeyManager.TYPE_URL).setOutputPrefixType(OutputPrefixType.TINK).build();
}
Also used : AesCtrKeyFormat(com.google.crypto.tink.proto.AesCtrKeyFormat) HmacKeyFormat(com.google.crypto.tink.proto.HmacKeyFormat) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)

Aggregations

AesCtrKeyFormat (com.google.crypto.tink.proto.AesCtrKeyFormat)3 AesCtrHmacAeadKeyFormat (com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat)1 AesCtrKey (com.google.crypto.tink.proto.AesCtrKey)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 ByteString (com.google.protobuf.ByteString)1 GeneralSecurityException (java.security.GeneralSecurityException)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1