Horia Geantă | 618b5dc | 2018-10-10 14:26:48 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 2 | /* |
| 3 | * caam - Freescale FSL CAAM support for Public Key Cryptography |
| 4 | * |
| 5 | * Copyright 2016 Freescale Semiconductor, Inc. |
Horia Geantă | a5e5c13 | 2019-05-03 17:17:38 +0300 | [diff] [blame] | 6 | * Copyright 2018-2019 NXP |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 7 | * |
| 8 | * There is no Shared Descriptor for PKC so that the Job Descriptor must carry |
| 9 | * all the desired key parameters, input and output pointers. |
| 10 | */ |
| 11 | #include "compat.h" |
| 12 | #include "regs.h" |
| 13 | #include "intern.h" |
| 14 | #include "jr.h" |
| 15 | #include "error.h" |
| 16 | #include "desc_constr.h" |
| 17 | #include "sg_sw_sec4.h" |
| 18 | #include "caampkc.h" |
| 19 | |
Andrey Smirnov | a1cf573 | 2019-08-20 13:23:59 -0700 | [diff] [blame] | 20 | #define DESC_RSA_PUB_LEN (2 * CAAM_CMD_SZ + SIZEOF_RSA_PUB_PDB) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 21 | #define DESC_RSA_PRIV_F1_LEN (2 * CAAM_CMD_SZ + \ |
Andrey Smirnov | a1cf573 | 2019-08-20 13:23:59 -0700 | [diff] [blame] | 22 | SIZEOF_RSA_PRIV_F1_PDB) |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 23 | #define DESC_RSA_PRIV_F2_LEN (2 * CAAM_CMD_SZ + \ |
Andrey Smirnov | a1cf573 | 2019-08-20 13:23:59 -0700 | [diff] [blame] | 24 | SIZEOF_RSA_PRIV_F2_PDB) |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 25 | #define DESC_RSA_PRIV_F3_LEN (2 * CAAM_CMD_SZ + \ |
Andrey Smirnov | a1cf573 | 2019-08-20 13:23:59 -0700 | [diff] [blame] | 26 | SIZEOF_RSA_PRIV_F3_PDB) |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 27 | #define CAAM_RSA_MAX_INPUT_SIZE 512 /* for a 4096-bit modulus */ |
| 28 | |
| 29 | /* buffer filled with zeros, used for padding */ |
| 30 | static u8 *zero_buffer; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 31 | |
Iuliana Prodan | 4e3a61c | 2019-07-31 16:08:13 +0300 | [diff] [blame] | 32 | /* |
| 33 | * variable used to avoid double free of resources in case |
| 34 | * algorithm registration was unsuccessful |
| 35 | */ |
| 36 | static bool init_done; |
| 37 | |
Iuliana Prodan | 58068cf | 2019-07-31 16:08:14 +0300 | [diff] [blame] | 38 | struct caam_akcipher_alg { |
| 39 | struct akcipher_alg akcipher; |
| 40 | bool registered; |
| 41 | }; |
| 42 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 43 | static void rsa_io_unmap(struct device *dev, struct rsa_edesc *edesc, |
| 44 | struct akcipher_request *req) |
| 45 | { |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 46 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
| 47 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 48 | dma_unmap_sg(dev, req->dst, edesc->dst_nents, DMA_FROM_DEVICE); |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 49 | dma_unmap_sg(dev, req_ctx->fixup_src, edesc->src_nents, DMA_TO_DEVICE); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 50 | |
| 51 | if (edesc->sec4_sg_bytes) |
| 52 | dma_unmap_single(dev, edesc->sec4_sg_dma, edesc->sec4_sg_bytes, |
| 53 | DMA_TO_DEVICE); |
| 54 | } |
| 55 | |
| 56 | static void rsa_pub_unmap(struct device *dev, struct rsa_edesc *edesc, |
| 57 | struct akcipher_request *req) |
| 58 | { |
| 59 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 60 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 61 | struct caam_rsa_key *key = &ctx->key; |
| 62 | struct rsa_pub_pdb *pdb = &edesc->pdb.pub; |
| 63 | |
| 64 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); |
| 65 | dma_unmap_single(dev, pdb->e_dma, key->e_sz, DMA_TO_DEVICE); |
| 66 | } |
| 67 | |
| 68 | static void rsa_priv_f1_unmap(struct device *dev, struct rsa_edesc *edesc, |
| 69 | struct akcipher_request *req) |
| 70 | { |
| 71 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 72 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 73 | struct caam_rsa_key *key = &ctx->key; |
| 74 | struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1; |
| 75 | |
| 76 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); |
| 77 | dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE); |
| 78 | } |
| 79 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 80 | static void rsa_priv_f2_unmap(struct device *dev, struct rsa_edesc *edesc, |
| 81 | struct akcipher_request *req) |
| 82 | { |
| 83 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 84 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 85 | struct caam_rsa_key *key = &ctx->key; |
| 86 | struct rsa_priv_f2_pdb *pdb = &edesc->pdb.priv_f2; |
| 87 | size_t p_sz = key->p_sz; |
Horia Geantă | 4bffaab | 2018-04-27 11:40:11 +0300 | [diff] [blame] | 88 | size_t q_sz = key->q_sz; |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 89 | |
| 90 | dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE); |
| 91 | dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE); |
| 92 | dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE); |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 93 | dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL); |
| 94 | dma_unmap_single(dev, pdb->tmp2_dma, q_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 95 | } |
| 96 | |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 97 | static void rsa_priv_f3_unmap(struct device *dev, struct rsa_edesc *edesc, |
| 98 | struct akcipher_request *req) |
| 99 | { |
| 100 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 101 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 102 | struct caam_rsa_key *key = &ctx->key; |
| 103 | struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3; |
| 104 | size_t p_sz = key->p_sz; |
Horia Geantă | 4bffaab | 2018-04-27 11:40:11 +0300 | [diff] [blame] | 105 | size_t q_sz = key->q_sz; |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 106 | |
| 107 | dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE); |
| 108 | dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE); |
| 109 | dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE); |
| 110 | dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE); |
| 111 | dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE); |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 112 | dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL); |
| 113 | dma_unmap_single(dev, pdb->tmp2_dma, q_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 114 | } |
| 115 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 116 | /* RSA Job Completion handler */ |
| 117 | static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context) |
| 118 | { |
| 119 | struct akcipher_request *req = context; |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 120 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
| 121 | struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 122 | struct rsa_edesc *edesc; |
Horia Geantă | 1984aae | 2019-07-31 16:08:03 +0300 | [diff] [blame] | 123 | int ecode = 0; |
Iuliana Prodan | 80994e3 | 2020-04-07 01:47:28 +0300 | [diff] [blame] | 124 | bool has_bklog; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 125 | |
| 126 | if (err) |
Horia Geantă | 1984aae | 2019-07-31 16:08:03 +0300 | [diff] [blame] | 127 | ecode = caam_jr_strstatus(dev, err); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 128 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 129 | edesc = req_ctx->edesc; |
Iuliana Prodan | 80994e3 | 2020-04-07 01:47:28 +0300 | [diff] [blame] | 130 | has_bklog = edesc->bklog; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 131 | |
| 132 | rsa_pub_unmap(dev, edesc, req); |
| 133 | rsa_io_unmap(dev, edesc, req); |
| 134 | kfree(edesc); |
| 135 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 136 | /* |
| 137 | * If no backlog flag, the completion of the request is done |
| 138 | * by CAAM, not crypto engine. |
| 139 | */ |
Iuliana Prodan | 80994e3 | 2020-04-07 01:47:28 +0300 | [diff] [blame] | 140 | if (!has_bklog) |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 141 | akcipher_request_complete(req, ecode); |
| 142 | else |
| 143 | crypto_finalize_akcipher_request(jrp->engine, req, ecode); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 144 | } |
| 145 | |
Iuliana Prodan | d53e44f | 2020-02-12 19:55:19 +0200 | [diff] [blame] | 146 | static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err, |
| 147 | void *context) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 148 | { |
| 149 | struct akcipher_request *req = context; |
Iuliana Prodan | d53e44f | 2020-02-12 19:55:19 +0200 | [diff] [blame] | 150 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 151 | struct caam_drv_private_jr *jrp = dev_get_drvdata(dev); |
Iuliana Prodan | d53e44f | 2020-02-12 19:55:19 +0200 | [diff] [blame] | 152 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 153 | struct caam_rsa_key *key = &ctx->key; |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 154 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 155 | struct rsa_edesc *edesc; |
Horia Geantă | 1984aae | 2019-07-31 16:08:03 +0300 | [diff] [blame] | 156 | int ecode = 0; |
Iuliana Prodan | 80994e3 | 2020-04-07 01:47:28 +0300 | [diff] [blame] | 157 | bool has_bklog; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 158 | |
| 159 | if (err) |
Horia Geantă | 1984aae | 2019-07-31 16:08:03 +0300 | [diff] [blame] | 160 | ecode = caam_jr_strstatus(dev, err); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 161 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 162 | edesc = req_ctx->edesc; |
Iuliana Prodan | 80994e3 | 2020-04-07 01:47:28 +0300 | [diff] [blame] | 163 | has_bklog = edesc->bklog; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 164 | |
Iuliana Prodan | d53e44f | 2020-02-12 19:55:19 +0200 | [diff] [blame] | 165 | switch (key->priv_form) { |
| 166 | case FORM1: |
| 167 | rsa_priv_f1_unmap(dev, edesc, req); |
| 168 | break; |
| 169 | case FORM2: |
| 170 | rsa_priv_f2_unmap(dev, edesc, req); |
| 171 | break; |
| 172 | case FORM3: |
| 173 | rsa_priv_f3_unmap(dev, edesc, req); |
| 174 | } |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 175 | |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 176 | rsa_io_unmap(dev, edesc, req); |
| 177 | kfree(edesc); |
| 178 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 179 | /* |
| 180 | * If no backlog flag, the completion of the request is done |
| 181 | * by CAAM, not crypto engine. |
| 182 | */ |
Iuliana Prodan | 80994e3 | 2020-04-07 01:47:28 +0300 | [diff] [blame] | 183 | if (!has_bklog) |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 184 | akcipher_request_complete(req, ecode); |
| 185 | else |
| 186 | crypto_finalize_akcipher_request(jrp->engine, req, ecode); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 187 | } |
| 188 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 189 | /** |
Lee Jones | 0beb2b6 | 2021-03-18 12:44:19 +0000 | [diff] [blame] | 190 | * caam_rsa_count_leading_zeros - Count leading zeros, need it to strip, |
| 191 | * from a given scatterlist |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 192 | * |
| 193 | * @sgl : scatterlist to count zeros from |
| 194 | * @nbytes: number of zeros, in bytes, to strip |
| 195 | * @flags : operation flags |
| 196 | */ |
Horia Geantă | 8a2a0dd | 2018-04-16 08:07:05 -0500 | [diff] [blame] | 197 | static int caam_rsa_count_leading_zeros(struct scatterlist *sgl, |
| 198 | unsigned int nbytes, |
| 199 | unsigned int flags) |
| 200 | { |
| 201 | struct sg_mapping_iter miter; |
| 202 | int lzeros, ents; |
| 203 | unsigned int len; |
| 204 | unsigned int tbytes = nbytes; |
| 205 | const u8 *buff; |
| 206 | |
| 207 | ents = sg_nents_for_len(sgl, nbytes); |
| 208 | if (ents < 0) |
| 209 | return ents; |
| 210 | |
| 211 | sg_miter_start(&miter, sgl, ents, SG_MITER_FROM_SG | flags); |
| 212 | |
| 213 | lzeros = 0; |
| 214 | len = 0; |
| 215 | while (nbytes > 0) { |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 216 | /* do not strip more than given bytes */ |
| 217 | while (len && !*buff && lzeros < nbytes) { |
Horia Geantă | 8a2a0dd | 2018-04-16 08:07:05 -0500 | [diff] [blame] | 218 | lzeros++; |
| 219 | len--; |
| 220 | buff++; |
| 221 | } |
| 222 | |
| 223 | if (len && *buff) |
| 224 | break; |
| 225 | |
| 226 | sg_miter_next(&miter); |
| 227 | buff = miter.addr; |
| 228 | len = miter.length; |
| 229 | |
| 230 | nbytes -= lzeros; |
| 231 | lzeros = 0; |
| 232 | } |
| 233 | |
| 234 | miter.consumed = lzeros; |
| 235 | sg_miter_stop(&miter); |
| 236 | nbytes -= lzeros; |
| 237 | |
| 238 | return tbytes - nbytes; |
| 239 | } |
| 240 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 241 | static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, |
| 242 | size_t desclen) |
| 243 | { |
| 244 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 245 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 246 | struct device *dev = ctx->dev; |
Horia Geantă | 8a2a0dd | 2018-04-16 08:07:05 -0500 | [diff] [blame] | 247 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 248 | struct caam_rsa_key *key = &ctx->key; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 249 | struct rsa_edesc *edesc; |
Horia Geantă | 019d62d | 2017-06-19 11:44:46 +0300 | [diff] [blame] | 250 | gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? |
| 251 | GFP_KERNEL : GFP_ATOMIC; |
Horia Geantă | 8a2a0dd | 2018-04-16 08:07:05 -0500 | [diff] [blame] | 252 | int sg_flags = (flags == GFP_ATOMIC) ? SG_MITER_ATOMIC : 0; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 253 | int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes; |
| 254 | int src_nents, dst_nents; |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 255 | int mapped_src_nents, mapped_dst_nents; |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 256 | unsigned int diff_size = 0; |
Horia Geantă | 8a2a0dd | 2018-04-16 08:07:05 -0500 | [diff] [blame] | 257 | int lzeros; |
| 258 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 259 | if (req->src_len > key->n_sz) { |
| 260 | /* |
| 261 | * strip leading zeros and |
| 262 | * return the number of zeros to skip |
| 263 | */ |
| 264 | lzeros = caam_rsa_count_leading_zeros(req->src, req->src_len - |
| 265 | key->n_sz, sg_flags); |
| 266 | if (lzeros < 0) |
| 267 | return ERR_PTR(lzeros); |
Horia Geantă | 8a2a0dd | 2018-04-16 08:07:05 -0500 | [diff] [blame] | 268 | |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 269 | req_ctx->fixup_src = scatterwalk_ffwd(req_ctx->src, req->src, |
| 270 | lzeros); |
| 271 | req_ctx->fixup_src_len = req->src_len - lzeros; |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 272 | } else { |
| 273 | /* |
| 274 | * input src is less then n key modulus, |
| 275 | * so there will be zero padding |
| 276 | */ |
| 277 | diff_size = key->n_sz - req->src_len; |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 278 | req_ctx->fixup_src = req->src; |
| 279 | req_ctx->fixup_src_len = req->src_len; |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 280 | } |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 281 | |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 282 | src_nents = sg_nents_for_len(req_ctx->fixup_src, |
| 283 | req_ctx->fixup_src_len); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 284 | dst_nents = sg_nents_for_len(req->dst, req->dst_len); |
| 285 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 286 | mapped_src_nents = dma_map_sg(dev, req_ctx->fixup_src, src_nents, |
| 287 | DMA_TO_DEVICE); |
| 288 | if (unlikely(!mapped_src_nents)) { |
| 289 | dev_err(dev, "unable to map source\n"); |
| 290 | return ERR_PTR(-ENOMEM); |
| 291 | } |
| 292 | mapped_dst_nents = dma_map_sg(dev, req->dst, dst_nents, |
| 293 | DMA_FROM_DEVICE); |
| 294 | if (unlikely(!mapped_dst_nents)) { |
| 295 | dev_err(dev, "unable to map destination\n"); |
| 296 | goto src_fail; |
| 297 | } |
| 298 | |
| 299 | if (!diff_size && mapped_src_nents == 1) |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 300 | sec4_sg_len = 0; /* no need for an input hw s/g table */ |
| 301 | else |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 302 | sec4_sg_len = mapped_src_nents + !!diff_size; |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 303 | sec4_sg_index = sec4_sg_len; |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 304 | |
| 305 | if (mapped_dst_nents > 1) |
| 306 | sec4_sg_len += pad_sg_nents(mapped_dst_nents); |
Horia Geantă | a5e5c13 | 2019-05-03 17:17:38 +0300 | [diff] [blame] | 307 | else |
| 308 | sec4_sg_len = pad_sg_nents(sec4_sg_len); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 309 | |
| 310 | sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry); |
| 311 | |
| 312 | /* allocate space for base edesc, hw desc commands and link tables */ |
| 313 | edesc = kzalloc(sizeof(*edesc) + desclen + sec4_sg_bytes, |
| 314 | GFP_DMA | flags); |
| 315 | if (!edesc) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 316 | goto dst_fail; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 317 | |
| 318 | edesc->sec4_sg = (void *)edesc + sizeof(*edesc) + desclen; |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 319 | if (diff_size) |
| 320 | dma_to_sec4_sg_one(edesc->sec4_sg, ctx->padding_dma, diff_size, |
| 321 | 0); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 322 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 323 | if (sec4_sg_index) |
Horia Geantă | 059d73e | 2019-06-10 16:30:58 +0300 | [diff] [blame] | 324 | sg_to_sec4_sg_last(req_ctx->fixup_src, req_ctx->fixup_src_len, |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 325 | edesc->sec4_sg + !!diff_size, 0); |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 326 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 327 | if (mapped_dst_nents > 1) |
Horia Geantă | 059d73e | 2019-06-10 16:30:58 +0300 | [diff] [blame] | 328 | sg_to_sec4_sg_last(req->dst, req->dst_len, |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 329 | edesc->sec4_sg + sec4_sg_index, 0); |
| 330 | |
| 331 | /* Save nents for later use in Job Descriptor */ |
| 332 | edesc->src_nents = src_nents; |
| 333 | edesc->dst_nents = dst_nents; |
| 334 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 335 | req_ctx->edesc = edesc; |
| 336 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 337 | if (!sec4_sg_bytes) |
| 338 | return edesc; |
| 339 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 340 | edesc->mapped_src_nents = mapped_src_nents; |
| 341 | edesc->mapped_dst_nents = mapped_dst_nents; |
| 342 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 343 | edesc->sec4_sg_dma = dma_map_single(dev, edesc->sec4_sg, |
| 344 | sec4_sg_bytes, DMA_TO_DEVICE); |
| 345 | if (dma_mapping_error(dev, edesc->sec4_sg_dma)) { |
| 346 | dev_err(dev, "unable to map S/G table\n"); |
| 347 | goto sec4_sg_fail; |
| 348 | } |
| 349 | |
| 350 | edesc->sec4_sg_bytes = sec4_sg_bytes; |
| 351 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 352 | print_hex_dump_debug("caampkc sec4_sg@" __stringify(__LINE__) ": ", |
| 353 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg, |
| 354 | edesc->sec4_sg_bytes, 1); |
| 355 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 356 | return edesc; |
| 357 | |
| 358 | sec4_sg_fail: |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 359 | kfree(edesc); |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 360 | dst_fail: |
| 361 | dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE); |
| 362 | src_fail: |
| 363 | dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 364 | return ERR_PTR(-ENOMEM); |
| 365 | } |
| 366 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 367 | static int akcipher_do_one_req(struct crypto_engine *engine, void *areq) |
| 368 | { |
| 369 | struct akcipher_request *req = container_of(areq, |
| 370 | struct akcipher_request, |
| 371 | base); |
| 372 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 373 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
| 374 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 375 | struct device *jrdev = ctx->dev; |
| 376 | u32 *desc = req_ctx->edesc->hw_desc; |
| 377 | int ret; |
| 378 | |
| 379 | req_ctx->edesc->bklog = true; |
| 380 | |
| 381 | ret = caam_jr_enqueue(jrdev, desc, req_ctx->akcipher_op_done, req); |
| 382 | |
| 383 | if (ret != -EINPROGRESS) { |
| 384 | rsa_pub_unmap(jrdev, req_ctx->edesc, req); |
| 385 | rsa_io_unmap(jrdev, req_ctx->edesc, req); |
| 386 | kfree(req_ctx->edesc); |
| 387 | } else { |
| 388 | ret = 0; |
| 389 | } |
| 390 | |
| 391 | return ret; |
| 392 | } |
| 393 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 394 | static int set_rsa_pub_pdb(struct akcipher_request *req, |
| 395 | struct rsa_edesc *edesc) |
| 396 | { |
| 397 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 398 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 399 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 400 | struct caam_rsa_key *key = &ctx->key; |
| 401 | struct device *dev = ctx->dev; |
| 402 | struct rsa_pub_pdb *pdb = &edesc->pdb.pub; |
| 403 | int sec4_sg_index = 0; |
| 404 | |
| 405 | pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE); |
| 406 | if (dma_mapping_error(dev, pdb->n_dma)) { |
| 407 | dev_err(dev, "Unable to map RSA modulus memory\n"); |
| 408 | return -ENOMEM; |
| 409 | } |
| 410 | |
| 411 | pdb->e_dma = dma_map_single(dev, key->e, key->e_sz, DMA_TO_DEVICE); |
| 412 | if (dma_mapping_error(dev, pdb->e_dma)) { |
| 413 | dev_err(dev, "Unable to map RSA public exponent memory\n"); |
| 414 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); |
| 415 | return -ENOMEM; |
| 416 | } |
| 417 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 418 | if (edesc->mapped_src_nents > 1) { |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 419 | pdb->sgf |= RSA_PDB_SGF_F; |
| 420 | pdb->f_dma = edesc->sec4_sg_dma; |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 421 | sec4_sg_index += edesc->mapped_src_nents; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 422 | } else { |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 423 | pdb->f_dma = sg_dma_address(req_ctx->fixup_src); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 424 | } |
| 425 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 426 | if (edesc->mapped_dst_nents > 1) { |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 427 | pdb->sgf |= RSA_PDB_SGF_G; |
| 428 | pdb->g_dma = edesc->sec4_sg_dma + |
| 429 | sec4_sg_index * sizeof(struct sec4_sg_entry); |
| 430 | } else { |
| 431 | pdb->g_dma = sg_dma_address(req->dst); |
| 432 | } |
| 433 | |
| 434 | pdb->sgf |= (key->e_sz << RSA_PDB_E_SHIFT) | key->n_sz; |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 435 | pdb->f_len = req_ctx->fixup_src_len; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int set_rsa_priv_f1_pdb(struct akcipher_request *req, |
| 441 | struct rsa_edesc *edesc) |
| 442 | { |
| 443 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 444 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 445 | struct caam_rsa_key *key = &ctx->key; |
| 446 | struct device *dev = ctx->dev; |
| 447 | struct rsa_priv_f1_pdb *pdb = &edesc->pdb.priv_f1; |
| 448 | int sec4_sg_index = 0; |
| 449 | |
| 450 | pdb->n_dma = dma_map_single(dev, key->n, key->n_sz, DMA_TO_DEVICE); |
| 451 | if (dma_mapping_error(dev, pdb->n_dma)) { |
| 452 | dev_err(dev, "Unable to map modulus memory\n"); |
| 453 | return -ENOMEM; |
| 454 | } |
| 455 | |
| 456 | pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE); |
| 457 | if (dma_mapping_error(dev, pdb->d_dma)) { |
| 458 | dev_err(dev, "Unable to map RSA private exponent memory\n"); |
| 459 | dma_unmap_single(dev, pdb->n_dma, key->n_sz, DMA_TO_DEVICE); |
| 460 | return -ENOMEM; |
| 461 | } |
| 462 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 463 | if (edesc->mapped_src_nents > 1) { |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 464 | pdb->sgf |= RSA_PRIV_PDB_SGF_G; |
| 465 | pdb->g_dma = edesc->sec4_sg_dma; |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 466 | sec4_sg_index += edesc->mapped_src_nents; |
| 467 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 468 | } else { |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 469 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
| 470 | |
| 471 | pdb->g_dma = sg_dma_address(req_ctx->fixup_src); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 472 | } |
| 473 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 474 | if (edesc->mapped_dst_nents > 1) { |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 475 | pdb->sgf |= RSA_PRIV_PDB_SGF_F; |
| 476 | pdb->f_dma = edesc->sec4_sg_dma + |
| 477 | sec4_sg_index * sizeof(struct sec4_sg_entry); |
| 478 | } else { |
| 479 | pdb->f_dma = sg_dma_address(req->dst); |
| 480 | } |
| 481 | |
| 482 | pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz; |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 487 | static int set_rsa_priv_f2_pdb(struct akcipher_request *req, |
| 488 | struct rsa_edesc *edesc) |
| 489 | { |
| 490 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 491 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 492 | struct caam_rsa_key *key = &ctx->key; |
| 493 | struct device *dev = ctx->dev; |
| 494 | struct rsa_priv_f2_pdb *pdb = &edesc->pdb.priv_f2; |
| 495 | int sec4_sg_index = 0; |
| 496 | size_t p_sz = key->p_sz; |
Horia Geantă | 4bffaab | 2018-04-27 11:40:11 +0300 | [diff] [blame] | 497 | size_t q_sz = key->q_sz; |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 498 | |
| 499 | pdb->d_dma = dma_map_single(dev, key->d, key->d_sz, DMA_TO_DEVICE); |
| 500 | if (dma_mapping_error(dev, pdb->d_dma)) { |
| 501 | dev_err(dev, "Unable to map RSA private exponent memory\n"); |
| 502 | return -ENOMEM; |
| 503 | } |
| 504 | |
| 505 | pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE); |
| 506 | if (dma_mapping_error(dev, pdb->p_dma)) { |
| 507 | dev_err(dev, "Unable to map RSA prime factor p memory\n"); |
| 508 | goto unmap_d; |
| 509 | } |
| 510 | |
| 511 | pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE); |
| 512 | if (dma_mapping_error(dev, pdb->q_dma)) { |
| 513 | dev_err(dev, "Unable to map RSA prime factor q memory\n"); |
| 514 | goto unmap_p; |
| 515 | } |
| 516 | |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 517 | pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 518 | if (dma_mapping_error(dev, pdb->tmp1_dma)) { |
| 519 | dev_err(dev, "Unable to map RSA tmp1 memory\n"); |
| 520 | goto unmap_q; |
| 521 | } |
| 522 | |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 523 | pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 524 | if (dma_mapping_error(dev, pdb->tmp2_dma)) { |
| 525 | dev_err(dev, "Unable to map RSA tmp2 memory\n"); |
| 526 | goto unmap_tmp1; |
| 527 | } |
| 528 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 529 | if (edesc->mapped_src_nents > 1) { |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 530 | pdb->sgf |= RSA_PRIV_PDB_SGF_G; |
| 531 | pdb->g_dma = edesc->sec4_sg_dma; |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 532 | sec4_sg_index += edesc->mapped_src_nents; |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 533 | } else { |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 534 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
| 535 | |
| 536 | pdb->g_dma = sg_dma_address(req_ctx->fixup_src); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 537 | } |
| 538 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 539 | if (edesc->mapped_dst_nents > 1) { |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 540 | pdb->sgf |= RSA_PRIV_PDB_SGF_F; |
| 541 | pdb->f_dma = edesc->sec4_sg_dma + |
| 542 | sec4_sg_index * sizeof(struct sec4_sg_entry); |
| 543 | } else { |
| 544 | pdb->f_dma = sg_dma_address(req->dst); |
| 545 | } |
| 546 | |
| 547 | pdb->sgf |= (key->d_sz << RSA_PDB_D_SHIFT) | key->n_sz; |
| 548 | pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz; |
| 549 | |
| 550 | return 0; |
| 551 | |
| 552 | unmap_tmp1: |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 553 | dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 554 | unmap_q: |
| 555 | dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE); |
| 556 | unmap_p: |
| 557 | dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE); |
| 558 | unmap_d: |
| 559 | dma_unmap_single(dev, pdb->d_dma, key->d_sz, DMA_TO_DEVICE); |
| 560 | |
| 561 | return -ENOMEM; |
| 562 | } |
| 563 | |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 564 | static int set_rsa_priv_f3_pdb(struct akcipher_request *req, |
| 565 | struct rsa_edesc *edesc) |
| 566 | { |
| 567 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 568 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 569 | struct caam_rsa_key *key = &ctx->key; |
| 570 | struct device *dev = ctx->dev; |
| 571 | struct rsa_priv_f3_pdb *pdb = &edesc->pdb.priv_f3; |
| 572 | int sec4_sg_index = 0; |
| 573 | size_t p_sz = key->p_sz; |
Horia Geantă | 4bffaab | 2018-04-27 11:40:11 +0300 | [diff] [blame] | 574 | size_t q_sz = key->q_sz; |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 575 | |
| 576 | pdb->p_dma = dma_map_single(dev, key->p, p_sz, DMA_TO_DEVICE); |
| 577 | if (dma_mapping_error(dev, pdb->p_dma)) { |
| 578 | dev_err(dev, "Unable to map RSA prime factor p memory\n"); |
| 579 | return -ENOMEM; |
| 580 | } |
| 581 | |
| 582 | pdb->q_dma = dma_map_single(dev, key->q, q_sz, DMA_TO_DEVICE); |
| 583 | if (dma_mapping_error(dev, pdb->q_dma)) { |
| 584 | dev_err(dev, "Unable to map RSA prime factor q memory\n"); |
| 585 | goto unmap_p; |
| 586 | } |
| 587 | |
| 588 | pdb->dp_dma = dma_map_single(dev, key->dp, p_sz, DMA_TO_DEVICE); |
| 589 | if (dma_mapping_error(dev, pdb->dp_dma)) { |
| 590 | dev_err(dev, "Unable to map RSA exponent dp memory\n"); |
| 591 | goto unmap_q; |
| 592 | } |
| 593 | |
| 594 | pdb->dq_dma = dma_map_single(dev, key->dq, q_sz, DMA_TO_DEVICE); |
| 595 | if (dma_mapping_error(dev, pdb->dq_dma)) { |
| 596 | dev_err(dev, "Unable to map RSA exponent dq memory\n"); |
| 597 | goto unmap_dp; |
| 598 | } |
| 599 | |
| 600 | pdb->c_dma = dma_map_single(dev, key->qinv, p_sz, DMA_TO_DEVICE); |
| 601 | if (dma_mapping_error(dev, pdb->c_dma)) { |
| 602 | dev_err(dev, "Unable to map RSA CRT coefficient qinv memory\n"); |
| 603 | goto unmap_dq; |
| 604 | } |
| 605 | |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 606 | pdb->tmp1_dma = dma_map_single(dev, key->tmp1, p_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 607 | if (dma_mapping_error(dev, pdb->tmp1_dma)) { |
| 608 | dev_err(dev, "Unable to map RSA tmp1 memory\n"); |
| 609 | goto unmap_qinv; |
| 610 | } |
| 611 | |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 612 | pdb->tmp2_dma = dma_map_single(dev, key->tmp2, q_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 613 | if (dma_mapping_error(dev, pdb->tmp2_dma)) { |
| 614 | dev_err(dev, "Unable to map RSA tmp2 memory\n"); |
| 615 | goto unmap_tmp1; |
| 616 | } |
| 617 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 618 | if (edesc->mapped_src_nents > 1) { |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 619 | pdb->sgf |= RSA_PRIV_PDB_SGF_G; |
| 620 | pdb->g_dma = edesc->sec4_sg_dma; |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 621 | sec4_sg_index += edesc->mapped_src_nents; |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 622 | } else { |
Iuliana Prodan | 3b2614c | 2019-05-28 12:52:11 +0300 | [diff] [blame] | 623 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
| 624 | |
| 625 | pdb->g_dma = sg_dma_address(req_ctx->fixup_src); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 626 | } |
| 627 | |
Iuliana Prodan | eff9771 | 2019-09-26 15:26:29 +0300 | [diff] [blame] | 628 | if (edesc->mapped_dst_nents > 1) { |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 629 | pdb->sgf |= RSA_PRIV_PDB_SGF_F; |
| 630 | pdb->f_dma = edesc->sec4_sg_dma + |
| 631 | sec4_sg_index * sizeof(struct sec4_sg_entry); |
| 632 | } else { |
| 633 | pdb->f_dma = sg_dma_address(req->dst); |
| 634 | } |
| 635 | |
| 636 | pdb->sgf |= key->n_sz; |
| 637 | pdb->p_q_len = (q_sz << RSA_PDB_Q_SHIFT) | p_sz; |
| 638 | |
| 639 | return 0; |
| 640 | |
| 641 | unmap_tmp1: |
Horia Geantă | f1bf9e6 | 2018-08-06 15:29:55 +0300 | [diff] [blame] | 642 | dma_unmap_single(dev, pdb->tmp1_dma, p_sz, DMA_BIDIRECTIONAL); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 643 | unmap_qinv: |
| 644 | dma_unmap_single(dev, pdb->c_dma, p_sz, DMA_TO_DEVICE); |
| 645 | unmap_dq: |
| 646 | dma_unmap_single(dev, pdb->dq_dma, q_sz, DMA_TO_DEVICE); |
| 647 | unmap_dp: |
| 648 | dma_unmap_single(dev, pdb->dp_dma, p_sz, DMA_TO_DEVICE); |
| 649 | unmap_q: |
| 650 | dma_unmap_single(dev, pdb->q_dma, q_sz, DMA_TO_DEVICE); |
| 651 | unmap_p: |
| 652 | dma_unmap_single(dev, pdb->p_dma, p_sz, DMA_TO_DEVICE); |
| 653 | |
| 654 | return -ENOMEM; |
| 655 | } |
| 656 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 657 | static int akcipher_enqueue_req(struct device *jrdev, |
| 658 | void (*cbk)(struct device *jrdev, u32 *desc, |
| 659 | u32 err, void *context), |
| 660 | struct akcipher_request *req) |
| 661 | { |
| 662 | struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev); |
| 663 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 664 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 665 | struct caam_rsa_key *key = &ctx->key; |
| 666 | struct caam_rsa_req_ctx *req_ctx = akcipher_request_ctx(req); |
| 667 | struct rsa_edesc *edesc = req_ctx->edesc; |
| 668 | u32 *desc = edesc->hw_desc; |
| 669 | int ret; |
| 670 | |
| 671 | req_ctx->akcipher_op_done = cbk; |
| 672 | /* |
| 673 | * Only the backlog request are sent to crypto-engine since the others |
| 674 | * can be handled by CAAM, if free, especially since JR has up to 1024 |
| 675 | * entries (more than the 10 entries from crypto-engine). |
| 676 | */ |
| 677 | if (req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG) |
| 678 | ret = crypto_transfer_akcipher_request_to_engine(jrpriv->engine, |
| 679 | req); |
| 680 | else |
| 681 | ret = caam_jr_enqueue(jrdev, desc, cbk, req); |
| 682 | |
| 683 | if ((ret != -EINPROGRESS) && (ret != -EBUSY)) { |
| 684 | switch (key->priv_form) { |
| 685 | case FORM1: |
| 686 | rsa_priv_f1_unmap(jrdev, edesc, req); |
| 687 | break; |
| 688 | case FORM2: |
| 689 | rsa_priv_f2_unmap(jrdev, edesc, req); |
| 690 | break; |
| 691 | case FORM3: |
| 692 | rsa_priv_f3_unmap(jrdev, edesc, req); |
| 693 | break; |
| 694 | default: |
| 695 | rsa_pub_unmap(jrdev, edesc, req); |
| 696 | } |
| 697 | rsa_io_unmap(jrdev, edesc, req); |
| 698 | kfree(edesc); |
| 699 | } |
| 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 704 | static int caam_rsa_enc(struct akcipher_request *req) |
| 705 | { |
| 706 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 707 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 708 | struct caam_rsa_key *key = &ctx->key; |
| 709 | struct device *jrdev = ctx->dev; |
| 710 | struct rsa_edesc *edesc; |
| 711 | int ret; |
| 712 | |
| 713 | if (unlikely(!key->n || !key->e)) |
| 714 | return -EINVAL; |
| 715 | |
| 716 | if (req->dst_len < key->n_sz) { |
| 717 | req->dst_len = key->n_sz; |
| 718 | dev_err(jrdev, "Output buffer length less than parameter n\n"); |
| 719 | return -EOVERFLOW; |
| 720 | } |
| 721 | |
| 722 | /* Allocate extended descriptor */ |
| 723 | edesc = rsa_edesc_alloc(req, DESC_RSA_PUB_LEN); |
| 724 | if (IS_ERR(edesc)) |
| 725 | return PTR_ERR(edesc); |
| 726 | |
| 727 | /* Set RSA Encrypt Protocol Data Block */ |
| 728 | ret = set_rsa_pub_pdb(req, edesc); |
| 729 | if (ret) |
| 730 | goto init_fail; |
| 731 | |
| 732 | /* Initialize Job Descriptor */ |
| 733 | init_rsa_pub_desc(edesc->hw_desc, &edesc->pdb.pub); |
| 734 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 735 | return akcipher_enqueue_req(jrdev, rsa_pub_done, req); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 736 | |
| 737 | init_fail: |
| 738 | rsa_io_unmap(jrdev, edesc, req); |
| 739 | kfree(edesc); |
| 740 | return ret; |
| 741 | } |
| 742 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 743 | static int caam_rsa_dec_priv_f1(struct akcipher_request *req) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 744 | { |
| 745 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 746 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 747 | struct device *jrdev = ctx->dev; |
| 748 | struct rsa_edesc *edesc; |
| 749 | int ret; |
| 750 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 751 | /* Allocate extended descriptor */ |
| 752 | edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F1_LEN); |
| 753 | if (IS_ERR(edesc)) |
| 754 | return PTR_ERR(edesc); |
| 755 | |
| 756 | /* Set RSA Decrypt Protocol Data Block - Private Key Form #1 */ |
| 757 | ret = set_rsa_priv_f1_pdb(req, edesc); |
| 758 | if (ret) |
| 759 | goto init_fail; |
| 760 | |
| 761 | /* Initialize Job Descriptor */ |
| 762 | init_rsa_priv_f1_desc(edesc->hw_desc, &edesc->pdb.priv_f1); |
| 763 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 764 | return akcipher_enqueue_req(jrdev, rsa_priv_f_done, req); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 765 | |
| 766 | init_fail: |
| 767 | rsa_io_unmap(jrdev, edesc, req); |
| 768 | kfree(edesc); |
| 769 | return ret; |
| 770 | } |
| 771 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 772 | static int caam_rsa_dec_priv_f2(struct akcipher_request *req) |
| 773 | { |
| 774 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 775 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 776 | struct device *jrdev = ctx->dev; |
| 777 | struct rsa_edesc *edesc; |
| 778 | int ret; |
| 779 | |
| 780 | /* Allocate extended descriptor */ |
| 781 | edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F2_LEN); |
| 782 | if (IS_ERR(edesc)) |
| 783 | return PTR_ERR(edesc); |
| 784 | |
| 785 | /* Set RSA Decrypt Protocol Data Block - Private Key Form #2 */ |
| 786 | ret = set_rsa_priv_f2_pdb(req, edesc); |
| 787 | if (ret) |
| 788 | goto init_fail; |
| 789 | |
| 790 | /* Initialize Job Descriptor */ |
| 791 | init_rsa_priv_f2_desc(edesc->hw_desc, &edesc->pdb.priv_f2); |
| 792 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 793 | return akcipher_enqueue_req(jrdev, rsa_priv_f_done, req); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 794 | |
| 795 | init_fail: |
| 796 | rsa_io_unmap(jrdev, edesc, req); |
| 797 | kfree(edesc); |
| 798 | return ret; |
| 799 | } |
| 800 | |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 801 | static int caam_rsa_dec_priv_f3(struct akcipher_request *req) |
| 802 | { |
| 803 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 804 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 805 | struct device *jrdev = ctx->dev; |
| 806 | struct rsa_edesc *edesc; |
| 807 | int ret; |
| 808 | |
| 809 | /* Allocate extended descriptor */ |
| 810 | edesc = rsa_edesc_alloc(req, DESC_RSA_PRIV_F3_LEN); |
| 811 | if (IS_ERR(edesc)) |
| 812 | return PTR_ERR(edesc); |
| 813 | |
| 814 | /* Set RSA Decrypt Protocol Data Block - Private Key Form #3 */ |
| 815 | ret = set_rsa_priv_f3_pdb(req, edesc); |
| 816 | if (ret) |
| 817 | goto init_fail; |
| 818 | |
| 819 | /* Initialize Job Descriptor */ |
| 820 | init_rsa_priv_f3_desc(edesc->hw_desc, &edesc->pdb.priv_f3); |
| 821 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 822 | return akcipher_enqueue_req(jrdev, rsa_priv_f_done, req); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 823 | |
| 824 | init_fail: |
| 825 | rsa_io_unmap(jrdev, edesc, req); |
| 826 | kfree(edesc); |
| 827 | return ret; |
| 828 | } |
| 829 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 830 | static int caam_rsa_dec(struct akcipher_request *req) |
| 831 | { |
| 832 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
| 833 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 834 | struct caam_rsa_key *key = &ctx->key; |
| 835 | int ret; |
| 836 | |
| 837 | if (unlikely(!key->n || !key->d)) |
| 838 | return -EINVAL; |
| 839 | |
| 840 | if (req->dst_len < key->n_sz) { |
| 841 | req->dst_len = key->n_sz; |
| 842 | dev_err(ctx->dev, "Output buffer length less than parameter n\n"); |
| 843 | return -EOVERFLOW; |
| 844 | } |
| 845 | |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 846 | if (key->priv_form == FORM3) |
| 847 | ret = caam_rsa_dec_priv_f3(req); |
| 848 | else if (key->priv_form == FORM2) |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 849 | ret = caam_rsa_dec_priv_f2(req); |
| 850 | else |
| 851 | ret = caam_rsa_dec_priv_f1(req); |
| 852 | |
| 853 | return ret; |
| 854 | } |
| 855 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 856 | static void caam_rsa_free_key(struct caam_rsa_key *key) |
| 857 | { |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 858 | kfree_sensitive(key->d); |
| 859 | kfree_sensitive(key->p); |
| 860 | kfree_sensitive(key->q); |
| 861 | kfree_sensitive(key->dp); |
| 862 | kfree_sensitive(key->dq); |
| 863 | kfree_sensitive(key->qinv); |
| 864 | kfree_sensitive(key->tmp1); |
| 865 | kfree_sensitive(key->tmp2); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 866 | kfree(key->e); |
| 867 | kfree(key->n); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 868 | memset(key, 0, sizeof(*key)); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 869 | } |
| 870 | |
Radu Alexe | 7ca4a9a | 2017-04-25 16:26:37 +0300 | [diff] [blame] | 871 | static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes) |
| 872 | { |
| 873 | while (!**ptr && *nbytes) { |
| 874 | (*ptr)++; |
| 875 | (*nbytes)--; |
| 876 | } |
| 877 | } |
| 878 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 879 | /** |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 880 | * caam_read_rsa_crt - Used for reading dP, dQ, qInv CRT members. |
| 881 | * dP, dQ and qInv could decode to less than corresponding p, q length, as the |
| 882 | * BER-encoding requires that the minimum number of bytes be used to encode the |
| 883 | * integer. dP, dQ, qInv decoded values have to be zero-padded to appropriate |
| 884 | * length. |
| 885 | * |
| 886 | * @ptr : pointer to {dP, dQ, qInv} CRT member |
| 887 | * @nbytes: length in bytes of {dP, dQ, qInv} CRT member |
| 888 | * @dstlen: length in bytes of corresponding p or q prime factor |
| 889 | */ |
| 890 | static u8 *caam_read_rsa_crt(const u8 *ptr, size_t nbytes, size_t dstlen) |
| 891 | { |
| 892 | u8 *dst; |
| 893 | |
| 894 | caam_rsa_drop_leading_zeros(&ptr, &nbytes); |
| 895 | if (!nbytes) |
| 896 | return NULL; |
| 897 | |
| 898 | dst = kzalloc(dstlen, GFP_DMA | GFP_KERNEL); |
| 899 | if (!dst) |
| 900 | return NULL; |
| 901 | |
| 902 | memcpy(dst + (dstlen - nbytes), ptr, nbytes); |
| 903 | |
| 904 | return dst; |
| 905 | } |
| 906 | |
| 907 | /** |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 908 | * caam_read_raw_data - Read a raw byte stream as a positive integer. |
| 909 | * The function skips buffer's leading zeros, copies the remained data |
| 910 | * to a buffer allocated in the GFP_DMA | GFP_KERNEL zone and returns |
| 911 | * the address of the new buffer. |
| 912 | * |
| 913 | * @buf : The data to read |
| 914 | * @nbytes: The amount of data to read |
| 915 | */ |
| 916 | static inline u8 *caam_read_raw_data(const u8 *buf, size_t *nbytes) |
| 917 | { |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 918 | |
Radu Alexe | 7ca4a9a | 2017-04-25 16:26:37 +0300 | [diff] [blame] | 919 | caam_rsa_drop_leading_zeros(&buf, nbytes); |
Tudor Ambarus | 7fcaf62 | 2017-04-25 16:26:36 +0300 | [diff] [blame] | 920 | if (!*nbytes) |
| 921 | return NULL; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 922 | |
Fabio Estevam | b930f3a | 2018-04-16 13:05:01 -0300 | [diff] [blame] | 923 | return kmemdup(buf, *nbytes, GFP_DMA | GFP_KERNEL); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static int caam_rsa_check_key_length(unsigned int len) |
| 927 | { |
| 928 | if (len > 4096) |
| 929 | return -EINVAL; |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static int caam_rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, |
| 934 | unsigned int keylen) |
| 935 | { |
| 936 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
Horia Geantă | 8439e94 | 2016-11-09 10:46:14 +0200 | [diff] [blame] | 937 | struct rsa_key raw_key = {NULL}; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 938 | struct caam_rsa_key *rsa_key = &ctx->key; |
| 939 | int ret; |
| 940 | |
| 941 | /* Free the old RSA key if any */ |
| 942 | caam_rsa_free_key(rsa_key); |
| 943 | |
| 944 | ret = rsa_parse_pub_key(&raw_key, key, keylen); |
| 945 | if (ret) |
| 946 | return ret; |
| 947 | |
| 948 | /* Copy key in DMA zone */ |
Fuqian Huang | cc2a58f | 2019-07-04 00:27:08 +0800 | [diff] [blame] | 949 | rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 950 | if (!rsa_key->e) |
| 951 | goto err; |
| 952 | |
| 953 | /* |
| 954 | * Skip leading zeros and copy the positive integer to a buffer |
| 955 | * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor |
| 956 | * expects a positive integer for the RSA modulus and uses its length as |
| 957 | * decryption output length. |
| 958 | */ |
| 959 | rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz); |
| 960 | if (!rsa_key->n) |
| 961 | goto err; |
| 962 | |
| 963 | if (caam_rsa_check_key_length(raw_key.n_sz << 3)) { |
| 964 | caam_rsa_free_key(rsa_key); |
| 965 | return -EINVAL; |
| 966 | } |
| 967 | |
| 968 | rsa_key->e_sz = raw_key.e_sz; |
| 969 | rsa_key->n_sz = raw_key.n_sz; |
| 970 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 971 | return 0; |
| 972 | err: |
| 973 | caam_rsa_free_key(rsa_key); |
| 974 | return -ENOMEM; |
| 975 | } |
| 976 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 977 | static void caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx, |
| 978 | struct rsa_key *raw_key) |
| 979 | { |
| 980 | struct caam_rsa_key *rsa_key = &ctx->key; |
| 981 | size_t p_sz = raw_key->p_sz; |
| 982 | size_t q_sz = raw_key->q_sz; |
| 983 | |
| 984 | rsa_key->p = caam_read_raw_data(raw_key->p, &p_sz); |
| 985 | if (!rsa_key->p) |
| 986 | return; |
| 987 | rsa_key->p_sz = p_sz; |
| 988 | |
| 989 | rsa_key->q = caam_read_raw_data(raw_key->q, &q_sz); |
| 990 | if (!rsa_key->q) |
| 991 | goto free_p; |
| 992 | rsa_key->q_sz = q_sz; |
| 993 | |
| 994 | rsa_key->tmp1 = kzalloc(raw_key->p_sz, GFP_DMA | GFP_KERNEL); |
| 995 | if (!rsa_key->tmp1) |
| 996 | goto free_q; |
| 997 | |
| 998 | rsa_key->tmp2 = kzalloc(raw_key->q_sz, GFP_DMA | GFP_KERNEL); |
| 999 | if (!rsa_key->tmp2) |
| 1000 | goto free_tmp1; |
| 1001 | |
| 1002 | rsa_key->priv_form = FORM2; |
| 1003 | |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 1004 | rsa_key->dp = caam_read_rsa_crt(raw_key->dp, raw_key->dp_sz, p_sz); |
| 1005 | if (!rsa_key->dp) |
| 1006 | goto free_tmp2; |
| 1007 | |
| 1008 | rsa_key->dq = caam_read_rsa_crt(raw_key->dq, raw_key->dq_sz, q_sz); |
| 1009 | if (!rsa_key->dq) |
| 1010 | goto free_dp; |
| 1011 | |
| 1012 | rsa_key->qinv = caam_read_rsa_crt(raw_key->qinv, raw_key->qinv_sz, |
| 1013 | q_sz); |
| 1014 | if (!rsa_key->qinv) |
| 1015 | goto free_dq; |
| 1016 | |
| 1017 | rsa_key->priv_form = FORM3; |
| 1018 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 1019 | return; |
| 1020 | |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 1021 | free_dq: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1022 | kfree_sensitive(rsa_key->dq); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 1023 | free_dp: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1024 | kfree_sensitive(rsa_key->dp); |
Radu Alexe | 4a651b1 | 2017-04-25 16:26:39 +0300 | [diff] [blame] | 1025 | free_tmp2: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1026 | kfree_sensitive(rsa_key->tmp2); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 1027 | free_tmp1: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1028 | kfree_sensitive(rsa_key->tmp1); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 1029 | free_q: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1030 | kfree_sensitive(rsa_key->q); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 1031 | free_p: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 1032 | kfree_sensitive(rsa_key->p); |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 1033 | } |
| 1034 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1035 | static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, |
| 1036 | unsigned int keylen) |
| 1037 | { |
| 1038 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
Horia Geantă | 8439e94 | 2016-11-09 10:46:14 +0200 | [diff] [blame] | 1039 | struct rsa_key raw_key = {NULL}; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1040 | struct caam_rsa_key *rsa_key = &ctx->key; |
| 1041 | int ret; |
| 1042 | |
| 1043 | /* Free the old RSA key if any */ |
| 1044 | caam_rsa_free_key(rsa_key); |
| 1045 | |
| 1046 | ret = rsa_parse_priv_key(&raw_key, key, keylen); |
| 1047 | if (ret) |
| 1048 | return ret; |
| 1049 | |
| 1050 | /* Copy key in DMA zone */ |
Fuqian Huang | cc2a58f | 2019-07-04 00:27:08 +0800 | [diff] [blame] | 1051 | rsa_key->d = kmemdup(raw_key.d, raw_key.d_sz, GFP_DMA | GFP_KERNEL); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1052 | if (!rsa_key->d) |
| 1053 | goto err; |
| 1054 | |
Fuqian Huang | cc2a58f | 2019-07-04 00:27:08 +0800 | [diff] [blame] | 1055 | rsa_key->e = kmemdup(raw_key.e, raw_key.e_sz, GFP_DMA | GFP_KERNEL); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1056 | if (!rsa_key->e) |
| 1057 | goto err; |
| 1058 | |
| 1059 | /* |
| 1060 | * Skip leading zeros and copy the positive integer to a buffer |
| 1061 | * allocated in the GFP_DMA | GFP_KERNEL zone. The decryption descriptor |
| 1062 | * expects a positive integer for the RSA modulus and uses its length as |
| 1063 | * decryption output length. |
| 1064 | */ |
| 1065 | rsa_key->n = caam_read_raw_data(raw_key.n, &raw_key.n_sz); |
| 1066 | if (!rsa_key->n) |
| 1067 | goto err; |
| 1068 | |
| 1069 | if (caam_rsa_check_key_length(raw_key.n_sz << 3)) { |
| 1070 | caam_rsa_free_key(rsa_key); |
| 1071 | return -EINVAL; |
| 1072 | } |
| 1073 | |
| 1074 | rsa_key->d_sz = raw_key.d_sz; |
| 1075 | rsa_key->e_sz = raw_key.e_sz; |
| 1076 | rsa_key->n_sz = raw_key.n_sz; |
| 1077 | |
Radu Alexe | 52e26d7 | 2017-04-25 16:26:38 +0300 | [diff] [blame] | 1078 | caam_rsa_set_priv_key_form(ctx, &raw_key); |
| 1079 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1080 | return 0; |
| 1081 | |
| 1082 | err: |
| 1083 | caam_rsa_free_key(rsa_key); |
| 1084 | return -ENOMEM; |
| 1085 | } |
| 1086 | |
Tudor-Dan Ambarus | e198429 | 2017-05-25 10:18:14 +0300 | [diff] [blame] | 1087 | static unsigned int caam_rsa_max_size(struct crypto_akcipher *tfm) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1088 | { |
| 1089 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1090 | |
Tudor-Dan Ambarus | e198429 | 2017-05-25 10:18:14 +0300 | [diff] [blame] | 1091 | return ctx->key.n_sz; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* Per session pkc's driver context creation function */ |
| 1095 | static int caam_rsa_init_tfm(struct crypto_akcipher *tfm) |
| 1096 | { |
| 1097 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 1098 | |
| 1099 | ctx->dev = caam_jr_alloc(); |
| 1100 | |
| 1101 | if (IS_ERR(ctx->dev)) { |
Horia Geantă | 33fa46d | 2017-04-03 18:30:07 +0300 | [diff] [blame] | 1102 | pr_err("Job Ring Device allocation for transform failed\n"); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1103 | return PTR_ERR(ctx->dev); |
| 1104 | } |
| 1105 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 1106 | ctx->padding_dma = dma_map_single(ctx->dev, zero_buffer, |
| 1107 | CAAM_RSA_MAX_INPUT_SIZE - 1, |
| 1108 | DMA_TO_DEVICE); |
| 1109 | if (dma_mapping_error(ctx->dev, ctx->padding_dma)) { |
| 1110 | dev_err(ctx->dev, "unable to map padding\n"); |
| 1111 | caam_jr_free(ctx->dev); |
| 1112 | return -ENOMEM; |
| 1113 | } |
| 1114 | |
Iuliana Prodan | bf53795 | 2020-02-12 19:55:23 +0200 | [diff] [blame] | 1115 | ctx->enginectx.op.do_one_request = akcipher_do_one_req; |
| 1116 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | /* Per session pkc's driver context cleanup function */ |
| 1121 | static void caam_rsa_exit_tfm(struct crypto_akcipher *tfm) |
| 1122 | { |
| 1123 | struct caam_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); |
| 1124 | struct caam_rsa_key *key = &ctx->key; |
| 1125 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 1126 | dma_unmap_single(ctx->dev, ctx->padding_dma, CAAM_RSA_MAX_INPUT_SIZE - |
| 1127 | 1, DMA_TO_DEVICE); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1128 | caam_rsa_free_key(key); |
| 1129 | caam_jr_free(ctx->dev); |
| 1130 | } |
| 1131 | |
Iuliana Prodan | 58068cf | 2019-07-31 16:08:14 +0300 | [diff] [blame] | 1132 | static struct caam_akcipher_alg caam_rsa = { |
| 1133 | .akcipher = { |
| 1134 | .encrypt = caam_rsa_enc, |
| 1135 | .decrypt = caam_rsa_dec, |
| 1136 | .set_pub_key = caam_rsa_set_pub_key, |
| 1137 | .set_priv_key = caam_rsa_set_priv_key, |
| 1138 | .max_size = caam_rsa_max_size, |
| 1139 | .init = caam_rsa_init_tfm, |
| 1140 | .exit = caam_rsa_exit_tfm, |
| 1141 | .reqsize = sizeof(struct caam_rsa_req_ctx), |
| 1142 | .base = { |
| 1143 | .cra_name = "rsa", |
| 1144 | .cra_driver_name = "rsa-caam", |
| 1145 | .cra_priority = 3000, |
| 1146 | .cra_module = THIS_MODULE, |
| 1147 | .cra_ctxsize = sizeof(struct caam_rsa_ctx), |
| 1148 | }, |
| 1149 | } |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1150 | }; |
| 1151 | |
| 1152 | /* Public Key Cryptography module initialization handler */ |
Horia Geantă | 1b46c90 | 2019-05-03 17:17:39 +0300 | [diff] [blame] | 1153 | int caam_pkc_init(struct device *ctrldev) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1154 | { |
Horia Geantă | 1b46c90 | 2019-05-03 17:17:39 +0300 | [diff] [blame] | 1155 | struct caam_drv_private *priv = dev_get_drvdata(ctrldev); |
Horia Geantă | d239b10 | 2018-11-08 15:36:27 +0200 | [diff] [blame] | 1156 | u32 pk_inst; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1157 | int err; |
Iuliana Prodan | 4e3a61c | 2019-07-31 16:08:13 +0300 | [diff] [blame] | 1158 | init_done = false; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1159 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1160 | /* Determine public key hardware accelerator presence. */ |
Horia Geantă | d239b10 | 2018-11-08 15:36:27 +0200 | [diff] [blame] | 1161 | if (priv->era < 10) |
| 1162 | pk_inst = (rd_reg32(&priv->ctrl->perfmon.cha_num_ls) & |
| 1163 | CHA_ID_LS_PK_MASK) >> CHA_ID_LS_PK_SHIFT; |
| 1164 | else |
| 1165 | pk_inst = rd_reg32(&priv->ctrl->vreg.pkha) & CHA_VER_NUM_MASK; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1166 | |
| 1167 | /* Do not register algorithms if PKHA is not present. */ |
Horia Geantă | 1b46c90 | 2019-05-03 17:17:39 +0300 | [diff] [blame] | 1168 | if (!pk_inst) |
| 1169 | return 0; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1170 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 1171 | /* allocate zero buffer, used for padding input */ |
| 1172 | zero_buffer = kzalloc(CAAM_RSA_MAX_INPUT_SIZE - 1, GFP_DMA | |
| 1173 | GFP_KERNEL); |
| 1174 | if (!zero_buffer) |
| 1175 | return -ENOMEM; |
| 1176 | |
Iuliana Prodan | 58068cf | 2019-07-31 16:08:14 +0300 | [diff] [blame] | 1177 | err = crypto_register_akcipher(&caam_rsa.akcipher); |
| 1178 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 1179 | if (err) { |
| 1180 | kfree(zero_buffer); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1181 | dev_warn(ctrldev, "%s alg registration failed\n", |
Iuliana Prodan | 58068cf | 2019-07-31 16:08:14 +0300 | [diff] [blame] | 1182 | caam_rsa.akcipher.base.cra_driver_name); |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 1183 | } else { |
Iuliana Prodan | 4e3a61c | 2019-07-31 16:08:13 +0300 | [diff] [blame] | 1184 | init_done = true; |
Iuliana Prodan | 58068cf | 2019-07-31 16:08:14 +0300 | [diff] [blame] | 1185 | caam_rsa.registered = true; |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1186 | dev_info(ctrldev, "caam pkc algorithms registered in /proc/crypto\n"); |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 1187 | } |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1188 | |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1189 | return err; |
| 1190 | } |
| 1191 | |
Horia Geantă | 1b46c90 | 2019-05-03 17:17:39 +0300 | [diff] [blame] | 1192 | void caam_pkc_exit(void) |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1193 | { |
Iuliana Prodan | 4e3a61c | 2019-07-31 16:08:13 +0300 | [diff] [blame] | 1194 | if (!init_done) |
| 1195 | return; |
| 1196 | |
Iuliana Prodan | 58068cf | 2019-07-31 16:08:14 +0300 | [diff] [blame] | 1197 | if (caam_rsa.registered) |
| 1198 | crypto_unregister_akcipher(&caam_rsa.akcipher); |
| 1199 | |
Iuliana Prodan | c3725f7 | 2019-05-28 12:52:10 +0300 | [diff] [blame] | 1200 | kfree(zero_buffer); |
Tudor Ambarus | 8c41977 | 2016-07-04 13:12:08 +0300 | [diff] [blame] | 1201 | } |