| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved. |
|---|
| 3 | | - * |
|---|
| 4 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | | - * it under the terms of the GNU General Public License version 2 and |
|---|
| 6 | | - * only version 2 as published by the Free Software Foundation. |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 9 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | | - * GNU General Public License for more details. |
|---|
| 12 | 4 | */ |
|---|
| 13 | 5 | |
|---|
| 14 | 6 | #include <linux/err.h> |
|---|
| .. | .. |
|---|
| 22 | 14 | #include "core.h" |
|---|
| 23 | 15 | #include "regs-v5.h" |
|---|
| 24 | 16 | #include "sha.h" |
|---|
| 25 | | - |
|---|
| 26 | | -#define QCE_SECTOR_SIZE 512 |
|---|
| 27 | 17 | |
|---|
| 28 | 18 | static inline u32 qce_read(struct qce_device *qce, u32 offset) |
|---|
| 29 | 19 | { |
|---|
| .. | .. |
|---|
| 53 | 43 | qce_write(qce, offset + i * sizeof(u32), 0); |
|---|
| 54 | 44 | } |
|---|
| 55 | 45 | |
|---|
| 56 | | -static u32 qce_encr_cfg(unsigned long flags, u32 aes_key_size) |
|---|
| 46 | +static u32 qce_config_reg(struct qce_device *qce, int little) |
|---|
| 57 | 47 | { |
|---|
| 58 | | - u32 cfg = 0; |
|---|
| 48 | + u32 beats = (qce->burst_size >> 3) - 1; |
|---|
| 49 | + u32 pipe_pair = qce->pipe_pair_id; |
|---|
| 50 | + u32 config; |
|---|
| 59 | 51 | |
|---|
| 60 | | - if (IS_AES(flags)) { |
|---|
| 61 | | - if (aes_key_size == AES_KEYSIZE_128) |
|---|
| 62 | | - cfg |= ENCR_KEY_SZ_AES128 << ENCR_KEY_SZ_SHIFT; |
|---|
| 63 | | - else if (aes_key_size == AES_KEYSIZE_256) |
|---|
| 64 | | - cfg |= ENCR_KEY_SZ_AES256 << ENCR_KEY_SZ_SHIFT; |
|---|
| 65 | | - } |
|---|
| 52 | + config = (beats << REQ_SIZE_SHIFT) & REQ_SIZE_MASK; |
|---|
| 53 | + config |= BIT(MASK_DOUT_INTR_SHIFT) | BIT(MASK_DIN_INTR_SHIFT) | |
|---|
| 54 | + BIT(MASK_OP_DONE_INTR_SHIFT) | BIT(MASK_ERR_INTR_SHIFT); |
|---|
| 55 | + config |= (pipe_pair << PIPE_SET_SELECT_SHIFT) & PIPE_SET_SELECT_MASK; |
|---|
| 56 | + config &= ~HIGH_SPD_EN_N_SHIFT; |
|---|
| 66 | 57 | |
|---|
| 67 | | - if (IS_AES(flags)) |
|---|
| 68 | | - cfg |= ENCR_ALG_AES << ENCR_ALG_SHIFT; |
|---|
| 69 | | - else if (IS_DES(flags) || IS_3DES(flags)) |
|---|
| 70 | | - cfg |= ENCR_ALG_DES << ENCR_ALG_SHIFT; |
|---|
| 58 | + if (little) |
|---|
| 59 | + config |= BIT(LITTLE_ENDIAN_MODE_SHIFT); |
|---|
| 71 | 60 | |
|---|
| 72 | | - if (IS_DES(flags)) |
|---|
| 73 | | - cfg |= ENCR_KEY_SZ_DES << ENCR_KEY_SZ_SHIFT; |
|---|
| 74 | | - |
|---|
| 75 | | - if (IS_3DES(flags)) |
|---|
| 76 | | - cfg |= ENCR_KEY_SZ_3DES << ENCR_KEY_SZ_SHIFT; |
|---|
| 77 | | - |
|---|
| 78 | | - switch (flags & QCE_MODE_MASK) { |
|---|
| 79 | | - case QCE_MODE_ECB: |
|---|
| 80 | | - cfg |= ENCR_MODE_ECB << ENCR_MODE_SHIFT; |
|---|
| 81 | | - break; |
|---|
| 82 | | - case QCE_MODE_CBC: |
|---|
| 83 | | - cfg |= ENCR_MODE_CBC << ENCR_MODE_SHIFT; |
|---|
| 84 | | - break; |
|---|
| 85 | | - case QCE_MODE_CTR: |
|---|
| 86 | | - cfg |= ENCR_MODE_CTR << ENCR_MODE_SHIFT; |
|---|
| 87 | | - break; |
|---|
| 88 | | - case QCE_MODE_XTS: |
|---|
| 89 | | - cfg |= ENCR_MODE_XTS << ENCR_MODE_SHIFT; |
|---|
| 90 | | - break; |
|---|
| 91 | | - case QCE_MODE_CCM: |
|---|
| 92 | | - cfg |= ENCR_MODE_CCM << ENCR_MODE_SHIFT; |
|---|
| 93 | | - cfg |= LAST_CCM_XFR << LAST_CCM_SHIFT; |
|---|
| 94 | | - break; |
|---|
| 95 | | - default: |
|---|
| 96 | | - return ~0; |
|---|
| 97 | | - } |
|---|
| 98 | | - |
|---|
| 99 | | - return cfg; |
|---|
| 61 | + return config; |
|---|
| 100 | 62 | } |
|---|
| 101 | 63 | |
|---|
| 64 | +void qce_cpu_to_be32p_array(__be32 *dst, const u8 *src, unsigned int len) |
|---|
| 65 | +{ |
|---|
| 66 | + __be32 *d = dst; |
|---|
| 67 | + const u8 *s = src; |
|---|
| 68 | + unsigned int n; |
|---|
| 69 | + |
|---|
| 70 | + n = len / sizeof(u32); |
|---|
| 71 | + for (; n > 0; n--) { |
|---|
| 72 | + *d = cpu_to_be32p((const __u32 *) s); |
|---|
| 73 | + s += sizeof(__u32); |
|---|
| 74 | + d++; |
|---|
| 75 | + } |
|---|
| 76 | +} |
|---|
| 77 | + |
|---|
| 78 | +static void qce_setup_config(struct qce_device *qce) |
|---|
| 79 | +{ |
|---|
| 80 | + u32 config; |
|---|
| 81 | + |
|---|
| 82 | + /* get big endianness */ |
|---|
| 83 | + config = qce_config_reg(qce, 0); |
|---|
| 84 | + |
|---|
| 85 | + /* clear status */ |
|---|
| 86 | + qce_write(qce, REG_STATUS, 0); |
|---|
| 87 | + qce_write(qce, REG_CONFIG, config); |
|---|
| 88 | +} |
|---|
| 89 | + |
|---|
| 90 | +static inline void qce_crypto_go(struct qce_device *qce) |
|---|
| 91 | +{ |
|---|
| 92 | + qce_write(qce, REG_GOPROC, BIT(GO_SHIFT) | BIT(RESULTS_DUMP_SHIFT)); |
|---|
| 93 | +} |
|---|
| 94 | + |
|---|
| 95 | +#ifdef CONFIG_CRYPTO_DEV_QCE_SHA |
|---|
| 102 | 96 | static u32 qce_auth_cfg(unsigned long flags, u32 key_size) |
|---|
| 103 | 97 | { |
|---|
| 104 | 98 | u32 cfg = 0; |
|---|
| .. | .. |
|---|
| 143 | 137 | cfg |= BIT(AUTH_LAST_SHIFT) | BIT(AUTH_FIRST_SHIFT); |
|---|
| 144 | 138 | |
|---|
| 145 | 139 | return cfg; |
|---|
| 146 | | -} |
|---|
| 147 | | - |
|---|
| 148 | | -static u32 qce_config_reg(struct qce_device *qce, int little) |
|---|
| 149 | | -{ |
|---|
| 150 | | - u32 beats = (qce->burst_size >> 3) - 1; |
|---|
| 151 | | - u32 pipe_pair = qce->pipe_pair_id; |
|---|
| 152 | | - u32 config; |
|---|
| 153 | | - |
|---|
| 154 | | - config = (beats << REQ_SIZE_SHIFT) & REQ_SIZE_MASK; |
|---|
| 155 | | - config |= BIT(MASK_DOUT_INTR_SHIFT) | BIT(MASK_DIN_INTR_SHIFT) | |
|---|
| 156 | | - BIT(MASK_OP_DONE_INTR_SHIFT) | BIT(MASK_ERR_INTR_SHIFT); |
|---|
| 157 | | - config |= (pipe_pair << PIPE_SET_SELECT_SHIFT) & PIPE_SET_SELECT_MASK; |
|---|
| 158 | | - config &= ~HIGH_SPD_EN_N_SHIFT; |
|---|
| 159 | | - |
|---|
| 160 | | - if (little) |
|---|
| 161 | | - config |= BIT(LITTLE_ENDIAN_MODE_SHIFT); |
|---|
| 162 | | - |
|---|
| 163 | | - return config; |
|---|
| 164 | | -} |
|---|
| 165 | | - |
|---|
| 166 | | -void qce_cpu_to_be32p_array(__be32 *dst, const u8 *src, unsigned int len) |
|---|
| 167 | | -{ |
|---|
| 168 | | - __be32 *d = dst; |
|---|
| 169 | | - const u8 *s = src; |
|---|
| 170 | | - unsigned int n; |
|---|
| 171 | | - |
|---|
| 172 | | - n = len / sizeof(u32); |
|---|
| 173 | | - for (; n > 0; n--) { |
|---|
| 174 | | - *d = cpu_to_be32p((const __u32 *) s); |
|---|
| 175 | | - s += sizeof(__u32); |
|---|
| 176 | | - d++; |
|---|
| 177 | | - } |
|---|
| 178 | | -} |
|---|
| 179 | | - |
|---|
| 180 | | -static void qce_xts_swapiv(__be32 *dst, const u8 *src, unsigned int ivsize) |
|---|
| 181 | | -{ |
|---|
| 182 | | - u8 swap[QCE_AES_IV_LENGTH]; |
|---|
| 183 | | - u32 i, j; |
|---|
| 184 | | - |
|---|
| 185 | | - if (ivsize > QCE_AES_IV_LENGTH) |
|---|
| 186 | | - return; |
|---|
| 187 | | - |
|---|
| 188 | | - memset(swap, 0, QCE_AES_IV_LENGTH); |
|---|
| 189 | | - |
|---|
| 190 | | - for (i = (QCE_AES_IV_LENGTH - ivsize), j = ivsize - 1; |
|---|
| 191 | | - i < QCE_AES_IV_LENGTH; i++, j--) |
|---|
| 192 | | - swap[i] = src[j]; |
|---|
| 193 | | - |
|---|
| 194 | | - qce_cpu_to_be32p_array(dst, swap, QCE_AES_IV_LENGTH); |
|---|
| 195 | | -} |
|---|
| 196 | | - |
|---|
| 197 | | -static void qce_xtskey(struct qce_device *qce, const u8 *enckey, |
|---|
| 198 | | - unsigned int enckeylen, unsigned int cryptlen) |
|---|
| 199 | | -{ |
|---|
| 200 | | - u32 xtskey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0}; |
|---|
| 201 | | - unsigned int xtsklen = enckeylen / (2 * sizeof(u32)); |
|---|
| 202 | | - unsigned int xtsdusize; |
|---|
| 203 | | - |
|---|
| 204 | | - qce_cpu_to_be32p_array((__be32 *)xtskey, enckey + enckeylen / 2, |
|---|
| 205 | | - enckeylen / 2); |
|---|
| 206 | | - qce_write_array(qce, REG_ENCR_XTS_KEY0, xtskey, xtsklen); |
|---|
| 207 | | - |
|---|
| 208 | | - /* xts du size 512B */ |
|---|
| 209 | | - xtsdusize = min_t(u32, QCE_SECTOR_SIZE, cryptlen); |
|---|
| 210 | | - qce_write(qce, REG_ENCR_XTS_DU_SIZE, xtsdusize); |
|---|
| 211 | | -} |
|---|
| 212 | | - |
|---|
| 213 | | -static void qce_setup_config(struct qce_device *qce) |
|---|
| 214 | | -{ |
|---|
| 215 | | - u32 config; |
|---|
| 216 | | - |
|---|
| 217 | | - /* get big endianness */ |
|---|
| 218 | | - config = qce_config_reg(qce, 0); |
|---|
| 219 | | - |
|---|
| 220 | | - /* clear status */ |
|---|
| 221 | | - qce_write(qce, REG_STATUS, 0); |
|---|
| 222 | | - qce_write(qce, REG_CONFIG, config); |
|---|
| 223 | | -} |
|---|
| 224 | | - |
|---|
| 225 | | -static inline void qce_crypto_go(struct qce_device *qce) |
|---|
| 226 | | -{ |
|---|
| 227 | | - qce_write(qce, REG_GOPROC, BIT(GO_SHIFT) | BIT(RESULTS_DUMP_SHIFT)); |
|---|
| 228 | 140 | } |
|---|
| 229 | 141 | |
|---|
| 230 | 142 | static int qce_setup_regs_ahash(struct crypto_async_request *async_req, |
|---|
| .. | .. |
|---|
| 311 | 223 | |
|---|
| 312 | 224 | return 0; |
|---|
| 313 | 225 | } |
|---|
| 226 | +#endif |
|---|
| 314 | 227 | |
|---|
| 315 | | -static int qce_setup_regs_ablkcipher(struct crypto_async_request *async_req, |
|---|
| 228 | +#ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER |
|---|
| 229 | +static u32 qce_encr_cfg(unsigned long flags, u32 aes_key_size) |
|---|
| 230 | +{ |
|---|
| 231 | + u32 cfg = 0; |
|---|
| 232 | + |
|---|
| 233 | + if (IS_AES(flags)) { |
|---|
| 234 | + if (aes_key_size == AES_KEYSIZE_128) |
|---|
| 235 | + cfg |= ENCR_KEY_SZ_AES128 << ENCR_KEY_SZ_SHIFT; |
|---|
| 236 | + else if (aes_key_size == AES_KEYSIZE_256) |
|---|
| 237 | + cfg |= ENCR_KEY_SZ_AES256 << ENCR_KEY_SZ_SHIFT; |
|---|
| 238 | + } |
|---|
| 239 | + |
|---|
| 240 | + if (IS_AES(flags)) |
|---|
| 241 | + cfg |= ENCR_ALG_AES << ENCR_ALG_SHIFT; |
|---|
| 242 | + else if (IS_DES(flags) || IS_3DES(flags)) |
|---|
| 243 | + cfg |= ENCR_ALG_DES << ENCR_ALG_SHIFT; |
|---|
| 244 | + |
|---|
| 245 | + if (IS_DES(flags)) |
|---|
| 246 | + cfg |= ENCR_KEY_SZ_DES << ENCR_KEY_SZ_SHIFT; |
|---|
| 247 | + |
|---|
| 248 | + if (IS_3DES(flags)) |
|---|
| 249 | + cfg |= ENCR_KEY_SZ_3DES << ENCR_KEY_SZ_SHIFT; |
|---|
| 250 | + |
|---|
| 251 | + switch (flags & QCE_MODE_MASK) { |
|---|
| 252 | + case QCE_MODE_ECB: |
|---|
| 253 | + cfg |= ENCR_MODE_ECB << ENCR_MODE_SHIFT; |
|---|
| 254 | + break; |
|---|
| 255 | + case QCE_MODE_CBC: |
|---|
| 256 | + cfg |= ENCR_MODE_CBC << ENCR_MODE_SHIFT; |
|---|
| 257 | + break; |
|---|
| 258 | + case QCE_MODE_CTR: |
|---|
| 259 | + cfg |= ENCR_MODE_CTR << ENCR_MODE_SHIFT; |
|---|
| 260 | + break; |
|---|
| 261 | + case QCE_MODE_XTS: |
|---|
| 262 | + cfg |= ENCR_MODE_XTS << ENCR_MODE_SHIFT; |
|---|
| 263 | + break; |
|---|
| 264 | + case QCE_MODE_CCM: |
|---|
| 265 | + cfg |= ENCR_MODE_CCM << ENCR_MODE_SHIFT; |
|---|
| 266 | + cfg |= LAST_CCM_XFR << LAST_CCM_SHIFT; |
|---|
| 267 | + break; |
|---|
| 268 | + default: |
|---|
| 269 | + return ~0; |
|---|
| 270 | + } |
|---|
| 271 | + |
|---|
| 272 | + return cfg; |
|---|
| 273 | +} |
|---|
| 274 | + |
|---|
| 275 | +static void qce_xts_swapiv(__be32 *dst, const u8 *src, unsigned int ivsize) |
|---|
| 276 | +{ |
|---|
| 277 | + u8 swap[QCE_AES_IV_LENGTH]; |
|---|
| 278 | + u32 i, j; |
|---|
| 279 | + |
|---|
| 280 | + if (ivsize > QCE_AES_IV_LENGTH) |
|---|
| 281 | + return; |
|---|
| 282 | + |
|---|
| 283 | + memset(swap, 0, QCE_AES_IV_LENGTH); |
|---|
| 284 | + |
|---|
| 285 | + for (i = (QCE_AES_IV_LENGTH - ivsize), j = ivsize - 1; |
|---|
| 286 | + i < QCE_AES_IV_LENGTH; i++, j--) |
|---|
| 287 | + swap[i] = src[j]; |
|---|
| 288 | + |
|---|
| 289 | + qce_cpu_to_be32p_array(dst, swap, QCE_AES_IV_LENGTH); |
|---|
| 290 | +} |
|---|
| 291 | + |
|---|
| 292 | +static void qce_xtskey(struct qce_device *qce, const u8 *enckey, |
|---|
| 293 | + unsigned int enckeylen, unsigned int cryptlen) |
|---|
| 294 | +{ |
|---|
| 295 | + u32 xtskey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0}; |
|---|
| 296 | + unsigned int xtsklen = enckeylen / (2 * sizeof(u32)); |
|---|
| 297 | + unsigned int xtsdusize; |
|---|
| 298 | + |
|---|
| 299 | + qce_cpu_to_be32p_array((__be32 *)xtskey, enckey + enckeylen / 2, |
|---|
| 300 | + enckeylen / 2); |
|---|
| 301 | + qce_write_array(qce, REG_ENCR_XTS_KEY0, xtskey, xtsklen); |
|---|
| 302 | + |
|---|
| 303 | + /* xts du size 512B */ |
|---|
| 304 | + xtsdusize = min_t(u32, QCE_SECTOR_SIZE, cryptlen); |
|---|
| 305 | + qce_write(qce, REG_ENCR_XTS_DU_SIZE, xtsdusize); |
|---|
| 306 | +} |
|---|
| 307 | + |
|---|
| 308 | +static int qce_setup_regs_skcipher(struct crypto_async_request *async_req, |
|---|
| 316 | 309 | u32 totallen, u32 offset) |
|---|
| 317 | 310 | { |
|---|
| 318 | | - struct ablkcipher_request *req = ablkcipher_request_cast(async_req); |
|---|
| 319 | | - struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req); |
|---|
| 311 | + struct skcipher_request *req = skcipher_request_cast(async_req); |
|---|
| 312 | + struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req); |
|---|
| 320 | 313 | struct qce_cipher_ctx *ctx = crypto_tfm_ctx(async_req->tfm); |
|---|
| 321 | | - struct qce_alg_template *tmpl = to_cipher_tmpl(async_req->tfm); |
|---|
| 314 | + struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req)); |
|---|
| 322 | 315 | struct qce_device *qce = tmpl->qce; |
|---|
| 323 | 316 | __be32 enckey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(__be32)] = {0}; |
|---|
| 324 | 317 | __be32 enciv[QCE_MAX_IV_SIZE / sizeof(__be32)] = {0}; |
|---|
| .. | .. |
|---|
| 392 | 385 | |
|---|
| 393 | 386 | return 0; |
|---|
| 394 | 387 | } |
|---|
| 388 | +#endif |
|---|
| 395 | 389 | |
|---|
| 396 | 390 | int qce_start(struct crypto_async_request *async_req, u32 type, u32 totallen, |
|---|
| 397 | 391 | u32 offset) |
|---|
| 398 | 392 | { |
|---|
| 399 | 393 | switch (type) { |
|---|
| 400 | | - case CRYPTO_ALG_TYPE_ABLKCIPHER: |
|---|
| 401 | | - return qce_setup_regs_ablkcipher(async_req, totallen, offset); |
|---|
| 394 | +#ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER |
|---|
| 395 | + case CRYPTO_ALG_TYPE_SKCIPHER: |
|---|
| 396 | + return qce_setup_regs_skcipher(async_req, totallen, offset); |
|---|
| 397 | +#endif |
|---|
| 398 | +#ifdef CONFIG_CRYPTO_DEV_QCE_SHA |
|---|
| 402 | 399 | case CRYPTO_ALG_TYPE_AHASH: |
|---|
| 403 | 400 | return qce_setup_regs_ahash(async_req, totallen, offset); |
|---|
| 401 | +#endif |
|---|
| 404 | 402 | default: |
|---|
| 405 | 403 | return -EINVAL; |
|---|
| 406 | 404 | } |
|---|