Skip to content

Commit 2f62955

Browse files
committed
Skip redundant cipher init in Frodo-AES
1 parent c339fd6 commit 2f62955

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/frodo/FrodoMatrixGenerator.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.bouncycastle.pqc.crypto.frodo;
22

3-
import org.bouncycastle.crypto.BufferedBlockCipher;
4-
import org.bouncycastle.crypto.InvalidCipherTextException;
3+
import org.bouncycastle.crypto.BlockCipher;
54
import org.bouncycastle.crypto.Xof;
65
import org.bouncycastle.crypto.digests.SHAKEDigest;
76
import org.bouncycastle.crypto.engines.AESEngine;
87
import org.bouncycastle.crypto.params.KeyParameter;
98
import org.bouncycastle.util.Arrays;
10-
import org.bouncycastle.util.Exceptions;
119
import org.bouncycastle.util.Pack;
1210

1311
abstract class FrodoMatrixGenerator
@@ -57,12 +55,11 @@ short[] genMatrix(byte[] seedA)
5755
static class Aes128MatrixGenerator
5856
extends FrodoMatrixGenerator
5957
{
60-
BufferedBlockCipher cipher;
58+
private final BlockCipher cipher;
6159
public Aes128MatrixGenerator(int n, int q)
6260
{
6361
super(n, q);
64-
cipher = new BufferedBlockCipher(new AESEngine());
65-
62+
cipher = new AESEngine();
6663
}
6764

6865
short[] genMatrix(byte[] seedA)
@@ -73,6 +70,9 @@ short[] genMatrix(byte[] seedA)
7370
byte[] b = new byte[16];
7471
byte[] c = new byte[16];
7572

73+
KeyParameter kp = new KeyParameter(seedA);
74+
cipher.init(true, kp);
75+
7676
// 1. for i = 0; i < n; i += 1
7777
for (int i = 0; i < n; i++)
7878
{
@@ -87,7 +87,7 @@ short[] genMatrix(byte[] seedA)
8787
// struct.pack_into('<H', b, 0, i)
8888
// struct.pack_into('<H', b, 2, j)
8989
// 4. c = AES128(seedA, b)
90-
aes128(c, seedA, b);
90+
cipher.processBlock(b, 0, c, 0);
9191
// 5. for k = 0; k < 8; k += 1
9292
for (int k = 0; k < 8; k++)
9393
{
@@ -98,20 +98,5 @@ short[] genMatrix(byte[] seedA)
9898
}
9999
return A;
100100
}
101-
102-
void aes128(byte[] out, byte[] keyBytes, byte[] msg)
103-
{
104-
try
105-
{
106-
KeyParameter kp = new KeyParameter(keyBytes);
107-
cipher.init(true, kp);
108-
int len = cipher.processBytes(msg, 0, msg.length, out, 0);
109-
cipher.doFinal(out, len);
110-
}
111-
catch (InvalidCipherTextException e)
112-
{
113-
throw Exceptions.illegalStateException(e.toString(), e);
114-
}
115-
}
116101
}
117102
}

0 commit comments

Comments
 (0)