1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48:
49:
53: public abstract class BaseKeyAgreementParty
54: implements IKeyAgreementParty
55: {
56: protected static final BigInteger TWO = BigInteger.valueOf(2L);
57:
58: protected String name;
59:
60: protected boolean initialised = false;
61:
62: protected int step = -1;
63:
64: protected boolean complete = false;
65:
66: protected SecureRandom rnd = null;
67:
68: protected IRandom irnd = null;
69:
70: private PRNG prng = null;
71:
72: protected BaseKeyAgreementParty(String name)
73: {
74: super();
75:
76: this.name = name;
77: }
78:
79: public String name()
80: {
81: return name;
82: }
83:
84: public void init(Map attributes) throws KeyAgreementException
85: {
86: if (initialised)
87: throw new IllegalStateException("already initialised");
88: this.engineInit(attributes);
89: initialised = true;
90: this.step = -1;
91: this.complete = false;
92: }
93:
94: public OutgoingMessage processMessage(IncomingMessage in)
95: throws KeyAgreementException
96: {
97: if (! initialised)
98: throw new IllegalStateException("not initialised");
99: if (complete)
100: throw new IllegalStateException("exchange has already concluded");
101: step++;
102: return this.engineProcessMessage(in);
103: }
104:
105: public boolean isComplete()
106: {
107: return complete;
108: }
109:
110: public byte[] getSharedSecret() throws KeyAgreementException
111: {
112: if (! initialised)
113: throw new KeyAgreementException("not yet initialised");
114: if (! isComplete())
115: throw new KeyAgreementException("not yet computed");
116: return engineSharedSecret();
117: }
118:
119: public void reset()
120: {
121: if (initialised)
122: {
123: this.engineReset();
124: initialised = false;
125: }
126: }
127:
128: protected abstract void engineInit(Map attributes)
129: throws KeyAgreementException;
130:
131: protected abstract OutgoingMessage engineProcessMessage(IncomingMessage in)
132: throws KeyAgreementException;
133:
134: protected abstract byte[] engineSharedSecret() throws KeyAgreementException;
135:
136: protected abstract void engineReset();
137:
138:
143: protected void nextRandomBytes(byte[] buffer)
144: {
145: if (rnd != null)
146: rnd.nextBytes(buffer);
147: else if (irnd != null)
148: try
149: {
150: irnd.nextBytes(buffer, 0, buffer.length);
151: }
152: catch (LimitReachedException lre)
153: {
154: irnd = null;
155: getDefaultPRNG().nextBytes(buffer);
156: }
157: else
158: getDefaultPRNG().nextBytes(buffer);
159: }
160:
161: private PRNG getDefaultPRNG()
162: {
163: if (prng == null)
164: prng = PRNG.getInstance();
165:
166: return prng;
167: }
168: }