Skip to content

Commit abb9961

Browse files
committed
fixed reverse byte method.
1 parent 6589fc4 commit abb9961

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public void init(boolean forEncryption, CipherParameters params)
9191
if (keyBytes.length != 16)
9292
{
9393
throw new IllegalArgumentException(
94-
"Grain-128AEAD key must be 128 bits long");
94+
"Grain-128AEAD key must be 128 bits long");
9595
}
9696

9797
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties(
98-
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
99-
98+
this.getAlgorithmName(), 128, params, Utils.getPurpose(forEncryption)));
99+
100100
/**
101101
* Initialize variables.
102102
*/
@@ -418,6 +418,7 @@ private void doProcessAADBytes(byte[] input, int inOff, int len)
418418
{
419419
ader[1 + aderlen + i] = (byte)reverseByte(input[inOff + i]);
420420
}
421+
421422
byte adval;
422423
int adCnt = 0;
423424
for (int i = 0; i < ader.length; ++i)
@@ -467,14 +468,14 @@ public int doFinal(byte[] out, int outOff)
467468
doProcessAADBytes(aadData.getBuf(), 0, aadData.size());
468469
aadFinished = true;
469470
}
470-
471+
471472
this.mac = new byte[8];
472473

473474
output = getOutput();
474475
nfsr = shift(nfsr, (getOutputNFSR() ^ lfsr[0]) & 1);
475476
lfsr = shift(lfsr, (getOutputLFSR()) & 1);
476477
accumulate();
477-
478+
478479
int cCnt = 0;
479480
for (int i = 0; i < 2; ++i)
480481
{
@@ -483,7 +484,7 @@ public int doFinal(byte[] out, int outOff)
483484
mac[cCnt++] = (byte)((authAcc[i] >>> (j << 3)) & 0xff);
484485
}
485486
}
486-
487+
487488
System.arraycopy(mac, 0, out, outOff, mac.length);
488489

489490
try
@@ -514,9 +515,9 @@ public int getOutputSize(int len)
514515

515516
private int reverseByte(int x)
516517
{
517-
x = (((x & 0x55) << 1) | ((x & (~0x55)) >>> 1)) & 0xFF;
518-
x = (((x & 0x33) << 2) | ((x & (~0x33)) >>> 2)) & 0xFF;
519-
x = (((x & 0x0f) << 4) | ((x & (~0x0f)) >>> 4)) & 0xFF;
518+
x = (((x & 0x55) << 1) | ((x & (0xAA)) >>> 1)) & 0xFF;
519+
x = (((x & 0x33) << 2) | ((x & (0xCC)) >>> 2)) & 0xFF;
520+
x = (((x & 0x0f) << 4) | ((x & (0xf0)) >>> 4)) & 0xFF;
520521
return x;
521522
}
522523

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,9 @@ private void testLongAEAD()
124124
byte[] Nonce = Hex.decode("000102030405060708090A0B");
125125
byte[] PT = Hex.decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
126126
byte[] AD = Hex.decode( // 186 bytes
127-
"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
128-
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
129-
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
130-
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
131-
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E"
132-
+ "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
133-
byte[] CT = Hex.decode("731daa8b1d15317a1ccb4e3dd320095fb27e5bb2a10f2c669f870538637d4f1641bdc02d3cc432a5");
134-
127+
"000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9");
128+
byte[] CT = Hex.decode("731DAA8B1D15317A1CCB4E3DD320095FB27E5BB2A10F2C669F870538637D4F162298C70430A2B560");
129+
135130
Grain128AEADEngine grain = new Grain128AEADEngine();
136131
ParametersWithIV params = new ParametersWithIV(new KeyParameter(Key), Nonce);
137132
grain.init(true, params);
@@ -144,7 +139,7 @@ private void testLongAEAD()
144139
len += grain.processBytes(PT, 11, PT.length - 11, rv, len);
145140

146141
grain.doFinal(rv, len);
147-
142+
148143
isTrue(Arrays.areEqual(rv, CT));
149144

150145
grain.processBytes(PT, 0, 10, rv, 0);

0 commit comments

Comments
 (0)