aes/key_wrap.go
func KeyWrap(cek,kek []byte) ([]byte,error) {
	// 1) Initialize variables
    a := defaultIV              // Set A = IV, an initial value
    r := arrays.Slice(cek, 8)   // For i = 1 to n
								//     R[0][i] = P[i]
	n := uint64(len(r))
	
    // 2) Calculate intermediate values.
	var j,i,t uint64
	
	for j = 0; j < 6; j++ {                              // For j = 0 to 5
		for i = 0; i < n; i++ {                          //    For i=1 to n
			t = n * j + i + 1;
			b,e := aesEnc(kek, arrays.Concat(a, r[i]))   //       B=AES(K, A | R[i])
			
			if e!=nil { return nil, e }
			
			a = b[:len(b)/2]   							 //       A=MSB(64,B) ^ t where t = (n*j)+i
			r[i] = b[len(b)/2:]             			 //       R[i] = LSB(64, B)
			a = arrays.Xor(a, arrays.UInt64ToBytes(t))
		}
	}
	
	// 3) Output the results
	c := make([][]byte, n+1, n+1)
	c[0] = a;                              //  Set C[0] = A
	for i = 1; i <= n; i++ {               //  For i = 1 to n
		c[i] = r[i - 1]                    //     C[i] = R[i]
	}                      

	return arrays.Unwrap(c),nil
}
func KeyWrap(cek,kek []byte) ([]byte,error) {
	// 1) Initialize variables
    a := defaultIV              // Set A = IV, an initial value
    r := arrays.Slice(cek, 8)   // For i = 1 to n
								//     R[0][i] = P[i]
	n := uint64(len(r))
	
    // 2) Calculate intermediate values.
	var j,i,t uint64
	
	for j = 0; j < 6; j++ {                              // For j = 0 to 5
		for i = 0; i < n; i++ {                          //    For i=1 to n
			t = n * j + i + 1;
			b,e := aesEnc(kek, arrays.Concat(a, r[i]))   //       B=AES(K, A | R[i])
			
			if e!=nil { return nil, e }
			
			a = b[:len(b)/2]   							 //       A=MSB(64,B) ^ t where t = (n*j)+i
			r[i] = b[len(b)/2:]             			 //       R[i] = LSB(64, B)
			a = arrays.Xor(a, arrays.UInt64ToBytes(t))
		}
	}
	
	// 3) Output the results
	c := make([][]byte, n+1, n+1)
	c[0] = a;                              //  Set C[0] = A
	for i = 1; i <= n; i++ {               //  For i = 1 to n
		c[i] = r[i - 1]                    //     C[i] = R[i]
	}                      

	return arrays.Unwrap(c),nil
}
// KeyWrap encrypts provided key (CEK) with KEK key using AES Key Wrap (rfc 3394) algorithm