internal byte[] Unwrap(byte[] encryptedCek)
{
// 1) Initialize variables
byte[][] c = Arrays.Slice(encryptedCek, 8);
byte[] a = c[0]; // Set A = C[0]
byte[][] r = new byte[c.Length - 1][];
for (int i = 1; i < c.Length; i++) // For i = 1 to n
r[i - 1] = c[i]; // R[i] = C[i]
long n = r.Length;
// 2) Calculate intermediate values
for (long j = 5; j >= 0; j--) // For j = 5 to 0
{
for (long i = n - 1; i >= 0; i--) // For i = n to 1
{
long t = n * j + i + 1;
a = Arrays.Xor(a, t);
byte[] B = AesDec(kek, Arrays.Concat(a, r[i])); // B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
a = Arrays.FirstHalf(B); // A = MSB(64, B)
r[i] = Arrays.SecondHalf(B); // R[i] = LSB(64, B)
}
}
// 3) Output the results
if (!Arrays.ConstantTimeEquals(DefaultIV, a)) // If A is an appropriate initial value
throw new InvalidSignatureException("AesKeyWrap integrity check failed.");
// For i = 1 to n
return Arrays.Concat(r); // P[i] = R[i]
}