33import org .bouncycastle .crypto .EncapsulatedSecretGenerator ;
44import org .bouncycastle .crypto .SecretWithEncapsulation ;
55import org .bouncycastle .crypto .params .AsymmetricKeyParameter ;
6+ import org .bouncycastle .crypto .params .SAKKEPublicKeyParameters ;
67import org .bouncycastle .math .ec .ECCurve ;
78import org .bouncycastle .math .ec .ECPoint ;
89import org .bouncycastle .util .Arrays ;
910import org .bouncycastle .util .BigIntegers ;
10- import org .bouncycastle .util .encoders .Hex ;
1111
1212import java .math .BigInteger ;
1313import java .security .SecureRandom ;
1414
1515public 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
0 commit comments