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