1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50:
51:
54: public abstract class BaseSignature
55: implements ISignature
56: {
57:
58: protected String schemeName;
59:
60:
61: protected IMessageDigest md;
62:
63:
64: protected PublicKey publicKey;
65:
66:
67: protected PrivateKey privateKey;
68:
69:
70: private Random rnd;
71:
72:
73: private IRandom irnd;
74:
75:
76: private PRNG prng = null;
77:
78:
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:
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:
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:
166: protected void init()
167: {
168: md.reset();
169: rnd = null;
170: irnd = null;
171: publicKey = null;
172: privateKey = null;
173: }
174:
175:
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:
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: }