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 MD5 Signatures.
031: */
032: public final class RsaMd5Sig extends Signature {
033: /**
034: * Expected prefix in the decrypted result when MD5 hashing is used
035: * with RSA signing. This prefix is followed by the MD5 hash.
036: * If you are interested, more details are in the comments around
037: * the verify method in X509Certificate.
038: */
039: private static final byte[] PREFIX_MD5 = { (byte) 0x30,
040: (byte) 0x20, (byte) 0x30, (byte) 0x0c, (byte) 0x06,
041: (byte) 0x08, (byte) 0x2a, (byte) 0x86, (byte) 0x48,
042: (byte) 0x86, (byte) 0xf7, (byte) 0x0d, (byte) 0x02,
043: (byte) 0x05, (byte) 0x05, (byte) 0x00, (byte) 0x04,
044: (byte) 0x10 };
045:
046: /** Common signature class. */
047: RSASig rsaSig;
048:
049: /**
050: * Constructs an RSA signature object that uses MD5 as
051: * message digest algorithm.
052: *
053: * @exception RuntimeException if MD5 is not available
054: */
055: public RsaMd5Sig() {
056: try {
057: rsaSig = new RSASig(PREFIX_MD5, MessageDigest
058: .getInstance("MD5"));
059: } catch (NoSuchAlgorithmException e) {
060: throw new RuntimeException("Needed algorithm not available");
061: }
062: }
063:
064: /**
065: * Gets the signature algorithm.
066: *
067: * @return the algorithmimplemented by this signature object
068: */
069: public String getAlgorithm() {
070: return "MD5withRSA";
071: }
072:
073: /**
074: * Gets the byte-length of the signature.
075: *
076: * @return the byte-length of the signature produced by this object
077: */
078: public int getLength() {
079: return rsaSig.getLength();
080: }
081:
082: /**
083: * Initializes the <CODE>RSASig</CODE> object with the appropriate
084: * <CODE>Key</CODE> for signature verification.
085: *
086: * @param theKey the key object to use for verification
087: *
088: * @exception InvalidKeyException if the key type is inconsistent
089: * with the mode or signature implementation.
090: */
091: public void initVerify(PublicKey theKey) throws InvalidKeyException {
092: rsaSig.initVerify(theKey);
093: }
094:
095: /**
096: * Initializes the <CODE>RSASig</CODE> object with the appropriate
097: * <CODE>Key</CODE> for signature creation.
098: *
099: * @param theKey the key object to use for signing
100: *
101: * @exception InvalidKeyException if the key type is inconsistent
102: * with the mode or signature implementation.
103: */
104: public void initSign(PrivateKey theKey) throws InvalidKeyException {
105: rsaSig.initSign(theKey);
106: }
107:
108: /**
109: * Accumulates a signature of the input data. When this method is used,
110: * temporary storage of intermediate results is required. This method
111: * should only be used if all the input data required for the signature
112: * is not available in one byte array. The sign() or verify() method is
113: * recommended whenever possible.
114: *
115: * @param inBuf the input buffer of data to be signed
116: * @param inOff starting offset within the input buffer for data to
117: * be signed
118: * @param inLen the byte length of data to be signed
119: *
120: * @exception SignatureException
121: * if the signature algorithm does not pad the message and the
122: * message is not block aligned
123: *
124: * @see #verify(byte[], int, int, byte[], int, short)
125: */
126: public void update(byte[] inBuf, int inOff, int inLen)
127: throws SignatureException {
128:
129: rsaSig.update(inBuf, inOff, inLen);
130: }
131:
132: /**
133: * Generates the signature of all/last input data. A call to this
134: * method also resets this signature object to the state it was in
135: * when previously initialized via a call to init(). That is, the
136: * object is reset and available to sign another message.
137: *
138: * @param sigBuf the output buffer to store signature data
139: * @param sigOff starting offset within the output buffer at which
140: * to begin signature data
141: * @param sigLen max length the signature can be
142: *
143: * @return number of bytes of signature output in sigBuf
144: *
145: * @exception SignatureException
146: * if the signature algorithm does not pad the message and the
147: * message is not block aligned
148: */
149: public int sign(byte[] sigBuf, int sigOff, int sigLen)
150: throws SignatureException {
151:
152: return rsaSig.sign(sigBuf, sigOff, sigLen);
153: }
154:
155: /**
156: * Verifies the signature of all/last input data against the passed
157: * in signature. A call to this method also resets this signature
158: * object to the state it was in when previously initialized via a
159: * call to init(). That is, the object is reset and available to
160: * verify another message.
161: *
162: * @param sigBuf the input buffer containing signature data
163: * @param sigOff starting offset within the sigBuf where signature
164: * data begins
165: * @param sigLen byte length of signature data
166: *
167: * @return true if signature verifies, false otherwise
168: *
169: * @exception SignatureException
170: * if the signature algorithm does not pad the message and the
171: * message is not block aligned
172: */
173: public boolean verify(byte[] sigBuf, int sigOff, int sigLen)
174: throws SignatureException {
175:
176: return rsaSig.verify(sigBuf, sigOff, sigLen);
177: }
178: }
|