001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.crypto;
028:
029: /**
030: * Implements RSA SHA1 Signatures.
031: */
032: public final class RsaShaSig extends Signature {
033: /**
034: * Expected prefix in decrypted value when SHA-1 hash is used
035: * with RSA signing. This prefix is followed by the SHA hash.
036: */
037: private static final byte[] PREFIX_SHA1 = { (byte) 0x30,
038: (byte) 0x21, (byte) 0x30, (byte) 0x09, (byte) 0x06,
039: (byte) 0x05, (byte) 0x2b, (byte) 0x0e, (byte) 0x03,
040: (byte) 0x02, (byte) 0x1a, (byte) 0x05, (byte) 0x00,
041: (byte) 0x04, (byte) 0x14 };
042:
043: /** Common signature class. */
044: RSASig rsaSig;
045:
046: /**
047: * Constructs an RSA signature object that uses SHA1 as
048: * message digest algorithm.
049: *
050: * @exception RuntimeException if SHA-1 is not available
051: */
052: public RsaShaSig() {
053: try {
054: rsaSig = new RSASig(PREFIX_SHA1, MessageDigest
055: .getInstance("SHA-1"));
056: } catch (NoSuchAlgorithmException e) {
057: throw new RuntimeException("Needed algorithm not available");
058: }
059: }
060:
061: /**
062: * Gets the signature algorithm.
063: *
064: * @return the algorithmimplemented by this signature object
065: */
066: public String getAlgorithm() {
067: return "SHA1withRSA";
068: }
069:
070: /**
071: * Gets the byte-length of the signature.
072: *
073: * @return the byte-length of the signature produced by this object
074: */
075: public int getLength() {
076: return rsaSig.getLength();
077: }
078:
079: /**
080: * Initializes the <CODE>RSASig</CODE> object with the appropriate
081: * <CODE>Key</CODE> for signature verification.
082: *
083: * @param theKey the key object to use for verification
084: *
085: * @exception InvalidKeyException if the key type is inconsistent
086: * with the mode or signature implementation.
087: */
088: public void initVerify(PublicKey theKey) throws InvalidKeyException {
089: rsaSig.initVerify(theKey);
090: }
091:
092: /**
093: * Initializes the <CODE>RSASig</CODE> object with the appropriate
094: * <CODE>Key</CODE> for signature creation.
095: *
096: * @param theKey the key object to use for signing
097: *
098: * @exception InvalidKeyException if the key type is inconsistent
099: * with the mode or signature implementation.
100: */
101: public void initSign(PrivateKey theKey) throws InvalidKeyException {
102: rsaSig.initSign(theKey);
103: }
104:
105: /**
106: * Accumulates a signature of the input data. When this method is used,
107: * temporary storage of intermediate results is required. This method
108: * should only be used if all the input data required for the signature
109: * is not available in one byte array. The sign() or verify() method is
110: * recommended whenever possible.
111: *
112: * @param inBuf the input buffer of data to be signed
113: * @param inOff starting offset within the input buffer for data to
114: * be signed
115: * @param inLen the byte length of data to be signed
116: *
117: * @exception SignatureException
118: * if the signature algorithm does not pad the message and the
119: * message is not block aligned
120: * @see #verify(byte[], int, int, byte[], int, short)
121: */
122: public void update(byte[] inBuf, int inOff, int inLen)
123: throws SignatureException {
124:
125: rsaSig.update(inBuf, inOff, inLen);
126: }
127:
128: /**
129: * Generates the signature of all/last input data. A call to this
130: * method also resets this signature object to the state it was in
131: * when previously initialized via a call to init(). That is, the
132: * object is reset and available to sign another message.
133: *
134: * @param sigBuf the output buffer to store signature data
135: * @param sigOff starting offset within the output buffer at which
136: * to begin signature data
137: * @param sigLen max length the signature can be
138: *
139: * @return number of bytes of signature output in sigBuf
140: *
141: * @exception SignatureException
142: * if the signature algorithm does not pad the message and the
143: * message is not block aligned
144: */
145: public int sign(byte[] sigBuf, int sigOff, int sigLen)
146: throws SignatureException {
147:
148: return rsaSig.sign(sigBuf, sigOff, sigLen);
149: }
150:
151: /**
152: * Verifies the signature of all/last input data against the passed
153: * in signature. A call to this method also resets this signature
154: * object to the state it was in when previously initialized via a
155: * call to init(). That is, the object is reset and available to
156: * verify another message.
157: *
158: * @param sigBuf the input buffer containing signature data
159: * @param sigOff starting offset within the sigBuf where signature
160: * data begins
161: * @param sigLen byte length of signature data
162: *
163: * @return true if signature verifies, false otherwise
164: *
165: * @exception SignatureException
166: * if the signature algorithm does not pad the message and the
167: * message is not block aligned
168: */
169: public boolean verify(byte[] sigBuf, int sigOff, int sigLen)
170: throws SignatureException {
171:
172: return rsaSig.verify(sigBuf, sigOff, sigLen);
173: }
174: }
|