1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/* EAX.java --
Copyright (C) 2004, 2006 Free Software Foundation, Inc.
This file is a part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.javax.crypto.mode;
import gnu.java.security.Registry;
import gnu.javax.crypto.cipher.IBlockCipher;
import gnu.javax.crypto.mac.IMac;
import gnu.javax.crypto.mac.MacFactory;
import java.security.InvalidKeyException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* A conventional two-pass authenticated-encrypted mode, EAX. EAX is a
* <i>Authenticated Encryption with Additional Data</i> (<b>AEAD</b>) scheme,
* which provides protection and authentication for the message, and provides
* authentication of an (optional) header. EAX is composed of the counter mode
* (CTR) and the one-key CBC MAC (OMAC).
* <p>
* This class makes full use of the {@link IAuthenticatedMode} interface, that
* is, all methods of both {@link IMode} and {@link IMac} can be used as
* specified in the {@link IAuthenticatedMode} interface.
* <p>
* References:
* <ol>
* <li>M. Bellare, P. Rogaway, and D. Wagner; <a
* href="http://www.cs.berkeley.edu/~daw/papers/eprint-short-ae.pdf">A
* Conventional Authenticated-Encryption Mode</a>.</li>
* </ol>
*/
public class EAX
implements IAuthenticatedMode
{
/** The tag size, in bytes. */
private int tagSize;
/** The nonce OMAC instance. */
private IMac nonceOmac;
/** The header OMAC instance. */
private IMac headerOmac;
/** The message OMAC instance. */
private IMac msgOmac;
/** The CTR instance. */
private IMode ctr;
/** The direction state (encrypting or decrypting). */
private int state;
/** Whether we're initialized or not. */
private boolean init;
/** The cipher block size. */
private int cipherBlockSize;
/** The cipher. */
private IBlockCipher cipher;
/** The [t]_n array. */
private byte[] t_n;
private static boolean valid = false;
public EAX(IBlockCipher cipher, int cipherBlockSize)
{
this.cipher = cipher;
this.cipherBlockSize = cipherBlockSize;
String name = cipher.name();
int i = name.indexOf('-');
if (i >= 0)
name = name.substring(0, i);
String omacname = Registry.OMAC_PREFIX + name;
nonceOmac = MacFactory.getInstance(omacname);
headerOmac = MacFactory.getInstance(omacname);
msgOmac = MacFactory.getInstance(omacname);
ctr = ModeFactory.getInstance(Registry.CTR_MODE, cipher, cipherBlockSize);
t_n = new byte[cipherBlockSize];
init = false;
}
public Object clone()
{
return new EAX((IBlockCipher) cipher.clone(), cipherBlockSize);
}
public String name()
{
return Registry.EAX_MODE + "(" + cipher.name() + ")";
}
public int defaultBlockSize()
{
return ctr.defaultBlockSize();
}
public int defaultKeySize()
{
return ctr.defaultKeySize();
}
public Iterator blockSizes()
{
return ctr.blockSizes();
}
public Iterator keySizes()
{
return ctr.keySizes();
}
public void init(Map attrib) throws InvalidKeyException
{
byte[] nonce = (byte[]) attrib.get(IV);
if (nonce == null)
throw new IllegalArgumentException("no nonce provided");
byte[] key = (byte[]) attrib.get(KEY_MATERIAL);
if (key == null)
throw new IllegalArgumentException("no key provided");
Arrays.fill(t_n, (byte) 0);
nonceOmac.reset();
nonceOmac.init(Collections.singletonMap(MAC_KEY_MATERIAL, key));
nonceOmac.update(t_n, 0, t_n.length);
nonceOmac.update(nonce, 0, nonce.length);
byte[] N = nonceOmac.digest();
nonceOmac.reset();
nonceOmac.update(t_n, 0, t_n.length);
nonceOmac.update(nonce, 0, nonce.length);
t_n[t_n.length - 1] = 1;
headerOmac.reset();
headerOmac.init(Collections.singletonMap(MAC_KEY_MATERIAL, key));
headerOmac.update(t_n, 0, t_n.length);
t_n[t_n.length - 1] = 2;
msgOmac.reset();
msgOmac.init(Collections.singletonMap(MAC_KEY_MATERIAL, key));
msgOmac.update(t_n, 0, t_n.length);
Integer modeSize = (Integer) attrib.get(MODE_BLOCK_SIZE);
if (modeSize == null)
modeSize = Integer.valueOf(cipherBlockSize);
HashMap ctrAttr = new HashMap();
ctrAttr.put(KEY_MATERIAL, key);
ctrAttr.put(IV, N);
ctrAttr.put(STATE, Integer.valueOf(ENCRYPTION));
ctrAttr.put(MODE_BLOCK_SIZE, modeSize);
ctr.reset();
ctr.init(ctrAttr);
Integer st = (Integer) attrib.get(STATE);
if (st != null)
{
state = st.intValue();
if (state != ENCRYPTION && state != DECRYPTION)
throw new IllegalArgumentException("invalid state");
}
else
state = ENCRYPTION;
Integer ts = (Integer) attrib.get(TRUNCATED_SIZE);
if (ts != null)
tagSize = ts.intValue();
else
tagSize = cipherBlockSize;
if (tagSize < 0 || tagSize > cipherBlockSize)
throw new IllegalArgumentException("tag size out of range");
init = true;
}
public int currentBlockSize()
{
return ctr.currentBlockSize();
}
public void encryptBlock(byte[] in, int inOff, byte[] out, int outOff)
{
if (! init)
throw new IllegalStateException("not initialized");
if (state != ENCRYPTION)
throw new IllegalStateException("not encrypting");
ctr.update(in, inOff, out, outOff);
msgOmac.update(out, outOff, ctr.currentBlockSize());
}
public void decryptBlock(byte[] in, int inOff, byte[] out, int outOff)
{
if (! init)
throw new IllegalStateException("not initialized");
if (state != DECRYPTION)
throw new IllegalStateException("not decrypting");
msgOmac.update(in, inOff, ctr.currentBlockSize());
ctr.update(in, inOff, out, outOff);
}
public void update(byte[] in, int inOff, byte[] out, int outOff)
{
switch (state)
{
case ENCRYPTION:
encryptBlock(in, inOff, out, outOff);
break;
case DECRYPTION:
decryptBlock(in, inOff, out, outOff);
break;
default:
throw new IllegalStateException("impossible state " + state);
}
}
public void reset()
{
nonceOmac.reset();
headerOmac.reset();
msgOmac.reset();
ctr.reset();
}
public boolean selfTest()
{
return true; // XXX
}
public int macSize()
{
return tagSize;
}
public byte[] digest()
{
byte[] tag = new byte[tagSize];
digest(tag, 0);
return tag;
}
public void digest(byte[] out, int outOffset)
{
if (outOffset < 0 || outOffset + tagSize > out.length)
throw new IndexOutOfBoundsException();
byte[] N = nonceOmac.digest();
byte[] H = headerOmac.digest();
byte[] M = msgOmac.digest();
for (int i = 0; i < tagSize; i++)
out[outOffset + i] = (byte)(N[i] ^ H[i] ^ M[i]);
reset();
}
public void update(byte b)
{
if (! init)
throw new IllegalStateException("not initialized");
headerOmac.update(b);
}
public void update(byte[] buf, int off, int len)
{
if (! init)
throw new IllegalStateException("not initialized");
headerOmac.update(buf, off, len);
}
}
|