Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/ssl/HKDF.java
38830 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ssl;2627import java.security.NoSuchAlgorithmException;28import java.security.InvalidKeyException;29import javax.crypto.Mac;30import javax.crypto.SecretKey;31import javax.crypto.ShortBufferException;32import javax.crypto.spec.SecretKeySpec;33import java.util.Objects;3435/**36* An implementation of the HKDF key derivation algorithm outlined in RFC 5869,37* specific to the needs of TLS 1.3 key derivation in JSSE. This is not a38* general purpose HKDF implementation and is suited only to single-key output39* derivations.40*41* HKDF objects are created by specifying a message digest algorithm. That42* digest algorithm will be used by the HMAC function as part of the HKDF43* derivation process.44*/45final class HKDF {46private final String hmacAlg;47private final Mac hmacObj;48private final int hmacLen;4950/**51* Create an HDKF object, specifying the underlying message digest52* algorithm.53*54* @param hashAlg a standard name corresponding to a supported message55* digest algorithm.56*57* @throws NoSuchAlgorithmException if that message digest algorithm does58* not have an HMAC variant supported on any available provider.59*/60HKDF(String hashAlg) throws NoSuchAlgorithmException {61Objects.requireNonNull(hashAlg,62"Must provide underlying HKDF Digest algorithm.");63hmacAlg = "Hmac" + hashAlg.replace("-", "");64hmacObj = JsseJce.getMac(hmacAlg);65hmacLen = hmacObj.getMacLength();66}6768/**69* Perform the HMAC-Extract derivation.70*71* @param salt a salt value, implemented as a {@code SecretKey}. A72* {@code null} value is allowed, which will internally use an array of73* zero bytes the same size as the underlying hash output length.74* @param inputKey the input keying material provided as a75* {@code SecretKey}.76* @param keyAlg the algorithm name assigned to the resulting77* {@code SecretKey} object.78*79* @return a {@code SecretKey} that is the result of the HKDF extract80* operation.81*82* @throws InvalidKeyException if the {@code salt} parameter cannot be83* used to initialize the underlying HMAC.84*/85SecretKey extract(SecretKey salt, SecretKey inputKey, String keyAlg)86throws InvalidKeyException {87if (salt == null) {88salt = new SecretKeySpec(new byte[hmacLen], "HKDF-Salt");89}90hmacObj.init(salt);9192return new SecretKeySpec(hmacObj.doFinal(inputKey.getEncoded()),93keyAlg);94}9596/**97* Perform the HMAC-Extract derivation.98*99* @param salt a salt value as cleartext bytes. A {@code null} value is100* allowed, which will internally use an array of zero bytes the same101* size as the underlying hash output length.102* @param inputKey the input keying material provided as a103* {@code SecretKey}.104* @param keyAlg the algorithm name assigned to the resulting105* {@code SecretKey} object.106*107* @return a {@code SecretKey} that is the result of the HKDF extract108* operation.109*110* @throws InvalidKeyException if the {@code salt} parameter cannot be111* used to initialize the underlying HMAC.112*/113SecretKey extract(byte[] salt, SecretKey inputKey, String keyAlg)114throws InvalidKeyException {115if (salt == null) {116salt = new byte[hmacLen];117}118return extract(new SecretKeySpec(salt, "HKDF-Salt"), inputKey, keyAlg);119}120121/**122* Perform the HKDF-Expand derivation for a single-key output.123*124* @param pseudoRandKey the pseudo random key (PRK).125* @param info optional context-specific info. A {@code null} value is126* allowed in which case a zero-length byte array will be used.127* @param outLen the length of the resulting {@code SecretKey}128* @param keyAlg the algorithm name applied to the resulting129* {@code SecretKey}130*131* @return the resulting key derivation as a {@code SecretKey} object132*133* @throws InvalidKeyException if the underlying HMAC operation cannot134* be initialized using the provided {@code pseudoRandKey} object.135*/136SecretKey expand(SecretKey pseudoRandKey, byte[] info, int outLen,137String keyAlg) throws InvalidKeyException {138byte[] kdfOutput;139140// Calculate the number of rounds of HMAC that are needed to141// meet the requested data. Then set up the buffers we will need.142Objects.requireNonNull(pseudoRandKey, "A null PRK is not allowed.");143144// Output from the expand operation must be <= 255 * hmac length145if (outLen > 255 * hmacLen) {146throw new IllegalArgumentException("Requested output length " +147"exceeds maximum length allowed for HKDF expansion");148}149hmacObj.init(pseudoRandKey);150if (info == null) {151info = new byte[0];152}153int rounds = (outLen + hmacLen - 1) / hmacLen;154kdfOutput = new byte[rounds * hmacLen];155int offset = 0;156int tLength = 0;157158for (int i = 0; i < rounds ; i++) {159160// Calculate this round161try {162// Add T(i). This will be an empty string on the first163// iteration since tLength starts at zero. After the first164// iteration, tLength is changed to the HMAC length for the165// rest of the loop.166hmacObj.update(kdfOutput,167Math.max(0, offset - hmacLen), tLength);168hmacObj.update(info); // Add info169hmacObj.update((byte)(i + 1)); // Add round number170hmacObj.doFinal(kdfOutput, offset);171172tLength = hmacLen;173offset += hmacLen; // For next iteration174} catch (ShortBufferException sbe) {175// This really shouldn't happen given that we've176// sized the buffers to their largest possible size up-front,177// but just in case...178throw new RuntimeException(sbe);179}180}181182return new SecretKeySpec(kdfOutput, 0, outLen, keyAlg);183}184}185186187188