Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1 | /* |
| 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 Kang | 6ec4733 | 2012-06-22 19:48:43 -0500 | [diff] [blame] | 40 | * | (output length) | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 41 | * | SEQ_IN_PTR | |
| 42 | * | (input buffer) | |
Yuan Kang | 6ec4733 | 2012-06-22 19:48:43 -0500 | [diff] [blame] | 43 | * | (input length) | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 44 | * --------------------- |
| 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 Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 54 | #include "sg_sw_sec4.h" |
Yuan Kang | 4c1ec1f | 2012-06-22 19:48:45 -0500 | [diff] [blame] | 55 | #include "key_gen.h" |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 56 | |
| 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 + \ |
| 63 | SHA512_DIGEST_SIZE * 2) |
| 64 | /* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */ |
| 65 | #define CAAM_MAX_IV_LENGTH 16 |
| 66 | |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 67 | /* length of descriptors text */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 68 | #define DESC_AEAD_BASE (4 * CAAM_CMD_SZ) |
Horia Geanta | 4464a7d | 2014-03-14 17:46:49 +0200 | [diff] [blame] | 69 | #define DESC_AEAD_ENC_LEN (DESC_AEAD_BASE + 15 * CAAM_CMD_SZ) |
| 70 | #define DESC_AEAD_DEC_LEN (DESC_AEAD_BASE + 18 * CAAM_CMD_SZ) |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 71 | #define DESC_AEAD_GIVENC_LEN (DESC_AEAD_ENC_LEN + 7 * CAAM_CMD_SZ) |
| 72 | |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 73 | #define DESC_AEAD_NULL_BASE (3 * CAAM_CMD_SZ) |
| 74 | #define DESC_AEAD_NULL_ENC_LEN (DESC_AEAD_NULL_BASE + 14 * CAAM_CMD_SZ) |
| 75 | #define DESC_AEAD_NULL_DEC_LEN (DESC_AEAD_NULL_BASE + 17 * CAAM_CMD_SZ) |
| 76 | |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 77 | #define DESC_GCM_BASE (3 * CAAM_CMD_SZ) |
| 78 | #define DESC_GCM_ENC_LEN (DESC_GCM_BASE + 23 * CAAM_CMD_SZ) |
| 79 | #define DESC_GCM_DEC_LEN (DESC_GCM_BASE + 19 * CAAM_CMD_SZ) |
| 80 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 81 | #define DESC_ABLKCIPHER_BASE (3 * CAAM_CMD_SZ) |
| 82 | #define DESC_ABLKCIPHER_ENC_LEN (DESC_ABLKCIPHER_BASE + \ |
| 83 | 20 * CAAM_CMD_SZ) |
| 84 | #define DESC_ABLKCIPHER_DEC_LEN (DESC_ABLKCIPHER_BASE + \ |
| 85 | 15 * CAAM_CMD_SZ) |
| 86 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 87 | #define DESC_MAX_USED_BYTES (DESC_AEAD_GIVENC_LEN + \ |
| 88 | CAAM_MAX_KEY_SIZE) |
| 89 | #define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ) |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 90 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 91 | #ifdef DEBUG |
| 92 | /* for print_hex_dumps with line references */ |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 93 | #define debug(format, arg...) printk(format, arg) |
| 94 | #else |
| 95 | #define debug(format, arg...) |
| 96 | #endif |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 97 | static struct list_head alg_list; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 98 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 99 | /* Set DK bit in class 1 operation if shared */ |
| 100 | static inline void append_dec_op1(u32 *desc, u32 type) |
| 101 | { |
| 102 | u32 *jump_cmd, *uncond_jump_cmd; |
| 103 | |
Horia Geanta | a60384d | 2014-07-11 15:46:58 +0300 | [diff] [blame] | 104 | /* DK bit is valid only for AES */ |
| 105 | if ((type & OP_ALG_ALGSEL_MASK) != OP_ALG_ALGSEL_AES) { |
| 106 | append_operation(desc, type | OP_ALG_AS_INITFINAL | |
| 107 | OP_ALG_DECRYPT); |
| 108 | return; |
| 109 | } |
| 110 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 111 | jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD); |
| 112 | append_operation(desc, type | OP_ALG_AS_INITFINAL | |
| 113 | OP_ALG_DECRYPT); |
| 114 | uncond_jump_cmd = append_jump(desc, JUMP_TEST_ALL); |
| 115 | set_jump_tgt_here(desc, jump_cmd); |
| 116 | append_operation(desc, type | OP_ALG_AS_INITFINAL | |
| 117 | OP_ALG_DECRYPT | OP_ALG_AAI_DK); |
| 118 | set_jump_tgt_here(desc, uncond_jump_cmd); |
| 119 | } |
| 120 | |
| 121 | /* |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 122 | * For aead functions, read payload and write payload, |
| 123 | * both of which are specified in req->src and req->dst |
| 124 | */ |
| 125 | static inline void aead_append_src_dst(u32 *desc, u32 msg_type) |
| 126 | { |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 127 | append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 128 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH | |
| 129 | KEY_VLF | msg_type | FIFOLD_TYPE_LASTBOTH); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* |
| 133 | * For aead encrypt and decrypt, read iv for both classes |
| 134 | */ |
| 135 | static inline void aead_append_ld_iv(u32 *desc, int ivsize) |
| 136 | { |
| 137 | append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT | |
| 138 | LDST_CLASS_1_CCB | ivsize); |
| 139 | append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO | ivsize); |
| 140 | } |
| 141 | |
| 142 | /* |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 143 | * For ablkcipher encrypt and decrypt, read from req->src and |
| 144 | * write to req->dst |
| 145 | */ |
| 146 | static inline void ablkcipher_append_src_dst(u32 *desc) |
| 147 | { |
Kim Phillips | 70d793c | 2012-06-22 19:42:35 -0500 | [diff] [blame] | 148 | append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ); |
| 149 | append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ); |
| 150 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | |
| 151 | KEY_VLF | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1); |
| 152 | append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 156 | * If all data, including src (with assoc and iv) or dst (with iv only) are |
| 157 | * contiguous |
| 158 | */ |
| 159 | #define GIV_SRC_CONTIG 1 |
| 160 | #define GIV_DST_CONTIG (1 << 1) |
| 161 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 162 | /* |
| 163 | * per-session context |
| 164 | */ |
| 165 | struct caam_ctx { |
| 166 | struct device *jrdev; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 167 | u32 sh_desc_enc[DESC_MAX_USED_LEN]; |
| 168 | u32 sh_desc_dec[DESC_MAX_USED_LEN]; |
| 169 | u32 sh_desc_givenc[DESC_MAX_USED_LEN]; |
| 170 | dma_addr_t sh_desc_enc_dma; |
| 171 | dma_addr_t sh_desc_dec_dma; |
| 172 | dma_addr_t sh_desc_givenc_dma; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 173 | u32 class1_alg_type; |
| 174 | u32 class2_alg_type; |
| 175 | u32 alg_op; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 176 | u8 key[CAAM_MAX_KEY_SIZE]; |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 177 | dma_addr_t key_dma; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 178 | unsigned int enckeylen; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 179 | unsigned int split_key_len; |
| 180 | unsigned int split_key_pad_len; |
| 181 | unsigned int authsize; |
| 182 | }; |
| 183 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 184 | static void append_key_aead(u32 *desc, struct caam_ctx *ctx, |
| 185 | int keys_fit_inline) |
| 186 | { |
| 187 | if (keys_fit_inline) { |
| 188 | append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len, |
| 189 | ctx->split_key_len, CLASS_2 | |
| 190 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
| 191 | append_key_as_imm(desc, (void *)ctx->key + |
| 192 | ctx->split_key_pad_len, ctx->enckeylen, |
| 193 | ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); |
| 194 | } else { |
| 195 | append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 | |
| 196 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
| 197 | append_key(desc, ctx->key_dma + ctx->split_key_pad_len, |
| 198 | ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static void init_sh_desc_key_aead(u32 *desc, struct caam_ctx *ctx, |
| 203 | int keys_fit_inline) |
| 204 | { |
| 205 | u32 *key_jump_cmd; |
| 206 | |
Kim Phillips | 61bb86b | 2012-07-13 17:49:28 -0500 | [diff] [blame] | 207 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 208 | |
| 209 | /* Skip if already shared */ |
| 210 | key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | |
| 211 | JUMP_COND_SHRD); |
| 212 | |
| 213 | append_key_aead(desc, ctx, keys_fit_inline); |
| 214 | |
| 215 | set_jump_tgt_here(desc, key_jump_cmd); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 216 | } |
| 217 | |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 218 | static int aead_null_set_sh_desc(struct crypto_aead *aead) |
| 219 | { |
| 220 | struct aead_tfm *tfm = &aead->base.crt_aead; |
| 221 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 222 | struct device *jrdev = ctx->jrdev; |
| 223 | bool keys_fit_inline = false; |
| 224 | u32 *key_jump_cmd, *jump_cmd, *read_move_cmd, *write_move_cmd; |
| 225 | u32 *desc; |
| 226 | |
| 227 | /* |
| 228 | * Job Descriptor and Shared Descriptors |
| 229 | * must all fit into the 64-word Descriptor h/w Buffer |
| 230 | */ |
| 231 | if (DESC_AEAD_NULL_ENC_LEN + DESC_JOB_IO_LEN + |
| 232 | ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX) |
| 233 | keys_fit_inline = true; |
| 234 | |
| 235 | /* aead_encrypt shared descriptor */ |
| 236 | desc = ctx->sh_desc_enc; |
| 237 | |
| 238 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
| 239 | |
| 240 | /* Skip if already shared */ |
| 241 | key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | |
| 242 | JUMP_COND_SHRD); |
| 243 | if (keys_fit_inline) |
| 244 | append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len, |
| 245 | ctx->split_key_len, CLASS_2 | |
| 246 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
| 247 | else |
| 248 | append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 | |
| 249 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
| 250 | set_jump_tgt_here(desc, key_jump_cmd); |
| 251 | |
| 252 | /* cryptlen = seqoutlen - authsize */ |
| 253 | append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize); |
| 254 | |
| 255 | /* |
| 256 | * NULL encryption; IV is zero |
| 257 | * assoclen = (assoclen + cryptlen) - cryptlen |
| 258 | */ |
| 259 | append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ); |
| 260 | |
| 261 | /* read assoc before reading payload */ |
| 262 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG | |
| 263 | KEY_VLF); |
| 264 | |
| 265 | /* Prepare to read and write cryptlen bytes */ |
| 266 | append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ); |
| 267 | append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ); |
| 268 | |
| 269 | /* |
| 270 | * MOVE_LEN opcode is not available in all SEC HW revisions, |
| 271 | * thus need to do some magic, i.e. self-patch the descriptor |
| 272 | * buffer. |
| 273 | */ |
| 274 | read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF | |
| 275 | MOVE_DEST_MATH3 | |
| 276 | (0x6 << MOVE_LEN_SHIFT)); |
| 277 | write_move_cmd = append_move(desc, MOVE_SRC_MATH3 | |
| 278 | MOVE_DEST_DESCBUF | |
| 279 | MOVE_WAITCOMP | |
| 280 | (0x8 << MOVE_LEN_SHIFT)); |
| 281 | |
| 282 | /* Class 2 operation */ |
| 283 | append_operation(desc, ctx->class2_alg_type | |
| 284 | OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT); |
| 285 | |
| 286 | /* Read and write cryptlen bytes */ |
| 287 | aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1); |
| 288 | |
| 289 | set_move_tgt_here(desc, read_move_cmd); |
| 290 | set_move_tgt_here(desc, write_move_cmd); |
| 291 | append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO); |
| 292 | append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO | |
| 293 | MOVE_AUX_LS); |
| 294 | |
| 295 | /* Write ICV */ |
| 296 | append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB | |
| 297 | LDST_SRCDST_BYTE_CONTEXT); |
| 298 | |
| 299 | ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc, |
| 300 | desc_bytes(desc), |
| 301 | DMA_TO_DEVICE); |
| 302 | if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) { |
| 303 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 304 | return -ENOMEM; |
| 305 | } |
| 306 | #ifdef DEBUG |
| 307 | print_hex_dump(KERN_ERR, |
| 308 | "aead null enc shdesc@"__stringify(__LINE__)": ", |
| 309 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 310 | desc_bytes(desc), 1); |
| 311 | #endif |
| 312 | |
| 313 | /* |
| 314 | * Job Descriptor and Shared Descriptors |
| 315 | * must all fit into the 64-word Descriptor h/w Buffer |
| 316 | */ |
Vakul Garg | 80cd88f | 2014-05-09 20:34:40 -0500 | [diff] [blame] | 317 | keys_fit_inline = false; |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 318 | if (DESC_AEAD_NULL_DEC_LEN + DESC_JOB_IO_LEN + |
| 319 | ctx->split_key_pad_len <= CAAM_DESC_BYTES_MAX) |
| 320 | keys_fit_inline = true; |
| 321 | |
| 322 | desc = ctx->sh_desc_dec; |
| 323 | |
| 324 | /* aead_decrypt shared descriptor */ |
| 325 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
| 326 | |
| 327 | /* Skip if already shared */ |
| 328 | key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | |
| 329 | JUMP_COND_SHRD); |
| 330 | if (keys_fit_inline) |
| 331 | append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len, |
| 332 | ctx->split_key_len, CLASS_2 | |
| 333 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
| 334 | else |
| 335 | append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 | |
| 336 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
| 337 | set_jump_tgt_here(desc, key_jump_cmd); |
| 338 | |
| 339 | /* Class 2 operation */ |
| 340 | append_operation(desc, ctx->class2_alg_type | |
| 341 | OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON); |
| 342 | |
| 343 | /* assoclen + cryptlen = seqinlen - ivsize - authsize */ |
| 344 | append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM, |
| 345 | ctx->authsize + tfm->ivsize); |
| 346 | /* assoclen = (assoclen + cryptlen) - cryptlen */ |
| 347 | append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ); |
| 348 | append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ); |
| 349 | |
| 350 | /* read assoc before reading payload */ |
| 351 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG | |
| 352 | KEY_VLF); |
| 353 | |
| 354 | /* Prepare to read and write cryptlen bytes */ |
| 355 | append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ); |
| 356 | append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ); |
| 357 | |
| 358 | /* |
| 359 | * MOVE_LEN opcode is not available in all SEC HW revisions, |
| 360 | * thus need to do some magic, i.e. self-patch the descriptor |
| 361 | * buffer. |
| 362 | */ |
| 363 | read_move_cmd = append_move(desc, MOVE_SRC_DESCBUF | |
| 364 | MOVE_DEST_MATH2 | |
| 365 | (0x6 << MOVE_LEN_SHIFT)); |
| 366 | write_move_cmd = append_move(desc, MOVE_SRC_MATH2 | |
| 367 | MOVE_DEST_DESCBUF | |
| 368 | MOVE_WAITCOMP | |
| 369 | (0x8 << MOVE_LEN_SHIFT)); |
| 370 | |
| 371 | /* Read and write cryptlen bytes */ |
| 372 | aead_append_src_dst(desc, FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1); |
| 373 | |
| 374 | /* |
| 375 | * Insert a NOP here, since we need at least 4 instructions between |
| 376 | * code patching the descriptor buffer and the location being patched. |
| 377 | */ |
| 378 | jump_cmd = append_jump(desc, JUMP_TEST_ALL); |
| 379 | set_jump_tgt_here(desc, jump_cmd); |
| 380 | |
| 381 | set_move_tgt_here(desc, read_move_cmd); |
| 382 | set_move_tgt_here(desc, write_move_cmd); |
| 383 | append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO); |
| 384 | append_move(desc, MOVE_SRC_INFIFO_CL | MOVE_DEST_OUTFIFO | |
| 385 | MOVE_AUX_LS); |
| 386 | append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO); |
| 387 | |
| 388 | /* Load ICV */ |
| 389 | append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 | |
| 390 | FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV); |
| 391 | |
| 392 | ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc, |
| 393 | desc_bytes(desc), |
| 394 | DMA_TO_DEVICE); |
| 395 | if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) { |
| 396 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 397 | return -ENOMEM; |
| 398 | } |
| 399 | #ifdef DEBUG |
| 400 | print_hex_dump(KERN_ERR, |
| 401 | "aead null dec shdesc@"__stringify(__LINE__)": ", |
| 402 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 403 | desc_bytes(desc), 1); |
| 404 | #endif |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 409 | static int aead_set_sh_desc(struct crypto_aead *aead) |
| 410 | { |
| 411 | struct aead_tfm *tfm = &aead->base.crt_aead; |
| 412 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 413 | struct device *jrdev = ctx->jrdev; |
Kim Phillips | 2af8f4a | 2012-09-07 04:17:03 +0800 | [diff] [blame] | 414 | bool keys_fit_inline = false; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 415 | u32 geniv, moveiv; |
| 416 | u32 *desc; |
| 417 | |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 418 | if (!ctx->authsize) |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 419 | return 0; |
| 420 | |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 421 | /* NULL encryption / decryption */ |
| 422 | if (!ctx->enckeylen) |
| 423 | return aead_null_set_sh_desc(aead); |
| 424 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 425 | /* |
| 426 | * Job Descriptor and Shared Descriptors |
| 427 | * must all fit into the 64-word Descriptor h/w Buffer |
| 428 | */ |
| 429 | if (DESC_AEAD_ENC_LEN + DESC_JOB_IO_LEN + |
| 430 | ctx->split_key_pad_len + ctx->enckeylen <= |
| 431 | CAAM_DESC_BYTES_MAX) |
Kim Phillips | 2af8f4a | 2012-09-07 04:17:03 +0800 | [diff] [blame] | 432 | keys_fit_inline = true; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 433 | |
| 434 | /* aead_encrypt shared descriptor */ |
| 435 | desc = ctx->sh_desc_enc; |
| 436 | |
| 437 | init_sh_desc_key_aead(desc, ctx, keys_fit_inline); |
| 438 | |
| 439 | /* Class 2 operation */ |
| 440 | append_operation(desc, ctx->class2_alg_type | |
| 441 | OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT); |
| 442 | |
| 443 | /* cryptlen = seqoutlen - authsize */ |
| 444 | append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize); |
| 445 | |
| 446 | /* assoclen + cryptlen = seqinlen - ivsize */ |
| 447 | append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize); |
| 448 | |
Horia Geanta | 4464a7d | 2014-03-14 17:46:49 +0200 | [diff] [blame] | 449 | /* assoclen = (assoclen + cryptlen) - cryptlen */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 450 | append_math_sub(desc, VARSEQINLEN, REG2, REG3, CAAM_CMD_SZ); |
| 451 | |
| 452 | /* read assoc before reading payload */ |
| 453 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG | |
| 454 | KEY_VLF); |
| 455 | aead_append_ld_iv(desc, tfm->ivsize); |
| 456 | |
| 457 | /* Class 1 operation */ |
| 458 | append_operation(desc, ctx->class1_alg_type | |
| 459 | OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT); |
| 460 | |
| 461 | /* Read and write cryptlen bytes */ |
| 462 | append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ); |
| 463 | append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ); |
| 464 | aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2); |
| 465 | |
| 466 | /* Write ICV */ |
| 467 | append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB | |
| 468 | LDST_SRCDST_BYTE_CONTEXT); |
| 469 | |
| 470 | ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc, |
| 471 | desc_bytes(desc), |
| 472 | DMA_TO_DEVICE); |
| 473 | if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) { |
| 474 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 475 | return -ENOMEM; |
| 476 | } |
| 477 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 478 | print_hex_dump(KERN_ERR, "aead enc shdesc@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 479 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 480 | desc_bytes(desc), 1); |
| 481 | #endif |
| 482 | |
| 483 | /* |
| 484 | * Job Descriptor and Shared Descriptors |
| 485 | * must all fit into the 64-word Descriptor h/w Buffer |
| 486 | */ |
Vakul Garg | 80cd88f | 2014-05-09 20:34:40 -0500 | [diff] [blame] | 487 | keys_fit_inline = false; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 488 | if (DESC_AEAD_DEC_LEN + DESC_JOB_IO_LEN + |
| 489 | ctx->split_key_pad_len + ctx->enckeylen <= |
| 490 | CAAM_DESC_BYTES_MAX) |
Kim Phillips | 2af8f4a | 2012-09-07 04:17:03 +0800 | [diff] [blame] | 491 | keys_fit_inline = true; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 492 | |
Horia Geanta | 4464a7d | 2014-03-14 17:46:49 +0200 | [diff] [blame] | 493 | /* aead_decrypt shared descriptor */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 494 | desc = ctx->sh_desc_dec; |
| 495 | |
Horia Geanta | 4464a7d | 2014-03-14 17:46:49 +0200 | [diff] [blame] | 496 | init_sh_desc_key_aead(desc, ctx, keys_fit_inline); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 497 | |
| 498 | /* Class 2 operation */ |
| 499 | append_operation(desc, ctx->class2_alg_type | |
| 500 | OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON); |
| 501 | |
Horia Geanta | 4464a7d | 2014-03-14 17:46:49 +0200 | [diff] [blame] | 502 | /* assoclen + cryptlen = seqinlen - ivsize - authsize */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 503 | append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM, |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 504 | ctx->authsize + tfm->ivsize); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 505 | /* assoclen = (assoclen + cryptlen) - cryptlen */ |
| 506 | append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ); |
| 507 | append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ); |
| 508 | |
| 509 | /* read assoc before reading payload */ |
| 510 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG | |
| 511 | KEY_VLF); |
| 512 | |
| 513 | aead_append_ld_iv(desc, tfm->ivsize); |
| 514 | |
| 515 | append_dec_op1(desc, ctx->class1_alg_type); |
| 516 | |
| 517 | /* Read and write cryptlen bytes */ |
| 518 | append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ); |
| 519 | append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ); |
| 520 | aead_append_src_dst(desc, FIFOLD_TYPE_MSG); |
| 521 | |
| 522 | /* Load ICV */ |
| 523 | append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 | |
| 524 | FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 525 | |
| 526 | ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc, |
| 527 | desc_bytes(desc), |
| 528 | DMA_TO_DEVICE); |
| 529 | if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) { |
| 530 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 531 | return -ENOMEM; |
| 532 | } |
| 533 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 534 | print_hex_dump(KERN_ERR, "aead dec shdesc@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 535 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 536 | desc_bytes(desc), 1); |
| 537 | #endif |
| 538 | |
| 539 | /* |
| 540 | * Job Descriptor and Shared Descriptors |
| 541 | * must all fit into the 64-word Descriptor h/w Buffer |
| 542 | */ |
Vakul Garg | 80cd88f | 2014-05-09 20:34:40 -0500 | [diff] [blame] | 543 | keys_fit_inline = false; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 544 | if (DESC_AEAD_GIVENC_LEN + DESC_JOB_IO_LEN + |
| 545 | ctx->split_key_pad_len + ctx->enckeylen <= |
| 546 | CAAM_DESC_BYTES_MAX) |
Kim Phillips | 2af8f4a | 2012-09-07 04:17:03 +0800 | [diff] [blame] | 547 | keys_fit_inline = true; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 548 | |
| 549 | /* aead_givencrypt shared descriptor */ |
| 550 | desc = ctx->sh_desc_givenc; |
| 551 | |
| 552 | init_sh_desc_key_aead(desc, ctx, keys_fit_inline); |
| 553 | |
| 554 | /* Generate IV */ |
| 555 | geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO | |
| 556 | NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 | |
| 557 | NFIFOENTRY_PTYPE_RND | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT); |
| 558 | append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB | |
| 559 | LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM); |
| 560 | append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO); |
| 561 | append_move(desc, MOVE_SRC_INFIFO | |
| 562 | MOVE_DEST_CLASS1CTX | (tfm->ivsize << MOVE_LEN_SHIFT)); |
| 563 | append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO); |
| 564 | |
| 565 | /* Copy IV to class 1 context */ |
| 566 | append_move(desc, MOVE_SRC_CLASS1CTX | |
| 567 | MOVE_DEST_OUTFIFO | (tfm->ivsize << MOVE_LEN_SHIFT)); |
| 568 | |
| 569 | /* Return to encryption */ |
| 570 | append_operation(desc, ctx->class2_alg_type | |
| 571 | OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT); |
| 572 | |
| 573 | /* ivsize + cryptlen = seqoutlen - authsize */ |
| 574 | append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize); |
| 575 | |
| 576 | /* assoclen = seqinlen - (ivsize + cryptlen) */ |
| 577 | append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ); |
| 578 | |
| 579 | /* read assoc before reading payload */ |
| 580 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG | |
| 581 | KEY_VLF); |
| 582 | |
| 583 | /* Copy iv from class 1 ctx to class 2 fifo*/ |
| 584 | moveiv = NFIFOENTRY_STYPE_OFIFO | NFIFOENTRY_DEST_CLASS2 | |
| 585 | NFIFOENTRY_DTYPE_MSG | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT); |
| 586 | append_load_imm_u32(desc, moveiv, LDST_CLASS_IND_CCB | |
| 587 | LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM); |
| 588 | append_load_imm_u32(desc, tfm->ivsize, LDST_CLASS_2_CCB | |
| 589 | LDST_SRCDST_WORD_DATASZ_REG | LDST_IMM); |
| 590 | |
| 591 | /* Class 1 operation */ |
| 592 | append_operation(desc, ctx->class1_alg_type | |
| 593 | OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT); |
| 594 | |
| 595 | /* Will write ivsize + cryptlen */ |
| 596 | append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ); |
| 597 | |
| 598 | /* Not need to reload iv */ |
| 599 | append_seq_fifo_load(desc, tfm->ivsize, |
| 600 | FIFOLD_CLASS_SKIP); |
| 601 | |
| 602 | /* Will read cryptlen */ |
| 603 | append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ); |
| 604 | aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2); |
| 605 | |
| 606 | /* Write ICV */ |
| 607 | append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB | |
| 608 | LDST_SRCDST_BYTE_CONTEXT); |
| 609 | |
| 610 | ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc, |
| 611 | desc_bytes(desc), |
| 612 | DMA_TO_DEVICE); |
| 613 | if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) { |
| 614 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 615 | return -ENOMEM; |
| 616 | } |
| 617 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 618 | print_hex_dump(KERN_ERR, "aead givenc shdesc@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 619 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 620 | desc_bytes(desc), 1); |
| 621 | #endif |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 626 | static int aead_setauthsize(struct crypto_aead *authenc, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 627 | unsigned int authsize) |
| 628 | { |
| 629 | struct caam_ctx *ctx = crypto_aead_ctx(authenc); |
| 630 | |
| 631 | ctx->authsize = authsize; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 632 | aead_set_sh_desc(authenc); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 637 | static int gcm_set_sh_desc(struct crypto_aead *aead) |
| 638 | { |
| 639 | struct aead_tfm *tfm = &aead->base.crt_aead; |
| 640 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 641 | struct device *jrdev = ctx->jrdev; |
| 642 | bool keys_fit_inline = false; |
| 643 | u32 *key_jump_cmd, *zero_payload_jump_cmd, |
| 644 | *zero_assoc_jump_cmd1, *zero_assoc_jump_cmd2; |
| 645 | u32 *desc; |
| 646 | |
| 647 | if (!ctx->enckeylen || !ctx->authsize) |
| 648 | return 0; |
| 649 | |
| 650 | /* |
| 651 | * AES GCM encrypt shared descriptor |
| 652 | * Job Descriptor and Shared Descriptor |
| 653 | * must fit into the 64-word Descriptor h/w Buffer |
| 654 | */ |
| 655 | if (DESC_GCM_ENC_LEN + DESC_JOB_IO_LEN + |
| 656 | ctx->enckeylen <= CAAM_DESC_BYTES_MAX) |
| 657 | keys_fit_inline = true; |
| 658 | |
| 659 | desc = ctx->sh_desc_enc; |
| 660 | |
| 661 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
| 662 | |
| 663 | /* skip key loading if they are loaded due to sharing */ |
| 664 | key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | |
| 665 | JUMP_COND_SHRD | JUMP_COND_SELF); |
| 666 | if (keys_fit_inline) |
| 667 | append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen, |
| 668 | ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); |
| 669 | else |
| 670 | append_key(desc, ctx->key_dma, ctx->enckeylen, |
| 671 | CLASS_1 | KEY_DEST_CLASS_REG); |
| 672 | set_jump_tgt_here(desc, key_jump_cmd); |
| 673 | |
| 674 | /* class 1 operation */ |
| 675 | append_operation(desc, ctx->class1_alg_type | |
| 676 | OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT); |
| 677 | |
| 678 | /* cryptlen = seqoutlen - authsize */ |
| 679 | append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize); |
| 680 | |
| 681 | /* assoclen + cryptlen = seqinlen - ivsize */ |
| 682 | append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize); |
| 683 | |
| 684 | /* assoclen = (assoclen + cryptlen) - cryptlen */ |
| 685 | append_math_sub(desc, REG1, REG2, REG3, CAAM_CMD_SZ); |
| 686 | |
| 687 | /* if cryptlen is ZERO jump to zero-payload commands */ |
| 688 | append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ); |
| 689 | zero_payload_jump_cmd = append_jump(desc, JUMP_TEST_ALL | |
| 690 | JUMP_COND_MATH_Z); |
| 691 | /* read IV */ |
| 692 | append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 | |
| 693 | FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1); |
| 694 | |
| 695 | /* if assoclen is ZERO, skip reading the assoc data */ |
| 696 | append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ); |
| 697 | zero_assoc_jump_cmd1 = append_jump(desc, JUMP_TEST_ALL | |
| 698 | JUMP_COND_MATH_Z); |
| 699 | |
| 700 | /* read assoc data */ |
| 701 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF | |
| 702 | FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1); |
| 703 | set_jump_tgt_here(desc, zero_assoc_jump_cmd1); |
| 704 | |
| 705 | append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ); |
| 706 | |
| 707 | /* write encrypted data */ |
| 708 | append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF); |
| 709 | |
| 710 | /* read payload data */ |
| 711 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF | |
| 712 | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1); |
| 713 | |
| 714 | /* jump the zero-payload commands */ |
| 715 | append_jump(desc, JUMP_TEST_ALL | 7); |
| 716 | |
| 717 | /* zero-payload commands */ |
| 718 | set_jump_tgt_here(desc, zero_payload_jump_cmd); |
| 719 | |
| 720 | /* if assoclen is ZERO, jump to IV reading - is the only input data */ |
| 721 | append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ); |
| 722 | zero_assoc_jump_cmd2 = append_jump(desc, JUMP_TEST_ALL | |
| 723 | JUMP_COND_MATH_Z); |
| 724 | /* read IV */ |
| 725 | append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 | |
| 726 | FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1); |
| 727 | |
| 728 | /* read assoc data */ |
| 729 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF | |
| 730 | FIFOLD_TYPE_AAD | FIFOLD_TYPE_LAST1); |
| 731 | |
| 732 | /* jump to ICV writing */ |
| 733 | append_jump(desc, JUMP_TEST_ALL | 2); |
| 734 | |
| 735 | /* read IV - is the only input data */ |
| 736 | set_jump_tgt_here(desc, zero_assoc_jump_cmd2); |
| 737 | append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 | |
| 738 | FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1 | |
| 739 | FIFOLD_TYPE_LAST1); |
| 740 | |
| 741 | /* write ICV */ |
| 742 | append_seq_store(desc, ctx->authsize, LDST_CLASS_1_CCB | |
| 743 | LDST_SRCDST_BYTE_CONTEXT); |
| 744 | |
| 745 | ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc, |
| 746 | desc_bytes(desc), |
| 747 | DMA_TO_DEVICE); |
| 748 | if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) { |
| 749 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 750 | return -ENOMEM; |
| 751 | } |
| 752 | #ifdef DEBUG |
| 753 | print_hex_dump(KERN_ERR, "gcm enc shdesc@"__stringify(__LINE__)": ", |
| 754 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 755 | desc_bytes(desc), 1); |
| 756 | #endif |
| 757 | |
| 758 | /* |
| 759 | * Job Descriptor and Shared Descriptors |
| 760 | * must all fit into the 64-word Descriptor h/w Buffer |
| 761 | */ |
| 762 | keys_fit_inline = false; |
| 763 | if (DESC_GCM_DEC_LEN + DESC_JOB_IO_LEN + |
| 764 | ctx->enckeylen <= CAAM_DESC_BYTES_MAX) |
| 765 | keys_fit_inline = true; |
| 766 | |
| 767 | desc = ctx->sh_desc_dec; |
| 768 | |
| 769 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
| 770 | |
| 771 | /* skip key loading if they are loaded due to sharing */ |
| 772 | key_jump_cmd = append_jump(desc, JUMP_JSL | |
| 773 | JUMP_TEST_ALL | JUMP_COND_SHRD | |
| 774 | JUMP_COND_SELF); |
| 775 | if (keys_fit_inline) |
| 776 | append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen, |
| 777 | ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); |
| 778 | else |
| 779 | append_key(desc, ctx->key_dma, ctx->enckeylen, |
| 780 | CLASS_1 | KEY_DEST_CLASS_REG); |
| 781 | set_jump_tgt_here(desc, key_jump_cmd); |
| 782 | |
| 783 | /* class 1 operation */ |
| 784 | append_operation(desc, ctx->class1_alg_type | |
| 785 | OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON); |
| 786 | |
| 787 | /* assoclen + cryptlen = seqinlen - ivsize - icvsize */ |
| 788 | append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM, |
| 789 | ctx->authsize + tfm->ivsize); |
| 790 | |
| 791 | /* assoclen = (assoclen + cryptlen) - cryptlen */ |
| 792 | append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ); |
| 793 | append_math_sub(desc, REG1, REG3, REG2, CAAM_CMD_SZ); |
| 794 | |
| 795 | /* read IV */ |
| 796 | append_seq_fifo_load(desc, tfm->ivsize, FIFOLD_CLASS_CLASS1 | |
| 797 | FIFOLD_TYPE_IV | FIFOLD_TYPE_FLUSH1); |
| 798 | |
| 799 | /* jump to zero-payload command if cryptlen is zero */ |
| 800 | append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ); |
| 801 | zero_payload_jump_cmd = append_jump(desc, JUMP_TEST_ALL | |
| 802 | JUMP_COND_MATH_Z); |
| 803 | |
| 804 | append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ); |
| 805 | /* if asoclen is ZERO, skip reading assoc data */ |
| 806 | zero_assoc_jump_cmd1 = append_jump(desc, JUMP_TEST_ALL | |
| 807 | JUMP_COND_MATH_Z); |
| 808 | /* read assoc data */ |
| 809 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF | |
| 810 | FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1); |
| 811 | set_jump_tgt_here(desc, zero_assoc_jump_cmd1); |
| 812 | |
| 813 | append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ); |
| 814 | |
| 815 | /* store encrypted data */ |
| 816 | append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | FIFOLDST_VLF); |
| 817 | |
| 818 | /* read payload data */ |
| 819 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF | |
| 820 | FIFOLD_TYPE_MSG | FIFOLD_TYPE_FLUSH1); |
| 821 | |
| 822 | /* jump the zero-payload commands */ |
| 823 | append_jump(desc, JUMP_TEST_ALL | 4); |
| 824 | |
| 825 | /* zero-payload command */ |
| 826 | set_jump_tgt_here(desc, zero_payload_jump_cmd); |
| 827 | |
| 828 | /* if assoclen is ZERO, jump to ICV reading */ |
| 829 | append_math_add(desc, VARSEQINLEN, ZERO, REG1, CAAM_CMD_SZ); |
| 830 | zero_assoc_jump_cmd2 = append_jump(desc, JUMP_TEST_ALL | |
| 831 | JUMP_COND_MATH_Z); |
| 832 | /* read assoc data */ |
| 833 | append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 | FIFOLDST_VLF | |
| 834 | FIFOLD_TYPE_AAD | FIFOLD_TYPE_FLUSH1); |
| 835 | set_jump_tgt_here(desc, zero_assoc_jump_cmd2); |
| 836 | |
| 837 | /* read ICV */ |
| 838 | append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS1 | |
| 839 | FIFOLD_TYPE_ICV | FIFOLD_TYPE_LAST1); |
| 840 | |
| 841 | ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc, |
| 842 | desc_bytes(desc), |
| 843 | DMA_TO_DEVICE); |
| 844 | if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) { |
| 845 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 846 | return -ENOMEM; |
| 847 | } |
| 848 | #ifdef DEBUG |
| 849 | print_hex_dump(KERN_ERR, "gcm dec shdesc@"__stringify(__LINE__)": ", |
| 850 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 851 | desc_bytes(desc), 1); |
| 852 | #endif |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static int gcm_setauthsize(struct crypto_aead *authenc, unsigned int authsize) |
| 858 | { |
| 859 | struct caam_ctx *ctx = crypto_aead_ctx(authenc); |
| 860 | |
| 861 | ctx->authsize = authsize; |
| 862 | gcm_set_sh_desc(authenc); |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
Yuan Kang | 4c1ec1f | 2012-06-22 19:48:45 -0500 | [diff] [blame] | 867 | static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in, |
| 868 | u32 authkeylen) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 869 | { |
Yuan Kang | 4c1ec1f | 2012-06-22 19:48:45 -0500 | [diff] [blame] | 870 | return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len, |
| 871 | ctx->split_key_pad_len, key_in, authkeylen, |
| 872 | ctx->alg_op); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 873 | } |
| 874 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 875 | static int aead_setkey(struct crypto_aead *aead, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 876 | const u8 *key, unsigned int keylen) |
| 877 | { |
| 878 | /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */ |
| 879 | static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 }; |
| 880 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 881 | struct device *jrdev = ctx->jrdev; |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 882 | struct crypto_authenc_keys keys; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 883 | int ret = 0; |
| 884 | |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 885 | if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 886 | goto badkey; |
| 887 | |
| 888 | /* Pick class 2 key length from algorithm submask */ |
| 889 | ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >> |
| 890 | OP_ALG_ALGSEL_SHIFT] * 2; |
| 891 | ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16); |
| 892 | |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 893 | if (ctx->split_key_pad_len + keys.enckeylen > CAAM_MAX_KEY_SIZE) |
| 894 | goto badkey; |
| 895 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 896 | #ifdef DEBUG |
| 897 | printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n", |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 898 | keys.authkeylen + keys.enckeylen, keys.enckeylen, |
| 899 | keys.authkeylen); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 900 | printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n", |
| 901 | ctx->split_key_len, ctx->split_key_pad_len); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 902 | print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 903 | DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1); |
| 904 | #endif |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 905 | |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 906 | ret = gen_split_aead_key(ctx, keys.authkey, keys.authkeylen); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 907 | if (ret) { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 908 | goto badkey; |
| 909 | } |
| 910 | |
| 911 | /* postpend encryption key to auth split key */ |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 912 | memcpy(ctx->key + ctx->split_key_pad_len, keys.enckey, keys.enckeylen); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 913 | |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 914 | ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len + |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 915 | keys.enckeylen, DMA_TO_DEVICE); |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 916 | if (dma_mapping_error(jrdev, ctx->key_dma)) { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 917 | dev_err(jrdev, "unable to map key i/o memory\n"); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 918 | return -ENOMEM; |
| 919 | } |
| 920 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 921 | print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 922 | DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 923 | ctx->split_key_pad_len + keys.enckeylen, 1); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 924 | #endif |
| 925 | |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 926 | ctx->enckeylen = keys.enckeylen; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 927 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 928 | ret = aead_set_sh_desc(aead); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 929 | if (ret) { |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 930 | dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len + |
Horia Geanta | 4e6e0b2 | 2013-12-19 17:27:35 +0200 | [diff] [blame] | 931 | keys.enckeylen, DMA_TO_DEVICE); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | return ret; |
| 935 | badkey: |
| 936 | crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 937 | return -EINVAL; |
| 938 | } |
| 939 | |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 940 | static int gcm_setkey(struct crypto_aead *aead, |
| 941 | const u8 *key, unsigned int keylen) |
| 942 | { |
| 943 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 944 | struct device *jrdev = ctx->jrdev; |
| 945 | int ret = 0; |
| 946 | |
| 947 | #ifdef DEBUG |
| 948 | print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ", |
| 949 | DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1); |
| 950 | #endif |
| 951 | |
| 952 | memcpy(ctx->key, key, keylen); |
| 953 | ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen, |
| 954 | DMA_TO_DEVICE); |
| 955 | if (dma_mapping_error(jrdev, ctx->key_dma)) { |
| 956 | dev_err(jrdev, "unable to map key i/o memory\n"); |
| 957 | return -ENOMEM; |
| 958 | } |
| 959 | ctx->enckeylen = keylen; |
| 960 | |
| 961 | ret = gcm_set_sh_desc(aead); |
| 962 | if (ret) { |
| 963 | dma_unmap_single(jrdev, ctx->key_dma, ctx->enckeylen, |
| 964 | DMA_TO_DEVICE); |
| 965 | } |
| 966 | |
| 967 | return ret; |
| 968 | } |
| 969 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 970 | static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher, |
| 971 | const u8 *key, unsigned int keylen) |
| 972 | { |
| 973 | struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher); |
| 974 | struct ablkcipher_tfm *tfm = &ablkcipher->base.crt_ablkcipher; |
| 975 | struct device *jrdev = ctx->jrdev; |
| 976 | int ret = 0; |
Horia Geanta | 4464a7d | 2014-03-14 17:46:49 +0200 | [diff] [blame] | 977 | u32 *key_jump_cmd; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 978 | u32 *desc; |
| 979 | |
| 980 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 981 | print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 982 | DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1); |
| 983 | #endif |
| 984 | |
| 985 | memcpy(ctx->key, key, keylen); |
| 986 | ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen, |
| 987 | DMA_TO_DEVICE); |
| 988 | if (dma_mapping_error(jrdev, ctx->key_dma)) { |
| 989 | dev_err(jrdev, "unable to map key i/o memory\n"); |
| 990 | return -ENOMEM; |
| 991 | } |
| 992 | ctx->enckeylen = keylen; |
| 993 | |
| 994 | /* ablkcipher_encrypt shared descriptor */ |
| 995 | desc = ctx->sh_desc_enc; |
Kim Phillips | 61bb86b | 2012-07-13 17:49:28 -0500 | [diff] [blame] | 996 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 997 | /* Skip if already shared */ |
| 998 | key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | |
| 999 | JUMP_COND_SHRD); |
| 1000 | |
| 1001 | /* Load class1 key only */ |
| 1002 | append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen, |
| 1003 | ctx->enckeylen, CLASS_1 | |
| 1004 | KEY_DEST_CLASS_REG); |
| 1005 | |
| 1006 | set_jump_tgt_here(desc, key_jump_cmd); |
| 1007 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1008 | /* Load iv */ |
| 1009 | append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT | |
| 1010 | LDST_CLASS_1_CCB | tfm->ivsize); |
| 1011 | |
| 1012 | /* Load operation */ |
| 1013 | append_operation(desc, ctx->class1_alg_type | |
| 1014 | OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT); |
| 1015 | |
| 1016 | /* Perform operation */ |
| 1017 | ablkcipher_append_src_dst(desc); |
| 1018 | |
| 1019 | ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc, |
| 1020 | desc_bytes(desc), |
| 1021 | DMA_TO_DEVICE); |
| 1022 | if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) { |
| 1023 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 1024 | return -ENOMEM; |
| 1025 | } |
| 1026 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1027 | print_hex_dump(KERN_ERR, |
| 1028 | "ablkcipher enc shdesc@"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1029 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 1030 | desc_bytes(desc), 1); |
| 1031 | #endif |
| 1032 | /* ablkcipher_decrypt shared descriptor */ |
| 1033 | desc = ctx->sh_desc_dec; |
| 1034 | |
Kim Phillips | 61bb86b | 2012-07-13 17:49:28 -0500 | [diff] [blame] | 1035 | init_sh_desc(desc, HDR_SHARE_SERIAL); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1036 | /* Skip if already shared */ |
| 1037 | key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | |
| 1038 | JUMP_COND_SHRD); |
| 1039 | |
| 1040 | /* Load class1 key only */ |
| 1041 | append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen, |
| 1042 | ctx->enckeylen, CLASS_1 | |
| 1043 | KEY_DEST_CLASS_REG); |
| 1044 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1045 | set_jump_tgt_here(desc, key_jump_cmd); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1046 | |
| 1047 | /* load IV */ |
| 1048 | append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT | |
| 1049 | LDST_CLASS_1_CCB | tfm->ivsize); |
| 1050 | |
| 1051 | /* Choose operation */ |
| 1052 | append_dec_op1(desc, ctx->class1_alg_type); |
| 1053 | |
| 1054 | /* Perform operation */ |
| 1055 | ablkcipher_append_src_dst(desc); |
| 1056 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1057 | ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc, |
| 1058 | desc_bytes(desc), |
| 1059 | DMA_TO_DEVICE); |
Horia Geanta | 71c65f7 | 2014-07-11 15:34:48 +0300 | [diff] [blame] | 1060 | if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) { |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1061 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 1062 | return -ENOMEM; |
| 1063 | } |
| 1064 | |
| 1065 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1066 | print_hex_dump(KERN_ERR, |
| 1067 | "ablkcipher dec shdesc@"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1068 | DUMP_PREFIX_ADDRESS, 16, 4, desc, |
| 1069 | desc_bytes(desc), 1); |
| 1070 | #endif |
| 1071 | |
| 1072 | return ret; |
| 1073 | } |
| 1074 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1075 | /* |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1076 | * aead_edesc - s/w-extended aead descriptor |
| 1077 | * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1078 | * @assoc_chained: if source is chained |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1079 | * @src_nents: number of segments in input scatterlist |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1080 | * @src_chained: if source is chained |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1081 | * @dst_nents: number of segments in output scatterlist |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1082 | * @dst_chained: if destination is chained |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1083 | * @iv_dma: dma address of iv for checking continuity and link table |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1084 | * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE) |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1085 | * @sec4_sg_bytes: length of dma mapped sec4_sg space |
| 1086 | * @sec4_sg_dma: bus physical mapped address of h/w link table |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1087 | * @hw_desc: the h/w job descriptor followed by any referenced link tables |
| 1088 | */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1089 | struct aead_edesc { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1090 | int assoc_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1091 | bool assoc_chained; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1092 | int src_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1093 | bool src_chained; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1094 | int dst_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1095 | bool dst_chained; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1096 | dma_addr_t iv_dma; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1097 | int sec4_sg_bytes; |
| 1098 | dma_addr_t sec4_sg_dma; |
| 1099 | struct sec4_sg_entry *sec4_sg; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1100 | u32 hw_desc[0]; |
| 1101 | }; |
| 1102 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1103 | /* |
| 1104 | * ablkcipher_edesc - s/w-extended ablkcipher descriptor |
| 1105 | * @src_nents: number of segments in input scatterlist |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1106 | * @src_chained: if source is chained |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1107 | * @dst_nents: number of segments in output scatterlist |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1108 | * @dst_chained: if destination is chained |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1109 | * @iv_dma: dma address of iv for checking continuity and link table |
| 1110 | * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE) |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1111 | * @sec4_sg_bytes: length of dma mapped sec4_sg space |
| 1112 | * @sec4_sg_dma: bus physical mapped address of h/w link table |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1113 | * @hw_desc: the h/w job descriptor followed by any referenced link tables |
| 1114 | */ |
| 1115 | struct ablkcipher_edesc { |
| 1116 | int src_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1117 | bool src_chained; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1118 | int dst_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1119 | bool dst_chained; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1120 | dma_addr_t iv_dma; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1121 | int sec4_sg_bytes; |
| 1122 | dma_addr_t sec4_sg_dma; |
| 1123 | struct sec4_sg_entry *sec4_sg; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1124 | u32 hw_desc[0]; |
| 1125 | }; |
| 1126 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1127 | static void caam_unmap(struct device *dev, struct scatterlist *src, |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1128 | struct scatterlist *dst, int src_nents, |
| 1129 | bool src_chained, int dst_nents, bool dst_chained, |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1130 | dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma, |
| 1131 | int sec4_sg_bytes) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1132 | { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1133 | if (dst != src) { |
| 1134 | dma_unmap_sg_chained(dev, src, src_nents ? : 1, DMA_TO_DEVICE, |
| 1135 | src_chained); |
| 1136 | dma_unmap_sg_chained(dev, dst, dst_nents ? : 1, DMA_FROM_DEVICE, |
| 1137 | dst_chained); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1138 | } else { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1139 | dma_unmap_sg_chained(dev, src, src_nents ? : 1, |
| 1140 | DMA_BIDIRECTIONAL, src_chained); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1143 | if (iv_dma) |
| 1144 | dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE); |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1145 | if (sec4_sg_bytes) |
| 1146 | dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1147 | DMA_TO_DEVICE); |
| 1148 | } |
| 1149 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1150 | static void aead_unmap(struct device *dev, |
| 1151 | struct aead_edesc *edesc, |
| 1152 | struct aead_request *req) |
| 1153 | { |
| 1154 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 1155 | int ivsize = crypto_aead_ivsize(aead); |
| 1156 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1157 | dma_unmap_sg_chained(dev, req->assoc, edesc->assoc_nents, |
| 1158 | DMA_TO_DEVICE, edesc->assoc_chained); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1159 | |
| 1160 | caam_unmap(dev, req->src, req->dst, |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1161 | edesc->src_nents, edesc->src_chained, edesc->dst_nents, |
| 1162 | edesc->dst_chained, edesc->iv_dma, ivsize, |
| 1163 | edesc->sec4_sg_dma, edesc->sec4_sg_bytes); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1166 | static void ablkcipher_unmap(struct device *dev, |
| 1167 | struct ablkcipher_edesc *edesc, |
| 1168 | struct ablkcipher_request *req) |
| 1169 | { |
| 1170 | struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); |
| 1171 | int ivsize = crypto_ablkcipher_ivsize(ablkcipher); |
| 1172 | |
| 1173 | caam_unmap(dev, req->src, req->dst, |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1174 | edesc->src_nents, edesc->src_chained, edesc->dst_nents, |
| 1175 | edesc->dst_chained, edesc->iv_dma, ivsize, |
| 1176 | edesc->sec4_sg_dma, edesc->sec4_sg_bytes); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1179 | static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1180 | void *context) |
| 1181 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1182 | struct aead_request *req = context; |
| 1183 | struct aead_edesc *edesc; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1184 | #ifdef DEBUG |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1185 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1186 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1187 | int ivsize = crypto_aead_ivsize(aead); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1188 | |
| 1189 | dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); |
| 1190 | #endif |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1191 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1192 | edesc = (struct aead_edesc *)((char *)desc - |
| 1193 | offsetof(struct aead_edesc, hw_desc)); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1194 | |
Marek Vasut | fa9659c | 2014-04-24 20:05:12 +0200 | [diff] [blame] | 1195 | if (err) |
| 1196 | caam_jr_strstatus(jrdev, err); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1197 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1198 | aead_unmap(jrdev, edesc, req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1199 | |
| 1200 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1201 | print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1202 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc), |
| 1203 | req->assoclen , 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1204 | print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1205 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1206 | edesc->src_nents ? 100 : ivsize, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1207 | print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1208 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 1209 | edesc->src_nents ? 100 : req->cryptlen + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1210 | ctx->authsize + 4, 1); |
| 1211 | #endif |
| 1212 | |
| 1213 | kfree(edesc); |
| 1214 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1215 | aead_request_complete(req, err); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1216 | } |
| 1217 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1218 | static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1219 | void *context) |
| 1220 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1221 | struct aead_request *req = context; |
| 1222 | struct aead_edesc *edesc; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1223 | #ifdef DEBUG |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1224 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1225 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1226 | int ivsize = crypto_aead_ivsize(aead); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1227 | |
| 1228 | dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); |
| 1229 | #endif |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1230 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1231 | edesc = (struct aead_edesc *)((char *)desc - |
| 1232 | offsetof(struct aead_edesc, hw_desc)); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1233 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1234 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1235 | print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1236 | DUMP_PREFIX_ADDRESS, 16, 4, req->iv, |
| 1237 | ivsize, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1238 | print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1239 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->dst), |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1240 | req->cryptlen - ctx->authsize, 1); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1241 | #endif |
| 1242 | |
Marek Vasut | fa9659c | 2014-04-24 20:05:12 +0200 | [diff] [blame] | 1243 | if (err) |
| 1244 | caam_jr_strstatus(jrdev, err); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1245 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1246 | aead_unmap(jrdev, edesc, req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1247 | |
| 1248 | /* |
| 1249 | * verify hw auth check passed else return -EBADMSG |
| 1250 | */ |
| 1251 | if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK) |
| 1252 | err = -EBADMSG; |
| 1253 | |
| 1254 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1255 | print_hex_dump(KERN_ERR, "iphdrout@"__stringify(__LINE__)": ", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1256 | DUMP_PREFIX_ADDRESS, 16, 4, |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1257 | ((char *)sg_virt(req->assoc) - sizeof(struct iphdr)), |
| 1258 | sizeof(struct iphdr) + req->assoclen + |
| 1259 | ((req->cryptlen > 1500) ? 1500 : req->cryptlen) + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1260 | ctx->authsize + 36, 1); |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1261 | if (!err && edesc->sec4_sg_bytes) { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1262 | struct scatterlist *sg = sg_last(req->src, edesc->src_nents); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1263 | print_hex_dump(KERN_ERR, "sglastout@"__stringify(__LINE__)": ", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1264 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(sg), |
| 1265 | sg->length + ctx->authsize + 16, 1); |
| 1266 | } |
| 1267 | #endif |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1268 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1269 | kfree(edesc); |
| 1270 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1271 | aead_request_complete(req, err); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1274 | static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err, |
| 1275 | void *context) |
| 1276 | { |
| 1277 | struct ablkcipher_request *req = context; |
| 1278 | struct ablkcipher_edesc *edesc; |
| 1279 | #ifdef DEBUG |
| 1280 | struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); |
| 1281 | int ivsize = crypto_ablkcipher_ivsize(ablkcipher); |
| 1282 | |
| 1283 | dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); |
| 1284 | #endif |
| 1285 | |
| 1286 | edesc = (struct ablkcipher_edesc *)((char *)desc - |
| 1287 | offsetof(struct ablkcipher_edesc, hw_desc)); |
| 1288 | |
Marek Vasut | fa9659c | 2014-04-24 20:05:12 +0200 | [diff] [blame] | 1289 | if (err) |
| 1290 | caam_jr_strstatus(jrdev, err); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1291 | |
| 1292 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1293 | print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1294 | DUMP_PREFIX_ADDRESS, 16, 4, req->info, |
| 1295 | edesc->src_nents > 1 ? 100 : ivsize, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1296 | print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1297 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 1298 | edesc->dst_nents > 1 ? 100 : req->nbytes, 1); |
| 1299 | #endif |
| 1300 | |
| 1301 | ablkcipher_unmap(jrdev, edesc, req); |
| 1302 | kfree(edesc); |
| 1303 | |
| 1304 | ablkcipher_request_complete(req, err); |
| 1305 | } |
| 1306 | |
| 1307 | static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err, |
| 1308 | void *context) |
| 1309 | { |
| 1310 | struct ablkcipher_request *req = context; |
| 1311 | struct ablkcipher_edesc *edesc; |
| 1312 | #ifdef DEBUG |
| 1313 | struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); |
| 1314 | int ivsize = crypto_ablkcipher_ivsize(ablkcipher); |
| 1315 | |
| 1316 | dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); |
| 1317 | #endif |
| 1318 | |
| 1319 | edesc = (struct ablkcipher_edesc *)((char *)desc - |
| 1320 | offsetof(struct ablkcipher_edesc, hw_desc)); |
Marek Vasut | fa9659c | 2014-04-24 20:05:12 +0200 | [diff] [blame] | 1321 | if (err) |
| 1322 | caam_jr_strstatus(jrdev, err); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1323 | |
| 1324 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1325 | print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1326 | DUMP_PREFIX_ADDRESS, 16, 4, req->info, |
| 1327 | ivsize, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1328 | print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1329 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 1330 | edesc->dst_nents > 1 ? 100 : req->nbytes, 1); |
| 1331 | #endif |
| 1332 | |
| 1333 | ablkcipher_unmap(jrdev, edesc, req); |
| 1334 | kfree(edesc); |
| 1335 | |
| 1336 | ablkcipher_request_complete(req, err); |
| 1337 | } |
| 1338 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1339 | /* |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1340 | * Fill in aead job descriptor |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1341 | */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1342 | static void init_aead_job(u32 *sh_desc, dma_addr_t ptr, |
| 1343 | struct aead_edesc *edesc, |
| 1344 | struct aead_request *req, |
| 1345 | bool all_contig, bool encrypt) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1346 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1347 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1348 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1349 | int ivsize = crypto_aead_ivsize(aead); |
| 1350 | int authsize = ctx->authsize; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1351 | u32 *desc = edesc->hw_desc; |
| 1352 | u32 out_options = 0, in_options; |
| 1353 | dma_addr_t dst_dma, src_dma; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1354 | int len, sec4_sg_index = 0; |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1355 | bool is_gcm = false; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1356 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1357 | #ifdef DEBUG |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1358 | debug("assoclen %d cryptlen %d authsize %d\n", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1359 | req->assoclen, req->cryptlen, authsize); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1360 | print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1361 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc), |
| 1362 | req->assoclen , 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1363 | print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1364 | DUMP_PREFIX_ADDRESS, 16, 4, req->iv, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1365 | edesc->src_nents ? 100 : ivsize, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1366 | print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1367 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1368 | edesc->src_nents ? 100 : req->cryptlen, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1369 | print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1370 | DUMP_PREFIX_ADDRESS, 16, 4, sh_desc, |
| 1371 | desc_bytes(sh_desc), 1); |
| 1372 | #endif |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1373 | |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1374 | if (((ctx->class1_alg_type & OP_ALG_ALGSEL_MASK) == |
| 1375 | OP_ALG_ALGSEL_AES) && |
| 1376 | ((ctx->class1_alg_type & OP_ALG_AAI_MASK) == OP_ALG_AAI_GCM)) |
| 1377 | is_gcm = true; |
| 1378 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1379 | len = desc_len(sh_desc); |
| 1380 | init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE); |
| 1381 | |
| 1382 | if (all_contig) { |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1383 | if (is_gcm) |
| 1384 | src_dma = edesc->iv_dma; |
| 1385 | else |
| 1386 | src_dma = sg_dma_address(req->assoc); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1387 | in_options = 0; |
| 1388 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1389 | src_dma = edesc->sec4_sg_dma; |
| 1390 | sec4_sg_index += (edesc->assoc_nents ? : 1) + 1 + |
| 1391 | (edesc->src_nents ? : 1); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1392 | in_options = LDST_SGF; |
| 1393 | } |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1394 | |
| 1395 | append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize + req->cryptlen, |
| 1396 | in_options); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1397 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1398 | if (likely(req->src == req->dst)) { |
| 1399 | if (all_contig) { |
| 1400 | dst_dma = sg_dma_address(req->src); |
| 1401 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1402 | dst_dma = src_dma + sizeof(struct sec4_sg_entry) * |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1403 | ((edesc->assoc_nents ? : 1) + 1); |
| 1404 | out_options = LDST_SGF; |
| 1405 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1406 | } else { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1407 | if (!edesc->dst_nents) { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1408 | dst_dma = sg_dma_address(req->dst); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1409 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1410 | dst_dma = edesc->sec4_sg_dma + |
| 1411 | sec4_sg_index * |
| 1412 | sizeof(struct sec4_sg_entry); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1413 | out_options = LDST_SGF; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1414 | } |
| 1415 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1416 | if (encrypt) |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1417 | append_seq_out_ptr(desc, dst_dma, req->cryptlen + authsize, |
| 1418 | out_options); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1419 | else |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1420 | append_seq_out_ptr(desc, dst_dma, req->cryptlen - authsize, |
| 1421 | out_options); |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Fill in aead givencrypt job descriptor |
| 1426 | */ |
| 1427 | static void init_aead_giv_job(u32 *sh_desc, dma_addr_t ptr, |
| 1428 | struct aead_edesc *edesc, |
| 1429 | struct aead_request *req, |
| 1430 | int contig) |
| 1431 | { |
| 1432 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 1433 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 1434 | int ivsize = crypto_aead_ivsize(aead); |
| 1435 | int authsize = ctx->authsize; |
| 1436 | u32 *desc = edesc->hw_desc; |
| 1437 | u32 out_options = 0, in_options; |
| 1438 | dma_addr_t dst_dma, src_dma; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1439 | int len, sec4_sg_index = 0; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1440 | |
| 1441 | #ifdef DEBUG |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1442 | debug("assoclen %d cryptlen %d authsize %d\n", |
| 1443 | req->assoclen, req->cryptlen, authsize); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1444 | print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1445 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc), |
| 1446 | req->assoclen , 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1447 | print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1448 | DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1449 | print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1450 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 1451 | edesc->src_nents > 1 ? 100 : req->cryptlen, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1452 | print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1453 | DUMP_PREFIX_ADDRESS, 16, 4, sh_desc, |
| 1454 | desc_bytes(sh_desc), 1); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1455 | #endif |
| 1456 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1457 | len = desc_len(sh_desc); |
| 1458 | init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE); |
| 1459 | |
| 1460 | if (contig & GIV_SRC_CONTIG) { |
| 1461 | src_dma = sg_dma_address(req->assoc); |
| 1462 | in_options = 0; |
| 1463 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1464 | src_dma = edesc->sec4_sg_dma; |
| 1465 | sec4_sg_index += edesc->assoc_nents + 1 + edesc->src_nents; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1466 | in_options = LDST_SGF; |
| 1467 | } |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1468 | append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize + req->cryptlen, |
| 1469 | in_options); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1470 | |
| 1471 | if (contig & GIV_DST_CONTIG) { |
| 1472 | dst_dma = edesc->iv_dma; |
| 1473 | } else { |
| 1474 | if (likely(req->src == req->dst)) { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1475 | dst_dma = src_dma + sizeof(struct sec4_sg_entry) * |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1476 | edesc->assoc_nents; |
| 1477 | out_options = LDST_SGF; |
| 1478 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1479 | dst_dma = edesc->sec4_sg_dma + |
| 1480 | sec4_sg_index * |
| 1481 | sizeof(struct sec4_sg_entry); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1482 | out_options = LDST_SGF; |
| 1483 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1484 | } |
| 1485 | |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1486 | append_seq_out_ptr(desc, dst_dma, ivsize + req->cryptlen + authsize, |
| 1487 | out_options); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /* |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1491 | * Fill in ablkcipher job descriptor |
| 1492 | */ |
| 1493 | static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr, |
| 1494 | struct ablkcipher_edesc *edesc, |
| 1495 | struct ablkcipher_request *req, |
| 1496 | bool iv_contig) |
| 1497 | { |
| 1498 | struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); |
| 1499 | int ivsize = crypto_ablkcipher_ivsize(ablkcipher); |
| 1500 | u32 *desc = edesc->hw_desc; |
| 1501 | u32 out_options = 0, in_options; |
| 1502 | dma_addr_t dst_dma, src_dma; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1503 | int len, sec4_sg_index = 0; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1504 | |
| 1505 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1506 | print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1507 | DUMP_PREFIX_ADDRESS, 16, 4, req->info, |
| 1508 | ivsize, 1); |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1509 | print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1510 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 1511 | edesc->src_nents ? 100 : req->nbytes, 1); |
| 1512 | #endif |
| 1513 | |
| 1514 | len = desc_len(sh_desc); |
| 1515 | init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE); |
| 1516 | |
| 1517 | if (iv_contig) { |
| 1518 | src_dma = edesc->iv_dma; |
| 1519 | in_options = 0; |
| 1520 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1521 | src_dma = edesc->sec4_sg_dma; |
| 1522 | sec4_sg_index += (iv_contig ? 0 : 1) + edesc->src_nents; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1523 | in_options = LDST_SGF; |
| 1524 | } |
| 1525 | append_seq_in_ptr(desc, src_dma, req->nbytes + ivsize, in_options); |
| 1526 | |
| 1527 | if (likely(req->src == req->dst)) { |
| 1528 | if (!edesc->src_nents && iv_contig) { |
| 1529 | dst_dma = sg_dma_address(req->src); |
| 1530 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1531 | dst_dma = edesc->sec4_sg_dma + |
| 1532 | sizeof(struct sec4_sg_entry); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1533 | out_options = LDST_SGF; |
| 1534 | } |
| 1535 | } else { |
| 1536 | if (!edesc->dst_nents) { |
| 1537 | dst_dma = sg_dma_address(req->dst); |
| 1538 | } else { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1539 | dst_dma = edesc->sec4_sg_dma + |
| 1540 | sec4_sg_index * sizeof(struct sec4_sg_entry); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1541 | out_options = LDST_SGF; |
| 1542 | } |
| 1543 | } |
| 1544 | append_seq_out_ptr(desc, dst_dma, req->nbytes, out_options); |
| 1545 | } |
| 1546 | |
| 1547 | /* |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1548 | * allocate and map the aead extended descriptor |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1549 | */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1550 | static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1551 | int desc_bytes, bool *all_contig_ptr, |
| 1552 | bool encrypt) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1553 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1554 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1555 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 1556 | struct device *jrdev = ctx->jrdev; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1557 | gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 1558 | CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; |
| 1559 | int assoc_nents, src_nents, dst_nents = 0; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1560 | struct aead_edesc *edesc; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1561 | dma_addr_t iv_dma = 0; |
| 1562 | int sgc; |
| 1563 | bool all_contig = true; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1564 | bool assoc_chained = false, src_chained = false, dst_chained = false; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1565 | int ivsize = crypto_aead_ivsize(aead); |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1566 | int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes; |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1567 | unsigned int authsize = ctx->authsize; |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1568 | bool is_gcm = false; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1569 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1570 | assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1571 | |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1572 | if (unlikely(req->dst != req->src)) { |
| 1573 | src_nents = sg_count(req->src, req->cryptlen, &src_chained); |
| 1574 | dst_nents = sg_count(req->dst, |
| 1575 | req->cryptlen + |
| 1576 | (encrypt ? authsize : (-authsize)), |
| 1577 | &dst_chained); |
| 1578 | } else { |
| 1579 | src_nents = sg_count(req->src, |
| 1580 | req->cryptlen + |
| 1581 | (encrypt ? authsize : 0), |
| 1582 | &src_chained); |
| 1583 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1584 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1585 | sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1, |
Horia Geanta | 286233e | 2013-05-10 15:08:39 +0300 | [diff] [blame] | 1586 | DMA_TO_DEVICE, assoc_chained); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1587 | if (likely(req->src == req->dst)) { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1588 | sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1, |
| 1589 | DMA_BIDIRECTIONAL, src_chained); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1590 | } else { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1591 | sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1, |
| 1592 | DMA_TO_DEVICE, src_chained); |
| 1593 | sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1, |
| 1594 | DMA_FROM_DEVICE, dst_chained); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1597 | iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE); |
Horia Geanta | ce57208 | 2014-07-11 15:34:49 +0300 | [diff] [blame] | 1598 | if (dma_mapping_error(jrdev, iv_dma)) { |
| 1599 | dev_err(jrdev, "unable to map IV\n"); |
| 1600 | return ERR_PTR(-ENOMEM); |
| 1601 | } |
| 1602 | |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1603 | if (((ctx->class1_alg_type & OP_ALG_ALGSEL_MASK) == |
| 1604 | OP_ALG_ALGSEL_AES) && |
| 1605 | ((ctx->class1_alg_type & OP_ALG_AAI_MASK) == OP_ALG_AAI_GCM)) |
| 1606 | is_gcm = true; |
| 1607 | |
| 1608 | /* |
| 1609 | * Check if data are contiguous. |
| 1610 | * GCM expected input sequence: IV, AAD, text |
| 1611 | * All other - expected input sequence: AAD, IV, text |
| 1612 | */ |
| 1613 | if (is_gcm) |
| 1614 | all_contig = (!assoc_nents && |
| 1615 | iv_dma + ivsize == sg_dma_address(req->assoc) && |
| 1616 | !src_nents && sg_dma_address(req->assoc) + |
| 1617 | req->assoclen == sg_dma_address(req->src)); |
| 1618 | else |
| 1619 | all_contig = (!assoc_nents && sg_dma_address(req->assoc) + |
| 1620 | req->assoclen == iv_dma && !src_nents && |
| 1621 | iv_dma + ivsize == sg_dma_address(req->src)); |
| 1622 | if (!all_contig) { |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1623 | assoc_nents = assoc_nents ? : 1; |
| 1624 | src_nents = src_nents ? : 1; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1625 | sec4_sg_len = assoc_nents + 1 + src_nents; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1626 | } |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1627 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1628 | sec4_sg_len += dst_nents; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1629 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1630 | sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1631 | |
| 1632 | /* allocate space for base edesc and hw desc commands, link tables */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1633 | edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes + |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1634 | sec4_sg_bytes, GFP_DMA | flags); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1635 | if (!edesc) { |
| 1636 | dev_err(jrdev, "could not allocate extended descriptor\n"); |
| 1637 | return ERR_PTR(-ENOMEM); |
| 1638 | } |
| 1639 | |
| 1640 | edesc->assoc_nents = assoc_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1641 | edesc->assoc_chained = assoc_chained; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1642 | edesc->src_nents = src_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1643 | edesc->src_chained = src_chained; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1644 | edesc->dst_nents = dst_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1645 | edesc->dst_chained = dst_chained; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1646 | edesc->iv_dma = iv_dma; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1647 | edesc->sec4_sg_bytes = sec4_sg_bytes; |
| 1648 | edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) + |
| 1649 | desc_bytes; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1650 | *all_contig_ptr = all_contig; |
| 1651 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1652 | sec4_sg_index = 0; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1653 | if (!all_contig) { |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1654 | if (!is_gcm) { |
| 1655 | sg_to_sec4_sg(req->assoc, |
| 1656 | (assoc_nents ? : 1), |
| 1657 | edesc->sec4_sg + |
| 1658 | sec4_sg_index, 0); |
| 1659 | sec4_sg_index += assoc_nents ? : 1; |
| 1660 | } |
| 1661 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1662 | dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index, |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1663 | iv_dma, ivsize, 0); |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1664 | sec4_sg_index += 1; |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 1665 | |
| 1666 | if (is_gcm) { |
| 1667 | sg_to_sec4_sg(req->assoc, |
| 1668 | (assoc_nents ? : 1), |
| 1669 | edesc->sec4_sg + |
| 1670 | sec4_sg_index, 0); |
| 1671 | sec4_sg_index += assoc_nents ? : 1; |
| 1672 | } |
| 1673 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1674 | sg_to_sec4_sg_last(req->src, |
| 1675 | (src_nents ? : 1), |
| 1676 | edesc->sec4_sg + |
| 1677 | sec4_sg_index, 0); |
| 1678 | sec4_sg_index += src_nents ? : 1; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1679 | } |
| 1680 | if (dst_nents) { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1681 | sg_to_sec4_sg_last(req->dst, dst_nents, |
| 1682 | edesc->sec4_sg + sec4_sg_index, 0); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1683 | } |
Ruchika Gupta | 1da2be3 | 2014-06-23 19:50:26 +0530 | [diff] [blame] | 1684 | edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, |
| 1685 | sec4_sg_bytes, DMA_TO_DEVICE); |
Horia Geanta | ce57208 | 2014-07-11 15:34:49 +0300 | [diff] [blame] | 1686 | if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { |
| 1687 | dev_err(jrdev, "unable to map S/G table\n"); |
| 1688 | return ERR_PTR(-ENOMEM); |
| 1689 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1690 | |
| 1691 | return edesc; |
| 1692 | } |
| 1693 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1694 | static int aead_encrypt(struct aead_request *req) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1695 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1696 | struct aead_edesc *edesc; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1697 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1698 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 1699 | struct device *jrdev = ctx->jrdev; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1700 | bool all_contig; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1701 | u32 *desc; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1702 | int ret = 0; |
| 1703 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1704 | /* allocate extended descriptor */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1705 | edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN * |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1706 | CAAM_CMD_SZ, &all_contig, true); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1707 | if (IS_ERR(edesc)) |
| 1708 | return PTR_ERR(edesc); |
| 1709 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1710 | /* Create and submit job descriptor */ |
| 1711 | init_aead_job(ctx->sh_desc_enc, ctx->sh_desc_enc_dma, edesc, req, |
| 1712 | all_contig, true); |
| 1713 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1714 | print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1715 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc, |
| 1716 | desc_bytes(edesc->hw_desc), 1); |
| 1717 | #endif |
| 1718 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1719 | desc = edesc->hw_desc; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1720 | ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); |
| 1721 | if (!ret) { |
| 1722 | ret = -EINPROGRESS; |
| 1723 | } else { |
| 1724 | aead_unmap(jrdev, edesc, req); |
| 1725 | kfree(edesc); |
| 1726 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1727 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1728 | return ret; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1729 | } |
| 1730 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1731 | static int aead_decrypt(struct aead_request *req) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1732 | { |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1733 | struct aead_edesc *edesc; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1734 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1735 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 1736 | struct device *jrdev = ctx->jrdev; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1737 | bool all_contig; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1738 | u32 *desc; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1739 | int ret = 0; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1740 | |
| 1741 | /* allocate extended descriptor */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1742 | edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN * |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1743 | CAAM_CMD_SZ, &all_contig, false); |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1744 | if (IS_ERR(edesc)) |
| 1745 | return PTR_ERR(edesc); |
| 1746 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1747 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1748 | print_hex_dump(KERN_ERR, "dec src@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1749 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 1750 | req->cryptlen, 1); |
| 1751 | #endif |
| 1752 | |
| 1753 | /* Create and submit job descriptor*/ |
| 1754 | init_aead_job(ctx->sh_desc_dec, |
| 1755 | ctx->sh_desc_dec_dma, edesc, req, all_contig, false); |
| 1756 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1757 | print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1758 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc, |
| 1759 | desc_bytes(edesc->hw_desc), 1); |
| 1760 | #endif |
| 1761 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1762 | desc = edesc->hw_desc; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1763 | ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req); |
| 1764 | if (!ret) { |
| 1765 | ret = -EINPROGRESS; |
| 1766 | } else { |
| 1767 | aead_unmap(jrdev, edesc, req); |
| 1768 | kfree(edesc); |
| 1769 | } |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1770 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1771 | return ret; |
| 1772 | } |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1773 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1774 | /* |
| 1775 | * allocate and map the aead extended descriptor for aead givencrypt |
| 1776 | */ |
| 1777 | static struct aead_edesc *aead_giv_edesc_alloc(struct aead_givcrypt_request |
| 1778 | *greq, int desc_bytes, |
| 1779 | u32 *contig_ptr) |
| 1780 | { |
| 1781 | struct aead_request *req = &greq->areq; |
| 1782 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 1783 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 1784 | struct device *jrdev = ctx->jrdev; |
| 1785 | gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 1786 | CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC; |
| 1787 | int assoc_nents, src_nents, dst_nents = 0; |
| 1788 | struct aead_edesc *edesc; |
| 1789 | dma_addr_t iv_dma = 0; |
| 1790 | int sgc; |
| 1791 | u32 contig = GIV_SRC_CONTIG | GIV_DST_CONTIG; |
| 1792 | int ivsize = crypto_aead_ivsize(aead); |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1793 | bool assoc_chained = false, src_chained = false, dst_chained = false; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1794 | int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1795 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1796 | assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained); |
| 1797 | src_nents = sg_count(req->src, req->cryptlen, &src_chained); |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1798 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1799 | if (unlikely(req->dst != req->src)) |
Horia Geanta | bbf9c89 | 2013-11-28 15:11:16 +0200 | [diff] [blame] | 1800 | dst_nents = sg_count(req->dst, req->cryptlen + ctx->authsize, |
| 1801 | &dst_chained); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1802 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1803 | sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1, |
Horia Geanta | 286233e | 2013-05-10 15:08:39 +0300 | [diff] [blame] | 1804 | DMA_TO_DEVICE, assoc_chained); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1805 | if (likely(req->src == req->dst)) { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1806 | sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1, |
| 1807 | DMA_BIDIRECTIONAL, src_chained); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1808 | } else { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1809 | sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1, |
| 1810 | DMA_TO_DEVICE, src_chained); |
| 1811 | sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1, |
| 1812 | DMA_FROM_DEVICE, dst_chained); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1813 | } |
| 1814 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1815 | iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE); |
Horia Geanta | ce57208 | 2014-07-11 15:34:49 +0300 | [diff] [blame] | 1816 | if (dma_mapping_error(jrdev, iv_dma)) { |
| 1817 | dev_err(jrdev, "unable to map IV\n"); |
| 1818 | return ERR_PTR(-ENOMEM); |
| 1819 | } |
| 1820 | |
| 1821 | /* Check if data are contiguous */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1822 | if (assoc_nents || sg_dma_address(req->assoc) + req->assoclen != |
| 1823 | iv_dma || src_nents || iv_dma + ivsize != sg_dma_address(req->src)) |
| 1824 | contig &= ~GIV_SRC_CONTIG; |
| 1825 | if (dst_nents || iv_dma + ivsize != sg_dma_address(req->dst)) |
| 1826 | contig &= ~GIV_DST_CONTIG; |
Kim Phillips | 2af8f4a | 2012-09-07 04:17:03 +0800 | [diff] [blame] | 1827 | if (unlikely(req->src != req->dst)) { |
| 1828 | dst_nents = dst_nents ? : 1; |
| 1829 | sec4_sg_len += 1; |
| 1830 | } |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1831 | if (!(contig & GIV_SRC_CONTIG)) { |
| 1832 | assoc_nents = assoc_nents ? : 1; |
| 1833 | src_nents = src_nents ? : 1; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1834 | sec4_sg_len += assoc_nents + 1 + src_nents; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1835 | if (likely(req->src == req->dst)) |
| 1836 | contig &= ~GIV_DST_CONTIG; |
| 1837 | } |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1838 | sec4_sg_len += dst_nents; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1839 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1840 | sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1841 | |
| 1842 | /* allocate space for base edesc and hw desc commands, link tables */ |
| 1843 | edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes + |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1844 | sec4_sg_bytes, GFP_DMA | flags); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1845 | if (!edesc) { |
| 1846 | dev_err(jrdev, "could not allocate extended descriptor\n"); |
| 1847 | return ERR_PTR(-ENOMEM); |
| 1848 | } |
| 1849 | |
| 1850 | edesc->assoc_nents = assoc_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1851 | edesc->assoc_chained = assoc_chained; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1852 | edesc->src_nents = src_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1853 | edesc->src_chained = src_chained; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1854 | edesc->dst_nents = dst_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1855 | edesc->dst_chained = dst_chained; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1856 | edesc->iv_dma = iv_dma; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1857 | edesc->sec4_sg_bytes = sec4_sg_bytes; |
| 1858 | edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) + |
| 1859 | desc_bytes; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1860 | *contig_ptr = contig; |
| 1861 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1862 | sec4_sg_index = 0; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1863 | if (!(contig & GIV_SRC_CONTIG)) { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1864 | sg_to_sec4_sg(req->assoc, assoc_nents, |
| 1865 | edesc->sec4_sg + |
| 1866 | sec4_sg_index, 0); |
| 1867 | sec4_sg_index += assoc_nents; |
| 1868 | dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index, |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1869 | iv_dma, ivsize, 0); |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1870 | sec4_sg_index += 1; |
| 1871 | sg_to_sec4_sg_last(req->src, src_nents, |
| 1872 | edesc->sec4_sg + |
| 1873 | sec4_sg_index, 0); |
| 1874 | sec4_sg_index += src_nents; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1875 | } |
| 1876 | if (unlikely(req->src != req->dst && !(contig & GIV_DST_CONTIG))) { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1877 | dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index, |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1878 | iv_dma, ivsize, 0); |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1879 | sec4_sg_index += 1; |
| 1880 | sg_to_sec4_sg_last(req->dst, dst_nents, |
| 1881 | edesc->sec4_sg + sec4_sg_index, 0); |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1882 | } |
Ruchika Gupta | 1da2be3 | 2014-06-23 19:50:26 +0530 | [diff] [blame] | 1883 | edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, |
| 1884 | sec4_sg_bytes, DMA_TO_DEVICE); |
Horia Geanta | ce57208 | 2014-07-11 15:34:49 +0300 | [diff] [blame] | 1885 | if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { |
| 1886 | dev_err(jrdev, "unable to map S/G table\n"); |
| 1887 | return ERR_PTR(-ENOMEM); |
| 1888 | } |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1889 | |
| 1890 | return edesc; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | static int aead_givencrypt(struct aead_givcrypt_request *areq) |
| 1894 | { |
| 1895 | struct aead_request *req = &areq->areq; |
| 1896 | struct aead_edesc *edesc; |
| 1897 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1898 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 1899 | struct device *jrdev = ctx->jrdev; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1900 | u32 contig; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1901 | u32 *desc; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1902 | int ret = 0; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1903 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1904 | /* allocate extended descriptor */ |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1905 | edesc = aead_giv_edesc_alloc(areq, DESC_JOB_IO_LEN * |
| 1906 | CAAM_CMD_SZ, &contig); |
| 1907 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1908 | if (IS_ERR(edesc)) |
| 1909 | return PTR_ERR(edesc); |
| 1910 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1911 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1912 | print_hex_dump(KERN_ERR, "giv src@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1913 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 1914 | req->cryptlen, 1); |
| 1915 | #endif |
| 1916 | |
| 1917 | /* Create and submit job descriptor*/ |
| 1918 | init_aead_giv_job(ctx->sh_desc_givenc, |
| 1919 | ctx->sh_desc_givenc_dma, edesc, req, contig); |
| 1920 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 1921 | print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ", |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1922 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc, |
| 1923 | desc_bytes(edesc->hw_desc), 1); |
| 1924 | #endif |
| 1925 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1926 | desc = edesc->hw_desc; |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1927 | ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req); |
| 1928 | if (!ret) { |
| 1929 | ret = -EINPROGRESS; |
| 1930 | } else { |
| 1931 | aead_unmap(jrdev, edesc, req); |
| 1932 | kfree(edesc); |
| 1933 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1934 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1935 | return ret; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1936 | } |
| 1937 | |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 1938 | static int aead_null_givencrypt(struct aead_givcrypt_request *areq) |
| 1939 | { |
| 1940 | return aead_encrypt(&areq->areq); |
| 1941 | } |
| 1942 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1943 | /* |
| 1944 | * allocate and map the ablkcipher extended descriptor for ablkcipher |
| 1945 | */ |
| 1946 | static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request |
| 1947 | *req, int desc_bytes, |
| 1948 | bool *iv_contig_out) |
| 1949 | { |
| 1950 | struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); |
| 1951 | struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher); |
| 1952 | struct device *jrdev = ctx->jrdev; |
| 1953 | gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 1954 | CRYPTO_TFM_REQ_MAY_SLEEP)) ? |
| 1955 | GFP_KERNEL : GFP_ATOMIC; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1956 | int src_nents, dst_nents = 0, sec4_sg_bytes; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1957 | struct ablkcipher_edesc *edesc; |
| 1958 | dma_addr_t iv_dma = 0; |
| 1959 | bool iv_contig = false; |
| 1960 | int sgc; |
| 1961 | int ivsize = crypto_ablkcipher_ivsize(ablkcipher); |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1962 | bool src_chained = false, dst_chained = false; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1963 | int sec4_sg_index; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1964 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1965 | src_nents = sg_count(req->src, req->nbytes, &src_chained); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1966 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1967 | if (req->dst != req->src) |
| 1968 | dst_nents = sg_count(req->dst, req->nbytes, &dst_chained); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1969 | |
| 1970 | if (likely(req->src == req->dst)) { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1971 | sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1, |
| 1972 | DMA_BIDIRECTIONAL, src_chained); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1973 | } else { |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 1974 | sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1, |
| 1975 | DMA_TO_DEVICE, src_chained); |
| 1976 | sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1, |
| 1977 | DMA_FROM_DEVICE, dst_chained); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1978 | } |
| 1979 | |
Horia Geanta | ce57208 | 2014-07-11 15:34:49 +0300 | [diff] [blame] | 1980 | iv_dma = dma_map_single(jrdev, req->info, ivsize, DMA_TO_DEVICE); |
| 1981 | if (dma_mapping_error(jrdev, iv_dma)) { |
| 1982 | dev_err(jrdev, "unable to map IV\n"); |
| 1983 | return ERR_PTR(-ENOMEM); |
| 1984 | } |
| 1985 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1986 | /* |
| 1987 | * Check if iv can be contiguous with source and destination. |
| 1988 | * If so, include it. If not, create scatterlist. |
| 1989 | */ |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1990 | if (!src_nents && iv_dma + ivsize == sg_dma_address(req->src)) |
| 1991 | iv_contig = true; |
| 1992 | else |
| 1993 | src_nents = src_nents ? : 1; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1994 | sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) * |
| 1995 | sizeof(struct sec4_sg_entry); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 1996 | |
| 1997 | /* allocate space for base edesc and hw desc commands, link tables */ |
| 1998 | edesc = kmalloc(sizeof(struct ablkcipher_edesc) + desc_bytes + |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 1999 | sec4_sg_bytes, GFP_DMA | flags); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2000 | if (!edesc) { |
| 2001 | dev_err(jrdev, "could not allocate extended descriptor\n"); |
| 2002 | return ERR_PTR(-ENOMEM); |
| 2003 | } |
| 2004 | |
| 2005 | edesc->src_nents = src_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 2006 | edesc->src_chained = src_chained; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2007 | edesc->dst_nents = dst_nents; |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 2008 | edesc->dst_chained = dst_chained; |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 2009 | edesc->sec4_sg_bytes = sec4_sg_bytes; |
| 2010 | edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) + |
| 2011 | desc_bytes; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2012 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 2013 | sec4_sg_index = 0; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2014 | if (!iv_contig) { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 2015 | dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0); |
| 2016 | sg_to_sec4_sg_last(req->src, src_nents, |
| 2017 | edesc->sec4_sg + 1, 0); |
| 2018 | sec4_sg_index += 1 + src_nents; |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2019 | } |
| 2020 | |
Yuan Kang | 643b39b | 2012-06-22 19:48:49 -0500 | [diff] [blame] | 2021 | if (dst_nents) { |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 2022 | sg_to_sec4_sg_last(req->dst, dst_nents, |
| 2023 | edesc->sec4_sg + sec4_sg_index, 0); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2024 | } |
| 2025 | |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 2026 | edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg, |
| 2027 | sec4_sg_bytes, DMA_TO_DEVICE); |
Horia Geanta | ce57208 | 2014-07-11 15:34:49 +0300 | [diff] [blame] | 2028 | if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { |
| 2029 | dev_err(jrdev, "unable to map S/G table\n"); |
| 2030 | return ERR_PTR(-ENOMEM); |
| 2031 | } |
| 2032 | |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2033 | edesc->iv_dma = iv_dma; |
| 2034 | |
| 2035 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 2036 | print_hex_dump(KERN_ERR, "ablkcipher sec4_sg@"__stringify(__LINE__)": ", |
Yuan Kang | a299c83 | 2012-06-22 19:48:46 -0500 | [diff] [blame] | 2037 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg, |
| 2038 | sec4_sg_bytes, 1); |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2039 | #endif |
| 2040 | |
| 2041 | *iv_contig_out = iv_contig; |
| 2042 | return edesc; |
| 2043 | } |
| 2044 | |
| 2045 | static int ablkcipher_encrypt(struct ablkcipher_request *req) |
| 2046 | { |
| 2047 | struct ablkcipher_edesc *edesc; |
| 2048 | struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); |
| 2049 | struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher); |
| 2050 | struct device *jrdev = ctx->jrdev; |
| 2051 | bool iv_contig; |
| 2052 | u32 *desc; |
| 2053 | int ret = 0; |
| 2054 | |
| 2055 | /* allocate extended descriptor */ |
| 2056 | edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN * |
| 2057 | CAAM_CMD_SZ, &iv_contig); |
| 2058 | if (IS_ERR(edesc)) |
| 2059 | return PTR_ERR(edesc); |
| 2060 | |
| 2061 | /* Create and submit job descriptor*/ |
| 2062 | init_ablkcipher_job(ctx->sh_desc_enc, |
| 2063 | ctx->sh_desc_enc_dma, edesc, req, iv_contig); |
| 2064 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 2065 | print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2066 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc, |
| 2067 | desc_bytes(edesc->hw_desc), 1); |
| 2068 | #endif |
| 2069 | desc = edesc->hw_desc; |
| 2070 | ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req); |
| 2071 | |
| 2072 | if (!ret) { |
| 2073 | ret = -EINPROGRESS; |
| 2074 | } else { |
| 2075 | ablkcipher_unmap(jrdev, edesc, req); |
| 2076 | kfree(edesc); |
| 2077 | } |
| 2078 | |
| 2079 | return ret; |
| 2080 | } |
| 2081 | |
| 2082 | static int ablkcipher_decrypt(struct ablkcipher_request *req) |
| 2083 | { |
| 2084 | struct ablkcipher_edesc *edesc; |
| 2085 | struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req); |
| 2086 | struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher); |
| 2087 | struct device *jrdev = ctx->jrdev; |
| 2088 | bool iv_contig; |
| 2089 | u32 *desc; |
| 2090 | int ret = 0; |
| 2091 | |
| 2092 | /* allocate extended descriptor */ |
| 2093 | edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN * |
| 2094 | CAAM_CMD_SZ, &iv_contig); |
| 2095 | if (IS_ERR(edesc)) |
| 2096 | return PTR_ERR(edesc); |
| 2097 | |
| 2098 | /* Create and submit job descriptor*/ |
| 2099 | init_ablkcipher_job(ctx->sh_desc_dec, |
| 2100 | ctx->sh_desc_dec_dma, edesc, req, iv_contig); |
| 2101 | desc = edesc->hw_desc; |
| 2102 | #ifdef DEBUG |
Alex Porosanu | 514df28 | 2013-08-14 18:56:45 +0300 | [diff] [blame] | 2103 | print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ", |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2104 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc, |
| 2105 | desc_bytes(edesc->hw_desc), 1); |
| 2106 | #endif |
| 2107 | |
| 2108 | ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req); |
| 2109 | if (!ret) { |
| 2110 | ret = -EINPROGRESS; |
| 2111 | } else { |
| 2112 | ablkcipher_unmap(jrdev, edesc, req); |
| 2113 | kfree(edesc); |
| 2114 | } |
| 2115 | |
| 2116 | return ret; |
| 2117 | } |
| 2118 | |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2119 | #define template_aead template_u.aead |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2120 | #define template_ablkcipher template_u.ablkcipher |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2121 | struct caam_alg_template { |
| 2122 | char name[CRYPTO_MAX_ALG_NAME]; |
| 2123 | char driver_name[CRYPTO_MAX_ALG_NAME]; |
| 2124 | unsigned int blocksize; |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2125 | u32 type; |
| 2126 | union { |
| 2127 | struct ablkcipher_alg ablkcipher; |
| 2128 | struct aead_alg aead; |
| 2129 | struct blkcipher_alg blkcipher; |
| 2130 | struct cipher_alg cipher; |
| 2131 | struct compress_alg compress; |
| 2132 | struct rng_alg rng; |
| 2133 | } template_u; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2134 | u32 class1_alg_type; |
| 2135 | u32 class2_alg_type; |
| 2136 | u32 alg_op; |
| 2137 | }; |
| 2138 | |
| 2139 | static struct caam_alg_template driver_algs[] = { |
Horia Geanta | 246bbed | 2013-03-20 16:31:58 +0200 | [diff] [blame] | 2140 | /* single-pass ipsec_esp descriptor */ |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2141 | { |
Horia Geanta | ae4a825 | 2014-03-14 17:46:52 +0200 | [diff] [blame] | 2142 | .name = "authenc(hmac(md5),ecb(cipher_null))", |
| 2143 | .driver_name = "authenc-hmac-md5-ecb-cipher_null-caam", |
| 2144 | .blocksize = NULL_BLOCK_SIZE, |
| 2145 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2146 | .template_aead = { |
| 2147 | .setkey = aead_setkey, |
| 2148 | .setauthsize = aead_setauthsize, |
| 2149 | .encrypt = aead_encrypt, |
| 2150 | .decrypt = aead_decrypt, |
| 2151 | .givencrypt = aead_null_givencrypt, |
| 2152 | .geniv = "<built-in>", |
| 2153 | .ivsize = NULL_IV_SIZE, |
| 2154 | .maxauthsize = MD5_DIGEST_SIZE, |
| 2155 | }, |
| 2156 | .class1_alg_type = 0, |
| 2157 | .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2158 | .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC, |
| 2159 | }, |
| 2160 | { |
| 2161 | .name = "authenc(hmac(sha1),ecb(cipher_null))", |
| 2162 | .driver_name = "authenc-hmac-sha1-ecb-cipher_null-caam", |
| 2163 | .blocksize = NULL_BLOCK_SIZE, |
| 2164 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2165 | .template_aead = { |
| 2166 | .setkey = aead_setkey, |
| 2167 | .setauthsize = aead_setauthsize, |
| 2168 | .encrypt = aead_encrypt, |
| 2169 | .decrypt = aead_decrypt, |
| 2170 | .givencrypt = aead_null_givencrypt, |
| 2171 | .geniv = "<built-in>", |
| 2172 | .ivsize = NULL_IV_SIZE, |
| 2173 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2174 | }, |
| 2175 | .class1_alg_type = 0, |
| 2176 | .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2177 | .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, |
| 2178 | }, |
| 2179 | { |
| 2180 | .name = "authenc(hmac(sha224),ecb(cipher_null))", |
| 2181 | .driver_name = "authenc-hmac-sha224-ecb-cipher_null-caam", |
| 2182 | .blocksize = NULL_BLOCK_SIZE, |
| 2183 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2184 | .template_aead = { |
| 2185 | .setkey = aead_setkey, |
| 2186 | .setauthsize = aead_setauthsize, |
| 2187 | .encrypt = aead_encrypt, |
| 2188 | .decrypt = aead_decrypt, |
| 2189 | .givencrypt = aead_null_givencrypt, |
| 2190 | .geniv = "<built-in>", |
| 2191 | .ivsize = NULL_IV_SIZE, |
| 2192 | .maxauthsize = SHA224_DIGEST_SIZE, |
| 2193 | }, |
| 2194 | .class1_alg_type = 0, |
| 2195 | .class2_alg_type = OP_ALG_ALGSEL_SHA224 | |
| 2196 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2197 | .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC, |
| 2198 | }, |
| 2199 | { |
| 2200 | .name = "authenc(hmac(sha256),ecb(cipher_null))", |
| 2201 | .driver_name = "authenc-hmac-sha256-ecb-cipher_null-caam", |
| 2202 | .blocksize = NULL_BLOCK_SIZE, |
| 2203 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2204 | .template_aead = { |
| 2205 | .setkey = aead_setkey, |
| 2206 | .setauthsize = aead_setauthsize, |
| 2207 | .encrypt = aead_encrypt, |
| 2208 | .decrypt = aead_decrypt, |
| 2209 | .givencrypt = aead_null_givencrypt, |
| 2210 | .geniv = "<built-in>", |
| 2211 | .ivsize = NULL_IV_SIZE, |
| 2212 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 2213 | }, |
| 2214 | .class1_alg_type = 0, |
| 2215 | .class2_alg_type = OP_ALG_ALGSEL_SHA256 | |
| 2216 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2217 | .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, |
| 2218 | }, |
| 2219 | { |
| 2220 | .name = "authenc(hmac(sha384),ecb(cipher_null))", |
| 2221 | .driver_name = "authenc-hmac-sha384-ecb-cipher_null-caam", |
| 2222 | .blocksize = NULL_BLOCK_SIZE, |
| 2223 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2224 | .template_aead = { |
| 2225 | .setkey = aead_setkey, |
| 2226 | .setauthsize = aead_setauthsize, |
| 2227 | .encrypt = aead_encrypt, |
| 2228 | .decrypt = aead_decrypt, |
| 2229 | .givencrypt = aead_null_givencrypt, |
| 2230 | .geniv = "<built-in>", |
| 2231 | .ivsize = NULL_IV_SIZE, |
| 2232 | .maxauthsize = SHA384_DIGEST_SIZE, |
| 2233 | }, |
| 2234 | .class1_alg_type = 0, |
| 2235 | .class2_alg_type = OP_ALG_ALGSEL_SHA384 | |
| 2236 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2237 | .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC, |
| 2238 | }, |
| 2239 | { |
| 2240 | .name = "authenc(hmac(sha512),ecb(cipher_null))", |
| 2241 | .driver_name = "authenc-hmac-sha512-ecb-cipher_null-caam", |
| 2242 | .blocksize = NULL_BLOCK_SIZE, |
| 2243 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2244 | .template_aead = { |
| 2245 | .setkey = aead_setkey, |
| 2246 | .setauthsize = aead_setauthsize, |
| 2247 | .encrypt = aead_encrypt, |
| 2248 | .decrypt = aead_decrypt, |
| 2249 | .givencrypt = aead_null_givencrypt, |
| 2250 | .geniv = "<built-in>", |
| 2251 | .ivsize = NULL_IV_SIZE, |
| 2252 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 2253 | }, |
| 2254 | .class1_alg_type = 0, |
| 2255 | .class2_alg_type = OP_ALG_ALGSEL_SHA512 | |
| 2256 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2257 | .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, |
| 2258 | }, |
| 2259 | { |
Kim Phillips | 8b4d43a | 2011-11-21 16:13:27 +0800 | [diff] [blame] | 2260 | .name = "authenc(hmac(md5),cbc(aes))", |
| 2261 | .driver_name = "authenc-hmac-md5-cbc-aes-caam", |
| 2262 | .blocksize = AES_BLOCK_SIZE, |
| 2263 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2264 | .template_aead = { |
| 2265 | .setkey = aead_setkey, |
| 2266 | .setauthsize = aead_setauthsize, |
| 2267 | .encrypt = aead_encrypt, |
| 2268 | .decrypt = aead_decrypt, |
| 2269 | .givencrypt = aead_givencrypt, |
| 2270 | .geniv = "<built-in>", |
| 2271 | .ivsize = AES_BLOCK_SIZE, |
| 2272 | .maxauthsize = MD5_DIGEST_SIZE, |
| 2273 | }, |
| 2274 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 2275 | .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2276 | .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC, |
| 2277 | }, |
| 2278 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2279 | .name = "authenc(hmac(sha1),cbc(aes))", |
| 2280 | .driver_name = "authenc-hmac-sha1-cbc-aes-caam", |
| 2281 | .blocksize = AES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2282 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2283 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2284 | .setkey = aead_setkey, |
| 2285 | .setauthsize = aead_setauthsize, |
| 2286 | .encrypt = aead_encrypt, |
| 2287 | .decrypt = aead_decrypt, |
| 2288 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2289 | .geniv = "<built-in>", |
| 2290 | .ivsize = AES_BLOCK_SIZE, |
| 2291 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2292 | }, |
| 2293 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 2294 | .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2295 | .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, |
| 2296 | }, |
| 2297 | { |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2298 | .name = "authenc(hmac(sha224),cbc(aes))", |
| 2299 | .driver_name = "authenc-hmac-sha224-cbc-aes-caam", |
| 2300 | .blocksize = AES_BLOCK_SIZE, |
Vakul Garg | cb7d566 | 2013-03-12 14:09:24 +0530 | [diff] [blame] | 2301 | .type = CRYPTO_ALG_TYPE_AEAD, |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2302 | .template_aead = { |
| 2303 | .setkey = aead_setkey, |
| 2304 | .setauthsize = aead_setauthsize, |
| 2305 | .encrypt = aead_encrypt, |
| 2306 | .decrypt = aead_decrypt, |
| 2307 | .givencrypt = aead_givencrypt, |
| 2308 | .geniv = "<built-in>", |
| 2309 | .ivsize = AES_BLOCK_SIZE, |
| 2310 | .maxauthsize = SHA224_DIGEST_SIZE, |
| 2311 | }, |
| 2312 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 2313 | .class2_alg_type = OP_ALG_ALGSEL_SHA224 | |
| 2314 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2315 | .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC, |
| 2316 | }, |
| 2317 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2318 | .name = "authenc(hmac(sha256),cbc(aes))", |
| 2319 | .driver_name = "authenc-hmac-sha256-cbc-aes-caam", |
| 2320 | .blocksize = AES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2321 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2322 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2323 | .setkey = aead_setkey, |
| 2324 | .setauthsize = aead_setauthsize, |
| 2325 | .encrypt = aead_encrypt, |
| 2326 | .decrypt = aead_decrypt, |
| 2327 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2328 | .geniv = "<built-in>", |
| 2329 | .ivsize = AES_BLOCK_SIZE, |
| 2330 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 2331 | }, |
| 2332 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 2333 | .class2_alg_type = OP_ALG_ALGSEL_SHA256 | |
| 2334 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2335 | .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, |
| 2336 | }, |
| 2337 | { |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2338 | .name = "authenc(hmac(sha384),cbc(aes))", |
| 2339 | .driver_name = "authenc-hmac-sha384-cbc-aes-caam", |
| 2340 | .blocksize = AES_BLOCK_SIZE, |
Vakul Garg | cb7d566 | 2013-03-12 14:09:24 +0530 | [diff] [blame] | 2341 | .type = CRYPTO_ALG_TYPE_AEAD, |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2342 | .template_aead = { |
| 2343 | .setkey = aead_setkey, |
| 2344 | .setauthsize = aead_setauthsize, |
| 2345 | .encrypt = aead_encrypt, |
| 2346 | .decrypt = aead_decrypt, |
| 2347 | .givencrypt = aead_givencrypt, |
| 2348 | .geniv = "<built-in>", |
| 2349 | .ivsize = AES_BLOCK_SIZE, |
| 2350 | .maxauthsize = SHA384_DIGEST_SIZE, |
| 2351 | }, |
| 2352 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 2353 | .class2_alg_type = OP_ALG_ALGSEL_SHA384 | |
| 2354 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2355 | .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC, |
| 2356 | }, |
| 2357 | |
| 2358 | { |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2359 | .name = "authenc(hmac(sha512),cbc(aes))", |
| 2360 | .driver_name = "authenc-hmac-sha512-cbc-aes-caam", |
| 2361 | .blocksize = AES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2362 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2363 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2364 | .setkey = aead_setkey, |
| 2365 | .setauthsize = aead_setauthsize, |
| 2366 | .encrypt = aead_encrypt, |
| 2367 | .decrypt = aead_decrypt, |
| 2368 | .givencrypt = aead_givencrypt, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2369 | .geniv = "<built-in>", |
| 2370 | .ivsize = AES_BLOCK_SIZE, |
| 2371 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 2372 | }, |
| 2373 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 2374 | .class2_alg_type = OP_ALG_ALGSEL_SHA512 | |
| 2375 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2376 | .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, |
| 2377 | }, |
| 2378 | { |
Kim Phillips | 8b4d43a | 2011-11-21 16:13:27 +0800 | [diff] [blame] | 2379 | .name = "authenc(hmac(md5),cbc(des3_ede))", |
| 2380 | .driver_name = "authenc-hmac-md5-cbc-des3_ede-caam", |
| 2381 | .blocksize = DES3_EDE_BLOCK_SIZE, |
| 2382 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2383 | .template_aead = { |
| 2384 | .setkey = aead_setkey, |
| 2385 | .setauthsize = aead_setauthsize, |
| 2386 | .encrypt = aead_encrypt, |
| 2387 | .decrypt = aead_decrypt, |
| 2388 | .givencrypt = aead_givencrypt, |
| 2389 | .geniv = "<built-in>", |
| 2390 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2391 | .maxauthsize = MD5_DIGEST_SIZE, |
| 2392 | }, |
| 2393 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 2394 | .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2395 | .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC, |
| 2396 | }, |
| 2397 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2398 | .name = "authenc(hmac(sha1),cbc(des3_ede))", |
| 2399 | .driver_name = "authenc-hmac-sha1-cbc-des3_ede-caam", |
| 2400 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2401 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2402 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2403 | .setkey = aead_setkey, |
| 2404 | .setauthsize = aead_setauthsize, |
| 2405 | .encrypt = aead_encrypt, |
| 2406 | .decrypt = aead_decrypt, |
| 2407 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2408 | .geniv = "<built-in>", |
| 2409 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2410 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2411 | }, |
| 2412 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 2413 | .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2414 | .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, |
| 2415 | }, |
| 2416 | { |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2417 | .name = "authenc(hmac(sha224),cbc(des3_ede))", |
| 2418 | .driver_name = "authenc-hmac-sha224-cbc-des3_ede-caam", |
| 2419 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Vakul Garg | cb7d566 | 2013-03-12 14:09:24 +0530 | [diff] [blame] | 2420 | .type = CRYPTO_ALG_TYPE_AEAD, |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2421 | .template_aead = { |
| 2422 | .setkey = aead_setkey, |
| 2423 | .setauthsize = aead_setauthsize, |
| 2424 | .encrypt = aead_encrypt, |
| 2425 | .decrypt = aead_decrypt, |
| 2426 | .givencrypt = aead_givencrypt, |
| 2427 | .geniv = "<built-in>", |
| 2428 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2429 | .maxauthsize = SHA224_DIGEST_SIZE, |
| 2430 | }, |
| 2431 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 2432 | .class2_alg_type = OP_ALG_ALGSEL_SHA224 | |
| 2433 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2434 | .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC, |
| 2435 | }, |
| 2436 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2437 | .name = "authenc(hmac(sha256),cbc(des3_ede))", |
| 2438 | .driver_name = "authenc-hmac-sha256-cbc-des3_ede-caam", |
| 2439 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2440 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2441 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2442 | .setkey = aead_setkey, |
| 2443 | .setauthsize = aead_setauthsize, |
| 2444 | .encrypt = aead_encrypt, |
| 2445 | .decrypt = aead_decrypt, |
| 2446 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2447 | .geniv = "<built-in>", |
| 2448 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2449 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 2450 | }, |
| 2451 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 2452 | .class2_alg_type = OP_ALG_ALGSEL_SHA256 | |
| 2453 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2454 | .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, |
| 2455 | }, |
| 2456 | { |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2457 | .name = "authenc(hmac(sha384),cbc(des3_ede))", |
| 2458 | .driver_name = "authenc-hmac-sha384-cbc-des3_ede-caam", |
| 2459 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Vakul Garg | cb7d566 | 2013-03-12 14:09:24 +0530 | [diff] [blame] | 2460 | .type = CRYPTO_ALG_TYPE_AEAD, |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2461 | .template_aead = { |
| 2462 | .setkey = aead_setkey, |
| 2463 | .setauthsize = aead_setauthsize, |
| 2464 | .encrypt = aead_encrypt, |
| 2465 | .decrypt = aead_decrypt, |
| 2466 | .givencrypt = aead_givencrypt, |
| 2467 | .geniv = "<built-in>", |
| 2468 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2469 | .maxauthsize = SHA384_DIGEST_SIZE, |
| 2470 | }, |
| 2471 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 2472 | .class2_alg_type = OP_ALG_ALGSEL_SHA384 | |
| 2473 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2474 | .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC, |
| 2475 | }, |
| 2476 | { |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2477 | .name = "authenc(hmac(sha512),cbc(des3_ede))", |
| 2478 | .driver_name = "authenc-hmac-sha512-cbc-des3_ede-caam", |
| 2479 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2480 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2481 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2482 | .setkey = aead_setkey, |
| 2483 | .setauthsize = aead_setauthsize, |
| 2484 | .encrypt = aead_encrypt, |
| 2485 | .decrypt = aead_decrypt, |
| 2486 | .givencrypt = aead_givencrypt, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2487 | .geniv = "<built-in>", |
| 2488 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2489 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 2490 | }, |
| 2491 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 2492 | .class2_alg_type = OP_ALG_ALGSEL_SHA512 | |
| 2493 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2494 | .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, |
| 2495 | }, |
| 2496 | { |
Kim Phillips | 8b4d43a | 2011-11-21 16:13:27 +0800 | [diff] [blame] | 2497 | .name = "authenc(hmac(md5),cbc(des))", |
| 2498 | .driver_name = "authenc-hmac-md5-cbc-des-caam", |
| 2499 | .blocksize = DES_BLOCK_SIZE, |
| 2500 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2501 | .template_aead = { |
| 2502 | .setkey = aead_setkey, |
| 2503 | .setauthsize = aead_setauthsize, |
| 2504 | .encrypt = aead_encrypt, |
| 2505 | .decrypt = aead_decrypt, |
| 2506 | .givencrypt = aead_givencrypt, |
| 2507 | .geniv = "<built-in>", |
| 2508 | .ivsize = DES_BLOCK_SIZE, |
| 2509 | .maxauthsize = MD5_DIGEST_SIZE, |
| 2510 | }, |
| 2511 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 2512 | .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2513 | .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC, |
| 2514 | }, |
| 2515 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2516 | .name = "authenc(hmac(sha1),cbc(des))", |
| 2517 | .driver_name = "authenc-hmac-sha1-cbc-des-caam", |
| 2518 | .blocksize = DES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2519 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2520 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2521 | .setkey = aead_setkey, |
| 2522 | .setauthsize = aead_setauthsize, |
| 2523 | .encrypt = aead_encrypt, |
| 2524 | .decrypt = aead_decrypt, |
| 2525 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2526 | .geniv = "<built-in>", |
| 2527 | .ivsize = DES_BLOCK_SIZE, |
| 2528 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2529 | }, |
| 2530 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 2531 | .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2532 | .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, |
| 2533 | }, |
| 2534 | { |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2535 | .name = "authenc(hmac(sha224),cbc(des))", |
| 2536 | .driver_name = "authenc-hmac-sha224-cbc-des-caam", |
| 2537 | .blocksize = DES_BLOCK_SIZE, |
Vakul Garg | cb7d566 | 2013-03-12 14:09:24 +0530 | [diff] [blame] | 2538 | .type = CRYPTO_ALG_TYPE_AEAD, |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2539 | .template_aead = { |
| 2540 | .setkey = aead_setkey, |
| 2541 | .setauthsize = aead_setauthsize, |
| 2542 | .encrypt = aead_encrypt, |
| 2543 | .decrypt = aead_decrypt, |
| 2544 | .givencrypt = aead_givencrypt, |
| 2545 | .geniv = "<built-in>", |
| 2546 | .ivsize = DES_BLOCK_SIZE, |
| 2547 | .maxauthsize = SHA224_DIGEST_SIZE, |
| 2548 | }, |
| 2549 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 2550 | .class2_alg_type = OP_ALG_ALGSEL_SHA224 | |
| 2551 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2552 | .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC, |
| 2553 | }, |
| 2554 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2555 | .name = "authenc(hmac(sha256),cbc(des))", |
| 2556 | .driver_name = "authenc-hmac-sha256-cbc-des-caam", |
| 2557 | .blocksize = DES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2558 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2559 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2560 | .setkey = aead_setkey, |
| 2561 | .setauthsize = aead_setauthsize, |
| 2562 | .encrypt = aead_encrypt, |
| 2563 | .decrypt = aead_decrypt, |
| 2564 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2565 | .geniv = "<built-in>", |
| 2566 | .ivsize = DES_BLOCK_SIZE, |
| 2567 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 2568 | }, |
| 2569 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 2570 | .class2_alg_type = OP_ALG_ALGSEL_SHA256 | |
| 2571 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2572 | .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, |
| 2573 | }, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2574 | { |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2575 | .name = "authenc(hmac(sha384),cbc(des))", |
| 2576 | .driver_name = "authenc-hmac-sha384-cbc-des-caam", |
| 2577 | .blocksize = DES_BLOCK_SIZE, |
Vakul Garg | cb7d566 | 2013-03-12 14:09:24 +0530 | [diff] [blame] | 2578 | .type = CRYPTO_ALG_TYPE_AEAD, |
Hemant Agrawal | e863f9cc | 2012-01-09 18:26:44 -0600 | [diff] [blame] | 2579 | .template_aead = { |
| 2580 | .setkey = aead_setkey, |
| 2581 | .setauthsize = aead_setauthsize, |
| 2582 | .encrypt = aead_encrypt, |
| 2583 | .decrypt = aead_decrypt, |
| 2584 | .givencrypt = aead_givencrypt, |
| 2585 | .geniv = "<built-in>", |
| 2586 | .ivsize = DES_BLOCK_SIZE, |
| 2587 | .maxauthsize = SHA384_DIGEST_SIZE, |
| 2588 | }, |
| 2589 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 2590 | .class2_alg_type = OP_ALG_ALGSEL_SHA384 | |
| 2591 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2592 | .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC, |
| 2593 | }, |
| 2594 | { |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2595 | .name = "authenc(hmac(sha512),cbc(des))", |
| 2596 | .driver_name = "authenc-hmac-sha512-cbc-des-caam", |
| 2597 | .blocksize = DES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2598 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2599 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2600 | .setkey = aead_setkey, |
| 2601 | .setauthsize = aead_setauthsize, |
| 2602 | .encrypt = aead_encrypt, |
| 2603 | .decrypt = aead_decrypt, |
| 2604 | .givencrypt = aead_givencrypt, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2605 | .geniv = "<built-in>", |
| 2606 | .ivsize = DES_BLOCK_SIZE, |
| 2607 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 2608 | }, |
| 2609 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 2610 | .class2_alg_type = OP_ALG_ALGSEL_SHA512 | |
| 2611 | OP_ALG_AAI_HMAC_PRECOMP, |
| 2612 | .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, |
| 2613 | }, |
Tudor Ambarus | 3ef8d94 | 2014-10-23 16:11:23 +0300 | [diff] [blame^] | 2614 | /* Galois Counter Mode */ |
| 2615 | { |
| 2616 | .name = "gcm(aes)", |
| 2617 | .driver_name = "gcm-aes-caam", |
| 2618 | .blocksize = 1, |
| 2619 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 2620 | .template_aead = { |
| 2621 | .setkey = gcm_setkey, |
| 2622 | .setauthsize = gcm_setauthsize, |
| 2623 | .encrypt = aead_encrypt, |
| 2624 | .decrypt = aead_decrypt, |
| 2625 | .givencrypt = NULL, |
| 2626 | .geniv = "<built-in>", |
| 2627 | .ivsize = 12, |
| 2628 | .maxauthsize = AES_BLOCK_SIZE, |
| 2629 | }, |
| 2630 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_GCM, |
| 2631 | }, |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2632 | /* ablkcipher descriptor */ |
| 2633 | { |
| 2634 | .name = "cbc(aes)", |
| 2635 | .driver_name = "cbc-aes-caam", |
| 2636 | .blocksize = AES_BLOCK_SIZE, |
| 2637 | .type = CRYPTO_ALG_TYPE_ABLKCIPHER, |
| 2638 | .template_ablkcipher = { |
| 2639 | .setkey = ablkcipher_setkey, |
| 2640 | .encrypt = ablkcipher_encrypt, |
| 2641 | .decrypt = ablkcipher_decrypt, |
| 2642 | .geniv = "eseqiv", |
| 2643 | .min_keysize = AES_MIN_KEY_SIZE, |
| 2644 | .max_keysize = AES_MAX_KEY_SIZE, |
| 2645 | .ivsize = AES_BLOCK_SIZE, |
| 2646 | }, |
| 2647 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 2648 | }, |
| 2649 | { |
| 2650 | .name = "cbc(des3_ede)", |
| 2651 | .driver_name = "cbc-3des-caam", |
| 2652 | .blocksize = DES3_EDE_BLOCK_SIZE, |
| 2653 | .type = CRYPTO_ALG_TYPE_ABLKCIPHER, |
| 2654 | .template_ablkcipher = { |
| 2655 | .setkey = ablkcipher_setkey, |
| 2656 | .encrypt = ablkcipher_encrypt, |
| 2657 | .decrypt = ablkcipher_decrypt, |
| 2658 | .geniv = "eseqiv", |
| 2659 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 2660 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 2661 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2662 | }, |
| 2663 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 2664 | }, |
| 2665 | { |
| 2666 | .name = "cbc(des)", |
| 2667 | .driver_name = "cbc-des-caam", |
| 2668 | .blocksize = DES_BLOCK_SIZE, |
| 2669 | .type = CRYPTO_ALG_TYPE_ABLKCIPHER, |
| 2670 | .template_ablkcipher = { |
| 2671 | .setkey = ablkcipher_setkey, |
| 2672 | .encrypt = ablkcipher_encrypt, |
| 2673 | .decrypt = ablkcipher_decrypt, |
| 2674 | .geniv = "eseqiv", |
| 2675 | .min_keysize = DES_KEY_SIZE, |
| 2676 | .max_keysize = DES_KEY_SIZE, |
| 2677 | .ivsize = DES_BLOCK_SIZE, |
| 2678 | }, |
| 2679 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 2680 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2681 | }; |
| 2682 | |
| 2683 | struct caam_crypto_alg { |
| 2684 | struct list_head entry; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2685 | int class1_alg_type; |
| 2686 | int class2_alg_type; |
| 2687 | int alg_op; |
| 2688 | struct crypto_alg crypto_alg; |
| 2689 | }; |
| 2690 | |
| 2691 | static int caam_cra_init(struct crypto_tfm *tfm) |
| 2692 | { |
| 2693 | struct crypto_alg *alg = tfm->__crt_alg; |
| 2694 | struct caam_crypto_alg *caam_alg = |
| 2695 | container_of(alg, struct caam_crypto_alg, crypto_alg); |
| 2696 | struct caam_ctx *ctx = crypto_tfm_ctx(tfm); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2697 | |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2698 | ctx->jrdev = caam_jr_alloc(); |
| 2699 | if (IS_ERR(ctx->jrdev)) { |
| 2700 | pr_err("Job Ring Device allocation for transform failed\n"); |
| 2701 | return PTR_ERR(ctx->jrdev); |
| 2702 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2703 | |
| 2704 | /* copy descriptor header template value */ |
| 2705 | ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam_alg->class1_alg_type; |
| 2706 | ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam_alg->class2_alg_type; |
| 2707 | ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_alg->alg_op; |
| 2708 | |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
| 2712 | static void caam_cra_exit(struct crypto_tfm *tfm) |
| 2713 | { |
| 2714 | struct caam_ctx *ctx = crypto_tfm_ctx(tfm); |
| 2715 | |
Yuan Kang | 1acebad | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2716 | if (ctx->sh_desc_enc_dma && |
| 2717 | !dma_mapping_error(ctx->jrdev, ctx->sh_desc_enc_dma)) |
| 2718 | dma_unmap_single(ctx->jrdev, ctx->sh_desc_enc_dma, |
| 2719 | desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE); |
| 2720 | if (ctx->sh_desc_dec_dma && |
| 2721 | !dma_mapping_error(ctx->jrdev, ctx->sh_desc_dec_dma)) |
| 2722 | dma_unmap_single(ctx->jrdev, ctx->sh_desc_dec_dma, |
| 2723 | desc_bytes(ctx->sh_desc_dec), DMA_TO_DEVICE); |
| 2724 | if (ctx->sh_desc_givenc_dma && |
| 2725 | !dma_mapping_error(ctx->jrdev, ctx->sh_desc_givenc_dma)) |
| 2726 | dma_unmap_single(ctx->jrdev, ctx->sh_desc_givenc_dma, |
| 2727 | desc_bytes(ctx->sh_desc_givenc), |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 2728 | DMA_TO_DEVICE); |
Horia Geanta | ec31eed | 2014-03-14 17:48:30 +0200 | [diff] [blame] | 2729 | if (ctx->key_dma && |
| 2730 | !dma_mapping_error(ctx->jrdev, ctx->key_dma)) |
| 2731 | dma_unmap_single(ctx->jrdev, ctx->key_dma, |
| 2732 | ctx->enckeylen + ctx->split_key_pad_len, |
| 2733 | DMA_TO_DEVICE); |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2734 | |
| 2735 | caam_jr_free(ctx->jrdev); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | static void __exit caam_algapi_exit(void) |
| 2739 | { |
| 2740 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2741 | struct caam_crypto_alg *t_alg, *n; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2742 | |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2743 | if (!alg_list.next) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2744 | return; |
| 2745 | |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2746 | list_for_each_entry_safe(t_alg, n, &alg_list, entry) { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2747 | crypto_unregister_alg(&t_alg->crypto_alg); |
| 2748 | list_del(&t_alg->entry); |
| 2749 | kfree(t_alg); |
| 2750 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2751 | } |
| 2752 | |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2753 | static struct caam_crypto_alg *caam_alg_alloc(struct caam_alg_template |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2754 | *template) |
| 2755 | { |
| 2756 | struct caam_crypto_alg *t_alg; |
| 2757 | struct crypto_alg *alg; |
| 2758 | |
| 2759 | t_alg = kzalloc(sizeof(struct caam_crypto_alg), GFP_KERNEL); |
| 2760 | if (!t_alg) { |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2761 | pr_err("failed to allocate t_alg\n"); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2762 | return ERR_PTR(-ENOMEM); |
| 2763 | } |
| 2764 | |
| 2765 | alg = &t_alg->crypto_alg; |
| 2766 | |
| 2767 | snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name); |
| 2768 | snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", |
| 2769 | template->driver_name); |
| 2770 | alg->cra_module = THIS_MODULE; |
| 2771 | alg->cra_init = caam_cra_init; |
| 2772 | alg->cra_exit = caam_cra_exit; |
| 2773 | alg->cra_priority = CAAM_CRA_PRIORITY; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2774 | alg->cra_blocksize = template->blocksize; |
| 2775 | alg->cra_alignmask = 0; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2776 | alg->cra_ctxsize = sizeof(struct caam_ctx); |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 2777 | alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 2778 | template->type; |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2779 | switch (template->type) { |
Yuan Kang | acdca31 | 2011-07-15 11:21:42 +0800 | [diff] [blame] | 2780 | case CRYPTO_ALG_TYPE_ABLKCIPHER: |
| 2781 | alg->cra_type = &crypto_ablkcipher_type; |
| 2782 | alg->cra_ablkcipher = template->template_ablkcipher; |
| 2783 | break; |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 2784 | case CRYPTO_ALG_TYPE_AEAD: |
| 2785 | alg->cra_type = &crypto_aead_type; |
| 2786 | alg->cra_aead = template->template_aead; |
| 2787 | break; |
| 2788 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2789 | |
| 2790 | t_alg->class1_alg_type = template->class1_alg_type; |
| 2791 | t_alg->class2_alg_type = template->class2_alg_type; |
| 2792 | t_alg->alg_op = template->alg_op; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2793 | |
| 2794 | return t_alg; |
| 2795 | } |
| 2796 | |
| 2797 | static int __init caam_algapi_init(void) |
| 2798 | { |
Ruchika Gupta | 35af640 | 2014-07-07 10:42:12 +0530 | [diff] [blame] | 2799 | struct device_node *dev_node; |
| 2800 | struct platform_device *pdev; |
| 2801 | struct device *ctrldev; |
| 2802 | void *priv; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2803 | int i = 0, err = 0; |
| 2804 | |
Ruchika Gupta | 35af640 | 2014-07-07 10:42:12 +0530 | [diff] [blame] | 2805 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); |
| 2806 | if (!dev_node) { |
| 2807 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0"); |
| 2808 | if (!dev_node) |
| 2809 | return -ENODEV; |
| 2810 | } |
| 2811 | |
| 2812 | pdev = of_find_device_by_node(dev_node); |
| 2813 | if (!pdev) { |
| 2814 | of_node_put(dev_node); |
| 2815 | return -ENODEV; |
| 2816 | } |
| 2817 | |
| 2818 | ctrldev = &pdev->dev; |
| 2819 | priv = dev_get_drvdata(ctrldev); |
| 2820 | of_node_put(dev_node); |
| 2821 | |
| 2822 | /* |
| 2823 | * If priv is NULL, it's probably because the caam driver wasn't |
| 2824 | * properly initialized (e.g. RNG4 init failed). Thus, bail out here. |
| 2825 | */ |
| 2826 | if (!priv) |
| 2827 | return -ENODEV; |
| 2828 | |
| 2829 | |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2830 | INIT_LIST_HEAD(&alg_list); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2831 | |
| 2832 | /* register crypto algorithms the device supports */ |
| 2833 | for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { |
| 2834 | /* TODO: check if h/w supports alg */ |
| 2835 | struct caam_crypto_alg *t_alg; |
| 2836 | |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2837 | t_alg = caam_alg_alloc(&driver_algs[i]); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2838 | if (IS_ERR(t_alg)) { |
| 2839 | err = PTR_ERR(t_alg); |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2840 | pr_warn("%s alg allocation failed\n", |
| 2841 | driver_algs[i].driver_name); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2842 | continue; |
| 2843 | } |
| 2844 | |
| 2845 | err = crypto_register_alg(&t_alg->crypto_alg); |
| 2846 | if (err) { |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2847 | pr_warn("%s alg registration failed\n", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2848 | t_alg->crypto_alg.cra_driver_name); |
| 2849 | kfree(t_alg); |
Horia Geanta | 246bbed | 2013-03-20 16:31:58 +0200 | [diff] [blame] | 2850 | } else |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2851 | list_add_tail(&t_alg->entry, &alg_list); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2852 | } |
Ruchika Gupta | cfc6f11 | 2013-10-25 12:01:03 +0530 | [diff] [blame] | 2853 | if (!list_empty(&alg_list)) |
| 2854 | pr_info("caam algorithms registered in /proc/crypto\n"); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2855 | |
| 2856 | return err; |
| 2857 | } |
| 2858 | |
| 2859 | module_init(caam_algapi_init); |
| 2860 | module_exit(caam_algapi_exit); |
| 2861 | |
| 2862 | MODULE_LICENSE("GPL"); |
| 2863 | MODULE_DESCRIPTION("FSL CAAM support for crypto API"); |
| 2864 | MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); |