func KeyUnwrap(encryptedCek, kek []byte) ([]byte,error) {
	c := arrays.Slice(encryptedCek, 8);
	a := c[0];                            
	r := make([][]byte,len(c) - 1);
	for i := 1; i < len(c); i++ {          
		r[i - 1] = c[i];                   
	}              
    n := uint64(len(r))
	var t,j uint64
    for j = 6; j > 0; j-- {      						   
        for i := n; i > 0; i-- {                           
			t = n * (j-1) + i;
			a = arrays.Xor(a, arrays.UInt64ToBytes(t))
			b,e := aesDec(kek, arrays.Concat(a, r[i-1]))   
			if e!=nil { return nil,e }			
			a = b[:len(b)/2]                               
			r[i-1] = b[len(b)/2:]                          
        }
    }
    if (!hmac.Equal(defaultIV, a)) {   
        return nil, errors.New("aes.KeyUnwrap(): integrity check failed.")
	}
    return arrays.Unwrap(r),nil     
}
KeyUnwrap decrypts previously encrypted key (CEK) with KEK key using AES Key Wrap (rfc 3394) algorithm
package aes
import (
	"github.com/dvsekhvalnov/jose2go/arrays"
	"crypto/cipher"
	"crypto/aes"
	"crypto/hmac"
	"errors"
)
var	defaultIV=[]byte { 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6 }
func KeyWrap(cek,kek []byte) ([]byte,error) {
    a := defaultIV               
    r := arrays.Slice(cek, 8)    
	n := uint64(len(r))
	var j,i,t uint64
	for j = 0; j < 6; j++ {                               
		for i = 0; i < n; i++ {                           
			t = n * j + i + 1;
			b,e := aesEnc(kek, arrays.Concat(a, r[i]))    
			if e!=nil { return nil, e }
			a = b[:len(b)/2]   							  
			r[i] = b[len(b)/2:]             			  
			a = arrays.Xor(a, arrays.UInt64ToBytes(t))
		}
	}
	c := make([][]byte, n+1, n+1)
	c[0] = a;                               
	for i = 1; i <= n; i++ {                
		c[i] = r[i - 1]                     
	}                      
	return arrays.Unwrap(c),nil
}