1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.signature; 18 19 import com.google.errorprone.annotations.Immutable; 20 import java.util.Objects; 21 22 /** This class describes the parameters of an {@link Ed25519Key}. */ 23 public final class Ed25519Parameters extends SignatureParameters { 24 /** 25 * An enum-like class with constant instances, which explains how the prefix is computed. 26 * 27 * <p>The standard Ed25519 key is used for variant "NO_PREFIX". Other variants slightly change how 28 * the signature is computed, or add a prefix to every computation depending on the key id. 29 */ 30 @Immutable 31 public static final class Variant { 32 public static final Variant TINK = new Variant("TINK"); 33 public static final Variant CRUNCHY = new Variant("CRUNCHY"); 34 public static final Variant LEGACY = new Variant("LEGACY"); 35 public static final Variant NO_PREFIX = new Variant("NO_PREFIX"); 36 37 private final String name; 38 Variant(String name)39 private Variant(String name) { 40 this.name = name; 41 } 42 43 @Override toString()44 public String toString() { 45 return name; 46 } 47 } 48 49 /** Creates an instance with NO_PREFIX variant. */ create()50 public static Ed25519Parameters create() { 51 return new Ed25519Parameters(Variant.NO_PREFIX); 52 } 53 54 /** Creates an instance with given variant. */ create(Variant variant)55 public static Ed25519Parameters create(Variant variant) { 56 return new Ed25519Parameters(variant); 57 } 58 59 private final Variant variant; 60 Ed25519Parameters(Variant variant)61 private Ed25519Parameters(Variant variant) { 62 this.variant = variant; 63 } 64 65 /** Returns a variant object. */ getVariant()66 public Variant getVariant() { 67 return variant; 68 } 69 70 @Override equals(Object o)71 public boolean equals(Object o) { 72 if (!(o instanceof Ed25519Parameters)) { 73 return false; 74 } 75 Ed25519Parameters that = (Ed25519Parameters) o; 76 return that.getVariant() == getVariant(); 77 } 78 79 @Override hashCode()80 public int hashCode() { 81 return Objects.hash(Ed25519Parameters.class, variant); 82 } 83 84 @Override hasIdRequirement()85 public boolean hasIdRequirement() { 86 return variant != Variant.NO_PREFIX; 87 } 88 89 @Override toString()90 public String toString() { 91 return "Ed25519 Parameters (variant: " + variant + ")"; 92 } 93 } 94