Skip to content

Commit 07ba85d

Browse files
Merge branch 'hqc-checksum-fail' into 'main'
Hqc checksum fail See merge request root/bc-java!82
2 parents fa0d3ae + 16ba11a commit 07ba85d

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/hqc/ReedSolomon.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ private static void computeZx(int[] output, int[] sigma, int deg, int[] syndrome
191191
for (int i = 2; i <= delta; i++)
192192
{
193193
int mask = i - deg < 1 ? 0xffff : 0;
194-
output[i] = mask & sigma[i - 1];
195-
194+
output[i] ^= (mask) & syndromes[i - 1];
196195
for (int j = 1; j < i; j++)
197196
{
198197
output[i] ^= (mask) & GFCalculator.mult(sigma[j], syndromes[i - j - 1]);

prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/HQCTest.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
import org.bouncycastle.jcajce.spec.KEMExtractSpec;
1717
import org.bouncycastle.jcajce.spec.KEMGenerateSpec;
1818
import org.bouncycastle.jcajce.spec.KEMParameterSpec;
19+
import org.bouncycastle.pqc.crypto.hqc.HQCKeyGenerationParameters;
20+
import org.bouncycastle.pqc.crypto.hqc.HQCKeyPairGenerator;
21+
import org.bouncycastle.pqc.crypto.hqc.HQCParameters;
1922
import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider;
2023
import org.bouncycastle.pqc.jcajce.spec.HQCParameterSpec;
2124
import org.bouncycastle.util.Arrays;
2225
import org.bouncycastle.util.encoders.Hex;
26+
import org.bouncycastle.util.test.FixedSecureRandom;
2327

2428
/**
2529
* KEM tests for HQC with the BCPQC provider.
@@ -36,7 +40,7 @@ public void setUp()
3640
}
3741

3842
public void testBasicKEMAES()
39-
throws Exception
43+
throws Exception
4044
{
4145
KeyPairGenerator kpg = KeyPairGenerator.getInstance("HQC", "BCPQC");
4246
kpg.initialize(HQCParameterSpec.hqc128, new SecureRandom());
@@ -51,7 +55,7 @@ public void testBasicKEMAES()
5155
}
5256

5357
public void testBasicKEMCamellia()
54-
throws Exception
58+
throws Exception
5559
{
5660
KeyPairGenerator kpg = KeyPairGenerator.getInstance("HQC", "BCPQC");
5761
kpg.initialize(HQCParameterSpec.hqc128, new SecureRandom());
@@ -61,7 +65,7 @@ public void testBasicKEMCamellia()
6165
}
6266

6367
public void testBasicKEMSEED()
64-
throws Exception
68+
throws Exception
6569
{
6670
KeyPairGenerator kpg = KeyPairGenerator.getInstance("HQC", "BCPQC");
6771
kpg.initialize(HQCParameterSpec.hqc128, new SecureRandom());
@@ -70,7 +74,7 @@ public void testBasicKEMSEED()
7074
}
7175

7276
public void testBasicKEMARIA()
73-
throws Exception
77+
throws Exception
7478
{
7579
KeyPairGenerator kpg = KeyPairGenerator.getInstance("HQC", "BCPQC");
7680
kpg.initialize(HQCParameterSpec.hqc128, new SecureRandom());
@@ -80,7 +84,7 @@ public void testBasicKEMARIA()
8084
}
8185

8286
private void performKEMScipher(KeyPair kp, String algorithm, KEMParameterSpec ktsParameterSpec)
83-
throws Exception
87+
throws Exception
8488
{
8589
Cipher w1 = Cipher.getInstance(algorithm, "BCPQC");
8690

@@ -109,7 +113,7 @@ private void performKEMScipher(KeyPair kp, String algorithm, KEMParameterSpec kt
109113
}
110114

111115
public void testGenerateAES()
112-
throws Exception
116+
throws Exception
113117
{
114118
KeyPairGenerator kpg = KeyPairGenerator.getInstance("HQC", "BCPQC");
115119
kpg.initialize(HQCParameterSpec.hqc128, new SecureRandom());
@@ -135,7 +139,7 @@ public void testGenerateAES()
135139
}
136140

137141
public void testGenerateAES256()
138-
throws Exception
142+
throws Exception
139143
{
140144
KeyPairGenerator kpg = KeyPairGenerator.getInstance("HQC", "BCPQC");
141145
kpg.initialize(HQCParameterSpec.hqc256, new SecureRandom());
@@ -159,4 +163,45 @@ public void testGenerateAES256()
159163

160164
assertTrue(Arrays.areEqual(secEnc1.getEncoded(), secEnc2.getEncoded()));
161165
}
166+
167+
public void testReedSolomon()
168+
throws Exception
169+
{
170+
byte[] seed = Hex.decode("416a32ada1c7a569c34d5334273a781c340aac25eb7614271aa6930d0358fb30fd87e111336a29e165dc60d9643a3e9b");//b
171+
byte[] kemSeed = Hex.decode("13f36c0636ff93af6d702f7774097c185bf67cddc9b09f9b584d736c4faf40e073b0499efa0c926e9a44fec1e45ee4cf");
172+
//HQCKeyPairGenerator kpg = new HQCKeyPairGenerator();
173+
//kpg.init(new HQCKeyGenerationParameters();
174+
KeyPairGenerator kpg = KeyPairGenerator.getInstance("HQC", "BCPQC");
175+
SecureRandom random = new FixedSecureRandom(new FixedSecureRandom.Source[]{new FixedSecureRandom.Data(seed)});
176+
SecureRandom kemRandom = new FixedSecureRandom(new FixedSecureRandom.Source[]{new FixedSecureRandom.Data(kemSeed)});
177+
kpg.initialize(HQCParameterSpec.hqc128, random);
178+
KeyPair kp = kpg.generateKeyPair();
179+
String algorithm = "HQC";
180+
KEMParameterSpec ktsParameterSpec = new KEMParameterSpec("ARIA-KWP");
181+
Cipher w1 = Cipher.getInstance(algorithm, "BCPQC");
182+
183+
byte[] keyBytes;
184+
if (algorithm.endsWith("KWP"))
185+
{
186+
keyBytes = Hex.decode("000102030405060708090a0b0c0d0e0faa");
187+
}
188+
else
189+
{
190+
keyBytes = Hex.decode("000102030405060708090a0b0c0d0e0f");
191+
}
192+
SecretKey key = new SecretKeySpec(keyBytes, "AES");
193+
194+
w1.init(Cipher.WRAP_MODE, kp.getPublic(), ktsParameterSpec, kemRandom);
195+
196+
byte[] data = w1.wrap(key);
197+
198+
Cipher w2 = Cipher.getInstance(algorithm, "BCPQC");
199+
200+
w2.init(Cipher.UNWRAP_MODE, kp.getPrivate(), ktsParameterSpec);
201+
202+
Key k = w2.unwrap(data, "AES", Cipher.SECRET_KEY);
203+
204+
assertTrue(Arrays.areEqual(keyBytes, k.getEncoded()));
205+
}
206+
162207
}

0 commit comments

Comments
 (0)