001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.crypto.AsymmetricBlockCipher;
004: import org.bouncycastle.crypto.CipherParameters;
005: import org.bouncycastle.crypto.Digest;
006: import org.bouncycastle.crypto.digests.MD5Digest;
007: import org.bouncycastle.crypto.digests.RIPEMD160Digest;
008: import org.bouncycastle.crypto.digests.SHA1Digest;
009: import org.bouncycastle.crypto.engines.RSABlindedEngine;
010: import org.bouncycastle.crypto.signers.ISO9796d2Signer;
011:
012: import java.security.InvalidKeyException;
013: import java.security.PrivateKey;
014: import java.security.PublicKey;
015: import java.security.Signature;
016: import java.security.SignatureException;
017: import java.security.interfaces.RSAPrivateKey;
018: import java.security.interfaces.RSAPublicKey;
019: import java.security.spec.AlgorithmParameterSpec;
020:
021: public class JDKISOSignature extends Signature {
022: private ISO9796d2Signer signer;
023:
024: protected JDKISOSignature(String name, Digest digest,
025: AsymmetricBlockCipher cipher) {
026: super (name);
027:
028: signer = new ISO9796d2Signer(cipher, digest, true);
029: }
030:
031: protected void engineInitVerify(PublicKey publicKey)
032: throws InvalidKeyException {
033: CipherParameters param = RSAUtil
034: .generatePublicKeyParameter((RSAPublicKey) publicKey);
035:
036: signer.init(false, param);
037: }
038:
039: protected void engineInitSign(PrivateKey privateKey)
040: throws InvalidKeyException {
041: CipherParameters param = RSAUtil
042: .generatePrivateKeyParameter((RSAPrivateKey) privateKey);
043:
044: signer.init(true, param);
045: }
046:
047: protected void engineUpdate(byte b) throws SignatureException {
048: signer.update(b);
049: }
050:
051: protected void engineUpdate(byte[] b, int off, int len)
052: throws SignatureException {
053: signer.update(b, off, len);
054: }
055:
056: protected byte[] engineSign() throws SignatureException {
057: try {
058: byte[] sig = signer.generateSignature();
059:
060: return sig;
061: } catch (Exception e) {
062: throw new SignatureException(e.toString());
063: }
064: }
065:
066: protected boolean engineVerify(byte[] sigBytes)
067: throws SignatureException {
068: boolean yes = signer.verifySignature(sigBytes);
069:
070: return yes;
071: }
072:
073: protected void engineSetParameter(AlgorithmParameterSpec params) {
074: throw new UnsupportedOperationException(
075: "engineSetParameter unsupported");
076: }
077:
078: /**
079: * @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
080: */
081: protected void engineSetParameter(String param, Object value) {
082: throw new UnsupportedOperationException(
083: "engineSetParameter unsupported");
084: }
085:
086: /**
087: * @deprecated
088: */
089: protected Object engineGetParameter(String param) {
090: throw new UnsupportedOperationException(
091: "engineSetParameter unsupported");
092: }
093:
094: static public class SHA1WithRSAEncryption extends JDKISOSignature {
095: public SHA1WithRSAEncryption() {
096: super ("SHA1withRSA/ISO9796-2", new SHA1Digest(),
097: new RSABlindedEngine());
098: }
099: }
100:
101: static public class MD5WithRSAEncryption extends JDKISOSignature {
102: public MD5WithRSAEncryption() {
103: super ("MD5withRSA/ISO9796-2", new MD5Digest(),
104: new RSABlindedEngine());
105: }
106: }
107:
108: static public class RIPEMD160WithRSAEncryption extends
109: JDKISOSignature {
110: public RIPEMD160WithRSAEncryption() {
111: super ("RIPEMD160withRSA/ISO9796-2", new RIPEMD160Digest(),
112: new RSABlindedEngine());
113: }
114: }
115: }
|