1 // Copyright 2023 Google Inc. 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.security.GeneralSecurityException; 21 import java.util.Objects; 22 23 /** Describes the parameters of an {@link KmsAeadKey} */ 24 public final class LegacyKmsAeadParameters extends AeadParameters { 25 26 /** 27 * Describes how the prefix is computed. There are two main possibilities: NO_PREFIX (empty 28 * prefix) and TINK (prefix the ciphertext with 0x01 followed by a 4-byte key id in big endian). 29 */ 30 @Immutable 31 public static final class Variant { 32 public static final Variant TINK = new Variant("TINK"); 33 public static final Variant NO_PREFIX = new Variant("NO_PREFIX"); 34 35 private final String name; 36 Variant(String name)37 private Variant(String name) { 38 this.name = name; 39 } 40 41 @Override toString()42 public String toString() { 43 return name; 44 } 45 } 46 47 private final String keyUri; 48 private final Variant variant; 49 LegacyKmsAeadParameters(String keyUri, Variant variant)50 private LegacyKmsAeadParameters(String keyUri, Variant variant) { 51 this.keyUri = keyUri; 52 this.variant = variant; 53 } 54 create(String keyUri)55 public static LegacyKmsAeadParameters create(String keyUri) throws GeneralSecurityException { 56 return new LegacyKmsAeadParameters(keyUri, Variant.NO_PREFIX); 57 } 58 create(String keyUri, Variant variant)59 public static LegacyKmsAeadParameters create(String keyUri, Variant variant) { 60 return new LegacyKmsAeadParameters(keyUri, variant); 61 } 62 keyUri()63 public String keyUri() { 64 return keyUri; 65 } 66 variant()67 public Variant variant() { 68 return variant; 69 } 70 71 @Override hasIdRequirement()72 public boolean hasIdRequirement() { 73 return variant != Variant.NO_PREFIX; 74 } 75 76 @Override equals(Object o)77 public boolean equals(Object o) { 78 if (!(o instanceof LegacyKmsAeadParameters)) { 79 return false; 80 } 81 LegacyKmsAeadParameters that = (LegacyKmsAeadParameters) o; 82 return that.keyUri.equals(keyUri) && that.variant.equals(variant); 83 } 84 85 @Override hashCode()86 public int hashCode() { 87 return Objects.hash(LegacyKmsAeadParameters.class, keyUri, variant); 88 } 89 90 @Override toString()91 public String toString() { 92 return "LegacyKmsAead Parameters (keyUri: " + keyUri + ", variant: " + variant + ")"; 93 } 94 } 95