Search in sources :

Example 1 with Ed25519PublicKey

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

the class Ed25519PrivateKeyManager method newKey.

private Ed25519PrivateKey newKey() throws GeneralSecurityException {
    Ed25519Sign.KeyPair keyPair = Ed25519Sign.KeyPair.newKeyPair();
    Ed25519PublicKey publicKey = Ed25519PublicKey.newBuilder().setVersion(VERSION).setKeyValue(ByteString.copyFrom(keyPair.getPublicKey())).build();
    return Ed25519PrivateKey.newBuilder().setVersion(VERSION).setKeyValue(ByteString.copyFrom(keyPair.getPrivateKey())).setPublicKey(publicKey).build();
}
Also used : Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign)

Example 2 with Ed25519PublicKey

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

the class Ed25519PublicKeyManager method getPrimitive.

@Override
public PublicKeyVerify getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof Ed25519PublicKey)) {
        throw new GeneralSecurityException("expected Ed25519PublicKey proto");
    }
    Ed25519PublicKey keyProto = (Ed25519PublicKey) key;
    validate(keyProto);
    return new Ed25519Verify(keyProto.getKeyValue().toByteArray());
}
Also used : Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) Ed25519Verify(com.google.crypto.tink.subtle.Ed25519Verify) GeneralSecurityException(java.security.GeneralSecurityException)

Example 3 with Ed25519PublicKey

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

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

the class RegistryTest method testRegisterAssymmetricKeyManagers_publicKeyManagerReRegister_getPublicKeyData.

@Test
public void testRegisterAssymmetricKeyManagers_publicKeyManagerReRegister_getPublicKeyData() throws Exception {
    Registry.reset();
    Registry.registerKeyManager(new TestPublicKeyTypeManager(), false);
    Registry.registerAsymmetricKeyManagers(new TestPrivateKeyTypeManager(), new TestPublicKeyTypeManager(), false);
    Registry.registerKeyManager(new TestPublicKeyTypeManager(), false);
    // Check that getPublicKeyData works now.
    Ed25519PrivateKey privateKey = Ed25519PrivateKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(32))).setPublicKey(Ed25519PublicKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(32)))).build();
    KeyData publicKeyData = Registry.getPublicKeyData(new TestPrivateKeyTypeManager().getKeyType(), privateKey.toByteString());
    assertThat(publicKeyData.getTypeUrl()).isEqualTo(new TestPublicKeyTypeManager().getKeyType());
    Ed25519PublicKey publicKey = Ed25519PublicKey.parseFrom(publicKeyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertThat(publicKey.getKeyValue()).isEqualTo(privateKey.getPublicKey().getKeyValue());
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 5 with Ed25519PublicKey

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

the class PrivateKeyManagerImplTest method getPublicKeyData_works.

@Test
public void getPublicKeyData_works() throws Exception {
    TestPrivateKeyTypeManager privateManager = new TestPrivateKeyTypeManager();
    TestPublicKeyTypeManager publicManager = new TestPublicKeyTypeManager();
    PrivateKeyManager<Void> manager = new PrivateKeyManagerImpl<>(privateManager, publicManager, Void.class);
    Ed25519PrivateKey privateKey = Ed25519PrivateKey.newBuilder().setPublicKey(Ed25519PublicKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(32)))).setKeyValue(ByteString.copyFrom(Random.randBytes(32))).build();
    KeyData keyData = manager.getPublicKeyData(privateKey.toByteString());
    assertThat(keyData.getTypeUrl()).isEqualTo("type.googleapis.com/google.crypto.tink.Ed25519PublicKey");
    Ed25519PublicKey publicKey = Ed25519PublicKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertThat(publicKey).isEqualTo(privateKey.getPublicKey());
    assertThat(keyData.getKeyMaterialType()).isEqualTo(KeyMaterialType.ASYMMETRIC_PUBLIC);
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519PublicKey(com.google.crypto.tink.proto.Ed25519PublicKey) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Aggregations

Ed25519PublicKey (com.google.crypto.tink.proto.Ed25519PublicKey)12 Test (org.junit.Test)9 Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)5 PublicKeySign (com.google.crypto.tink.PublicKeySign)2 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)2 KeyData (com.google.crypto.tink.proto.KeyData)2 Ed25519Sign (com.google.crypto.tink.subtle.Ed25519Sign)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Ed25519KeyFormat (com.google.crypto.tink.proto.Ed25519KeyFormat)1 Ed25519Verify (com.google.crypto.tink.subtle.Ed25519Verify)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1