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 com.google.errorprone.annotations.CanIgnoreReturnValue; 20 import com.google.errorprone.annotations.Immutable; 21 import java.security.GeneralSecurityException; 22 import java.security.InvalidAlgorithmParameterException; 23 import java.util.Objects; 24 import javax.annotation.Nullable; 25 26 /** Describes the parameters of an {@link HmacPrfKey}. */ 27 public final class HmacPrfParameters extends PrfParameters { 28 private static final int MIN_KEY_SIZE = 16; 29 30 /** The Hash algorithm used. */ 31 @Immutable 32 public static final class HashType { 33 public static final HashType SHA1 = new HashType("SHA1"); 34 public static final HashType SHA224 = new HashType("SHA224"); 35 public static final HashType SHA256 = new HashType("SHA256"); 36 public static final HashType SHA384 = new HashType("SHA384"); 37 public static final HashType SHA512 = new HashType("SHA512"); 38 39 private final String name; 40 HashType(String name)41 private HashType(String name) { 42 this.name = name; 43 } 44 45 @Override toString()46 public String toString() { 47 return name; 48 } 49 } 50 51 /** Builder for HmacPrfParameters. */ 52 public static final class Builder { 53 @Nullable private Integer keySizeBytes = null; 54 @Nullable private HashType hashType = null; 55 Builder()56 private Builder() {} 57 58 @CanIgnoreReturnValue setKeySizeBytes(int keySizeBytes)59 public Builder setKeySizeBytes(int keySizeBytes) throws GeneralSecurityException { 60 if (keySizeBytes < MIN_KEY_SIZE) { 61 throw new InvalidAlgorithmParameterException( 62 String.format( 63 "Invalid key size %d; only 128-bit or larger are supported", keySizeBytes * 8)); 64 } 65 this.keySizeBytes = keySizeBytes; 66 return this; 67 } 68 69 @CanIgnoreReturnValue setHashType(HashType hashType)70 public Builder setHashType(HashType hashType) { 71 this.hashType = hashType; 72 return this; 73 } 74 build()75 public HmacPrfParameters build() throws GeneralSecurityException { 76 if (keySizeBytes == null) { 77 throw new GeneralSecurityException("key size is not set"); 78 } 79 if (hashType == null) { 80 throw new GeneralSecurityException("hash type is not set"); 81 } 82 return new HmacPrfParameters(keySizeBytes, hashType); 83 } 84 } 85 86 private final int keySizeBytes; 87 private final HashType hashType; 88 HmacPrfParameters(int keySizeBytes, HashType hashType)89 private HmacPrfParameters(int keySizeBytes, HashType hashType) { 90 this.keySizeBytes = keySizeBytes; 91 this.hashType = hashType; 92 } 93 builder()94 public static Builder builder() { 95 return new Builder(); 96 } 97 getKeySizeBytes()98 public int getKeySizeBytes() { 99 return keySizeBytes; 100 } 101 getHashType()102 public HashType getHashType() { 103 return hashType; 104 } 105 106 @Override equals(Object o)107 public boolean equals(Object o) { 108 if (!(o instanceof HmacPrfParameters)) { 109 return false; 110 } 111 HmacPrfParameters that = (HmacPrfParameters) o; 112 return that.getKeySizeBytes() == getKeySizeBytes() && that.getHashType() == getHashType(); 113 } 114 115 @Override hashCode()116 public int hashCode() { 117 return Objects.hash(HmacPrfParameters.class, keySizeBytes, hashType); 118 } 119 120 @Override hasIdRequirement()121 public boolean hasIdRequirement() { 122 return false; 123 } 124 125 @Override toString()126 public String toString() { 127 return "HMAC PRF Parameters (hashType: " + hashType + " and " + keySizeBytes + "-byte key)"; 128 } 129 } 130