public class Crypto
This object controls and provides the available and supported signature schemes for Corda.
Any implemented class SignatureScheme
should be strictly defined here.
However, only the schemes returned by {@link #listSupportedSignatureSchemes()} are supported.
Note that Corda currently supports the following signature schemes by their code names:
class SignatureScheme
Modifier and Type | Field and Description |
---|---|
static SignatureScheme |
COMPOSITE_KEY
Corda composite key type
|
static SignatureScheme |
DEFAULT_SIGNATURE_SCHEME
Our default signature scheme if no algorithm is specified (e.g. for key generation).
|
static SignatureScheme |
ECDSA_SECP256K1_SHA256
ECDSA signature scheme using the secp256k1 Koblitz curve.
|
static SignatureScheme |
ECDSA_SECP256R1_SHA256
ECDSA signature scheme using the secp256r1 (NIST P-256) curve.
|
static SignatureScheme |
EDDSA_ED25519_SHA512
EdDSA signature scheme using the ed255519 twisted Edwards curve.
|
static Crypto |
INSTANCE
This object controls and provides the available and supported signature schemes for Corda.
Any implemented
class SignatureScheme should be strictly defined here.
However, only the schemes returned by {@link #listSupportedSignatureSchemes()} are supported.
Note that Corda currently supports the following signature schemes by their code names: |
static SignatureScheme |
RSA_SHA256
RSA_SHA256 signature scheme using SHA256 as hash algorithm and MGF1 (with SHA256) as mask generation function.
Note: Recommended key size >= 3072 bits.
|
static org.bouncycastle.asn1.DLSequence |
SHA512_256
SPHINCS-256 hash-based signature scheme. It provides 128bit security against post-quantum attackers
at the cost of larger key sizes and loss of compatibility.
|
static SignatureScheme |
SPHINCS256_SHA256 |
Modifier and Type | Method and Description |
---|---|
static java.security.PrivateKey |
decodePrivateKey(byte[] encodedKey)
Decode a PKCS8 encoded key to its PrivateKey object.
Use this method if the key type is a-priori unknown.
|
static java.security.PrivateKey |
decodePrivateKey(java.lang.String schemeCodeName,
byte[] encodedKey)
Decode a PKCS8 encoded key to its PrivateKey object based on the input scheme code name.
This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
|
static java.security.PrivateKey |
decodePrivateKey(SignatureScheme signatureScheme,
byte[] encodedKey)
Decode a PKCS8 encoded key to its PrivateKey object based on the input scheme code name.
This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
|
static java.security.PublicKey |
decodePublicKey(byte[] encodedKey)
Decode an X509 encoded key to its PublicKey object.
Use this method if the key type is a-priori unknown.
|
static java.security.PublicKey |
decodePublicKey(java.lang.String schemeCodeName,
byte[] encodedKey)
Decode an X509 encoded key to its PrivateKey object based on the input scheme code name.
This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
|
static java.security.PublicKey |
decodePublicKey(SignatureScheme signatureScheme,
byte[] encodedKey)
Decode an X509 encoded key to its PrivateKey object based on the input scheme code name.
This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
|
static java.security.KeyPair |
deriveKeyPair(SignatureScheme signatureScheme,
java.security.PrivateKey privateKey,
byte[] seed)
Deterministically generate/derive a KeyPair using an existing private key and a seed as inputs.
This operation is currently supported for ECDSA secp256r1 (NIST P-256), ECDSA secp256k1 and EdDSA ed25519.
|
static java.security.KeyPair |
deriveKeyPair(java.security.PrivateKey privateKey,
byte[] seed)
Deterministically generate/derive a KeyPair using an existing private key and a seed as inputs.
Use this method if the
class SignatureScheme of the private key input is not known. |
static java.security.KeyPair |
deriveKeyPairFromEntropy(SignatureScheme signatureScheme,
java.math.BigInteger entropy)
Returns a key pair derived from the given BigInteger entropy. This is useful for unit tests
and other cases where you want hard-coded private keys.
Currently, EDDSA_ED25519_SHA512 is the sole scheme supported for this operation.
|
static java.security.KeyPair |
deriveKeyPairFromEntropy(java.math.BigInteger entropy)
Returns a DEFAULT_SIGNATURE_SCHEME key pair derived from the given BigInteger entropy.
|
static byte[] |
doSign(java.security.PrivateKey privateKey,
byte[] clearData)
Generic way to sign ByteArray data with a PrivateKey. Strategy on on identifying the actual signing scheme is based
on the PrivateKey type, but if the schemeCodeName is known, then better use
doSign(signatureScheme: String, privateKey: PrivateKey, clearData: ByteArray).
|
static byte[] |
doSign(java.lang.String schemeCodeName,
java.security.PrivateKey privateKey,
byte[] clearData)
|
static byte[] |
doSign(SignatureScheme signatureScheme,
java.security.PrivateKey privateKey,
byte[] clearData)
|
static TransactionSignature |
doSign(java.security.KeyPair keyPair,
SignableData signableData)
Generic way to sign
class SignableData objects with a PrivateKey.
class SignableData is a wrapper over the transaction's id (Merkle root) in order to attach extra information, such as
a timestamp or partial and blind signature indicators. |
static boolean |
doVerify(java.lang.String schemeCodeName,
java.security.PublicKey publicKey,
byte[] signatureData,
byte[] clearData)
Utility to simplify the act of verifying a digital signature.
It returns true if it succeeds, but it always throws an exception if verification fails.
|
static boolean |
doVerify(java.security.PublicKey publicKey,
byte[] signatureData,
byte[] clearData)
Utility to simplify the act of verifying a digital signature by identifying the signature scheme used from the input public key's type.
It returns true if it succeeds, but it always throws an exception if verification fails.
Strategy on identifying the actual signing scheme is based on the PublicKey type, but if the schemeCodeName is known,
then better use doVerify(schemeCodeName: String, publicKey: PublicKey, signatureData: ByteArray, clearData: ByteArray).
|
static boolean |
doVerify(SignatureScheme signatureScheme,
java.security.PublicKey publicKey,
byte[] signatureData,
byte[] clearData)
Method to verify a digital signature.
It returns true if it succeeds, but it always throws an exception if verification fails.
|
static boolean |
doVerify(SecureHash txId,
TransactionSignature transactionSignature)
Utility to simplify the act of verifying a
class TransactionSignature .
It returns true if it succeeds, but it always throws an exception if verification fails. |
static java.security.Provider |
findProvider(java.lang.String name) |
static SignatureScheme |
findSignatureScheme(org.bouncycastle.asn1.x509.AlgorithmIdentifier algorithm) |
static SignatureScheme |
findSignatureScheme(java.lang.String schemeCodeName)
Factory pattern to retrieve the corresponding
class SignatureScheme based on the type of the String input.
This function is usually called by key generators and verify signature functions.
In case the input is not a key in the supportedSignatureSchemes map, null will be returned. |
static SignatureScheme |
findSignatureScheme(java.security.PublicKey key)
Retrieve the corresponding
class SignatureScheme based on the type of the input Key.
This function is usually called when requiring to verify signatures and the signing schemes must be defined.
For the supported signature schemes see class Crypto . |
static SignatureScheme |
findSignatureScheme(java.security.PrivateKey key)
Retrieve the corresponding
class SignatureScheme based on the type of the input Key.
This function is usually called when requiring to verify signatures and the signing schemes must be defined.
For the supported signature schemes see class Crypto . |
static java.security.KeyPair |
generateKeyPair(java.lang.String schemeCodeName)
Utility to simplify the act of generating keys.
Normally, we don't expect other errors here, assuming that key generation parameters for every supported signature scheme have been unit-tested.
|
static java.security.KeyPair |
generateKeyPair(SignatureScheme signatureScheme)
Generate a KeyPair for the selected
class SignatureScheme .
Note that RSA is the sole algorithm initialized specifically by its supported keySize. |
static java.security.KeyPair |
generateKeyPair()
Generate a KeyPair for the selected
class SignatureScheme .
Note that RSA is the sole algorithm initialized specifically by its supported keySize. |
static boolean |
isSupportedSignatureScheme(SignatureScheme signatureScheme)
Check if the requested
class SignatureScheme is supported by the system. |
static boolean |
isValid(SecureHash txId,
TransactionSignature transactionSignature)
Utility to simplify the act of verifying a digital signature by identifying the signature scheme used from the
input public key's type.
It returns true if it succeeds and false if not. In comparison to doVerify if the key and signature
do not match it returns false rather than throwing an exception. Normally you should use the function which throws,
as it avoids the risk of failing to test the result.
|
static boolean |
isValid(java.security.PublicKey publicKey,
byte[] signatureData,
byte[] clearData)
Utility to simplify the act of verifying a digital signature by identifying the signature scheme used from the
input public key's type.
It returns true if it succeeds and false if not. In comparison to doVerify if the key and signature
do not match it returns false rather than throwing an exception. Normally you should use the function which throws,
as it avoids the risk of failing to test the result.
Use this method if the signature scheme is not a-priori known.
|
static boolean |
isValid(SignatureScheme signatureScheme,
java.security.PublicKey publicKey,
byte[] signatureData,
byte[] clearData)
Method to verify a digital signature. In comparison to doVerify if the key and signature
do not match it returns false rather than throwing an exception.
Use this method if the signature scheme type is a-priori unknown.
|
static boolean |
publicKeyOnCurve(SignatureScheme signatureScheme,
java.security.PublicKey publicKey)
Check if a point's coordinates are on the expected curve to avoid certain types of ECC attacks.
Point-at-infinity is not permitted as well.
|
static java.util.List<net.corda.core.crypto.SignatureScheme> |
supportedSignatureSchemes() |
static java.security.PrivateKey |
toSupportedPrivateKey(java.security.PrivateKey key)
Convert a private key to a supported implementation. This can be used to convert a SUN's EC key to an BC key.
This method is usually required to retrieve keys from JKS keystores that by default return SUN implementations.
|
static java.security.PublicKey |
toSupportedPublicKey(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo key)
Convert a public key to a supported implementation.
|
static java.security.PublicKey |
toSupportedPublicKey(java.security.PublicKey key)
Convert a public key to a supported implementation. This can be used to convert a SUN's EC key to an BC key.
This method is usually required to retrieve a key (via its corresponding cert) from JKS keystores that by default
return SUN implementations.
|
public static SignatureScheme RSA_SHA256
RSA_SHA256 signature scheme using SHA256 as hash algorithm and MGF1 (with SHA256) as mask generation function. Note: Recommended key size >= 3072 bits.
public static SignatureScheme ECDSA_SECP256K1_SHA256
ECDSA signature scheme using the secp256k1 Koblitz curve.
public static SignatureScheme ECDSA_SECP256R1_SHA256
ECDSA signature scheme using the secp256r1 (NIST P-256) curve.
public static SignatureScheme EDDSA_ED25519_SHA512
EdDSA signature scheme using the ed255519 twisted Edwards curve.
public static org.bouncycastle.asn1.DLSequence SHA512_256
SPHINCS-256 hash-based signature scheme. It provides 128bit security against post-quantum attackers at the cost of larger key sizes and loss of compatibility.
public static SignatureScheme SPHINCS256_SHA256
public static SignatureScheme COMPOSITE_KEY
Corda composite key type
public static SignatureScheme DEFAULT_SIGNATURE_SCHEME
Our default signature scheme if no algorithm is specified (e.g. for key generation).
public static Crypto INSTANCE
This object controls and provides the available and supported signature schemes for Corda.
Any implemented class SignatureScheme
should be strictly defined here.
However, only the schemes returned by {@link #listSupportedSignatureSchemes()} are supported.
Note that Corda currently supports the following signature schemes by their code names:
class SignatureScheme
public static java.util.List<net.corda.core.crypto.SignatureScheme> supportedSignatureSchemes()
public static java.security.Provider findProvider(java.lang.String name)
public static SignatureScheme findSignatureScheme(org.bouncycastle.asn1.x509.AlgorithmIdentifier algorithm)
public static SignatureScheme findSignatureScheme(java.lang.String schemeCodeName)
Factory pattern to retrieve the corresponding class SignatureScheme
based on the type of the String input.
This function is usually called by key generators and verify signature functions.
In case the input is not a key in the supportedSignatureSchemes map, null will be returned.
schemeCodeName
- a String that should match a supported signature scheme code name (e.g. ECDSA_SECP256K1_SHA256), see class Crypto
.class SignatureScheme
,
Stringpublic static SignatureScheme findSignatureScheme(java.security.PublicKey key)
Retrieve the corresponding class SignatureScheme
based on the type of the input Key.
This function is usually called when requiring to verify signatures and the signing schemes must be defined.
For the supported signature schemes see class Crypto
.
key
- either private or public.class SignatureScheme
,
Key,
class Crypto
public static SignatureScheme findSignatureScheme(java.security.PrivateKey key)
Retrieve the corresponding class SignatureScheme
based on the type of the input Key.
This function is usually called when requiring to verify signatures and the signing schemes must be defined.
For the supported signature schemes see class Crypto
.
key
- either private or public.class SignatureScheme
,
Key,
class Crypto
public static java.security.PrivateKey decodePrivateKey(byte[] encodedKey)
Decode a PKCS8 encoded key to its PrivateKey object. Use this method if the key type is a-priori unknown.
encodedKey
- a PKCS8 encoded private key.public static java.security.PrivateKey decodePrivateKey(java.lang.String schemeCodeName, byte[] encodedKey)
Decode a PKCS8 encoded key to its PrivateKey object based on the input scheme code name. This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
schemeCodeName
- a String that should match a key in supportedSignatureSchemes map (e.g. ECDSA_SECP256K1_SHA256).encodedKey
- a PKCS8 encoded private key.public static java.security.PrivateKey decodePrivateKey(SignatureScheme signatureScheme, byte[] encodedKey)
Decode a PKCS8 encoded key to its PrivateKey object based on the input scheme code name. This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
signatureScheme
- a signature scheme (e.g. ECDSA_SECP256K1_SHA256).encodedKey
- a PKCS8 encoded private key.public static java.security.PublicKey decodePublicKey(byte[] encodedKey)
Decode an X509 encoded key to its PublicKey object. Use this method if the key type is a-priori unknown.
encodedKey
- an X509 encoded public key.public static java.security.PublicKey decodePublicKey(java.lang.String schemeCodeName, byte[] encodedKey)
Decode an X509 encoded key to its PrivateKey object based on the input scheme code name. This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
schemeCodeName
- a String that should match a key in supportedSignatureSchemes map (e.g. ECDSA_SECP256K1_SHA256).encodedKey
- an X509 encoded public key.public static java.security.PublicKey decodePublicKey(SignatureScheme signatureScheme, byte[] encodedKey)
Decode an X509 encoded key to its PrivateKey object based on the input scheme code name. This should be used when the type key is known, e.g. during deserialisation or with key caches or key managers.
signatureScheme
- a signature scheme (e.g. ECDSA_SECP256K1_SHA256).encodedKey
- an X509 encoded public key.public static byte[] doSign(java.security.PrivateKey privateKey, byte[] clearData)
Generic way to sign ByteArray data with a PrivateKey. Strategy on on identifying the actual signing scheme is based on the PrivateKey type, but if the schemeCodeName is known, then better use doSign(signatureScheme: String, privateKey: PrivateKey, clearData: ByteArray).
privateKey
- the signer's PrivateKey.clearData
- the data/message to be signed in ByteArray form (usually the Merkle root).public static byte[] doSign(java.lang.String schemeCodeName, java.security.PrivateKey privateKey, byte[] clearData)
Generic way to sign ByteArray data with a PrivateKey and a known schemeCodeName String.
schemeCodeName
- a signature scheme's code name (e.g. ECDSA_SECP256K1_SHA256).privateKey
- the signer's PrivateKey.clearData
- the data/message to be signed in ByteArray form (usually the Merkle root).public static byte[] doSign(SignatureScheme signatureScheme, java.security.PrivateKey privateKey, byte[] clearData)
Generic way to sign ByteArray data with a PrivateKey and a known Signature.
signatureScheme
- a class SignatureScheme
object, retrieved from supported signature schemes, see class Crypto
.privateKey
- the signer's PrivateKey.clearData
- the data/message to be signed in ByteArray form (usually the Merkle root).public static TransactionSignature doSign(java.security.KeyPair keyPair, SignableData signableData)
Generic way to sign class SignableData
objects with a PrivateKey.
class SignableData
is a wrapper over the transaction's id (Merkle root) in order to attach extra information, such as
a timestamp or partial and blind signature indicators.
keyPair
- the signer's KeyPair.signableData
- a class SignableData
object that adds extra information to a transaction.class TransactionSignature
object than contains the output of a successful signing, signer's public key and
the signature metadata.class SignableData
,
PrivateKey,
class SignableData
public static boolean doVerify(java.lang.String schemeCodeName, java.security.PublicKey publicKey, byte[] signatureData, byte[] clearData)
Utility to simplify the act of verifying a digital signature. It returns true if it succeeds, but it always throws an exception if verification fails.
schemeCodeName
- a signature scheme's code name (e.g. ECDSA_SECP256K1_SHA256).publicKey
- the signer's PublicKey.signatureData
- the signatureData on a message.clearData
- the clear data/message that was signed (usually the Merkle root).public static boolean doVerify(java.security.PublicKey publicKey, byte[] signatureData, byte[] clearData)
Utility to simplify the act of verifying a digital signature by identifying the signature scheme used from the input public key's type. It returns true if it succeeds, but it always throws an exception if verification fails. Strategy on identifying the actual signing scheme is based on the PublicKey type, but if the schemeCodeName is known, then better use doVerify(schemeCodeName: String, publicKey: PublicKey, signatureData: ByteArray, clearData: ByteArray).
publicKey
- the signer's PublicKey.signatureData
- the signatureData on a message.clearData
- the clear data/message that was signed (usually the Merkle root).public static boolean doVerify(SignatureScheme signatureScheme, java.security.PublicKey publicKey, byte[] signatureData, byte[] clearData)
Method to verify a digital signature. It returns true if it succeeds, but it always throws an exception if verification fails.
signatureScheme
- a class SignatureScheme
object, retrieved from supported signature schemes, see class Crypto
.publicKey
- the signer's PublicKey.signatureData
- the signatureData on a message.clearData
- the clear data/message that was signed (usually the Merkle root).public static boolean doVerify(SecureHash txId, TransactionSignature transactionSignature)
Utility to simplify the act of verifying a class TransactionSignature
.
It returns true if it succeeds, but it always throws an exception if verification fails.
txId
- transaction's id (Merkle root).transactionSignature
- the signature on the transaction.class TransactionSignature
public static boolean isValid(SecureHash txId, TransactionSignature transactionSignature)
Utility to simplify the act of verifying a digital signature by identifying the signature scheme used from the input public key's type. It returns true if it succeeds and false if not. In comparison to doVerify if the key and signature do not match it returns false rather than throwing an exception. Normally you should use the function which throws, as it avoids the risk of failing to test the result.
txId
- transaction's id (Merkle root).transactionSignature
- the signature on the transaction.public static boolean isValid(java.security.PublicKey publicKey, byte[] signatureData, byte[] clearData)
Utility to simplify the act of verifying a digital signature by identifying the signature scheme used from the input public key's type. It returns true if it succeeds and false if not. In comparison to doVerify if the key and signature do not match it returns false rather than throwing an exception. Normally you should use the function which throws, as it avoids the risk of failing to test the result. Use this method if the signature scheme is not a-priori known.
publicKey
- the signer's PublicKey.signatureData
- the signatureData on a message.clearData
- the clear data/message that was signed (usually the Merkle root).public static boolean isValid(SignatureScheme signatureScheme, java.security.PublicKey publicKey, byte[] signatureData, byte[] clearData)
Method to verify a digital signature. In comparison to doVerify if the key and signature do not match it returns false rather than throwing an exception. Use this method if the signature scheme type is a-priori unknown.
signatureScheme
- a class SignatureScheme
object, retrieved from supported signature schemes, see class Crypto
.publicKey
- the signer's PublicKey.signatureData
- the signatureData on a message.clearData
- the clear data/message that was signed (usually the Merkle root).public static java.security.KeyPair generateKeyPair(java.lang.String schemeCodeName)
Utility to simplify the act of generating keys. Normally, we don't expect other errors here, assuming that key generation parameters for every supported signature scheme have been unit-tested.
schemeCodeName
- a signature scheme's code name (e.g. ECDSA_SECP256K1_SHA256).public static java.security.KeyPair generateKeyPair(SignatureScheme signatureScheme)
Generate a KeyPair for the selected class SignatureScheme
.
Note that RSA is the sole algorithm initialized specifically by its supported keySize.
signatureScheme
- a supported class SignatureScheme
, see class Crypto
, default to DEFAULT_SIGNATURE_SCHEME if not provided.class SignatureScheme
.class SignatureScheme
public static java.security.KeyPair generateKeyPair()
Generate a KeyPair for the selected class SignatureScheme
.
Note that RSA is the sole algorithm initialized specifically by its supported keySize.
class SignatureScheme
.class SignatureScheme
public static java.security.KeyPair deriveKeyPair(SignatureScheme signatureScheme, java.security.PrivateKey privateKey, byte[] seed)
Deterministically generate/derive a KeyPair using an existing private key and a seed as inputs. This operation is currently supported for ECDSA secp256r1 (NIST P-256), ECDSA secp256k1 and EdDSA ed25519.
Similarly to BIP32, the implemented algorithm uses an HMAC function based on SHA512 and it is actually an implementation the HKDF rfc - Step 1: Extract function,
signatureScheme
- the class SignatureScheme
of the private key input.privateKey
- the PrivateKey that will be used as key to the HMAC-ed DKG function.seed
- an extra seed that will be used as value to the underlying HMAC.public static java.security.KeyPair deriveKeyPair(java.security.PrivateKey privateKey, byte[] seed)
Deterministically generate/derive a KeyPair using an existing private key and a seed as inputs.
Use this method if the class SignatureScheme
of the private key input is not known.
privateKey
- the PrivateKey that will be used as key to the HMAC-ed DKG function.seed
- an extra seed that will be used as value to the underlying HMAC.class SignatureScheme
public static java.security.KeyPair deriveKeyPairFromEntropy(SignatureScheme signatureScheme, java.math.BigInteger entropy)
Returns a key pair derived from the given BigInteger entropy. This is useful for unit tests and other cases where you want hard-coded private keys. Currently, EDDSA_ED25519_SHA512 is the sole scheme supported for this operation.
signatureScheme
- a supported class SignatureScheme
, see class Crypto
.entropy
- a BigInteger value.public static java.security.KeyPair deriveKeyPairFromEntropy(java.math.BigInteger entropy)
Returns a DEFAULT_SIGNATURE_SCHEME key pair derived from the given BigInteger entropy.
entropy
- a BigInteger value.public static boolean publicKeyOnCurve(SignatureScheme signatureScheme, java.security.PublicKey publicKey)
Check if a point's coordinates are on the expected curve to avoid certain types of ECC attacks. Point-at-infinity is not permitted as well.
publicKey
- a PublicKey, usually used to validate a signer's public key in on the Curve.signatureScheme
- a class SignatureScheme
object, retrieved from supported signature schemes, see class Crypto
.public static boolean isSupportedSignatureScheme(SignatureScheme signatureScheme)
Check if the requested class SignatureScheme
is supported by the system.
class SignatureScheme
public static java.security.PublicKey toSupportedPublicKey(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo key)
Convert a public key to a supported implementation.
key
- a public key.public static java.security.PublicKey toSupportedPublicKey(java.security.PublicKey key)
Convert a public key to a supported implementation. This can be used to convert a SUN's EC key to an BC key. This method is usually required to retrieve a key (via its corresponding cert) from JKS keystores that by default return SUN implementations.
key
- a public key.public static java.security.PrivateKey toSupportedPrivateKey(java.security.PrivateKey key)
Convert a private key to a supported implementation. This can be used to convert a SUN's EC key to an BC key. This method is usually required to retrieve keys from JKS keystores that by default return SUN implementations.
key
- a private key.