Skip to content

Commit 05d1599

Browse files
committed
javaDoc
corrected doFinal() methods to take mbCipher into account.
1 parent 9ddc55d commit 05d1599

File tree

3 files changed

+121
-66
lines changed

3 files changed

+121
-66
lines changed

core/src/main/java/org/bouncycastle/asn1/gm/GMObjectIdentifiers.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ public interface GMObjectIdentifiers
5252
* <Information security technology — Cryptographic application identifier criterion specification>
5353
* <url>http://c.gb688.cn/bzgk/gb/showGb?type=online&hcno=252CF0F72A7BE339A56DEA7D774E8994</url>,
5454
* Page 21 only cover from 301.1 to 301.3
55-
* */
56-
ASN1ObjectIdentifier wapip192v1 = sm_scheme.branch("301.101");
55+
*/
56+
ASN1ObjectIdentifier wapip192v1 = sm_scheme.branch("301.101");
5757
/**
5858
* <WAPI certificate management—Part 5: Example of certificate format (draft)>
5959
* <url>http://www.chinabwips.org.cn/zqyjgs1.htm</url> and
6060
* <url>http://www.chinabwips.org.cn/doc/101.pdf</url>,
6161
* Page 9 and page 10 states the OID of ECDSA-192 algorithm based on SHA-256 is 1.2.156.11235.1.1.1
62-
* */
63-
ASN1ObjectIdentifier wapi192v1 = new ASN1ObjectIdentifier("1.2.156.11235.1.1.1");
64-
ASN1ObjectIdentifier wapi192v1_parameters = new ASN1ObjectIdentifier("1.2.156.11235.1.1.2.1");
62+
*/
63+
ASN1ObjectIdentifier wapi192v1 = new ASN1ObjectIdentifier("1.2.156.11235.1.1.1");
64+
ASN1ObjectIdentifier wapi192v1_parameters = new ASN1ObjectIdentifier("1.2.156.11235.1.1.2.1");
6565

6666
ASN1ObjectIdentifier sm2encrypt_recommendedParameters = sm2encrypt.branch("1");
6767
ASN1ObjectIdentifier sm2encrypt_specifiedParameters = sm2encrypt.branch("2");
@@ -71,8 +71,8 @@ public interface GMObjectIdentifiers
7171
ASN1ObjectIdentifier sm2encrypt_with_sha256 = sm2encrypt.branch("2.4");
7272
ASN1ObjectIdentifier sm2encrypt_with_sha384 = sm2encrypt.branch("2.5");
7373
ASN1ObjectIdentifier sm2encrypt_with_sha512 = sm2encrypt.branch("2.6");
74-
ASN1ObjectIdentifier sm2encrypt_with_rmd160 = sm2encrypt.branch("2.7");
75-
ASN1ObjectIdentifier sm2encrypt_with_whirlpool =sm2encrypt.branch("2.8");
74+
ASN1ObjectIdentifier sm2encrypt_with_rmd160 = sm2encrypt.branch("2.7");
75+
ASN1ObjectIdentifier sm2encrypt_with_whirlpool = sm2encrypt.branch("2.8");
7676
ASN1ObjectIdentifier sm2encrypt_with_blake2b512 = sm2encrypt.branch("2.9");
7777
ASN1ObjectIdentifier sm2encrypt_with_blake2s256 = sm2encrypt.branch("2.10");
7878
ASN1ObjectIdentifier sm2encrypt_with_md5 = sm2encrypt.branch("2.11");

core/src/main/java/org/bouncycastle/crypto/BufferedBlockCipher.java

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,26 @@ public int getOutputSize(
168168
/**
169169
* process a single byte, producing an output block if necessary.
170170
*
171-
* @param in the input byte.
172-
* @param out the space for any output that might be produced.
171+
* @param in the input byte.
172+
* @param out the space for any output that might be produced.
173173
* @param outOff the offset from which the output will be copied.
174174
* @return the number of output bytes copied to out.
175-
* @throws DataLengthException if there isn't enough space in out.
176-
* @throws IllegalStateException if the cipher isn't initialised.
175+
* @exception DataLengthException if there isn't enough space in out.
176+
* @exception IllegalStateException if the cipher isn't initialised.
177177
*/
178178
public int processByte(
179-
byte in,
180-
byte[] out,
181-
int outOff)
179+
byte in,
180+
byte[] out,
181+
int outOff)
182182
throws DataLengthException, IllegalStateException
183183
{
184-
int resultLen = 0;
184+
int resultLen = 0;
185185

186186
buf[bufOff++] = in;
187187

188188
if (bufOff == buf.length)
189189
{
190-
resultLen = cipher.processBlock(buf, 0, out, outOff);
191-
bufOff = 0;
190+
resultLen = processBuffer(out, outOff);
192191
}
193192

194193
return resultLen;
@@ -197,30 +196,30 @@ public int processByte(
197196
/**
198197
* process an array of bytes, producing output if necessary.
199198
*
200-
* @param in the input byte array.
201-
* @param inOff the offset at which the input data starts.
202-
* @param len the number of bytes to be copied out of the input array.
203-
* @param out the space for any output that might be produced.
199+
* @param in the input byte array.
200+
* @param inOff the offset at which the input data starts.
201+
* @param len the number of bytes to be copied out of the input array.
202+
* @param out the space for any output that might be produced.
204203
* @param outOff the offset from which the output will be copied.
205204
* @return the number of output bytes copied to out.
206-
* @throws DataLengthException if there isn't enough space in out.
207-
* @throws IllegalStateException if the cipher isn't initialised.
205+
* @exception DataLengthException if there isn't enough space in out.
206+
* @exception IllegalStateException if the cipher isn't initialised.
208207
*/
209208
public int processBytes(
210-
byte[] in,
211-
int inOff,
212-
int len,
213-
byte[] out,
214-
int outOff)
209+
byte[] in,
210+
int inOff,
211+
int len,
212+
byte[] out,
213+
int outOff)
215214
throws DataLengthException, IllegalStateException
216215
{
217216
if (len < 0)
218217
{
219218
throw new IllegalArgumentException("Can't have a negative input length!");
220219
}
221220

222-
int blockSize = getBlockSize();
223-
int length = getUpdateOutputSize(len);
221+
int blockSize = getBlockSize();
222+
int length = getUpdateOutputSize(len);
224223

225224
if (length > 0)
226225
{
@@ -235,29 +234,35 @@ public int processBytes(
235234

236235
if (len > gapLen)
237236
{
238-
System.arraycopy(in, inOff, buf, bufOff, gapLen);
239-
inOff += gapLen;
240-
len -= gapLen;
237+
if (bufOff != 0)
238+
{
239+
System.arraycopy(in, inOff, buf, bufOff, gapLen);
240+
inOff += gapLen;
241+
len -= gapLen;
242+
}
243+
241244
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, length))
242245
{
243246
in = new byte[len];
244247
System.arraycopy(out, inOff, in, 0, len);
245248
inOff = 0;
246249
}
247250

248-
resultLen += cipher.processBlock(buf, 0, out, outOff);
249-
250-
bufOff = 0;
251+
// if bufOff non-zero buffer must now be full
252+
if (bufOff != 0)
253+
{
254+
resultLen += processBuffer(out, outOff);
255+
}
251256

252257
if (mbCipher != null)
253258
{
254-
int blockCount = len / mbCipher.getMultiBlockSize();
259+
int blockCount = len / blockSize;
255260

256261
if (blockCount > 0)
257262
{
258263
resultLen += mbCipher.processBlocks(in, inOff, blockCount, out, outOff + resultLen);
259264

260-
int processed = blockCount * mbCipher.getMultiBlockSize();
265+
int processed = blockCount * blockSize;
261266

262267
len -= processed;
263268
inOff += processed;
@@ -281,13 +286,25 @@ public int processBytes(
281286

282287
if (bufOff == buf.length)
283288
{
284-
resultLen += cipher.processBlock(buf, 0, out, outOff + resultLen);
285-
bufOff = 0;
289+
resultLen += processBuffer(out, outOff + resultLen);
286290
}
287291

288292
return resultLen;
289293
}
290294

295+
private int processBuffer(byte[] out, int outOff)
296+
{
297+
bufOff = 0;
298+
if (mbCipher != null)
299+
{
300+
return mbCipher.processBlocks(buf, 0, buf.length / mbCipher.getBlockSize(), out, outOff);
301+
}
302+
else
303+
{
304+
return cipher.processBlock(buf, 0, out, outOff);
305+
}
306+
}
307+
291308
/**
292309
* Process the last block in the buffer.
293310
*
@@ -318,15 +335,26 @@ public int doFinal(
318335

319336
if (bufOff != 0)
320337
{
321-
if (!partialBlockOkay)
338+
int index = 0;
339+
if (mbCipher != null)
322340
{
323-
throw new DataLengthException("data not block size aligned");
341+
int nBlocks = bufOff / mbCipher.getBlockSize();
342+
resultLen += mbCipher.processBlocks(buf, 0, nBlocks, out, outOff);
343+
index = nBlocks * mbCipher.getBlockSize();
324344
}
325345

326-
cipher.processBlock(buf, 0, buf, 0);
327-
resultLen = bufOff;
328-
bufOff = 0;
329-
System.arraycopy(buf, 0, out, outOff, resultLen);
346+
if (bufOff != index)
347+
{
348+
if (!partialBlockOkay)
349+
{
350+
throw new DataLengthException("data not block size aligned");
351+
}
352+
353+
cipher.processBlock(buf, index, buf, index);
354+
resultLen += bufOff - index;
355+
bufOff = 0;
356+
System.arraycopy(buf, index, out, outOff, resultLen);
357+
}
330358
}
331359

332360
return resultLen;

core/src/main/java/org/bouncycastle/crypto/DefaultBufferedBlockCipher.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ public int processByte(
189189

190190
if (bufOff == buf.length)
191191
{
192-
resultLen = cipher.processBlock(buf, 0, out, outOff);
193-
bufOff = 0;
192+
resultLen = processBuffer(out, outOff);
194193
}
195194

196195
return resultLen;
@@ -223,7 +222,7 @@ public int processBytes(
223222

224223
int blockSize = getBlockSize();
225224
int length = getUpdateOutputSize(len);
226-
225+
227226
if (length > 0)
228227
{
229228
if ((outOff + length) > out.length)
@@ -237,30 +236,35 @@ public int processBytes(
237236

238237
if (len > gapLen)
239238
{
240-
System.arraycopy(in, inOff, buf, bufOff, gapLen);
241-
inOff += gapLen;
242-
len -= gapLen;
239+
if (bufOff != 0)
240+
{
241+
System.arraycopy(in, inOff, buf, bufOff, gapLen);
242+
inOff += gapLen;
243+
len -= gapLen;
244+
}
245+
243246
if (in == out && Arrays.segmentsOverlap(inOff, len, outOff, length))
244247
{
245248
in = new byte[len];
246249
System.arraycopy(out, inOff, in, 0, len);
247250
inOff = 0;
248251
}
249252

250-
resultLen += cipher.processBlock(buf, 0, out, outOff);
251-
252-
bufOff = 0;
253-
253+
// if bufOff non-zero buffer must now be full
254+
if (bufOff != 0)
255+
{
256+
resultLen += processBuffer(out, outOff);
257+
}
254258

255259
if (mbCipher != null)
256260
{
257-
int blockCount = len / mbCipher.getMultiBlockSize();
261+
int blockCount = len / blockSize;
258262

259263
if (blockCount > 0)
260264
{
261265
resultLen += mbCipher.processBlocks(in, inOff, blockCount, out, outOff + resultLen);
262266

263-
int processed = blockCount * mbCipher.getMultiBlockSize();
267+
int processed = blockCount * blockSize;
264268

265269
len -= processed;
266270
inOff += processed;
@@ -284,13 +288,25 @@ public int processBytes(
284288

285289
if (bufOff == buf.length)
286290
{
287-
resultLen += cipher.processBlock(buf, 0, out, outOff + resultLen);
288-
bufOff = 0;
291+
resultLen += processBuffer(out, outOff + resultLen);
289292
}
290293

291294
return resultLen;
292295
}
293296

297+
private int processBuffer(byte[] out, int outOff)
298+
{
299+
bufOff = 0;
300+
if (mbCipher != null)
301+
{
302+
return mbCipher.processBlocks(buf, 0, buf.length / mbCipher.getBlockSize(), out, outOff);
303+
}
304+
else
305+
{
306+
return cipher.processBlock(buf, 0, out, outOff);
307+
}
308+
}
309+
294310
/**
295311
* Process the last block in the buffer.
296312
*
@@ -321,15 +337,26 @@ public int doFinal(
321337

322338
if (bufOff != 0)
323339
{
324-
if (!partialBlockOkay)
340+
int index = 0;
341+
if (mbCipher != null)
325342
{
326-
throw new DataLengthException("data not block size aligned");
343+
int nBlocks = bufOff / mbCipher.getBlockSize();
344+
resultLen += mbCipher.processBlocks(buf, 0, nBlocks, out, outOff);
345+
index = nBlocks * mbCipher.getBlockSize();
327346
}
328347

329-
cipher.processBlock(buf, 0, buf, 0);
330-
resultLen = bufOff;
331-
bufOff = 0;
332-
System.arraycopy(buf, 0, out, outOff, resultLen);
348+
if (bufOff != index)
349+
{
350+
if (!partialBlockOkay)
351+
{
352+
throw new DataLengthException("data not block size aligned");
353+
}
354+
355+
cipher.processBlock(buf, index, buf, index);
356+
resultLen += bufOff - index;
357+
bufOff = 0;
358+
System.arraycopy(buf, index, out, outOff, resultLen);
359+
}
333360
}
334361

335362
return resultLen;

0 commit comments

Comments
 (0)