Skip to content

Commit 0cdd69c

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents 8662038 + 44061a7 commit 0cdd69c

File tree

112 files changed

+2101
-2048
lines changed

Some content is hidden

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

112 files changed

+2101
-2048
lines changed

core/src/main/java/org/bouncycastle/asn1/cryptopro/GOST3410NamedParameters.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class GOST3410NamedParameters
1515
static final Hashtable params = new Hashtable();
1616
static final Hashtable names = new Hashtable();
1717

18-
static private GOST3410ParamSetParameters cryptoProA = new GOST3410ParamSetParameters(
18+
private static final GOST3410ParamSetParameters cryptoProA = new GOST3410ParamSetParameters(
1919
1024,
2020
new BigInteger("127021248288932417465907042777176443525787653508916535812817507265705031260985098497423188333483401180925999995120988934130659205614996724254121049274349357074920312769561451689224110579311248812610229678534638401693520013288995000362260684222750813532307004517341633685004541062586971416883686778842537820383"),
2121
new BigInteger("68363196144955700784444165611827252895102170888761442055095051287550314083023"),
@@ -32,7 +32,7 @@ public class GOST3410NamedParameters
3232

3333
);
3434

35-
static private GOST3410ParamSetParameters cryptoProB = new GOST3410ParamSetParameters(
35+
private static final GOST3410ParamSetParameters cryptoProB = new GOST3410ParamSetParameters(
3636
1024,
3737
new BigInteger("139454871199115825601409655107690713107041707059928031797758001454375765357722984094124368522288239833039114681648076688236921220737322672160740747771700911134550432053804647694904686120113087816240740184800477047157336662926249423571248823968542221753660143391485680840520336859458494803187341288580489525163"),
3838
new BigInteger("79885141663410976897627118935756323747307951916507639758300472692338873533959"),
@@ -53,7 +53,7 @@ public class GOST3410NamedParameters
5353
//}
5454
);
5555

56-
static private GOST3410ParamSetParameters cryptoProXchA = new GOST3410ParamSetParameters(
56+
private static final GOST3410ParamSetParameters cryptoProXchA = new GOST3410ParamSetParameters(
5757
1024,
5858
new BigInteger("142011741597563481196368286022318089743276138395243738762872573441927459393512718973631166078467600360848946623567625795282774719212241929071046134208380636394084512691828894000571524625445295769349356752728956831541775441763139384457191755096847107846595662547942312293338483924514339614727760681880609734239"),
5959
new BigInteger("91771529896554605945588149018382750217296858393520724172743325725474374979801"),

core/src/main/java/org/bouncycastle/crypto/params/DESParameters.java

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

3+
import org.bouncycastle.util.Arrays;
4+
35
public class DESParameters
46
extends KeyParameter
57
{
@@ -17,14 +19,14 @@ public DESParameters(
1719
/*
1820
* DES Key length in bytes.
1921
*/
20-
static public final int DES_KEY_LENGTH = 8;
22+
public static final int DES_KEY_LENGTH = 8;
2123

2224
/*
2325
* Table of weak and semi-weak keys taken from Schneier pp281
2426
*/
25-
static private final int N_DES_WEAK_KEYS = 16;
27+
private static final int N_DES_WEAK_KEYS = 16;
2628

27-
static private byte[] DES_weak_keys =
29+
private static byte[] DES_weak_keys =
2830
{
2931
/* weak keys */
3032
(byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01, (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01,
@@ -58,27 +60,21 @@ public DESParameters(
5860
* @return true if the given DES key material is weak or semi-weak,
5961
* false otherwise.
6062
*/
61-
public static boolean isWeakKey(
62-
byte[] key,
63-
int offset)
63+
public static boolean isWeakKey(byte[] key, int offset)
6464
{
65-
if (key.length - offset < DES_KEY_LENGTH)
65+
if (offset > (key.length - DES_KEY_LENGTH))
6666
{
6767
throw new IllegalArgumentException("key material too short.");
6868
}
6969

70-
nextkey: for (int i = 0; i < N_DES_WEAK_KEYS; i++)
70+
for (int i = 0; i < N_DES_WEAK_KEYS; i++)
7171
{
72-
for (int j = 0; j < DES_KEY_LENGTH; j++)
72+
if (Arrays.constantTimeAreEqual(DES_KEY_LENGTH, key, offset, DES_weak_keys, i * DES_KEY_LENGTH))
7373
{
74-
if (key[j + offset] != DES_weak_keys[i * DES_KEY_LENGTH + j])
75-
{
76-
continue nextkey;
77-
}
74+
return true;
7875
}
79-
80-
return true;
8176
}
77+
8278
return false;
8379
}
8480

core/src/main/java/org/bouncycastle/crypto/params/DESedeParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class DESedeParameters
66
/*
77
* DES-EDE Key length in bytes.
88
*/
9-
static public final int DES_EDE_KEY_LENGTH = 24;
9+
public static final int DES_EDE_KEY_LENGTH = 24;
1010

1111
public DESedeParameters(
1212
byte[] key)

core/src/main/java/org/bouncycastle/crypto/signers/SM2Signer.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.math.BigInteger;
44
import java.security.SecureRandom;
5+
import java.util.logging.Level;
6+
import java.util.logging.Logger;
57

68
import org.bouncycastle.crypto.CipherParameters;
79
import org.bouncycastle.crypto.CryptoException;
@@ -30,6 +32,8 @@
3032
public class SM2Signer
3133
implements Signer, ECConstants
3234
{
35+
private static final Logger LOG = Logger.getLogger(SM2Signer.class.getName());
36+
3337
private static final class State
3438
{
3539
static final int UNINITIALIZED = 0;
@@ -160,6 +164,10 @@ public boolean verifySignature(byte[] signature)
160164
}
161165
catch (Exception e)
162166
{
167+
if (LOG.isLoggable(Level.FINE))
168+
{
169+
LOG.log(Level.FINE, "SM2 signature verification failed due to exception", e);
170+
}
163171
}
164172
finally
165173
{
@@ -248,12 +256,20 @@ private boolean verifySignature(BigInteger r, BigInteger s)
248256
// B1
249257
if (r.compareTo(ONE) < 0 || r.compareTo(n) >= 0)
250258
{
259+
if (LOG.isLoggable(Level.FINE))
260+
{
261+
LOG.fine("SM2 signature verification failed: r out of range");
262+
}
251263
return false;
252264
}
253265

254266
// B2
255267
if (s.compareTo(ONE) < 0 || s.compareTo(n) >= 0)
256268
{
269+
if (LOG.isLoggable(Level.FINE))
270+
{
271+
LOG.fine("SM2 signature verification failed: s out of range");
272+
}
257273
return false;
258274
}
259275

@@ -267,6 +283,10 @@ private boolean verifySignature(BigInteger r, BigInteger s)
267283
BigInteger t = r.add(s).mod(n);
268284
if (t.equals(ZERO))
269285
{
286+
if (LOG.isLoggable(Level.FINE))
287+
{
288+
LOG.fine("SM2 signature verification failed: t equals zero");
289+
}
270290
return false;
271291
}
272292

@@ -275,6 +295,10 @@ private boolean verifySignature(BigInteger r, BigInteger s)
275295
ECPoint x1y1 = ECAlgorithms.sumOfTwoMultiplies(ecParams.getG(), s, q, t).normalize();
276296
if (x1y1.isInfinity())
277297
{
298+
if (LOG.isLoggable(Level.FINE))
299+
{
300+
LOG.fine("SM2 signature verification failed: calculated point at infinity");
301+
}
278302
return false;
279303
}
280304

core/src/main/java/org/bouncycastle/crypto/signers/StandardDSAEncoding.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,9 @@ public class StandardDSAEncoding
1616
{
1717
public static final StandardDSAEncoding INSTANCE = new StandardDSAEncoding();
1818

19-
public byte[] encode(BigInteger n, BigInteger r, BigInteger s) throws IOException
20-
{
21-
ASN1EncodableVector v = new ASN1EncodableVector();
22-
encodeValue(n, v, r);
23-
encodeValue(n, v, s);
24-
return new DERSequence(v).getEncoded(ASN1Encoding.DER);
25-
}
26-
2719
public BigInteger[] decode(BigInteger n, byte[] encoding) throws IOException
2820
{
29-
ASN1Sequence seq = (ASN1Sequence)ASN1Primitive.fromByteArray(encoding);
21+
ASN1Sequence seq = ASN1Sequence.getInstance(encoding);
3022
if (seq.size() == 2)
3123
{
3224
BigInteger r = decodeValue(n, seq, 0);
@@ -42,6 +34,14 @@ public BigInteger[] decode(BigInteger n, byte[] encoding) throws IOException
4234
throw new IllegalArgumentException("Malformed signature");
4335
}
4436

37+
public byte[] encode(BigInteger n, BigInteger r, BigInteger s) throws IOException
38+
{
39+
return new DERSequence(
40+
encodeValue(n, r),
41+
encodeValue(n, s)
42+
).getEncoded(ASN1Encoding.DER);
43+
}
44+
4545
protected BigInteger checkValue(BigInteger n, BigInteger x)
4646
{
4747
if (x.signum() < 0 || (null != n && x.compareTo(n) >= 0))
@@ -57,8 +57,8 @@ protected BigInteger decodeValue(BigInteger n, ASN1Sequence s, int pos)
5757
return checkValue(n, ((ASN1Integer)s.getObjectAt(pos)).getValue());
5858
}
5959

60-
protected void encodeValue(BigInteger n, ASN1EncodableVector v, BigInteger x)
60+
protected ASN1Integer encodeValue(BigInteger n, BigInteger x)
6161
{
62-
v.add(new ASN1Integer(checkValue(n, x)));
62+
return new ASN1Integer(checkValue(n, x));
6363
}
6464
}

core/src/main/java/org/bouncycastle/pqc/crypto/cmce/CMCEEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ private static int ctz(long in)
10491049
}
10501050

10511051
/* Used in mov columns*/
1052-
static private long same_mask64(short x, short y)
1052+
private static long same_mask64(short x, short y)
10531053
{
10541054
long mask;
10551055

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ class HQCEngine
3030
private final int pkSize;
3131
private final GF2PolynomialCalculator gf;
3232
private final long rejectionThreshold;
33+
private final int cipherTextBytes;
3334

34-
public HQCEngine(int n, int n1, int n2, int k, int g, int delta, int w, int wr,
35-
int fft, int nmu, int pkSize, int[] generatorPoly)
35+
HQCEngine(int n, int n1, int n2, int k, int g, int delta, int w, int wr, int fft, int nmu, int pkSize,
36+
int[] generatorPoly)
3637
{
3738
this.n = n;
3839
this.k = k;
@@ -54,6 +55,12 @@ public HQCEngine(int n, int n1, int n2, int k, int g, int delta, int w, int wr,
5455
long RED_MASK = ((1L << (n & 63)) - 1);
5556
this.gf = new GF2PolynomialCalculator(N_BYTE_64, n, RED_MASK);
5657
this.rejectionThreshold = ((1L << 24) / n) * n;
58+
this.cipherTextBytes = N_BYTE + N1N2_BYTE + 16;
59+
}
60+
61+
int getCipherTextBytes()
62+
{
63+
return cipherTextBytes;
5764
}
5865

5966
/**

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,32 @@
66
public class HQCKEMExtractor
77
implements EncapsulatedSecretExtractor
88
{
9-
private HQCEngine engine;
9+
private final HQCPrivateKeyParameters privateKey;
10+
private final HQCEngine engine;
1011

11-
private final HQCKeyParameters key;
12-
13-
public HQCKEMExtractor(HQCPrivateKeyParameters privParams)
12+
public HQCKEMExtractor(HQCPrivateKeyParameters privateKey)
1413
{
15-
this.key = privParams;
16-
initCipher(key.getParameters());
17-
}
14+
if (privateKey == null)
15+
{
16+
throw new NullPointerException("'privateKey' cannot be null");
17+
}
1818

19-
private void initCipher(HQCParameters param)
20-
{
21-
engine = param.getEngine();
19+
this.privateKey = privateKey;
20+
this.engine = privateKey.getParameters().getEngine();
2221
}
2322

2423
public byte[] extractSecret(byte[] encapsulation)
2524
{
2625
byte[] session_key = new byte[64];
27-
HQCPrivateKeyParameters secretKey = (HQCPrivateKeyParameters)key;
28-
byte[] sk = secretKey.getPrivateKey();
26+
byte[] sk = privateKey.getPrivateKey();
2927

3028
engine.decaps(session_key, encapsulation, sk);
3129

3230
return Arrays.copyOfRange(session_key, 0, 32);
3331
}
3432

3533
public int getEncapsulationLength()
36-
{ // Hash + salt
37-
return key.getParameters().getN_BYTES() + key.getParameters().getN1N2_BYTES() + 16;
34+
{
35+
return engine.getCipherTextBytes();
3836
}
3937
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.security.SecureRandom;
44

5+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
56
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;
67
import org.bouncycastle.crypto.SecretWithEncapsulation;
78
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
@@ -11,11 +12,11 @@
1112
public class HQCKEMGenerator
1213
implements EncapsulatedSecretGenerator
1314
{
14-
private final SecureRandom sr;
15+
private final SecureRandom random;
1516

1617
public HQCKEMGenerator(SecureRandom random)
1718
{
18-
this.sr = random;
19+
this.random = CryptoServicesRegistrar.getSecureRandom(random);
1920
}
2021

2122
public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recipientKey)
@@ -29,7 +30,7 @@ public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recip
2930
byte[] salt = new byte[key.getParameters().getSALT_SIZE_BYTES()];
3031
byte[] pk = key.getPublicKey();
3132

32-
engine.encaps(u, v, K, pk, salt, sr);
33+
engine.encaps(u, v, K, pk, salt, random);
3334

3435
byte[] cipherText = Arrays.concatenate(u, v, salt);
3536

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ public class HQCParameters
3131
final static int PARAM_M = 8;
3232
final static int GF_MUL_ORDER = 255;
3333

34-
private final HQCEngine hqcEngine;
34+
private final HQCEngine engine;
3535

36-
private HQCParameters(String name, int n, int n1, int n2, int k, int g, int delta, int w, int wr,
37-
int fft, int nMu, int pkSize, int skSize, int[] generatorPoly)
36+
private HQCParameters(String name, int n, int n1, int n2, int k, int g, int delta, int w, int wr, int fft, int nMu,
37+
int pkSize, int skSize, int[] generatorPoly)
3838
{
3939
this.name = name;
4040
this.n = n;
4141
this.n1 = n1;
4242
this.n2 = n2;
4343
this.publicKeyBytes = pkSize;
4444
this.secretKeyBytes = skSize;
45-
hqcEngine = new HQCEngine(n, n1, n2, k, g, delta, w, wr, fft, nMu, pkSize, generatorPoly);
45+
this.engine = new HQCEngine(n, n1, n2, k, g, delta, w, wr, fft, nMu, pkSize, generatorPoly);
4646
}
4747

4848
int getSHA512_BYTES()
@@ -67,7 +67,12 @@ int getN1N2_BYTES()
6767

6868
HQCEngine getEngine()
6969
{
70-
return hqcEngine;
70+
return engine;
71+
}
72+
73+
public int getEncapsulationLength()
74+
{
75+
return engine.getCipherTextBytes();
7176
}
7277

7378
public int getSessionKeySize()

0 commit comments

Comments
 (0)