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();
}
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());
}
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);
}
};
}
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());
}
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);
}
Aggregations