src/jdk.crypto.ec/share/classes/sun/security/ec/XECParameters.java
author bpb
Fri, 14 Sep 2018 09:00:22 -0700
changeset 51746 07ae9da7a230
parent 50053 9bc1e6487cbb
child 55353 946f7f2d321c
permissions -rw-r--r--
8210741: Typo in Java API documentation of java.nio.file.Paths Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     1
/*
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     4
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    10
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    15
 * accompanied this code).
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    16
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    20
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    23
 * questions.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    24
 */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    25
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    26
package sun.security.ec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    27
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    28
import java.io.IOException;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    29
import java.math.BigInteger;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    31
import java.security.spec.NamedParameterSpec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    32
import java.util.Collections;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    33
import java.util.Map;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    34
import java.util.HashMap;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    35
import java.util.Optional;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    36
import java.util.function.Function;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    37
import java.util.function.Supplier;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    38
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    39
import sun.security.util.ObjectIdentifier;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    40
import sun.security.x509.AlgorithmId;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    41
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    42
public class XECParameters {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    43
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    44
    // Naming/identification parameters
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    45
    private final ObjectIdentifier oid;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    46
    private final String name;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    47
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    48
    // Curve/field parameters
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    49
    private final int bits;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    50
    private final BigInteger p;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    51
    private final int logCofactor;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    52
    private final int a24;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    53
    private final byte basePoint;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    54
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    55
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    56
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    57
     * Construct an object holding the supplied parameters. No parameters are
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    58
     * checked, so this method always succeeds. This method supports
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    59
     * Montgomery curves of the form y^2 = x^3 + ax^2 + x.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    60
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    61
     * @param bits The number of relevant bits in a public/private key.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    62
     * @param p The prime that defines the finite field.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    63
     * @param a24 The value of (a - 2) / 4, where a is the second-degree curve
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    64
     *            coefficient.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    65
     * @param basePoint The point that generates the desired group
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    66
     * @param logCofactor The base-2 logarithm of the cofactor of the curve
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    67
     * @param oid
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    68
     * @param name
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    69
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    70
    public XECParameters(int bits, BigInteger p, int a24,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    71
                         byte basePoint, int logCofactor,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    72
                         ObjectIdentifier oid, String name) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    73
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    74
        this.bits = bits;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    75
        this.logCofactor = logCofactor;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    76
        this.p = p;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    77
        this.a24 = a24;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    78
        this.basePoint = basePoint;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    79
        this.oid = oid;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    80
        this.name = name;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    81
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    82
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    83
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    84
    public int getBits() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    85
        return bits;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    86
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    87
    public int getBytes() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    88
        return (bits + 7) / 8;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    89
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    90
    public int getLogCofactor() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    91
        return logCofactor;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    92
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    93
    public BigInteger getP() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    94
        return p;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    95
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    96
    public int getA24() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    97
        return a24;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    98
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    99
    public byte getBasePoint() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   100
        return basePoint;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   101
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   102
    public ObjectIdentifier getOid() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   103
        return oid;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   104
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   105
    public String getName() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   106
        return name;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   107
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   108
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   109
    private static final Map<Integer, XECParameters> SIZE_MAP;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   110
    private static final Map<ObjectIdentifier, XECParameters> OID_MAP;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   111
    private static final Map<String, XECParameters> NAME_MAP;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   112
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   113
    static {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   114
        final BigInteger TWO = BigInteger.valueOf(2);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   115
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   116
        Map<Integer, XECParameters> bySize = new HashMap<>();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   117
        Map<ObjectIdentifier, XECParameters> byOid = new HashMap<>();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   118
        Map<String, XECParameters> byName = new HashMap<>();
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   119
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   120
        // set up X25519
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   121
        try {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   122
            BigInteger p = TWO.pow(255).subtract(BigInteger.valueOf(19));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   123
            addParameters(255, p, 121665, (byte) 0x09, 3,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   124
                new int[]{1, 3, 101, 110}, NamedParameterSpec.X25519.getName(),
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   125
                bySize, byOid, byName);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   126
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   127
        } catch (IOException ex) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   128
            // Unable to set X25519 parameters---it will be disabled
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   129
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   130
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   131
        // set up X448
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   132
        try {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   133
            BigInteger p = TWO.pow(448).subtract(TWO.pow(224))
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   134
                .subtract(BigInteger.ONE);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   135
            addParameters(448, p, 39081, (byte) 0x05, 2,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   136
                new int[]{1, 3, 101, 111}, NamedParameterSpec.X448.getName(),
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   137
                bySize, byOid, byName);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   138
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   139
        } catch (IOException ex) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   140
            // Unable to set X448 parameters---it will be disabled
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   141
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   142
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   143
        SIZE_MAP = Collections.unmodifiableMap(bySize);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   144
        OID_MAP = Collections.unmodifiableMap(byOid);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   145
        NAME_MAP = Collections.unmodifiableMap(byName);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   146
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   147
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   148
    private static void addParameters(int bits, BigInteger p, int a24,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   149
        byte basePoint, int logCofactor, int[] oidBytes, String name,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   150
        Map<Integer, XECParameters> bySize,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   151
        Map<ObjectIdentifier, XECParameters> byOid,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   152
        Map<String, XECParameters> byName) throws IOException {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   153
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   154
        ObjectIdentifier oid = new ObjectIdentifier(oidBytes);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   155
        XECParameters params =
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   156
            new XECParameters(bits, p, a24, basePoint, logCofactor, oid, name);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   157
        bySize.put(bits, params);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   158
        byOid.put(oid, params);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   159
        byName.put(name, params);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   160
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   161
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   162
    public static Optional<XECParameters> getByOid(ObjectIdentifier id) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   163
        return Optional.ofNullable(OID_MAP.get(id));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   164
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   165
    public static Optional<XECParameters> getBySize(int size) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   166
        return Optional.ofNullable(SIZE_MAP.get(size));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   167
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   168
    public static Optional<XECParameters> getByName(String name) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   169
        return Optional.ofNullable(NAME_MAP.get(name));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   170
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   171
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   172
    boolean oidEquals(XECParameters other) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   173
        return oid.equals(other.getOid());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   174
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   175
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   176
    // Utility method that is used by the methods below to handle exception
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   177
    // suppliers
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   178
    private static
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   179
    <A, B> Supplier<B> apply(final Function<A, B> func, final A a) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   180
        return new Supplier<B>() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   181
            @Override
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   182
            public B get() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   183
                return func.apply(a);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   184
            }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   185
        };
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   186
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   187
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   188
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   189
     * Get parameters by key size, or throw an exception if no parameters are
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   190
     * defined for the specified key size. This method is used in several
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   191
     * contexts that should throw different exceptions when the parameters
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   192
     * are not found. The first argument is a function that produces the
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   193
     * desired exception.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   194
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   195
     * @param exception a function that produces an exception from a string
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   196
     * @param size the desired key size
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   197
     * @param <T> the type of exception that is thrown
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   198
     * @return the parameters for the specified key size
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   199
     * @throws T when suitable parameters do not exist
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   200
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   201
    public static
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   202
    <T extends Throwable>
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   203
    XECParameters getBySize(Function<String, T> exception,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   204
                            int size) throws T {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   205
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   206
        Optional<XECParameters> xecParams = getBySize(size);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   207
        return xecParams.orElseThrow(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   208
            apply(exception, "Unsupported size: " + size));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   209
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   210
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   211
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   212
     * Get parameters by algorithm ID, or throw an exception if no
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   213
     * parameters are defined for the specified ID. This method is used in
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   214
     * several contexts that should throw different exceptions when the
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   215
     * parameters are not found. The first argument is a function that produces
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   216
     * the desired exception.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   217
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   218
     * @param exception a function that produces an exception from a string
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   219
     * @param algId the algorithm ID
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   220
     * @param <T> the type of exception that is thrown
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   221
     * @return the parameters for the specified algorithm ID
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   222
     * @throws T when suitable parameters do not exist
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   223
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   224
    public static
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   225
    <T extends Throwable>
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   226
    XECParameters get(Function<String, T> exception,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   227
                      AlgorithmId algId) throws T {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   228
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   229
        Optional<XECParameters> xecParams = getByOid(algId.getOID());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   230
        return xecParams.orElseThrow(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   231
            apply(exception, "Unsupported OID: " + algId.getOID()));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   232
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   233
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   234
    /**
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   235
     * Get parameters by algorithm parameter spec, or throw an exception if no
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   236
     * parameters are defined for the spec. This method is used in
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   237
     * several contexts that should throw different exceptions when the
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   238
     * parameters are not found. The first argument is a function that produces
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   239
     * the desired exception.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   240
     *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   241
     * @param exception a function that produces an exception from a string
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   242
     * @param params the algorithm parameters spec
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   243
     * @param <T> the type of exception that is thrown
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   244
     * @return the parameters for the spec
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   245
     * @throws T when suitable parameters do not exist
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   246
     */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   247
    public static
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   248
    <T extends Throwable>
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   249
    XECParameters get(Function<String, T> exception,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   250
                      AlgorithmParameterSpec params) throws T {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   251
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   252
        if (params instanceof NamedParameterSpec) {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   253
            NamedParameterSpec namedParams = (NamedParameterSpec) params;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   254
            Optional<XECParameters> xecParams =
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   255
                getByName(namedParams.getName());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   256
            return xecParams.orElseThrow(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   257
                apply(exception, "Unsupported name: " + namedParams.getName()));
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   258
        } else {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   259
            throw exception.apply("Only NamedParameterSpec is supported.");
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   260
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   261
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   262
}
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   263