Search in sources :

Example 1 with Ed25519KeyFormat

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

the class Ed25519PrivateKeyManager method keyFactory.

@Override
public KeyFactory<Ed25519KeyFormat, Ed25519PrivateKey> keyFactory() {
    return new KeyFactory<Ed25519KeyFormat, Ed25519PrivateKey>(Ed25519KeyFormat.class) {

        @Override
        public void validateKeyFormat(Ed25519KeyFormat format) throws GeneralSecurityException {
        }

        @Override
        public Ed25519KeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException {
            return Ed25519KeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
        }

        @Override
        public Ed25519PrivateKey createKey(Ed25519KeyFormat format) throws GeneralSecurityException {
            Ed25519Sign.KeyPair keyPair = Ed25519Sign.KeyPair.newKeyPair();
            Ed25519PublicKey publicKey = Ed25519PublicKey.newBuilder().setVersion(getVersion()).setKeyValue(ByteString.copyFrom(keyPair.getPublicKey())).build();
            return Ed25519PrivateKey.newBuilder().setVersion(getVersion()).setKeyValue(ByteString.copyFrom(keyPair.getPrivateKey())).setPublicKey(publicKey).build();
        }

        @Override
        public Ed25519PrivateKey deriveKey(Ed25519KeyFormat format, InputStream inputStream) throws GeneralSecurityException {
            Validators.validateVersion(format.getVersion(), getVersion());
            byte[] pseudorandomness = new byte[Ed25519Sign.SECRET_KEY_LEN];
            try {
                int read = inputStream.read(pseudorandomness);
                if (read != Ed25519Sign.SECRET_KEY_LEN) {
                    throw new GeneralSecurityException("Not enough pseudorandomness given");
                }
                Ed25519Sign.KeyPair keyPair = Ed25519Sign.KeyPair.newKeyPairFromSeed(pseudorandomness);
                Ed25519PublicKey publicKey = Ed25519PublicKey.newBuilder().setVersion(getVersion()).setKeyValue(ByteString.copyFrom(keyPair.getPublicKey())).build();
                return Ed25519PrivateKey.newBuilder().setVersion(getVersion()).setKeyValue(ByteString.copyFrom(keyPair.getPrivateKey())).setPublicKey(publicKey).build();
            } catch (IOException e) {
                throw new GeneralSecurityException("Reading pseudorandomness failed", e);
            }
        }

        @Override
        public Map<String, KeyFactory.KeyFormat<Ed25519KeyFormat>> keyFormats() throws GeneralSecurityException {
            Map<String, KeyFactory.KeyFormat<Ed25519KeyFormat>> result = new HashMap<>();
            result.put("ED25519", new KeyFormat<>(Ed25519KeyFormat.getDefaultInstance(), KeyTemplate.OutputPrefixType.TINK));
            result.put("ED25519_RAW", new KeyFormat<>(Ed25519KeyFormat.getDefaultInstance(), KeyTemplate.OutputPrefixType.RAW));
            // This is identical to ED25519_RAW.
            // It is needed to maintain backward compatibility with SignatureKeyTemplates.
            // TODO(b/185475349): remove this in 2.0.0.
            result.put("ED25519WithRawOutput", new KeyFormat<>(Ed25519KeyFormat.getDefaultInstance(), KeyTemplate.OutputPrefixType.RAW));
            return Collections.unmodifiableMap(result);
        }
    };
}
Also used : Ed25519KeyFormat(com.google.crypto.tink.proto.Ed25519KeyFormat) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) Ed25519KeyFormat(com.google.crypto.tink.proto.Ed25519KeyFormat) Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign)

Example 2 with Ed25519KeyFormat

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

the class Ed25519PrivateKeyManagerTest method createKey_differentValues.

// Tests that generated keys are different.
@Test
public void createKey_differentValues() throws Exception {
    Ed25519KeyFormat format = Ed25519KeyFormat.getDefaultInstance();
    Set<String> keys = new TreeSet<>();
    int numTests = 100;
    for (int i = 0; i < numTests; i++) {
        keys.add(TestUtil.hexEncode(factory.createKey(format).getKeyValue().toByteArray()));
    }
    assertThat(keys).hasSize(numTests);
}
Also used : Ed25519KeyFormat(com.google.crypto.tink.proto.Ed25519KeyFormat) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 3 with Ed25519KeyFormat

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

the class Ed25519PrivateKeyManagerTest method testRawEd25519Template.

@Test
public void testRawEd25519Template() throws Exception {
    KeyTemplate template = Ed25519PrivateKeyManager.rawEd25519Template();
    assertThat(template.getTypeUrl()).isEqualTo(new Ed25519PrivateKeyManager().getKeyType());
    assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.RAW);
    Ed25519KeyFormat unused = Ed25519KeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
}
Also used : Ed25519KeyFormat(com.google.crypto.tink.proto.Ed25519KeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 4 with Ed25519KeyFormat

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

the class Ed25519PrivateKeyManagerTest method testEd25519Template.

@Test
public void testEd25519Template() throws Exception {
    KeyTemplate template = Ed25519PrivateKeyManager.ed25519Template();
    assertThat(template.getTypeUrl()).isEqualTo(new Ed25519PrivateKeyManager().getKeyType());
    assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.TINK);
    Ed25519KeyFormat unused = Ed25519KeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
}
Also used : Ed25519KeyFormat(com.google.crypto.tink.proto.Ed25519KeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Aggregations

Ed25519KeyFormat (com.google.crypto.tink.proto.Ed25519KeyFormat)4 Test (org.junit.Test)3 KeyTemplate (com.google.crypto.tink.KeyTemplate)2 ByteString (com.google.protobuf.ByteString)2 Ed25519PublicKey (com.google.crypto.tink.proto.Ed25519PublicKey)1 Ed25519Sign (com.google.crypto.tink.subtle.Ed25519Sign)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1