Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/ed/EdECOperations.java
41162 views
/*1* Copyright (c) 2020, 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.ed;2627import sun.security.ec.point.*;28import sun.security.util.math.IntegerModuloP;29import sun.security.util.math.MutableIntegerModuloP;3031import java.util.function.Function;3233/*34* Base class for Edwards curve ECC implementations.35*/36public abstract class EdECOperations {3738// Curve-specific base point multiplication.39public abstract Point basePointMultiply(byte[] s);4041// Decode curve-specifics to the affinePoint42public abstract <T extends Throwable>43AffinePoint decodeAffinePoint(Function<String, T> exception,44int xLSB, IntegerModuloP y) throws T;4546// Curve specific point from an X,Y point47public abstract ImmutablePoint of(AffinePoint p);4849/*50* Generic method for taking two classes implementing MutablePoint to be51* called by the curve-specific setSum()52*/53public MutablePoint setSum(MutablePoint p1, MutablePoint p2) {54MutableIntegerModuloP t1 = p2.getField().get1().mutable();55MutableIntegerModuloP t2 = p2.getField().get1().mutable();56MutableIntegerModuloP t3 = p2.getField().get1().mutable();57return setSum(p1, p2, t1, t2, t3);58}5960/*61* Generic method for taking a class implementing MutablePoint with a62* scalar to returning the point product using curve-specific methods.63*/64public MutablePoint setProduct(MutablePoint p1, byte[] s) {65MutablePoint p = p1.mutable();66p1.setValue(getNeutral());67MutablePoint addResult = getNeutral().mutable();68MutableIntegerModuloP t1 = p.getField().get0().mutable();69MutableIntegerModuloP t2 = p.getField().get0().mutable();70MutableIntegerModuloP t3 = p.getField().get0().mutable();7172for (int i = 0; i < s.length * 8; i++) {73addResult.setValue(p1);74setSum(addResult, p, t1, t2, t3);75int swap = bitAt(s, i);76p1.conditionalSet(addResult, swap);77setDouble(p, t1, t2);78}7980return p1;81}8283// Abstract method for constructing the neutral point on the curve84protected abstract ImmutablePoint getNeutral();858687// Abstract method for Curve-specific point addition88protected abstract MutablePoint setSum(MutablePoint p1, MutablePoint p2,89MutableIntegerModuloP t1,90MutableIntegerModuloP t2,91MutableIntegerModuloP t3);92// Abstract method for Curve-specific point doubling93protected abstract MutablePoint setDouble(MutablePoint p,94MutableIntegerModuloP t1,95MutableIntegerModuloP t2);9697private static int bitAt(byte[] arr, int index) {98int byteIndex = index / 8;99int bitIndex = index % 8;100return (arr[byteIndex] & (1 << bitIndex)) >> bitIndex;101}102}103104105