com.intel.crypto
Class EccAlg
- java.lang.Object
-
- com.intel.crypto.EccAlg
-
public abstract class EccAlg extends java.lang.Object
This abstract class represents ECC algorithm set including key-generation, signing and verifying. This class allows the instantiation of an ECC implementation class through the
create()
factory method.NOTE:
The ECC algorithm implementation supports the following curve types only:- The secp256k1 curve is a 256-bit elliptic curve over finite field Fp, based on "SEC 2: Recommended Elliptic Curve Domain Parameters" Version 1.0 September 20, 2000.
The following key generation and signing algorithms are supported by this class:- ECC keys generation
- ECC signature using the ECDSA algorithm
The default hash algorithm is SHA-526HashAlg.HASH_TYPE_SHA256
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static class
EccAlg.CurvePoint
This class represents a generic uncompressed point (x, y) on an elliptic curve.static class
EccAlg.CurveProperties
This class represents elliptic curve properties, namely the tuple T = (p, a, b, G, n, h).
-
Field Summary
Fields Modifier and Type Field and Description static short
ECC_CURVE_TYPE_SECP256K1
The ECC secp256k1 curve is a 256-bit elliptic curve over finite field Fp.
-
Method Summary
Methods Modifier and Type Method and Description abstract void
calculatePublicKey(EccAlg.CurvePoint publicKey)
Calculates the public key based on the private key scalar set in the instance and stores it into the provided publicKey.static EccAlg
create(short curveType)
Factory method for creating a concrete instance.abstract void
generateKeys()
Generates ECC public and private keys.abstract EccAlg.CurveProperties
getCurveProperties()
Returns the object that represents the properties of the instance's curve.abstract short
getCurveType()
Return the ECC curve type.abstract short
getHashAlg()
Obtains the hash algorithm which is currently used by this instance during signing operations.abstract void
getKeys(EccAlg.CurvePoint publicKey, byte[] privateKey, short privateKeyIndex)
Obtains the ECC public and private keys for this instance.abstract short
getPrivateKeySize()
Returns the ECC private key scalar length in bytes.abstract void
getPublicKey(EccAlg.CurvePoint publicKey)
Obtains the ECC public key for this instance.abstract short
getPublicKeySize()
Returns the ECC public key point coordinate length in bytes.abstract short
getSignatureSize()
Returns the signature length in bytes of the curve currently used in the instance.abstract void
setHashAlg(short hashAlgType)
Sets the hash algorithm to be used by this instance during signing operations.abstract void
setKeys(EccAlg.CurvePoint publicKey, byte[] privateKey, short privateKeyIndex, short privateKeyLength)
Sets the ECC public and private keys.abstract void
setPrivateKey(byte[] privateKey, short privateKeyIndex, short privateKeyLength)
Sets the ECC private key.abstract void
setPublicKey(EccAlg.CurvePoint publicKey)
Sets the ECC public key.abstract void
signComplete(byte[] data, short dataIndex, short dataLength, byte[] signatureR, short signatureRIndex, byte[] signatureS, short signatureSIndex)
Signs the provided input data using the key and hash algorithm currently stored by the instance.abstract void
signHash(byte[] hash, short hashIndex, short hashLength, byte[] signatureR, short signatureRIndex, byte[] signatureS, short signatureSIndex)
Signs the provided already hashed data using the key currently stored in the instance.abstract boolean
validatePublicKey(EccAlg.CurvePoint publicKey)
Validates the provided public key by verifying that it represents a legal point on the elliptic curve.abstract boolean
verifyComplete(byte[] data, short dataIndex, short dataLength, byte[] signatureR, short signatureRIndex, short signatureRLength, byte[] signatureS, short signatureSIndex, short signatureSLength)
Verifies the signature on the provided input data using the key and hash algorithm currently stored by the instance.abstract boolean
verifyHash(byte[] hash, short hashIndex, short hashLength, byte[] signatureR, short signatureRIndex, short signatureRLength, byte[] signatureS, short signatureSIndex, short signatureSLength)
Verifies the signature on the provided already hashed input data using the key currently stored in the instance.
-
-
-
Field Detail
-
ECC_CURVE_TYPE_SECP256K1
public static final short ECC_CURVE_TYPE_SECP256K1
The ECC secp256k1 curve is a 256-bit elliptic curve over finite field Fp.- See Also:
- Constant Field Values
-
-
Method Detail
-
generateKeys
public abstract void generateKeys() throws CryptoException
Generates ECC public and private keys. Keys are generated according to the curve type and are stored internally in the instance. Calling this method will override any other key (public or private) previously stored by this instance.- Throws:
ComputationException
- if the operation did not complete successfully due to errors returned from the crypto engineCryptoException
- if an unexpected error occurred
-
setPublicKey
public abstract void setPublicKey(EccAlg.CurvePoint publicKey) throws CryptoException
Sets the ECC public key. Calling this method will override any other public key previously stored by this instance.The key validity that it is a point on the elliptic curve is not checked, use thevalidatePublicKey()
method if needed.- Parameters:
publicKey
- the ECC public key. Should contain valid X and Y coordinates buffers with sizegetPublicKeySize()
- Throws:
IllegalParameterException
- if size ofpublicKey.x
orpublicKey.y
don't match the size returned bygetPublicKeySize()
or the provided input is illegalCryptoException
-
setPrivateKey
public abstract void setPrivateKey(byte[] privateKey, short privateKeyIndex, short privateKeyLength) throws CryptoException
Sets the ECC private key. Calling this method will override any other private key previously stored by this instance. The key validity is not checked.- Parameters:
privateKey
- the ECC private key scalarprivateKeyIndex
- index in the privateKey arrayprivateKeyLength
- private key length in bytes should be equal togetPrivateKeySize()
- Throws:
IllegalParameterException
- if the privateKeyLength doesn't match the size returned bygetPrivateKeySize()
or input is invalidCryptoException
-
setKeys
public abstract void setKeys(EccAlg.CurvePoint publicKey, byte[] privateKey, short privateKeyIndex, short privateKeyLength) throws CryptoException
Sets the ECC public and private keys. Calling this method will override any other key (public or private) previously stored by this instance. The key-pair validity is checked based on the elliptic curve.- Parameters:
publicKey
- the ECC public key. Should contain valid X and Y coordinates buffers with sizegetPublicKeySize()
privateKey
- the ECC private key scalarprivateKeyIndex
- index in the privateKey arrayprivateKeyLength
- private key length in bytes should be equal togetPrivateKeySize()
- Throws:
IllegalParameterException
- if the lengths ofpublicKey.x
orpublicKey.y
don't match the size returned bygetPublicKeySize()
or privateKeyLength doesn't match the size returned bygetPrivateKeySize()
or provided keys are invalidComputationException
- if the operation did not complete successfully due to errors returned from the crypto engineCryptoException
- if an unexpected error occurred
-
getCurveType
public abstract short getCurveType()
Return the ECC curve type.- Returns:
- returns the ECC curve type used by this instance.
-
getPublicKeySize
public abstract short getPublicKeySize()
Returns the ECC public key point coordinate length in bytes.- Returns:
- the ECC public key point coordinate according to the curve type used by this instance.
-
getPrivateKeySize
public abstract short getPrivateKeySize()
Returns the ECC private key scalar length in bytes.- Returns:
- the ECC private key length according to the curve type used by this instance.
-
getPublicKey
public abstract void getPublicKey(EccAlg.CurvePoint publicKey) throws CryptoException
Obtains the ECC public key for this instance. ThegetPublicKeySize()
method can be used to retrieve the required size forpublicKey.x
andpublicKey.y
coordinate buffers.- Parameters:
publicKey
- the ECC public key. should contain X coordinate and the Y coordinate buffers with sizegetPublicKeySize()
- Throws:
NotInitializedException
- if the public key was not set or generated for this instanceIllegalParameterException
- if the lengths ofpublicKey.x
orpublicKey.y
don't match the size returned bygetPublicKeySize()
or input is invalidCryptoException
-
getKeys
public abstract void getKeys(EccAlg.CurvePoint publicKey, byte[] privateKey, short privateKeyIndex) throws CryptoException
Obtains the ECC public and private keys for this instance. ThegetPublicKeySize()
method can be used to retrieve the required size forpublicKey.x
andpublicKey.y
coordinate buffers. ThegetPrivateKeySize()
can be used to retrieve the required size for theprivateKey
buffer.- Parameters:
publicKey
- the ECC public key. should contain X coordinate and the Y coordinate buffers with sizegetPublicKeySize()
privateKey
- an array to hold the ECC private key with the correct sizeprivateKeyIndex
- index in the private key array- Throws:
NotInitializedException
- if the keys were not set or generated for this instanceIllegalParameterException
- if the lengths ofpublicKey.x
orpublicKey.y
don't match the size returned bygetPublicKeySize()
or input is invalidCryptoException
-
signComplete
public abstract void signComplete(byte[] data, short dataIndex, short dataLength, byte[] signatureR, short signatureRIndex, byte[] signatureS, short signatureSIndex) throws CryptoException
Signs the provided input data using the key and hash algorithm currently stored by the instance. The methodgetSignatureSize()
provides the required size for the signatureR and signatureS buffers. The default hash algorithm is SHA-526HashAlg.HASH_TYPE_SHA256
.- Parameters:
data
- the plain data to signdataIndex
- index in the data arraydataLength
- data length in bytessignatureR
- the R signature buffer to hold the data must be at leastgetSignatureSize()
lengthsignatureRIndex
- index in the R signature buffersignatureS
- the S signature buffer to hold the data must be at leastgetSignatureSize()
lengthsignatureSIndex
- index in the S signature buffer- Throws:
NotInitializedException
- if this instance is not configured correctly in order to generate a signature. For example, if the private key or the hash algorithm were not set for this instanceComputationException
- if the operation did not complete successfully due to errors returned from the crypto engineCryptoException
- if an unexpected error occurredIllegalParameterException
- if the lengths ofsignatureR
orsignatureS
are less than the size returned bygetSignatureSize()
or input is invalid
-
verifyComplete
public abstract boolean verifyComplete(byte[] data, short dataIndex, short dataLength, byte[] signatureR, short signatureRIndex, short signatureRLength, byte[] signatureS, short signatureSIndex, short signatureSLength) throws CryptoException
Verifies the signature on the provided input data using the key and hash algorithm currently stored by the instance.- Parameters:
data
- The data to verifydataIndex
- index in the data arraydataLength
- data length in bytessignatureR
- The R signature buffer to verifysignatureRIndex
- index in the R signature buffersignatureRLength
- R signature buffer length in bytessignatureS
- The S signature buffer to verifysignatureSIndex
- index in the S signature buffersignatureSLength
- S signature buffer length in bytes- Throws:
NotInitializedException
- if this instance is not configured correctly in order to generate a signature. For example, if the public key or the hash algorithm were not set for this instanceIllegalParameterException
- signatureLength doesn't matchgetSignatureSize()
or input invalidComputationException
- if the operation did not complete successfully due to errors returned from the crypto engineCryptoException
- if an unexpected error occurred
-
signHash
public abstract void signHash(byte[] hash, short hashIndex, short hashLength, byte[] signatureR, short signatureRIndex, byte[] signatureS, short signatureSIndex) throws CryptoException
Signs the provided already hashed data using the key currently stored in the instance.- Parameters:
hash
- the input data to signhashIndex
- index in the input arrayhashLength
- input data length must be no larger thangetPrivateKeySize()
signatureR
- the R signature buffer to hold the data must be at leastgetSignatureSize()
lengthsignatureRIndex
- index in the R signature buffersignatureS
- the S signature buffer to hold the data must be at leastgetSignatureSize()
lengthsignatureSIndex
- index in the S signature buffer- Throws:
NotInitializedException
- if this instance is not configured correctly in order to generate a signature. For example, the private key or the public key is not set.IllegalParameterException
- if the lengths ofsignatureR
orsignatureS
are less than the size returned bygetSignatureSize()
or hash is bigger thangetPrivateKeySize()
or input is invalidComputationException
- if the operation did not complete successfully due to errors returned from the crypto engineCryptoException
- if an unexpected error occurred
-
verifyHash
public abstract boolean verifyHash(byte[] hash, short hashIndex, short hashLength, byte[] signatureR, short signatureRIndex, short signatureRLength, byte[] signatureS, short signatureSIndex, short signatureSLength) throws CryptoException
Verifies the signature on the provided already hashed input data using the key currently stored in the instance.- Parameters:
hash
- the hash that was signedhashIndex
- index in the hash arrayhashLength
- hash data lengthsignatureR
- The R signature buffer to verifysignatureRIndex
- index in the R signature buffersignatureRLength
- R signature buffer length in bytessignatureS
- The S signature buffer to verifysignatureSIndex
- index in the S signature buffersignatureSLength
- S signature buffer length in bytes- Returns:
true
if the signature verified successfully,false
otherwise.- Throws:
NotInitializedException
- if this instance is not configured correctly in order to verify the signature. For example, the public key.IllegalParameterException
- if hash doesn't match the size returned bygetPrivateKeySize()
or signatureLengths don't matchgetSignatureSize()
ComputationException
- if the operation did not complete successfully due to errors returned from the crypto engineCryptoException
- if an unexpected error occurred
-
validatePublicKey
public abstract boolean validatePublicKey(EccAlg.CurvePoint publicKey) throws CryptoException
Validates the provided public key by verifying that it represents a legal point on the elliptic curve.- Parameters:
publicKey
- the public key to verify.- Returns:
- return true iff publicKey is valid false otherwise.
- Throws:
IllegalParameterException
- if size ofpublicKey.x
orpublicKey.y
don't match the size returned bygetPublicKeySize()
or the provided input is illegalCryptoException
- if an unexpected error occurredComputationException
- if the operation did not complete successfully due to errors returned from the crypto engine
-
calculatePublicKey
public abstract void calculatePublicKey(EccAlg.CurvePoint publicKey) throws CryptoException
Calculates the public key based on the private key scalar set in the instance and stores it into the provided publicKey.- Parameters:
publicKey
- the output ECC public key. Should contain valid X and Y coordinates buffers with sizegetPublicKeySize()
- Throws:
NotInitializedException
- if this instance is not configured correctly in order to calculate the public key. For example, if the private key is not set for this instanceIllegalParameterException
- if size ofpublicKey.x
orpublicKey.y
don't match the size returned bygetPublicKeySize()
or the provided input is illegalCryptoException
- if an unexpected error occurredComputationException
- if the operation did not complete successfully due to errors returned from the crypto engine
-
getSignatureSize
public abstract short getSignatureSize()
Returns the signature length in bytes of the curve currently used in the instance. (Note: this size is for R and S)
-
setHashAlg
public abstract void setHashAlg(short hashAlgType) throws CryptoException
Sets the hash algorithm to be used by this instance during signing operations.- Parameters:
hashAlgType
- the hash algorithm that will be used during signing operations, seeHashAlg.HASH_TYPE_XXX
definitions for possible values- Throws:
NotSupportedException
- if the provided hashing algorithm is not supported.CryptoException
-
getHashAlg
public abstract short getHashAlg()
Obtains the hash algorithm which is currently used by this instance during signing operations.- Returns:
- The hash algorithm that is used during signing operations, see
HashAlg.HASH_TYPE_XXX
definitions for possible values
-
getCurveProperties
public abstract EccAlg.CurveProperties getCurveProperties()
Returns the object that represents the properties of the instance's curve.- Returns:
- a
EccAlg.CurveProperties
object that represents the curve's properties.
-
create
public static final EccAlg create(short curveType) throws CryptoException
Factory method for creating a concrete instance.- Parameters:
curveType
- the ECC curve type - one ofEccAlg.ECC_CURVE_TYPE_XXX
values- Returns:
EccAlg
instance.- Throws:
IllegalParameterException
- if the curveType is unsupportedCryptoException
-
-