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.prf; 18 19 import java.security.GeneralSecurityException; 20 import java.security.InvalidAlgorithmParameterException; 21 import java.util.Objects; 22 23 /** Describes the parameters of an {@link AesCmacPrfKey}. */ 24 public final class AesCmacPrfParameters extends PrfParameters { create(int keySizeBytes)25 public static AesCmacPrfParameters create(int keySizeBytes) throws GeneralSecurityException { 26 if (keySizeBytes != 16 && keySizeBytes != 32) { 27 throw new InvalidAlgorithmParameterException( 28 String.format( 29 "Invalid key size %d; only 128-bit and 256-bit are supported", keySizeBytes * 8)); 30 } 31 return new AesCmacPrfParameters(keySizeBytes); 32 } 33 34 private final int keySizeBytes; 35 AesCmacPrfParameters(int keySizeBytes)36 private AesCmacPrfParameters(int keySizeBytes) { 37 this.keySizeBytes = keySizeBytes; 38 } 39 getKeySizeBytes()40 public int getKeySizeBytes() { 41 return keySizeBytes; 42 } 43 44 @Override equals(Object o)45 public boolean equals(Object o) { 46 if (!(o instanceof AesCmacPrfParameters)) { 47 return false; 48 } 49 AesCmacPrfParameters that = (AesCmacPrfParameters) o; 50 return that.getKeySizeBytes() == getKeySizeBytes(); 51 } 52 53 @Override hashCode()54 public int hashCode() { 55 return Objects.hash(AesCmacPrfParameters.class, keySizeBytes); 56 } 57 58 @Override hasIdRequirement()59 public boolean hasIdRequirement() { 60 return false; 61 } 62 63 @Override toString()64 public String toString() { 65 return "AesCmac PRF Parameters (" + keySizeBytes + "-byte key)"; 66 } 67 } 68