Home | History | Annotate | Download | only in handshake
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.tlswire.handshake;
     18 
     19 /**
     20  * {@code EllipticCurve} enum from RFC 4492 section 5.1.1. Curves are assigned
     21  * via the
     22  * <a href="https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8">IANA registry</a>.
     23  */
     24 public enum EllipticCurve {
     25     SECT163K1(1, "sect163k1"),
     26     SECT163R1(2, "sect163r1"),
     27     SECT163R2(3, "sect163r2"),
     28     SECT193R1(4, "sect193r1"),
     29     SECT193R2(5, "sect193r2"),
     30     SECT233K1(6, "sect233k1"),
     31     SECT233R1(7, "sect233r1"),
     32     SECT239K1(8, "sect239k1"),
     33     SECT283K1(9, "sect283k1"),
     34     SECT283R1(10, "sect283r1"),
     35     SECT409K1(11, "sect409k1"),
     36     SECT409R1(12, "sect409r1"),
     37     SECT571K1(13, "sect571k1"),
     38     SECT571R1(14, "sect571r1"),
     39     SECP160K1(15, "secp160k1"),
     40     SECP160R1(16, "secp160r1"),
     41     SECP160R2(17, "secp160r2"),
     42     SECP192K1(18, "secp192k1"),
     43     SECP192R1(19, "secp192r1"),
     44     SECP224K1(20, "secp224k1"),
     45     SECP224R1(21, "secp224r1"),
     46     SECP256K1(22, "secp256k1"),
     47     SECP256R1(23, "secp256r1"),
     48     SECP384R1(24, "secp384r1"),
     49     SECP521R1(25, "secp521r1"),
     50     BRAINPOOLP256R1(26, "brainpoolP256r1"),
     51     BRAINPOOLP384R1(27, "brainpoolP384r1"),
     52     BRAINPOOLP521R1(28, "brainpoolP521r1"),
     53     X25519(29, "x25519"),
     54     X448(30, "x448"),
     55     ARBITRARY_PRIME(0xFF01, "arbitrary_explicit_prime_curves"),
     56     ARBITRARY_CHAR2(0xFF02, "arbitrary_explicit_char2_curves");
     57 
     58     public final int identifier;
     59     public final String name;
     60 
     61     private EllipticCurve(int identifier, String name) {
     62         this.identifier = identifier;
     63         this.name = name;
     64     }
     65 
     66     public static EllipticCurve fromIdentifier(int identifier) {
     67         for (EllipticCurve curve : values()) {
     68             if (curve.identifier == identifier) {
     69                 return curve;
     70             }
     71         }
     72         throw new AssertionError("Unknown curve identifier " + identifier);
     73     }
     74 
     75     @Override
     76     public String toString() {
     77         StringBuilder sb = new StringBuilder(name);
     78         sb.append(" (");
     79         sb.append(identifier);
     80         sb.append(')');
     81         return sb.toString();
     82     }
     83 }
     84