OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [security/] [sig/] [BaseSignature.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* BaseSignature.java --
2
   Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
3
 
4
This file is a part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or (at
9
your option) any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19
USA
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version.  */
37
 
38
 
39
package gnu.java.security.sig;
40
 
41
import gnu.java.security.hash.IMessageDigest;
42
import gnu.java.security.prng.IRandom;
43
import gnu.java.security.prng.LimitReachedException;
44
import gnu.java.security.util.PRNG;
45
 
46
import java.security.PrivateKey;
47
import java.security.PublicKey;
48
import java.util.Map;
49
import java.util.Random;
50
 
51
/**
52
 * A base abstract class to facilitate implementations of concrete Signatures.
53
 */
54
public abstract class BaseSignature
55
    implements ISignature
56
{
57
  /** The canonical name of this signature scheme. */
58
  protected String schemeName;
59
 
60
  /** The underlying message digest instance for this signature scheme. */
61
  protected IMessageDigest md;
62
 
63
  /** The public key to use when verifying signatures. */
64
  protected PublicKey publicKey;
65
 
66
  /** The private key to use when generating signatures (signing). */
67
  protected PrivateKey privateKey;
68
 
69
  /** The optional {@link Random} instance to use. */
70
  private Random rnd;
71
 
72
  /** The optional {@link IRandom} instance to use. */
73
  private IRandom irnd;
74
 
75
  /** Our default source of randomness. */
76
  private PRNG prng = null;
77
 
78
  /**
79
   * Trivial constructor.
80
   *
81
   * @param schemeName the name of this signature scheme.
82
   * @param md the underlying instance of the message digest algorithm.
83
   * @throws IllegalArgumentException if the designated hash instance is
84
   *           <code>null</code>.
85
   */
86
  protected BaseSignature(String schemeName, IMessageDigest md)
87
  {
88
    super();
89
 
90
    this.schemeName = schemeName;
91
    if (md == null)
92
      throw new IllegalArgumentException("Message digest MUST NOT be null");
93
 
94
    this.md = md;
95
  }
96
 
97
  public String name()
98
  {
99
    return schemeName + "-" + md.name();
100
  }
101
 
102
  public void setupVerify(Map attributes) throws IllegalArgumentException
103
  {
104
    setup(attributes);
105
    // do we have a public key?
106
    PublicKey key = (PublicKey) attributes.get(VERIFIER_KEY);
107
    if (key != null)
108
      setupForVerification(key);
109
  }
110
 
111
  public void setupSign(Map attributes) throws IllegalArgumentException
112
  {
113
    setup(attributes);
114
    // do we have a private key?
115
    PrivateKey key = (PrivateKey) attributes.get(SIGNER_KEY);
116
    if (key != null)
117
      setupForSigning(key);
118
  }
119
 
120
  public void update(byte b)
121
  {
122
    if (md == null)
123
      throw new IllegalStateException();
124
 
125
    md.update(b);
126
  }
127
 
128
  public void update(byte[] b, int off, int len)
129
  {
130
    if (md == null)
131
      throw new IllegalStateException();
132
 
133
    md.update(b, off, len);
134
  }
135
 
136
  public Object sign()
137
  {
138
    if (md == null || privateKey == null)
139
      throw new IllegalStateException();
140
 
141
    return generateSignature();
142
  }
143
 
144
  public boolean verify(Object sig)
145
  {
146
    if (md == null || publicKey == null)
147
      throw new IllegalStateException();
148
 
149
    return verifySignature(sig);
150
  }
151
 
152
  public abstract Object clone();
153
 
154
  protected abstract void setupForVerification(PublicKey key)
155
      throws IllegalArgumentException;
156
 
157
  protected abstract void setupForSigning(PrivateKey key)
158
      throws IllegalArgumentException;
159
 
160
  protected abstract Object generateSignature() throws IllegalStateException;
161
 
162
  protected abstract boolean verifySignature(Object signature)
163
      throws IllegalStateException;
164
 
165
  /** Initialises the internal fields of this instance. */
166
  protected void init()
167
  {
168
    md.reset();
169
    rnd = null;
170
    irnd = null;
171
    publicKey = null;
172
    privateKey = null;
173
  }
174
 
175
  /**
176
   * Fills the designated byte array with random data.
177
   *
178
   * @param buffer the byte array to fill with random data.
179
   */
180
  protected void nextRandomBytes(byte[] buffer)
181
  {
182
    if (rnd != null)
183
      rnd.nextBytes(buffer);
184
    else if (irnd != null)
185
      try
186
        {
187
          irnd.nextBytes(buffer, 0, buffer.length);
188
        }
189
      catch (IllegalStateException x)
190
        {
191
          throw new RuntimeException("nextRandomBytes(): " + x);
192
        }
193
      catch (LimitReachedException x)
194
        {
195
          throw new RuntimeException("nextRandomBytes(): " + x);
196
        }
197
    else
198
      getDefaultPRNG().nextBytes(buffer);
199
  }
200
 
201
  private void setup(Map attributes)
202
  {
203
    init();
204
    // do we have a Random or SecureRandom, or should we use our own?
205
    Object obj = attributes.get(SOURCE_OF_RANDOMNESS);
206
    if (obj instanceof Random)
207
      rnd = (Random) obj;
208
    else if (obj instanceof IRandom)
209
      irnd = (IRandom) obj;
210
  }
211
 
212
  private PRNG getDefaultPRNG()
213
  {
214
    if (prng == null)
215
      prng = PRNG.getInstance();
216
 
217
    return prng;
218
  }
219
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.