~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to jdk1.4/org/bouncycastle/jce/provider/JDKPSSSigner.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.jce.provider;
 
2
 
 
3
import java.security.AlgorithmParameters;
 
4
import java.security.InvalidKeyException;
 
5
import java.security.InvalidParameterException;
 
6
import java.security.PrivateKey;
 
7
import java.security.PublicKey;
 
8
import java.security.SecureRandom;
 
9
import java.security.Signature;
 
10
import java.security.SignatureException;
 
11
import java.security.interfaces.RSAPrivateKey;
 
12
import java.security.interfaces.RSAPublicKey;
 
13
import java.security.spec.AlgorithmParameterSpec;
 
14
import java.security.spec.PSSParameterSpec;
 
15
 
 
16
import org.bouncycastle.crypto.AsymmetricBlockCipher;
 
17
import org.bouncycastle.crypto.CryptoException;
 
18
import org.bouncycastle.crypto.Digest;
 
19
import org.bouncycastle.crypto.digests.SHA1Digest;
 
20
import org.bouncycastle.crypto.digests.SHA224Digest;
 
21
import org.bouncycastle.crypto.digests.SHA256Digest;
 
22
import org.bouncycastle.crypto.digests.SHA384Digest;
 
23
import org.bouncycastle.crypto.digests.SHA512Digest;
 
24
import org.bouncycastle.crypto.engines.RSABlindedEngine;
 
25
import org.bouncycastle.crypto.params.ParametersWithRandom;
 
26
import org.bouncycastle.crypto.signers.PSSSigner;
 
27
 
 
28
public class JDKPSSSigner
 
29
    extends Signature
 
30
{
 
31
    private AsymmetricBlockCipher signer;
 
32
    private Digest digest;
 
33
    private int saltLength;
 
34
    private AlgorithmParameters engineParams;
 
35
    private PSSSigner pss;
 
36
 
 
37
    protected JDKPSSSigner(
 
38
        String name,
 
39
        AsymmetricBlockCipher signer,
 
40
        Digest digest)
 
41
    {
 
42
        super(name);
 
43
 
 
44
        this.signer = signer;
 
45
        this.digest = digest;
 
46
        if (digest != null)
 
47
        {
 
48
            this.saltLength = digest.getDigestSize();
 
49
        }
 
50
        else
 
51
        {
 
52
            this.saltLength = 20;
 
53
        }
 
54
    }
 
55
 
 
56
    protected void engineInitVerify(
 
57
        PublicKey   publicKey)
 
58
        throws InvalidKeyException
 
59
    {
 
60
        if (!(publicKey instanceof RSAPublicKey))
 
61
        {
 
62
            throw new InvalidKeyException("Supplied key is not a RSAPublicKey instance");
 
63
        }
 
64
 
 
65
        pss = new PSSSigner(signer, digest, saltLength);
 
66
        pss.init(false,
 
67
            RSAUtil.generatePublicKeyParameter((RSAPublicKey)publicKey));
 
68
    }
 
69
 
 
70
    protected void engineInitSign(
 
71
        PrivateKey      privateKey,
 
72
        SecureRandom    random)
 
73
        throws InvalidKeyException
 
74
    {
 
75
        if (!(privateKey instanceof RSAPrivateKey))
 
76
        {
 
77
            throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
 
78
        }
 
79
 
 
80
        pss = new PSSSigner(signer, digest, saltLength);
 
81
        pss.init(true, new ParametersWithRandom(RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey), random));
 
82
    }
 
83
 
 
84
    protected void engineInitSign(
 
85
        PrivateKey  privateKey)
 
86
        throws InvalidKeyException
 
87
    {
 
88
        if (!(privateKey instanceof RSAPrivateKey))
 
89
        {
 
90
            throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
 
91
        }
 
92
 
 
93
        pss = new PSSSigner(signer, digest, saltLength);
 
94
        pss.init(true, RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey));
 
95
    }
 
96
 
 
97
    protected void engineUpdate(
 
98
        byte    b)
 
99
        throws SignatureException
 
100
    {
 
101
        pss.update(b);
 
102
    }
 
103
 
 
104
    protected void engineUpdate(
 
105
        byte[]  b,
 
106
        int     off,
 
107
        int     len) 
 
108
        throws SignatureException
 
109
    {
 
110
        pss.update(b, off, len);
 
111
    }
 
112
 
 
113
    protected byte[] engineSign()
 
114
        throws SignatureException
 
115
    {
 
116
        try
 
117
        {
 
118
            return pss.generateSignature();
 
119
        }
 
120
        catch (CryptoException e)
 
121
        {
 
122
            throw new SignatureException(e.getMessage());
 
123
        }
 
124
    }
 
125
 
 
126
    protected boolean engineVerify(
 
127
        byte[]  sigBytes) 
 
128
        throws SignatureException
 
129
    {
 
130
        return pss.verifySignature(sigBytes);
 
131
    }
 
132
 
 
133
    protected void engineSetParameter(
 
134
        AlgorithmParameterSpec params)
 
135
        throws InvalidParameterException
 
136
    {
 
137
        if (params instanceof PSSParameterSpec)
 
138
        {
 
139
            saltLength = ((PSSParameterSpec)params).getSaltLength();
 
140
        }
 
141
        else
 
142
        {
 
143
            throw new InvalidParameterException("Only PSSParameterSpec supported");
 
144
        }
 
145
    }
 
146
    
 
147
    protected AlgorithmParameters engineGetParameters() 
 
148
    {
 
149
        if (engineParams == null)
 
150
        {
 
151
            try
 
152
            {
 
153
                engineParams = AlgorithmParameters.getInstance("PSS", "BC");
 
154
                engineParams.init(new PSSParameterSpec(saltLength));
 
155
            }
 
156
            catch (Exception e)
 
157
            {
 
158
                throw new RuntimeException(e.toString());
 
159
            }
 
160
        }
 
161
 
 
162
        return engineParams;
 
163
    }
 
164
 
 
165
    /**
 
166
     * @deprecated replaced with <a href = "#engineSetParameter(java.security.spec.AlgorithmParameterSpec)">
 
167
     */
 
168
    protected void engineSetParameter(
 
169
        String  param,
 
170
        Object  value)
 
171
    {
 
172
        throw new UnsupportedOperationException("engineSetParameter unsupported");
 
173
    }
 
174
    
 
175
    protected Object engineGetParameter(
 
176
        String param)
 
177
    {
 
178
        throw new UnsupportedOperationException("engineGetParameter unsupported");
 
179
    }
 
180
 
 
181
    static public class PSSwithRSA
 
182
        extends JDKPSSSigner
 
183
    {
 
184
        public PSSwithRSA()
 
185
        {
 
186
            super("SHA1withRSAandMGF1", new RSABlindedEngine(), null);
 
187
        }
 
188
    }
 
189
 
 
190
    static public class SHA1withRSA
 
191
        extends JDKPSSSigner
 
192
    {
 
193
        public SHA1withRSA()
 
194
        {
 
195
            super("SHA1withRSAandMGF1", new RSABlindedEngine(), new SHA1Digest());
 
196
        }
 
197
    }
 
198
 
 
199
    static public class SHA224withRSA
 
200
        extends JDKPSSSigner
 
201
    {
 
202
        public SHA224withRSA()
 
203
        {
 
204
            super("SHA224withRSAandMGF1", new RSABlindedEngine(), new SHA224Digest());
 
205
        }
 
206
    }
 
207
 
 
208
    static public class SHA256withRSA
 
209
        extends JDKPSSSigner
 
210
    {
 
211
        public SHA256withRSA()
 
212
        {
 
213
            super("SHA256withRSAandMGF1", new RSABlindedEngine(), new SHA256Digest());
 
214
        }
 
215
    }
 
216
 
 
217
    static public class SHA384withRSA
 
218
        extends JDKPSSSigner
 
219
    {
 
220
        public SHA384withRSA()
 
221
        {
 
222
            super("SHA384withRSAandMGF1", new RSABlindedEngine(), new SHA384Digest());
 
223
        }
 
224
    }
 
225
 
 
226
    static public class SHA512withRSA
 
227
        extends JDKPSSSigner
 
228
    {
 
229
        public SHA512withRSA()
 
230
        {
 
231
            super("SHA512withRSAandMGF1", new RSABlindedEngine(), new SHA512Digest());
 
232
        }
 
233
    }
 
234
}