Class KeyPair
- java.lang.Object
-
- javacard.security.KeyPair
-
public final class KeyPair extends Object
This class is a container for a key pair (a public key and a private key). It does not enforce any security, and, when initialized, should be treated like a PrivateKey.In addition, this class features a key generation method.
- See Also:
PublicKey,PrivateKey
-
-
Field Summary
Fields Modifier and Type Field Description static byteALG_DHKeyPairobject containing an DH key pair for modular exponentiation based Diffie HellmanKeyAgreementoperations.static byteALG_DSAKeyPairobject containing a DSA key pair.static byteALG_EC_F2MKeyPairobject containing an EC key pair for EC operations over fields of characteristic 2 with polynomial basis.static byteALG_EC_FPKeyPairobject containing an EC key pair for EC operations over large prime fieldsstatic byteALG_RSAKeyPairobject containing a RSA key pair.static byteALG_RSA_CRTKeyPairobject containing a RSA key pair with private key in its Chinese Remainder Theorem form.
-
Constructor Summary
Constructors Constructor Description KeyPair(byte algorithm, short keyLength)Constructs aKeyPairinstance for the specified algorithm and keylength; the encapsulated keys are uninitialized.KeyPair(PublicKey publicKey, PrivateKey privateKey)Constructs a newKeyPairobject containing the specified public key and private key.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidgenKeyPair()(Re)Initializes the key objects encapsulated in thisKeyPairinstance with new key values.voidgenKeyPair(AlgorithmParameterSpec params)(Re)Initializes the key objects encapsulated in thisKeyPairinstance with new key values.PrivateKeygetPrivate()Returns a reference to the private key component of thisKeyPairobject.PublicKeygetPublic()Returns a reference to the public key component of thisKeyPairobject.
-
-
-
Field Detail
-
ALG_RSA
public static final byte ALG_RSA
KeyPairobject containing a RSA key pair.- See Also:
- Constant Field Values
-
ALG_RSA_CRT
public static final byte ALG_RSA_CRT
KeyPairobject containing a RSA key pair with private key in its Chinese Remainder Theorem form.- See Also:
- Constant Field Values
-
ALG_DSA
public static final byte ALG_DSA
KeyPairobject containing a DSA key pair.- See Also:
- Constant Field Values
-
ALG_EC_F2M
public static final byte ALG_EC_F2M
KeyPairobject containing an EC key pair for EC operations over fields of characteristic 2 with polynomial basis.- See Also:
- Constant Field Values
-
ALG_EC_FP
public static final byte ALG_EC_FP
KeyPairobject containing an EC key pair for EC operations over large prime fields- See Also:
- Constant Field Values
-
ALG_DH
public static final byte ALG_DH
KeyPairobject containing an DH key pair for modular exponentiation based Diffie HellmanKeyAgreementoperations.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
KeyPair
public KeyPair(byte algorithm, short keyLength) throws CryptoExceptionConstructs aKeyPairinstance for the specified algorithm and keylength; the encapsulated keys are uninitialized. To initialize theKeyPairinstance use thegenKeyPair()method.The encapsulated key objects are of the specified
keyLengthsize and implement the appropriateKeyinterface associated with the specified algorithm (example -RSAPublicKeyinterface for the public key andRSAPrivateKeyinterface for the private key within anALG_RSAkey pair).Notes:
- The key objects encapsulated in the generated
KeyPairobject need not support theKeyEncryptioninterface.
- Parameters:
algorithm- the type of algorithm whose key pair needs to be generated. Valid codes listed inALG_*constants above, for exampleALG_RSA.keyLength- the key size in bits. The valid key bit lengths are key type dependent. See theKeyBuilderclass.- Throws:
CryptoException- with the following reason codes:CryptoException.NO_SUCH_ALGORITHMif:- the requested algorithm associated with the specified type, size of key is not supported.
- See Also:
KeyBuilder,Signature,KeyAgreement,javacardx.crypto.Cipher,javacardx.crypto.KeyEncryption
- The key objects encapsulated in the generated
-
KeyPair
public KeyPair(PublicKey publicKey, PrivateKey privateKey) throws CryptoException
Constructs a newKeyPairobject containing the specified public key and private key.Note that this constructor only stores references to the public and private key components in the generated
KeyPairobject. It does not throw an exception if the key parameter objects are uninitialized.It may be used to construct a
KeyPairobject for named curves of any type as defined inNamedParameterSpec.Example:
NamedParameterSpec x25519 = NamedParameterSpec.getInstance(X25519); XECPublicKey pub = KeyBuilder.buildXECKey(x25519, (short)(MEMORY_TYPE_PERSISTENT|ATTR_PUBLIC), false); XECPrivateKey priv = KeyBuilder.buildXECKey(x25519, (short)(MEMORY_TYPE_PERSISTENT|ATTR_PRIVATE), false); KeyPair kp = new KeyPair(pub,priv); kp.genKeyPair();
- Parameters:
publicKey- the public key.privateKey- the private key.- Throws:
CryptoException- with the following reason codes:CryptoException.ILLEGAL_VALUEif the input parameter key objects are mismatched - different algorithms or different key sizes. Parameter values are not checked.CryptoException.NO_SUCH_ALGORITHMif the algorithm associated with the specified type, size of key is not supported.
- See Also:
KeyBuilder,Signature,KeyAgreement,javacardx.crypto.Cipher,javacardx.crypto.KeyEncryption
-
-
Method Detail
-
genKeyPair
public final void genKeyPair() throws CryptoException(Re)Initializes the key objects encapsulated in thisKeyPairinstance with new key values. The initialized public and private key objects encapsulated in this instance will then be suitable for use with theSignature,CipherandKeyAgreementobjects. An internal secure random number generator is used during new key pair generation.Notes:
- For the RSA algorithm, if the exponent value in the public key object is pre-initialized, it will be retained. Otherwise, a default value of 65537 will be used.
- For the DSA algorithm, if the p, q and g parameters of the public key object are pre-initialized, they will be retained. Otherwise, default precomputed parameter sets MAY be used.
- For the EC case, if the Field, A, B, G and R parameters of the public key object are pre-initialized, then they will be retained. Otherwise default pre-specified values MAY be used.
- If the time taken to generate the key values is excessive, the implementation may automatically request additional APDU processing time from the CAD.
- Throws:
CryptoException- with the following reason codes:CryptoException.ILLEGAL_VALUEif the pre-initialized exponent value parameter in the RSA public key or the pre-initialized p, q, g parameter set in the DSA public key or the pre-initialized Field, A, B, G and R parameter set in public EC key is invalid.
- See Also:
Signature,javacardx.crypto.Cipher,RSAPublicKey,ECKey,DSAKey
-
genKeyPair
public final void genKeyPair(AlgorithmParameterSpec params) throws CryptoException
(Re)Initializes the key objects encapsulated in thisKeyPairinstance with new key values. The initialized public and private key objects encapsulated in this instance will then be suitable for use with theSignature,CipherandKeyAgreementobjects.This method provides means for the caller to configure some parameters of the key generation algorithm by providing a class implementing the relevant sub-interfaces of
AlgorithmParameterSpec.- The primality test can be customized using the
PrimalityTestParamSpecinterface. - The random number generator can be customized using one of the following
interfaces:
KDFCounterModeSpec: the random number generator will internally use theDerivationFunction.ALG_KDF_COUNTER_MODEalgorithmKDFDoublePipelineIterationModeSpec: the random number generator will internally use theDerivationFunction.ALG_KDF_DPI_MODEalgorithmKDFFeedbackModeSpec: the random number generator will internally use theDerivationFunction.ALG_KDF_FEEDBACK_MODEalgorithm
- If
paramsparameter isnullthen the behavior is the same as thegenKeyPair()method.
Notes:
- For the RSA algorithm, if the exponent value in the public key object is pre-initialized, it will be retained. Otherwise, a default value of 65537 will be used.
- If the time taken to generate the key values is excessive, the implementation may automatically request additional APDU processing time from the CAD.
- Parameters:
params- algorithm parameters to configure the key generation- Throws:
CryptoException- with the following reason codes:CryptoException.NO_SUCH_ALGORITHMif the algorithm parameters are not supported.CryptoException.ILLEGAL_VALUE- if the pre-initialized exponent value parameter in the RSA public key is invalid
-
if the
paramsparameter implements none or an invalid combination of valid interfaces (e.g implementing bothKDFCounterModeSpecandKDFDoublePipelineIterationModeSpecat the same time) -
if the values retrieved from the
paramsparameters are invalid (e.g getRound() for Miller Rabin test returning a negative value}
- Since:
- 3.1
- See Also:
Signature,javacardx.crypto.Cipher,RSAPublicKey
- The primality test can be customized using the
-
getPublic
public PublicKey getPublic()
Returns a reference to the public key component of thisKeyPairobject.- Returns:
- a reference to the public key.
-
getPrivate
public PrivateKey getPrivate()
Returns a reference to the private key component of thisKeyPairobject.- Returns:
- a reference to the private key.
-
-