Skip to content

Commit 2613693

Browse files
gefeilidghgit
authored andcommitted
Set teh parameter settings of SAKKE
1 parent 1fdc823 commit 2613693

File tree

5 files changed

+97
-115
lines changed

5 files changed

+97
-115
lines changed

core/src/main/java/org/bouncycastle/crypto/kems/SAKKEKEMExtractor.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ public class SAKKEKEMExtractor
1919
private final BigInteger q;
2020
private final ECPoint P;
2121
private final ECPoint Z_S;
22-
private final ECPoint K_bS; // Receiver's RSK
22+
private final ECPoint K_bs;
2323
private final int n; // Security parameter
24-
private final SAKKEPrivateKeyParameters privateKey;
24+
private final BigInteger identifier;
2525

2626
public SAKKEKEMExtractor(SAKKEPrivateKeyParameters privateKey)
2727
{
28-
this.privateKey = privateKey;
2928
SAKKEPublicKeyParameters publicKey = privateKey.getPublicParams();
3029
this.curve = publicKey.getCurve();
3130
this.q = publicKey.getQ();
3231
this.P = publicKey.getP();
33-
this.p = publicKey.getp();
32+
this.p = publicKey.getPrime();
3433
this.Z_S = publicKey.getZ();
35-
this.K_bS = privateKey.getPrivatePoint();
34+
this.K_bs = privateKey.getRSK();
3635
this.n = publicKey.getN();
36+
this.identifier = publicKey.getIdentifier();
3737
}
3838

3939
@Override
@@ -46,16 +46,15 @@ public byte[] extractSecret(byte[] encapsulation)
4646
BigInteger H = new BigInteger(Arrays.copyOfRange(encapsulation, 257, 274));
4747

4848
// Step 2: Compute w = <R_bS, K_bS> using pairing
49-
BigInteger w = computePairing(R_bS, K_bS, p, q);
50-
//System.out.println(new String(Hex.encode(w.toByteArray())));
51-
//BigInteger w = tatePairing(R_bS.getXCoord().toBigInteger(), R_bS.getYCoord().toBigInteger(), K_bS.getXCoord().toBigInteger(), K_bS.getYCoord().toBigInteger(), q, p);
49+
BigInteger w = computePairing(R_bS, K_bs, p, q);
50+
5251
// Step 3: Compute SSV = H XOR HashToIntegerRange(w, 2^n)
5352
BigInteger twoToN = BigInteger.ONE.shiftLeft(n);
5453
BigInteger mask = SAKKEUtils.hashToIntegerRange(w.toByteArray(), twoToN);
5554
BigInteger ssv = H.xor(mask);
5655

5756
// Step 4: Compute r = HashToIntegerRange(SSV || b)
58-
BigInteger b = privateKey.getB();
57+
BigInteger b = identifier;
5958
BigInteger r = SAKKEUtils.hashToIntegerRange(Arrays.concatenate(ssv.toByteArray(), b.toByteArray()), q);
6059

6160
// Step 5: Validate R_bS

core/src/main/java/org/bouncycastle/crypto/kems/SAKKEKEMSGenerator.java

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,18 @@
33
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;
44
import org.bouncycastle.crypto.SecretWithEncapsulation;
55
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
6+
import org.bouncycastle.crypto.params.SAKKEPublicKeyParameters;
67
import org.bouncycastle.math.ec.ECCurve;
78
import org.bouncycastle.math.ec.ECPoint;
89
import org.bouncycastle.util.Arrays;
910
import org.bouncycastle.util.BigIntegers;
10-
import org.bouncycastle.util.encoders.Hex;
1111

1212
import java.math.BigInteger;
1313
import java.security.SecureRandom;
1414

1515
public class SAKKEKEMSGenerator
1616
implements EncapsulatedSecretGenerator
1717
{
18-
19-
private static final BigInteger p = new BigInteger(
20-
"997ABB1F0A563FDA65C61198DAD0657A416C0CE19CB48261BE9AE358B3E01A2E" +
21-
"F40AAB27E2FC0F1B228730D531A59CB0E791B39FF7C88A19356D27F4A666A6D0" +
22-
"E26C6487326B4CD4512AC5CD65681CE1B6AFF4A831852A82A7CF3C521C3C09AA" +
23-
"9F94D6AF56971F1FFCE3E82389857DB080C5DF10AC7ACE87666D807AFEA85FEB", 16
24-
);
25-
26-
private static final BigInteger q = new BigInteger(
27-
"265EAEC7C2958FF69971846636B4195E905B0338672D20986FA6B8D62CF8068B" +
28-
"BD02AAC9F8BF03C6C8A1CC354C69672C39E46CE7FDF222864D5B49FD2999A9B4" +
29-
"389B1921CC9AD335144AB173595A07386DABFD2A0C614AA0A9F3CF14870F026A" +
30-
"A7E535ABD5A5C7C7FF38FA08E2615F6C203177C42B1EB3A1D99B601EBFAA17FB", 16
31-
);
32-
33-
private static final BigInteger Px = new BigInteger(
34-
"53FC09EE332C29AD0A7990053ED9B52A2B1A2FD60AEC69C698B2F204B6FF7CBF" +
35-
"B5EDB6C0F6CE2308AB10DB9030B09E1043D5F22CDB9DFA55718BD9E7406CE890" +
36-
"9760AF765DD5BCCB337C86548B72F2E1A702C3397A60DE74A7C1514DBA66910D" +
37-
"D5CFB4CC80728D87EE9163A5B63F73EC80EC46C4967E0979880DC8ABEAE63895", 16
38-
);
39-
40-
private static final BigInteger Py = new BigInteger(
41-
"0A8249063F6009F1F9F1F0533634A135D3E82016029906963D778D821E141178" +
42-
"F5EA69F4654EC2B9E7F7F5E5F0DE55F66B598CCF9A140B2E416CFF0CA9E032B9" +
43-
"70DAE117AD547C6CCAD696B5B7652FE0AC6F1E80164AA989492D979FC5A4D5F2" +
44-
"13515AD7E9CB99A980BDAD5AD5BB4636ADB9B5706A67DCDE75573FD71BEF16D7", 16
45-
);
46-
47-
BigInteger g = new BigInteger(Hex.decode("66FC2A43 2B6EA392 148F1586 7D623068\n" +
48-
" C6A87BD1 FB94C41E 27FABE65 8E015A87\n" +
49-
" 371E9474 4C96FEDA 449AE956 3F8BC446\n" +
50-
" CBFDA85D 5D00EF57 7072DA8F 541721BE\n" +
51-
" EE0FAED1 828EAB90 B99DFB01 38C78433\n" +
52-
" 55DF0460 B4A9FD74 B4F1A32B CAFA1FFA\n" +
53-
" D682C033 A7942BCC E3720F20 B9B7B040\n" +
54-
" 3C8CAE87 B7A0042A CDE0FAB3 6461EA46"));
55-
private static final int n = 128;
5618
private final SecureRandom random;
5719

5820
public SAKKEKEMSGenerator(SecureRandom random)
@@ -63,54 +25,34 @@ public SAKKEKEMSGenerator(SecureRandom random)
6325
@Override
6426
public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recipientKey)
6527
{
28+
SAKKEPublicKeyParameters keyParameters = (SAKKEPublicKeyParameters)recipientKey;
29+
ECPoint Z = keyParameters.getZ();
30+
BigInteger b = keyParameters.getIdentifier();
31+
BigInteger p = keyParameters.getPrime();
32+
BigInteger q = keyParameters.getQ();
33+
BigInteger g = keyParameters.getG();
34+
int n = keyParameters.getN();
35+
ECCurve curve = keyParameters.getCurve();
36+
ECPoint P = keyParameters.getP();
37+
6638
// 1. Generate random SSV in range [0, 2^n - 1]
6739
BigInteger ssv = new BigInteger(n, random);
6840

41+
6942
// 2. Compute r = HashToIntegerRange(SSV || b, q)
70-
BigInteger b = new BigInteger("323031312D30320074656C3A2B34343737303039303031323300", 16); //getRecipientId((SAKKEPublicKey)recipientKey);
43+
7144
BigInteger r = SAKKEUtils.hashToIntegerRange(Arrays.concatenate(ssv.toByteArray(), b.toByteArray()), q);
72-
//System.out.println(new String(Hex.encode(r.toByteArray())));
73-
ECCurve.Fp curve = new ECCurve.Fp(
74-
p, // Prime p
75-
BigInteger.valueOf(-3).mod(p), // a = -3
76-
BigInteger.ZERO, // ,
77-
g, // Order of the subgroup (from RFC 6509)
78-
BigInteger.ONE // Cofactor = 1
79-
);
80-
ECPoint P = curve.createPoint(Px, Py);
81-
82-
ECPoint Z = curve.createPoint(
83-
new BigInteger("5958EF1B1679BF099B3A030DF255AA6A" +
84-
"23C1D8F143D4D23F753E69BD27A832F3" +
85-
"8CB4AD53DDEF4260B0FE8BB45C4C1FF5" +
86-
"10EFFE300367A37B61F701D914AEF097" +
87-
"24825FA0707D61A6DFF4FBD7273566CD" +
88-
"DE352A0B04B7C16A78309BE640697DE7" +
89-
"47613A5FC195E8B9F328852A579DB8F9" +
90-
"9B1D0034479EA9C5595F47C4B2F54FF2", 16), // Px
91-
new BigInteger("1508D37514DCF7A8E143A6058C09A6BF" +
92-
"2C9858CA37C258065AE6BF7532BC8B5B" +
93-
"63383866E0753C5AC0E72709F8445F2E" +
94-
"6178E065857E0EDA10F68206B63505ED" +
95-
"87E534FB2831FF957FB7DC619DAE6130" +
96-
"1EEACC2FDA3680EA4999258A833CEA8F" +
97-
"C67C6D19487FB449059F26CC8AAB655A" +
98-
"B58B7CC796E24E9A394095754F5F8BAE", 16) // Py
99-
);
45+
10046

10147
// 3. Compute R_(b,S) = [r]([b]P + Z_S)
10248
ECPoint bP = P.multiply(b).normalize();
10349
ECPoint R_bS = bP.add(Z).multiply(r).normalize(); // [r]([b]P + Z_S)
104-
// System.out.println("R_Bs x:" + new String(Hex.encode(R_bS.getXCoord().toBigInteger().toByteArray())));
105-
// System.out.println("R_Bs y:" + new String(Hex.encode(R_bS.getYCoord().toBigInteger().toByteArray())));
106-
10750

10851
// 4. Compute H = SSV XOR HashToIntegerRange( g^r, 2^n )
10952
BigInteger[] v = fp2Exponentiate(p, BigInteger.ONE, g, r, curve);
11053
BigInteger g_r = v[1].multiply(v[0].modInverse(p)).mod(p);
11154

11255
BigInteger mask = SAKKEUtils.hashToIntegerRange(g_r.toByteArray(), BigInteger.ONE.shiftLeft(n)); // 2^n
113-
//System.out.println(new String(Hex.encode(mask.toByteArray())));
11456

11557
BigInteger H = ssv.xor(mask);
11658
//System.out.println(new String(Hex.encode(H.toByteArray())));
@@ -124,14 +66,12 @@ public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recip
12466
}
12567

12668

127-
// Helper method for F_p² exponentiation
12869
public static BigInteger[] fp2Exponentiate(
12970
BigInteger p,
13071
BigInteger pointX,
13172
BigInteger pointY,
13273
BigInteger n,
133-
ECCurve.Fp curve
134-
)
74+
ECCurve curve)
13575
{
13676
BigInteger[] result = new BigInteger[2];
13777

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,31 @@
77
public class SAKKEPrivateKeyParameters
88
extends AsymmetricKeyParameter
99
{
10-
private final BigInteger b; // User's identity
11-
private final ECPoint K; // Private key K_a
1210
private final SAKKEPublicKeyParameters publicParams;
11+
private final BigInteger z; // KMS Public Key: Z = [z]P
12+
private final ECPoint rsk;
1313

14-
public SAKKEPrivateKeyParameters(BigInteger b, ECPoint K, SAKKEPublicKeyParameters publicParams)
14+
public SAKKEPrivateKeyParameters(BigInteger z, ECPoint rsk, SAKKEPublicKeyParameters publicParams)
1515
{
1616
super(true);
17-
this.b = b;
18-
this.K = K;
17+
this.z = z;
18+
this.rsk = rsk;
1919
this.publicParams = publicParams;
2020
}
2121

22-
public BigInteger getB()
22+
public SAKKEPublicKeyParameters getPublicParams()
2323
{
24-
return b;
24+
return publicParams;
2525
}
2626

27-
public SAKKEPublicKeyParameters getPublicParams()
27+
28+
public BigInteger getMasterSecret()
2829
{
29-
return publicParams;
30+
return z;
3031
}
3132

32-
public ECPoint getPrivatePoint()
33+
public ECPoint getRSK()
3334
{
34-
return K;
35+
return rsk;
3536
}
3637
}

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

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

33
import java.math.BigInteger;
44

5+
import org.bouncycastle.crypto.Digest;
6+
import org.bouncycastle.crypto.digests.SHA256Digest;
57
import org.bouncycastle.math.ec.ECCurve;
68
import org.bouncycastle.math.ec.ECPoint;
79
import org.bouncycastle.util.encoders.Hex;
@@ -17,6 +19,13 @@ public class SAKKEPublicKeyParameters
1719
"9F94D6AF56971F1FFCE3E82389857DB080C5DF10AC7ACE87666D807AFEA85FEB", 16
1820
);
1921

22+
private static final BigInteger q = new BigInteger(
23+
"265EAEC7C2958FF69971846636B4195E905B0338672D20986FA6B8D62CF8068B" +
24+
"BD02AAC9F8BF03C6C8A1CC354C69672C39E46CE7FDF222864D5B49FD2999A9B4" +
25+
"389B1921CC9AD335144AB173595A07386DABFD2A0C614AA0A9F3CF14870F026A" +
26+
"A7E535ABD5A5C7C7FF38FA08E2615F6C203177C42B1EB3A1D99B601EBFAA17FB", 16
27+
);
28+
2029
private static final BigInteger Px = new BigInteger(
2130
"53FC09EE332C29AD0A7990053ED9B52A2B1A2FD60AEC69C698B2F204B6FF7CBF" +
2231
"B5EDB6C0F6CE2308AB10DB9030B09E1043D5F22CDB9DFA55718BD9E7406CE890" +
@@ -31,7 +40,8 @@ public class SAKKEPublicKeyParameters
3140
"13515AD7E9CB99A980BDAD5AD5BB4636ADB9B5706A67DCDE75573FD71BEF16D7", 16
3241
);
3342

34-
43+
// g = <P,P>
44+
// < , > is Tate-Lichtenbaum Pairing
3545
private static final BigInteger g = new BigInteger(Hex.decode("66FC2A43 2B6EA392 148F1586 7D623068\n" +
3646
" C6A87BD1 FB94C41E 27FABE65 8E015A87\n" +
3747
" 371E9474 4C96FEDA 449AE956 3F8BC446\n" +
@@ -41,13 +51,6 @@ public class SAKKEPublicKeyParameters
4151
" D682C033 A7942BCC E3720F20 B9B7B040\n" +
4252
" 3C8CAE87 B7A0042A CDE0FAB3 6461EA46"));
4353

44-
private static final BigInteger q = new BigInteger(
45-
"265EAEC7C2958FF69971846636B4195E905B0338672D20986FA6B8D62CF8068B" +
46-
"BD02AAC9F8BF03C6C8A1CC354C69672C39E46CE7FDF222864D5B49FD2999A9B4" +
47-
"389B1921CC9AD335144AB173595A07386DABFD2A0C614AA0A9F3CF14870F026A" +
48-
"A7E535ABD5A5C7C7FF38FA08E2615F6C203177C42B1EB3A1D99B601EBFAA17FB", 16
49-
);
50-
5154
private static final ECCurve.Fp curve = new ECCurve.Fp(
5255
p, // Prime p
5356
BigInteger.valueOf(-3).mod(p), // a = -3
@@ -56,18 +59,34 @@ public class SAKKEPublicKeyParameters
5659
BigInteger.ONE // Cofactor = 1
5760
);
5861

62+
5963
private static final ECPoint P = curve.createPoint(Px, Py);
60-
private final ECPoint Z; // KMS Public Key: Z = [z]P
64+
private final ECPoint Z;
65+
66+
private final BigInteger identifier; // User's identity
6167

6268
private static final int n = 128; // SSV bit length
6369

64-
public SAKKEPublicKeyParameters(ECPoint Z)
70+
private final Digest digest = new SHA256Digest();
71+
72+
public SAKKEPublicKeyParameters(BigInteger identifier, ECPoint Z)
6573
{
6674
super(false);
75+
this.identifier = identifier;
6776
this.Z = Z;
6877
}
6978

7079
// Getters
80+
public BigInteger getIdentifier()
81+
{
82+
return identifier;
83+
}
84+
85+
public ECPoint getZ()
86+
{
87+
return Z;
88+
}
89+
7190
public ECCurve getCurve()
7291
{
7392
return curve;
@@ -78,12 +97,7 @@ public ECPoint getP()
7897
return P;
7998
}
8099

81-
public ECPoint getZ()
82-
{
83-
return Z;
84-
}
85-
86-
public BigInteger getp()
100+
public BigInteger getPrime()
87101
{
88102
return p;
89103
}
@@ -97,4 +111,14 @@ public int getN()
97111
{
98112
return n;
99113
}
114+
115+
public Digest getDigest()
116+
{
117+
return digest;
118+
}
119+
120+
public BigInteger getG()
121+
{
122+
return g;
123+
}
100124
}

core/src/test/java/org/bouncycastle/crypto/kems/test/SAKKEKEMSTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ public String getName()
3838
public void performTest()
3939
throws Exception
4040
{
41+
42+
final BigInteger Px = new BigInteger(
43+
"53FC09EE332C29AD0A7990053ED9B52A2B1A2FD60AEC69C698B2F204B6FF7CBF" +
44+
"B5EDB6C0F6CE2308AB10DB9030B09E1043D5F22CDB9DFA55718BD9E7406CE890" +
45+
"9760AF765DD5BCCB337C86548B72F2E1A702C3397A60DE74A7C1514DBA66910D" +
46+
"D5CFB4CC80728D87EE9163A5B63F73EC80EC46C4967E0979880DC8ABEAE63895", 16
47+
);
48+
49+
final BigInteger Py = new BigInteger(
50+
"0A8249063F6009F1F9F1F0533634A135D3E82016029906963D778D821E141178" +
51+
"F5EA69F4654EC2B9E7F7F5E5F0DE55F66B598CCF9A140B2E416CFF0CA9E032B9" +
52+
"70DAE117AD547C6CCAD696B5B7652FE0AC6F1E80164AA989492D979FC5A4D5F2" +
53+
"13515AD7E9CB99A980BDAD5AD5BB4636ADB9B5706A67DCDE75573FD71BEF16D7", 16
54+
);
4155
BigInteger g = new BigInteger(Hex.decode("66FC2A43 2B6EA392 148F1586 7D623068" +
4256
" C6A87BD1 FB94C41E 27FABE65 8E015A87" +
4357
" 371E9474 4C96FEDA 449AE956 3F8BC446" +
@@ -127,14 +141,18 @@ public void performTest()
127141
SecureRandom random = new FixedSecureRandom(new FixedSecureRandom.Source[]{new FixedSecureRandom.Data(ssv),
128142
new FixedSecureRandom.Data(b)});
129143
SAKKEKEMSGenerator generator = new SAKKEKEMSGenerator(random);
130-
SecretWithEncapsulation rlt = generator.generateEncapsulated(null);
144+
SecretWithEncapsulation rlt = generator.generateEncapsulated(new SAKKEPublicKeyParameters(new BigInteger(b), curve.createPoint(Zx, Zy)));
131145

146+
ECPoint P = curve.createPoint(Px, Py);
147+
148+
BigInteger computed_g2 = SAKKEKEMExtractor.computePairing(P, P, p, q);
149+
Assert.assertTrue(computed_g2.equals(g));
132150
ECPoint K_bS = curve.createPoint(kbx, kby);
133151

134-
SAKKEKEMExtractor extractor = new SAKKEKEMExtractor(new SAKKEPrivateKeyParameters(new BigInteger(b), K_bS,
135-
new SAKKEPublicKeyParameters(curve.createPoint(Zx, Zy))));
152+
153+
SAKKEKEMExtractor extractor = new SAKKEKEMExtractor(new SAKKEPrivateKeyParameters(z, K_bS,
154+
new SAKKEPublicKeyParameters(new BigInteger(b), curve.createPoint(Zx, Zy))));
136155
byte[] test = extractor.extractSecret(rlt.getEncapsulation());
137156
Assert.assertTrue(Arrays.areEqual(test, ssv));
138-
139157
}
140158
}

0 commit comments

Comments
 (0)