1
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
74
78
79
80
/* ... */
#include "includes.h"
#include "common.h"
#include "aes.h"
#include "aes_wrap.h"
/* ... */
int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
u8 *plain)
{
u8 a[8], *r, b[AES_BLOCK_SIZE];
int i, j;
void *ctx;
unsigned int t;
os_memcpy(a, cipher, 8);
r = plain;
os_memcpy(r, cipher + 8, 8 * n);
ctx = aes_decrypt_init(kek, kek_len);
if (ctx == NULL)
return -1;
/* ... */
for (j = 5; j >= 0; j--) {
r = plain + (n - 1) * 8;
for (i = n; i >= 1; i--) {
os_memcpy(b, a, 8);
t = n * j + i;
b[7] ^= t;
b[6] ^= t >> 8;
b[5] ^= t >> 16;
b[4] ^= t >> 24;
os_memcpy(b + 8, r, 8);
aes_decrypt(ctx, b, b);
os_memcpy(a, b, 8);
os_memcpy(r, b + 8, 8);
r -= 8;
}{...}
}{...}
aes_decrypt_deinit(ctx);
/* ... */
for (i = 0; i < 8; i++) {
if (a[i] != 0xa6)
return -1;
}{...}
return 0;
}{ ... }