iaik.security.rsa
Class RSASignature

java.lang.Object
  |
  +--java.security.SignatureSpi
        |
        +--java.security.Signature
              |
              +--iaik.security.rsa.RSAPkcs1Signature
                    |
                    +--iaik.security.rsa.RSASignature
Direct Known Subclasses:
Md2RSASignature, Md5RSASignature, RawRSAPkcs1v15Signature, RipeMd128RSASignature, RipeMd160RSASignature, Sha256RSASignature, Sha384RSASignature, Sha512RSASignature, ShaRSASignature

public abstract class RSASignature
extends iaik.security.rsa.RSAPkcs1Signature

This class implements the sign and verify methods of the PKCS#1v1.5 signature scheme (RSASSA-PKCS1-v1_5).

Digital signatures are used for ensuring data authentication and data integrity. RSA PKCS1v1.5 based signature algorithms use a proper hash function (like MD5 or SHA) for creating a message digest of the message to be signed. Subsequently this message digest is wrapped in a DigestInfo and then 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 recipient 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 (v.2.1)) for implementing a PKCS#1v1.5 (RSASSA-PKCS1-v1_5) signature algorithm based on the RSA encryption method.

An application wishing to sign some message or to verify some signature, generally has to perform three steps (in the following example, HASH has to be replaced by the name of the desired hash algorithm):

Version:
File Revision 10
See Also:
Md2RSASignature, Md5RSASignature, ShaRSASignature, Sha256RSASignature, Sha384RSASignature, Sha512RSASignature, RipeMd128RSASignature, RipeMd160RSASignature, RawRSAPkcs1v15Signature, RawRSASignature, Signature

Field Summary
protected  MessageDigest hash
          The MessageDigest engine used to hash the data; supplied with an instance of the desired MessageDigest algorithm by any extending subclass.
 
Fields inherited from class java.security.Signature
SIGN, state, UNINITIALIZED, VERIFY
 
Fields inherited from class java.security.SignatureSpi
appRandom
 
Constructor Summary
protected RSASignature(AlgorithmID hashAlgorithm, MessageDigest hash)
          Creates a RSA PKCS#1v1.5 Signature object with a specified hash algorithm.
protected RSASignature(String name, MessageDigest hash)
          Creates a RSA PKCS#1v1.5 Signature object with the given name using the specified hash algorithm.
 
Method Summary
protected  Object engineGetParameter(String param)
          This method is not used 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 engineInitSign(PrivateKey pk, SecureRandom random)
          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(AlgorithmParameterSpec params)
          Allows to supply a SecureRandom object if required by the underlying signature scheme (e.g.
protected  void engineSetParameter(String param, Object value)
          Allows to supply a SecureRandom object if required by the underlying signature scheme (e.g.
protected  byte[] engineSign()
          SPI: Calculates the signature.
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#1v1.5.
 
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
engineSign
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

hash

protected MessageDigest hash
The MessageDigest engine used to hash the data; supplied with an instance of the desired MessageDigest algorithm by any extending subclass.
Constructor Detail

RSASignature

protected RSASignature(AlgorithmID hashAlgorithm,
                       MessageDigest hash)
Creates a RSA PKCS#1v1.5 Signature object with a specified hash algorithm.

This constructor is called from subclasses to represent a RSA PKCS#1v1.5 signature with a defined hash algorithm like Md2, Md5 or Sha.

Applications use

 Signature.getInstance("HASHwithRSA");
 
for creating a Signature object, where HASH has to be replaced by the name of the desired hash algorithm.
Parameters:
hashAlgorithm - the algorithm id of the hash algorithm to use
hash - the MessageDigest engine to be used for hashing the data
See Also:
Signature.getInstance(java.lang.String)

RSASignature

protected RSASignature(String name,
                       MessageDigest hash)
Creates a RSA PKCS#1v1.5 Signature object with the given name using the specified hash algorithm.

This constructor is called from subclasses to represent a RSA PKCS#1v1.5 signature with a defined hash algorithm like Md2, Md5 or Sha.

Applications use

 Signature.getInstance("HASHwithRSA");
 
for creating a Signature object, where HASH has to be replaced by the name of the desired hash algorithm.
Parameters:
name - the name of the siganture algorithm
hash - the MessageDigest engine to be used for hashing the data
See Also:
Signature.getInstance(java.lang.String)
Method Detail

engineSign

protected byte[] engineSign()
                     throws SignatureException
SPI: Calculates the signature.
Overrides:
engineSign in class SignatureSpi
Returns:
a byte array holding the signature value calculated on the data that has been supplied when updating this engine
Throws:
SignatureException - if an error occurs when creating the signature

engineVerify

protected boolean engineVerify(byte[] sigBytes)
                        throws SignatureException
Verifies the given signature of a message according to PKCS#1v1.5.

PKCS#1v1.5 defines a signature as bit string, which has to be converted into an octet string, RSA decrypted with the signer´s RSA public key, and DER decoded for giving an ASN.1 DigestInfo. From this DigestInfo the message digest has to be taken to compare it with the message digest derived from hashing the original message with the same message digest algorithm.

Please notice that the 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.

Overrides:
engineVerify in class SignatureSpi
Parameters:
sigBytes - the signature bytes to be verified
Returns:
true if signature is OK, false otherwise
Throws:
SignatureException - if an error occurs when verifying the signature

engineInitVerify

protected void engineInitVerify(PublicKey pk)
                         throws InvalidKeyException
SPI: Initializes this Signature object with the given RSA public key for performing a signature verification.
Overrides:
engineInitVerify in class SignatureSpi
Parameters:
publicKey - the RSA public key belonging to the RSA private key that has been used for signing.
Throws:
InvalidKeyException - if a key encoding error occurs

engineInitSign

protected void engineInitSign(PrivateKey pk)
                       throws InvalidKeyException
SPI: Initializes this Signature object with the given RSA private key for going to sign some data.
Overrides:
engineInitSign in class SignatureSpi
Parameters:
privateKey - the RSA private key to be used for signing.
Throws:
InvalidKeyException - if a key encoding error occurs

engineInitSign

protected void engineInitSign(PrivateKey pk,
                              SecureRandom random)
                       throws InvalidKeyException
SPI: Initializes this Signature object with the given RSA private key for going to sign some data. The supplied SecureRandom may be used by the signature engine if random numbers are required (e.g. PSS).

Note that this method is not available for JDK versions prior JDK 1.2. When using JDK 1.1 a SecureRandom object may be supplied as parameter by calling method setParameter. If a SecureRandom never has been supplied by the application, the signature engine will use a default SecureRandom, if required.

Overrides:
engineInitSign in class SignatureSpi
Parameters:
privateKey - the RSA private key to be used for signing.
SecureRandom - the SecureRandom if random numbers are required by the signature engine (e.g. PSS)
Throws:
InvalidKeyException - if a key encoding error occurs

engineUpdate

protected void engineUpdate(byte b)
SPI: Updates the data to be signed or verified with the specified byte.
Overrides:
engineUpdate in class SignatureSpi
Parameters:
b - the byte to be used for updating.

engineUpdate

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.
Overrides:
engineUpdate in class SignatureSpi
Parameters:
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.

engineSetParameter

protected void engineSetParameter(String param,
                                  Object value)
                           throws InvalidParameterException
Allows to supply a SecureRandom object if required by the underlying signature scheme (e.g. PSS). When using JDK versions prior JDK 1.2 method initSign(PrivateKey, SecureRandom) is not available. If required by the underlying signature scheme (e.g. PSS) an application may supply a SecureRandom object as parameter. If a SecureRandom never has been supplied by the application, the signature engine will use a default SecureRandom, if required.
Overrides:
engineSetParameter in class SignatureSpi
Parameters:
param - ignored
value - the SecureRandom supplied as PKCS1AlgorithmParameterSpec
Throws:
InvalidParameterException - if the SecureRandom is not supplied as PKCS1AlgorithmParameterSpec

engineSetParameter

protected void engineSetParameter(AlgorithmParameterSpec params)
                           throws InvalidAlgorithmParameterException
Allows to supply a SecureRandom object if required by the underlying signature scheme (e.g. PSS). JDK 1.2 (or later) based applications may prefer to use method initSign(PrivateKey, SecureRandom) to supply a SecureRandom object if required. If a SecureRandom never has been supplied by the application, the signature engine will use a default SecureRandom, if required.
Overrides:
engineSetParameter in class SignatureSpi
Parameters:
params - the SecureRandom supplied as PKCS1AlgorithmParameterSpec
Throws:
InvalidParameterException - if the SecureRandom is not supplied as PKCS1AlgorithmParameterSpec

engineGetParameter

protected Object engineGetParameter(String param)
                             throws InvalidParameterException
This method is not used and only throws an InvalidParameterException.
Overrides:
engineGetParameter in class SignatureSpi
Throws:
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).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK