Skip to content

Commit fabed60

Browse files
committed
Merge branch 'main' into 'shamir-secret-splitting'
# Conflicts: # core/src/test/java/org/bouncycastle/crypto/test/ElephantTest.java
2 parents 3910287 + 2b2fc60 commit fabed60

File tree

76 files changed

+4990
-575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+4990
-575
lines changed

ci/pub.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ source ci/common.sh
1414
export JAVA_HOME=`openjdk_21`
1515
export PATH=$JAVA_HOME/bin:$PATH
1616

17+
./gradlew clean build -x test
18+
./osgi_scan.sh
1719

18-
./gradlew clean build publishAllPublicationsToCwmavenRepository -x test
20+
./gradlew publishAllPublicationsToCwmavenRepository -x test
1921

2022

core/src/main/java/org/bouncycastle/crypto/engines/ElephantEngine.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public void init(boolean forEncryption, CipherParameters params)
320320
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
321321
initialised = true;
322322
m_state = forEncryption ? State.EncInit : State.DecInit;
323-
inputMessage = new byte[BLOCK_SIZE + (forEncryption ? 0 : CRYPTO_ABYTES)];
323+
inputMessage = new byte[BLOCK_SIZE * 2 + (forEncryption ? 0 : CRYPTO_ABYTES)];
324324
reset(false);
325325
}
326326

@@ -372,12 +372,19 @@ public int processBytes(byte[] input, int inOff, int len, byte[] output, int out
372372
int nb_it = Math.max(nblocks_c + 1, nblocks_ad - 1);
373373
byte[] tempInput = new byte[Math.max(nblocks_c, 1) * BLOCK_SIZE];
374374
System.arraycopy(inputMessage, 0, tempInput, 0, inputOff);
375-
System.arraycopy(input, inOff, tempInput, inputOff, Math.min(len, tempInput.length));
375+
System.arraycopy(input, inOff, tempInput, inputOff, Math.min(len, tempInput.length - inputOff));
376376
int rv = processBytes(tempInput, output, outOff, nb_it, nblocks_m, nblocks_c, mlen, nblocks_ad, false);
377-
int copyLen = rv - inputOff;
378-
inputOff = inputOff + len - rv;
379-
System.arraycopy(input, inOff + copyLen, inputMessage, 0, inputOff);
380-
377+
if (rv >= inputOff)
378+
{
379+
int copyLen = rv - inputOff;
380+
inputOff = inputOff + len - rv;
381+
System.arraycopy(input, inOff + copyLen, inputMessage, 0, inputOff);
382+
}
383+
else
384+
{
385+
System.arraycopy(input, inOff + rv, inputMessage, inputOff, len - rv);
386+
inputOff += len - rv;
387+
}
381388
messageLen += rv;
382389
return rv;
383390
}
@@ -404,6 +411,7 @@ public int doFinal(byte[] output, int outOff)
404411
throw new OutputLengthException("output buffer is too short");
405412
}
406413
int mlen = len + messageLen - (forEncryption ? 0 : CRYPTO_ABYTES);
414+
int rv = mlen - messageLen;
407415
int adlen = processAADBytes();
408416
int nblocks_c = 1 + mlen / BLOCK_SIZE;
409417
int nblocks_m = (mlen % BLOCK_SIZE) != 0 ? nblocks_c : nblocks_c - 1;
@@ -418,7 +426,7 @@ public int doFinal(byte[] output, int outOff)
418426
{
419427
System.arraycopy(tag_buffer, 0, tag, 0, CRYPTO_ABYTES);
420428
System.arraycopy(tag, 0, output, outOff, tag.length);
421-
mlen += CRYPTO_ABYTES;
429+
rv += CRYPTO_ABYTES;
422430
}
423431
else
424432
{
@@ -432,7 +440,7 @@ public int doFinal(byte[] output, int outOff)
432440
}
433441
}
434442
reset(false);
435-
return mlen;
443+
return rv;
436444
}
437445

438446
@Override
@@ -454,7 +462,17 @@ public int getUpdateOutputSize(int len)
454462
case EncAad:
455463
case EncData:
456464
case EncInit:
457-
return inputOff + len + CRYPTO_ABYTES;
465+
{
466+
int total = inputOff + len;
467+
return total - total % BLOCK_SIZE;
468+
}
469+
case DecAad:
470+
case DecData:
471+
case DecInit:
472+
{
473+
int total = Math.max(0, inputOff + len - CRYPTO_ABYTES);
474+
return total - total % BLOCK_SIZE;
475+
}
458476
}
459477
return Math.max(0, len + inputOff - CRYPTO_ABYTES);
460478
}
@@ -472,9 +490,9 @@ public int getOutputSize(int len)
472490
case EncAad:
473491
case EncData:
474492
case EncInit:
475-
return len + CRYPTO_ABYTES;
493+
return len + inputOff + CRYPTO_ABYTES;
476494
}
477-
return Math.max(0, len - CRYPTO_ABYTES);
495+
return Math.max(0, len + inputOff - CRYPTO_ABYTES);
478496
}
479497

480498
@Override
@@ -523,7 +541,7 @@ public int getIVBytesSize()
523541

524542
public int getBlockSize()
525543
{
526-
return CRYPTO_ABYTES;
544+
return BLOCK_SIZE;
527545
}
528546

529547
private void checkAad()
@@ -621,7 +639,7 @@ private int processBytes(byte[] m, byte[] output, int outOff, int nb_it, int nbl
621639
for (i = nb_its; i < nb_it; ++i)
622640
{
623641
int r_size = (i == nblocks_m - 1) ? mlen - i * BLOCK_SIZE : BLOCK_SIZE;
624-
if (!isDofinal && (r_size % BLOCK_SIZE != 0 || mlen <= i * BLOCK_SIZE))
642+
if (!isDofinal && (mlen <= i * BLOCK_SIZE || r_size % BLOCK_SIZE != 0))
625643
{
626644
break;
627645
}

core/src/main/java/org/bouncycastle/crypto/engines/ISAPEngine.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ public void init(boolean forEncryption, CipherParameters params)
806806
if (iv == null || iv.length != 16)
807807
{
808808
throw new IllegalArgumentException(
809-
"ISAP AEAD requires exactly 12 bytes of IV");
809+
"ISAP AEAD requires exactly 16 bytes of IV");
810810
}
811811

812812
if (!(ivParams.getParameters() instanceof KeyParameter))
@@ -961,13 +961,14 @@ public byte[] getMac()
961961
@Override
962962
public int getUpdateOutputSize(int len)
963963
{
964-
return len;
964+
int total = Math.max(0, len + message.size() + (forEncryption ? 0 : -16));
965+
return total - total % ISAP_rH_SZ;
965966
}
966967

967968
@Override
968969
public int getOutputSize(int len)
969970
{
970-
return len + 16;
971+
return Math.max(0, len + message.size() + (forEncryption ? 16 : -16));
971972
}
972973

973974
@Override

core/src/main/java/org/bouncycastle/crypto/engines/PhotonBeetleEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,14 @@ public byte[] getMac()
270270
@Override
271271
public int getUpdateOutputSize(int len)
272272
{
273-
return len;
273+
int total = Math.max(0, len + message.size() + (forEncryption ? 0 : -TAG_INBYTES));
274+
return total - total % RATE_INBYTES;
274275
}
275276

276277
@Override
277278
public int getOutputSize(int len)
278279
{
279-
return len + TAG_INBYTES;
280+
return Math.max(0, len + message.size() + (forEncryption ? TAG_INBYTES : -TAG_INBYTES));
280281
}
281282

282283
@Override

core/src/main/java/org/bouncycastle/crypto/engines/XoodyakEngine.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class XoodyakEngine
3636
private byte[] iv;
3737
private final int PhaseDown = 1;
3838
private final int PhaseUp = 2;
39-
// private final int NLANES = 12;
39+
// private final int NLANES = 12;
4040
// private final int NROWS = 3;
4141
// private final int NCOLUMS = 4;
4242
private final int MAXROUNDS = 12;
@@ -262,13 +262,14 @@ public byte[] getMac()
262262
@Override
263263
public int getUpdateOutputSize(int len)
264264
{
265-
return len;
265+
int total = Math.max(0, len + message.size() + (forEncryption ? 0 : -TAGLEN));
266+
return total - total % Rkout;
266267
}
267268

268269
@Override
269270
public int getOutputSize(int len)
270271
{
271-
return len + TAGLEN;
272+
return Math.max(0, len + message.size() + (forEncryption ? TAGLEN : -TAGLEN));
272273
}
273274

274275
@Override
@@ -371,7 +372,7 @@ private void Up(byte[] Yi, int YiLen, int Cu)
371372
a3 ^= e3;
372373
a7 ^= e3;
373374
a11 ^= e3;
374-
375+
375376
/* Rho-west: plane shift */
376377
int b0 = a0;
377378
int b1 = a1;
@@ -390,7 +391,7 @@ private void Up(byte[] Yi, int YiLen, int Cu)
390391

391392
/* Iota: round ant */
392393
b0 ^= RC[i];
393-
394+
394395
/* Chi: non linear layer */
395396
a0 = b0 ^ (~b4 & b8);
396397
a1 = b1 ^ (~b5 & b9);
@@ -406,7 +407,7 @@ private void Up(byte[] Yi, int YiLen, int Cu)
406407
b9 ^= (~b1 & b5);
407408
b10 ^= (~b2 & b6);
408409
b11 ^= (~b3 & b7);
409-
410+
410411
/* Rho-east: plane shift */
411412
a4 = Integers.rotateLeft(a4, 1);
412413
a5 = Integers.rotateLeft(a5, 1);

core/src/main/java/org/bouncycastle/pqc/crypto/lms/LMSigParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public ASN1ObjectIdentifier getDigestOID()
9494
return digestOid;
9595
}
9696

97-
static LMSigParameters getParametersForType(int type)
97+
public static LMSigParameters getParametersForType(int type)
9898
{
9999
return paramBuilders.get(type);
100100
}

core/src/main/java/org/bouncycastle/pqc/crypto/util/PublicKeyFactory.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,6 @@ AsymmetricKeyParameter getPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Obje
451451
private LMSKeyParameters getLmsKeyParameters(byte[] keyEnc)
452452
throws IOException
453453
{
454-
if (keyEnc.length == 64)
455-
{
456-
keyEnc = Arrays.copyOfRange(keyEnc, 4, keyEnc.length);
457-
}
458454
return HSSPublicKeyParameters.getInstance(keyEnc);
459455
}
460456
}

core/src/test/java/org/bouncycastle/crypto/test/CipherTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bouncycastle.crypto.params.ParametersWithIV;
1313
import org.bouncycastle.util.Arrays;
1414
import org.bouncycastle.util.test.SimpleTest;
15+
import org.junit.Assert;
1516

1617
public abstract class CipherTest
1718
extends SimpleTest
@@ -185,4 +186,57 @@ static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
185186
throw new RuntimeException(e);
186187
}
187188
}
189+
190+
static void checkAEADCipherOutputSize(int keySize, int ivSize, int blockSize, int tagSize, AEADCipher cipher)
191+
throws InvalidCipherTextException
192+
{
193+
final SecureRandom random = new SecureRandom();
194+
int tmpLength = random.nextInt(blockSize - 1) + 1;
195+
final byte[] plaintext = new byte[blockSize * 2 + tmpLength];
196+
byte[] key = new byte[keySize];
197+
byte[] iv = new byte[ivSize];
198+
random.nextBytes(key);
199+
random.nextBytes(iv);
200+
random.nextBytes(plaintext);
201+
cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
202+
byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];
203+
//before the encrypt
204+
Assert.assertEquals(plaintext.length + tagSize, ciphertext.length);
205+
Assert.assertEquals(plaintext.length, cipher.getUpdateOutputSize(plaintext.length) + tmpLength);
206+
//during the encrypt process of the first block
207+
int len = cipher.processBytes(plaintext, 0, tmpLength, ciphertext, 0);
208+
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getOutputSize(plaintext.length - tmpLength));
209+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(plaintext.length - tmpLength) + tmpLength);
210+
//during the encrypt process of the second block
211+
len += cipher.processBytes(plaintext, tmpLength, blockSize, ciphertext, len);
212+
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getOutputSize(plaintext.length - tmpLength - blockSize));
213+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(plaintext.length - tmpLength - blockSize) + tmpLength);
214+
//process the remaining bytes
215+
len += cipher.processBytes(plaintext, tmpLength + blockSize, blockSize, ciphertext, len);
216+
Assert.assertEquals(plaintext.length + tagSize, len + cipher.getOutputSize(0));
217+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(0) + tmpLength);
218+
//process doFinal
219+
len += cipher.doFinal(ciphertext, len);
220+
Assert.assertEquals(len, ciphertext.length);
221+
222+
cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
223+
//before the encrypt
224+
Assert.assertEquals(plaintext.length, cipher.getOutputSize(ciphertext.length));
225+
Assert.assertEquals(plaintext.length, cipher.getUpdateOutputSize(ciphertext.length) + tmpLength);
226+
//during the encrypt process of the first block
227+
len = cipher.processBytes(ciphertext, 0, tmpLength, plaintext, 0);
228+
Assert.assertEquals(plaintext.length, len + cipher.getOutputSize(ciphertext.length - tmpLength));
229+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(ciphertext.length - tmpLength) + tmpLength);
230+
//during the encrypt process of the second block
231+
len += cipher.processBytes(ciphertext, tmpLength, blockSize, plaintext, len);
232+
Assert.assertEquals(plaintext.length, len + cipher.getOutputSize(ciphertext.length - tmpLength - blockSize));
233+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(ciphertext.length - tmpLength - blockSize) + tmpLength);
234+
//process the remaining bytes
235+
len += cipher.processBytes(ciphertext, tmpLength + blockSize, blockSize + tagSize, plaintext, len);
236+
Assert.assertEquals(plaintext.length, len + cipher.getOutputSize(0));
237+
Assert.assertEquals(plaintext.length, len + cipher.getUpdateOutputSize(0) + tmpLength);
238+
//process doFinal
239+
len += cipher.doFinal(plaintext, len);
240+
Assert.assertEquals(len, plaintext.length);
241+
}
188242
}

core/src/test/java/org/bouncycastle/crypto/test/ElephantTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public AEADCipher CreateInstace()
5757
testVectors(ElephantEngine.ElephantParameters.elephant160, "v160");
5858
testVectors(ElephantEngine.ElephantParameters.elephant176, "v176");
5959

60-
ElephantEngine elephant = new ElephantEngine(ElephantEngine.ElephantParameters.elephant200);
61-
testExceptions(elephant, elephant.getKeyBytesSize(), elephant.getIVBytesSize(), elephant.getBlockSize());
62-
testParameters(elephant, 16, 12, 16);
6360

6461
elephant = new ElephantEngine(ElephantEngine.ElephantParameters.elephant160);
6562
testExceptions(elephant, elephant.getKeyBytesSize(), elephant.getIVBytesSize(), elephant.getBlockSize());
@@ -233,6 +230,7 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
233230
}
234231

235232
aeadBlockCipher.init(true, params);
233+
c1 = new byte[aeadBlockCipher.getOutputSize(0)];
236234
try
237235
{
238236
aeadBlockCipher.doFinal(c1, m.length);
@@ -442,7 +440,5 @@ public static void main(String[] args)
442440
{
443441
runTest(new ElephantTest());
444442
}
445-
446-
447443
}
448444

core/src/test/java/org/bouncycastle/crypto/test/ISAPTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public void performTest()
5151
testVectors("isapk128av20", IsapType.ISAP_K_128A);
5252
testVectors("isapk128v20", IsapType.ISAP_K_128);
5353
testVectors();
54+
CipherTest.checkAEADCipherOutputSize(16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128A));
55+
CipherTest.checkAEADCipherOutputSize(16, 16, 18, 16, new ISAPEngine(IsapType.ISAP_K_128));
56+
CipherTest.checkAEADCipherOutputSize(16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128A));
57+
CipherTest.checkAEADCipherOutputSize(16, 16, 8, 16, new ISAPEngine(IsapType.ISAP_A_128));
5458
}
5559

5660
private void testVectors(String filename, IsapType isapType)
@@ -282,6 +286,7 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
282286
}
283287

284288
aeadBlockCipher.init(true, params);
289+
c1 = new byte[aeadBlockCipher.getOutputSize(m.length)];
285290
try
286291
{
287292
aeadBlockCipher.doFinal(c1, m.length);
@@ -431,10 +436,11 @@ private void testExceptions(AEADCipher aeadBlockCipher, int keysize, int ivsize,
431436
{
432437
m7[i] = (byte)rand.nextInt();
433438
}
439+
440+
aeadBlockCipher.init(true, params);
434441
byte[] c7 = new byte[aeadBlockCipher.getOutputSize(m7.length)];
435442
byte[] c8 = new byte[c7.length];
436443
byte[] c9 = new byte[c7.length];
437-
aeadBlockCipher.init(true, params);
438444
aeadBlockCipher.processAADBytes(aad2, 0, aad2.length);
439445
offset = aeadBlockCipher.processBytes(m7, 0, m7.length, c7, 0);
440446
aeadBlockCipher.doFinal(c7, offset);

0 commit comments

Comments
 (0)