Skip to content

Commit e2df273

Browse files
committed
Grain128AEADEngine: fix DER length encoding for AAD lengths > 255
1 parent 316c2a4 commit e2df273

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

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

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -239,43 +239,49 @@ protected void processBufferAAD(byte[] input, int inOff)
239239
@Override
240240
protected void processFinalAAD()
241241
{
242+
// Encode(ad length) denotes the message length encoded in the DER format.
243+
242244
int len = aadOperator.getLen();
243245
byte[] input = ((StreamAADOperator)aadOperator).getBytes();
244-
byte[] ader;
245246

246-
//encodeDer
247+
// Need up to 5 bytes for the DER length as an 'int'
248+
byte[] ader = new byte[5];
249+
250+
int pos;
247251
if (len < 128)
248252
{
249-
ader = new byte[1];
250-
ader[0] = (byte)len;
253+
pos = ader.length - 1;
254+
ader[pos] = (byte)len;
251255
}
252256
else
253257
{
254-
// aderlen is the highest bit position divided by 8
255-
int aderlen = len_length(len);
256-
ader = new byte[1 + aderlen];
257-
ader[0] = (byte)(0x80 | aderlen);
258-
int tmp = len;
259-
for (int i = 1; i < ader.length; ++i)
258+
pos = ader.length;
259+
260+
int dl = len;
261+
do
260262
{
261-
ader[i] = (byte)tmp;
262-
tmp >>>= 8;
263+
ader[--pos] = (byte)dl;
264+
dl >>>= 8;
263265
}
266+
while (dl != 0);
267+
268+
int count = ader.length - pos;
269+
ader[--pos] = (byte)(0x80 | count);
264270
}
265271

266-
absorbAadData(ader, ader.length);
267-
absorbAadData(input, len);
272+
absorbAadData(ader, pos, ader.length - pos);
273+
absorbAadData(input, 0, len);
268274
}
269275

270-
private void absorbAadData(byte[] ader, int len)
276+
private void absorbAadData(byte[] buf, int off, int len)
271277
{
272278
for (int i = 0; i < len; ++i)
273279
{
274-
byte ader_i = ader[i];
280+
byte b = buf[off + i];
275281
for (int j = 0; j < 8; ++j)
276282
{
277283
shift();
278-
updateInternalState((ader_i >> j) & 1);
284+
updateInternalState((b >> j) & 1);
279285
}
280286
}
281287
}
@@ -319,21 +325,4 @@ protected void processBufferDecrypt(byte[] input, int inOff, byte[] output, int
319325
output[outOff + i] = cc;
320326
}
321327
}
322-
323-
private static int len_length(int v)
324-
{
325-
if ((v & 0xff) == v)
326-
{
327-
return 1;
328-
}
329-
if ((v & 0xffff) == v)
330-
{
331-
return 2;
332-
}
333-
if ((v & 0xffffff) == v)
334-
{
335-
return 3;
336-
}
337-
return 4;
338-
}
339328
}

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,8 @@ static void isEqualTo(
228228
}
229229
}
230230

231-
// public static void main(String[] args)
232-
// {
233-
// runTest(new AsconTest());
234-
// runTest(new ElephantTest());
235-
// runTest(new GiftCofbTest());
236-
// runTest(new Grain128AEADTest());
237-
// runTest(new ISAPTest());
238-
// runTest(new PhotonBeetleTest());
239-
// runTest(new RomulusTest());
240-
// runTest(new SparkleTest());
241-
// runTest(new XoodyakTest());
242-
// }
231+
public static void main(String[] args)
232+
{
233+
runTest(new Grain128AEADTest());
234+
}
243235
}
244-

0 commit comments

Comments
 (0)