Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/XECParameters.java
41161 views
/*1* Copyright (c) 2018, 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;2627import java.io.IOException;28import java.math.BigInteger;29import java.security.spec.AlgorithmParameterSpec;30import java.security.spec.NamedParameterSpec;31import java.util.Map;32import java.util.HashMap;33import java.util.function.Function;3435import sun.security.util.KnownOIDs;36import sun.security.util.ObjectIdentifier;37import sun.security.x509.AlgorithmId;3839public class XECParameters {4041static ParametersMap<XECParameters> namedParams = new ParametersMap<>();4243// Naming/identification parameters44private final ObjectIdentifier oid;45private final String name;4647// Curve/field parameters48private final int bits;49private final BigInteger p;50private final int logCofactor;51private final int a24;52private final byte basePoint;5354/**55*56* Construct an object holding the supplied parameters. No parameters are57* checked, so this method always succeeds. This method supports58* Montgomery curves of the form y^2 = x^3 + ax^2 + x.59*60* @param bits The number of relevant bits in a public/private key.61* @param p The prime that defines the finite field.62* @param a24 The value of (a - 2) / 4, where a is the second-degree curve63* coefficient.64* @param basePoint The point that generates the desired group65* @param logCofactor The base-2 logarithm of the cofactor of the curve66* @param oid67* @param name68*/69public XECParameters(int bits, BigInteger p, int a24,70byte basePoint, int logCofactor,71ObjectIdentifier oid, String name) {7273this.bits = bits;74this.logCofactor = logCofactor;75this.p = p;76this.a24 = a24;77this.basePoint = basePoint;78this.oid = oid;79this.name = name;8081}8283public int getBits() {84return bits;85}86public int getBytes() {87return (bits + 7) / 8;88}89public int getLogCofactor() {90return logCofactor;91}92public BigInteger getP() {93return p;94}95public int getA24() {96return a24;97}98public byte getBasePoint() {99return basePoint;100}101public ObjectIdentifier getOid() {102return oid;103}104public String getName() {105return name;106}107108static {109final BigInteger TWO = BigInteger.valueOf(2);110111Map<Integer, XECParameters> bySize = new HashMap<>();112Map<ObjectIdentifier, XECParameters> byOid = new HashMap<>();113Map<String, XECParameters> byName = new HashMap<>();114115// set up X25519116try {117BigInteger p = TWO.pow(255).subtract(BigInteger.valueOf(19));118addParameters(255, p, 121665, (byte)0x09, 3,119KnownOIDs.X25519.value(), NamedParameterSpec.X25519.getName(),120bySize, byOid, byName);121122} catch (IOException ex) {123// Unable to set X25519 parameters---it will be disabled124}125126// set up X448127try {128BigInteger p = TWO.pow(448).subtract(TWO.pow(224))129.subtract(BigInteger.ONE);130addParameters(448, p, 39081, (byte)0x05, 2,131KnownOIDs.X448.value(), NamedParameterSpec.X448.getName(),132bySize, byOid, byName);133134} catch (IOException ex) {135// Unable to set X448 parameters---it will be disabled136}137138namedParams.fix();139}140141private static void addParameters(int bits, BigInteger p, int a24,142byte basePoint, int logCofactor, String objectId, String name,143Map<Integer, XECParameters> bySize,144Map<ObjectIdentifier, XECParameters> byOid,145Map<String, XECParameters> byName) throws IOException {146147ObjectIdentifier oid = ObjectIdentifier.of(objectId);148XECParameters params =149new XECParameters(bits, p, a24, basePoint, logCofactor, oid, name);150namedParams.put(name.toLowerCase(), oid, bits, params);151}152153boolean oidEquals(XECParameters other) {154return oid.equals(other.getOid());155}156157158public static159<T extends Throwable>160XECParameters getBySize(Function<String, T> exception,161int size) throws T {162163return namedParams.getBySize(exception, size);164}165166public static167<T extends Throwable>168XECParameters get(Function<String, T> exception,169AlgorithmId algId) throws T {170171return namedParams.get(exception, algId);172}173174public static175<T extends Throwable>176XECParameters get(Function<String, T> exception,177AlgorithmParameterSpec params) throws T {178179return namedParams.get(exception, params);180}181}182183184185