// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink.subtle;

import java.security.Key;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;
import javax.crypto.Mac;

public final class Hkdf
{
    public static byte[] computeHkdf(final String macAlgorithm, final byte[] ikm, final byte[] salt, final byte[] info, final int size) throws GeneralSecurityException {
        final Mac mac = EngineFactory.MAC.getInstance(macAlgorithm);
        if (size > 255 * mac.getMacLength()) {
            throw new GeneralSecurityException("size too large");
        }
        if (salt == null || salt.length == 0) {
            mac.init(new SecretKeySpec(new byte[mac.getMacLength()], macAlgorithm));
        }
        else {
            mac.init(new SecretKeySpec(salt, macAlgorithm));
        }
        final byte[] prk = mac.doFinal(ikm);
        final byte[] result = new byte[size];
        int ctr = 1;
        int pos = 0;
        mac.init(new SecretKeySpec(prk, macAlgorithm));
        byte[] digest = new byte[0];
        while (true) {
            mac.update(digest);
            mac.update(info);
            mac.update((byte)ctr);
            digest = mac.doFinal();
            if (pos + digest.length >= size) {
                break;
            }
            System.arraycopy(digest, 0, result, pos, digest.length);
            pos += digest.length;
            ++ctr;
        }
        System.arraycopy(digest, 0, result, pos, size - pos);
        return result;
    }
    
    public static byte[] computeEciesHkdfSymmetricKey(final byte[] ephemeralPublicKeyBytes, final byte[] sharedSecret, final String hmacAlgo, final byte[] hkdfSalt, final byte[] hkdfInfo, final int keySizeInBytes) throws GeneralSecurityException {
        final byte[] hkdfInput = Bytes.concat(new byte[][] { ephemeralPublicKeyBytes, sharedSecret });
        return computeHkdf(hmacAlgo, hkdfInput, hkdfSalt, hkdfInfo, keySizeInBytes);
    }
    
    private Hkdf() {
    }
}
