blob: bc433edd34c45d040d7f76ed22da12a74961fa47 [file] [log] [blame]
Kim Phillips8e8ec592011-03-13 16:54:26 +08001/*
2 * caam - Freescale FSL CAAM support for crypto API
3 *
4 * Copyright 2008-2011 Freescale Semiconductor, Inc.
5 *
6 * Based on talitos crypto API driver.
7 *
8 * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008):
9 *
10 * --------------- ---------------
11 * | JobDesc #1 |-------------------->| ShareDesc |
12 * | *(packet 1) | | (PDB) |
13 * --------------- |------------->| (hashKey) |
14 * . | | (cipherKey) |
15 * . | |-------->| (operation) |
16 * --------------- | | ---------------
17 * | JobDesc #2 |------| |
18 * | *(packet 2) | |
19 * --------------- |
20 * . |
21 * . |
22 * --------------- |
23 * | JobDesc #3 |------------
24 * | *(packet 3) |
25 * ---------------
26 *
27 * The SharedDesc never changes for a connection unless rekeyed, but
28 * each packet will likely be in a different place. So all we need
29 * to know to process the packet is where the input is, where the
30 * output goes, and what context we want to process with. Context is
31 * in the SharedDesc, packet references in the JobDesc.
32 *
33 * So, a job desc looks like:
34 *
35 * ---------------------
36 * | Header |
37 * | ShareDesc Pointer |
38 * | SEQ_OUT_PTR |
39 * | (output buffer) |
Yuan Kang6ec47332012-06-22 19:48:43 -050040 * | (output length) |
Kim Phillips8e8ec592011-03-13 16:54:26 +080041 * | SEQ_IN_PTR |
42 * | (input buffer) |
Yuan Kang6ec47332012-06-22 19:48:43 -050043 * | (input length) |
Kim Phillips8e8ec592011-03-13 16:54:26 +080044 * ---------------------
45 */
46
47#include "compat.h"
48
49#include "regs.h"
50#include "intern.h"
51#include "desc_constr.h"
52#include "jr.h"
53#include "error.h"
Yuan Kanga299c832012-06-22 19:48:46 -050054#include "sg_sw_sec4.h"
Yuan Kang4c1ec1f2012-06-22 19:48:45 -050055#include "key_gen.h"
Kim Phillips8e8ec592011-03-13 16:54:26 +080056
57/*
58 * crypto alg
59 */
60#define CAAM_CRA_PRIORITY 3000
61/* max key is sum of AES_MAX_KEY_SIZE, max split key size */
62#define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
Catalin Vasiledaebc462014-10-31 12:45:37 +020063 CTR_RFC3686_NONCE_SIZE + \
Kim Phillips8e8ec592011-03-13 16:54:26 +080064 SHA512_DIGEST_SIZE * 2)
65/* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
66#define CAAM_MAX_IV_LENGTH 16
67
Herbert Xuf2147b82015-06-16 13:54:23 +080068#define AEAD_DESC_JOB_IO_LEN (DESC_JOB_IO_LEN + CAAM_CMD_SZ * 2)
69#define GCM_DESC_JOB_IO_LEN (AEAD_DESC_JOB_IO_LEN + \
70 CAAM_CMD_SZ * 4)
Herbert Xu479bcc72015-07-30 17:53:17 +080071#define AUTHENC_DESC_JOB_IO_LEN (AEAD_DESC_JOB_IO_LEN + \
72 CAAM_CMD_SZ * 5)
Herbert Xuf2147b82015-06-16 13:54:23 +080073
Kim Phillips4427b1b2011-05-14 22:08:17 -050074/* length of descriptors text */
Yuan Kang1acebad2011-07-15 11:21:42 +080075#define DESC_AEAD_BASE (4 * CAAM_CMD_SZ)
Herbert Xu479bcc72015-07-30 17:53:17 +080076#define DESC_AEAD_ENC_LEN (DESC_AEAD_BASE + 11 * CAAM_CMD_SZ)
77#define DESC_AEAD_DEC_LEN (DESC_AEAD_BASE + 15 * CAAM_CMD_SZ)
78#define DESC_AEAD_GIVENC_LEN (DESC_AEAD_ENC_LEN + 9 * CAAM_CMD_SZ)
Yuan Kang1acebad2011-07-15 11:21:42 +080079
Catalin Vasiledaebc462014-10-31 12:45:37 +020080/* Note: Nonce is counted in enckeylen */
Herbert Xu479bcc72015-07-30 17:53:17 +080081#define DESC_AEAD_CTR_RFC3686_LEN (4 * CAAM_CMD_SZ)
Catalin Vasiledaebc462014-10-31 12:45:37 +020082
Horia Geantaae4a8252014-03-14 17:46:52 +020083#define DESC_AEAD_NULL_BASE (3 * CAAM_CMD_SZ)
Herbert Xu479bcc72015-07-30 17:53:17 +080084#define DESC_AEAD_NULL_ENC_LEN (DESC_AEAD_NULL_BASE + 11 * CAAM_CMD_SZ)
85#define DESC_AEAD_NULL_DEC_LEN (DESC_AEAD_NULL_BASE + 13 * CAAM_CMD_SZ)
Horia Geantaae4a8252014-03-14 17:46:52 +020086
Tudor Ambarus3ef8d942014-10-23 16:11:23 +030087#define DESC_GCM_BASE (3 * CAAM_CMD_SZ)
Herbert Xuf2147b82015-06-16 13:54:23 +080088#define DESC_GCM_ENC_LEN (DESC_GCM_BASE + 16 * CAAM_CMD_SZ)
89#define DESC_GCM_DEC_LEN (DESC_GCM_BASE + 12 * CAAM_CMD_SZ)
Tudor Ambarus3ef8d942014-10-23 16:11:23 +030090
Tudor Ambarusbac68f22014-10-23 16:14:03 +030091#define DESC_RFC4106_BASE (3 * CAAM_CMD_SZ)
Horia Geant?4aad0cc2015-07-30 22:11:18 +030092#define DESC_RFC4106_ENC_LEN (DESC_RFC4106_BASE + 13 * CAAM_CMD_SZ)
93#define DESC_RFC4106_DEC_LEN (DESC_RFC4106_BASE + 13 * CAAM_CMD_SZ)
Tudor Ambarusbac68f22014-10-23 16:14:03 +030094
Tudor Ambarus5d0429a2014-10-30 18:55:07 +020095#define DESC_RFC4543_BASE (3 * CAAM_CMD_SZ)
Herbert Xuf2147b82015-06-16 13:54:23 +080096#define DESC_RFC4543_ENC_LEN (DESC_RFC4543_BASE + 11 * CAAM_CMD_SZ)
97#define DESC_RFC4543_DEC_LEN (DESC_RFC4543_BASE + 12 * CAAM_CMD_SZ)
Tudor Ambarus5d0429a2014-10-30 18:55:07 +020098
Yuan Kangacdca312011-07-15 11:21:42 +080099#define DESC_ABLKCIPHER_BASE (3 * CAAM_CMD_SZ)
100#define DESC_ABLKCIPHER_ENC_LEN (DESC_ABLKCIPHER_BASE + \
101 20 * CAAM_CMD_SZ)
102#define DESC_ABLKCIPHER_DEC_LEN (DESC_ABLKCIPHER_BASE + \
103 15 * CAAM_CMD_SZ)
104
Herbert Xu87e51b02015-06-18 14:25:55 +0800105#define DESC_MAX_USED_BYTES (CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN)
106#define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
Kim Phillips4427b1b2011-05-14 22:08:17 -0500107
Kim Phillips8e8ec592011-03-13 16:54:26 +0800108#ifdef DEBUG
109/* for print_hex_dumps with line references */
Kim Phillips8e8ec592011-03-13 16:54:26 +0800110#define debug(format, arg...) printk(format, arg)
111#else
112#define debug(format, arg...)
113#endif
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +0300114
115#ifdef DEBUG
116#include <linux/highmem.h>
117
118static void dbg_dump_sg(const char *level, const char *prefix_str,
119 int prefix_type, int rowsize, int groupsize,
120 struct scatterlist *sg, size_t tlen, bool ascii,
121 bool may_sleep)
122{
123 struct scatterlist *it;
124 void *it_page;
125 size_t len;
126 void *buf;
127
128 for (it = sg; it != NULL && tlen > 0 ; it = sg_next(sg)) {
129 /*
130 * make sure the scatterlist's page
131 * has a valid virtual memory mapping
132 */
133 it_page = kmap_atomic(sg_page(it));
134 if (unlikely(!it_page)) {
135 printk(KERN_ERR "dbg_dump_sg: kmap failed\n");
136 return;
137 }
138
139 buf = it_page + it->offset;
Arnd Bergmannd69985a2016-10-25 23:29:10 +0200140 len = min_t(size_t, tlen, it->length);
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +0300141 print_hex_dump(level, prefix_str, prefix_type, rowsize,
142 groupsize, buf, len, ascii);
143 tlen -= len;
144
145 kunmap_atomic(it_page);
146 }
147}
148#endif
149
Ruchika Guptacfc6f112013-10-25 12:01:03 +0530150static struct list_head alg_list;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800151
Herbert Xu479bcc72015-07-30 17:53:17 +0800152struct caam_alg_entry {
153 int class1_alg_type;
154 int class2_alg_type;
155 int alg_op;
156 bool rfc3686;
157 bool geniv;
158};
159
160struct caam_aead_alg {
161 struct aead_alg aead;
162 struct caam_alg_entry caam;
163 bool registered;
164};
165
Yuan Kang1acebad2011-07-15 11:21:42 +0800166/* Set DK bit in class 1 operation if shared */
167static inline void append_dec_op1(u32 *desc, u32 type)
168{
169 u32 *jump_cmd, *uncond_jump_cmd;
170
Horia Geantaa60384d2014-07-11 15:46:58 +0300171 /* DK bit is valid only for AES */
172 if ((type & OP_ALG_ALGSEL_MASK) != OP_ALG_ALGSEL_AES) {
173 append_operation(desc, type | OP_ALG_AS_INITFINAL |
174 OP_ALG_DECRYPT);
175 return;
176 }
177
Yuan Kang1acebad2011-07-15 11:21:42 +0800178 jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
179 append_operation(desc, type | OP_ALG_AS_INITFINAL |
180 OP_ALG_DECRYPT);
181 uncond_jump_cmd = append_jump(desc, JUMP_TEST_ALL);
182 set_jump_tgt_here(desc, jump_cmd);
183 append_operation(desc, type | OP_ALG_AS_INITFINAL |
184 OP_ALG_DECRYPT | OP_ALG_AAI_DK);
185 set_jump_tgt_here(desc, uncond_jump_cmd);
186}
187
188/*
Yuan Kang1acebad2011-07-15 11:21:42 +0800189 * For aead functions, read payload and write payload,
190 * both of which are specified in req->src and req->dst
191 */
192static inline void aead_append_src_dst(u32 *desc, u32 msg_type)
193{
Horia Geantaae4a8252014-03-14 17:46:52 +0200194 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
Yuan Kang1acebad2011-07-15 11:21:42 +0800195 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH |
196 KEY_VLF | msg_type | FIFOLD_TYPE_LASTBOTH);
Yuan Kang1acebad2011-07-15 11:21:42 +0800197}
198
199/*
Yuan Kangacdca312011-07-15 11:21:42 +0800200 * For ablkcipher encrypt and decrypt, read from req->src and
201 * write to req->dst
202 */
203static inline void ablkcipher_append_src_dst(u32 *desc)
204{
Kim Phillips70d793c2012-06-22 19:42:35 -0500205 append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
206 append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
207 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 |
208 KEY_VLF | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
209 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
Yuan Kangacdca312011-07-15 11:21:42 +0800210}
211
212/*
Kim Phillips8e8ec592011-03-13 16:54:26 +0800213 * per-session context
214 */
215struct caam_ctx {
216 struct device *jrdev;
Yuan Kang1acebad2011-07-15 11:21:42 +0800217 u32 sh_desc_enc[DESC_MAX_USED_LEN];
218 u32 sh_desc_dec[DESC_MAX_USED_LEN];
219 u32 sh_desc_givenc[DESC_MAX_USED_LEN];
220 dma_addr_t sh_desc_enc_dma;
221 dma_addr_t sh_desc_dec_dma;
222 dma_addr_t sh_desc_givenc_dma;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800223 u32 class1_alg_type;
224 u32 class2_alg_type;
225 u32 alg_op;
Yuan Kang1acebad2011-07-15 11:21:42 +0800226 u8 key[CAAM_MAX_KEY_SIZE];
Yuan Kang885e9e22011-07-15 11:21:41 +0800227 dma_addr_t key_dma;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800228 unsigned int enckeylen;
Kim Phillips8e8ec592011-03-13 16:54:26 +0800229 unsigned int split_key_len;
230 unsigned int split_key_pad_len;
231 unsigned int authsize;
232};
233
Yuan Kang1acebad2011-07-15 11:21:42 +0800234static void append_key_aead(u32 *desc, struct caam_ctx *ctx,
Catalin Vasiledaebc462014-10-31 12:45:37 +0200235 int keys_fit_inline, bool is_rfc3686)
Yuan Kang1acebad2011-07-15 11:21:42 +0800236{
Catalin Vasiledaebc462014-10-31 12:45:37 +0200237 u32 *nonce;
238 unsigned int enckeylen = ctx->enckeylen;
239
240 /*
241 * RFC3686 specific:
242 * | ctx->key = {AUTH_KEY, ENC_KEY, NONCE}
243 * | enckeylen = encryption key size + nonce size
244 */
245 if (is_rfc3686)
246 enckeylen -= CTR_RFC3686_NONCE_SIZE;
247
Yuan Kang1acebad2011-07-15 11:21:42 +0800248 if (keys_fit_inline) {
249 append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
250 ctx->split_key_len, CLASS_2 |
251 KEY_DEST_MDHA_SPLIT | KEY_ENC);
252 append_key_as_imm(desc, (void *)ctx->key +
Catalin Vasiledaebc462014-10-31 12:45:37 +0200253 ctx->split_key_pad_len, enckeylen,
254 enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
Yuan Kang1acebad2011-07-15 11:21:42 +0800255 } else {
256 append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
257 KEY_DEST_MDHA_SPLIT | KEY_ENC);
258 append_key(desc, ctx->key_dma + ctx->split_key_pad_len,
Catalin Vasiledaebc462014-10-31 12:45:37 +0200259 enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
260 }
261
262 /* Load Counter into CONTEXT1 reg */
263 if (is_rfc3686) {
264 nonce = (u32 *)((void *)ctx->key + ctx->split_key_pad_len +
265 enckeylen);
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +0300266 append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
267 LDST_CLASS_IND_CCB |
268 LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
Catalin Vasiledaebc462014-10-31 12:45:37 +0200269 append_move(desc,
270 MOVE_SRC_OUTFIFO |
271 MOVE_DEST_CLASS1CTX |
272 (16 << MOVE_OFFSET_SHIFT) |
273 (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
Yuan Kang1acebad2011-07-15 11:21:42 +0800274 }
275}
276
277static void init_sh_desc_key_aead(u32 *desc, struct caam_ctx *ctx,
Catalin Vasiledaebc462014-10-31 12:45:37 +0200278 int keys_fit_inline, bool is_rfc3686)
Yuan Kang1acebad2011-07-15 11:21:42 +0800279{
280 u32 *key_jump_cmd;
281
Catalin Vasiledaebc462014-10-31 12:45:37 +0200282 /* Note: Context registers are saved. */
283 init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
Yuan Kang1acebad2011-07-15 11:21:42 +0800284
285 /* Skip if already shared */
286 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
287 JUMP_COND_SHRD);
288
Catalin Vasiledaebc462014-10-31 12:45:37 +0200289 append_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
Yuan Kang1acebad2011-07-15 11:21:42 +0800290
291 set_jump_tgt_here(desc, key_jump_cmd);
Yuan Kang1acebad2011-07-15 11:21:42 +0800292}
293
Horia Geantaae4a8252014-03-14 17:46:52 +0200294static int aead_null_set_sh_desc(struct crypto_aead *aead)
295{
Horia Geantaae4a8252014-03-14 17:46:52 +0200296 struct caam_ctx *ctx = crypto_aead_ctx(aead);
297 struct device *jrdev = ctx->jrdev;
298 bool keys_fit_inline = false;
299 u32 *key_jump_cmd, *jump_cmd, *read_move_cmd, *write_move_cmd;
300 u32 *desc;
301
302 /*
303 * Job Descriptor and Shared Descriptors
304 * must all fit into the 64-word Descriptor h/w Buffer
305 */
Herbert Xu479bcc72015-07-30 17:53:17 +0800306 if (DESC_AEAD_NULL_ENC_LEN + AEAD_DESC_JOB_IO_LEN +
Horia Geantaae4a8252014-03-14 17:46:52 +0200307 ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX)
308 keys_fit_inline = true;
309
Herbert Xu479bcc72015-07-30 17:53:17 +0800310 /* aead_encrypt shared descriptor */
Horia Geantaae4a8252014-03-14 17:46:52 +0200311 desc = ctx->sh_desc_enc;
312
313 init_sh_desc(desc, HDR_SHARE_SERIAL);
314
315 /* Skip if already shared */
316 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
317 JUMP_COND_SHRD);
318 if (keys_fit_inline)
319 append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
320 ctx->split_key_len, CLASS_2 |
321 KEY_DEST_MDHA_SPLIT | KEY_ENC);
322 else
323 append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
324 KEY_DEST_MDHA_SPLIT | KEY_ENC);
325 set_jump_tgt_here(desc, key_jump_cmd);
326
Herbert Xu479bcc72015-07-30 17:53:17 +0800327 /* assoclen + cryptlen = seqinlen */
328 append_math_sub(desc, REG3, SEQINLEN, REG0, CAAM_CMD_SZ);
Horia Geantaae4a8252014-03-14 17:46:52 +0200329
Herbert Xu479bcc72015-07-30 17:53:17 +0800330 /* Prepare to read and write cryptlen + assoclen bytes */
Horia Geantaae4a8252014-03-14 17:46:52 +0200331 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
332 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
333
334 /*
335 * MOVE_LEN opcode is not available in all SEC HW revisions,
336 * thus need to do some magic, i.e. self-patch the descriptor
337 * buffer.
338 */
339 read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF |
340 MOVE_DEST_MATH3 |
341 (0x6 << MOVE_LEN_SHIFT));
342 write_move_cmd = append_move(desc, MOVE_SRC_MATH3 |
343 MOVE_DEST_DESCBUF |
344 MOVE_WAITCOMP |
345 (0x8 << MOVE_LEN_SHIFT));
346
347 /* Class 2 operation */
348 append_operation(desc, ctx->class2_alg_type |
349 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
350
351 /* Read and write cryptlen bytes */
352 aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
353
354 set_move_tgt_here(desc, read_move_cmd);
355 set_move_tgt_here(desc, write_move_cmd);
356 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
357 append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO |
358 MOVE_AUX_LS);
359
360 /* Write ICV */
361 append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
362 LDST_SRCDST_BYTE_CONTEXT);
363
364 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
365 desc_bytes(desc),
366 DMA_TO_DEVICE);
367 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
368 dev_err(jrdev, "unable to map shared descriptor\n");
369 return -ENOMEM;
370 }
371#ifdef DEBUG
372 print_hex_dump(KERN_ERR,
373 "aead null enc shdesc@"__stringify(__LINE__)": ",
374 DUMP_PREFIX_ADDRESS, 16, 4, desc,
375 desc_bytes(desc), 1);
376#endif
377
378 /*
379 * Job Descriptor and Shared Descriptors
380 * must all fit into the 64-word Descriptor h/w Buffer
381 */
Vakul Garg80cd88f2014-05-09 20:34:40 -0500382 keys_fit_inline = false;
Horia Geantaae4a8252014-03-14 17:46:52 +0200383 if (DESC_AEAD_NULL_DEC_LEN + DESC_JOB_IO_LEN +
384 ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX)
385 keys_fit_inline = true;
386
387 desc = ctx->sh_desc_dec;
388
Herbert Xu479bcc72015-07-30 17:53:17 +0800389 /* aead_decrypt shared descriptor */
Horia Geantaae4a8252014-03-14 17:46:52 +0200390 init_sh_desc(desc, HDR_SHARE_SERIAL);
391
392 /* Skip if already shared */
393 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
394 JUMP_COND_SHRD);
395 if (keys_fit_inline)
396 append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
397 ctx->split_key_len, CLASS_2 |
398 KEY_DEST_MDHA_SPLIT | KEY_ENC);
399 else
400 append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
401 KEY_DEST_MDHA_SPLIT | KEY_ENC);
402 set_jump_tgt_here(desc, key_jump_cmd);
403
404 /* Class 2 operation */
405 append_operation(desc, ctx->class2_alg_type |
406 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
407
Herbert Xu479bcc72015-07-30 17:53:17 +0800408 /* assoclen + cryptlen = seqoutlen */
Horia Geantaae4a8252014-03-14 17:46:52 +0200409 append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
Horia Geantaae4a8252014-03-14 17:46:52 +0200410
Herbert Xu479bcc72015-07-30 17:53:17 +0800411 /* Prepare to read and write cryptlen + assoclen bytes */
Horia Geantaae4a8252014-03-14 17:46:52 +0200412 append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
413 append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
414
415 /*
416 * MOVE_LEN opcode is not available in all SEC HW revisions,
417 * thus need to do some magic, i.e. self-patch the descriptor
418 * buffer.
419 */
420 read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF |
421 MOVE_DEST_MATH2 |
422 (0x6 << MOVE_LEN_SHIFT));
423 write_move_cmd = append_move(desc, MOVE_SRC_MATH2 |
424 MOVE_DEST_DESCBUF |
425 MOVE_WAITCOMP |
426 (0x8 << MOVE_LEN_SHIFT));
427
428 /* Read and write cryptlen bytes */
429 aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
430
431 /*
432 * Insert a NOP here, since we need at least 4 instructions between
433 * code patching the descriptor buffer and the location being patched.
434 */
435 jump_cmd = append_jump(desc, JUMP_TEST_ALL);
436 set_jump_tgt_here(desc, jump_cmd);
437
438 set_move_tgt_here(desc, read_move_cmd);
439 set_move_tgt_here(desc, write_move_cmd);
440 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
441 append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO |
442 MOVE_AUX_LS);
443 append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
444
445 /* Load ICV */
446 append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
447 FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
448
449 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
450 desc_bytes(desc),
451 DMA_TO_DEVICE);
452 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
453 dev_err(jrdev, "unable to map shared descriptor\n");
454 return -ENOMEM;
455 }
456#ifdef DEBUG
457 print_hex_dump(KERN_ERR,
458 "aead null dec shdesc@"__stringify(__LINE__)": ",
459 DUMP_PREFIX_ADDRESS, 16, 4, desc,
460 desc_bytes(desc), 1);
461#endif
462
463 return 0;
464}
465
Yuan Kang1acebad2011-07-15 11:21:42 +0800466static int aead_set_sh_desc(struct crypto_aead *aead)
467{
Herbert Xu479bcc72015-07-30 17:53:17 +0800468 struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
469 struct caam_aead_alg, aead);
Herbert Xuadd86d52015-05-11 17:47:50 +0800470 unsigned int ivsize = crypto_aead_ivsize(aead);
Yuan Kang1acebad2011-07-15 11:21:42 +0800471 struct caam_ctx *ctx = crypto_aead_ctx(aead);
472 struct device *jrdev = ctx->jrdev;
Catalin Vasiledaebc462014-10-31 12:45:37 +0200473 bool keys_fit_inline;
Yuan Kang1acebad2011-07-15 11:21:42 +0800474 u32 geniv, moveiv;
Catalin Vasiledaebc462014-10-31 12:45:37 +0200475 u32 ctx1_iv_off = 0;
Yuan Kang1acebad2011-07-15 11:21:42 +0800476 u32 *desc;
Catalin Vasiledaebc462014-10-31 12:45:37 +0200477 const bool ctr_mode = ((ctx->class1_alg_type & OP_ALG_AAI_MASK) ==
478 OP_ALG_AAI_CTR_MOD128);
Herbert Xu479bcc72015-07-30 17:53:17 +0800479 const bool is_rfc3686 = alg->caam.rfc3686;
Yuan Kang1acebad2011-07-15 11:21:42 +0800480
Horia Geantă2fdea252016-08-04 20:02:47 +0300481 if (!ctx->authsize)
482 return 0;
483
Horia Geantaae4a8252014-03-14 17:46:52 +0200484 /* NULL encryption / decryption */
485 if (!ctx->enckeylen)
486 return aead_null_set_sh_desc(aead);
487
Yuan Kang1acebad2011-07-15 11:21:42 +0800488 /*
Catalin Vasiledaebc462014-10-31 12:45:37 +0200489 * AES-CTR needs to load IV in CONTEXT1 reg
490 * at an offset of 128bits (16bytes)
491 * CONTEXT1[255:128] = IV
492 */
493 if (ctr_mode)
494 ctx1_iv_off = 16;
495
496 /*
497 * RFC3686 specific:
498 * CONTEXT1[255:128] = {NONCE, IV, COUNTER}
499 */
500 if (is_rfc3686)
501 ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
502
Herbert Xu479bcc72015-07-30 17:53:17 +0800503 if (alg->caam.geniv)
504 goto skip_enc;
505
Catalin Vasiledaebc462014-10-31 12:45:37 +0200506 /*
Yuan Kang1acebad2011-07-15 11:21:42 +0800507 * Job Descriptor and Shared Descriptors
508 * must all fit into the 64-word Descriptor h/w Buffer
509 */
Catalin Vasiledaebc462014-10-31 12:45:37 +0200510 keys_fit_inline = false;
Herbert Xu479bcc72015-07-30 17:53:17 +0800511 if (DESC_AEAD_ENC_LEN + AUTHENC_DESC_JOB_IO_LEN +
Catalin Vasiledaebc462014-10-31 12:45:37 +0200512 ctx->split_key_pad_len + ctx->enckeylen +
513 (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0) <=
Yuan Kang1acebad2011-07-15 11:21:42 +0800514 CAAM_DESC_BYTES_MAX)
Kim Phillips2af8f4a2012-09-07 04:17:03 +0800515 keys_fit_inline = true;
Yuan Kang1acebad2011-07-15 11:21:42 +0800516
Herbert Xu479bcc72015-07-30 17:53:17 +0800517 /* aead_encrypt shared descriptor */
Yuan Kang1acebad2011-07-15 11:21:42 +0800518 desc = ctx->sh_desc_enc;
519
Catalin Vasiledaebc462014-10-31 12:45:37 +0200520 /* Note: Context registers are saved. */
521 init_sh_desc_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
Yuan Kang1acebad2011-07-15 11:21:42 +0800522
523 /* Class 2 operation */
524 append_operation(desc, ctx->class2_alg_type |
525 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
526
Herbert Xu479bcc72015-07-30 17:53:17 +0800527 /* Read and write assoclen bytes */
528 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
529 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
Yuan Kang1acebad2011-07-15 11:21:42 +0800530
Herbert Xu479bcc72015-07-30 17:53:17 +0800531 /* Skip assoc data */
532 append_seq_fifo_store(desc, 0, FIFOST_TYPE_SKIP | FIFOLDST_VLF);
Yuan Kang1acebad2011-07-15 11:21:42 +0800533
534 /* read assoc before reading payload */
535 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
Herbert Xu479bcc72015-07-30 17:53:17 +0800536 FIFOLDST_VLF);
Catalin Vasiledaebc462014-10-31 12:45:37 +0200537
538 /* Load Counter into CONTEXT1 reg */
539 if (is_rfc3686)
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +0300540 append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
541 LDST_SRCDST_BYTE_CONTEXT |
542 ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
543 LDST_OFFSET_SHIFT));
Yuan Kang1acebad2011-07-15 11:21:42 +0800544
545 /* Class 1 operation */
546 append_operation(desc, ctx->class1_alg_type |
547 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
548
549 /* Read and write cryptlen bytes */
Herbert Xu479bcc72015-07-30 17:53:17 +0800550 append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
551 append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
Yuan Kang1acebad2011-07-15 11:21:42 +0800552 aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
553
554 /* Write ICV */
555 append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
556 LDST_SRCDST_BYTE_CONTEXT);
557
558 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
559 desc_bytes(desc),
560 DMA_TO_DEVICE);
561 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
562 dev_err(jrdev, "unable to map shared descriptor\n");
563 return -ENOMEM;
564 }
565#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300566 print_hex_dump(KERN_ERR, "aead enc shdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad2011-07-15 11:21:42 +0800567 DUMP_PREFIX_ADDRESS, 16, 4, desc,
568 desc_bytes(desc), 1);
569#endif
570
Herbert Xu479bcc72015-07-30 17:53:17 +0800571skip_enc:
Yuan Kang1acebad2011-07-15 11:21:42 +0800572 /*
573 * Job Descriptor and Shared Descriptors
574 * must all fit into the 64-word Descriptor h/w Buffer
575 */
Vakul Garg80cd88f2014-05-09 20:34:40 -0500576 keys_fit_inline = false;
Herbert Xu479bcc72015-07-30 17:53:17 +0800577 if (DESC_AEAD_DEC_LEN + AUTHENC_DESC_JOB_IO_LEN +
Catalin Vasiledaebc462014-10-31 12:45:37 +0200578 ctx->split_key_pad_len + ctx->enckeylen +
579 (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0) <=
Yuan Kang1acebad2011-07-15 11:21:42 +0800580 CAAM_DESC_BYTES_MAX)
Kim Phillips2af8f4a2012-09-07 04:17:03 +0800581 keys_fit_inline = true;
Yuan Kang1acebad2011-07-15 11:21:42 +0800582
Herbert Xu479bcc72015-07-30 17:53:17 +0800583 /* aead_decrypt shared descriptor */
Yuan Kang1acebad2011-07-15 11:21:42 +0800584 desc = ctx->sh_desc_dec;
585
Catalin Vasiledaebc462014-10-31 12:45:37 +0200586 /* Note: Context registers are saved. */
587 init_sh_desc_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
Yuan Kang1acebad2011-07-15 11:21:42 +0800588
589 /* Class 2 operation */
590 append_operation(desc, ctx->class2_alg_type |
591 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
592
Herbert Xu479bcc72015-07-30 17:53:17 +0800593 /* Read and write assoclen bytes */
594 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
Horia Geantă8b18e232016-08-29 14:52:14 +0300595 if (alg->caam.geniv)
596 append_math_add_imm_u32(desc, VARSEQOUTLEN, REG3, IMM, ivsize);
597 else
598 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
Herbert Xu479bcc72015-07-30 17:53:17 +0800599
600 /* Skip assoc data */
601 append_seq_fifo_store(desc, 0, FIFOST_TYPE_SKIP | FIFOLDST_VLF);
Yuan Kang1acebad2011-07-15 11:21:42 +0800602
603 /* read assoc before reading payload */
604 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
605 KEY_VLF);
606
Horia Geantă8b18e232016-08-29 14:52:14 +0300607 if (alg->caam.geniv) {
608 append_seq_load(desc, ivsize, LDST_CLASS_1_CCB |
609 LDST_SRCDST_BYTE_CONTEXT |
610 (ctx1_iv_off << LDST_OFFSET_SHIFT));
611 append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO |
612 (ctx1_iv_off << MOVE_OFFSET_SHIFT) | ivsize);
613 }
614
Catalin Vasiledaebc462014-10-31 12:45:37 +0200615 /* Load Counter into CONTEXT1 reg */
616 if (is_rfc3686)
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +0300617 append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
618 LDST_SRCDST_BYTE_CONTEXT |
619 ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
620 LDST_OFFSET_SHIFT));
Catalin Vasiledaebc462014-10-31 12:45:37 +0200621
622 /* Choose operation */
623 if (ctr_mode)
624 append_operation(desc, ctx->class1_alg_type |
625 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT);
626 else
627 append_dec_op1(desc, ctx->class1_alg_type);
Yuan Kang1acebad2011-07-15 11:21:42 +0800628
629 /* Read and write cryptlen bytes */
Herbert Xu479bcc72015-07-30 17:53:17 +0800630 append_math_add(desc, VARSEQINLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
631 append_math_add(desc, VARSEQOUTLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
Yuan Kang1acebad2011-07-15 11:21:42 +0800632 aead_append_src_dst(desc, FIFOLD_TYPE_MSG);
633
634 /* Load ICV */
635 append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
636 FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
Yuan Kang1acebad2011-07-15 11:21:42 +0800637
638 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
639 desc_bytes(desc),
640 DMA_TO_DEVICE);
641 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
642 dev_err(jrdev, "unable to map shared descriptor\n");
643 return -ENOMEM;
644 }
645#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300646 print_hex_dump(KERN_ERR, "aead dec shdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad2011-07-15 11:21:42 +0800647 DUMP_PREFIX_ADDRESS, 16, 4, desc,
648 desc_bytes(desc), 1);
649#endif
650
Herbert Xu479bcc72015-07-30 17:53:17 +0800651 if (!alg->caam.geniv)
652 goto skip_givenc;
653
Yuan Kang1acebad2011-07-15 11:21:42 +0800654 /*
655 * Job Descriptor and Shared Descriptors
656 * must all fit into the 64-word Descriptor h/w Buffer
657 */
Vakul Garg80cd88f2014-05-09 20:34:40 -0500658 keys_fit_inline = false;
Herbert Xu479bcc72015-07-30 17:53:17 +0800659 if (DESC_AEAD_GIVENC_LEN + AUTHENC_DESC_JOB_IO_LEN +
Catalin Vasiledaebc462014-10-31 12:45:37 +0200660 ctx->split_key_pad_len + ctx->enckeylen +
661 (is_rfc3686 ? DESC_AEAD_CTR_RFC3686_LEN : 0) <=
Yuan Kang1acebad2011-07-15 11:21:42 +0800662 CAAM_DESC_BYTES_MAX)
Kim Phillips2af8f4a2012-09-07 04:17:03 +0800663 keys_fit_inline = true;
Yuan Kang1acebad2011-07-15 11:21:42 +0800664
665 /* aead_givencrypt shared descriptor */
Horia Geantă1d2d87e2016-08-04 20:02:46 +0300666 desc = ctx->sh_desc_enc;
Yuan Kang1acebad2011-07-15 11:21:42 +0800667
Catalin Vasiledaebc462014-10-31 12:45:37 +0200668 /* Note: Context registers are saved. */
669 init_sh_desc_key_aead(desc, ctx, keys_fit_inline, is_rfc3686);
Yuan Kang1acebad2011-07-15 11:21:42 +0800670
Herbert Xu479bcc72015-07-30 17:53:17 +0800671 if (is_rfc3686)
672 goto copy_iv;
673
Yuan Kang1acebad2011-07-15 11:21:42 +0800674 /* Generate IV */
675 geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
676 NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
Herbert Xuadd86d52015-05-11 17:47:50 +0800677 NFIFOENTRY_PTYPE_RND | (ivsize << NFIFOENTRY_DLEN_SHIFT);
Yuan Kang1acebad2011-07-15 11:21:42 +0800678 append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
679 LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
680 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
Catalin Vasiledaebc462014-10-31 12:45:37 +0200681 append_move(desc, MOVE_WAITCOMP |
682 MOVE_SRC_INFIFO | MOVE_DEST_CLASS1CTX |
683 (ctx1_iv_off << MOVE_OFFSET_SHIFT) |
Herbert Xuadd86d52015-05-11 17:47:50 +0800684 (ivsize << MOVE_LEN_SHIFT));
Yuan Kang1acebad2011-07-15 11:21:42 +0800685 append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
686
Herbert Xu479bcc72015-07-30 17:53:17 +0800687copy_iv:
Yuan Kang1acebad2011-07-15 11:21:42 +0800688 /* Copy IV to class 1 context */
Catalin Vasiledaebc462014-10-31 12:45:37 +0200689 append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_OUTFIFO |
690 (ctx1_iv_off << MOVE_OFFSET_SHIFT) |
Herbert Xuadd86d52015-05-11 17:47:50 +0800691 (ivsize << MOVE_LEN_SHIFT));
Yuan Kang1acebad2011-07-15 11:21:42 +0800692
693 /* Return to encryption */
694 append_operation(desc, ctx->class2_alg_type |
695 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
696
Herbert Xu479bcc72015-07-30 17:53:17 +0800697 /* Read and write assoclen bytes */
698 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
699 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
700
Horia Geantă1d2d87e2016-08-04 20:02:46 +0300701 /* ivsize + cryptlen = seqoutlen - authsize */
702 append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
703
Herbert Xu479bcc72015-07-30 17:53:17 +0800704 /* Skip assoc data */
705 append_seq_fifo_store(desc, 0, FIFOST_TYPE_SKIP | FIFOLDST_VLF);
Yuan Kang1acebad2011-07-15 11:21:42 +0800706
707 /* read assoc before reading payload */
708 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
709 KEY_VLF);
710
Catalin Vasiledaebc462014-10-31 12:45:37 +0200711 /* Copy iv from outfifo to class 2 fifo */
Yuan Kang1acebad2011-07-15 11:21:42 +0800712 moveiv = NFIFOENTRY_STYPE_OFIFO | NFIFOENTRY_DEST_CLASS2 |
Herbert Xuadd86d52015-05-11 17:47:50 +0800713 NFIFOENTRY_DTYPE_MSG | (ivsize << NFIFOENTRY_DLEN_SHIFT);
Yuan Kang1acebad2011-07-15 11:21:42 +0800714 append_load_imm_u32(desc, moveiv, LDST_CLASS_IND_CCB |
715 LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
Herbert Xuadd86d52015-05-11 17:47:50 +0800716 append_load_imm_u32(desc, ivsize, LDST_CLASS_2_CCB |
Yuan Kang1acebad2011-07-15 11:21:42 +0800717 LDST_SRCDST_WORD_DATASZ_REG | LDST_IMM);
718
Catalin Vasiledaebc462014-10-31 12:45:37 +0200719 /* Load Counter into CONTEXT1 reg */
720 if (is_rfc3686)
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +0300721 append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
722 LDST_SRCDST_BYTE_CONTEXT |
723 ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
724 LDST_OFFSET_SHIFT));
Catalin Vasiledaebc462014-10-31 12:45:37 +0200725
Yuan Kang1acebad2011-07-15 11:21:42 +0800726 /* Class 1 operation */
727 append_operation(desc, ctx->class1_alg_type |
728 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
729
730 /* Will write ivsize + cryptlen */
731 append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
732
733 /* Not need to reload iv */
Herbert Xuadd86d52015-05-11 17:47:50 +0800734 append_seq_fifo_load(desc, ivsize,
Yuan Kang1acebad2011-07-15 11:21:42 +0800735 FIFOLD_CLASS_SKIP);
736
737 /* Will read cryptlen */
738 append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
Alex Porosanud128af12016-11-09 10:46:11 +0200739 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH | KEY_VLF |
740 FIFOLD_TYPE_MSG1OUT2 | FIFOLD_TYPE_LASTBOTH);
741 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
Yuan Kang1acebad2011-07-15 11:21:42 +0800742
743 /* Write ICV */
744 append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
745 LDST_SRCDST_BYTE_CONTEXT);
746
Herbert Xu479bcc72015-07-30 17:53:17 +0800747 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
748 desc_bytes(desc),
749 DMA_TO_DEVICE);
Horia Geantă1d2d87e2016-08-04 20:02:46 +0300750 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
Yuan Kang1acebad2011-07-15 11:21:42 +0800751 dev_err(jrdev, "unable to map shared descriptor\n");
752 return -ENOMEM;
753 }
754#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +0300755 print_hex_dump(KERN_ERR, "aead givenc shdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad2011-07-15 11:21:42 +0800756 DUMP_PREFIX_ADDRESS, 16, 4, desc,
757 desc_bytes(desc), 1);
758#endif
759
Herbert Xu479bcc72015-07-30 17:53:17 +0800760skip_givenc:
Yuan Kang1acebad2011-07-15 11:21:42 +0800761 return 0;
762}
763
Yuan Kang0e479302011-07-15 11:21:41 +0800764static int aead_setauthsize(struct crypto_aead *authenc,
Kim Phillips8e8ec592011-03-13 16:54:26 +0800765 unsigned int authsize)
766{
767 struct caam_ctx *ctx = crypto_aead_ctx(authenc);
768
769 ctx->authsize = authsize;
Yuan Kang1acebad2011-07-15 11:21:42 +0800770 aead_set_sh_desc(authenc);
Kim Phillips8e8ec592011-03-13 16:54:26 +0800771
772 return 0;
773}
774
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300775static int gcm_set_sh_desc(struct crypto_aead *aead)
776{
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300777 struct caam_ctx *ctx = crypto_aead_ctx(aead);
778 struct device *jrdev = ctx->jrdev;
779 bool keys_fit_inline = false;
780 u32 *key_jump_cmd, *zero_payload_jump_cmd,
781 *zero_assoc_jump_cmd1, *zero_assoc_jump_cmd2;
782 u32 *desc;
783
784 if (!ctx->enckeylen || !ctx->authsize)
785 return 0;
786
787 /*
788 * AES GCM encrypt shared descriptor
789 * Job Descriptor and Shared Descriptor
790 * must fit into the 64-word Descriptor h/w Buffer
791 */
Herbert Xuf2147b82015-06-16 13:54:23 +0800792 if (DESC_GCM_ENC_LEN + GCM_DESC_JOB_IO_LEN +
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300793 ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
794 keys_fit_inline = true;
795
796 desc = ctx->sh_desc_enc;
797
798 init_sh_desc(desc, HDR_SHARE_SERIAL);
799
800 /* skip key loading if they are loaded due to sharing */
801 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
802 JUMP_COND_SHRD | JUMP_COND_SELF);
803 if (keys_fit_inline)
804 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
805 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
806 else
807 append_key(desc, ctx->key_dma, ctx->enckeylen,
808 CLASS_1 | KEY_DEST_CLASS_REG);
809 set_jump_tgt_here(desc, key_jump_cmd);
810
811 /* class 1 operation */
812 append_operation(desc, ctx->class1_alg_type |
813 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
814
Herbert Xuf2147b82015-06-16 13:54:23 +0800815 /* if assoclen + cryptlen is ZERO, skip to ICV write */
816 append_math_sub(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
817 zero_assoc_jump_cmd2 = append_jump(desc, JUMP_TEST_ALL |
818 JUMP_COND_MATH_Z);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300819
820 /* if assoclen is ZERO, skip reading the assoc data */
Herbert Xuf2147b82015-06-16 13:54:23 +0800821 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300822 zero_assoc_jump_cmd1 = append_jump(desc, JUMP_TEST_ALL |
Herbert Xuf2147b82015-06-16 13:54:23 +0800823 JUMP_COND_MATH_Z);
824
825 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
826
827 /* skip assoc data */
828 append_seq_fifo_store(desc, 0, FIFOST_TYPE_SKIP | FIFOLDST_VLF);
829
830 /* cryptlen = seqinlen - assoclen */
831 append_math_sub(desc, VARSEQOUTLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
832
833 /* if cryptlen is ZERO jump to zero-payload commands */
834 zero_payload_jump_cmd = append_jump(desc, JUMP_TEST_ALL |
835 JUMP_COND_MATH_Z);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300836
837 /* read assoc data */
838 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
839 FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
840 set_jump_tgt_here(desc, zero_assoc_jump_cmd1);
841
Herbert Xuf2147b82015-06-16 13:54:23 +0800842 append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300843
844 /* write encrypted data */
845 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
846
847 /* read payload data */
848 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
849 FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
850
851 /* jump the zero-payload commands */
Herbert Xuf2147b82015-06-16 13:54:23 +0800852 append_jump(desc, JUMP_TEST_ALL | 2);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300853
854 /* zero-payload commands */
855 set_jump_tgt_here(desc, zero_payload_jump_cmd);
856
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300857 /* read assoc data */
858 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
859 FIFOLD_TYPE_AAD | FIFOLD_TYPE_LAST1);
860
Herbert Xuf2147b82015-06-16 13:54:23 +0800861 /* There is no input data */
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300862 set_jump_tgt_here(desc, zero_assoc_jump_cmd2);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300863
864 /* write ICV */
865 append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
866 LDST_SRCDST_BYTE_CONTEXT);
867
868 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
869 desc_bytes(desc),
870 DMA_TO_DEVICE);
871 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
872 dev_err(jrdev, "unable to map shared descriptor\n");
873 return -ENOMEM;
874 }
875#ifdef DEBUG
876 print_hex_dump(KERN_ERR, "gcm enc shdesc@"__stringify(__LINE__)": ",
877 DUMP_PREFIX_ADDRESS, 16, 4, desc,
878 desc_bytes(desc), 1);
879#endif
880
881 /*
882 * Job Descriptor and Shared Descriptors
883 * must all fit into the 64-word Descriptor h/w Buffer
884 */
885 keys_fit_inline = false;
Herbert Xuf2147b82015-06-16 13:54:23 +0800886 if (DESC_GCM_DEC_LEN + GCM_DESC_JOB_IO_LEN +
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300887 ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
888 keys_fit_inline = true;
889
890 desc = ctx->sh_desc_dec;
891
892 init_sh_desc(desc, HDR_SHARE_SERIAL);
893
894 /* skip key loading if they are loaded due to sharing */
895 key_jump_cmd = append_jump(desc, JUMP_JSL |
896 JUMP_TEST_ALL | JUMP_COND_SHRD |
897 JUMP_COND_SELF);
898 if (keys_fit_inline)
899 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
900 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
901 else
902 append_key(desc, ctx->key_dma, ctx->enckeylen,
903 CLASS_1 | KEY_DEST_CLASS_REG);
904 set_jump_tgt_here(desc, key_jump_cmd);
905
906 /* class 1 operation */
907 append_operation(desc, ctx->class1_alg_type |
908 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
909
Herbert Xuf2147b82015-06-16 13:54:23 +0800910 /* if assoclen is ZERO, skip reading the assoc data */
911 append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300912 zero_assoc_jump_cmd1 = append_jump(desc, JUMP_TEST_ALL |
Herbert Xuf2147b82015-06-16 13:54:23 +0800913 JUMP_COND_MATH_Z);
914
915 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
916
917 /* skip assoc data */
918 append_seq_fifo_store(desc, 0, FIFOST_TYPE_SKIP | FIFOLDST_VLF);
919
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300920 /* read assoc data */
921 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
922 FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
Herbert Xuf2147b82015-06-16 13:54:23 +0800923
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300924 set_jump_tgt_here(desc, zero_assoc_jump_cmd1);
925
Herbert Xuf2147b82015-06-16 13:54:23 +0800926 /* cryptlen = seqoutlen - assoclen */
927 append_math_sub(desc, VARSEQINLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
928
929 /* jump to zero-payload command if cryptlen is zero */
930 zero_payload_jump_cmd = append_jump(desc, JUMP_TEST_ALL |
931 JUMP_COND_MATH_Z);
932
933 append_math_sub(desc, VARSEQOUTLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300934
935 /* store encrypted data */
936 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
937
938 /* read payload data */
939 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
940 FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
941
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300942 /* zero-payload command */
943 set_jump_tgt_here(desc, zero_payload_jump_cmd);
944
Tudor Ambarus3ef8d942014-10-23 16:11:23 +0300945 /* read ICV */
946 append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS1 |
947 FIFOLD_TYPE_ICV | FIFOLD_TYPE_LAST1);
948
949 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
950 desc_bytes(desc),
951 DMA_TO_DEVICE);
952 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
953 dev_err(jrdev, "unable to map shared descriptor\n");
954 return -ENOMEM;
955 }
956#ifdef DEBUG
957 print_hex_dump(KERN_ERR, "gcm dec shdesc@"__stringify(__LINE__)": ",
958 DUMP_PREFIX_ADDRESS, 16, 4, desc,
959 desc_bytes(desc), 1);
960#endif
961
962 return 0;
963}
964
965static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize)
966{
967 struct caam_ctx *ctx = crypto_aead_ctx(authenc);
968
969 ctx->authsize = authsize;
970 gcm_set_sh_desc(authenc);
971
972 return 0;
973}
974
Tudor Ambarusbac68f22014-10-23 16:14:03 +0300975static int rfc4106_set_sh_desc(struct crypto_aead *aead)
976{
Tudor Ambarusbac68f22014-10-23 16:14:03 +0300977 struct caam_ctx *ctx = crypto_aead_ctx(aead);
978 struct device *jrdev = ctx->jrdev;
979 bool keys_fit_inline = false;
Herbert Xuf2147b82015-06-16 13:54:23 +0800980 u32 *key_jump_cmd;
Tudor Ambarusbac68f22014-10-23 16:14:03 +0300981 u32 *desc;
Tudor Ambarusbac68f22014-10-23 16:14:03 +0300982
983 if (!ctx->enckeylen || !ctx->authsize)
984 return 0;
985
986 /*
987 * RFC4106 encrypt shared descriptor
988 * Job Descriptor and Shared Descriptor
989 * must fit into the 64-word Descriptor h/w Buffer
990 */
Herbert Xuf2147b82015-06-16 13:54:23 +0800991 if (DESC_RFC4106_ENC_LEN + GCM_DESC_JOB_IO_LEN +
Tudor Ambarusbac68f22014-10-23 16:14:03 +0300992 ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
993 keys_fit_inline = true;
994
995 desc = ctx->sh_desc_enc;
996
997 init_sh_desc(desc, HDR_SHARE_SERIAL);
998
999 /* Skip key loading if it is loaded due to sharing */
1000 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
1001 JUMP_COND_SHRD);
1002 if (keys_fit_inline)
1003 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1004 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
1005 else
1006 append_key(desc, ctx->key_dma, ctx->enckeylen,
1007 CLASS_1 | KEY_DEST_CLASS_REG);
1008 set_jump_tgt_here(desc, key_jump_cmd);
1009
1010 /* Class 1 operation */
1011 append_operation(desc, ctx->class1_alg_type |
1012 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
1013
Herbert Xu46218752015-07-09 07:17:33 +08001014 append_math_sub_imm_u32(desc, VARSEQINLEN, REG3, IMM, 8);
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001015 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
1016
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001017 /* Read assoc data */
1018 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
1019 FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
1020
Herbert Xu46218752015-07-09 07:17:33 +08001021 /* Skip IV */
1022 append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP);
Herbert Xuf2147b82015-06-16 13:54:23 +08001023
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001024 /* Will read cryptlen bytes */
Herbert Xuf2147b82015-06-16 13:54:23 +08001025 append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001026
Horia Geant?4aad0cc2015-07-30 22:11:18 +03001027 /* Workaround for erratum A-005473 (simultaneous SEQ FIFO skips) */
1028 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLD_TYPE_MSG);
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001029
Herbert Xu46218752015-07-09 07:17:33 +08001030 /* Skip assoc data */
1031 append_seq_fifo_store(desc, 0, FIFOST_TYPE_SKIP | FIFOLDST_VLF);
1032
1033 /* cryptlen = seqoutlen - assoclen */
Horia Geant?4aad0cc2015-07-30 22:11:18 +03001034 append_math_sub(desc, VARSEQOUTLEN, VARSEQINLEN, REG0, CAAM_CMD_SZ);
Herbert Xu46218752015-07-09 07:17:33 +08001035
1036 /* Write encrypted data */
1037 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
1038
Horia Geant?4aad0cc2015-07-30 22:11:18 +03001039 /* Read payload data */
1040 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
1041 FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
1042
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001043 /* Write ICV */
1044 append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
1045 LDST_SRCDST_BYTE_CONTEXT);
1046
1047 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
1048 desc_bytes(desc),
1049 DMA_TO_DEVICE);
1050 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
1051 dev_err(jrdev, "unable to map shared descriptor\n");
1052 return -ENOMEM;
1053 }
1054#ifdef DEBUG
1055 print_hex_dump(KERN_ERR, "rfc4106 enc shdesc@"__stringify(__LINE__)": ",
1056 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1057 desc_bytes(desc), 1);
1058#endif
1059
1060 /*
1061 * Job Descriptor and Shared Descriptors
1062 * must all fit into the 64-word Descriptor h/w Buffer
1063 */
1064 keys_fit_inline = false;
1065 if (DESC_RFC4106_DEC_LEN + DESC_JOB_IO_LEN +
1066 ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
1067 keys_fit_inline = true;
1068
1069 desc = ctx->sh_desc_dec;
1070
1071 init_sh_desc(desc, HDR_SHARE_SERIAL);
1072
1073 /* Skip key loading if it is loaded due to sharing */
1074 key_jump_cmd = append_jump(desc, JUMP_JSL |
1075 JUMP_TEST_ALL | JUMP_COND_SHRD);
1076 if (keys_fit_inline)
1077 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1078 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
1079 else
1080 append_key(desc, ctx->key_dma, ctx->enckeylen,
1081 CLASS_1 | KEY_DEST_CLASS_REG);
1082 set_jump_tgt_here(desc, key_jump_cmd);
1083
1084 /* Class 1 operation */
1085 append_operation(desc, ctx->class1_alg_type |
1086 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
1087
Herbert Xu46218752015-07-09 07:17:33 +08001088 append_math_sub_imm_u32(desc, VARSEQINLEN, REG3, IMM, 8);
Herbert Xuf2147b82015-06-16 13:54:23 +08001089 append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001090
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001091 /* Read assoc data */
1092 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
1093 FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1);
1094
Herbert Xu46218752015-07-09 07:17:33 +08001095 /* Skip IV */
1096 append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP);
Herbert Xuf2147b82015-06-16 13:54:23 +08001097
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001098 /* Will read cryptlen bytes */
Herbert Xu46218752015-07-09 07:17:33 +08001099 append_math_sub(desc, VARSEQINLEN, SEQOUTLEN, REG3, CAAM_CMD_SZ);
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001100
Horia Geant?4aad0cc2015-07-30 22:11:18 +03001101 /* Workaround for erratum A-005473 (simultaneous SEQ FIFO skips) */
1102 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLD_TYPE_MSG);
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001103
Herbert Xu46218752015-07-09 07:17:33 +08001104 /* Skip assoc data */
1105 append_seq_fifo_store(desc, 0, FIFOST_TYPE_SKIP | FIFOLDST_VLF);
1106
1107 /* Will write cryptlen bytes */
1108 append_math_sub(desc, VARSEQOUTLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
1109
1110 /* Store payload data */
1111 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
1112
Horia Geant?4aad0cc2015-07-30 22:11:18 +03001113 /* Read encrypted data */
1114 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF |
1115 FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1);
1116
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001117 /* Read ICV */
1118 append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS1 |
1119 FIFOLD_TYPE_ICV | FIFOLD_TYPE_LAST1);
1120
1121 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
1122 desc_bytes(desc),
1123 DMA_TO_DEVICE);
1124 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
1125 dev_err(jrdev, "unable to map shared descriptor\n");
1126 return -ENOMEM;
1127 }
1128#ifdef DEBUG
1129 print_hex_dump(KERN_ERR, "rfc4106 dec shdesc@"__stringify(__LINE__)": ",
1130 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1131 desc_bytes(desc), 1);
1132#endif
1133
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001134 return 0;
1135}
1136
1137static int rfc4106_setauthsize(struct crypto_aead *authenc,
1138 unsigned int authsize)
1139{
1140 struct caam_ctx *ctx = crypto_aead_ctx(authenc);
1141
1142 ctx->authsize = authsize;
1143 rfc4106_set_sh_desc(authenc);
1144
1145 return 0;
1146}
1147
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001148static int rfc4543_set_sh_desc(struct crypto_aead *aead)
1149{
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001150 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1151 struct device *jrdev = ctx->jrdev;
1152 bool keys_fit_inline = false;
Herbert Xuf2147b82015-06-16 13:54:23 +08001153 u32 *key_jump_cmd;
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001154 u32 *read_move_cmd, *write_move_cmd;
1155 u32 *desc;
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001156
1157 if (!ctx->enckeylen || !ctx->authsize)
1158 return 0;
1159
1160 /*
1161 * RFC4543 encrypt shared descriptor
1162 * Job Descriptor and Shared Descriptor
1163 * must fit into the 64-word Descriptor h/w Buffer
1164 */
Herbert Xuf2147b82015-06-16 13:54:23 +08001165 if (DESC_RFC4543_ENC_LEN + GCM_DESC_JOB_IO_LEN +
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001166 ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
1167 keys_fit_inline = true;
1168
1169 desc = ctx->sh_desc_enc;
1170
1171 init_sh_desc(desc, HDR_SHARE_SERIAL);
1172
1173 /* Skip key loading if it is loaded due to sharing */
1174 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
1175 JUMP_COND_SHRD);
1176 if (keys_fit_inline)
1177 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1178 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
1179 else
1180 append_key(desc, ctx->key_dma, ctx->enckeylen,
1181 CLASS_1 | KEY_DEST_CLASS_REG);
1182 set_jump_tgt_here(desc, key_jump_cmd);
1183
1184 /* Class 1 operation */
1185 append_operation(desc, ctx->class1_alg_type |
1186 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
1187
Herbert Xuf2147b82015-06-16 13:54:23 +08001188 /* assoclen + cryptlen = seqinlen */
1189 append_math_sub(desc, REG3, SEQINLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001190
1191 /*
1192 * MOVE_LEN opcode is not available in all SEC HW revisions,
1193 * thus need to do some magic, i.e. self-patch the descriptor
1194 * buffer.
1195 */
1196 read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH3 |
1197 (0x6 << MOVE_LEN_SHIFT));
1198 write_move_cmd = append_move(desc, MOVE_SRC_MATH3 | MOVE_DEST_DESCBUF |
1199 (0x8 << MOVE_LEN_SHIFT));
1200
Herbert Xuf2147b82015-06-16 13:54:23 +08001201 /* Will read assoclen + cryptlen bytes */
1202 append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001203
Herbert Xuf2147b82015-06-16 13:54:23 +08001204 /* Will write assoclen + cryptlen bytes */
1205 append_math_sub(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
1206
1207 /* Read and write assoclen + cryptlen bytes */
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001208 aead_append_src_dst(desc, FIFOLD_TYPE_AAD);
1209
1210 set_move_tgt_here(desc, read_move_cmd);
1211 set_move_tgt_here(desc, write_move_cmd);
1212 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
1213 /* Move payload data to OFIFO */
1214 append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO);
1215
1216 /* Write ICV */
1217 append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB |
1218 LDST_SRCDST_BYTE_CONTEXT);
1219
1220 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
1221 desc_bytes(desc),
1222 DMA_TO_DEVICE);
1223 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
1224 dev_err(jrdev, "unable to map shared descriptor\n");
1225 return -ENOMEM;
1226 }
1227#ifdef DEBUG
1228 print_hex_dump(KERN_ERR, "rfc4543 enc shdesc@"__stringify(__LINE__)": ",
1229 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1230 desc_bytes(desc), 1);
1231#endif
1232
1233 /*
1234 * Job Descriptor and Shared Descriptors
1235 * must all fit into the 64-word Descriptor h/w Buffer
1236 */
1237 keys_fit_inline = false;
Herbert Xuf2147b82015-06-16 13:54:23 +08001238 if (DESC_RFC4543_DEC_LEN + GCM_DESC_JOB_IO_LEN +
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001239 ctx->enckeylen <= CAAM_DESC_BYTES_MAX)
1240 keys_fit_inline = true;
1241
1242 desc = ctx->sh_desc_dec;
1243
1244 init_sh_desc(desc, HDR_SHARE_SERIAL);
1245
1246 /* Skip key loading if it is loaded due to sharing */
1247 key_jump_cmd = append_jump(desc, JUMP_JSL |
1248 JUMP_TEST_ALL | JUMP_COND_SHRD);
1249 if (keys_fit_inline)
1250 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1251 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
1252 else
1253 append_key(desc, ctx->key_dma, ctx->enckeylen,
1254 CLASS_1 | KEY_DEST_CLASS_REG);
1255 set_jump_tgt_here(desc, key_jump_cmd);
1256
1257 /* Class 1 operation */
1258 append_operation(desc, ctx->class1_alg_type |
1259 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
1260
Herbert Xuf2147b82015-06-16 13:54:23 +08001261 /* assoclen + cryptlen = seqoutlen */
1262 append_math_sub(desc, REG3, SEQOUTLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001263
1264 /*
1265 * MOVE_LEN opcode is not available in all SEC HW revisions,
1266 * thus need to do some magic, i.e. self-patch the descriptor
1267 * buffer.
1268 */
1269 read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH3 |
1270 (0x6 << MOVE_LEN_SHIFT));
1271 write_move_cmd = append_move(desc, MOVE_SRC_MATH3 | MOVE_DEST_DESCBUF |
1272 (0x8 << MOVE_LEN_SHIFT));
1273
Herbert Xuf2147b82015-06-16 13:54:23 +08001274 /* Will read assoclen + cryptlen bytes */
1275 append_math_sub(desc, VARSEQINLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001276
Herbert Xuf2147b82015-06-16 13:54:23 +08001277 /* Will write assoclen + cryptlen bytes */
1278 append_math_sub(desc, VARSEQOUTLEN, SEQOUTLEN, REG0, CAAM_CMD_SZ);
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001279
1280 /* Store payload data */
1281 append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF);
1282
Herbert Xuf2147b82015-06-16 13:54:23 +08001283 /* In-snoop assoclen + cryptlen data */
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001284 append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH | FIFOLDST_VLF |
1285 FIFOLD_TYPE_AAD | FIFOLD_TYPE_LAST2FLUSH1);
1286
1287 set_move_tgt_here(desc, read_move_cmd);
1288 set_move_tgt_here(desc, write_move_cmd);
1289 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
1290 /* Move payload data to OFIFO */
1291 append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO);
1292 append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
1293
1294 /* Read ICV */
1295 append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS1 |
1296 FIFOLD_TYPE_ICV | FIFOLD_TYPE_LAST1);
1297
1298 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
1299 desc_bytes(desc),
1300 DMA_TO_DEVICE);
1301 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
1302 dev_err(jrdev, "unable to map shared descriptor\n");
1303 return -ENOMEM;
1304 }
1305#ifdef DEBUG
1306 print_hex_dump(KERN_ERR, "rfc4543 dec shdesc@"__stringify(__LINE__)": ",
1307 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1308 desc_bytes(desc), 1);
1309#endif
1310
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001311 return 0;
1312}
1313
1314static int rfc4543_setauthsize(struct crypto_aead *authenc,
1315 unsigned int authsize)
1316{
1317 struct caam_ctx *ctx = crypto_aead_ctx(authenc);
1318
1319 ctx->authsize = authsize;
1320 rfc4543_set_sh_desc(authenc);
1321
1322 return 0;
1323}
1324
Yuan Kang4c1ec1f2012-06-22 19:48:45 -05001325static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in,
1326 u32 authkeylen)
Kim Phillips8e8ec592011-03-13 16:54:26 +08001327{
Yuan Kang4c1ec1f2012-06-22 19:48:45 -05001328 return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
1329 ctx->split_key_pad_len, key_in, authkeylen,
1330 ctx->alg_op);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001331}
1332
Yuan Kang0e479302011-07-15 11:21:41 +08001333static int aead_setkey(struct crypto_aead *aead,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001334 const u8 *key, unsigned int keylen)
1335{
1336 /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
1337 static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
1338 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1339 struct device *jrdev = ctx->jrdev;
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001340 struct crypto_authenc_keys keys;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001341 int ret = 0;
1342
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001343 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Kim Phillips8e8ec592011-03-13 16:54:26 +08001344 goto badkey;
1345
1346 /* Pick class 2 key length from algorithm submask */
1347 ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
1348 OP_ALG_ALGSEL_SHIFT] * 2;
1349 ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
1350
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001351 if (ctx->split_key_pad_len + keys.enckeylen > CAAM_MAX_KEY_SIZE)
1352 goto badkey;
1353
Kim Phillips8e8ec592011-03-13 16:54:26 +08001354#ifdef DEBUG
1355 printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001356 keys.authkeylen + keys.enckeylen, keys.enckeylen,
1357 keys.authkeylen);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001358 printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
1359 ctx->split_key_len, ctx->split_key_pad_len);
Alex Porosanu514df282013-08-14 18:56:45 +03001360 print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
Kim Phillips8e8ec592011-03-13 16:54:26 +08001361 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
1362#endif
Kim Phillips8e8ec592011-03-13 16:54:26 +08001363
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001364 ret = gen_split_aead_key(ctx, keys.authkey, keys.authkeylen);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001365 if (ret) {
Kim Phillips8e8ec592011-03-13 16:54:26 +08001366 goto badkey;
1367 }
1368
1369 /* postpend encryption key to auth split key */
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001370 memcpy(ctx->key + ctx->split_key_pad_len, keys.enckey, keys.enckeylen);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001371
Yuan Kang885e9e22011-07-15 11:21:41 +08001372 ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len +
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001373 keys.enckeylen, DMA_TO_DEVICE);
Yuan Kang885e9e22011-07-15 11:21:41 +08001374 if (dma_mapping_error(jrdev, ctx->key_dma)) {
Kim Phillips8e8ec592011-03-13 16:54:26 +08001375 dev_err(jrdev, "unable to map key i/o memory\n");
Kim Phillips8e8ec592011-03-13 16:54:26 +08001376 return -ENOMEM;
1377 }
1378#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001379 print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
Kim Phillips8e8ec592011-03-13 16:54:26 +08001380 DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001381 ctx->split_key_pad_len + keys.enckeylen, 1);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001382#endif
1383
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001384 ctx->enckeylen = keys.enckeylen;
Kim Phillips8e8ec592011-03-13 16:54:26 +08001385
Yuan Kang1acebad2011-07-15 11:21:42 +08001386 ret = aead_set_sh_desc(aead);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001387 if (ret) {
Yuan Kang885e9e22011-07-15 11:21:41 +08001388 dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len +
Horia Geanta4e6e0b22013-12-19 17:27:35 +02001389 keys.enckeylen, DMA_TO_DEVICE);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001390 }
1391
1392 return ret;
1393badkey:
1394 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
1395 return -EINVAL;
1396}
1397
Tudor Ambarus3ef8d942014-10-23 16:11:23 +03001398static int gcm_setkey(struct crypto_aead *aead,
1399 const u8 *key, unsigned int keylen)
1400{
1401 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1402 struct device *jrdev = ctx->jrdev;
1403 int ret = 0;
1404
1405#ifdef DEBUG
1406 print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
1407 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
1408#endif
1409
1410 memcpy(ctx->key, key, keylen);
1411 ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen,
1412 DMA_TO_DEVICE);
1413 if (dma_mapping_error(jrdev, ctx->key_dma)) {
1414 dev_err(jrdev, "unable to map key i/o memory\n");
1415 return -ENOMEM;
1416 }
1417 ctx->enckeylen = keylen;
1418
1419 ret = gcm_set_sh_desc(aead);
1420 if (ret) {
1421 dma_unmap_single(jrdev, ctx->key_dma, ctx->enckeylen,
1422 DMA_TO_DEVICE);
1423 }
1424
1425 return ret;
1426}
1427
Tudor Ambarusbac68f22014-10-23 16:14:03 +03001428static int rfc4106_setkey(struct crypto_aead *aead,
1429 const u8 *key, unsigned int keylen)
1430{
1431 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1432 struct device *jrdev = ctx->jrdev;
1433 int ret = 0;
1434
1435 if (keylen < 4)
1436 return -EINVAL;
1437
1438#ifdef DEBUG
1439 print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
1440 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
1441#endif
1442
1443 memcpy(ctx->key, key, keylen);
1444
1445 /*
1446 * The last four bytes of the key material are used as the salt value
1447 * in the nonce. Update the AES key length.
1448 */
1449 ctx->enckeylen = keylen - 4;
1450
1451 ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->enckeylen,
1452 DMA_TO_DEVICE);
1453 if (dma_mapping_error(jrdev, ctx->key_dma)) {
1454 dev_err(jrdev, "unable to map key i/o memory\n");
1455 return -ENOMEM;
1456 }
1457
1458 ret = rfc4106_set_sh_desc(aead);
1459 if (ret) {
1460 dma_unmap_single(jrdev, ctx->key_dma, ctx->enckeylen,
1461 DMA_TO_DEVICE);
1462 }
1463
1464 return ret;
1465}
1466
Tudor Ambarus5d0429a2014-10-30 18:55:07 +02001467static int rfc4543_setkey(struct crypto_aead *aead,
1468 const u8 *key, unsigned int keylen)
1469{
1470 struct caam_ctx *ctx = crypto_aead_ctx(aead);
1471 struct device *jrdev = ctx->jrdev;
1472 int ret = 0;
1473
1474 if (keylen < 4)
1475 return -EINVAL;
1476
1477#ifdef DEBUG
1478 print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
1479 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
1480#endif
1481
1482 memcpy(ctx->key, key, keylen);
1483
1484 /*
1485 * The last four bytes of the key material are used as the salt value
1486 * in the nonce. Update the AES key length.
1487 */
1488 ctx->enckeylen = keylen - 4;
1489
1490 ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->enckeylen,
1491 DMA_TO_DEVICE);
1492 if (dma_mapping_error(jrdev, ctx->key_dma)) {
1493 dev_err(jrdev, "unable to map key i/o memory\n");
1494 return -ENOMEM;
1495 }
1496
1497 ret = rfc4543_set_sh_desc(aead);
1498 if (ret) {
1499 dma_unmap_single(jrdev, ctx->key_dma, ctx->enckeylen,
1500 DMA_TO_DEVICE);
1501 }
1502
1503 return ret;
1504}
1505
Yuan Kangacdca312011-07-15 11:21:42 +08001506static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
1507 const u8 *key, unsigned int keylen)
1508{
1509 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001510 struct ablkcipher_tfm *crt = &ablkcipher->base.crt_ablkcipher;
1511 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(ablkcipher);
1512 const char *alg_name = crypto_tfm_alg_name(tfm);
Yuan Kangacdca312011-07-15 11:21:42 +08001513 struct device *jrdev = ctx->jrdev;
1514 int ret = 0;
Horia Geanta4464a7d2014-03-14 17:46:49 +02001515 u32 *key_jump_cmd;
Yuan Kangacdca312011-07-15 11:21:42 +08001516 u32 *desc;
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +03001517 u8 *nonce;
Catalin Vasile7222d1a2014-10-31 12:45:38 +02001518 u32 geniv;
Catalin Vasile2b22f6c2014-10-31 12:45:35 +02001519 u32 ctx1_iv_off = 0;
1520 const bool ctr_mode = ((ctx->class1_alg_type & OP_ALG_AAI_MASK) ==
1521 OP_ALG_AAI_CTR_MOD128);
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001522 const bool is_rfc3686 = (ctr_mode &&
1523 (strstr(alg_name, "rfc3686") != NULL));
Yuan Kangacdca312011-07-15 11:21:42 +08001524
1525#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001526 print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001527 DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
1528#endif
Catalin Vasile2b22f6c2014-10-31 12:45:35 +02001529 /*
1530 * AES-CTR needs to load IV in CONTEXT1 reg
1531 * at an offset of 128bits (16bytes)
1532 * CONTEXT1[255:128] = IV
1533 */
1534 if (ctr_mode)
1535 ctx1_iv_off = 16;
Yuan Kangacdca312011-07-15 11:21:42 +08001536
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001537 /*
1538 * RFC3686 specific:
1539 * | CONTEXT1[255:128] = {NONCE, IV, COUNTER}
1540 * | *key = {KEY, NONCE}
1541 */
1542 if (is_rfc3686) {
1543 ctx1_iv_off = 16 + CTR_RFC3686_NONCE_SIZE;
1544 keylen -= CTR_RFC3686_NONCE_SIZE;
1545 }
1546
Yuan Kangacdca312011-07-15 11:21:42 +08001547 memcpy(ctx->key, key, keylen);
1548 ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen,
1549 DMA_TO_DEVICE);
1550 if (dma_mapping_error(jrdev, ctx->key_dma)) {
1551 dev_err(jrdev, "unable to map key i/o memory\n");
1552 return -ENOMEM;
1553 }
1554 ctx->enckeylen = keylen;
1555
1556 /* ablkcipher_encrypt shared descriptor */
1557 desc = ctx->sh_desc_enc;
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001558 init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
Yuan Kangacdca312011-07-15 11:21:42 +08001559 /* Skip if already shared */
1560 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
1561 JUMP_COND_SHRD);
1562
1563 /* Load class1 key only */
1564 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1565 ctx->enckeylen, CLASS_1 |
1566 KEY_DEST_CLASS_REG);
1567
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001568 /* Load nonce into CONTEXT1 reg */
1569 if (is_rfc3686) {
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +03001570 nonce = (u8 *)key + keylen;
1571 append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
1572 LDST_CLASS_IND_CCB |
1573 LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001574 append_move(desc, MOVE_WAITCOMP |
1575 MOVE_SRC_OUTFIFO |
1576 MOVE_DEST_CLASS1CTX |
1577 (16 << MOVE_OFFSET_SHIFT) |
1578 (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
1579 }
1580
Yuan Kangacdca312011-07-15 11:21:42 +08001581 set_jump_tgt_here(desc, key_jump_cmd);
1582
Yuan Kangacdca312011-07-15 11:21:42 +08001583 /* Load iv */
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001584 append_seq_load(desc, crt->ivsize, LDST_SRCDST_BYTE_CONTEXT |
Catalin Vasile2b22f6c2014-10-31 12:45:35 +02001585 LDST_CLASS_1_CCB | (ctx1_iv_off << LDST_OFFSET_SHIFT));
Yuan Kangacdca312011-07-15 11:21:42 +08001586
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001587 /* Load counter into CONTEXT1 reg */
1588 if (is_rfc3686)
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +03001589 append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
1590 LDST_SRCDST_BYTE_CONTEXT |
1591 ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
1592 LDST_OFFSET_SHIFT));
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001593
Yuan Kangacdca312011-07-15 11:21:42 +08001594 /* Load operation */
1595 append_operation(desc, ctx->class1_alg_type |
1596 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
1597
1598 /* Perform operation */
1599 ablkcipher_append_src_dst(desc);
1600
1601 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
1602 desc_bytes(desc),
1603 DMA_TO_DEVICE);
1604 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
1605 dev_err(jrdev, "unable to map shared descriptor\n");
1606 return -ENOMEM;
1607 }
1608#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001609 print_hex_dump(KERN_ERR,
1610 "ablkcipher enc shdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001611 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1612 desc_bytes(desc), 1);
1613#endif
1614 /* ablkcipher_decrypt shared descriptor */
1615 desc = ctx->sh_desc_dec;
1616
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001617 init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
Yuan Kangacdca312011-07-15 11:21:42 +08001618 /* Skip if already shared */
1619 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
1620 JUMP_COND_SHRD);
1621
1622 /* Load class1 key only */
1623 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1624 ctx->enckeylen, CLASS_1 |
1625 KEY_DEST_CLASS_REG);
1626
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001627 /* Load nonce into CONTEXT1 reg */
1628 if (is_rfc3686) {
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +03001629 nonce = (u8 *)key + keylen;
1630 append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
1631 LDST_CLASS_IND_CCB |
1632 LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001633 append_move(desc, MOVE_WAITCOMP |
1634 MOVE_SRC_OUTFIFO |
1635 MOVE_DEST_CLASS1CTX |
1636 (16 << MOVE_OFFSET_SHIFT) |
1637 (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
1638 }
1639
Yuan Kangacdca312011-07-15 11:21:42 +08001640 set_jump_tgt_here(desc, key_jump_cmd);
Yuan Kangacdca312011-07-15 11:21:42 +08001641
1642 /* load IV */
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001643 append_seq_load(desc, crt->ivsize, LDST_SRCDST_BYTE_CONTEXT |
Catalin Vasile2b22f6c2014-10-31 12:45:35 +02001644 LDST_CLASS_1_CCB | (ctx1_iv_off << LDST_OFFSET_SHIFT));
Yuan Kangacdca312011-07-15 11:21:42 +08001645
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001646 /* Load counter into CONTEXT1 reg */
1647 if (is_rfc3686)
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +03001648 append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
1649 LDST_SRCDST_BYTE_CONTEXT |
1650 ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
1651 LDST_OFFSET_SHIFT));
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02001652
Yuan Kangacdca312011-07-15 11:21:42 +08001653 /* Choose operation */
Catalin Vasile2b22f6c2014-10-31 12:45:35 +02001654 if (ctr_mode)
1655 append_operation(desc, ctx->class1_alg_type |
1656 OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT);
1657 else
1658 append_dec_op1(desc, ctx->class1_alg_type);
Yuan Kangacdca312011-07-15 11:21:42 +08001659
1660 /* Perform operation */
1661 ablkcipher_append_src_dst(desc);
1662
Yuan Kangacdca312011-07-15 11:21:42 +08001663 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
1664 desc_bytes(desc),
1665 DMA_TO_DEVICE);
Horia Geanta71c65f72014-07-11 15:34:48 +03001666 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
Yuan Kangacdca312011-07-15 11:21:42 +08001667 dev_err(jrdev, "unable to map shared descriptor\n");
1668 return -ENOMEM;
1669 }
1670
1671#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03001672 print_hex_dump(KERN_ERR,
1673 "ablkcipher dec shdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08001674 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1675 desc_bytes(desc), 1);
1676#endif
Catalin Vasile7222d1a2014-10-31 12:45:38 +02001677 /* ablkcipher_givencrypt shared descriptor */
1678 desc = ctx->sh_desc_givenc;
1679
1680 init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
1681 /* Skip if already shared */
1682 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
1683 JUMP_COND_SHRD);
1684
1685 /* Load class1 key only */
1686 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1687 ctx->enckeylen, CLASS_1 |
1688 KEY_DEST_CLASS_REG);
1689
1690 /* Load Nonce into CONTEXT1 reg */
1691 if (is_rfc3686) {
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +03001692 nonce = (u8 *)key + keylen;
1693 append_load_as_imm(desc, nonce, CTR_RFC3686_NONCE_SIZE,
1694 LDST_CLASS_IND_CCB |
1695 LDST_SRCDST_BYTE_OUTFIFO | LDST_IMM);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02001696 append_move(desc, MOVE_WAITCOMP |
1697 MOVE_SRC_OUTFIFO |
1698 MOVE_DEST_CLASS1CTX |
1699 (16 << MOVE_OFFSET_SHIFT) |
1700 (CTR_RFC3686_NONCE_SIZE << MOVE_LEN_SHIFT));
1701 }
1702 set_jump_tgt_here(desc, key_jump_cmd);
1703
1704 /* Generate IV */
1705 geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
1706 NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
1707 NFIFOENTRY_PTYPE_RND | (crt->ivsize << NFIFOENTRY_DLEN_SHIFT);
1708 append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
1709 LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
1710 append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
1711 append_move(desc, MOVE_WAITCOMP |
1712 MOVE_SRC_INFIFO |
1713 MOVE_DEST_CLASS1CTX |
1714 (crt->ivsize << MOVE_LEN_SHIFT) |
1715 (ctx1_iv_off << MOVE_OFFSET_SHIFT));
1716 append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
1717
1718 /* Copy generated IV to memory */
1719 append_seq_store(desc, crt->ivsize,
1720 LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB |
1721 (ctx1_iv_off << LDST_OFFSET_SHIFT));
1722
1723 /* Load Counter into CONTEXT1 reg */
1724 if (is_rfc3686)
Catalin Vasile5ba1c7b2016-08-31 15:57:55 +03001725 append_load_imm_be32(desc, 1, LDST_IMM | LDST_CLASS_1_CCB |
1726 LDST_SRCDST_BYTE_CONTEXT |
1727 ((ctx1_iv_off + CTR_RFC3686_IV_SIZE) <<
1728 LDST_OFFSET_SHIFT));
Catalin Vasile7222d1a2014-10-31 12:45:38 +02001729
1730 if (ctx1_iv_off)
1731 append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | JUMP_COND_NCP |
1732 (1 << JUMP_OFFSET_SHIFT));
1733
1734 /* Load operation */
1735 append_operation(desc, ctx->class1_alg_type |
1736 OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
1737
1738 /* Perform operation */
1739 ablkcipher_append_src_dst(desc);
1740
1741 ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
1742 desc_bytes(desc),
1743 DMA_TO_DEVICE);
1744 if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
1745 dev_err(jrdev, "unable to map shared descriptor\n");
1746 return -ENOMEM;
1747 }
1748#ifdef DEBUG
1749 print_hex_dump(KERN_ERR,
1750 "ablkcipher givenc shdesc@" __stringify(__LINE__) ": ",
1751 DUMP_PREFIX_ADDRESS, 16, 4, desc,
1752 desc_bytes(desc), 1);
1753#endif
Yuan Kangacdca312011-07-15 11:21:42 +08001754
1755 return ret;
1756}
1757
Catalin Vasilec6415a62015-10-02 13:13:18 +03001758static int xts_ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
1759 const u8 *key, unsigned int keylen)
1760{
1761 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
1762 struct device *jrdev = ctx->jrdev;
1763 u32 *key_jump_cmd, *desc;
1764 __be64 sector_size = cpu_to_be64(512);
1765
1766 if (keylen != 2 * AES_MIN_KEY_SIZE && keylen != 2 * AES_MAX_KEY_SIZE) {
1767 crypto_ablkcipher_set_flags(ablkcipher,
1768 CRYPTO_TFM_RES_BAD_KEY_LEN);
1769 dev_err(jrdev, "key size mismatch\n");
1770 return -EINVAL;
1771 }
1772
1773 memcpy(ctx->key, key, keylen);
1774 ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen, DMA_TO_DEVICE);
1775 if (dma_mapping_error(jrdev, ctx->key_dma)) {
1776 dev_err(jrdev, "unable to map key i/o memory\n");
1777 return -ENOMEM;
1778 }
1779 ctx->enckeylen = keylen;
1780
1781 /* xts_ablkcipher_encrypt shared descriptor */
1782 desc = ctx->sh_desc_enc;
1783 init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
1784 /* Skip if already shared */
1785 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
1786 JUMP_COND_SHRD);
1787
1788 /* Load class1 keys only */
1789 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1790 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
1791
1792 /* Load sector size with index 40 bytes (0x28) */
1793 append_cmd(desc, CMD_LOAD | IMMEDIATE | LDST_SRCDST_BYTE_CONTEXT |
1794 LDST_CLASS_1_CCB | (0x28 << LDST_OFFSET_SHIFT) | 8);
1795 append_data(desc, (void *)&sector_size, 8);
1796
1797 set_jump_tgt_here(desc, key_jump_cmd);
1798
1799 /*
1800 * create sequence for loading the sector index
1801 * Upper 8B of IV - will be used as sector index
1802 * Lower 8B of IV - will be discarded
1803 */
1804 append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
1805 LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT) | 8);
1806 append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP);
1807
1808 /* Load operation */
1809 append_operation(desc, ctx->class1_alg_type | OP_ALG_AS_INITFINAL |
1810 OP_ALG_ENCRYPT);
1811
1812 /* Perform operation */
1813 ablkcipher_append_src_dst(desc);
1814
1815 ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
1816 DMA_TO_DEVICE);
1817 if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
1818 dev_err(jrdev, "unable to map shared descriptor\n");
1819 return -ENOMEM;
1820 }
1821#ifdef DEBUG
1822 print_hex_dump(KERN_ERR,
1823 "xts ablkcipher enc shdesc@" __stringify(__LINE__) ": ",
1824 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1825#endif
1826
1827 /* xts_ablkcipher_decrypt shared descriptor */
1828 desc = ctx->sh_desc_dec;
1829
1830 init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX);
1831 /* Skip if already shared */
1832 key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
1833 JUMP_COND_SHRD);
1834
1835 /* Load class1 key only */
1836 append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
1837 ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
1838
1839 /* Load sector size with index 40 bytes (0x28) */
1840 append_cmd(desc, CMD_LOAD | IMMEDIATE | LDST_SRCDST_BYTE_CONTEXT |
1841 LDST_CLASS_1_CCB | (0x28 << LDST_OFFSET_SHIFT) | 8);
1842 append_data(desc, (void *)&sector_size, 8);
1843
1844 set_jump_tgt_here(desc, key_jump_cmd);
1845
1846 /*
1847 * create sequence for loading the sector index
1848 * Upper 8B of IV - will be used as sector index
1849 * Lower 8B of IV - will be discarded
1850 */
1851 append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
1852 LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT) | 8);
1853 append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP);
1854
1855 /* Load operation */
1856 append_dec_op1(desc, ctx->class1_alg_type);
1857
1858 /* Perform operation */
1859 ablkcipher_append_src_dst(desc);
1860
1861 ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc, desc_bytes(desc),
1862 DMA_TO_DEVICE);
1863 if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
1864 dma_unmap_single(jrdev, ctx->sh_desc_enc_dma,
1865 desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE);
1866 dev_err(jrdev, "unable to map shared descriptor\n");
1867 return -ENOMEM;
1868 }
1869#ifdef DEBUG
1870 print_hex_dump(KERN_ERR,
1871 "xts ablkcipher dec shdesc@" __stringify(__LINE__) ": ",
1872 DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1);
1873#endif
1874
1875 return 0;
1876}
1877
Kim Phillips8e8ec592011-03-13 16:54:26 +08001878/*
Yuan Kang1acebad2011-07-15 11:21:42 +08001879 * aead_edesc - s/w-extended aead descriptor
1880 * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist
Kim Phillips8e8ec592011-03-13 16:54:26 +08001881 * @src_nents: number of segments in input scatterlist
1882 * @dst_nents: number of segments in output scatterlist
Yuan Kang1acebad2011-07-15 11:21:42 +08001883 * @iv_dma: dma address of iv for checking continuity and link table
Kim Phillips8e8ec592011-03-13 16:54:26 +08001884 * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
Yuan Kanga299c832012-06-22 19:48:46 -05001885 * @sec4_sg_bytes: length of dma mapped sec4_sg space
1886 * @sec4_sg_dma: bus physical mapped address of h/w link table
Kim Phillips8e8ec592011-03-13 16:54:26 +08001887 * @hw_desc: the h/w job descriptor followed by any referenced link tables
1888 */
Yuan Kang0e479302011-07-15 11:21:41 +08001889struct aead_edesc {
Kim Phillips8e8ec592011-03-13 16:54:26 +08001890 int assoc_nents;
1891 int src_nents;
1892 int dst_nents;
Yuan Kang1acebad2011-07-15 11:21:42 +08001893 dma_addr_t iv_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05001894 int sec4_sg_bytes;
1895 dma_addr_t sec4_sg_dma;
1896 struct sec4_sg_entry *sec4_sg;
Herbert Xuf2147b82015-06-16 13:54:23 +08001897 u32 hw_desc[];
Kim Phillips8e8ec592011-03-13 16:54:26 +08001898};
1899
Yuan Kangacdca312011-07-15 11:21:42 +08001900/*
1901 * ablkcipher_edesc - s/w-extended ablkcipher descriptor
1902 * @src_nents: number of segments in input scatterlist
1903 * @dst_nents: number of segments in output scatterlist
1904 * @iv_dma: dma address of iv for checking continuity and link table
1905 * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
Yuan Kanga299c832012-06-22 19:48:46 -05001906 * @sec4_sg_bytes: length of dma mapped sec4_sg space
1907 * @sec4_sg_dma: bus physical mapped address of h/w link table
Yuan Kangacdca312011-07-15 11:21:42 +08001908 * @hw_desc: the h/w job descriptor followed by any referenced link tables
1909 */
1910struct ablkcipher_edesc {
1911 int src_nents;
1912 int dst_nents;
1913 dma_addr_t iv_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05001914 int sec4_sg_bytes;
1915 dma_addr_t sec4_sg_dma;
1916 struct sec4_sg_entry *sec4_sg;
Yuan Kangacdca312011-07-15 11:21:42 +08001917 u32 hw_desc[0];
1918};
1919
Yuan Kang1acebad2011-07-15 11:21:42 +08001920static void caam_unmap(struct device *dev, struct scatterlist *src,
Yuan Kang643b39b2012-06-22 19:48:49 -05001921 struct scatterlist *dst, int src_nents,
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02001922 int dst_nents,
Yuan Kanga299c832012-06-22 19:48:46 -05001923 dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
1924 int sec4_sg_bytes)
Kim Phillips8e8ec592011-03-13 16:54:26 +08001925{
Yuan Kang643b39b2012-06-22 19:48:49 -05001926 if (dst != src) {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02001927 dma_unmap_sg(dev, src, src_nents ? : 1, DMA_TO_DEVICE);
1928 dma_unmap_sg(dev, dst, dst_nents ? : 1, DMA_FROM_DEVICE);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001929 } else {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02001930 dma_unmap_sg(dev, src, src_nents ? : 1, DMA_BIDIRECTIONAL);
Kim Phillips8e8ec592011-03-13 16:54:26 +08001931 }
1932
Yuan Kang1acebad2011-07-15 11:21:42 +08001933 if (iv_dma)
1934 dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
Yuan Kanga299c832012-06-22 19:48:46 -05001935 if (sec4_sg_bytes)
1936 dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001937 DMA_TO_DEVICE);
1938}
1939
Yuan Kang1acebad2011-07-15 11:21:42 +08001940static void aead_unmap(struct device *dev,
1941 struct aead_edesc *edesc,
1942 struct aead_request *req)
1943{
Herbert Xuf2147b82015-06-16 13:54:23 +08001944 caam_unmap(dev, req->src, req->dst,
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02001945 edesc->src_nents, edesc->dst_nents, 0, 0,
Herbert Xuf2147b82015-06-16 13:54:23 +08001946 edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
1947}
1948
Yuan Kangacdca312011-07-15 11:21:42 +08001949static void ablkcipher_unmap(struct device *dev,
1950 struct ablkcipher_edesc *edesc,
1951 struct ablkcipher_request *req)
1952{
1953 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
1954 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
1955
1956 caam_unmap(dev, req->src, req->dst,
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02001957 edesc->src_nents, edesc->dst_nents,
1958 edesc->iv_dma, ivsize,
Yuan Kang643b39b2012-06-22 19:48:49 -05001959 edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
Yuan Kangacdca312011-07-15 11:21:42 +08001960}
1961
Yuan Kang0e479302011-07-15 11:21:41 +08001962static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001963 void *context)
1964{
Yuan Kang0e479302011-07-15 11:21:41 +08001965 struct aead_request *req = context;
1966 struct aead_edesc *edesc;
Herbert Xuf2147b82015-06-16 13:54:23 +08001967
1968#ifdef DEBUG
1969 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
1970#endif
1971
1972 edesc = container_of(desc, struct aead_edesc, hw_desc[0]);
1973
1974 if (err)
1975 caam_jr_strstatus(jrdev, err);
1976
1977 aead_unmap(jrdev, edesc, req);
1978
1979 kfree(edesc);
1980
1981 aead_request_complete(req, err);
1982}
1983
Yuan Kang0e479302011-07-15 11:21:41 +08001984static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
Kim Phillips8e8ec592011-03-13 16:54:26 +08001985 void *context)
1986{
Yuan Kang0e479302011-07-15 11:21:41 +08001987 struct aead_request *req = context;
1988 struct aead_edesc *edesc;
Herbert Xuf2147b82015-06-16 13:54:23 +08001989
1990#ifdef DEBUG
1991 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
1992#endif
1993
1994 edesc = container_of(desc, struct aead_edesc, hw_desc[0]);
1995
1996 if (err)
1997 caam_jr_strstatus(jrdev, err);
1998
1999 aead_unmap(jrdev, edesc, req);
2000
2001 /*
2002 * verify hw auth check passed else return -EBADMSG
2003 */
2004 if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
2005 err = -EBADMSG;
2006
2007 kfree(edesc);
2008
2009 aead_request_complete(req, err);
2010}
2011
Yuan Kangacdca312011-07-15 11:21:42 +08002012static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
2013 void *context)
2014{
2015 struct ablkcipher_request *req = context;
2016 struct ablkcipher_edesc *edesc;
2017#ifdef DEBUG
2018 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2019 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
2020
2021 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
2022#endif
2023
2024 edesc = (struct ablkcipher_edesc *)((char *)desc -
2025 offsetof(struct ablkcipher_edesc, hw_desc));
2026
Marek Vasutfa9659c2014-04-24 20:05:12 +02002027 if (err)
2028 caam_jr_strstatus(jrdev, err);
Yuan Kangacdca312011-07-15 11:21:42 +08002029
2030#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03002031 print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08002032 DUMP_PREFIX_ADDRESS, 16, 4, req->info,
2033 edesc->src_nents > 1 ? 100 : ivsize, 1);
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +03002034 dbg_dump_sg(KERN_ERR, "dst @"__stringify(__LINE__)": ",
2035 DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
2036 edesc->dst_nents > 1 ? 100 : req->nbytes, 1, true);
Yuan Kangacdca312011-07-15 11:21:42 +08002037#endif
2038
2039 ablkcipher_unmap(jrdev, edesc, req);
2040 kfree(edesc);
2041
2042 ablkcipher_request_complete(req, err);
2043}
2044
2045static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
2046 void *context)
2047{
2048 struct ablkcipher_request *req = context;
2049 struct ablkcipher_edesc *edesc;
2050#ifdef DEBUG
2051 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2052 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
2053
2054 dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
2055#endif
2056
2057 edesc = (struct ablkcipher_edesc *)((char *)desc -
2058 offsetof(struct ablkcipher_edesc, hw_desc));
Marek Vasutfa9659c2014-04-24 20:05:12 +02002059 if (err)
2060 caam_jr_strstatus(jrdev, err);
Yuan Kangacdca312011-07-15 11:21:42 +08002061
2062#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03002063 print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08002064 DUMP_PREFIX_ADDRESS, 16, 4, req->info,
2065 ivsize, 1);
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +03002066 dbg_dump_sg(KERN_ERR, "dst @"__stringify(__LINE__)": ",
2067 DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
2068 edesc->dst_nents > 1 ? 100 : req->nbytes, 1, true);
Yuan Kangacdca312011-07-15 11:21:42 +08002069#endif
2070
2071 ablkcipher_unmap(jrdev, edesc, req);
2072 kfree(edesc);
2073
2074 ablkcipher_request_complete(req, err);
2075}
2076
Kim Phillips8e8ec592011-03-13 16:54:26 +08002077/*
Yuan Kang1acebad2011-07-15 11:21:42 +08002078 * Fill in aead job descriptor
Kim Phillips8e8ec592011-03-13 16:54:26 +08002079 */
Herbert Xuf2147b82015-06-16 13:54:23 +08002080static void init_aead_job(struct aead_request *req,
2081 struct aead_edesc *edesc,
2082 bool all_contig, bool encrypt)
2083{
2084 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2085 struct caam_ctx *ctx = crypto_aead_ctx(aead);
2086 int authsize = ctx->authsize;
2087 u32 *desc = edesc->hw_desc;
2088 u32 out_options, in_options;
2089 dma_addr_t dst_dma, src_dma;
2090 int len, sec4_sg_index = 0;
2091 dma_addr_t ptr;
2092 u32 *sh_desc;
2093
2094 sh_desc = encrypt ? ctx->sh_desc_enc : ctx->sh_desc_dec;
2095 ptr = encrypt ? ctx->sh_desc_enc_dma : ctx->sh_desc_dec_dma;
2096
2097 len = desc_len(sh_desc);
2098 init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
2099
2100 if (all_contig) {
2101 src_dma = sg_dma_address(req->src);
2102 in_options = 0;
2103 } else {
2104 src_dma = edesc->sec4_sg_dma;
2105 sec4_sg_index += edesc->src_nents;
2106 in_options = LDST_SGF;
2107 }
2108
2109 append_seq_in_ptr(desc, src_dma, req->assoclen + req->cryptlen,
2110 in_options);
2111
2112 dst_dma = src_dma;
2113 out_options = in_options;
2114
2115 if (unlikely(req->src != req->dst)) {
2116 if (!edesc->dst_nents) {
2117 dst_dma = sg_dma_address(req->dst);
2118 } else {
2119 dst_dma = edesc->sec4_sg_dma +
2120 sec4_sg_index *
2121 sizeof(struct sec4_sg_entry);
2122 out_options = LDST_SGF;
2123 }
2124 }
2125
2126 if (encrypt)
2127 append_seq_out_ptr(desc, dst_dma,
2128 req->assoclen + req->cryptlen + authsize,
2129 out_options);
2130 else
2131 append_seq_out_ptr(desc, dst_dma,
2132 req->assoclen + req->cryptlen - authsize,
2133 out_options);
2134
2135 /* REG3 = assoclen */
2136 append_math_add_imm_u32(desc, REG3, ZERO, IMM, req->assoclen);
2137}
2138
2139static void init_gcm_job(struct aead_request *req,
2140 struct aead_edesc *edesc,
2141 bool all_contig, bool encrypt)
2142{
2143 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2144 struct caam_ctx *ctx = crypto_aead_ctx(aead);
2145 unsigned int ivsize = crypto_aead_ivsize(aead);
2146 u32 *desc = edesc->hw_desc;
2147 bool generic_gcm = (ivsize == 12);
2148 unsigned int last;
2149
2150 init_aead_job(req, edesc, all_contig, encrypt);
2151
2152 /* BUG This should not be specific to generic GCM. */
2153 last = 0;
2154 if (encrypt && generic_gcm && !(req->assoclen + req->cryptlen))
2155 last = FIFOLD_TYPE_LAST1;
2156
2157 /* Read GCM IV */
2158 append_cmd(desc, CMD_FIFO_LOAD | FIFOLD_CLASS_CLASS1 | IMMEDIATE |
2159 FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | 12 | last);
2160 /* Append Salt */
2161 if (!generic_gcm)
2162 append_data(desc, ctx->key + ctx->enckeylen, 4);
2163 /* Append IV */
2164 append_data(desc, req->iv, ivsize);
2165 /* End of blank commands */
2166}
2167
Herbert Xu479bcc72015-07-30 17:53:17 +08002168static void init_authenc_job(struct aead_request *req,
2169 struct aead_edesc *edesc,
2170 bool all_contig, bool encrypt)
Yuan Kang1acebad2011-07-15 11:21:42 +08002171{
2172 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xu479bcc72015-07-30 17:53:17 +08002173 struct caam_aead_alg *alg = container_of(crypto_aead_alg(aead),
2174 struct caam_aead_alg, aead);
2175 unsigned int ivsize = crypto_aead_ivsize(aead);
Yuan Kang1acebad2011-07-15 11:21:42 +08002176 struct caam_ctx *ctx = crypto_aead_ctx(aead);
Herbert Xu479bcc72015-07-30 17:53:17 +08002177 const bool ctr_mode = ((ctx->class1_alg_type & OP_ALG_AAI_MASK) ==
2178 OP_ALG_AAI_CTR_MOD128);
2179 const bool is_rfc3686 = alg->caam.rfc3686;
Yuan Kang1acebad2011-07-15 11:21:42 +08002180 u32 *desc = edesc->hw_desc;
Herbert Xu479bcc72015-07-30 17:53:17 +08002181 u32 ivoffset = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002182
Herbert Xu479bcc72015-07-30 17:53:17 +08002183 /*
2184 * AES-CTR needs to load IV in CONTEXT1 reg
2185 * at an offset of 128bits (16bytes)
2186 * CONTEXT1[255:128] = IV
2187 */
2188 if (ctr_mode)
2189 ivoffset = 16;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002190
Herbert Xu479bcc72015-07-30 17:53:17 +08002191 /*
2192 * RFC3686 specific:
2193 * CONTEXT1[255:128] = {NONCE, IV, COUNTER}
2194 */
2195 if (is_rfc3686)
2196 ivoffset = 16 + CTR_RFC3686_NONCE_SIZE;
Tudor Ambarusbac68f22014-10-23 16:14:03 +03002197
Herbert Xu479bcc72015-07-30 17:53:17 +08002198 init_aead_job(req, edesc, all_contig, encrypt);
Yuan Kang1acebad2011-07-15 11:21:42 +08002199
Horia Geantă8b18e232016-08-29 14:52:14 +03002200 if (ivsize && ((is_rfc3686 && encrypt) || !alg->caam.geniv))
Herbert Xu479bcc72015-07-30 17:53:17 +08002201 append_load_as_imm(desc, req->iv, ivsize,
2202 LDST_CLASS_1_CCB |
2203 LDST_SRCDST_BYTE_CONTEXT |
2204 (ivoffset << LDST_OFFSET_SHIFT));
Kim Phillips8e8ec592011-03-13 16:54:26 +08002205}
2206
2207/*
Yuan Kangacdca312011-07-15 11:21:42 +08002208 * Fill in ablkcipher job descriptor
2209 */
2210static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr,
2211 struct ablkcipher_edesc *edesc,
2212 struct ablkcipher_request *req,
2213 bool iv_contig)
2214{
2215 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2216 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
2217 u32 *desc = edesc->hw_desc;
2218 u32 out_options = 0, in_options;
2219 dma_addr_t dst_dma, src_dma;
Yuan Kanga299c832012-06-22 19:48:46 -05002220 int len, sec4_sg_index = 0;
Yuan Kangacdca312011-07-15 11:21:42 +08002221
2222#ifdef DEBUG
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +03002223 bool may_sleep = ((req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2224 CRYPTO_TFM_REQ_MAY_SLEEP)) != 0);
Alex Porosanu514df282013-08-14 18:56:45 +03002225 print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08002226 DUMP_PREFIX_ADDRESS, 16, 4, req->info,
2227 ivsize, 1);
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +03002228 printk(KERN_ERR "asked=%d, nbytes%d\n", (int)edesc->src_nents ? 100 : req->nbytes, req->nbytes);
2229 dbg_dump_sg(KERN_ERR, "src @"__stringify(__LINE__)": ",
2230 DUMP_PREFIX_ADDRESS, 16, 4, req->src,
2231 edesc->src_nents ? 100 : req->nbytes, 1, may_sleep);
Yuan Kangacdca312011-07-15 11:21:42 +08002232#endif
2233
2234 len = desc_len(sh_desc);
2235 init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
2236
2237 if (iv_contig) {
2238 src_dma = edesc->iv_dma;
2239 in_options = 0;
2240 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05002241 src_dma = edesc->sec4_sg_dma;
Cristian Stoica35b82e52015-01-21 11:53:30 +02002242 sec4_sg_index += edesc->src_nents + 1;
Yuan Kangacdca312011-07-15 11:21:42 +08002243 in_options = LDST_SGF;
2244 }
2245 append_seq_in_ptr(desc, src_dma, req->nbytes + ivsize, in_options);
2246
2247 if (likely(req->src == req->dst)) {
2248 if (!edesc->src_nents && iv_contig) {
2249 dst_dma = sg_dma_address(req->src);
2250 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05002251 dst_dma = edesc->sec4_sg_dma +
2252 sizeof(struct sec4_sg_entry);
Yuan Kangacdca312011-07-15 11:21:42 +08002253 out_options = LDST_SGF;
2254 }
2255 } else {
2256 if (!edesc->dst_nents) {
2257 dst_dma = sg_dma_address(req->dst);
2258 } else {
Yuan Kanga299c832012-06-22 19:48:46 -05002259 dst_dma = edesc->sec4_sg_dma +
2260 sec4_sg_index * sizeof(struct sec4_sg_entry);
Yuan Kangacdca312011-07-15 11:21:42 +08002261 out_options = LDST_SGF;
2262 }
2263 }
2264 append_seq_out_ptr(desc, dst_dma, req->nbytes, out_options);
2265}
2266
2267/*
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002268 * Fill in ablkcipher givencrypt job descriptor
2269 */
2270static void init_ablkcipher_giv_job(u32 *sh_desc, dma_addr_t ptr,
2271 struct ablkcipher_edesc *edesc,
2272 struct ablkcipher_request *req,
2273 bool iv_contig)
2274{
2275 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2276 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
2277 u32 *desc = edesc->hw_desc;
2278 u32 out_options, in_options;
2279 dma_addr_t dst_dma, src_dma;
2280 int len, sec4_sg_index = 0;
2281
2282#ifdef DEBUG
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +03002283 bool may_sleep = ((req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2284 CRYPTO_TFM_REQ_MAY_SLEEP)) != 0);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002285 print_hex_dump(KERN_ERR, "presciv@" __stringify(__LINE__) ": ",
2286 DUMP_PREFIX_ADDRESS, 16, 4, req->info,
2287 ivsize, 1);
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +03002288 dbg_dump_sg(KERN_ERR, "src @" __stringify(__LINE__) ": ",
2289 DUMP_PREFIX_ADDRESS, 16, 4, req->src,
2290 edesc->src_nents ? 100 : req->nbytes, 1, may_sleep);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002291#endif
2292
2293 len = desc_len(sh_desc);
2294 init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
2295
2296 if (!edesc->src_nents) {
2297 src_dma = sg_dma_address(req->src);
2298 in_options = 0;
2299 } else {
2300 src_dma = edesc->sec4_sg_dma;
2301 sec4_sg_index += edesc->src_nents;
2302 in_options = LDST_SGF;
2303 }
2304 append_seq_in_ptr(desc, src_dma, req->nbytes, in_options);
2305
2306 if (iv_contig) {
2307 dst_dma = edesc->iv_dma;
2308 out_options = 0;
2309 } else {
2310 dst_dma = edesc->sec4_sg_dma +
2311 sec4_sg_index * sizeof(struct sec4_sg_entry);
2312 out_options = LDST_SGF;
2313 }
2314 append_seq_out_ptr(desc, dst_dma, req->nbytes + ivsize, out_options);
2315}
2316
2317/*
Yuan Kang1acebad2011-07-15 11:21:42 +08002318 * allocate and map the aead extended descriptor
Kim Phillips8e8ec592011-03-13 16:54:26 +08002319 */
Herbert Xuf2147b82015-06-16 13:54:23 +08002320static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
2321 int desc_bytes, bool *all_contig_ptr,
2322 bool encrypt)
2323{
2324 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2325 struct caam_ctx *ctx = crypto_aead_ctx(aead);
2326 struct device *jrdev = ctx->jrdev;
2327 gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2328 CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
2329 int src_nents, dst_nents = 0;
2330 struct aead_edesc *edesc;
2331 int sgc;
2332 bool all_contig = true;
Herbert Xuf2147b82015-06-16 13:54:23 +08002333 int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
2334 unsigned int authsize = ctx->authsize;
2335
2336 if (unlikely(req->dst != req->src)) {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002337 src_nents = sg_count(req->src, req->assoclen + req->cryptlen);
Herbert Xuf2147b82015-06-16 13:54:23 +08002338 dst_nents = sg_count(req->dst,
2339 req->assoclen + req->cryptlen +
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002340 (encrypt ? authsize : (-authsize)));
Herbert Xuf2147b82015-06-16 13:54:23 +08002341 } else {
2342 src_nents = sg_count(req->src,
2343 req->assoclen + req->cryptlen +
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002344 (encrypt ? authsize : 0));
Herbert Xuf2147b82015-06-16 13:54:23 +08002345 }
2346
2347 /* Check if data are contiguous. */
2348 all_contig = !src_nents;
Horia Geantăc530e342016-11-09 10:46:15 +02002349 if (!all_contig)
Herbert Xuf2147b82015-06-16 13:54:23 +08002350 sec4_sg_len = src_nents;
Herbert Xuf2147b82015-06-16 13:54:23 +08002351
2352 sec4_sg_len += dst_nents;
2353
2354 sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
2355
2356 /* allocate space for base edesc and hw desc commands, link tables */
Victoria Milhoandde20ae2015-08-05 11:28:39 -07002357 edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
2358 GFP_DMA | flags);
Herbert Xuf2147b82015-06-16 13:54:23 +08002359 if (!edesc) {
2360 dev_err(jrdev, "could not allocate extended descriptor\n");
2361 return ERR_PTR(-ENOMEM);
2362 }
2363
2364 if (likely(req->src == req->dst)) {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002365 sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
2366 DMA_BIDIRECTIONAL);
Herbert Xuf2147b82015-06-16 13:54:23 +08002367 if (unlikely(!sgc)) {
2368 dev_err(jrdev, "unable to map source\n");
2369 kfree(edesc);
2370 return ERR_PTR(-ENOMEM);
2371 }
2372 } else {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002373 sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
2374 DMA_TO_DEVICE);
Herbert Xuf2147b82015-06-16 13:54:23 +08002375 if (unlikely(!sgc)) {
2376 dev_err(jrdev, "unable to map source\n");
2377 kfree(edesc);
2378 return ERR_PTR(-ENOMEM);
2379 }
2380
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002381 sgc = dma_map_sg(jrdev, req->dst, dst_nents ? : 1,
2382 DMA_FROM_DEVICE);
Herbert Xuf2147b82015-06-16 13:54:23 +08002383 if (unlikely(!sgc)) {
2384 dev_err(jrdev, "unable to map destination\n");
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002385 dma_unmap_sg(jrdev, req->src, src_nents ? : 1,
2386 DMA_TO_DEVICE);
Herbert Xuf2147b82015-06-16 13:54:23 +08002387 kfree(edesc);
2388 return ERR_PTR(-ENOMEM);
2389 }
2390 }
2391
2392 edesc->src_nents = src_nents;
Herbert Xuf2147b82015-06-16 13:54:23 +08002393 edesc->dst_nents = dst_nents;
Herbert Xuf2147b82015-06-16 13:54:23 +08002394 edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
2395 desc_bytes;
2396 *all_contig_ptr = all_contig;
2397
2398 sec4_sg_index = 0;
2399 if (!all_contig) {
Herbert Xu7793bda2015-06-18 14:25:56 +08002400 sg_to_sec4_sg_last(req->src, src_nents,
Herbert Xuf2147b82015-06-16 13:54:23 +08002401 edesc->sec4_sg + sec4_sg_index, 0);
2402 sec4_sg_index += src_nents;
2403 }
2404 if (dst_nents) {
2405 sg_to_sec4_sg_last(req->dst, dst_nents,
2406 edesc->sec4_sg + sec4_sg_index, 0);
2407 }
2408
2409 if (!sec4_sg_bytes)
2410 return edesc;
2411
2412 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
2413 sec4_sg_bytes, DMA_TO_DEVICE);
2414 if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
2415 dev_err(jrdev, "unable to map S/G table\n");
2416 aead_unmap(jrdev, edesc, req);
2417 kfree(edesc);
2418 return ERR_PTR(-ENOMEM);
2419 }
2420
2421 edesc->sec4_sg_bytes = sec4_sg_bytes;
2422
2423 return edesc;
2424}
2425
2426static int gcm_encrypt(struct aead_request *req)
Kim Phillips8e8ec592011-03-13 16:54:26 +08002427{
Yuan Kang0e479302011-07-15 11:21:41 +08002428 struct aead_edesc *edesc;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002429 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002430 struct caam_ctx *ctx = crypto_aead_ctx(aead);
2431 struct device *jrdev = ctx->jrdev;
Yuan Kang1acebad2011-07-15 11:21:42 +08002432 bool all_contig;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002433 u32 *desc;
Yuan Kang1acebad2011-07-15 11:21:42 +08002434 int ret = 0;
2435
Kim Phillips8e8ec592011-03-13 16:54:26 +08002436 /* allocate extended descriptor */
Herbert Xuf2147b82015-06-16 13:54:23 +08002437 edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, true);
Kim Phillips8e8ec592011-03-13 16:54:26 +08002438 if (IS_ERR(edesc))
2439 return PTR_ERR(edesc);
2440
Yuan Kang1acebad2011-07-15 11:21:42 +08002441 /* Create and submit job descriptor */
Herbert Xuf2147b82015-06-16 13:54:23 +08002442 init_gcm_job(req, edesc, all_contig, true);
Yuan Kang1acebad2011-07-15 11:21:42 +08002443#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03002444 print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad2011-07-15 11:21:42 +08002445 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
2446 desc_bytes(edesc->hw_desc), 1);
2447#endif
2448
Kim Phillips8e8ec592011-03-13 16:54:26 +08002449 desc = edesc->hw_desc;
Yuan Kang1acebad2011-07-15 11:21:42 +08002450 ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
2451 if (!ret) {
2452 ret = -EINPROGRESS;
2453 } else {
2454 aead_unmap(jrdev, edesc, req);
2455 kfree(edesc);
2456 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08002457
Yuan Kang1acebad2011-07-15 11:21:42 +08002458 return ret;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002459}
2460
Herbert Xu46218752015-07-09 07:17:33 +08002461static int ipsec_gcm_encrypt(struct aead_request *req)
2462{
2463 if (req->assoclen < 8)
2464 return -EINVAL;
2465
2466 return gcm_encrypt(req);
2467}
2468
Herbert Xu479bcc72015-07-30 17:53:17 +08002469static int aead_encrypt(struct aead_request *req)
Kim Phillips8e8ec592011-03-13 16:54:26 +08002470{
Yuan Kang1acebad2011-07-15 11:21:42 +08002471 struct aead_edesc *edesc;
Yuan Kang0e479302011-07-15 11:21:41 +08002472 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Yuan Kang0e479302011-07-15 11:21:41 +08002473 struct caam_ctx *ctx = crypto_aead_ctx(aead);
2474 struct device *jrdev = ctx->jrdev;
Yuan Kang1acebad2011-07-15 11:21:42 +08002475 bool all_contig;
Yuan Kang0e479302011-07-15 11:21:41 +08002476 u32 *desc;
Yuan Kang1acebad2011-07-15 11:21:42 +08002477 int ret = 0;
Yuan Kang0e479302011-07-15 11:21:41 +08002478
2479 /* allocate extended descriptor */
Herbert Xu479bcc72015-07-30 17:53:17 +08002480 edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
2481 &all_contig, true);
Yuan Kang0e479302011-07-15 11:21:41 +08002482 if (IS_ERR(edesc))
2483 return PTR_ERR(edesc);
2484
Herbert Xuf2147b82015-06-16 13:54:23 +08002485 /* Create and submit job descriptor */
Herbert Xu479bcc72015-07-30 17:53:17 +08002486 init_authenc_job(req, edesc, all_contig, true);
Yuan Kang1acebad2011-07-15 11:21:42 +08002487#ifdef DEBUG
Herbert Xuf2147b82015-06-16 13:54:23 +08002488 print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
2489 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
2490 desc_bytes(edesc->hw_desc), 1);
Yuan Kang1acebad2011-07-15 11:21:42 +08002491#endif
2492
Herbert Xuf2147b82015-06-16 13:54:23 +08002493 desc = edesc->hw_desc;
Herbert Xu479bcc72015-07-30 17:53:17 +08002494 ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
Herbert Xuf2147b82015-06-16 13:54:23 +08002495 if (!ret) {
2496 ret = -EINPROGRESS;
2497 } else {
Herbert Xu479bcc72015-07-30 17:53:17 +08002498 aead_unmap(jrdev, edesc, req);
Herbert Xuf2147b82015-06-16 13:54:23 +08002499 kfree(edesc);
2500 }
2501
2502 return ret;
2503}
2504
2505static int gcm_decrypt(struct aead_request *req)
2506{
2507 struct aead_edesc *edesc;
2508 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2509 struct caam_ctx *ctx = crypto_aead_ctx(aead);
2510 struct device *jrdev = ctx->jrdev;
2511 bool all_contig;
2512 u32 *desc;
2513 int ret = 0;
2514
2515 /* allocate extended descriptor */
2516 edesc = aead_edesc_alloc(req, GCM_DESC_JOB_IO_LEN, &all_contig, false);
2517 if (IS_ERR(edesc))
2518 return PTR_ERR(edesc);
2519
Yuan Kang1acebad2011-07-15 11:21:42 +08002520 /* Create and submit job descriptor*/
Herbert Xuf2147b82015-06-16 13:54:23 +08002521 init_gcm_job(req, edesc, all_contig, false);
Yuan Kang1acebad2011-07-15 11:21:42 +08002522#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03002523 print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
Yuan Kang1acebad2011-07-15 11:21:42 +08002524 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
2525 desc_bytes(edesc->hw_desc), 1);
2526#endif
2527
Yuan Kang0e479302011-07-15 11:21:41 +08002528 desc = edesc->hw_desc;
Yuan Kang1acebad2011-07-15 11:21:42 +08002529 ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
2530 if (!ret) {
2531 ret = -EINPROGRESS;
2532 } else {
2533 aead_unmap(jrdev, edesc, req);
2534 kfree(edesc);
2535 }
Yuan Kang0e479302011-07-15 11:21:41 +08002536
Yuan Kang1acebad2011-07-15 11:21:42 +08002537 return ret;
2538}
Yuan Kang0e479302011-07-15 11:21:41 +08002539
Herbert Xu46218752015-07-09 07:17:33 +08002540static int ipsec_gcm_decrypt(struct aead_request *req)
2541{
2542 if (req->assoclen < 8)
2543 return -EINVAL;
2544
2545 return gcm_decrypt(req);
2546}
2547
Herbert Xu479bcc72015-07-30 17:53:17 +08002548static int aead_decrypt(struct aead_request *req)
Herbert Xuf2147b82015-06-16 13:54:23 +08002549{
2550 struct aead_edesc *edesc;
2551 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2552 struct caam_ctx *ctx = crypto_aead_ctx(aead);
2553 struct device *jrdev = ctx->jrdev;
2554 bool all_contig;
2555 u32 *desc;
2556 int ret = 0;
2557
Catalin Vasile5ecf8ef2016-09-22 11:57:58 +03002558#ifdef DEBUG
2559 bool may_sleep = ((req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2560 CRYPTO_TFM_REQ_MAY_SLEEP)) != 0);
2561 dbg_dump_sg(KERN_ERR, "dec src@"__stringify(__LINE__)": ",
2562 DUMP_PREFIX_ADDRESS, 16, 4, req->src,
2563 req->assoclen + req->cryptlen, 1, may_sleep);
2564#endif
2565
Herbert Xuf2147b82015-06-16 13:54:23 +08002566 /* allocate extended descriptor */
Herbert Xu479bcc72015-07-30 17:53:17 +08002567 edesc = aead_edesc_alloc(req, AUTHENC_DESC_JOB_IO_LEN,
2568 &all_contig, false);
Herbert Xuf2147b82015-06-16 13:54:23 +08002569 if (IS_ERR(edesc))
2570 return PTR_ERR(edesc);
2571
Herbert Xuf2147b82015-06-16 13:54:23 +08002572 /* Create and submit job descriptor*/
Herbert Xu479bcc72015-07-30 17:53:17 +08002573 init_authenc_job(req, edesc, all_contig, false);
Herbert Xuf2147b82015-06-16 13:54:23 +08002574#ifdef DEBUG
2575 print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
2576 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
2577 desc_bytes(edesc->hw_desc), 1);
2578#endif
2579
2580 desc = edesc->hw_desc;
Herbert Xu479bcc72015-07-30 17:53:17 +08002581 ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
Herbert Xuf2147b82015-06-16 13:54:23 +08002582 if (!ret) {
2583 ret = -EINPROGRESS;
2584 } else {
Herbert Xu479bcc72015-07-30 17:53:17 +08002585 aead_unmap(jrdev, edesc, req);
Herbert Xuf2147b82015-06-16 13:54:23 +08002586 kfree(edesc);
2587 }
2588
2589 return ret;
2590}
2591
Yuan Kangacdca312011-07-15 11:21:42 +08002592/*
2593 * allocate and map the ablkcipher extended descriptor for ablkcipher
2594 */
2595static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
2596 *req, int desc_bytes,
2597 bool *iv_contig_out)
2598{
2599 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2600 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
2601 struct device *jrdev = ctx->jrdev;
2602 gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2603 CRYPTO_TFM_REQ_MAY_SLEEP)) ?
2604 GFP_KERNEL : GFP_ATOMIC;
Yuan Kanga299c832012-06-22 19:48:46 -05002605 int src_nents, dst_nents = 0, sec4_sg_bytes;
Yuan Kangacdca312011-07-15 11:21:42 +08002606 struct ablkcipher_edesc *edesc;
2607 dma_addr_t iv_dma = 0;
2608 bool iv_contig = false;
2609 int sgc;
2610 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
Yuan Kanga299c832012-06-22 19:48:46 -05002611 int sec4_sg_index;
Yuan Kangacdca312011-07-15 11:21:42 +08002612
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002613 src_nents = sg_count(req->src, req->nbytes);
Yuan Kangacdca312011-07-15 11:21:42 +08002614
Yuan Kang643b39b2012-06-22 19:48:49 -05002615 if (req->dst != req->src)
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002616 dst_nents = sg_count(req->dst, req->nbytes);
Yuan Kangacdca312011-07-15 11:21:42 +08002617
2618 if (likely(req->src == req->dst)) {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002619 sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
2620 DMA_BIDIRECTIONAL);
Yuan Kangacdca312011-07-15 11:21:42 +08002621 } else {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002622 sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
2623 DMA_TO_DEVICE);
2624 sgc = dma_map_sg(jrdev, req->dst, dst_nents ? : 1,
2625 DMA_FROM_DEVICE);
Yuan Kangacdca312011-07-15 11:21:42 +08002626 }
2627
Horia Geantace572082014-07-11 15:34:49 +03002628 iv_dma = dma_map_single(jrdev, req->info, ivsize, DMA_TO_DEVICE);
2629 if (dma_mapping_error(jrdev, iv_dma)) {
2630 dev_err(jrdev, "unable to map IV\n");
2631 return ERR_PTR(-ENOMEM);
2632 }
2633
Yuan Kangacdca312011-07-15 11:21:42 +08002634 /*
2635 * Check if iv can be contiguous with source and destination.
2636 * If so, include it. If not, create scatterlist.
2637 */
Yuan Kangacdca312011-07-15 11:21:42 +08002638 if (!src_nents && iv_dma + ivsize == sg_dma_address(req->src))
2639 iv_contig = true;
2640 else
2641 src_nents = src_nents ? : 1;
Yuan Kanga299c832012-06-22 19:48:46 -05002642 sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) *
2643 sizeof(struct sec4_sg_entry);
Yuan Kangacdca312011-07-15 11:21:42 +08002644
2645 /* allocate space for base edesc and hw desc commands, link tables */
Victoria Milhoandde20ae2015-08-05 11:28:39 -07002646 edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
2647 GFP_DMA | flags);
Yuan Kangacdca312011-07-15 11:21:42 +08002648 if (!edesc) {
2649 dev_err(jrdev, "could not allocate extended descriptor\n");
2650 return ERR_PTR(-ENOMEM);
2651 }
2652
2653 edesc->src_nents = src_nents;
2654 edesc->dst_nents = dst_nents;
Yuan Kanga299c832012-06-22 19:48:46 -05002655 edesc->sec4_sg_bytes = sec4_sg_bytes;
2656 edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
2657 desc_bytes;
Yuan Kangacdca312011-07-15 11:21:42 +08002658
Yuan Kanga299c832012-06-22 19:48:46 -05002659 sec4_sg_index = 0;
Yuan Kangacdca312011-07-15 11:21:42 +08002660 if (!iv_contig) {
Yuan Kanga299c832012-06-22 19:48:46 -05002661 dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
2662 sg_to_sec4_sg_last(req->src, src_nents,
2663 edesc->sec4_sg + 1, 0);
2664 sec4_sg_index += 1 + src_nents;
Yuan Kangacdca312011-07-15 11:21:42 +08002665 }
2666
Yuan Kang643b39b2012-06-22 19:48:49 -05002667 if (dst_nents) {
Yuan Kanga299c832012-06-22 19:48:46 -05002668 sg_to_sec4_sg_last(req->dst, dst_nents,
2669 edesc->sec4_sg + sec4_sg_index, 0);
Yuan Kangacdca312011-07-15 11:21:42 +08002670 }
2671
Yuan Kanga299c832012-06-22 19:48:46 -05002672 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
2673 sec4_sg_bytes, DMA_TO_DEVICE);
Horia Geantace572082014-07-11 15:34:49 +03002674 if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
2675 dev_err(jrdev, "unable to map S/G table\n");
2676 return ERR_PTR(-ENOMEM);
2677 }
2678
Yuan Kangacdca312011-07-15 11:21:42 +08002679 edesc->iv_dma = iv_dma;
2680
2681#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03002682 print_hex_dump(KERN_ERR, "ablkcipher sec4_sg@"__stringify(__LINE__)": ",
Yuan Kanga299c832012-06-22 19:48:46 -05002683 DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
2684 sec4_sg_bytes, 1);
Yuan Kangacdca312011-07-15 11:21:42 +08002685#endif
2686
2687 *iv_contig_out = iv_contig;
2688 return edesc;
2689}
2690
2691static int ablkcipher_encrypt(struct ablkcipher_request *req)
2692{
2693 struct ablkcipher_edesc *edesc;
2694 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2695 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
2696 struct device *jrdev = ctx->jrdev;
2697 bool iv_contig;
2698 u32 *desc;
2699 int ret = 0;
2700
2701 /* allocate extended descriptor */
2702 edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
2703 CAAM_CMD_SZ, &iv_contig);
2704 if (IS_ERR(edesc))
2705 return PTR_ERR(edesc);
2706
2707 /* Create and submit job descriptor*/
2708 init_ablkcipher_job(ctx->sh_desc_enc,
2709 ctx->sh_desc_enc_dma, edesc, req, iv_contig);
2710#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03002711 print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08002712 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
2713 desc_bytes(edesc->hw_desc), 1);
2714#endif
2715 desc = edesc->hw_desc;
2716 ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req);
2717
2718 if (!ret) {
2719 ret = -EINPROGRESS;
2720 } else {
2721 ablkcipher_unmap(jrdev, edesc, req);
2722 kfree(edesc);
2723 }
2724
2725 return ret;
2726}
2727
2728static int ablkcipher_decrypt(struct ablkcipher_request *req)
2729{
2730 struct ablkcipher_edesc *edesc;
2731 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2732 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
2733 struct device *jrdev = ctx->jrdev;
2734 bool iv_contig;
2735 u32 *desc;
2736 int ret = 0;
2737
2738 /* allocate extended descriptor */
2739 edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
2740 CAAM_CMD_SZ, &iv_contig);
2741 if (IS_ERR(edesc))
2742 return PTR_ERR(edesc);
2743
2744 /* Create and submit job descriptor*/
2745 init_ablkcipher_job(ctx->sh_desc_dec,
2746 ctx->sh_desc_dec_dma, edesc, req, iv_contig);
2747 desc = edesc->hw_desc;
2748#ifdef DEBUG
Alex Porosanu514df282013-08-14 18:56:45 +03002749 print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
Yuan Kangacdca312011-07-15 11:21:42 +08002750 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
2751 desc_bytes(edesc->hw_desc), 1);
2752#endif
2753
2754 ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req);
2755 if (!ret) {
2756 ret = -EINPROGRESS;
2757 } else {
2758 ablkcipher_unmap(jrdev, edesc, req);
2759 kfree(edesc);
2760 }
2761
2762 return ret;
2763}
2764
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002765/*
2766 * allocate and map the ablkcipher extended descriptor
2767 * for ablkcipher givencrypt
2768 */
2769static struct ablkcipher_edesc *ablkcipher_giv_edesc_alloc(
2770 struct skcipher_givcrypt_request *greq,
2771 int desc_bytes,
2772 bool *iv_contig_out)
2773{
2774 struct ablkcipher_request *req = &greq->creq;
2775 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2776 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
2777 struct device *jrdev = ctx->jrdev;
2778 gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
2779 CRYPTO_TFM_REQ_MAY_SLEEP)) ?
2780 GFP_KERNEL : GFP_ATOMIC;
2781 int src_nents, dst_nents = 0, sec4_sg_bytes;
2782 struct ablkcipher_edesc *edesc;
2783 dma_addr_t iv_dma = 0;
2784 bool iv_contig = false;
2785 int sgc;
2786 int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002787 int sec4_sg_index;
2788
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002789 src_nents = sg_count(req->src, req->nbytes);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002790
2791 if (unlikely(req->dst != req->src))
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002792 dst_nents = sg_count(req->dst, req->nbytes);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002793
2794 if (likely(req->src == req->dst)) {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002795 sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
2796 DMA_BIDIRECTIONAL);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002797 } else {
LABBE Corentin13fb8fd2015-09-23 13:55:27 +02002798 sgc = dma_map_sg(jrdev, req->src, src_nents ? : 1,
2799 DMA_TO_DEVICE);
2800 sgc = dma_map_sg(jrdev, req->dst, dst_nents ? : 1,
2801 DMA_FROM_DEVICE);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002802 }
2803
2804 /*
2805 * Check if iv can be contiguous with source and destination.
2806 * If so, include it. If not, create scatterlist.
2807 */
2808 iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE);
2809 if (dma_mapping_error(jrdev, iv_dma)) {
2810 dev_err(jrdev, "unable to map IV\n");
2811 return ERR_PTR(-ENOMEM);
2812 }
2813
2814 if (!dst_nents && iv_dma + ivsize == sg_dma_address(req->dst))
2815 iv_contig = true;
2816 else
2817 dst_nents = dst_nents ? : 1;
2818 sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) *
2819 sizeof(struct sec4_sg_entry);
2820
2821 /* allocate space for base edesc and hw desc commands, link tables */
Victoria Milhoandde20ae2015-08-05 11:28:39 -07002822 edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes,
2823 GFP_DMA | flags);
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002824 if (!edesc) {
2825 dev_err(jrdev, "could not allocate extended descriptor\n");
2826 return ERR_PTR(-ENOMEM);
2827 }
2828
2829 edesc->src_nents = src_nents;
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002830 edesc->dst_nents = dst_nents;
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002831 edesc->sec4_sg_bytes = sec4_sg_bytes;
2832 edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
2833 desc_bytes;
2834
2835 sec4_sg_index = 0;
2836 if (src_nents) {
2837 sg_to_sec4_sg_last(req->src, src_nents, edesc->sec4_sg, 0);
2838 sec4_sg_index += src_nents;
2839 }
2840
2841 if (!iv_contig) {
2842 dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
2843 iv_dma, ivsize, 0);
2844 sec4_sg_index += 1;
2845 sg_to_sec4_sg_last(req->dst, dst_nents,
2846 edesc->sec4_sg + sec4_sg_index, 0);
2847 }
2848
2849 edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
2850 sec4_sg_bytes, DMA_TO_DEVICE);
2851 if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) {
2852 dev_err(jrdev, "unable to map S/G table\n");
2853 return ERR_PTR(-ENOMEM);
2854 }
2855 edesc->iv_dma = iv_dma;
2856
2857#ifdef DEBUG
2858 print_hex_dump(KERN_ERR,
2859 "ablkcipher sec4_sg@" __stringify(__LINE__) ": ",
2860 DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
2861 sec4_sg_bytes, 1);
2862#endif
2863
2864 *iv_contig_out = iv_contig;
2865 return edesc;
2866}
2867
2868static int ablkcipher_givencrypt(struct skcipher_givcrypt_request *creq)
2869{
2870 struct ablkcipher_request *req = &creq->creq;
2871 struct ablkcipher_edesc *edesc;
2872 struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
2873 struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
2874 struct device *jrdev = ctx->jrdev;
2875 bool iv_contig;
2876 u32 *desc;
2877 int ret = 0;
2878
2879 /* allocate extended descriptor */
2880 edesc = ablkcipher_giv_edesc_alloc(creq, DESC_JOB_IO_LEN *
2881 CAAM_CMD_SZ, &iv_contig);
2882 if (IS_ERR(edesc))
2883 return PTR_ERR(edesc);
2884
2885 /* Create and submit job descriptor*/
2886 init_ablkcipher_giv_job(ctx->sh_desc_givenc, ctx->sh_desc_givenc_dma,
2887 edesc, req, iv_contig);
2888#ifdef DEBUG
2889 print_hex_dump(KERN_ERR,
2890 "ablkcipher jobdesc@" __stringify(__LINE__) ": ",
2891 DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
2892 desc_bytes(edesc->hw_desc), 1);
2893#endif
2894 desc = edesc->hw_desc;
2895 ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req);
2896
2897 if (!ret) {
2898 ret = -EINPROGRESS;
2899 } else {
2900 ablkcipher_unmap(jrdev, edesc, req);
2901 kfree(edesc);
2902 }
2903
2904 return ret;
2905}
2906
Yuan Kang885e9e22011-07-15 11:21:41 +08002907#define template_aead template_u.aead
Yuan Kangacdca312011-07-15 11:21:42 +08002908#define template_ablkcipher template_u.ablkcipher
Kim Phillips8e8ec592011-03-13 16:54:26 +08002909struct caam_alg_template {
2910 char name[CRYPTO_MAX_ALG_NAME];
2911 char driver_name[CRYPTO_MAX_ALG_NAME];
2912 unsigned int blocksize;
Yuan Kang885e9e22011-07-15 11:21:41 +08002913 u32 type;
2914 union {
2915 struct ablkcipher_alg ablkcipher;
Yuan Kang885e9e22011-07-15 11:21:41 +08002916 } template_u;
Kim Phillips8e8ec592011-03-13 16:54:26 +08002917 u32 class1_alg_type;
2918 u32 class2_alg_type;
2919 u32 alg_op;
2920};
2921
2922static struct caam_alg_template driver_algs[] = {
Yuan Kangacdca312011-07-15 11:21:42 +08002923 /* ablkcipher descriptor */
2924 {
2925 .name = "cbc(aes)",
2926 .driver_name = "cbc-aes-caam",
2927 .blocksize = AES_BLOCK_SIZE,
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002928 .type = CRYPTO_ALG_TYPE_GIVCIPHER,
Yuan Kangacdca312011-07-15 11:21:42 +08002929 .template_ablkcipher = {
2930 .setkey = ablkcipher_setkey,
2931 .encrypt = ablkcipher_encrypt,
2932 .decrypt = ablkcipher_decrypt,
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002933 .givencrypt = ablkcipher_givencrypt,
2934 .geniv = "<built-in>",
Yuan Kangacdca312011-07-15 11:21:42 +08002935 .min_keysize = AES_MIN_KEY_SIZE,
2936 .max_keysize = AES_MAX_KEY_SIZE,
2937 .ivsize = AES_BLOCK_SIZE,
2938 },
2939 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
2940 },
2941 {
2942 .name = "cbc(des3_ede)",
2943 .driver_name = "cbc-3des-caam",
2944 .blocksize = DES3_EDE_BLOCK_SIZE,
Catalin Vasileff2c3a32014-11-11 16:18:13 +02002945 .type = CRYPTO_ALG_TYPE_GIVCIPHER,
Yuan Kangacdca312011-07-15 11:21:42 +08002946 .template_ablkcipher = {
2947 .setkey = ablkcipher_setkey,
2948 .encrypt = ablkcipher_encrypt,
2949 .decrypt = ablkcipher_decrypt,
Catalin Vasileff2c3a32014-11-11 16:18:13 +02002950 .givencrypt = ablkcipher_givencrypt,
2951 .geniv = "<built-in>",
Yuan Kangacdca312011-07-15 11:21:42 +08002952 .min_keysize = DES3_EDE_KEY_SIZE,
2953 .max_keysize = DES3_EDE_KEY_SIZE,
2954 .ivsize = DES3_EDE_BLOCK_SIZE,
2955 },
2956 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
2957 },
2958 {
2959 .name = "cbc(des)",
2960 .driver_name = "cbc-des-caam",
2961 .blocksize = DES_BLOCK_SIZE,
Catalin Vasileff2c3a32014-11-11 16:18:13 +02002962 .type = CRYPTO_ALG_TYPE_GIVCIPHER,
Yuan Kangacdca312011-07-15 11:21:42 +08002963 .template_ablkcipher = {
2964 .setkey = ablkcipher_setkey,
2965 .encrypt = ablkcipher_encrypt,
2966 .decrypt = ablkcipher_decrypt,
Catalin Vasileff2c3a32014-11-11 16:18:13 +02002967 .givencrypt = ablkcipher_givencrypt,
2968 .geniv = "<built-in>",
Yuan Kangacdca312011-07-15 11:21:42 +08002969 .min_keysize = DES_KEY_SIZE,
2970 .max_keysize = DES_KEY_SIZE,
2971 .ivsize = DES_BLOCK_SIZE,
2972 },
2973 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
Catalin Vasile2b22f6c2014-10-31 12:45:35 +02002974 },
2975 {
2976 .name = "ctr(aes)",
2977 .driver_name = "ctr-aes-caam",
2978 .blocksize = 1,
2979 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2980 .template_ablkcipher = {
2981 .setkey = ablkcipher_setkey,
2982 .encrypt = ablkcipher_encrypt,
2983 .decrypt = ablkcipher_decrypt,
2984 .geniv = "chainiv",
2985 .min_keysize = AES_MIN_KEY_SIZE,
2986 .max_keysize = AES_MAX_KEY_SIZE,
2987 .ivsize = AES_BLOCK_SIZE,
2988 },
2989 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02002990 },
2991 {
2992 .name = "rfc3686(ctr(aes))",
2993 .driver_name = "rfc3686-ctr-aes-caam",
2994 .blocksize = 1,
Catalin Vasile7222d1a2014-10-31 12:45:38 +02002995 .type = CRYPTO_ALG_TYPE_GIVCIPHER,
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02002996 .template_ablkcipher = {
2997 .setkey = ablkcipher_setkey,
2998 .encrypt = ablkcipher_encrypt,
2999 .decrypt = ablkcipher_decrypt,
Catalin Vasile7222d1a2014-10-31 12:45:38 +02003000 .givencrypt = ablkcipher_givencrypt,
3001 .geniv = "<built-in>",
Catalin Vasilea5f57cf2014-10-31 12:45:36 +02003002 .min_keysize = AES_MIN_KEY_SIZE +
3003 CTR_RFC3686_NONCE_SIZE,
3004 .max_keysize = AES_MAX_KEY_SIZE +
3005 CTR_RFC3686_NONCE_SIZE,
3006 .ivsize = CTR_RFC3686_IV_SIZE,
3007 },
3008 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128,
Catalin Vasilec6415a62015-10-02 13:13:18 +03003009 },
3010 {
3011 .name = "xts(aes)",
3012 .driver_name = "xts-aes-caam",
3013 .blocksize = AES_BLOCK_SIZE,
3014 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
3015 .template_ablkcipher = {
3016 .setkey = xts_ablkcipher_setkey,
3017 .encrypt = ablkcipher_encrypt,
3018 .decrypt = ablkcipher_decrypt,
3019 .geniv = "eseqiv",
3020 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3021 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3022 .ivsize = AES_BLOCK_SIZE,
3023 },
3024 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS,
3025 },
Kim Phillips8e8ec592011-03-13 16:54:26 +08003026};
3027
Herbert Xuf2147b82015-06-16 13:54:23 +08003028static struct caam_aead_alg driver_aeads[] = {
3029 {
3030 .aead = {
3031 .base = {
3032 .cra_name = "rfc4106(gcm(aes))",
3033 .cra_driver_name = "rfc4106-gcm-aes-caam",
3034 .cra_blocksize = 1,
3035 },
3036 .setkey = rfc4106_setkey,
3037 .setauthsize = rfc4106_setauthsize,
Herbert Xu46218752015-07-09 07:17:33 +08003038 .encrypt = ipsec_gcm_encrypt,
3039 .decrypt = ipsec_gcm_decrypt,
Herbert Xuf2147b82015-06-16 13:54:23 +08003040 .ivsize = 8,
3041 .maxauthsize = AES_BLOCK_SIZE,
3042 },
3043 .caam = {
3044 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
3045 },
3046 },
3047 {
3048 .aead = {
3049 .base = {
3050 .cra_name = "rfc4543(gcm(aes))",
3051 .cra_driver_name = "rfc4543-gcm-aes-caam",
3052 .cra_blocksize = 1,
3053 },
3054 .setkey = rfc4543_setkey,
3055 .setauthsize = rfc4543_setauthsize,
Herbert Xu46218752015-07-09 07:17:33 +08003056 .encrypt = ipsec_gcm_encrypt,
3057 .decrypt = ipsec_gcm_decrypt,
Herbert Xuf2147b82015-06-16 13:54:23 +08003058 .ivsize = 8,
3059 .maxauthsize = AES_BLOCK_SIZE,
3060 },
3061 .caam = {
3062 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
3063 },
3064 },
3065 /* Galois Counter Mode */
3066 {
3067 .aead = {
3068 .base = {
3069 .cra_name = "gcm(aes)",
3070 .cra_driver_name = "gcm-aes-caam",
3071 .cra_blocksize = 1,
3072 },
3073 .setkey = gcm_setkey,
3074 .setauthsize = gcm_setauthsize,
3075 .encrypt = gcm_encrypt,
3076 .decrypt = gcm_decrypt,
3077 .ivsize = 12,
3078 .maxauthsize = AES_BLOCK_SIZE,
3079 },
3080 .caam = {
3081 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM,
3082 },
3083 },
Herbert Xu479bcc72015-07-30 17:53:17 +08003084 /* single-pass ipsec_esp descriptor */
3085 {
3086 .aead = {
3087 .base = {
3088 .cra_name = "authenc(hmac(md5),"
3089 "ecb(cipher_null))",
3090 .cra_driver_name = "authenc-hmac-md5-"
3091 "ecb-cipher_null-caam",
3092 .cra_blocksize = NULL_BLOCK_SIZE,
3093 },
3094 .setkey = aead_setkey,
3095 .setauthsize = aead_setauthsize,
3096 .encrypt = aead_encrypt,
3097 .decrypt = aead_decrypt,
3098 .ivsize = NULL_IV_SIZE,
3099 .maxauthsize = MD5_DIGEST_SIZE,
3100 },
3101 .caam = {
3102 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
3103 OP_ALG_AAI_HMAC_PRECOMP,
3104 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
3105 },
3106 },
3107 {
3108 .aead = {
3109 .base = {
3110 .cra_name = "authenc(hmac(sha1),"
3111 "ecb(cipher_null))",
3112 .cra_driver_name = "authenc-hmac-sha1-"
3113 "ecb-cipher_null-caam",
3114 .cra_blocksize = NULL_BLOCK_SIZE,
3115 },
3116 .setkey = aead_setkey,
3117 .setauthsize = aead_setauthsize,
3118 .encrypt = aead_encrypt,
3119 .decrypt = aead_decrypt,
3120 .ivsize = NULL_IV_SIZE,
3121 .maxauthsize = SHA1_DIGEST_SIZE,
3122 },
3123 .caam = {
3124 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
3125 OP_ALG_AAI_HMAC_PRECOMP,
3126 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
3127 },
3128 },
3129 {
3130 .aead = {
3131 .base = {
3132 .cra_name = "authenc(hmac(sha224),"
3133 "ecb(cipher_null))",
3134 .cra_driver_name = "authenc-hmac-sha224-"
3135 "ecb-cipher_null-caam",
3136 .cra_blocksize = NULL_BLOCK_SIZE,
3137 },
3138 .setkey = aead_setkey,
3139 .setauthsize = aead_setauthsize,
3140 .encrypt = aead_encrypt,
3141 .decrypt = aead_decrypt,
3142 .ivsize = NULL_IV_SIZE,
3143 .maxauthsize = SHA224_DIGEST_SIZE,
3144 },
3145 .caam = {
3146 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
3147 OP_ALG_AAI_HMAC_PRECOMP,
3148 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
3149 },
3150 },
3151 {
3152 .aead = {
3153 .base = {
3154 .cra_name = "authenc(hmac(sha256),"
3155 "ecb(cipher_null))",
3156 .cra_driver_name = "authenc-hmac-sha256-"
3157 "ecb-cipher_null-caam",
3158 .cra_blocksize = NULL_BLOCK_SIZE,
3159 },
3160 .setkey = aead_setkey,
3161 .setauthsize = aead_setauthsize,
3162 .encrypt = aead_encrypt,
3163 .decrypt = aead_decrypt,
3164 .ivsize = NULL_IV_SIZE,
3165 .maxauthsize = SHA256_DIGEST_SIZE,
3166 },
3167 .caam = {
3168 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
3169 OP_ALG_AAI_HMAC_PRECOMP,
3170 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
3171 },
3172 },
3173 {
3174 .aead = {
3175 .base = {
3176 .cra_name = "authenc(hmac(sha384),"
3177 "ecb(cipher_null))",
3178 .cra_driver_name = "authenc-hmac-sha384-"
3179 "ecb-cipher_null-caam",
3180 .cra_blocksize = NULL_BLOCK_SIZE,
3181 },
3182 .setkey = aead_setkey,
3183 .setauthsize = aead_setauthsize,
3184 .encrypt = aead_encrypt,
3185 .decrypt = aead_decrypt,
3186 .ivsize = NULL_IV_SIZE,
3187 .maxauthsize = SHA384_DIGEST_SIZE,
3188 },
3189 .caam = {
3190 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
3191 OP_ALG_AAI_HMAC_PRECOMP,
3192 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
3193 },
3194 },
3195 {
3196 .aead = {
3197 .base = {
3198 .cra_name = "authenc(hmac(sha512),"
3199 "ecb(cipher_null))",
3200 .cra_driver_name = "authenc-hmac-sha512-"
3201 "ecb-cipher_null-caam",
3202 .cra_blocksize = NULL_BLOCK_SIZE,
3203 },
3204 .setkey = aead_setkey,
3205 .setauthsize = aead_setauthsize,
3206 .encrypt = aead_encrypt,
3207 .decrypt = aead_decrypt,
3208 .ivsize = NULL_IV_SIZE,
3209 .maxauthsize = SHA512_DIGEST_SIZE,
3210 },
3211 .caam = {
3212 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
3213 OP_ALG_AAI_HMAC_PRECOMP,
3214 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
3215 },
3216 },
3217 {
3218 .aead = {
3219 .base = {
3220 .cra_name = "authenc(hmac(md5),cbc(aes))",
3221 .cra_driver_name = "authenc-hmac-md5-"
3222 "cbc-aes-caam",
3223 .cra_blocksize = AES_BLOCK_SIZE,
3224 },
3225 .setkey = aead_setkey,
3226 .setauthsize = aead_setauthsize,
3227 .encrypt = aead_encrypt,
3228 .decrypt = aead_decrypt,
3229 .ivsize = AES_BLOCK_SIZE,
3230 .maxauthsize = MD5_DIGEST_SIZE,
3231 },
3232 .caam = {
3233 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3234 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
3235 OP_ALG_AAI_HMAC_PRECOMP,
3236 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
3237 },
3238 },
3239 {
3240 .aead = {
3241 .base = {
3242 .cra_name = "echainiv(authenc(hmac(md5),"
3243 "cbc(aes)))",
3244 .cra_driver_name = "echainiv-authenc-hmac-md5-"
3245 "cbc-aes-caam",
3246 .cra_blocksize = AES_BLOCK_SIZE,
3247 },
3248 .setkey = aead_setkey,
3249 .setauthsize = aead_setauthsize,
3250 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003251 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003252 .ivsize = AES_BLOCK_SIZE,
3253 .maxauthsize = MD5_DIGEST_SIZE,
3254 },
3255 .caam = {
3256 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3257 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
3258 OP_ALG_AAI_HMAC_PRECOMP,
3259 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
3260 .geniv = true,
3261 },
3262 },
3263 {
3264 .aead = {
3265 .base = {
3266 .cra_name = "authenc(hmac(sha1),cbc(aes))",
3267 .cra_driver_name = "authenc-hmac-sha1-"
3268 "cbc-aes-caam",
3269 .cra_blocksize = AES_BLOCK_SIZE,
3270 },
3271 .setkey = aead_setkey,
3272 .setauthsize = aead_setauthsize,
3273 .encrypt = aead_encrypt,
3274 .decrypt = aead_decrypt,
3275 .ivsize = AES_BLOCK_SIZE,
3276 .maxauthsize = SHA1_DIGEST_SIZE,
3277 },
3278 .caam = {
3279 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3280 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
3281 OP_ALG_AAI_HMAC_PRECOMP,
3282 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
3283 },
3284 },
3285 {
3286 .aead = {
3287 .base = {
3288 .cra_name = "echainiv(authenc(hmac(sha1),"
3289 "cbc(aes)))",
3290 .cra_driver_name = "echainiv-authenc-"
3291 "hmac-sha1-cbc-aes-caam",
3292 .cra_blocksize = AES_BLOCK_SIZE,
3293 },
3294 .setkey = aead_setkey,
3295 .setauthsize = aead_setauthsize,
3296 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003297 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003298 .ivsize = AES_BLOCK_SIZE,
3299 .maxauthsize = SHA1_DIGEST_SIZE,
3300 },
3301 .caam = {
3302 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3303 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
3304 OP_ALG_AAI_HMAC_PRECOMP,
3305 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
3306 .geniv = true,
3307 },
3308 },
3309 {
3310 .aead = {
3311 .base = {
3312 .cra_name = "authenc(hmac(sha224),cbc(aes))",
3313 .cra_driver_name = "authenc-hmac-sha224-"
3314 "cbc-aes-caam",
3315 .cra_blocksize = AES_BLOCK_SIZE,
3316 },
3317 .setkey = aead_setkey,
3318 .setauthsize = aead_setauthsize,
3319 .encrypt = aead_encrypt,
3320 .decrypt = aead_decrypt,
3321 .ivsize = AES_BLOCK_SIZE,
3322 .maxauthsize = SHA224_DIGEST_SIZE,
3323 },
3324 .caam = {
3325 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3326 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
3327 OP_ALG_AAI_HMAC_PRECOMP,
3328 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
3329 },
3330 },
3331 {
3332 .aead = {
3333 .base = {
3334 .cra_name = "echainiv(authenc(hmac(sha224),"
3335 "cbc(aes)))",
3336 .cra_driver_name = "echainiv-authenc-"
3337 "hmac-sha224-cbc-aes-caam",
3338 .cra_blocksize = AES_BLOCK_SIZE,
3339 },
3340 .setkey = aead_setkey,
3341 .setauthsize = aead_setauthsize,
3342 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003343 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003344 .ivsize = AES_BLOCK_SIZE,
3345 .maxauthsize = SHA224_DIGEST_SIZE,
3346 },
3347 .caam = {
3348 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3349 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
3350 OP_ALG_AAI_HMAC_PRECOMP,
3351 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
3352 .geniv = true,
3353 },
3354 },
3355 {
3356 .aead = {
3357 .base = {
3358 .cra_name = "authenc(hmac(sha256),cbc(aes))",
3359 .cra_driver_name = "authenc-hmac-sha256-"
3360 "cbc-aes-caam",
3361 .cra_blocksize = AES_BLOCK_SIZE,
3362 },
3363 .setkey = aead_setkey,
3364 .setauthsize = aead_setauthsize,
3365 .encrypt = aead_encrypt,
3366 .decrypt = aead_decrypt,
3367 .ivsize = AES_BLOCK_SIZE,
3368 .maxauthsize = SHA256_DIGEST_SIZE,
3369 },
3370 .caam = {
3371 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3372 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
3373 OP_ALG_AAI_HMAC_PRECOMP,
3374 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
3375 },
3376 },
3377 {
3378 .aead = {
3379 .base = {
3380 .cra_name = "echainiv(authenc(hmac(sha256),"
3381 "cbc(aes)))",
3382 .cra_driver_name = "echainiv-authenc-"
3383 "hmac-sha256-cbc-aes-caam",
3384 .cra_blocksize = AES_BLOCK_SIZE,
3385 },
3386 .setkey = aead_setkey,
3387 .setauthsize = aead_setauthsize,
3388 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003389 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003390 .ivsize = AES_BLOCK_SIZE,
3391 .maxauthsize = SHA256_DIGEST_SIZE,
3392 },
3393 .caam = {
3394 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3395 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
3396 OP_ALG_AAI_HMAC_PRECOMP,
3397 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
3398 .geniv = true,
3399 },
3400 },
3401 {
3402 .aead = {
3403 .base = {
3404 .cra_name = "authenc(hmac(sha384),cbc(aes))",
3405 .cra_driver_name = "authenc-hmac-sha384-"
3406 "cbc-aes-caam",
3407 .cra_blocksize = AES_BLOCK_SIZE,
3408 },
3409 .setkey = aead_setkey,
3410 .setauthsize = aead_setauthsize,
3411 .encrypt = aead_encrypt,
3412 .decrypt = aead_decrypt,
3413 .ivsize = AES_BLOCK_SIZE,
3414 .maxauthsize = SHA384_DIGEST_SIZE,
3415 },
3416 .caam = {
3417 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3418 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
3419 OP_ALG_AAI_HMAC_PRECOMP,
3420 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
3421 },
3422 },
3423 {
3424 .aead = {
3425 .base = {
3426 .cra_name = "echainiv(authenc(hmac(sha384),"
3427 "cbc(aes)))",
3428 .cra_driver_name = "echainiv-authenc-"
3429 "hmac-sha384-cbc-aes-caam",
3430 .cra_blocksize = AES_BLOCK_SIZE,
3431 },
3432 .setkey = aead_setkey,
3433 .setauthsize = aead_setauthsize,
3434 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003435 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003436 .ivsize = AES_BLOCK_SIZE,
3437 .maxauthsize = SHA384_DIGEST_SIZE,
3438 },
3439 .caam = {
3440 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3441 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
3442 OP_ALG_AAI_HMAC_PRECOMP,
3443 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
3444 .geniv = true,
3445 },
3446 },
3447 {
3448 .aead = {
3449 .base = {
3450 .cra_name = "authenc(hmac(sha512),cbc(aes))",
3451 .cra_driver_name = "authenc-hmac-sha512-"
3452 "cbc-aes-caam",
3453 .cra_blocksize = AES_BLOCK_SIZE,
3454 },
3455 .setkey = aead_setkey,
3456 .setauthsize = aead_setauthsize,
3457 .encrypt = aead_encrypt,
3458 .decrypt = aead_decrypt,
3459 .ivsize = AES_BLOCK_SIZE,
3460 .maxauthsize = SHA512_DIGEST_SIZE,
3461 },
3462 .caam = {
3463 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3464 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
3465 OP_ALG_AAI_HMAC_PRECOMP,
3466 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
3467 },
3468 },
3469 {
3470 .aead = {
3471 .base = {
3472 .cra_name = "echainiv(authenc(hmac(sha512),"
3473 "cbc(aes)))",
3474 .cra_driver_name = "echainiv-authenc-"
3475 "hmac-sha512-cbc-aes-caam",
3476 .cra_blocksize = AES_BLOCK_SIZE,
3477 },
3478 .setkey = aead_setkey,
3479 .setauthsize = aead_setauthsize,
3480 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003481 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003482 .ivsize = AES_BLOCK_SIZE,
3483 .maxauthsize = SHA512_DIGEST_SIZE,
3484 },
3485 .caam = {
3486 .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
3487 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
3488 OP_ALG_AAI_HMAC_PRECOMP,
3489 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
3490 .geniv = true,
3491 },
3492 },
3493 {
3494 .aead = {
3495 .base = {
3496 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
3497 .cra_driver_name = "authenc-hmac-md5-"
3498 "cbc-des3_ede-caam",
3499 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3500 },
3501 .setkey = aead_setkey,
3502 .setauthsize = aead_setauthsize,
3503 .encrypt = aead_encrypt,
3504 .decrypt = aead_decrypt,
3505 .ivsize = DES3_EDE_BLOCK_SIZE,
3506 .maxauthsize = MD5_DIGEST_SIZE,
3507 },
3508 .caam = {
3509 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3510 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
3511 OP_ALG_AAI_HMAC_PRECOMP,
3512 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
3513 }
3514 },
3515 {
3516 .aead = {
3517 .base = {
3518 .cra_name = "echainiv(authenc(hmac(md5),"
3519 "cbc(des3_ede)))",
3520 .cra_driver_name = "echainiv-authenc-hmac-md5-"
3521 "cbc-des3_ede-caam",
3522 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3523 },
3524 .setkey = aead_setkey,
3525 .setauthsize = aead_setauthsize,
3526 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003527 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003528 .ivsize = DES3_EDE_BLOCK_SIZE,
3529 .maxauthsize = MD5_DIGEST_SIZE,
3530 },
3531 .caam = {
3532 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3533 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
3534 OP_ALG_AAI_HMAC_PRECOMP,
3535 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
3536 .geniv = true,
3537 }
3538 },
3539 {
3540 .aead = {
3541 .base = {
3542 .cra_name = "authenc(hmac(sha1),"
3543 "cbc(des3_ede))",
3544 .cra_driver_name = "authenc-hmac-sha1-"
3545 "cbc-des3_ede-caam",
3546 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3547 },
3548 .setkey = aead_setkey,
3549 .setauthsize = aead_setauthsize,
3550 .encrypt = aead_encrypt,
3551 .decrypt = aead_decrypt,
3552 .ivsize = DES3_EDE_BLOCK_SIZE,
3553 .maxauthsize = SHA1_DIGEST_SIZE,
3554 },
3555 .caam = {
3556 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3557 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
3558 OP_ALG_AAI_HMAC_PRECOMP,
3559 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
3560 },
3561 },
3562 {
3563 .aead = {
3564 .base = {
3565 .cra_name = "echainiv(authenc(hmac(sha1),"
3566 "cbc(des3_ede)))",
3567 .cra_driver_name = "echainiv-authenc-"
3568 "hmac-sha1-"
3569 "cbc-des3_ede-caam",
3570 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3571 },
3572 .setkey = aead_setkey,
3573 .setauthsize = aead_setauthsize,
3574 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003575 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003576 .ivsize = DES3_EDE_BLOCK_SIZE,
3577 .maxauthsize = SHA1_DIGEST_SIZE,
3578 },
3579 .caam = {
3580 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3581 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
3582 OP_ALG_AAI_HMAC_PRECOMP,
3583 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
3584 .geniv = true,
3585 },
3586 },
3587 {
3588 .aead = {
3589 .base = {
3590 .cra_name = "authenc(hmac(sha224),"
3591 "cbc(des3_ede))",
3592 .cra_driver_name = "authenc-hmac-sha224-"
3593 "cbc-des3_ede-caam",
3594 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3595 },
3596 .setkey = aead_setkey,
3597 .setauthsize = aead_setauthsize,
3598 .encrypt = aead_encrypt,
3599 .decrypt = aead_decrypt,
3600 .ivsize = DES3_EDE_BLOCK_SIZE,
3601 .maxauthsize = SHA224_DIGEST_SIZE,
3602 },
3603 .caam = {
3604 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3605 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
3606 OP_ALG_AAI_HMAC_PRECOMP,
3607 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
3608 },
3609 },
3610 {
3611 .aead = {
3612 .base = {
3613 .cra_name = "echainiv(authenc(hmac(sha224),"
3614 "cbc(des3_ede)))",
3615 .cra_driver_name = "echainiv-authenc-"
3616 "hmac-sha224-"
3617 "cbc-des3_ede-caam",
3618 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3619 },
3620 .setkey = aead_setkey,
3621 .setauthsize = aead_setauthsize,
3622 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003623 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003624 .ivsize = DES3_EDE_BLOCK_SIZE,
3625 .maxauthsize = SHA224_DIGEST_SIZE,
3626 },
3627 .caam = {
3628 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3629 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
3630 OP_ALG_AAI_HMAC_PRECOMP,
3631 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
3632 .geniv = true,
3633 },
3634 },
3635 {
3636 .aead = {
3637 .base = {
3638 .cra_name = "authenc(hmac(sha256),"
3639 "cbc(des3_ede))",
3640 .cra_driver_name = "authenc-hmac-sha256-"
3641 "cbc-des3_ede-caam",
3642 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3643 },
3644 .setkey = aead_setkey,
3645 .setauthsize = aead_setauthsize,
3646 .encrypt = aead_encrypt,
3647 .decrypt = aead_decrypt,
3648 .ivsize = DES3_EDE_BLOCK_SIZE,
3649 .maxauthsize = SHA256_DIGEST_SIZE,
3650 },
3651 .caam = {
3652 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3653 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
3654 OP_ALG_AAI_HMAC_PRECOMP,
3655 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
3656 },
3657 },
3658 {
3659 .aead = {
3660 .base = {
3661 .cra_name = "echainiv(authenc(hmac(sha256),"
3662 "cbc(des3_ede)))",
3663 .cra_driver_name = "echainiv-authenc-"
3664 "hmac-sha256-"
3665 "cbc-des3_ede-caam",
3666 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3667 },
3668 .setkey = aead_setkey,
3669 .setauthsize = aead_setauthsize,
3670 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003671 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003672 .ivsize = DES3_EDE_BLOCK_SIZE,
3673 .maxauthsize = SHA256_DIGEST_SIZE,
3674 },
3675 .caam = {
3676 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3677 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
3678 OP_ALG_AAI_HMAC_PRECOMP,
3679 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
3680 .geniv = true,
3681 },
3682 },
3683 {
3684 .aead = {
3685 .base = {
3686 .cra_name = "authenc(hmac(sha384),"
3687 "cbc(des3_ede))",
3688 .cra_driver_name = "authenc-hmac-sha384-"
3689 "cbc-des3_ede-caam",
3690 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3691 },
3692 .setkey = aead_setkey,
3693 .setauthsize = aead_setauthsize,
3694 .encrypt = aead_encrypt,
3695 .decrypt = aead_decrypt,
3696 .ivsize = DES3_EDE_BLOCK_SIZE,
3697 .maxauthsize = SHA384_DIGEST_SIZE,
3698 },
3699 .caam = {
3700 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3701 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
3702 OP_ALG_AAI_HMAC_PRECOMP,
3703 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
3704 },
3705 },
3706 {
3707 .aead = {
3708 .base = {
3709 .cra_name = "echainiv(authenc(hmac(sha384),"
3710 "cbc(des3_ede)))",
3711 .cra_driver_name = "echainiv-authenc-"
3712 "hmac-sha384-"
3713 "cbc-des3_ede-caam",
3714 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3715 },
3716 .setkey = aead_setkey,
3717 .setauthsize = aead_setauthsize,
3718 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003719 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003720 .ivsize = DES3_EDE_BLOCK_SIZE,
3721 .maxauthsize = SHA384_DIGEST_SIZE,
3722 },
3723 .caam = {
3724 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3725 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
3726 OP_ALG_AAI_HMAC_PRECOMP,
3727 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
3728 .geniv = true,
3729 },
3730 },
3731 {
3732 .aead = {
3733 .base = {
3734 .cra_name = "authenc(hmac(sha512),"
3735 "cbc(des3_ede))",
3736 .cra_driver_name = "authenc-hmac-sha512-"
3737 "cbc-des3_ede-caam",
3738 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3739 },
3740 .setkey = aead_setkey,
3741 .setauthsize = aead_setauthsize,
3742 .encrypt = aead_encrypt,
3743 .decrypt = aead_decrypt,
3744 .ivsize = DES3_EDE_BLOCK_SIZE,
3745 .maxauthsize = SHA512_DIGEST_SIZE,
3746 },
3747 .caam = {
3748 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3749 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
3750 OP_ALG_AAI_HMAC_PRECOMP,
3751 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
3752 },
3753 },
3754 {
3755 .aead = {
3756 .base = {
3757 .cra_name = "echainiv(authenc(hmac(sha512),"
3758 "cbc(des3_ede)))",
3759 .cra_driver_name = "echainiv-authenc-"
3760 "hmac-sha512-"
3761 "cbc-des3_ede-caam",
3762 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
3763 },
3764 .setkey = aead_setkey,
3765 .setauthsize = aead_setauthsize,
3766 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003767 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003768 .ivsize = DES3_EDE_BLOCK_SIZE,
3769 .maxauthsize = SHA512_DIGEST_SIZE,
3770 },
3771 .caam = {
3772 .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
3773 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
3774 OP_ALG_AAI_HMAC_PRECOMP,
3775 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
3776 .geniv = true,
3777 },
3778 },
3779 {
3780 .aead = {
3781 .base = {
3782 .cra_name = "authenc(hmac(md5),cbc(des))",
3783 .cra_driver_name = "authenc-hmac-md5-"
3784 "cbc-des-caam",
3785 .cra_blocksize = DES_BLOCK_SIZE,
3786 },
3787 .setkey = aead_setkey,
3788 .setauthsize = aead_setauthsize,
3789 .encrypt = aead_encrypt,
3790 .decrypt = aead_decrypt,
3791 .ivsize = DES_BLOCK_SIZE,
3792 .maxauthsize = MD5_DIGEST_SIZE,
3793 },
3794 .caam = {
3795 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3796 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
3797 OP_ALG_AAI_HMAC_PRECOMP,
3798 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
3799 },
3800 },
3801 {
3802 .aead = {
3803 .base = {
3804 .cra_name = "echainiv(authenc(hmac(md5),"
3805 "cbc(des)))",
3806 .cra_driver_name = "echainiv-authenc-hmac-md5-"
3807 "cbc-des-caam",
3808 .cra_blocksize = DES_BLOCK_SIZE,
3809 },
3810 .setkey = aead_setkey,
3811 .setauthsize = aead_setauthsize,
3812 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003813 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003814 .ivsize = DES_BLOCK_SIZE,
3815 .maxauthsize = MD5_DIGEST_SIZE,
3816 },
3817 .caam = {
3818 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3819 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
3820 OP_ALG_AAI_HMAC_PRECOMP,
3821 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
3822 .geniv = true,
3823 },
3824 },
3825 {
3826 .aead = {
3827 .base = {
3828 .cra_name = "authenc(hmac(sha1),cbc(des))",
3829 .cra_driver_name = "authenc-hmac-sha1-"
3830 "cbc-des-caam",
3831 .cra_blocksize = DES_BLOCK_SIZE,
3832 },
3833 .setkey = aead_setkey,
3834 .setauthsize = aead_setauthsize,
3835 .encrypt = aead_encrypt,
3836 .decrypt = aead_decrypt,
3837 .ivsize = DES_BLOCK_SIZE,
3838 .maxauthsize = SHA1_DIGEST_SIZE,
3839 },
3840 .caam = {
3841 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3842 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
3843 OP_ALG_AAI_HMAC_PRECOMP,
3844 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
3845 },
3846 },
3847 {
3848 .aead = {
3849 .base = {
3850 .cra_name = "echainiv(authenc(hmac(sha1),"
3851 "cbc(des)))",
3852 .cra_driver_name = "echainiv-authenc-"
3853 "hmac-sha1-cbc-des-caam",
3854 .cra_blocksize = DES_BLOCK_SIZE,
3855 },
3856 .setkey = aead_setkey,
3857 .setauthsize = aead_setauthsize,
3858 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003859 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003860 .ivsize = DES_BLOCK_SIZE,
3861 .maxauthsize = SHA1_DIGEST_SIZE,
3862 },
3863 .caam = {
3864 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3865 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
3866 OP_ALG_AAI_HMAC_PRECOMP,
3867 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
3868 .geniv = true,
3869 },
3870 },
3871 {
3872 .aead = {
3873 .base = {
3874 .cra_name = "authenc(hmac(sha224),cbc(des))",
3875 .cra_driver_name = "authenc-hmac-sha224-"
3876 "cbc-des-caam",
3877 .cra_blocksize = DES_BLOCK_SIZE,
3878 },
3879 .setkey = aead_setkey,
3880 .setauthsize = aead_setauthsize,
3881 .encrypt = aead_encrypt,
3882 .decrypt = aead_decrypt,
3883 .ivsize = DES_BLOCK_SIZE,
3884 .maxauthsize = SHA224_DIGEST_SIZE,
3885 },
3886 .caam = {
3887 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3888 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
3889 OP_ALG_AAI_HMAC_PRECOMP,
3890 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
3891 },
3892 },
3893 {
3894 .aead = {
3895 .base = {
3896 .cra_name = "echainiv(authenc(hmac(sha224),"
3897 "cbc(des)))",
3898 .cra_driver_name = "echainiv-authenc-"
3899 "hmac-sha224-cbc-des-caam",
3900 .cra_blocksize = DES_BLOCK_SIZE,
3901 },
3902 .setkey = aead_setkey,
3903 .setauthsize = aead_setauthsize,
3904 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003905 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003906 .ivsize = DES_BLOCK_SIZE,
3907 .maxauthsize = SHA224_DIGEST_SIZE,
3908 },
3909 .caam = {
3910 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3911 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
3912 OP_ALG_AAI_HMAC_PRECOMP,
3913 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
3914 .geniv = true,
3915 },
3916 },
3917 {
3918 .aead = {
3919 .base = {
3920 .cra_name = "authenc(hmac(sha256),cbc(des))",
3921 .cra_driver_name = "authenc-hmac-sha256-"
3922 "cbc-des-caam",
3923 .cra_blocksize = DES_BLOCK_SIZE,
3924 },
3925 .setkey = aead_setkey,
3926 .setauthsize = aead_setauthsize,
3927 .encrypt = aead_encrypt,
3928 .decrypt = aead_decrypt,
3929 .ivsize = DES_BLOCK_SIZE,
3930 .maxauthsize = SHA256_DIGEST_SIZE,
3931 },
3932 .caam = {
3933 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3934 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
3935 OP_ALG_AAI_HMAC_PRECOMP,
3936 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
3937 },
3938 },
3939 {
3940 .aead = {
3941 .base = {
3942 .cra_name = "echainiv(authenc(hmac(sha256),"
3943 "cbc(des)))",
3944 .cra_driver_name = "echainiv-authenc-"
3945 "hmac-sha256-cbc-des-caam",
3946 .cra_blocksize = DES_BLOCK_SIZE,
3947 },
3948 .setkey = aead_setkey,
3949 .setauthsize = aead_setauthsize,
3950 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003951 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003952 .ivsize = DES_BLOCK_SIZE,
3953 .maxauthsize = SHA256_DIGEST_SIZE,
3954 },
3955 .caam = {
3956 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3957 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
3958 OP_ALG_AAI_HMAC_PRECOMP,
3959 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
3960 .geniv = true,
3961 },
3962 },
3963 {
3964 .aead = {
3965 .base = {
3966 .cra_name = "authenc(hmac(sha384),cbc(des))",
3967 .cra_driver_name = "authenc-hmac-sha384-"
3968 "cbc-des-caam",
3969 .cra_blocksize = DES_BLOCK_SIZE,
3970 },
3971 .setkey = aead_setkey,
3972 .setauthsize = aead_setauthsize,
3973 .encrypt = aead_encrypt,
3974 .decrypt = aead_decrypt,
3975 .ivsize = DES_BLOCK_SIZE,
3976 .maxauthsize = SHA384_DIGEST_SIZE,
3977 },
3978 .caam = {
3979 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
3980 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
3981 OP_ALG_AAI_HMAC_PRECOMP,
3982 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
3983 },
3984 },
3985 {
3986 .aead = {
3987 .base = {
3988 .cra_name = "echainiv(authenc(hmac(sha384),"
3989 "cbc(des)))",
3990 .cra_driver_name = "echainiv-authenc-"
3991 "hmac-sha384-cbc-des-caam",
3992 .cra_blocksize = DES_BLOCK_SIZE,
3993 },
3994 .setkey = aead_setkey,
3995 .setauthsize = aead_setauthsize,
3996 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03003997 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08003998 .ivsize = DES_BLOCK_SIZE,
3999 .maxauthsize = SHA384_DIGEST_SIZE,
4000 },
4001 .caam = {
4002 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
4003 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
4004 OP_ALG_AAI_HMAC_PRECOMP,
4005 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
4006 .geniv = true,
4007 },
4008 },
4009 {
4010 .aead = {
4011 .base = {
4012 .cra_name = "authenc(hmac(sha512),cbc(des))",
4013 .cra_driver_name = "authenc-hmac-sha512-"
4014 "cbc-des-caam",
4015 .cra_blocksize = DES_BLOCK_SIZE,
4016 },
4017 .setkey = aead_setkey,
4018 .setauthsize = aead_setauthsize,
4019 .encrypt = aead_encrypt,
4020 .decrypt = aead_decrypt,
4021 .ivsize = DES_BLOCK_SIZE,
4022 .maxauthsize = SHA512_DIGEST_SIZE,
4023 },
4024 .caam = {
4025 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
4026 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
4027 OP_ALG_AAI_HMAC_PRECOMP,
4028 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
4029 },
4030 },
4031 {
4032 .aead = {
4033 .base = {
4034 .cra_name = "echainiv(authenc(hmac(sha512),"
4035 "cbc(des)))",
4036 .cra_driver_name = "echainiv-authenc-"
4037 "hmac-sha512-cbc-des-caam",
4038 .cra_blocksize = DES_BLOCK_SIZE,
4039 },
4040 .setkey = aead_setkey,
4041 .setauthsize = aead_setauthsize,
4042 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03004043 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08004044 .ivsize = DES_BLOCK_SIZE,
4045 .maxauthsize = SHA512_DIGEST_SIZE,
4046 },
4047 .caam = {
4048 .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
4049 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
4050 OP_ALG_AAI_HMAC_PRECOMP,
4051 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
4052 .geniv = true,
4053 },
4054 },
4055 {
4056 .aead = {
4057 .base = {
4058 .cra_name = "authenc(hmac(md5),"
4059 "rfc3686(ctr(aes)))",
4060 .cra_driver_name = "authenc-hmac-md5-"
4061 "rfc3686-ctr-aes-caam",
4062 .cra_blocksize = 1,
4063 },
4064 .setkey = aead_setkey,
4065 .setauthsize = aead_setauthsize,
4066 .encrypt = aead_encrypt,
4067 .decrypt = aead_decrypt,
4068 .ivsize = CTR_RFC3686_IV_SIZE,
4069 .maxauthsize = MD5_DIGEST_SIZE,
4070 },
4071 .caam = {
4072 .class1_alg_type = OP_ALG_ALGSEL_AES |
4073 OP_ALG_AAI_CTR_MOD128,
4074 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
4075 OP_ALG_AAI_HMAC_PRECOMP,
4076 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
4077 .rfc3686 = true,
4078 },
4079 },
4080 {
4081 .aead = {
4082 .base = {
4083 .cra_name = "seqiv(authenc("
4084 "hmac(md5),rfc3686(ctr(aes))))",
4085 .cra_driver_name = "seqiv-authenc-hmac-md5-"
4086 "rfc3686-ctr-aes-caam",
4087 .cra_blocksize = 1,
4088 },
4089 .setkey = aead_setkey,
4090 .setauthsize = aead_setauthsize,
4091 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03004092 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08004093 .ivsize = CTR_RFC3686_IV_SIZE,
4094 .maxauthsize = MD5_DIGEST_SIZE,
4095 },
4096 .caam = {
4097 .class1_alg_type = OP_ALG_ALGSEL_AES |
4098 OP_ALG_AAI_CTR_MOD128,
4099 .class2_alg_type = OP_ALG_ALGSEL_MD5 |
4100 OP_ALG_AAI_HMAC_PRECOMP,
4101 .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
4102 .rfc3686 = true,
4103 .geniv = true,
4104 },
4105 },
4106 {
4107 .aead = {
4108 .base = {
4109 .cra_name = "authenc(hmac(sha1),"
4110 "rfc3686(ctr(aes)))",
4111 .cra_driver_name = "authenc-hmac-sha1-"
4112 "rfc3686-ctr-aes-caam",
4113 .cra_blocksize = 1,
4114 },
4115 .setkey = aead_setkey,
4116 .setauthsize = aead_setauthsize,
4117 .encrypt = aead_encrypt,
4118 .decrypt = aead_decrypt,
4119 .ivsize = CTR_RFC3686_IV_SIZE,
4120 .maxauthsize = SHA1_DIGEST_SIZE,
4121 },
4122 .caam = {
4123 .class1_alg_type = OP_ALG_ALGSEL_AES |
4124 OP_ALG_AAI_CTR_MOD128,
4125 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
4126 OP_ALG_AAI_HMAC_PRECOMP,
4127 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
4128 .rfc3686 = true,
4129 },
4130 },
4131 {
4132 .aead = {
4133 .base = {
4134 .cra_name = "seqiv(authenc("
4135 "hmac(sha1),rfc3686(ctr(aes))))",
4136 .cra_driver_name = "seqiv-authenc-hmac-sha1-"
4137 "rfc3686-ctr-aes-caam",
4138 .cra_blocksize = 1,
4139 },
4140 .setkey = aead_setkey,
4141 .setauthsize = aead_setauthsize,
4142 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03004143 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08004144 .ivsize = CTR_RFC3686_IV_SIZE,
4145 .maxauthsize = SHA1_DIGEST_SIZE,
4146 },
4147 .caam = {
4148 .class1_alg_type = OP_ALG_ALGSEL_AES |
4149 OP_ALG_AAI_CTR_MOD128,
4150 .class2_alg_type = OP_ALG_ALGSEL_SHA1 |
4151 OP_ALG_AAI_HMAC_PRECOMP,
4152 .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
4153 .rfc3686 = true,
4154 .geniv = true,
4155 },
4156 },
4157 {
4158 .aead = {
4159 .base = {
4160 .cra_name = "authenc(hmac(sha224),"
4161 "rfc3686(ctr(aes)))",
4162 .cra_driver_name = "authenc-hmac-sha224-"
4163 "rfc3686-ctr-aes-caam",
4164 .cra_blocksize = 1,
4165 },
4166 .setkey = aead_setkey,
4167 .setauthsize = aead_setauthsize,
4168 .encrypt = aead_encrypt,
4169 .decrypt = aead_decrypt,
4170 .ivsize = CTR_RFC3686_IV_SIZE,
4171 .maxauthsize = SHA224_DIGEST_SIZE,
4172 },
4173 .caam = {
4174 .class1_alg_type = OP_ALG_ALGSEL_AES |
4175 OP_ALG_AAI_CTR_MOD128,
4176 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
4177 OP_ALG_AAI_HMAC_PRECOMP,
4178 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
4179 .rfc3686 = true,
4180 },
4181 },
4182 {
4183 .aead = {
4184 .base = {
4185 .cra_name = "seqiv(authenc("
4186 "hmac(sha224),rfc3686(ctr(aes))))",
4187 .cra_driver_name = "seqiv-authenc-hmac-sha224-"
4188 "rfc3686-ctr-aes-caam",
4189 .cra_blocksize = 1,
4190 },
4191 .setkey = aead_setkey,
4192 .setauthsize = aead_setauthsize,
4193 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03004194 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08004195 .ivsize = CTR_RFC3686_IV_SIZE,
4196 .maxauthsize = SHA224_DIGEST_SIZE,
4197 },
4198 .caam = {
4199 .class1_alg_type = OP_ALG_ALGSEL_AES |
4200 OP_ALG_AAI_CTR_MOD128,
4201 .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
4202 OP_ALG_AAI_HMAC_PRECOMP,
4203 .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
4204 .rfc3686 = true,
4205 .geniv = true,
4206 },
4207 },
4208 {
4209 .aead = {
4210 .base = {
4211 .cra_name = "authenc(hmac(sha256),"
4212 "rfc3686(ctr(aes)))",
4213 .cra_driver_name = "authenc-hmac-sha256-"
4214 "rfc3686-ctr-aes-caam",
4215 .cra_blocksize = 1,
4216 },
4217 .setkey = aead_setkey,
4218 .setauthsize = aead_setauthsize,
4219 .encrypt = aead_encrypt,
4220 .decrypt = aead_decrypt,
4221 .ivsize = CTR_RFC3686_IV_SIZE,
4222 .maxauthsize = SHA256_DIGEST_SIZE,
4223 },
4224 .caam = {
4225 .class1_alg_type = OP_ALG_ALGSEL_AES |
4226 OP_ALG_AAI_CTR_MOD128,
4227 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
4228 OP_ALG_AAI_HMAC_PRECOMP,
4229 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
4230 .rfc3686 = true,
4231 },
4232 },
4233 {
4234 .aead = {
4235 .base = {
4236 .cra_name = "seqiv(authenc(hmac(sha256),"
4237 "rfc3686(ctr(aes))))",
4238 .cra_driver_name = "seqiv-authenc-hmac-sha256-"
4239 "rfc3686-ctr-aes-caam",
4240 .cra_blocksize = 1,
4241 },
4242 .setkey = aead_setkey,
4243 .setauthsize = aead_setauthsize,
4244 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03004245 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08004246 .ivsize = CTR_RFC3686_IV_SIZE,
4247 .maxauthsize = SHA256_DIGEST_SIZE,
4248 },
4249 .caam = {
4250 .class1_alg_type = OP_ALG_ALGSEL_AES |
4251 OP_ALG_AAI_CTR_MOD128,
4252 .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
4253 OP_ALG_AAI_HMAC_PRECOMP,
4254 .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
4255 .rfc3686 = true,
4256 .geniv = true,
4257 },
4258 },
4259 {
4260 .aead = {
4261 .base = {
4262 .cra_name = "authenc(hmac(sha384),"
4263 "rfc3686(ctr(aes)))",
4264 .cra_driver_name = "authenc-hmac-sha384-"
4265 "rfc3686-ctr-aes-caam",
4266 .cra_blocksize = 1,
4267 },
4268 .setkey = aead_setkey,
4269 .setauthsize = aead_setauthsize,
4270 .encrypt = aead_encrypt,
4271 .decrypt = aead_decrypt,
4272 .ivsize = CTR_RFC3686_IV_SIZE,
4273 .maxauthsize = SHA384_DIGEST_SIZE,
4274 },
4275 .caam = {
4276 .class1_alg_type = OP_ALG_ALGSEL_AES |
4277 OP_ALG_AAI_CTR_MOD128,
4278 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
4279 OP_ALG_AAI_HMAC_PRECOMP,
4280 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
4281 .rfc3686 = true,
4282 },
4283 },
4284 {
4285 .aead = {
4286 .base = {
4287 .cra_name = "seqiv(authenc(hmac(sha384),"
4288 "rfc3686(ctr(aes))))",
4289 .cra_driver_name = "seqiv-authenc-hmac-sha384-"
4290 "rfc3686-ctr-aes-caam",
4291 .cra_blocksize = 1,
4292 },
4293 .setkey = aead_setkey,
4294 .setauthsize = aead_setauthsize,
4295 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03004296 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08004297 .ivsize = CTR_RFC3686_IV_SIZE,
4298 .maxauthsize = SHA384_DIGEST_SIZE,
4299 },
4300 .caam = {
4301 .class1_alg_type = OP_ALG_ALGSEL_AES |
4302 OP_ALG_AAI_CTR_MOD128,
4303 .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
4304 OP_ALG_AAI_HMAC_PRECOMP,
4305 .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
4306 .rfc3686 = true,
4307 .geniv = true,
4308 },
4309 },
4310 {
4311 .aead = {
4312 .base = {
4313 .cra_name = "authenc(hmac(sha512),"
4314 "rfc3686(ctr(aes)))",
4315 .cra_driver_name = "authenc-hmac-sha512-"
4316 "rfc3686-ctr-aes-caam",
4317 .cra_blocksize = 1,
4318 },
4319 .setkey = aead_setkey,
4320 .setauthsize = aead_setauthsize,
4321 .encrypt = aead_encrypt,
4322 .decrypt = aead_decrypt,
4323 .ivsize = CTR_RFC3686_IV_SIZE,
4324 .maxauthsize = SHA512_DIGEST_SIZE,
4325 },
4326 .caam = {
4327 .class1_alg_type = OP_ALG_ALGSEL_AES |
4328 OP_ALG_AAI_CTR_MOD128,
4329 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
4330 OP_ALG_AAI_HMAC_PRECOMP,
4331 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
4332 .rfc3686 = true,
4333 },
4334 },
4335 {
4336 .aead = {
4337 .base = {
4338 .cra_name = "seqiv(authenc(hmac(sha512),"
4339 "rfc3686(ctr(aes))))",
4340 .cra_driver_name = "seqiv-authenc-hmac-sha512-"
4341 "rfc3686-ctr-aes-caam",
4342 .cra_blocksize = 1,
4343 },
4344 .setkey = aead_setkey,
4345 .setauthsize = aead_setauthsize,
4346 .encrypt = aead_encrypt,
Horia Geantă8b18e232016-08-29 14:52:14 +03004347 .decrypt = aead_decrypt,
Herbert Xu479bcc72015-07-30 17:53:17 +08004348 .ivsize = CTR_RFC3686_IV_SIZE,
4349 .maxauthsize = SHA512_DIGEST_SIZE,
4350 },
4351 .caam = {
4352 .class1_alg_type = OP_ALG_ALGSEL_AES |
4353 OP_ALG_AAI_CTR_MOD128,
4354 .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
4355 OP_ALG_AAI_HMAC_PRECOMP,
4356 .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
4357 .rfc3686 = true,
4358 .geniv = true,
4359 },
4360 },
Herbert Xuf2147b82015-06-16 13:54:23 +08004361};
4362
4363struct caam_crypto_alg {
4364 struct crypto_alg crypto_alg;
4365 struct list_head entry;
4366 struct caam_alg_entry caam;
4367};
4368
4369static int caam_init_common(struct caam_ctx *ctx, struct caam_alg_entry *caam)
4370{
4371 ctx->jrdev = caam_jr_alloc();
4372 if (IS_ERR(ctx->jrdev)) {
4373 pr_err("Job Ring Device allocation for transform failed\n");
4374 return PTR_ERR(ctx->jrdev);
4375 }
4376
4377 /* copy descriptor header template value */
4378 ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam->class1_alg_type;
4379 ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam->class2_alg_type;
4380 ctx->alg_op = OP_TYPE_CLASS2_ALG | caam->alg_op;
4381
4382 return 0;
4383}
4384
Kim Phillips8e8ec592011-03-13 16:54:26 +08004385static int caam_cra_init(struct crypto_tfm *tfm)
4386{
4387 struct crypto_alg *alg = tfm->__crt_alg;
4388 struct caam_crypto_alg *caam_alg =
4389 container_of(alg, struct caam_crypto_alg, crypto_alg);
4390 struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004391
Herbert Xuf2147b82015-06-16 13:54:23 +08004392 return caam_init_common(ctx, &caam_alg->caam);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004393}
4394
Herbert Xuf2147b82015-06-16 13:54:23 +08004395static int caam_aead_init(struct crypto_aead *tfm)
Kim Phillips8e8ec592011-03-13 16:54:26 +08004396{
Herbert Xuf2147b82015-06-16 13:54:23 +08004397 struct aead_alg *alg = crypto_aead_alg(tfm);
4398 struct caam_aead_alg *caam_alg =
4399 container_of(alg, struct caam_aead_alg, aead);
4400 struct caam_ctx *ctx = crypto_aead_ctx(tfm);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004401
Herbert Xuf2147b82015-06-16 13:54:23 +08004402 return caam_init_common(ctx, &caam_alg->caam);
4403}
4404
4405static void caam_exit_common(struct caam_ctx *ctx)
4406{
Yuan Kang1acebad2011-07-15 11:21:42 +08004407 if (ctx->sh_desc_enc_dma &&
4408 !dma_mapping_error(ctx->jrdev, ctx->sh_desc_enc_dma))
4409 dma_unmap_single(ctx->jrdev, ctx->sh_desc_enc_dma,
4410 desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE);
4411 if (ctx->sh_desc_dec_dma &&
4412 !dma_mapping_error(ctx->jrdev, ctx->sh_desc_dec_dma))
4413 dma_unmap_single(ctx->jrdev, ctx->sh_desc_dec_dma,
4414 desc_bytes(ctx->sh_desc_dec), DMA_TO_DEVICE);
4415 if (ctx->sh_desc_givenc_dma &&
4416 !dma_mapping_error(ctx->jrdev, ctx->sh_desc_givenc_dma))
4417 dma_unmap_single(ctx->jrdev, ctx->sh_desc_givenc_dma,
4418 desc_bytes(ctx->sh_desc_givenc),
Kim Phillips4427b1b2011-05-14 22:08:17 -05004419 DMA_TO_DEVICE);
Horia Geantaec31eed2014-03-14 17:48:30 +02004420 if (ctx->key_dma &&
4421 !dma_mapping_error(ctx->jrdev, ctx->key_dma))
4422 dma_unmap_single(ctx->jrdev, ctx->key_dma,
4423 ctx->enckeylen + ctx->split_key_pad_len,
4424 DMA_TO_DEVICE);
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304425
4426 caam_jr_free(ctx->jrdev);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004427}
4428
Herbert Xuf2147b82015-06-16 13:54:23 +08004429static void caam_cra_exit(struct crypto_tfm *tfm)
4430{
4431 caam_exit_common(crypto_tfm_ctx(tfm));
4432}
4433
4434static void caam_aead_exit(struct crypto_aead *tfm)
4435{
4436 caam_exit_common(crypto_aead_ctx(tfm));
4437}
4438
Kim Phillips8e8ec592011-03-13 16:54:26 +08004439static void __exit caam_algapi_exit(void)
4440{
4441
Kim Phillips8e8ec592011-03-13 16:54:26 +08004442 struct caam_crypto_alg *t_alg, *n;
Herbert Xuf2147b82015-06-16 13:54:23 +08004443 int i;
4444
4445 for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
4446 struct caam_aead_alg *t_alg = driver_aeads + i;
4447
4448 if (t_alg->registered)
4449 crypto_unregister_aead(&t_alg->aead);
4450 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08004451
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304452 if (!alg_list.next)
Kim Phillips8e8ec592011-03-13 16:54:26 +08004453 return;
4454
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304455 list_for_each_entry_safe(t_alg, n, &alg_list, entry) {
Kim Phillips8e8ec592011-03-13 16:54:26 +08004456 crypto_unregister_alg(&t_alg->crypto_alg);
4457 list_del(&t_alg->entry);
4458 kfree(t_alg);
4459 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08004460}
4461
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304462static struct caam_crypto_alg *caam_alg_alloc(struct caam_alg_template
Kim Phillips8e8ec592011-03-13 16:54:26 +08004463 *template)
4464{
4465 struct caam_crypto_alg *t_alg;
4466 struct crypto_alg *alg;
4467
Fabio Estevam9c4f9732015-08-21 13:52:00 -03004468 t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004469 if (!t_alg) {
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304470 pr_err("failed to allocate t_alg\n");
Kim Phillips8e8ec592011-03-13 16:54:26 +08004471 return ERR_PTR(-ENOMEM);
4472 }
4473
4474 alg = &t_alg->crypto_alg;
4475
4476 snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
4477 snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
4478 template->driver_name);
4479 alg->cra_module = THIS_MODULE;
4480 alg->cra_init = caam_cra_init;
4481 alg->cra_exit = caam_cra_exit;
4482 alg->cra_priority = CAAM_CRA_PRIORITY;
Kim Phillips8e8ec592011-03-13 16:54:26 +08004483 alg->cra_blocksize = template->blocksize;
4484 alg->cra_alignmask = 0;
Kim Phillips8e8ec592011-03-13 16:54:26 +08004485 alg->cra_ctxsize = sizeof(struct caam_ctx);
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01004486 alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
4487 template->type;
Yuan Kang885e9e22011-07-15 11:21:41 +08004488 switch (template->type) {
Catalin Vasile7222d1a2014-10-31 12:45:38 +02004489 case CRYPTO_ALG_TYPE_GIVCIPHER:
4490 alg->cra_type = &crypto_givcipher_type;
4491 alg->cra_ablkcipher = template->template_ablkcipher;
4492 break;
Yuan Kangacdca312011-07-15 11:21:42 +08004493 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4494 alg->cra_type = &crypto_ablkcipher_type;
4495 alg->cra_ablkcipher = template->template_ablkcipher;
4496 break;
Yuan Kang885e9e22011-07-15 11:21:41 +08004497 }
Kim Phillips8e8ec592011-03-13 16:54:26 +08004498
Herbert Xuf2147b82015-06-16 13:54:23 +08004499 t_alg->caam.class1_alg_type = template->class1_alg_type;
4500 t_alg->caam.class2_alg_type = template->class2_alg_type;
4501 t_alg->caam.alg_op = template->alg_op;
Kim Phillips8e8ec592011-03-13 16:54:26 +08004502
4503 return t_alg;
4504}
4505
Herbert Xuf2147b82015-06-16 13:54:23 +08004506static void caam_aead_alg_init(struct caam_aead_alg *t_alg)
4507{
4508 struct aead_alg *alg = &t_alg->aead;
4509
4510 alg->base.cra_module = THIS_MODULE;
4511 alg->base.cra_priority = CAAM_CRA_PRIORITY;
4512 alg->base.cra_ctxsize = sizeof(struct caam_ctx);
Herbert Xu5e4b8c12015-08-13 17:29:06 +08004513 alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
Herbert Xuf2147b82015-06-16 13:54:23 +08004514
4515 alg->init = caam_aead_init;
4516 alg->exit = caam_aead_exit;
4517}
4518
Kim Phillips8e8ec592011-03-13 16:54:26 +08004519static int __init caam_algapi_init(void)
4520{
Ruchika Gupta35af6402014-07-07 10:42:12 +05304521 struct device_node *dev_node;
4522 struct platform_device *pdev;
4523 struct device *ctrldev;
Victoria Milhoanbf834902015-08-05 11:28:48 -07004524 struct caam_drv_private *priv;
Kim Phillips8e8ec592011-03-13 16:54:26 +08004525 int i = 0, err = 0;
Victoria Milhoanbf834902015-08-05 11:28:48 -07004526 u32 cha_vid, cha_inst, des_inst, aes_inst, md_inst;
4527 unsigned int md_limit = SHA512_DIGEST_SIZE;
Herbert Xuf2147b82015-06-16 13:54:23 +08004528 bool registered = false;
Kim Phillips8e8ec592011-03-13 16:54:26 +08004529
Ruchika Gupta35af6402014-07-07 10:42:12 +05304530 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
4531 if (!dev_node) {
4532 dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
4533 if (!dev_node)
4534 return -ENODEV;
4535 }
4536
4537 pdev = of_find_device_by_node(dev_node);
4538 if (!pdev) {
4539 of_node_put(dev_node);
4540 return -ENODEV;
4541 }
4542
4543 ctrldev = &pdev->dev;
4544 priv = dev_get_drvdata(ctrldev);
4545 of_node_put(dev_node);
4546
4547 /*
4548 * If priv is NULL, it's probably because the caam driver wasn't
4549 * properly initialized (e.g. RNG4 init failed). Thus, bail out here.
4550 */
4551 if (!priv)
4552 return -ENODEV;
4553
4554
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304555 INIT_LIST_HEAD(&alg_list);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004556
Victoria Milhoanbf834902015-08-05 11:28:48 -07004557 /*
4558 * Register crypto algorithms the device supports.
4559 * First, detect presence and attributes of DES, AES, and MD blocks.
4560 */
4561 cha_vid = rd_reg32(&priv->ctrl->perfmon.cha_id_ls);
4562 cha_inst = rd_reg32(&priv->ctrl->perfmon.cha_num_ls);
4563 des_inst = (cha_inst & CHA_ID_LS_DES_MASK) >> CHA_ID_LS_DES_SHIFT;
4564 aes_inst = (cha_inst & CHA_ID_LS_AES_MASK) >> CHA_ID_LS_AES_SHIFT;
4565 md_inst = (cha_inst & CHA_ID_LS_MD_MASK) >> CHA_ID_LS_MD_SHIFT;
Kim Phillips8e8ec592011-03-13 16:54:26 +08004566
Victoria Milhoanbf834902015-08-05 11:28:48 -07004567 /* If MD is present, limit digest size based on LP256 */
4568 if (md_inst && ((cha_vid & CHA_ID_LS_MD_MASK) == CHA_ID_LS_MD_LP256))
4569 md_limit = SHA256_DIGEST_SIZE;
4570
4571 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4572 struct caam_crypto_alg *t_alg;
4573 struct caam_alg_template *alg = driver_algs + i;
4574 u32 alg_sel = alg->class1_alg_type & OP_ALG_ALGSEL_MASK;
4575
4576 /* Skip DES algorithms if not supported by device */
4577 if (!des_inst &&
4578 ((alg_sel == OP_ALG_ALGSEL_3DES) ||
4579 (alg_sel == OP_ALG_ALGSEL_DES)))
4580 continue;
4581
4582 /* Skip AES algorithms if not supported by device */
4583 if (!aes_inst && (alg_sel == OP_ALG_ALGSEL_AES))
4584 continue;
4585
4586 t_alg = caam_alg_alloc(alg);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004587 if (IS_ERR(t_alg)) {
4588 err = PTR_ERR(t_alg);
Victoria Milhoanbf834902015-08-05 11:28:48 -07004589 pr_warn("%s alg allocation failed\n", alg->driver_name);
Kim Phillips8e8ec592011-03-13 16:54:26 +08004590 continue;
4591 }
4592
4593 err = crypto_register_alg(&t_alg->crypto_alg);
4594 if (err) {
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304595 pr_warn("%s alg registration failed\n",
Kim Phillips8e8ec592011-03-13 16:54:26 +08004596 t_alg->crypto_alg.cra_driver_name);
4597 kfree(t_alg);
Herbert Xuf2147b82015-06-16 13:54:23 +08004598 continue;
4599 }
4600
4601 list_add_tail(&t_alg->entry, &alg_list);
4602 registered = true;
Kim Phillips8e8ec592011-03-13 16:54:26 +08004603 }
Herbert Xuf2147b82015-06-16 13:54:23 +08004604
4605 for (i = 0; i < ARRAY_SIZE(driver_aeads); i++) {
4606 struct caam_aead_alg *t_alg = driver_aeads + i;
Victoria Milhoanbf834902015-08-05 11:28:48 -07004607 u32 c1_alg_sel = t_alg->caam.class1_alg_type &
4608 OP_ALG_ALGSEL_MASK;
4609 u32 c2_alg_sel = t_alg->caam.class2_alg_type &
4610 OP_ALG_ALGSEL_MASK;
4611 u32 alg_aai = t_alg->caam.class1_alg_type & OP_ALG_AAI_MASK;
4612
4613 /* Skip DES algorithms if not supported by device */
4614 if (!des_inst &&
4615 ((c1_alg_sel == OP_ALG_ALGSEL_3DES) ||
4616 (c1_alg_sel == OP_ALG_ALGSEL_DES)))
4617 continue;
4618
4619 /* Skip AES algorithms if not supported by device */
4620 if (!aes_inst && (c1_alg_sel == OP_ALG_ALGSEL_AES))
4621 continue;
4622
4623 /*
4624 * Check support for AES algorithms not available
4625 * on LP devices.
4626 */
4627 if ((cha_vid & CHA_ID_LS_AES_MASK) == CHA_ID_LS_AES_LP)
4628 if (alg_aai == OP_ALG_AAI_GCM)
4629 continue;
4630
4631 /*
4632 * Skip algorithms requiring message digests
4633 * if MD or MD size is not supported by device.
4634 */
4635 if (c2_alg_sel &&
4636 (!md_inst || (t_alg->aead.maxauthsize > md_limit)))
4637 continue;
Herbert Xuf2147b82015-06-16 13:54:23 +08004638
4639 caam_aead_alg_init(t_alg);
4640
4641 err = crypto_register_aead(&t_alg->aead);
4642 if (err) {
4643 pr_warn("%s alg registration failed\n",
4644 t_alg->aead.base.cra_driver_name);
4645 continue;
4646 }
4647
4648 t_alg->registered = true;
4649 registered = true;
4650 }
4651
4652 if (registered)
Ruchika Guptacfc6f112013-10-25 12:01:03 +05304653 pr_info("caam algorithms registered in /proc/crypto\n");
Kim Phillips8e8ec592011-03-13 16:54:26 +08004654
4655 return err;
4656}
4657
4658module_init(caam_algapi_init);
4659module_exit(caam_algapi_exit);
4660
4661MODULE_LICENSE("GPL");
4662MODULE_DESCRIPTION("FSL CAAM support for crypto API");
4663MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");