This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
scc2_aes_test.c
238 lines (182 loc) · 7.44 KB
/
scc2_aes_test.c
1
2
3
4
5
6
7
8
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright (c) WithSecure Corporation
*
* https://github.com/usbarmory/mxc-scc2
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation under version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
static int aes_encrypt_test(void)
{
int part_no, length;
uint32_t part_phys;
uint32_t plaintext[4];
uint32_t iv[4];
void *part_base;
void *black_ram;
dma_addr_t handle;
scc_return_t ret;
iv[0] = 0x03020100;
iv[1] = 0x07060504;
iv[2] = 0x0b0a0908;
iv[3] = 0x0f0e0d0c;
plaintext[0] = 0xe2bec16b;
plaintext[1] = 0x969f402e;
plaintext[2] = 0x117e3de9;
plaintext[3] = 0x2a179373;
length = sizeof(plaintext);
// key: 603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4
// iv: 000102030405060708090a0b0c0d0e0f
// test vector: 6bc1bee22e409f96e93d7e117393172a # 1st block
// cipher text: f58c4c04d6e5f1ba779eabfb5f7bfbd6
// test vector: 6bc1bee22e409f96e93d7e117393172a # 2nd block
// cipher text: eb2d9e942831bd84dff00db9776b8088
printk(KERN_ALERT "scc2_aes_test: ---- encryption test ----\n");
black_ram = dma_alloc_coherent(NULL, length, &handle, GFP_KERNEL);
if (!black_ram) {
printk(KERN_ALERT "scc2_aes_test: failed to allocate black ram\n");
return 0;
}
ret = scc_allocate_partition(0, &part_no, &part_base, &part_phys);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to allocate partition, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: allocated part_no: %x, part_base: %p, part_phys: %x\n",
part_no, part_base, part_phys);
ret = scc_engage_partition(part_base, NULL, SCM_PERM_TH_READ | SCM_PERM_TH_WRITE | SCM_PERM_HD_READ | SCM_PERM_HD_WRITE);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to engage partition, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: engaged part_no: %x\n", part_no);
writel(plaintext[0], (void *)(part_base + 0));
writel(plaintext[1], (void *)(part_base + 4));
writel(plaintext[2], (void *)(part_base + 8));
writel(plaintext[3], (void *)(part_base + 12));
print_hex_dump(KERN_DEBUG, "scc2_aes_test: partition ", DUMP_PREFIX_ADDRESS,
length, 1, part_base, length, false);
print_hex_dump(KERN_DEBUG, "scc2_aes_test: black pre ", DUMP_PREFIX_ADDRESS,
length, 1, black_ram, length, false);
// 1st block
ret = scc_encrypt_region((uint32_t) part_base, 0, length, (uint8_t *) virt_to_phys(black_ram), iv, SCC_CYPHER_MODE_CBC);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to encrypt region, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: encrypted region part_base: %p\n", part_base);
print_hex_dump(KERN_DEBUG, "scc2_aes_test: black post ", DUMP_PREFIX_ADDRESS,
length, 1, black_ram, length, false);
// 2nd block
ret = scc_encrypt_region((uint32_t) part_base, 0, length, (uint8_t *) virt_to_phys(black_ram), NULL, SCC_CYPHER_MODE_CBC);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to encrypt region, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: encrypted region part_base: %p\n", part_base);
print_hex_dump(KERN_DEBUG, "scc2_aes_test: black post ", DUMP_PREFIX_ADDRESS,
length, 1, black_ram, length, false);
// done
ret = scc_release_partition(part_base);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to release partition, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: released part_no: %x\n", part_no);
out:
dma_free_coherent(NULL, length, black_ram, handle);
return 0;
}
static int aes_decrypt_test(void)
{
int part_no, length;
uint32_t part_phys;
uint32_t ciphertext[4];
uint32_t iv[4];
void *part_base;
void *black_ram;
dma_addr_t handle;
scc_return_t ret;
iv[0] = 0x03020100;
iv[1] = 0x07060504;
iv[2] = 0x0b0a0908;
iv[3] = 0x0f0e0d0c;
ciphertext[0] = 0x044c8cf5;
ciphertext[1] = 0xbaf1e5d6;
ciphertext[2] = 0xfbab9e77;
ciphertext[3] = 0xd6fb7b5f;
length = sizeof(ciphertext);
// key: 603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4
// iv: 000102030405060708090a0b0c0d0e0f
// test vector: f58c4c04d6e5f1ba779eabfb5f7bfbd6 # 1st block
// plaintext: 6bc1bee22e409f96e93d7e117393172a
// test vector: eb2d9e942831bd84dff00db9776b8088 # 2nd block
// plaintext: 6bc1bee22e409f96e93d7e117393172a
printk(KERN_ALERT "scc2_aes_test: ---- decryption test ----\n");
black_ram = dma_alloc_coherent(NULL, length, &handle, GFP_KERNEL);
if (!black_ram) {
printk(KERN_ALERT "scc2_aes_test: failed to allocate black ram\n");
return 0;
}
ret = scc_allocate_partition(0, &part_no, &part_base, &part_phys);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to allocate partition, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: allocated part_no: %x, part_base: %p, part_phys: %x\n",
part_no, part_base, part_phys);
ret = scc_engage_partition(part_base, NULL, SCM_PERM_TH_READ | SCM_PERM_TH_WRITE | SCM_PERM_HD_READ | SCM_PERM_HD_WRITE);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to engage partition, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: engaged part_no: %x\n", part_no);
memcpy(black_ram, ciphertext, length);
print_hex_dump(KERN_DEBUG, "scc2_aes_test: black ", DUMP_PREFIX_ADDRESS,
length, 1, black_ram, length, false);
print_hex_dump(KERN_DEBUG, "scc2_aes_test: partition pre ", DUMP_PREFIX_ADDRESS,
length, 1, part_base, length, false);
// 1st block
ret = scc_decrypt_region((uint32_t) part_base, 0, length, (uint8_t *) virt_to_phys(black_ram), iv, SCC_CYPHER_MODE_CBC);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to decrypt black ram, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: decrypted black ram to region part_base: %p\n", part_base);
print_hex_dump(KERN_DEBUG, "scc2_aes_test: partition post ", DUMP_PREFIX_ADDRESS,
length, 1, part_base, length, false);
// 2nd block
ciphertext[0] = 0x949e2deb;
ciphertext[1] = 0x84bd3128;
ciphertext[2] = 0xb90df0df;
ciphertext[3] = 0x88806b77;
memcpy(black_ram, ciphertext, length);
ret = scc_decrypt_region((uint32_t) part_base, 0, length, (uint8_t *) virt_to_phys(black_ram), NULL, SCC_CYPHER_MODE_CBC);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to decrypt black ram, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: decrypted black ram to region part_base: %p\n", part_base);
print_hex_dump(KERN_DEBUG, "scc2_aes_test: partition post ", DUMP_PREFIX_ADDRESS,
length, 1, part_base, length, false);
// done
ret = scc_release_partition(part_base);
if (ret != SCC_RET_OK) {
printk(KERN_ALERT "scc2_aes_test: failed to release partition, error %x\n", ret);
goto out;
}
printk(KERN_DEBUG "scc2_aes_test: released part_no: %x\n", part_no);
out:
dma_free_coherent(NULL, length, black_ram, handle);
return 0;
}