Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/XECOperations.java
41161 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ec;2627import sun.security.util.math.IntegerFieldModuloP;28import sun.security.util.math.ImmutableIntegerModuloP;29import sun.security.util.math.IntegerModuloP;30import sun.security.util.math.MutableIntegerModuloP;31import sun.security.util.math.SmallValue;32import sun.security.util.math.intpoly.IntegerPolynomial25519;33import sun.security.util.math.intpoly.IntegerPolynomial448;3435import java.math.BigInteger;36import java.security.ProviderException;37import java.security.SecureRandom;3839public class XECOperations {4041private final XECParameters params;42private final IntegerFieldModuloP field;43private final ImmutableIntegerModuloP zero;44private final ImmutableIntegerModuloP one;45private final SmallValue a24;46private final ImmutableIntegerModuloP basePoint;4748public XECOperations(XECParameters c) {49this.params = c;5051BigInteger p = params.getP();52this.field = getIntegerFieldModulo(p);53this.zero = field.getElement(BigInteger.ZERO).fixed();54this.one = field.get1().fixed();55this.a24 = field.getSmallValue(params.getA24());56this.basePoint = field.getElement(57BigInteger.valueOf(c.getBasePoint()));58}5960public XECParameters getParameters() {61return params;62}6364public byte[] generatePrivate(SecureRandom random) {65byte[] result = new byte[this.params.getBytes()];66random.nextBytes(result);67return result;68}6970/**71* Compute a public key from an encoded private key. This method will72* modify the supplied array in order to prune it.73*/74public BigInteger computePublic(byte[] k) {75pruneK(k);76return pointMultiply(k, this.basePoint).asBigInteger();77}7879/**80*81* Multiply an encoded scalar with a point as a BigInteger and return an82* encoded point. The array k holding the scalar will be pruned by83* modifying it in place.84*85* @param k an encoded scalar86* @param u the u-coordinate of a point as a BigInteger87* @return the encoded product88*/89public byte[] encodedPointMultiply(byte[] k, BigInteger u) {90pruneK(k);91ImmutableIntegerModuloP elemU = field.getElement(u);92return pointMultiply(k, elemU).asByteArray(params.getBytes());93}9495/**96*97* Multiply an encoded scalar with an encoded point and return an encoded98* point. The array k holding the scalar will be pruned by99* modifying it in place.100*101* @param k an encoded scalar102* @param u an encoded point103* @return the encoded product104*/105public byte[] encodedPointMultiply(byte[] k, byte[] u) {106pruneK(k);107ImmutableIntegerModuloP elemU = decodeU(u);108return pointMultiply(k, elemU).asByteArray(params.getBytes());109}110111/**112* Return the field element corresponding to an encoded u-coordinate.113* This method prunes u by modifying it in place.114*115* @param u116* @param bits117* @return118*/119private ImmutableIntegerModuloP decodeU(byte[] u, int bits) {120121maskHighOrder(u, bits);122123return field.getElement(u);124}125126/**127* Mask off the high order bits of an encoded integer in an array. The128* array is modified in place.129*130* @param arr an array containing an encoded integer131* @param bits the number of bits to keep132* @return the number, in range [1,8], of bits kept in the highest byte133*/134private static byte maskHighOrder(byte[] arr, int bits) {135136int lastByteIndex = arr.length - 1;137byte bitsMod8 = (byte) (bits % 8);138byte highBits = bitsMod8 == 0 ? 8 : bitsMod8;139byte msbMaskOff = (byte) ((1 << highBits) - 1);140arr[lastByteIndex] &= msbMaskOff;141142return highBits;143}144145/**146* Prune an encoded scalar value by modifying it in place. The extra147* high-order bits are masked off, the highest valid bit it set, and the148* number is rounded down to a multiple of the cofactor.149*150* @param k an encoded scalar value151* @param bits the number of bits in the scalar152* @param logCofactor the base-2 logarithm of the cofactor153*/154private static void pruneK(byte[] k, int bits, int logCofactor) {155156int lastByteIndex = k.length - 1;157158// mask off unused high-order bits159byte highBits = maskHighOrder(k, bits);160161// set the highest bit162byte msbMaskOn = (byte) (1 << (highBits - 1));163k[lastByteIndex] |= msbMaskOn;164165// round down to a multiple of the cofactor166byte lsbMaskOff = (byte) (0xFF << logCofactor);167k[0] &= lsbMaskOff;168}169170private void pruneK(byte[] k) {171pruneK(k, params.getBits(), params.getLogCofactor());172}173174private ImmutableIntegerModuloP decodeU(byte [] u) {175return decodeU(u, params.getBits());176}177178// Constant-time conditional swap179private static void cswap(int swap, MutableIntegerModuloP x1,180MutableIntegerModuloP x2) {181182x1.conditionalSwapWith(x2, swap);183}184185private static IntegerFieldModuloP getIntegerFieldModulo(BigInteger p) {186187if (p.equals(IntegerPolynomial25519.MODULUS)) {188return new IntegerPolynomial25519();189}190else if (p.equals(IntegerPolynomial448.MODULUS)) {191return new IntegerPolynomial448();192}193194throw new ProviderException("Unsupported prime: " + p.toString());195}196197private int bitAt(byte[] arr, int index) {198int byteIndex = index / 8;199int bitIndex = index % 8;200return (arr[byteIndex] & (1 << bitIndex)) >> bitIndex;201}202203/*204* Constant-time Montgomery ladder that computes k*u and returns the205* result as a field element.206*/207private IntegerModuloP pointMultiply(byte[] k,208ImmutableIntegerModuloP u) {209210ImmutableIntegerModuloP x_1 = u;211MutableIntegerModuloP x_2 = this.one.mutable();212MutableIntegerModuloP z_2 = this.zero.mutable();213MutableIntegerModuloP x_3 = u.mutable();214MutableIntegerModuloP z_3 = this.one.mutable();215int swap = 0;216217// Variables below are reused to avoid unnecessary allocation218// They will be assigned in the loop, so initial value doesn't matter219MutableIntegerModuloP m1 = this.zero.mutable();220MutableIntegerModuloP DA = this.zero.mutable();221MutableIntegerModuloP E = this.zero.mutable();222MutableIntegerModuloP a24_times_E = this.zero.mutable();223224// Comments describe the equivalent operations from RFC 7748225// In comments, A(m1) means the variable m1 holds the value A226for (int t = params.getBits() - 1; t >= 0; t--) {227int k_t = bitAt(k, t);228swap = swap ^ k_t;229cswap(swap, x_2, x_3);230cswap(swap, z_2, z_3);231swap = k_t;232233// A(m1) = x_2 + z_2234m1.setValue(x_2).setSum(z_2);235// D = x_3 - z_3236// DA = D * A(m1)237DA.setValue(x_3).setDifference(z_3).setProduct(m1);238// AA(m1) = A(m1)^2239m1.setSquare();240// B(x_2) = x_2 - z_2241x_2.setDifference(z_2);242// C = x_3 + z_3243// CB(x_3) = C * B(x_2)244x_3.setSum(z_3).setProduct(x_2);245// BB(x_2) = B^2246x_2.setSquare();247// E = AA(m1) - BB(x_2)248E.setValue(m1).setDifference(x_2);249// compute a24 * E using SmallValue250a24_times_E.setValue(E);251a24_times_E.setProduct(this.a24);252253// assign results to x_3, z_3, x_2, z_2254// x_2 = AA(m1) * BB255x_2.setProduct(m1);256// z_2 = E * (AA(m1) + a24 * E)257z_2.setValue(m1).setSum(a24_times_E).setProduct(E);258// z_3 = x_1*(DA - CB(x_3))^2259z_3.setValue(DA).setDifference(x_3).setSquare().setProduct(x_1);260// x_3 = (CB(x_3) + DA)^2261x_3.setSum(DA).setSquare();262}263264cswap(swap, x_2, x_3);265cswap(swap, z_2, z_3);266267// return (x_2 * z_2^(p - 2))268return x_2.setProduct(z_2.multiplicativeInverse());269}270}271272273