Search in sources :

Example 1 with Ed25519PrivateKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project popstellar by dedis.

the class Base64DataUtils method generatePoPToken.

/**
 * @return a pseudo randomly generated valid Ed25519 keypair
 */
public static PoPToken generatePoPToken() {
    byte[] privateKey = new byte[KEY_LENGTH];
    RANDOM.nextBytes(privateKey);
    Ed25519PrivateKeyParameters privateKeyParameters = new Ed25519PrivateKeyParameters(privateKey, 0);
    Ed25519PublicKeyParameters publicKeyParameters = privateKeyParameters.generatePublicKey();
    return new PoPToken(privateKey, publicKeyParameters.getEncoded());
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 2 with Ed25519PrivateKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project hedera-services by hashgraph.

the class Ed25519PrivateKey method fromBytes.

public static Ed25519PrivateKey fromBytes(byte[] keyBytes) {
    Ed25519PrivateKeyParameters privKeyParams;
    Ed25519PublicKeyParameters pubKeyParams = null;
    if (keyBytes.length == Ed25519.SECRET_KEY_SIZE) {
        // if the decoded bytes matches the length of a private key, try that
        privKeyParams = new Ed25519PrivateKeyParameters(keyBytes, 0);
    } else if (keyBytes.length == Ed25519.SECRET_KEY_SIZE + Ed25519.PUBLIC_KEY_SIZE) {
        // some legacy code delivers private and public key pairs concatted together
        try {
            // this is how we read only the first 32 bytes
            privKeyParams = new Ed25519PrivateKeyParameters(new ByteArrayInputStream(keyBytes, 0, Ed25519.SECRET_KEY_SIZE));
            // read the remaining 32 bytes as the public key
            pubKeyParams = new Ed25519PublicKeyParameters(keyBytes, Ed25519.SECRET_KEY_SIZE);
            return new Ed25519PrivateKey(privKeyParams, pubKeyParams);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        // decode a properly DER-encoded private key descriptor
        var privateKeyInfo = PrivateKeyInfo.getInstance(keyBytes);
        try {
            var privateKey = privateKeyInfo.parsePrivateKey();
            privKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey).getOctets(), 0);
            var pubKeyData = privateKeyInfo.getPublicKeyData();
            if (pubKeyData != null) {
                pubKeyParams = new Ed25519PublicKeyParameters(pubKeyData.getOctets(), 0);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return new Ed25519PrivateKey(privKeyParams, pubKeyParams);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Ed25519PublicKeyParameters(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) IOException(java.io.IOException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)

Example 3 with Ed25519PrivateKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project hotmoka by Hotmoka.

the class ED25519 method encodingOf.

@Override
public byte[] encodingOf(PrivateKey privateKey) {
    try {
        PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(new PKCS8EncodedKeySpec(privateKey.getEncoded()).getEncoded()));
        ASN1Encodable privateKey2 = privateKeyInfo.parsePrivateKey();
        Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey2).getOctets(), 0);
        return privateKeyParams.getEncoded();
    } catch (IOException e) {
        throw InternalFailureException.of("cannot encode the private key", e);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 4 with Ed25519PrivateKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project hotmoka by Hotmoka.

the class ED25519DET method encodingOf.

@Override
public byte[] encodingOf(PrivateKey privateKey) {
    try {
        PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(new PKCS8EncodedKeySpec(privateKey.getEncoded()).getEncoded()));
        ASN1Encodable privateKey2 = privateKeyInfo.parsePrivateKey();
        Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(((ASN1OctetString) privateKey2).getOctets(), 0);
        return privateKeyParams.getEncoded();
    } catch (IOException e) {
        throw InternalFailureException.of("cannot encode the private key", e);
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable) IOException(java.io.IOException) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 5 with Ed25519PrivateKeyParameters

use of org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters in project beckn-one by beckn.

the class SubscriberImpl method getRawPrivateKey.

public String getRawPrivateKey(String algo, String keyId) {
    Subscriber subscriber = getProxy();
    String pem = Request.getPrivateKey(subscriber.getSubscriberId(), keyId);
    try {
        if (!ObjectUtil.isVoid(pem)) {
            PrivateKey privateKey = Crypt.getInstance().getPrivateKey(algo, pem);
            if (privateKey instanceof BCEdDSAPrivateKey) {
                BCEdDSAPrivateKey privateKey1 = (BCEdDSAPrivateKey) privateKey;
                Field f = privateKey1.getClass().getDeclaredField("eddsaPrivateKey");
                // BC Desnot expose this hence this reflection stuff.
                f.setAccessible(true);
                Ed25519PrivateKeyParameters privateKeyParameters1 = (Ed25519PrivateKeyParameters) f.get(privateKey1);
                return Base64.getEncoder().encodeToString(privateKeyParameters1.getEncoded());
            } else if (privateKey instanceof BCXDHPrivateKey) {
                BCXDHPrivateKey privateKey1 = (BCXDHPrivateKey) privateKey;
                Field f = privateKey1.getClass().getDeclaredField("xdhPrivateKey");
                // BC Desnot expose this hence this reflection stuff.
                f.setAccessible(true);
                X25519PrivateKeyParameters privateKeyParameters1 = (X25519PrivateKeyParameters) f.get(privateKey1);
                return Base64.getEncoder().encodeToString(privateKeyParameters1.getEncoded());
            } else {
                throw new RuntimeException("Key not  identifiable!");
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) PrivateKey(java.security.PrivateKey) BCEdDSAPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.edec.BCEdDSAPrivateKey) BCXDHPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.edec.BCXDHPrivateKey) BCEdDSAPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.edec.BCEdDSAPrivateKey) X25519PrivateKeyParameters(org.bouncycastle.crypto.params.X25519PrivateKeyParameters) Ed25519PrivateKeyParameters(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) BCXDHPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.edec.BCXDHPrivateKey)

Aggregations

Ed25519PrivateKeyParameters (org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters)22 Ed25519PublicKeyParameters (org.bouncycastle.crypto.params.Ed25519PublicKeyParameters)9 IOException (java.io.IOException)4 Test (org.junit.jupiter.api.Test)4 SneakyThrows (lombok.SneakyThrows)3 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)3 Ed25519Signer (org.bouncycastle.crypto.signers.Ed25519Signer)3 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)2 CryptoException (org.bouncycastle.crypto.CryptoException)2 ECPrivateKeyParameters (org.bouncycastle.crypto.params.ECPrivateKeyParameters)2 UnsignedByteArray (org.xrpl.xrpl4j.codec.addresses.UnsignedByteArray)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 KeyGenerationException (com.github.dedis.popstellar.utility.error.keys.KeyGenerationException)1 UninitializedWalletException (com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException)1 Bytes (com.novi.serde.Bytes)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Field (java.lang.reflect.Field)1