aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/crypto/caam/sm_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/crypto/caam/sm_test.c')
-rw-r--r--drivers/crypto/caam/sm_test.c844
1 files changed, 844 insertions, 0 deletions
diff --git a/drivers/crypto/caam/sm_test.c b/drivers/crypto/caam/sm_test.c
new file mode 100644
index 000000000000..43590ca52155
--- /dev/null
+++ b/drivers/crypto/caam/sm_test.c
@@ -0,0 +1,844 @@
1/*
2 * Secure Memory / Keystore Exemplification Module
3 * Copyright (C) 2013 Freescale Semiconductor, Inc. All Rights Reserved
4 *
5 * Serves as a functional example, and as a self-contained unit test for
6 * the functionality contained in sm_store.c.
7 *
8 * The example function, caam_sm_example_init(), runs a thread that:
9 *
10 * - initializes a set of fixed keys
11 * - stores one copy in clear buffers
12 * - stores them again in secure memory
13 * - extracts stored keys back out for use
14 * - intializes 3 data buffers for a test:
15 * (1) containing cleartext
16 * (2) to hold ciphertext encrypted with an extracted black key
17 * (3) to hold extracted cleartext decrypted with an equivalent clear key
18 *
19 * The function then builds simple job descriptors that reference the key
20 * material and buffers as initialized, and executes an encryption job
21 * with a black key, and a decryption job using a the same key held in the
22 * clear. The output of the decryption job is compared to the original
23 * cleartext; if they don't compare correctly, one can assume a key problem
24 * exists, where the function will exit with an error.
25 *
26 * This module can use a substantial amount of refactoring, which may occur
27 * after the API gets some mileage. Furthermore, expect this module to
28 * eventually disappear once the API is integrated into "real" software.
29 */
30
31#include "compat.h"
32#include "intern.h"
33#include "desc.h"
34#include "error.h"
35#include "jr.h"
36#include "sm.h"
37
38static u8 skeymod[] = {
39 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
40 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
41};
42static u8 symkey[] = {
43 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
44 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
45 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
46 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
47};
48
49static u8 symdata[] = {
50 0x00, 0x01, 0x02, 0x03, 0x04, 0x0f, 0x06, 0x07,
51 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
52 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
53 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
54 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
55 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
56 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
57 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
58 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
59 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
60 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
61 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
62 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
63 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
64 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
65 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
66 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
67 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
68 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
69 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
70 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
71 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
72 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
73 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
74 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
75 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
76 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
77 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
78 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
79 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
80 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
81 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
82};
83
84static int mk_job_desc(u32 *desc, dma_addr_t key, u16 keysz, dma_addr_t indata,
85 dma_addr_t outdata, u16 sz, u32 cipherdir, u32 keymode)
86{
87 desc[1] = CMD_KEY | CLASS_1 | (keysz & KEY_LENGTH_MASK) | keymode;
88 desc[2] = (u32)key;
89 desc[3] = CMD_OPERATION | OP_TYPE_CLASS1_ALG | OP_ALG_AAI_ECB |
90 cipherdir;
91 desc[4] = CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 |
92 FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1 | sz;
93 desc[5] = (u32)indata;
94 desc[6] = CMD_FIFO_STORE | FIFOST_TYPE_MESSAGE_DATA | sz;
95 desc[7] = (u32)outdata;
96
97 desc[0] = CMD_DESC_HDR | HDR_ONE | (8 & HDR_DESCLEN_MASK);
98 return 8 * sizeof(u32);
99}
100
101struct exec_test_result {
102 int error;
103 struct completion completion;
104};
105
106void exec_test_done(struct device *dev, u32 *desc, u32 err, void *context)
107{
108 struct exec_test_result *res = context;
109
110 if (err) {
111 char tmp[CAAM_ERROR_STR_MAX];
112 dev_err(dev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
113 }
114
115 res->error = err;
116 complete(&res->completion);
117}
118
119static int exec_test_job(struct device *ksdev, u32 *jobdesc)
120{
121 struct exec_test_result testres;
122 struct caam_drv_private_sm *kspriv;
123 int rtn = 0;
124
125 kspriv = dev_get_drvdata(ksdev);
126
127 init_completion(&testres.completion);
128
129 rtn = caam_jr_enqueue(kspriv->smringdev, jobdesc, exec_test_done,
130 &testres);
131 if (!rtn) {
132 wait_for_completion_interruptible(&testres.completion);
133 rtn = testres.error;
134 }
135 return rtn;
136}
137
138
139int caam_sm_example_init(struct platform_device *pdev)
140{
141 struct device *ctrldev, *ksdev;
142 struct caam_drv_private *ctrlpriv;
143 struct caam_drv_private_sm *kspriv;
144 u32 unit, units, jdescsz;
145 int stat, jstat, rtnval = 0;
146 u8 __iomem *syminp, *symint, *symout = NULL;
147 dma_addr_t syminp_dma, symint_dma, symout_dma;
148 u8 __iomem *black_key_des, *black_key_aes128;
149 u8 __iomem *black_key_aes256;
150 dma_addr_t black_key_des_dma, black_key_aes128_dma;
151 dma_addr_t black_key_aes256_dma;
152 u8 __iomem *clear_key_des, *clear_key_aes128, *clear_key_aes256;
153 dma_addr_t clear_key_des_dma, clear_key_aes128_dma;
154 dma_addr_t clear_key_aes256_dma;
155 u32 __iomem *jdesc;
156 u32 keyslot_des, keyslot_aes128, keyslot_aes256 = 0;
157
158 jdesc = NULL;
159 black_key_des = black_key_aes128 = black_key_aes256 = NULL;
160 clear_key_des = clear_key_aes128 = clear_key_aes256 = NULL;
161
162 /* We can lose this cruft once we can get a pdev by name */
163 ctrldev = &pdev->dev;
164 ctrlpriv = dev_get_drvdata(ctrldev);
165 ksdev = ctrlpriv->smdev;
166 kspriv = dev_get_drvdata(ksdev);
167 if (kspriv == NULL)
168 return -ENODEV;
169
170 /* Now that we have the dev for the single SM instance, connect */
171#ifdef SM_TEST_DETAIL
172 dev_info(ksdev, "caam_sm_test_init() running\n");
173#endif
174 /* Probe to see what keystores are available to us */
175 units = sm_detect_keystore_units(ksdev);
176 if (!units)
177 dev_err(ksdev, "caam_sm_test: no keystore units available\n");
178
179 /*
180 * MX6 bootloader stores some stuff in unit 0, so let's
181 * use 1 or above
182 */
183 if (units < 2) {
184 dev_err(ksdev, "caam_sm_test: insufficient keystore units\n");
185 return -ENODEV;
186 }
187 unit = 1;
188
189#ifdef SM_TEST_DETAIL
190 dev_info(ksdev, "caam_sm_test: %d keystore units available\n", units);
191#endif
192
193 /* Initialize/Establish Keystore */
194 sm_establish_keystore(ksdev, unit); /* Initalize store in #1 */
195
196 /*
197 * Top of main test thread
198 */
199
200 /* Allocate test data blocks (input, intermediate, output) */
201 syminp = kmalloc(256, GFP_KERNEL | GFP_DMA);
202 symint = kmalloc(256, GFP_KERNEL | GFP_DMA);
203 symout = kmalloc(256, GFP_KERNEL | GFP_DMA);
204 if ((syminp == NULL) || (symint == NULL) || (symout == NULL)) {
205 rtnval = -ENOMEM;
206 dev_err(ksdev, "caam_sm_test: can't get test data buffers\n");
207 goto freemem;
208 }
209
210 /* Allocate storage for 3 black keys: encapsulated 8, 16, 32 */
211 black_key_des = kmalloc(16, GFP_KERNEL | GFP_DMA); /* padded to 16... */
212 black_key_aes128 = kmalloc(16, GFP_KERNEL | GFP_DMA);
213 black_key_aes256 = kmalloc(16, GFP_KERNEL | GFP_DMA);
214 if ((black_key_des == NULL) || (black_key_aes128 == NULL) ||
215 (black_key_aes256 == NULL)) {
216 rtnval = -ENOMEM;
217 dev_err(ksdev, "caam_sm_test: can't black key buffers\n");
218 goto freemem;
219 }
220
221 clear_key_des = kmalloc(8, GFP_KERNEL | GFP_DMA);
222 clear_key_aes128 = kmalloc(16, GFP_KERNEL | GFP_DMA);
223 clear_key_aes256 = kmalloc(32, GFP_KERNEL | GFP_DMA);
224 if ((clear_key_des == NULL) || (clear_key_aes128 == NULL) ||
225 (clear_key_aes256 == NULL)) {
226 rtnval = -ENOMEM;
227 dev_err(ksdev, "caam_sm_test: can't get clear key buffers\n");
228 goto freemem;
229 }
230
231 /* Allocate storage for job descriptor */
232 jdesc = kmalloc(8 * sizeof(u32), GFP_KERNEL | GFP_DMA);
233 if (jdesc == NULL) {
234 rtnval = -ENOMEM;
235 dev_err(ksdev, "caam_sm_test: can't get descriptor buffers\n");
236 goto freemem;
237 }
238
239#ifdef SM_TEST_DETAIL
240 dev_info(ksdev, "caam_sm_test: all buffers allocated\n");
241#endif
242
243 /* Load up input data block, clear outputs */
244 memcpy(syminp, symdata, 256);
245 memset(symint, 0, 256);
246 memset(symout, 0, 256);
247#ifdef SM_TEST_DETAIL
248 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
249 "0x%02x 0x%02x 0x%02x 0x%02x\n",
250 syminp[0], syminp[1], syminp[2], syminp[3],
251 syminp[4], syminp[5], syminp[6], syminp[7]);
252 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
253 "0x%02x 0x%02x 0x%02x 0x%02x\n",
254 symint[0], symint[1], symint[2], symint[3],
255 symint[4], symint[5], symint[6], symint[7]);
256 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
257 "0x%02x 0x%02x 0x%02x 0x%02x\n",
258 symout[0], symout[1], symout[2], symout[3],
259 symout[4], symout[5], symout[6], symout[7]);
260
261 dev_info(ksdev, "caam_sm_test: data buffers initialized\n");
262#endif
263
264 /* Load up clear keys */
265 memcpy(clear_key_des, symkey, 8);
266 memcpy(clear_key_aes128, symkey, 16);
267 memcpy(clear_key_aes256, symkey, 32);
268
269#ifdef SM_TEST_DETAIL
270 dev_info(ksdev, "caam_sm_test: all clear keys loaded\n");
271#endif
272
273 /*
274 * Place clear keys in keystore.
275 * All the interesting stuff happens here.
276 */
277 /* 8 bit DES key */
278 stat = sm_keystore_slot_alloc(ksdev, unit, 8, &keyslot_des);
279 if (stat)
280 goto freemem;
281#ifdef SM_TEST_DETAIL
282 dev_info(ksdev, "caam_sm_test: 8 byte key slot in %d\n", keyslot_des);
283#endif
284 stat = sm_keystore_slot_load(ksdev, unit, keyslot_des, clear_key_des,
285 8);
286 if (stat) {
287#ifdef SM_TEST_DETAIL
288 dev_info(ksdev, "caam_sm_test: can't load 8 byte key in %d\n",
289 keyslot_des);
290#endif
291 sm_keystore_slot_dealloc(ksdev, unit, keyslot_des);
292 goto freemem;
293 }
294
295 /* 16 bit AES key */
296 stat = sm_keystore_slot_alloc(ksdev, unit, 16, &keyslot_aes128);
297 if (stat) {
298 sm_keystore_slot_dealloc(ksdev, unit, keyslot_des);
299 goto freemem;
300 }
301#ifdef SM_TEST_DETAIL
302 dev_info(ksdev, "caam_sm_test: 16 byte key slot in %d\n",
303 keyslot_aes128);
304#endif
305 stat = sm_keystore_slot_load(ksdev, unit, keyslot_aes128,
306 clear_key_aes128, 16);
307 if (stat) {
308#ifdef SM_TEST_DETAIL
309 dev_info(ksdev, "caam_sm_test: can't load 16 byte key in %d\n",
310 keyslot_aes128);
311#endif
312 sm_keystore_slot_dealloc(ksdev, unit, keyslot_aes128);
313 sm_keystore_slot_dealloc(ksdev, unit, keyslot_des);
314 goto freemem;
315 }
316
317 /* 32 bit AES key */
318 stat = sm_keystore_slot_alloc(ksdev, unit, 32, &keyslot_aes256);
319 if (stat) {
320 sm_keystore_slot_dealloc(ksdev, unit, keyslot_aes128);
321 sm_keystore_slot_dealloc(ksdev, unit, keyslot_des);
322 goto freemem;
323 }
324#ifdef SM_TEST_DETAIL
325 dev_info(ksdev, "caam_sm_test: 32 byte key slot in %d\n",
326 keyslot_aes256);
327#endif
328 stat = sm_keystore_slot_load(ksdev, unit, keyslot_aes256,
329 clear_key_aes256, 32);
330 if (stat) {
331#ifdef SM_TEST_DETAIL
332 dev_info(ksdev, "caam_sm_test: can't load 32 byte key in %d\n",
333 keyslot_aes128);
334#endif
335 sm_keystore_slot_dealloc(ksdev, unit, keyslot_aes256);
336 sm_keystore_slot_dealloc(ksdev, unit, keyslot_aes128);
337 sm_keystore_slot_dealloc(ksdev, unit, keyslot_des);
338 goto freemem;
339 }
340
341 /* Encapsulate all keys as SM blobs */
342 stat = sm_keystore_slot_encapsulate(ksdev, unit, keyslot_des,
343 keyslot_des, 8, skeymod, 8);
344 if (stat) {
345 dev_info(ksdev, "caam_sm_test: can't encapsulate DES key\n");
346 goto freekeys;
347 }
348
349 stat = sm_keystore_slot_encapsulate(ksdev, unit, keyslot_aes128,
350 keyslot_aes128, 16, skeymod, 8);
351 if (stat) {
352 dev_info(ksdev, "caam_sm_test: can't encapsulate AES128 key\n");
353 goto freekeys;
354 }
355
356 stat = sm_keystore_slot_encapsulate(ksdev, unit, keyslot_aes256,
357 keyslot_aes256, 32, skeymod, 8);
358 if (stat) {
359 dev_info(ksdev, "caam_sm_test: can't encapsulate AES256 key\n");
360 goto freekeys;
361 }
362
363 /* Now decapsulate as black key blobs */
364 stat = sm_keystore_slot_decapsulate(ksdev, unit, keyslot_des,
365 keyslot_des, 8, skeymod, 8);
366 if (stat) {
367 dev_info(ksdev, "caam_sm_test: can't decapsulate DES key\n");
368 goto freekeys;
369 }
370
371 stat = sm_keystore_slot_decapsulate(ksdev, unit, keyslot_aes128,
372 keyslot_aes128, 16, skeymod, 8);
373 if (stat) {
374 dev_info(ksdev, "caam_sm_test: can't decapsulate AES128 key\n");
375 goto freekeys;
376 }
377
378 stat = sm_keystore_slot_decapsulate(ksdev, unit, keyslot_aes256,
379 keyslot_aes256, 32, skeymod, 8);
380 if (stat) {
381 dev_info(ksdev, "caam_sm_test: can't decapsulate AES128 key\n");
382 goto freekeys;
383 }
384
385 /* Extract 8/16/32 byte black keys */
386 sm_keystore_slot_read(ksdev, unit, keyslot_des, 8, black_key_des);
387 sm_keystore_slot_read(ksdev, unit, keyslot_aes128, 16,
388 black_key_aes128);
389 sm_keystore_slot_read(ksdev, unit, keyslot_aes256, 32,
390 black_key_aes256);
391
392#ifdef SM_TEST_DETAIL
393 dev_info(ksdev, "caam_sm_test: all black keys extracted\n");
394#endif
395
396 /* DES encrypt using 8 byte black key */
397 black_key_des_dma = dma_map_single(ksdev, black_key_des, 8,
398 DMA_TO_DEVICE);
399 dma_sync_single_for_device(ksdev, black_key_des_dma, 8, DMA_TO_DEVICE);
400 syminp_dma = dma_map_single(ksdev, syminp, 256, DMA_TO_DEVICE);
401 dma_sync_single_for_device(ksdev, syminp_dma, 256, DMA_TO_DEVICE);
402 symint_dma = dma_map_single(ksdev, symint, 256, DMA_FROM_DEVICE);
403
404 jdescsz = mk_job_desc(jdesc, black_key_des_dma, 8, syminp_dma,
405 symint_dma, 256,
406 OP_ALG_ENCRYPT | OP_ALG_ALGSEL_DES, 0);
407
408#ifdef SM_TEST_DETAIL
409 dev_info(ksdev, "jobdesc:\n");
410 dev_info(ksdev, "0x%08x\n", jdesc[0]);
411 dev_info(ksdev, "0x%08x\n", jdesc[1]);
412 dev_info(ksdev, "0x%08x\n", jdesc[2]);
413 dev_info(ksdev, "0x%08x\n", jdesc[3]);
414 dev_info(ksdev, "0x%08x\n", jdesc[4]);
415 dev_info(ksdev, "0x%08x\n", jdesc[5]);
416 dev_info(ksdev, "0x%08x\n", jdesc[6]);
417 dev_info(ksdev, "0x%08x\n", jdesc[7]);
418#endif
419
420 jstat = exec_test_job(ksdev, jdesc);
421
422 dma_sync_single_for_cpu(ksdev, symint_dma, 256, DMA_FROM_DEVICE);
423 dma_unmap_single(ksdev, symint_dma, 256, DMA_FROM_DEVICE);
424 dma_unmap_single(ksdev, syminp_dma, 256, DMA_TO_DEVICE);
425 dma_unmap_single(ksdev, black_key_des_dma, 8, DMA_TO_DEVICE);
426
427#ifdef SM_TEST_DETAIL
428 dev_info(ksdev, "input block:\n");
429 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
430 "0x%02x 0x%02x 0x%02x 0x%02x\n",
431 syminp[0], syminp[1], syminp[2], syminp[3],
432 syminp[4], syminp[5], syminp[6], syminp[7]);
433 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
434 "0x%02x 0x%02x 0x%02x 0x%02x\n",
435 syminp[8], syminp[9], syminp[10], syminp[11],
436 syminp[12], syminp[13], syminp[14], syminp[15]);
437 dev_info(ksdev, "intermediate block:\n");
438 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
439 "0x%02x 0x%02x 0x%02x 0x%02x\n",
440 symint[0], symint[1], symint[2], symint[3],
441 symint[4], symint[5], symint[6], symint[7]);
442 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
443 "0x%02x 0x%02x 0x%02x 0x%02x\n",
444 symint[8], symint[9], symint[10], symint[11],
445 symint[12], symint[13], symint[14], symint[15]);
446 dev_info(ksdev, "caam_sm_test: encrypt cycle with 8 byte key\n");
447#endif
448
449 /* DES decrypt using 8 byte clear key */
450 clear_key_des_dma = dma_map_single(ksdev, clear_key_des, 8,
451 DMA_TO_DEVICE);
452 dma_sync_single_for_device(ksdev, clear_key_des_dma, 8, DMA_TO_DEVICE);
453 symint_dma = dma_map_single(ksdev, symint, 256, DMA_TO_DEVICE);
454 dma_sync_single_for_device(ksdev, symint_dma, 256, DMA_TO_DEVICE);
455 symout_dma = dma_map_single(ksdev, symout, 256, DMA_FROM_DEVICE);
456
457 jdescsz = mk_job_desc(jdesc, clear_key_des_dma, 8, symint_dma,
458 symout_dma, 256,
459 OP_ALG_DECRYPT | OP_ALG_ALGSEL_DES, 0);
460
461#ifdef SM_TEST_DETAIL
462 dev_info(ksdev, "jobdesc:\n");
463 dev_info(ksdev, "0x%08x\n", jdesc[0]);
464 dev_info(ksdev, "0x%08x\n", jdesc[1]);
465 dev_info(ksdev, "0x%08x\n", jdesc[2]);
466 dev_info(ksdev, "0x%08x\n", jdesc[3]);
467 dev_info(ksdev, "0x%08x\n", jdesc[4]);
468 dev_info(ksdev, "0x%08x\n", jdesc[5]);
469 dev_info(ksdev, "0x%08x\n", jdesc[6]);
470 dev_info(ksdev, "0x%08x\n", jdesc[7]);
471#endif
472
473 jstat = exec_test_job(ksdev, jdesc);
474
475 dma_sync_single_for_cpu(ksdev, symout_dma, 256, DMA_FROM_DEVICE);
476 dma_unmap_single(ksdev, symout_dma, 256, DMA_FROM_DEVICE);
477 dma_unmap_single(ksdev, symint_dma, 256, DMA_TO_DEVICE);
478 dma_unmap_single(ksdev, clear_key_des_dma, 8, DMA_TO_DEVICE);
479
480#ifdef SM_TEST_DETAIL
481 dev_info(ksdev, "intermediate block:\n");
482 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
483 "0x%02x 0x%02x 0x%02x 0x%02x\n",
484 symint[0], symint[1], symint[2], symint[3],
485 symint[4], symint[5], symint[6], symint[7]);
486 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
487 "0x%02x 0x%02x 0x%02x 0x%02x\n",
488 symint[8], symint[9], symint[10], symint[11],
489 symint[12], symint[13], symint[14], symint[15]);
490 dev_info(ksdev, "decrypted block:\n");
491 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
492 "0x%02x 0x%02x 0x%02x 0x%02x\n",
493 symout[0], symout[1], symout[2], symout[3],
494 symout[4], symout[5], symout[6], symout[7]);
495 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
496 "0x%02x 0x%02x 0x%02x 0x%02x\n",
497 symout[8], symout[9], symout[10], symout[11],
498 symout[12], symout[13], symout[14], symout[15]);
499 dev_info(ksdev, "caam_sm_test: decrypt cycle with 8 byte key\n");
500#endif
501
502 /* Check result */
503 if (memcmp(symout, syminp, 256)) {
504 dev_info(ksdev, "caam_sm_test: 8-byte key test mismatch\n");
505 rtnval = -1;
506 goto freekeys;
507 } else
508 dev_info(ksdev, "caam_sm_test: 8-byte key test match OK\n");
509
510 /* AES-128 encrypt using 16 byte black key */
511 black_key_aes128_dma = dma_map_single(ksdev, black_key_aes128, 16,
512 DMA_TO_DEVICE);
513 dma_sync_single_for_device(ksdev, black_key_aes128_dma, 16,
514 DMA_TO_DEVICE);
515 syminp_dma = dma_map_single(ksdev, syminp, 256, DMA_TO_DEVICE);
516 dma_sync_single_for_device(ksdev, syminp_dma, 256, DMA_TO_DEVICE);
517 symint_dma = dma_map_single(ksdev, symint, 256, DMA_FROM_DEVICE);
518
519 jdescsz = mk_job_desc(jdesc, black_key_aes128_dma, 16, syminp_dma,
520 symint_dma, 256,
521 OP_ALG_ENCRYPT | OP_ALG_ALGSEL_AES, 0);
522
523#ifdef SM_TEST_DETAIL
524 dev_info(ksdev, "jobdesc:\n");
525 dev_info(ksdev, "0x%08x\n", jdesc[0]);
526 dev_info(ksdev, "0x%08x\n", jdesc[1]);
527 dev_info(ksdev, "0x%08x\n", jdesc[2]);
528 dev_info(ksdev, "0x%08x\n", jdesc[3]);
529 dev_info(ksdev, "0x%08x\n", jdesc[4]);
530 dev_info(ksdev, "0x%08x\n", jdesc[5]);
531 dev_info(ksdev, "0x%08x\n", jdesc[6]);
532 dev_info(ksdev, "0x%08x\n", jdesc[7]);
533#endif
534
535 jstat = exec_test_job(ksdev, jdesc);
536
537 dma_sync_single_for_cpu(ksdev, symint_dma, 256, DMA_FROM_DEVICE);
538 dma_unmap_single(ksdev, symint_dma, 256, DMA_FROM_DEVICE);
539 dma_unmap_single(ksdev, syminp_dma, 256, DMA_TO_DEVICE);
540 dma_unmap_single(ksdev, black_key_aes128_dma, 16, DMA_TO_DEVICE);
541
542#ifdef SM_TEST_DETAIL
543 dev_info(ksdev, "input block:\n");
544 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
545 "0x%02x 0x%02x 0x%02x 0x%02x\n",
546 syminp[0], syminp[1], syminp[2], syminp[3],
547 syminp[4], syminp[5], syminp[6], syminp[7]);
548 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
549 "0x%02x 0x%02x 0x%02x 0x%02x\n",
550 syminp[8], syminp[9], syminp[10], syminp[11],
551 syminp[12], syminp[13], syminp[14], syminp[15]);
552 dev_info(ksdev, "intermediate block:\n");
553 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
554 "0x%02x 0x%02x 0x%02x 0x%02x\n",
555 symint[0], symint[1], symint[2], symint[3],
556 symint[4], symint[5], symint[6], symint[7]);
557 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
558 "0x%02x 0x%02x 0x%02x 0x%02x\n",
559 symint[8], symint[9], symint[10], symint[11],
560 symint[12], symint[13], symint[14], symint[15]);
561 dev_info(ksdev, "caam_sm_test: encrypt cycle with 16 byte key\n");
562#endif
563
564 /* AES-128 decrypt using 16 byte clear key */
565 clear_key_aes128_dma = dma_map_single(ksdev, clear_key_aes128, 16,
566 DMA_TO_DEVICE);
567 dma_sync_single_for_device(ksdev, clear_key_aes128_dma, 16,
568 DMA_TO_DEVICE);
569 symint_dma = dma_map_single(ksdev, symint, 256, DMA_TO_DEVICE);
570 dma_sync_single_for_device(ksdev, symint_dma, 256, DMA_TO_DEVICE);
571 symout_dma = dma_map_single(ksdev, symout, 256, DMA_FROM_DEVICE);
572
573 jdescsz = mk_job_desc(jdesc, clear_key_aes128_dma, 16, symint_dma,
574 symout_dma, 256,
575 OP_ALG_DECRYPT | OP_ALG_ALGSEL_AES, 0);
576
577#ifdef SM_TEST_DETAIL
578 dev_info(ksdev, "jobdesc:\n");
579 dev_info(ksdev, "0x%08x\n", jdesc[0]);
580 dev_info(ksdev, "0x%08x\n", jdesc[1]);
581 dev_info(ksdev, "0x%08x\n", jdesc[2]);
582 dev_info(ksdev, "0x%08x\n", jdesc[3]);
583 dev_info(ksdev, "0x%08x\n", jdesc[4]);
584 dev_info(ksdev, "0x%08x\n", jdesc[5]);
585 dev_info(ksdev, "0x%08x\n", jdesc[6]);
586 dev_info(ksdev, "0x%08x\n", jdesc[7]);
587#endif
588 jstat = exec_test_job(ksdev, jdesc);
589
590 dma_sync_single_for_cpu(ksdev, symout_dma, 256, DMA_FROM_DEVICE);
591 dma_unmap_single(ksdev, symout_dma, 256, DMA_FROM_DEVICE);
592 dma_unmap_single(ksdev, symint_dma, 256, DMA_TO_DEVICE);
593 dma_unmap_single(ksdev, clear_key_aes128_dma, 16, DMA_TO_DEVICE);
594
595#ifdef SM_TEST_DETAIL
596 dev_info(ksdev, "intermediate block:\n");
597 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
598 "0x%02x 0x%02x 0x%02x 0x%02x\n",
599 symint[0], symint[1], symint[2], symint[3],
600 symint[4], symint[5], symint[6], symint[7]);
601 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
602 "0x%02x 0x%02x 0x%02x 0x%02x\n",
603 symint[8], symint[9], symint[10], symint[11],
604 symint[12], symint[13], symint[14], symint[15]);
605 dev_info(ksdev, "decrypted block:\n");
606 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
607 "0x%02x 0x%02x 0x%02x 0x%02x\n",
608 symout[0], symout[1], symout[2], symout[3],
609 symout[4], symout[5], symout[6], symout[7]);
610 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
611 "0x%02x 0x%02x 0x%02x 0x%02x\n",
612 symout[8], symout[9], symout[10], symout[11],
613 symout[12], symout[13], symout[14], symout[15]);
614 dev_info(ksdev, "caam_sm_test: decrypt cycle with 16 byte key\n");
615#endif
616
617 /* Check result */
618 if (memcmp(symout, syminp, 256)) {
619 dev_info(ksdev, "caam_sm_test: 16-byte key test mismatch\n");
620 rtnval = -1;
621 goto freekeys;
622 } else
623 dev_info(ksdev, "caam_sm_test: 16-byte key test match OK\n");
624
625 /* AES-256 encrypt using 32 byte black key */
626 black_key_aes256_dma = dma_map_single(ksdev, black_key_aes256, 32,
627 DMA_TO_DEVICE);
628 dma_sync_single_for_device(ksdev, black_key_aes256_dma, 32,
629 DMA_TO_DEVICE);
630 syminp_dma = dma_map_single(ksdev, syminp, 256, DMA_TO_DEVICE);
631 dma_sync_single_for_device(ksdev, syminp_dma, 256, DMA_TO_DEVICE);
632 symint_dma = dma_map_single(ksdev, symint, 256, DMA_FROM_DEVICE);
633
634 jdescsz = mk_job_desc(jdesc, black_key_aes256_dma, 32, syminp_dma,
635 symint_dma, 256,
636 OP_ALG_ENCRYPT | OP_ALG_ALGSEL_AES, 0);
637
638#ifdef SM_TEST_DETAIL
639 dev_info(ksdev, "jobdesc:\n");
640 dev_info(ksdev, "0x%08x\n", jdesc[0]);
641 dev_info(ksdev, "0x%08x\n", jdesc[1]);
642 dev_info(ksdev, "0x%08x\n", jdesc[2]);
643 dev_info(ksdev, "0x%08x\n", jdesc[3]);
644 dev_info(ksdev, "0x%08x\n", jdesc[4]);
645 dev_info(ksdev, "0x%08x\n", jdesc[5]);
646 dev_info(ksdev, "0x%08x\n", jdesc[6]);
647 dev_info(ksdev, "0x%08x\n", jdesc[7]);
648#endif
649
650 jstat = exec_test_job(ksdev, jdesc);
651
652 dma_sync_single_for_cpu(ksdev, symint_dma, 256, DMA_FROM_DEVICE);
653 dma_unmap_single(ksdev, symint_dma, 256, DMA_FROM_DEVICE);
654 dma_unmap_single(ksdev, syminp_dma, 256, DMA_TO_DEVICE);
655 dma_unmap_single(ksdev, black_key_aes256_dma, 32, DMA_TO_DEVICE);
656
657#ifdef SM_TEST_DETAIL
658 dev_info(ksdev, "input block:\n");
659 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
660 "0x%02x 0x%02x 0x%02x 0x%02x\n",
661 syminp[0], syminp[1], syminp[2], syminp[3],
662 syminp[4], syminp[5], syminp[6], syminp[7]);
663 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
664 "0x%02x 0x%02x 0x%02x 0x%02x\n",
665 syminp[8], syminp[9], syminp[10], syminp[11],
666 syminp[12], syminp[13], syminp[14], syminp[15]);
667 dev_info(ksdev, "intermediate block:\n");
668 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
669 "0x%02x 0x%02x 0x%02x 0x%02x\n",
670 symint[0], symint[1], symint[2], symint[3],
671 symint[4], symint[5], symint[6], symint[7]);
672 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
673 "0x%02x 0x%02x 0x%02x 0x%02x\n",
674 symint[8], symint[9], symint[10], symint[11],
675 symint[12], symint[13], symint[14], symint[15]);
676 dev_info(ksdev, "caam_sm_test: encrypt cycle with 32 byte key\n");
677#endif
678
679 /* AES-256 decrypt using 32-byte black key */
680 clear_key_aes256_dma = dma_map_single(ksdev, clear_key_aes256, 32,
681 DMA_TO_DEVICE);
682 dma_sync_single_for_device(ksdev, clear_key_aes256_dma, 32,
683 DMA_TO_DEVICE);
684 symint_dma = dma_map_single(ksdev, symint, 256, DMA_TO_DEVICE);
685 dma_sync_single_for_device(ksdev, symint_dma, 256, DMA_TO_DEVICE);
686 symout_dma = dma_map_single(ksdev, symout, 256, DMA_FROM_DEVICE);
687
688 jdescsz = mk_job_desc(jdesc, clear_key_aes256_dma, 32, symint_dma,
689 symout_dma, 256,
690 OP_ALG_DECRYPT | OP_ALG_ALGSEL_AES, 0);
691
692#ifdef SM_TEST_DETAIL
693 dev_info(ksdev, "jobdesc:\n");
694 dev_info(ksdev, "0x%08x\n", jdesc[0]);
695 dev_info(ksdev, "0x%08x\n", jdesc[1]);
696 dev_info(ksdev, "0x%08x\n", jdesc[2]);
697 dev_info(ksdev, "0x%08x\n", jdesc[3]);
698 dev_info(ksdev, "0x%08x\n", jdesc[4]);
699 dev_info(ksdev, "0x%08x\n", jdesc[5]);
700 dev_info(ksdev, "0x%08x\n", jdesc[6]);
701 dev_info(ksdev, "0x%08x\n", jdesc[7]);
702#endif
703
704 jstat = exec_test_job(ksdev, jdesc);
705
706 dma_sync_single_for_cpu(ksdev, symout_dma, 256, DMA_FROM_DEVICE);
707 dma_unmap_single(ksdev, symout_dma, 256, DMA_FROM_DEVICE);
708 dma_unmap_single(ksdev, symint_dma, 256, DMA_TO_DEVICE);
709 dma_unmap_single(ksdev, clear_key_aes256_dma, 32, DMA_TO_DEVICE);
710
711#ifdef SM_TEST_DETAIL
712 dev_info(ksdev, "intermediate block:\n");
713 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
714 "0x%02x 0x%02x 0x%02x 0x%02x\n",
715 symint[0], symint[1], symint[2], symint[3],
716 symint[4], symint[5], symint[6], symint[7]);
717 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
718 "0x%02x 0x%02x 0x%02x 0x%02x\n",
719 symint[8], symint[9], symint[10], symint[11],
720 symint[12], symint[13], symint[14], symint[15]);
721 dev_info(ksdev, "decrypted block:\n");
722 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
723 "0x%02x 0x%02x 0x%02x 0x%02x\n",
724 symout[0], symout[1], symout[2], symout[3],
725 symout[4], symout[5], symout[6], symout[7]);
726 dev_info(ksdev, "0x%02x 0x%02x 0x%02x 0x%02x " \
727 "0x%02x 0x%02x 0x%02x 0x%02x\n",
728 symout[8], symout[9], symout[10], symout[11],
729 symout[12], symout[13], symout[14], symout[15]);
730 dev_info(ksdev, "caam_sm_test: decrypt cycle with 32 byte key\n");
731#endif
732
733 /* Check result */
734 if (memcmp(symout, syminp, 256)) {
735 dev_info(ksdev, "caam_sm_test: 32-byte key test mismatch\n");
736 rtnval = -1;
737 goto freekeys;
738 } else
739 dev_info(ksdev, "caam_sm_test: 32-byte key test match OK\n");
740
741
742 /* Remove 8/16/32 byte keys from keystore */
743freekeys:
744 stat = sm_keystore_slot_dealloc(ksdev, unit, keyslot_des);
745 if (stat)
746 dev_info(ksdev, "caam_sm_test: can't release slot %d\n",
747 keyslot_des);
748
749 stat = sm_keystore_slot_dealloc(ksdev, unit, keyslot_aes128);
750 if (stat)
751 dev_info(ksdev, "caam_sm_test: can't release slot %d\n",
752 keyslot_aes128);
753
754 stat = sm_keystore_slot_dealloc(ksdev, unit, keyslot_aes256);
755 if (stat)
756 dev_info(ksdev, "caam_sm_test: can't release slot %d\n",
757 keyslot_aes256);
758
759
760 /* Free resources */
761freemem:
762#ifdef SM_TEST_DETAIL
763 dev_info(ksdev, "caam_sm_test: cleaning up\n");
764#endif
765 kfree(syminp);
766 kfree(symint);
767 kfree(symout);
768 kfree(clear_key_des);
769 kfree(clear_key_aes128);
770 kfree(clear_key_aes256);
771 kfree(black_key_des);
772 kfree(black_key_aes128);
773 kfree(black_key_aes256);
774 kfree(jdesc);
775
776 /* Disconnect from keystore and leave */
777 sm_release_keystore(ksdev, unit);
778
779 return rtnval;
780}
781EXPORT_SYMBOL(caam_sm_example_init);
782
783void caam_sm_example_shutdown(void)
784{
785 /* unused in present version */
786 struct device_node *dev_node;
787 struct platform_device *pdev;
788
789 /*
790 * Do of_find_compatible_node() then of_find_device_by_node()
791 * once a functional device tree is available
792 */
793 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
794 if (!dev_node) {
795 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
796 if (!dev_node)
797 return;
798 }
799
800 pdev = of_find_device_by_node(dev_node);
801 if (!pdev)
802 return;
803
804 of_node_get(dev_node);
805
806}
807
808static int __init caam_sm_test_init(void)
809{
810 struct device_node *dev_node;
811 struct platform_device *pdev;
812
813 /*
814 * Do of_find_compatible_node() then of_find_device_by_node()
815 * once a functional device tree is available
816 */
817 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
818 if (!dev_node) {
819 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
820 if (!dev_node)
821 return -ENODEV;
822 }
823
824 pdev = of_find_device_by_node(dev_node);
825 if (!pdev)
826 return -ENODEV;
827
828 of_node_put(dev_node);
829
830 caam_sm_example_init(pdev);
831
832 return 0;
833}
834
835
836/* Module-based initialization needs to wait for dev tree */
837#ifdef CONFIG_OF
838module_init(caam_sm_test_init);
839module_exit(caam_sm_example_shutdown);
840
841MODULE_LICENSE("Dual BSD/GPL");
842MODULE_DESCRIPTION("FSL CAAM Keystore Usage Example");
843MODULE_AUTHOR("Freescale Semiconductor - NMSG/MAD");
844#endif