|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.security.SignatureSpi | +--java.security.Signature | +--iaik.security.rsa.RawRSASignature
This class represents a "raw" implemention of the RSA PKCS#1v1.5 digital signature algorithm (RSASSA-PKCS1-v1_5). Hashing and building a digest info object must be done by the application.
Digital signatures are used for ensuring data authentication and data integrity. RSA based signature algorithms use a proper hash function (like MD2, MD5 or SHA) for creating a message digest of the message to be signed. This class requires that the application does the hasing. Subsequently this message digest is encrypted with the RSA private key of the entity going to sign the message. Message and encrypted message digest together are sent to the intended receiver that verifies the signature by decrypting the received encrypted message digest with the corresponding RSA public key, and comparing it with the hash value derived from the received original message after hashing it with the same hash function as used by the entity that has sent the message.
This class follows the guidelines presented in PKCS#1) for implementing a signature algorithm based on the RSA encryption method.
An application wishing to sign some message or to verify some signature, generally
has to perform four steps (in the following example, HASH
has to be
replaced by the name of the desired hash algorithm):
AlgorithmID hashAlgorithm = AlgorithmID.HASH; // e.g. AlgorithmID.sha1 MessageDigest hashEngine = MessageDigest.getInstance(hashAlgorithm.getImplementationName()); byte[] rawHash = hashEngine.digest(data); byte[] preparedHash = new DigestInfo(hashAlgorithm, rawHash).toByteArray();
getInstance
method, e.g.:
Signature rawRsaSignatureEngine = Signature.getInstance("RSA", "IAIK");
rawRsaSignatureEngine.initSign(rsaPrivateKey);
rawRsaSignatureEngine.initVerify(rsaPublicKey);
sign
method returning the signature as byte array.
Otherwise, if the Signature object has been initialized for verifying, first the
prepared hash to be verified is supplied to the Signature object, and subsequently the
signature is verified by calling the verify
method, supplied with
the byte array holding the corresponding signature value:
rawRsaSignatureEngine.update(preparedHash); byte[] signature = rawRsaSignatureEngine.sign();
rawRsaSignatureEngine.update(preparedHash); System.out.println("Signature " + (rawRsaSignatureEngine.verify(signature) ? "correct!" : "incorrect!"));
Signature
Field Summary | |
protected ByteArrayOutputStream |
dataBuffer_
Data buffer to which the DigestInfo is written when supplied via an update method. |
Fields inherited from class java.security.Signature |
SIGN, state, UNINITIALIZED, VERIFY |
Fields inherited from class java.security.SignatureSpi |
appRandom |
Constructor Summary | |
RawRSASignature()
Creates a RSA Signature object. |
Method Summary | |
protected Object |
engineGetParameter(String param)
This method is not implemented and only throws an InvalidParameterException |
protected void |
engineInitSign(PrivateKey pk)
SPI: Initializes this Signature object with the given RSA private key for going to sign some data. |
protected void |
engineInitVerify(PublicKey pk)
SPI: Initializes this Signature object with the given RSA public key for performing a signature verification. |
protected void |
engineSetParameter(String param,
Object value)
This method is not implemented and only throws an InvalidParameterException |
protected byte[] |
engineSign()
SPI: Returns a byte array holding the signature resulting from all already performed prepared hash update operations. |
protected void |
engineUpdate(byte b)
SPI: Updates the data to be signed or verified with the specified byte. |
protected void |
engineUpdate(byte[] b,
int off,
int len)
SPI: Updates the data to be signed or verified with the specified number of bytes, beginning at the specified offset within the given byte array. |
protected boolean |
engineVerify(byte[] sigBytes)
Verifies the given signature of a message according to PKCS#1. |
Methods inherited from class java.security.Signature |
clone, getAlgorithm, getInstance, getInstance, getParameter, getProvider, initSign, initSign, initVerify, initVerify, setParameter, setParameter, sign, sign, toString, update, update, update, verify |
Methods inherited from class java.security.SignatureSpi |
engineInitSign, engineSetParameter, engineSign |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected ByteArrayOutputStream dataBuffer_
Constructor Detail |
public RawRSASignature()
Applications use
for creating a Signature object.Signature.getInstance("RSA");
Signature.getInstance(java.lang.String)
Method Detail |
protected void engineInitVerify(PublicKey pk) throws InvalidKeyException
engineInitVerify
in class SignatureSpi
publicKey
- the RSA public key belonging to the RSA private key that has been used for signing.InvalidKeyException
- if a key encoding error occursprotected void engineInitSign(PrivateKey pk) throws InvalidKeyException
engineInitSign
in class SignatureSpi
privateKey
- the RSA private key to be used for signing.InvalidKeyException
- if a key encoding error occursprotected byte[] engineSign() throws SignatureException
engineSign
in class SignatureSpi
SignatureException
- if an error occurs when creating the signatureprotected boolean engineVerify(byte[] sigBytes) throws SignatureException
PKCS#1
defines a signature as bit string, which has to be converted into an octet
string, RSA decrypted with the signer´s RSA public key
giving the prepared hash, which is normally an DER encoded
DigestInfo
object.
This prepared hash is compared to the prepared hash provided by the
applciation. If they are equal, the verification succeeded and
true
is returned, otherwise false
is
returned.
Please notice that first step of bit-string-to-octet-string conversion already has to be done when calling this verify method. In this way the supplied sigBytes value has to be the octet string signature value.
engineVerify
in class SignatureSpi
sigBytes
- the signature bytes to be verifiedtrue
if signature is OK, false
otherwiseSignatureException
- if an error occurs when verifying the signatureprotected void engineUpdate(byte b)
engineUpdate
in class SignatureSpi
b
- the byte to be used for updating.protected void engineUpdate(byte[] b, int off, int len)
engineUpdate
in class SignatureSpi
b
- the byte array holding the data to be used for this update operation.off
- the offset, indicating the start position within the given byte array.len
- the number of bytes to be obtained from the given byte array, starting at the given position.protected void engineSetParameter(String param, Object value) throws InvalidParameterException
engineSetParameter
in class SignatureSpi
InvalidParameterException
- This Method is not supportedprotected Object engineGetParameter(String param) throws InvalidParameterException
engineGetParameter
in class SignatureSpi
InvalidParameterException
- This Method is not supported
|
This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |