internal byte[] Wrap(byte[] cek)
{
// 1) Initialize variables
byte[] a = DefaultIV; // Set A = IV, an initial value
byte[][] r = Arrays.Slice(cek, 8); // For i = 1 to n
// R[0][i] = P[i]
long n = r.Length;
// 2) Calculate intermediate values.
for (long j = 0; j < 6; j++) // For j = 0 to 5
{
for (long i = 0; i < n; i++) // For i=1 to n
{
long t = n * j + i + 1;
byte[] b = AesEnc(kek, Arrays.Concat(a, r[i])); // B=AES(K, A | R[i])
a = Arrays.FirstHalf(b); // A=MSB(64,B) ^ t where t = (n*j)+i
r[i] = Arrays.SecondHalf(b); // R[i] = LSB(64, B)
a = Arrays.Xor(a, t);
}
}
// 3) Output the results
byte[][] c = new byte[n + 1][];
c[0] = a; // Set C[0] = A
for (long i = 1; i <= n; i++) // For i = 1 to n
c[i] = r[i - 1]; // C[i] = R[i]
return Arrays.Concat(c);
}