15008
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
16748
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
15008
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.crypto.provider;
|
|
27 |
|
|
28 |
import java.util.Arrays;
|
|
29 |
import java.io.*;
|
|
30 |
import java.security.*;
|
|
31 |
import javax.crypto.*;
|
|
32 |
import static com.sun.crypto.provider.AESConstants.AES_BLOCK_SIZE;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* This class represents ciphers in GaloisCounter (GCM) mode.
|
|
36 |
*
|
|
37 |
* <p>This mode currently should only be used w/ AES cipher.
|
|
38 |
* Although no checking is done here, caller should only
|
|
39 |
* pass AES Cipher to the constructor.
|
|
40 |
*
|
|
41 |
* <p>NOTE: This class does not deal with buffering or padding.
|
|
42 |
*
|
|
43 |
* @since 1.8
|
|
44 |
*/
|
|
45 |
final class GaloisCounterMode extends FeedbackCipher {
|
|
46 |
|
|
47 |
static int DEFAULT_TAG_LEN = AES_BLOCK_SIZE;
|
|
48 |
static int DEFAULT_IV_LEN = 12; // in bytes
|
|
49 |
|
|
50 |
// buffer for AAD data; if null, meaning update has been called
|
|
51 |
private ByteArrayOutputStream aadBuffer = new ByteArrayOutputStream();
|
|
52 |
private int sizeOfAAD = 0;
|
|
53 |
|
|
54 |
// in bytes; need to convert to bits (default value 128) when needed
|
|
55 |
private int tagLenBytes = DEFAULT_TAG_LEN;
|
|
56 |
|
|
57 |
// these following 2 fields can only be initialized after init() is
|
|
58 |
// called, e.g. after cipher key k is set, and STAY UNCHANGED
|
|
59 |
private byte[] subkeyH = null;
|
|
60 |
private byte[] preCounterBlock = null;
|
|
61 |
|
|
62 |
private GCTR gctrPAndC = null;
|
|
63 |
private GHASH ghashAllToS = null;
|
|
64 |
|
|
65 |
// length of total data, i.e. len(C)
|
|
66 |
private int processed = 0;
|
|
67 |
|
|
68 |
// additional variables for save/restore calls
|
|
69 |
private byte[] aadBufferSave = null;
|
|
70 |
private int sizeOfAADSave = 0;
|
|
71 |
private int processedSave = 0;
|
|
72 |
|
|
73 |
// value must be 16-byte long; used by GCTR and GHASH as well
|
|
74 |
static void increment32(byte[] value) {
|
|
75 |
if (value.length != AES_BLOCK_SIZE) {
|
|
76 |
throw new RuntimeException("Unexpected counter block length");
|
|
77 |
}
|
|
78 |
// start from last byte and only go over 4 bytes, i.e. total 32 bits
|
|
79 |
int n = value.length - 1;
|
|
80 |
while ((n >= value.length - 4) && (++value[n] == 0)) {
|
|
81 |
n--;
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
// ivLen in bits
|
|
86 |
private static byte[] getLengthBlock(int ivLen) {
|
|
87 |
byte[] out = new byte[AES_BLOCK_SIZE];
|
|
88 |
out[12] = (byte)(ivLen >>> 24);
|
|
89 |
out[13] = (byte)(ivLen >>> 16);
|
|
90 |
out[14] = (byte)(ivLen >>> 8);
|
|
91 |
out[15] = (byte)ivLen;
|
|
92 |
return out;
|
|
93 |
}
|
|
94 |
|
|
95 |
// aLen and cLen both in bits
|
|
96 |
private static byte[] getLengthBlock(int aLen, int cLen) {
|
|
97 |
byte[] out = new byte[AES_BLOCK_SIZE];
|
|
98 |
out[4] = (byte)(aLen >>> 24);
|
|
99 |
out[5] = (byte)(aLen >>> 16);
|
|
100 |
out[6] = (byte)(aLen >>> 8);
|
|
101 |
out[7] = (byte)aLen;
|
|
102 |
out[12] = (byte)(cLen >>> 24);
|
|
103 |
out[13] = (byte)(cLen >>> 16);
|
|
104 |
out[14] = (byte)(cLen >>> 8);
|
|
105 |
out[15] = (byte)cLen;
|
|
106 |
return out;
|
|
107 |
}
|
|
108 |
|
|
109 |
private static byte[] expandToOneBlock(byte[] in, int inOfs, int len) {
|
|
110 |
if (len > AES_BLOCK_SIZE) {
|
|
111 |
throw new ProviderException("input " + len + " too long");
|
|
112 |
}
|
|
113 |
if (len == AES_BLOCK_SIZE && inOfs == 0) {
|
|
114 |
return in;
|
|
115 |
} else {
|
|
116 |
byte[] paddedIn = new byte[AES_BLOCK_SIZE];
|
|
117 |
System.arraycopy(in, inOfs, paddedIn, 0, len);
|
|
118 |
return paddedIn;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
private static byte[] getJ0(byte[] iv, byte[] subkeyH) {
|
|
123 |
byte[] j0;
|
|
124 |
if (iv.length == 12) { // 96 bits
|
|
125 |
j0 = expandToOneBlock(iv, 0, iv.length);
|
|
126 |
j0[AES_BLOCK_SIZE - 1] = 1;
|
|
127 |
} else {
|
|
128 |
GHASH g = new GHASH(subkeyH);
|
|
129 |
int lastLen = iv.length % AES_BLOCK_SIZE;
|
|
130 |
if (lastLen != 0) {
|
|
131 |
g.update(iv, 0, iv.length - lastLen);
|
|
132 |
byte[] padded =
|
|
133 |
expandToOneBlock(iv, iv.length - lastLen, lastLen);
|
|
134 |
g.update(padded);
|
|
135 |
} else {
|
|
136 |
g.update(iv);
|
|
137 |
}
|
|
138 |
byte[] lengthBlock = getLengthBlock(iv.length*8);
|
|
139 |
g.update(lengthBlock);
|
|
140 |
j0 = g.digest();
|
|
141 |
}
|
|
142 |
return j0;
|
|
143 |
}
|
|
144 |
|
|
145 |
GaloisCounterMode(SymmetricCipher embeddedCipher) {
|
|
146 |
super(embeddedCipher);
|
|
147 |
aadBuffer = new ByteArrayOutputStream();
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Gets the name of the feedback mechanism
|
|
152 |
*
|
|
153 |
* @return the name of the feedback mechanism
|
|
154 |
*/
|
|
155 |
String getFeedback() {
|
|
156 |
return "GCM";
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Resets the cipher object to its original state.
|
|
161 |
* This is used when doFinal is called in the Cipher class, so that the
|
|
162 |
* cipher can be reused (with its original key and iv).
|
|
163 |
*/
|
|
164 |
void reset() {
|
|
165 |
if (aadBuffer == null) {
|
|
166 |
aadBuffer = new ByteArrayOutputStream();
|
|
167 |
} else {
|
|
168 |
aadBuffer.reset();
|
|
169 |
}
|
|
170 |
if (gctrPAndC != null) gctrPAndC.reset();
|
|
171 |
if (ghashAllToS != null) ghashAllToS.reset();
|
|
172 |
processed = 0;
|
|
173 |
sizeOfAAD = 0;
|
|
174 |
}
|
|
175 |
|
|
176 |
/**
|
|
177 |
* Save the current content of this cipher.
|
|
178 |
*/
|
|
179 |
void save() {
|
|
180 |
processedSave = processed;
|
|
181 |
sizeOfAADSave = sizeOfAAD;
|
|
182 |
aadBufferSave =
|
|
183 |
((aadBuffer == null || aadBuffer.size() == 0)?
|
|
184 |
null : aadBuffer.toByteArray());
|
|
185 |
if (gctrPAndC != null) gctrPAndC.save();
|
|
186 |
if (ghashAllToS != null) ghashAllToS.save();
|
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Restores the content of this cipher to the previous saved one.
|
|
191 |
*/
|
|
192 |
void restore() {
|
|
193 |
processed = processedSave;
|
|
194 |
sizeOfAAD = sizeOfAADSave;
|
|
195 |
if (aadBuffer != null) {
|
|
196 |
aadBuffer.reset();
|
|
197 |
if (aadBufferSave != null) {
|
|
198 |
aadBuffer.write(aadBufferSave, 0, aadBufferSave.length);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
if (gctrPAndC != null) gctrPAndC.restore();
|
|
202 |
if (ghashAllToS != null) ghashAllToS.restore();
|
|
203 |
}
|
|
204 |
|
|
205 |
/**
|
|
206 |
* Initializes the cipher in the specified mode with the given key
|
|
207 |
* and iv.
|
|
208 |
*
|
|
209 |
* @param decrypting flag indicating encryption or decryption
|
|
210 |
* @param algorithm the algorithm name
|
|
211 |
* @param key the key
|
|
212 |
* @param iv the iv
|
|
213 |
* @param tagLenBytes the length of tag in bytes
|
|
214 |
*
|
|
215 |
* @exception InvalidKeyException if the given key is inappropriate for
|
|
216 |
* initializing this cipher
|
|
217 |
*/
|
|
218 |
void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
|
|
219 |
throws InvalidKeyException {
|
|
220 |
init(decrypting, algorithm, key, iv, DEFAULT_TAG_LEN);
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Initializes the cipher in the specified mode with the given key
|
|
225 |
* and iv.
|
|
226 |
*
|
|
227 |
* @param decrypting flag indicating encryption or decryption
|
|
228 |
* @param algorithm the algorithm name
|
|
229 |
* @param key the key
|
|
230 |
* @param iv the iv
|
|
231 |
* @param tagLenBytes the length of tag in bytes
|
|
232 |
*
|
|
233 |
* @exception InvalidKeyException if the given key is inappropriate for
|
|
234 |
* initializing this cipher
|
|
235 |
*/
|
|
236 |
void init(boolean decrypting, String algorithm, byte[] keyValue,
|
|
237 |
byte[] ivValue, int tagLenBytes)
|
|
238 |
throws InvalidKeyException {
|
|
239 |
if (keyValue == null || ivValue == null) {
|
|
240 |
throw new InvalidKeyException("Internal error");
|
|
241 |
}
|
|
242 |
|
|
243 |
// always encrypt mode for embedded cipher
|
|
244 |
this.embeddedCipher.init(false, algorithm, keyValue);
|
|
245 |
this.subkeyH = new byte[AES_BLOCK_SIZE];
|
|
246 |
this.embeddedCipher.encryptBlock(new byte[AES_BLOCK_SIZE], 0,
|
|
247 |
this.subkeyH, 0);
|
|
248 |
|
|
249 |
this.iv = ivValue.clone();
|
|
250 |
preCounterBlock = getJ0(iv, subkeyH);
|
|
251 |
byte[] j0Plus1 = preCounterBlock.clone();
|
|
252 |
increment32(j0Plus1);
|
|
253 |
gctrPAndC = new GCTR(embeddedCipher, j0Plus1);
|
|
254 |
ghashAllToS = new GHASH(subkeyH);
|
|
255 |
|
|
256 |
this.tagLenBytes = tagLenBytes;
|
|
257 |
if (aadBuffer == null) {
|
|
258 |
aadBuffer = new ByteArrayOutputStream();
|
|
259 |
} else {
|
|
260 |
aadBuffer.reset();
|
|
261 |
}
|
|
262 |
processed = 0;
|
|
263 |
sizeOfAAD = 0;
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Continues a multi-part update of the Additional Authentication
|
|
268 |
* Data (AAD), using a subset of the provided buffer. If this
|
|
269 |
* cipher is operating in either GCM or CCM mode, all AAD must be
|
|
270 |
* supplied before beginning operations on the ciphertext (via the
|
|
271 |
* {@code update} and {@code doFinal} methods).
|
|
272 |
* <p>
|
|
273 |
* NOTE: Given most modes do not accept AAD, default impl for this
|
|
274 |
* method throws IllegalStateException.
|
|
275 |
*
|
|
276 |
* @param src the buffer containing the AAD
|
|
277 |
* @param offset the offset in {@code src} where the AAD input starts
|
|
278 |
* @param len the number of AAD bytes
|
|
279 |
*
|
|
280 |
* @throws IllegalStateException if this cipher is in a wrong state
|
|
281 |
* (e.g., has not been initialized), does not accept AAD, or if
|
|
282 |
* operating in either GCM or CCM mode and one of the {@code update}
|
|
283 |
* methods has already been called for the active
|
|
284 |
* encryption/decryption operation
|
|
285 |
* @throws UnsupportedOperationException if this method
|
|
286 |
* has not been overridden by an implementation
|
|
287 |
*
|
|
288 |
* @since 1.8
|
|
289 |
*/
|
|
290 |
void updateAAD(byte[] src, int offset, int len) {
|
|
291 |
if (aadBuffer != null) {
|
|
292 |
aadBuffer.write(src, offset, len);
|
|
293 |
} else {
|
|
294 |
// update has already been called
|
|
295 |
throw new IllegalStateException
|
|
296 |
("Update has been called; no more AAD data");
|
|
297 |
}
|
|
298 |
}
|
|
299 |
|
|
300 |
// Feed the AAD data to GHASH, pad if necessary
|
|
301 |
void processAAD() {
|
|
302 |
if (aadBuffer != null) {
|
|
303 |
byte[] aad = aadBuffer.toByteArray();
|
|
304 |
sizeOfAAD = aad.length;
|
|
305 |
aadBuffer = null;
|
|
306 |
|
|
307 |
int lastLen = aad.length % AES_BLOCK_SIZE;
|
|
308 |
if (lastLen != 0) {
|
|
309 |
ghashAllToS.update(aad, 0, aad.length - lastLen);
|
|
310 |
byte[] padded = expandToOneBlock(aad, aad.length - lastLen,
|
|
311 |
lastLen);
|
|
312 |
ghashAllToS.update(padded);
|
|
313 |
} else {
|
|
314 |
ghashAllToS.update(aad);
|
|
315 |
}
|
|
316 |
}
|
|
317 |
}
|
|
318 |
|
|
319 |
// Utility to process the last block; used by encryptFinal and decryptFinal
|
|
320 |
void doLastBlock(byte[] in, int inOfs, int len, byte[] out, int outOfs,
|
|
321 |
boolean isEncrypt) throws IllegalBlockSizeException {
|
|
322 |
// process data in 'in'
|
|
323 |
gctrPAndC.doFinal(in, inOfs, len, out, outOfs);
|
|
324 |
processed += len;
|
|
325 |
|
|
326 |
byte[] ct;
|
|
327 |
int ctOfs;
|
|
328 |
if (isEncrypt) {
|
|
329 |
ct = out;
|
|
330 |
ctOfs = outOfs;
|
|
331 |
} else {
|
|
332 |
ct = in;
|
|
333 |
ctOfs = inOfs;
|
|
334 |
}
|
|
335 |
int lastLen = len % AES_BLOCK_SIZE;
|
|
336 |
if (lastLen != 0) {
|
|
337 |
ghashAllToS.update(ct, ctOfs, len - lastLen);
|
|
338 |
byte[] padded =
|
|
339 |
expandToOneBlock(ct, (ctOfs + len - lastLen), lastLen);
|
|
340 |
ghashAllToS.update(padded);
|
|
341 |
} else {
|
|
342 |
ghashAllToS.update(ct, ctOfs, len);
|
|
343 |
}
|
|
344 |
}
|
|
345 |
|
|
346 |
|
|
347 |
/**
|
|
348 |
* Performs encryption operation.
|
|
349 |
*
|
|
350 |
* <p>The input plain text <code>in</code>, starting at <code>inOff</code>
|
|
351 |
* and ending at <code>(inOff + len - 1)</code>, is encrypted. The result
|
|
352 |
* is stored in <code>out</code>, starting at <code>outOfs</code>.
|
|
353 |
*
|
|
354 |
* <p>It is the application's responsibility to make sure that
|
|
355 |
* <code>len</code> is a multiple of the embedded cipher's block size,
|
|
356 |
* otherwise, a ProviderException will be thrown.
|
|
357 |
*
|
|
358 |
* <p>It is also the application's responsibility to make sure that
|
|
359 |
* <code>init</code> has been called before this method is called.
|
|
360 |
* (This check is omitted here, to avoid double checking.)
|
|
361 |
*
|
|
362 |
* @param in the buffer with the input data to be encrypted
|
|
363 |
* @param inOfs the offset in <code>in</code>
|
|
364 |
* @param len the length of the input data
|
|
365 |
* @param out the buffer for the result
|
|
366 |
* @param outOfs the offset in <code>out</code>
|
|
367 |
*/
|
|
368 |
void encrypt(byte[] in, int inOfs, int len, byte[] out, int outOfs) {
|
|
369 |
processAAD();
|
|
370 |
if (len > 0) {
|
|
371 |
gctrPAndC.update(in, inOfs, len, out, outOfs);
|
|
372 |
processed += len;
|
|
373 |
ghashAllToS.update(out, outOfs, len);
|
|
374 |
}
|
|
375 |
}
|
|
376 |
|
|
377 |
/**
|
|
378 |
* Performs encryption operation for the last time.
|
|
379 |
*
|
|
380 |
* <p>NOTE: <code>len</code> may not be multiple of the embedded
|
|
381 |
* cipher's block size for this call.
|
|
382 |
*
|
|
383 |
* @param in the input buffer with the data to be encrypted
|
|
384 |
* @param inOfs the offset in <code>in</code>
|
|
385 |
* @param len the length of the input data
|
|
386 |
* @param out the buffer for the encryption result
|
|
387 |
* @param outOfs the offset in <code>out</code>
|
|
388 |
* @return the number of bytes placed into the <code>out</code> buffer
|
|
389 |
*/
|
|
390 |
int encryptFinal(byte[] in, int inOfs, int len, byte[] out, int outOfs)
|
|
391 |
throws IllegalBlockSizeException {
|
|
392 |
if (out.length - outOfs < (len + tagLenBytes)) {
|
|
393 |
throw new RuntimeException("Output buffer too small");
|
|
394 |
}
|
|
395 |
|
|
396 |
processAAD();
|
|
397 |
if (len > 0) {
|
|
398 |
//ByteUtil.dumpArray(Arrays.copyOfRange(in, inOfs, inOfs + len));
|
|
399 |
doLastBlock(in, inOfs, len, out, outOfs, true);
|
|
400 |
}
|
|
401 |
|
|
402 |
byte[] lengthBlock = getLengthBlock(sizeOfAAD*8, processed*8);
|
|
403 |
ghashAllToS.update(lengthBlock);
|
|
404 |
byte[] s = ghashAllToS.digest();
|
|
405 |
byte[] sOut = new byte[s.length];
|
|
406 |
GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
|
|
407 |
gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);
|
|
408 |
|
|
409 |
System.arraycopy(sOut, 0, out, (outOfs + len), tagLenBytes);
|
|
410 |
return (len + tagLenBytes);
|
|
411 |
}
|
|
412 |
|
|
413 |
/**
|
|
414 |
* Performs decryption operation.
|
|
415 |
*
|
|
416 |
* <p>The input cipher text <code>in</code>, starting at
|
|
417 |
* <code>inOfs</code> and ending at <code>(inOfs + len - 1)</code>,
|
|
418 |
* is decrypted. The result is stored in <code>out</code>, starting at
|
|
419 |
* <code>outOfs</code>.
|
|
420 |
*
|
|
421 |
* <p>It is the application's responsibility to make sure that
|
|
422 |
* <code>len</code> is a multiple of the embedded cipher's block
|
|
423 |
* size, as any excess bytes are ignored.
|
|
424 |
*
|
|
425 |
* <p>It is also the application's responsibility to make sure that
|
|
426 |
* <code>init</code> has been called before this method is called.
|
|
427 |
* (This check is omitted here, to avoid double checking.)
|
|
428 |
*
|
|
429 |
* @param in the buffer with the input data to be decrypted
|
|
430 |
* @param inOfs the offset in <code>in</code>
|
|
431 |
* @param len the length of the input data
|
|
432 |
* @param out the buffer for the result
|
|
433 |
* @param outOfs the offset in <code>out</code>
|
|
434 |
*/
|
|
435 |
void decrypt(byte[] in, int inOfs, int len, byte[] out, int outOfs) {
|
|
436 |
processAAD();
|
|
437 |
|
|
438 |
if (len > 0) { // must be at least AES_BLOCK_SIZE bytes long
|
|
439 |
gctrPAndC.update(in, inOfs, len, out, outOfs);
|
|
440 |
processed += len;
|
|
441 |
ghashAllToS.update(in, inOfs, len);
|
|
442 |
}
|
|
443 |
}
|
|
444 |
|
|
445 |
/**
|
|
446 |
* Performs decryption operation for the last time.
|
|
447 |
*
|
|
448 |
* <p>NOTE: For cipher feedback modes which does not perform
|
|
449 |
* special handling for the last few blocks, this is essentially
|
|
450 |
* the same as <code>encrypt(...)</code>. Given most modes do
|
|
451 |
* not do special handling, the default impl for this method is
|
|
452 |
* to simply call <code>decrypt(...)</code>.
|
|
453 |
*
|
|
454 |
* @param in the input buffer with the data to be decrypted
|
|
455 |
* @param inOfs the offset in <code>cipher</code>
|
|
456 |
* @param len the length of the input data
|
|
457 |
* @param out the buffer for the decryption result
|
|
458 |
* @param outOfs the offset in <code>plain</code>
|
|
459 |
* @return the number of bytes placed into the <code>out</code> buffer
|
|
460 |
*/
|
|
461 |
int decryptFinal(byte[] in, int inOfs, int len,
|
|
462 |
byte[] out, int outOfs)
|
|
463 |
throws IllegalBlockSizeException, AEADBadTagException {
|
|
464 |
if (len < tagLenBytes) {
|
|
465 |
throw new RuntimeException("Input buffer too short - need tag");
|
|
466 |
}
|
|
467 |
if (out.length - outOfs < (len - tagLenBytes)) {
|
|
468 |
throw new RuntimeException("Output buffer too small");
|
|
469 |
}
|
|
470 |
processAAD();
|
|
471 |
|
|
472 |
int processedOld = processed;
|
|
473 |
byte[] tag = new byte[tagLenBytes];
|
|
474 |
// get the trailing tag bytes from 'in'
|
|
475 |
System.arraycopy(in, inOfs + len - tagLenBytes, tag, 0, tagLenBytes);
|
|
476 |
len -= tagLenBytes;
|
|
477 |
|
|
478 |
if (len > 0) {
|
|
479 |
doLastBlock(in, inOfs, len, out, outOfs, false);
|
|
480 |
}
|
|
481 |
|
|
482 |
byte[] lengthBlock = getLengthBlock(sizeOfAAD*8, processed*8);
|
|
483 |
ghashAllToS.update(lengthBlock);
|
|
484 |
|
|
485 |
byte[] s = ghashAllToS.digest();
|
|
486 |
byte[] sOut = new byte[s.length];
|
|
487 |
GCTR gctrForSToTag = new GCTR(embeddedCipher, this.preCounterBlock);
|
|
488 |
gctrForSToTag.doFinal(s, 0, s.length, sOut, 0);
|
|
489 |
for (int i = 0; i < tagLenBytes; i++) {
|
|
490 |
if (tag[i] != sOut[i]) {
|
|
491 |
throw new AEADBadTagException("Tag mismatch!");
|
|
492 |
}
|
|
493 |
}
|
|
494 |
return len;
|
|
495 |
}
|
|
496 |
|
|
497 |
// return tag length in bytes
|
|
498 |
int getTagLen() {
|
|
499 |
return this.tagLenBytes;
|
|
500 |
}
|
|
501 |
}
|