aes/key_wrap.go
func KeyUnwrap(encryptedCek, kek []byte) ([]byte,error) {
    // 1) Initialize variables
	c := arrays.Slice(encryptedCek, 8);
	a := c[0];                           //   Set A = C[0]
	r := make([][]byte,len(c) - 1);

	for i := 1; i < len(c); i++ {         //   For i = 1 to n
		r[i - 1] = c[i];                  //       R[i] = C[i]
	}              

    n := uint64(len(r))
	
    // 2) Calculate intermediate values
	var t,j uint64

    for j = 6; j > 0; j-- {      						  // For j = 5 to 0    
        for i := n; i > 0; i-- {                          //   For i = n to 1        
			t = n * (j-1) + i;
			a = arrays.Xor(a, arrays.UInt64ToBytes(t))
			b,e := aesDec(kek, arrays.Concat(a, r[i-1]))  //     B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i			
			
			if e!=nil { return nil,e }			
			
			a = b[:len(b)/2]                              //     A = MSB(64, B)
			r[i-1] = b[len(b)/2:]                         //     R[i] = LSB(64, B)
        }
    }

    // 3) Output the results
    if (!hmac.Equal(defaultIV, a)) {  // If A is an appropriate initial value 
        return nil, errors.New("aes.KeyUnwrap(): integrity check failed.")
	}

								   // For i = 1 to n
    return arrays.Unwrap(r),nil    //    P[i] = R[i]

}
func KeyUnwrap(encryptedCek, kek []byte) ([]byte,error) {
    // 1) Initialize variables
	c := arrays.Slice(encryptedCek, 8);
	a := c[0];                           //   Set A = C[0]
	r := make([][]byte,len(c) - 1);

	for i := 1; i < len(c); i++ {         //   For i = 1 to n
		r[i - 1] = c[i];                  //       R[i] = C[i]
	}              

    n := uint64(len(r))
	
    // 2) Calculate intermediate values
	var t,j uint64

    for j = 6; j > 0; j-- {      						  // For j = 5 to 0    
        for i := n; i > 0; i-- {                          //   For i = n to 1        
			t = n * (j-1) + i;
			a = arrays.Xor(a, arrays.UInt64ToBytes(t))
			b,e := aesDec(kek, arrays.Concat(a, r[i-1]))  //     B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i			
			
			if e!=nil { return nil,e }			
			
			a = b[:len(b)/2]                              //     A = MSB(64, B)
			r[i-1] = b[len(b)/2:]                         //     R[i] = LSB(64, B)
        }
    }

    // 3) Output the results
    if (!hmac.Equal(defaultIV, a)) {  // If A is an appropriate initial value 
        return nil, errors.New("aes.KeyUnwrap(): integrity check failed.")
	}

								   // For i = 1 to n
    return arrays.Unwrap(r),nil    //    P[i] = R[i]

}
func KeyUnwrap ( encryptedCek , kek [ ] byte ) ( [ ] byte , error ) { // 1) Initialize variables c := arrays . Slice ( encryptedCek , 8 ) ; a := c [ 0 ] ; //   Set A = C[0] r := make ( [ ] [ ] byte , len ( c ) - 1 ) ; for i := 1 ; i < len ( c ) ; i ++ { //   For i = 1 to n r [ i - 1 ] = c [ i ] ; //       R[i] = C[i] } n := uint64 ( len ( r ) ) // 2) Calculate intermediate values var t , j uint64 for j = 6 ; j > 0 ; j -- { // For j = 5 to 0     for i := n ; i > 0 ; i -- { //   For i = n to 1         t = n * ( j - 1 ) + i ; a = arrays . Xor ( a , arrays . UInt64ToBytes ( t ) ) b , e := aesDec ( kek , arrays . Concat ( a , r [ i - 1 ] ) ) //     B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i			 if e != nil { return nil , e } a = b [ : len ( b ) / 2 ] //     A = MSB(64, B) r [ i - 1 ] = b [ len ( b ) / 2 : ] //     R[i] = LSB(64, B) } } // 3) Output the results if ( ! hmac . Equal ( defaultIV , a ) ) { // If A is an appropriate initial value  return nil , errors . New ( " " ) } // For i = 1 to n return arrays . Unwrap ( r ) , nil //    P[i] = R[i] }
// KeyUnwrap decrypts previously encrypted key (CEK) with KEK key using AES Key Wrap (rfc 3394) algorithm
KeyUnwrap decrypts previously encrypted key ( CEK ) with KEK key using AES Key Wrap ( rfc 3394 ) algorithm