anon.crypto
Class MyDSASignature

java.lang.Object
  extended by anon.crypto.MyDSASignature
All Implemented Interfaces:
IMySignature, ISignatureCreationAlgorithm, ISignatureVerificationAlgorithm

public final class MyDSASignature
extends java.lang.Object
implements IMySignature

Implements the DSA algorithm for signatures.


Field Summary
private  org.bouncycastle.crypto.digests.SHA1Digest m_Digest
           
private  java.security.Key m_initKey
          The key with that this algorithm has been initialised.
private  org.bouncycastle.crypto.signers.DSASigner m_SignatureAlgorithm
           
private static org.bouncycastle.asn1.x509.AlgorithmIdentifier ms_identifier
           
 
Constructor Summary
MyDSASignature()
           
 
Method Summary
 byte[] decodeForXMLSignature(byte[] a_encodedSignature)
          Tries to decode a signature in a way as it would meet the W3C standard for DSA XML signature values.
(package private) static java.math.BigInteger[] derDecode(byte[] encoding, int off, int len)
           
(package private) static byte[] derEncode(java.math.BigInteger r, java.math.BigInteger s)
           
 byte[] encodeForXMLSignature(byte[] a_signature)
          Encodes a signature in a way it meets the W3C standard for DSA XML signature values.
 org.bouncycastle.asn1.x509.AlgorithmIdentifier getIdentifier()
          Returns the algorithm identifier (DSA with SHA1).
 java.lang.String getXMLSignatureAlgorithmReference()
          Returns http://www.w3.org/2000/09/xmldsig#dsa-sha1.
 void initSign(IMyPrivateKey k)
          Initialises the algorithm for signing.
 void initVerify(IMyPublicKey k)
          Initialises the algorithm for verifying.
 byte[] sign(byte[] bytesToSign)
          Signs a message and returns the signature.
 boolean verify(byte[] a_message, byte[] a_signature)
          Tests if the signature of a specified message is valid.
 boolean verify(byte[] a_message, int message_offset, int message_len, byte[] a_signature, int signature_offset, int signature_len)
          Tests if the signature of a specified message is valid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ms_identifier

private static final org.bouncycastle.asn1.x509.AlgorithmIdentifier ms_identifier

m_SignatureAlgorithm

private org.bouncycastle.crypto.signers.DSASigner m_SignatureAlgorithm

m_Digest

private org.bouncycastle.crypto.digests.SHA1Digest m_Digest

m_initKey

private java.security.Key m_initKey
The key with that this algorithm has been initialised.

Constructor Detail

MyDSASignature

public MyDSASignature()
Method Detail

initVerify

public void initVerify(IMyPublicKey k)
                throws java.security.InvalidKeyException
Description copied from interface: IMySignature
Initialises the algorithm for verifying. This must be done before doing the verify operation. The general contract of this method is that it must check if the algorithm has previously been initialised with the given key. If yes the method does nothing to save resources.

Specified by:
initVerify in interface IMySignature
Parameters:
k - a public key
Throws:
java.security.InvalidKeyException - if the key is invalid

initSign

public void initSign(IMyPrivateKey k)
              throws java.security.InvalidKeyException
Description copied from interface: IMySignature
Initialises the algorithm for signing. This must be done before doing the sign operation. The general contract of this method is that it must check if the algorithm has previously been initialised with the given key. If yes the method does nothing to save resources.

Specified by:
initSign in interface IMySignature
Parameters:
k - a private key
Throws:
java.security.InvalidKeyException - if the key is invalid

verify

public boolean verify(byte[] a_message,
                      int message_offset,
                      int message_len,
                      byte[] a_signature,
                      int signature_offset,
                      int signature_len)
Description copied from interface: ISignatureVerificationAlgorithm
Tests if the signature of a specified message is valid.

Specified by:
verify in interface ISignatureVerificationAlgorithm
Parameters:
a_message - a message
message_offset - start of message
message_len - length of message
a_signature - a signature
signature_offset - start of signature
signature_len - length of signature
Returns:
true if the signature of a specified message is valid; false otherwiese

verify

public boolean verify(byte[] a_message,
                      byte[] a_signature)
Description copied from interface: IMySignature
Tests if the signature of a specified message is valid.

Specified by:
verify in interface IMySignature
Specified by:
verify in interface ISignatureVerificationAlgorithm
Parameters:
a_message - a message
a_signature - a signature
Returns:
true if the signature of a specified message is valid; false otherwiese

sign

public byte[] sign(byte[] bytesToSign)
Description copied from interface: IMySignature
Signs a message and returns the signature.

Specified by:
sign in interface IMySignature
Specified by:
sign in interface ISignatureCreationAlgorithm
Parameters:
bytesToSign - a message
Returns:
the signature that was created

getIdentifier

public org.bouncycastle.asn1.x509.AlgorithmIdentifier getIdentifier()
Returns the algorithm identifier (DSA with SHA1).

Specified by:
getIdentifier in interface ISignatureCreationAlgorithm
Specified by:
getIdentifier in interface ISignatureVerificationAlgorithm
Returns:
the algorithm identifier (DSA with SHA1)

encodeForXMLSignature

public byte[] encodeForXMLSignature(byte[] a_signature)
Encodes a signature in a way it meets the W3C standard for DSA XML signature values. Extracts the ASN.1 encoded values for r and s from a DER encoded byte array. ASN.1 Notation: sequence { integer r integer s } --> Der-Encoding byte 0x30 // Sequence byte 44 + x // len in bytes (x = {0|1|2} depending on r and s byte 0x02 // integer byte <= 21 // len of r (21: if first bit of r set, we need a leading 0 --> 20 + 1 bytes) byte[] ... // value of r (with leading zero if necessary) byte 0x02 // integer byte <= 21 // len of s (21: if first bit of s set, we need a leading 0 --> 20 + 1 bytes) byte[] ... // value of s (with leading zero if necessary)

Specified by:
encodeForXMLSignature in interface IMySignature
Specified by:
encodeForXMLSignature in interface ISignatureCreationAlgorithm
Parameters:
a_signature - an non-encoded signature in DER format
Returns:
the encoded signature in R-S-encoding or null if an error occured
See Also:
http://www.w3.org/TR/xmldsig-core/#sec-DSA

decodeForXMLSignature

public byte[] decodeForXMLSignature(byte[] a_encodedSignature)
Tries to decode a signature in a way as it would meet the W3C standard for DSA XML signature values. Making DER-Encoding of r and s..... ASN.1 Notation: sequence { integer r integer s } HINT: Sun JDK 1.4.x needs a leading '0' in the binary representation of r (and s) if r[0]>0x7F or s[0]>0x7F --> Der-Encoding 0x30 //Sequence 44 + x // len in bytes (x = {0|1|2} depending on r and s (see above) 0x02 // integer 20 | 21 // len in bytes of r .... //value of r (with leading zero if necessary) 0x02 //integer 20 | 21 //len of s ... value of s (with leading zero if necessary)

Specified by:
decodeForXMLSignature in interface IMySignature
Specified by:
decodeForXMLSignature in interface ISignatureVerificationAlgorithm
Parameters:
a_encodedSignature - an encoded signature in R-S format
Returns:
the decoded signature in DER format or null if an error occured
See Also:
http://www.w3.org/TR/xmldsig-core/#sec-DSA

getXMLSignatureAlgorithmReference

public java.lang.String getXMLSignatureAlgorithmReference()
Returns http://www.w3.org/2000/09/xmldsig#dsa-sha1.

Specified by:
getXMLSignatureAlgorithmReference in interface IMySignature
Specified by:
getXMLSignatureAlgorithmReference in interface ISignatureCreationAlgorithm
Specified by:
getXMLSignatureAlgorithmReference in interface ISignatureVerificationAlgorithm
Returns:
http://www.w3.org/2000/09/xmldsig#dsa-sha1
See Also:
http://www.w3.org/TR/xmldsig-core/#sec-AlgID

derEncode

static byte[] derEncode(java.math.BigInteger r,
                        java.math.BigInteger s)
                 throws java.io.IOException
Throws:
java.io.IOException

derDecode

static java.math.BigInteger[] derDecode(byte[] encoding,
                                        int off,
                                        int len)
                                 throws java.io.IOException
Throws:
java.io.IOException