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

package com.nimbusds.jose.crypto.impl;

import com.nimbusds.jose.util.KeyUtils;
import java.security.GeneralSecurityException;
import javax.crypto.IllegalBlockSizeException;
import java.security.InvalidKeyException;
import javax.crypto.NoSuchPaddingException;
import java.security.NoSuchAlgorithmException;
import com.nimbusds.jose.JOSEException;
import java.security.Key;
import javax.crypto.Cipher;
import java.security.Provider;
import javax.crypto.SecretKey;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;

@ThreadSafe
public class AESKW
{
    public static byte[] wrapCEK(final SecretKey cek, final SecretKey kek, final Provider provider) throws JOSEException {
        try {
            Cipher cipher;
            if (provider != null) {
                cipher = Cipher.getInstance("AESWrap", provider);
            }
            else {
                cipher = Cipher.getInstance("AESWrap");
            }
            cipher.init(3, kek);
            return cipher.wrap(cek);
        }
        catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException e) {
            throw new JOSEException("Couldn't wrap AES key: " + e.getMessage(), e);
        }
    }
    
    public static SecretKey unwrapCEK(final SecretKey kek, final byte[] encryptedCEK, final Provider provider) throws JOSEException {
        try {
            Cipher cipher;
            if (provider != null) {
                cipher = Cipher.getInstance("AESWrap", provider);
            }
            else {
                cipher = Cipher.getInstance("AESWrap");
            }
            cipher.init(4, KeyUtils.toAESKey(kek));
            return (SecretKey)cipher.unwrap(encryptedCEK, "AES", 3);
        }
        catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
            throw new JOSEException("Couldn't unwrap AES key: " + e.getMessage(), e);
        }
    }
    
    private AESKW() {
    }
}
