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.aead; 18 19 import com.google.errorprone.annotations.Immutable; 20 import java.util.Objects; 21 22 /** Describes the parameters of an {@link XChaChaPoly1305Key}. */ 23 public final class XChaCha20Poly1305Parameters extends AeadParameters { 24 /** 25 * Describes how the prefix is computed. For AEAD there are three main possibilities: NO_PREFIX 26 * (empty prefix), TINK (prefix the ciphertext with 0x01 followed by a 4-byte key id in big endian 27 * format) or CRUNCHY (prefix the ciphertext with 0x00 followed by a 4-byte key id in big endian 28 * format). 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 NO_PREFIX = new Variant("NO_PREFIX"); 35 36 private final String name; 37 Variant(String name)38 private Variant(String name) { 39 this.name = name; 40 } 41 42 @Override toString()43 public String toString() { 44 return name; 45 } 46 } 47 create()48 public static XChaCha20Poly1305Parameters create() { 49 return new XChaCha20Poly1305Parameters(Variant.NO_PREFIX); 50 } 51 create(Variant variant)52 public static XChaCha20Poly1305Parameters create(Variant variant) { 53 return new XChaCha20Poly1305Parameters(variant); 54 } 55 56 private final Variant variant; 57 XChaCha20Poly1305Parameters(Variant variant)58 private XChaCha20Poly1305Parameters(Variant variant) { 59 this.variant = variant; 60 } 61 62 /** Returns a variant object. */ getVariant()63 public Variant getVariant() { 64 return variant; 65 } 66 67 @Override equals(Object o)68 public boolean equals(Object o) { 69 if (!(o instanceof XChaCha20Poly1305Parameters)) { 70 return false; 71 } 72 XChaCha20Poly1305Parameters that = (XChaCha20Poly1305Parameters) o; 73 return that.getVariant() == getVariant(); 74 } 75 76 @Override hashCode()77 public int hashCode() { 78 return Objects.hash(XChaCha20Poly1305Parameters.class, variant); 79 } 80 81 @Override hasIdRequirement()82 public boolean hasIdRequirement() { 83 return variant != Variant.NO_PREFIX; 84 } 85 86 @Override toString()87 public String toString() { 88 return "XChaCha20Poly1305 Parameters (variant: " + variant + ")"; 89 } 90 } 91