Skip to content

Commit ee7336f

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/main'
2 parents 0e35045 + b10b8ad commit ee7336f

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/ntru/NTRUKEMGenerator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recip
4848
r = pair.r();
4949
m = pair.m();
5050

51-
byte[] rm1 = r.s3ToBytes(parameterSet.owcpaMsgBytes());
52-
System.arraycopy(rm1, 0, rm, 0, rm1.length);
53-
byte[] rm2 = m.s3ToBytes(rm.length - parameterSet.packTrinaryBytes());
54-
System.arraycopy(rm2, 0, rm, parameterSet.packTrinaryBytes(), rm2.length);
51+
r.s3ToBytes(rm, 0);
52+
m.s3ToBytes(rm, parameterSet.packTrinaryBytes());
5553

5654
SHA3Digest sha3256 = new SHA3Digest(256);
5755
sha3256.update(rm, 0, rm.length);

core/src/main/java/org/bouncycastle/pqc/crypto/ntru/NTRUOWCPA.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ public OWCPAKeyPair keypair(byte[] seed)
5252
g = pair.g();
5353

5454
invfMod3.s3Inv(f);
55-
byte[] fs3ToBytes = f.s3ToBytes(params.owcpaMsgBytes());
56-
System.arraycopy(fs3ToBytes, 0, privateKey, 0, fs3ToBytes.length);
57-
byte[] s3Res = invfMod3.s3ToBytes(privateKey.length - this.params.packTrinaryBytes());
58-
System.arraycopy(s3Res, 0, privateKey, this.params.packTrinaryBytes(), s3Res.length);
55+
f.s3ToBytes(privateKey, 0);
56+
invfMod3.s3ToBytes(privateKey, params.packTrinaryBytes());
5957

6058
f.z3ToZq();
6159
g.z3ToZq();
@@ -152,7 +150,7 @@ public OWCPADecryptResult decrypt(byte[] ciphertext, byte[] privateKey)
152150

153151
finv3.s3FromBytes(Arrays.copyOfRange(sk, params.packTrinaryBytes(), sk.length));
154152
m.s3Mul(mf, finv3);
155-
byte[] arr1 = m.s3ToBytes(rm.length - params.packTrinaryBytes());
153+
m.s3ToBytes(rm, params.packTrinaryBytes());
156154

157155
fail = 0;
158156

@@ -193,9 +191,7 @@ public OWCPADecryptResult decrypt(byte[] ciphertext, byte[] privateKey)
193191
fail |= checkR(r);
194192

195193
r.trinaryZqToZ3();
196-
byte[] arr2 = r.s3ToBytes(params.owcpaMsgBytes());
197-
System.arraycopy(arr2, 0, rm, 0, arr2.length);
198-
System.arraycopy(arr1, 0, rm, params.packTrinaryBytes(), arr1.length);
194+
r.s3ToBytes(rm, 0);
199195

200196
return new OWCPADecryptResult(rm, fail);
201197
}

core/src/main/java/org/bouncycastle/pqc/math/ntru/Polynomial.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class Polynomial
1212
*/
1313
// TODO: maybe the maths library needs to move.
1414
public short[] coeffs;
15-
15+
1616
protected NTRUParameterSet params;
1717

1818
public Polynomial(NTRUParameterSet params)
@@ -141,6 +141,12 @@ public void rqSumZeroFromBytes(byte[] a)
141141
public byte[] s3ToBytes(int messageSize)
142142
{
143143
byte[] msg = new byte[messageSize];
144+
s3ToBytes(msg, 0);
145+
return msg;
146+
}
147+
148+
public void s3ToBytes(byte[] msg, int msgOff)
149+
{
144150
byte c;
145151

146152
for (int i = 0; i < params.packDegree() / 5; i++)
@@ -150,7 +156,7 @@ public byte[] s3ToBytes(int messageSize)
150156
c = (byte)(3 * c + this.coeffs[5 * i + 2] & 255);
151157
c = (byte)(3 * c + this.coeffs[5 * i + 1] & 255);
152158
c = (byte)(3 * c + this.coeffs[5 * i + 0] & 255);
153-
msg[i] = c;
159+
msg[i + msgOff] = c;
154160
}
155161

156162
// if 5 does not divide NTRU_N-1
@@ -162,9 +168,8 @@ public byte[] s3ToBytes(int messageSize)
162168
{
163169
c = (byte)(3 * c + this.coeffs[5 * i + j] & 255);
164170
}
165-
msg[i] = c;
171+
msg[i + msgOff] = c;
166172
}
167-
return msg;
168173
}
169174

170175
/**
@@ -388,7 +393,7 @@ private void r2InvToRqInv(Polynomial ai, Polynomial a, Polynomial b, Polynomial
388393
c.coeffs[0] += 2;
389394
this.rqMul(c, s);
390395
}
391-
396+
392397
void s3Inv(Polynomial a, Polynomial f, Polynomial g, Polynomial v, Polynomial w)
393398
{
394399
int n = this.coeffs.length;

core/src/main/java/org/bouncycastle/pqc/math/ntru/parameters/NTRUHRSS1373.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
/**
5-
* NTRU-HRSS parameter set with n = 701.
5+
* NTRU-HRSS parameter set with n = 1373.
66
*
77
* @see NTRUHRSSParameterSet
88
*/

util/src/main/java/org/bouncycastle/asn1/cms/CMSORIforKEMOtherInfo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* <pre>
1414
* CMSORIforKEMOtherInfo ::= SEQUENCE {
1515
* wrap KeyEncryptionAlgorithmIdentifier,
16-
* kekLength INTEGER (1..MAX),
16+
* kekLength INTEGER (1..65535),
1717
* ukm [0] EXPLICIT UserKeyingMaterial OPTIONAL
1818
* }
1919
*
@@ -34,6 +34,10 @@ public CMSORIforKEMOtherInfo(AlgorithmIdentifier wrap, int kekLength)
3434

3535
public CMSORIforKEMOtherInfo(AlgorithmIdentifier wrap, int kekLength, byte[] ukm)
3636
{
37+
if (kekLength > 65535)
38+
{
39+
throw new IllegalArgumentException("kekLength must be <= 65535");
40+
}
3741
this.wrap = wrap;
3842
this.kekLength = kekLength;
3943
this.ukm = ukm;

0 commit comments

Comments
 (0)