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.keyderivation; 18 19 import com.google.crypto.tink.Parameters; 20 import com.google.crypto.tink.prf.PrfParameters; 21 import com.google.errorprone.annotations.CanIgnoreReturnValue; 22 import com.google.errorprone.annotations.Immutable; 23 import java.security.GeneralSecurityException; 24 import java.util.Objects; 25 import javax.annotation.Nullable; 26 27 /** Represents the parameters needed in a {@link PrfBasedKeyDerivationKey}. */ 28 @Immutable 29 public final class PrfBasedKeyDerivationParameters extends KeyDerivationParameters { 30 /** Builds a new PrfBasedKeyDerivationParameters instance. */ 31 public static class Builder { 32 @Nullable private PrfParameters prfParameters = null; 33 34 @Nullable private Parameters derivedKeyParameters = null; 35 36 /** Sets the parameters of the PRF used to create randomness from the salt. */ 37 @CanIgnoreReturnValue setPrfParameters(PrfParameters prfParameters)38 public Builder setPrfParameters(PrfParameters prfParameters) { 39 this.prfParameters = prfParameters; 40 return this; 41 } 42 43 /** 44 * The parameters of the keys which are in the result keyset when the user calls {@code 45 * KeysetDeriver.deriveKeyset()}. 46 */ 47 @CanIgnoreReturnValue setDerivedKeyParameters(Parameters derivedKeyParameters)48 public Builder setDerivedKeyParameters(Parameters derivedKeyParameters) { 49 this.derivedKeyParameters = derivedKeyParameters; 50 return this; 51 } 52 build()53 public PrfBasedKeyDerivationParameters build() throws GeneralSecurityException { 54 if (prfParameters == null) { 55 throw new GeneralSecurityException("PrfParameters must be set."); 56 } 57 if (derivedKeyParameters == null) { 58 throw new GeneralSecurityException("DerivedKeyParameters must be set."); 59 } 60 return new PrfBasedKeyDerivationParameters(prfParameters, derivedKeyParameters); 61 } 62 } 63 64 private final PrfParameters prfParameters; 65 private final Parameters derivedKeyParameters; 66 PrfBasedKeyDerivationParameters( PrfParameters prfParameters, Parameters derivedKeyParameters)67 private PrfBasedKeyDerivationParameters( 68 PrfParameters prfParameters, Parameters derivedKeyParameters) { 69 this.prfParameters = prfParameters; 70 this.derivedKeyParameters = derivedKeyParameters; 71 } 72 builder()73 public static Builder builder() { 74 return new Builder(); 75 } 76 77 /** The parameters of the PRF used to create randomness from the salt. */ getPrfParameters()78 public PrfParameters getPrfParameters() { 79 return prfParameters; 80 } 81 82 /** 83 * The parameters of the keys which are in the result keyset when the user calls {@code 84 * KeysetDeriver.deriveKeyset()}. 85 */ 86 @Override getDerivedKeyParameters()87 public Parameters getDerivedKeyParameters() { 88 return derivedKeyParameters; 89 } 90 91 @Override equals(Object o)92 public boolean equals(Object o) { 93 if (!(o instanceof PrfBasedKeyDerivationParameters)) { 94 return false; 95 } 96 PrfBasedKeyDerivationParameters that = (PrfBasedKeyDerivationParameters) o; 97 return that.getPrfParameters().equals(getPrfParameters()) 98 && that.getDerivedKeyParameters().equals(getDerivedKeyParameters()); 99 } 100 101 @Override hashCode()102 public int hashCode() { 103 return Objects.hash(PrfBasedKeyDerivationParameters.class, prfParameters, derivedKeyParameters); 104 } 105 106 @Override toString()107 public String toString() { 108 return String.format( 109 "PrfBasedKeyDerivationParameters(%s, %s)", prfParameters, derivedKeyParameters); 110 } 111 } 112