author | apetcher |
Tue, 22 May 2018 13:44:02 -0400 | |
branch | JDK-8171279-XDH-TLS-branch |
changeset 56589 | bafd8be2f970 |
parent 50053 | 9bc1e6487cbb |
permissions | -rw-r--r-- |
50053 | 1 |
/* |
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.ec; |
|
27 |
||
28 |
import sun.security.util.math.IntegerFieldModuloP; |
|
29 |
import sun.security.util.math.ImmutableIntegerModuloP; |
|
30 |
import sun.security.util.math.IntegerModuloP; |
|
31 |
import sun.security.util.math.MutableIntegerModuloP; |
|
32 |
import sun.security.util.math.SmallValue; |
|
33 |
import sun.security.util.math.intpoly.IntegerPolynomial25519; |
|
34 |
import sun.security.util.math.intpoly.IntegerPolynomial448; |
|
56589
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
50053
diff
changeset
|
35 |
import sun.security.util.XECParameters; |
50053 | 36 |
|
37 |
import java.math.BigInteger; |
|
38 |
import java.security.ProviderException; |
|
39 |
import java.security.SecureRandom; |
|
40 |
||
41 |
public class XECOperations { |
|
42 |
||
43 |
private final XECParameters params; |
|
44 |
private final IntegerFieldModuloP field; |
|
45 |
private final ImmutableIntegerModuloP zero; |
|
46 |
private final ImmutableIntegerModuloP one; |
|
47 |
private final SmallValue a24; |
|
48 |
private final ImmutableIntegerModuloP basePoint; |
|
49 |
||
50 |
public XECOperations(XECParameters c) { |
|
51 |
this.params = c; |
|
52 |
||
53 |
BigInteger p = params.getP(); |
|
54 |
this.field = getIntegerFieldModulo(p); |
|
55 |
this.zero = field.getElement(BigInteger.ZERO).fixed(); |
|
56 |
this.one = field.get1().fixed(); |
|
57 |
this.a24 = field.getSmallValue(params.getA24()); |
|
58 |
this.basePoint = field.getElement( |
|
59 |
BigInteger.valueOf(c.getBasePoint())); |
|
60 |
} |
|
61 |
||
62 |
public XECParameters getParameters() { |
|
63 |
return params; |
|
64 |
} |
|
65 |
||
66 |
public byte[] generatePrivate(SecureRandom random) { |
|
67 |
byte[] result = new byte[this.params.getBytes()]; |
|
68 |
random.nextBytes(result); |
|
69 |
return result; |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Compute a public key from an encoded private key. This method will |
|
74 |
* modify the supplied array in order to prune it. |
|
75 |
*/ |
|
76 |
public BigInteger computePublic(byte[] k) { |
|
77 |
pruneK(k); |
|
78 |
return pointMultiply(k, this.basePoint).asBigInteger(); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* |
|
83 |
* Multiply an encoded scalar with a point as a BigInteger and return an |
|
84 |
* encoded point. The array k holding the scalar will be pruned by |
|
85 |
* modifying it in place. |
|
86 |
* |
|
87 |
* @param k an encoded scalar |
|
88 |
* @param u the u-coordinate of a point as a BigInteger |
|
89 |
* @return the encoded product |
|
90 |
*/ |
|
91 |
public byte[] encodedPointMultiply(byte[] k, BigInteger u) { |
|
92 |
pruneK(k); |
|
93 |
ImmutableIntegerModuloP elemU = field.getElement(u); |
|
94 |
return pointMultiply(k, elemU).asByteArray(params.getBytes()); |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* |
|
99 |
* Multiply an encoded scalar with an encoded point and return an encoded |
|
100 |
* point. The array k holding the scalar will be pruned by |
|
101 |
* modifying it in place. |
|
102 |
* |
|
103 |
* @param k an encoded scalar |
|
104 |
* @param u an encoded point |
|
105 |
* @return the encoded product |
|
106 |
*/ |
|
107 |
public byte[] encodedPointMultiply(byte[] k, byte[] u) { |
|
108 |
pruneK(k); |
|
109 |
ImmutableIntegerModuloP elemU = decodeU(u); |
|
110 |
return pointMultiply(k, elemU).asByteArray(params.getBytes()); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Return the field element corresponding to an encoded u-coordinate. |
|
115 |
* This method prunes u by modifying it in place. |
|
116 |
* |
|
117 |
* @param u |
|
118 |
* @param bits |
|
119 |
* @return |
|
120 |
*/ |
|
121 |
private ImmutableIntegerModuloP decodeU(byte[] u, int bits) { |
|
122 |
||
123 |
maskHighOrder(u, bits); |
|
124 |
||
125 |
return field.getElement(u); |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Mask off the high order bits of an encoded integer in an array. The |
|
130 |
* array is modified in place. |
|
131 |
* |
|
132 |
* @param arr an array containing an encoded integer |
|
133 |
* @param bits the number of bits to keep |
|
134 |
* @return the number, in range [1,8], of bits kept in the highest byte |
|
135 |
*/ |
|
136 |
private static byte maskHighOrder(byte[] arr, int bits) { |
|
137 |
||
138 |
int lastByteIndex = arr.length - 1; |
|
139 |
byte bitsMod8 = (byte) (bits % 8); |
|
140 |
byte highBits = bitsMod8 == 0 ? 8 : bitsMod8; |
|
141 |
byte msbMaskOff = (byte) ((1 << highBits) - 1); |
|
142 |
arr[lastByteIndex] &= msbMaskOff; |
|
143 |
||
144 |
return highBits; |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Prune an encoded scalar value by modifying it in place. The extra |
|
149 |
* high-order bits are masked off, the highest valid bit it set, and the |
|
150 |
* number is rounded down to a multiple of the cofactor. |
|
151 |
* |
|
152 |
* @param k an encoded scalar value |
|
153 |
* @param bits the number of bits in the scalar |
|
154 |
* @param logCofactor the base-2 logarithm of the cofactor |
|
155 |
*/ |
|
156 |
private static void pruneK(byte[] k, int bits, int logCofactor) { |
|
157 |
||
158 |
int lastByteIndex = k.length - 1; |
|
159 |
||
160 |
// mask off unused high-order bits |
|
161 |
byte highBits = maskHighOrder(k, bits); |
|
162 |
||
163 |
// set the highest bit |
|
164 |
byte msbMaskOn = (byte) (1 << (highBits - 1)); |
|
165 |
k[lastByteIndex] |= msbMaskOn; |
|
166 |
||
167 |
// round down to a multiple of the cofactor |
|
168 |
byte lsbMaskOff = (byte) (0xFF << logCofactor); |
|
169 |
k[0] &= lsbMaskOff; |
|
170 |
} |
|
171 |
||
172 |
private void pruneK(byte[] k) { |
|
173 |
pruneK(k, params.getBits(), params.getLogCofactor()); |
|
174 |
} |
|
175 |
||
176 |
private ImmutableIntegerModuloP decodeU(byte [] u) { |
|
177 |
return decodeU(u, params.getBits()); |
|
178 |
} |
|
179 |
||
180 |
// Constant-time conditional swap |
|
181 |
private static void cswap(int swap, MutableIntegerModuloP x1, |
|
182 |
MutableIntegerModuloP x2) { |
|
183 |
||
184 |
x1.conditionalSwapWith(x2, swap); |
|
185 |
} |
|
186 |
||
187 |
private static IntegerFieldModuloP getIntegerFieldModulo(BigInteger p) { |
|
188 |
||
189 |
if (p.equals(IntegerPolynomial25519.MODULUS)) { |
|
190 |
return new IntegerPolynomial25519(); |
|
191 |
} |
|
192 |
else if (p.equals(IntegerPolynomial448.MODULUS)) { |
|
193 |
return new IntegerPolynomial448(); |
|
194 |
} |
|
195 |
||
196 |
throw new ProviderException("Unsupported prime: " + p.toString()); |
|
197 |
} |
|
198 |
||
199 |
private int bitAt(byte[] arr, int index) { |
|
200 |
int byteIndex = index / 8; |
|
201 |
int bitIndex = index % 8; |
|
202 |
return (arr[byteIndex] & (1 << bitIndex)) >> bitIndex; |
|
203 |
} |
|
204 |
||
205 |
/* |
|
206 |
* Constant-time Montgomery ladder that computes k*u and returns the |
|
207 |
* result as a field element. |
|
208 |
*/ |
|
209 |
private IntegerModuloP pointMultiply(byte[] k, |
|
210 |
ImmutableIntegerModuloP u) { |
|
211 |
||
212 |
ImmutableIntegerModuloP x_1 = u; |
|
213 |
MutableIntegerModuloP x_2 = this.one.mutable(); |
|
214 |
MutableIntegerModuloP z_2 = this.zero.mutable(); |
|
215 |
MutableIntegerModuloP x_3 = u.mutable(); |
|
216 |
MutableIntegerModuloP z_3 = this.one.mutable(); |
|
217 |
int swap = 0; |
|
218 |
||
219 |
// Variables below are reused to avoid unnecessary allocation |
|
220 |
// They will be assigned in the loop, so initial value doesn't matter |
|
221 |
MutableIntegerModuloP m1 = this.zero.mutable(); |
|
222 |
MutableIntegerModuloP DA = this.zero.mutable(); |
|
223 |
MutableIntegerModuloP E = this.zero.mutable(); |
|
224 |
MutableIntegerModuloP a24_times_E = this.zero.mutable(); |
|
225 |
||
226 |
// Comments describe the equivalent operations from RFC 7748 |
|
227 |
// In comments, A(m1) means the variable m1 holds the value A |
|
228 |
for (int t = params.getBits() - 1; t >= 0; t--) { |
|
229 |
int k_t = bitAt(k, t); |
|
230 |
swap = swap ^ k_t; |
|
231 |
cswap(swap, x_2, x_3); |
|
232 |
cswap(swap, z_2, z_3); |
|
233 |
swap = k_t; |
|
234 |
||
235 |
// A(m1) = x_2 + z_2 |
|
236 |
m1.setValue(x_2).setSum(z_2); |
|
237 |
// D = x_3 - z_3 |
|
238 |
// DA = D * A(m1) |
|
239 |
DA.setValue(x_3).setDifference(z_3).setProduct(m1); |
|
240 |
// AA(m1) = A(m1)^2 |
|
241 |
m1.setSquare(); |
|
242 |
// B(x_2) = x_2 - z_2 |
|
243 |
x_2.setDifference(z_2); |
|
244 |
// C = x_3 + z_3 |
|
245 |
// CB(x_3) = C * B(x_2) |
|
246 |
x_3.setSum(z_3).setProduct(x_2); |
|
247 |
// BB(x_2) = B^2 |
|
248 |
x_2.setSquare(); |
|
249 |
// E = AA(m1) - BB(x_2) |
|
250 |
E.setValue(m1).setDifference(x_2); |
|
251 |
// compute a24 * E using SmallValue |
|
252 |
a24_times_E.setValue(E); |
|
253 |
a24_times_E.setProduct(this.a24); |
|
254 |
||
255 |
// assign results to x_3, z_3, x_2, z_2 |
|
256 |
// x_2 = AA(m1) * BB |
|
257 |
x_2.setProduct(m1); |
|
258 |
// z_2 = E * (AA(m1) + a24 * E) |
|
259 |
z_2.setValue(m1).setSum(a24_times_E).setProduct(E); |
|
260 |
// z_3 = x_1*(DA - CB(x_3))^2 |
|
261 |
z_3.setValue(DA).setDifference(x_3).setSquare().setProduct(x_1); |
|
262 |
// x_3 = (CB(x_3) + DA)^2 |
|
263 |
x_3.setSum(DA).setSquare(); |
|
264 |
} |
|
265 |
||
266 |
cswap(swap, x_2, x_3); |
|
267 |
cswap(swap, z_2, z_3); |
|
268 |
||
269 |
// return (x_2 * z_2^(p - 2)) |
|
270 |
return x_2.setProduct(z_2.multiplicativeInverse()); |
|
271 |
} |
|
272 |
} |