Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Qualcomm Crypto driver |
| 2 | * |
| 3 | * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 and |
| 7 | * only version 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/dmapool.h> |
| 20 | #include <linux/crypto.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/rtnetlink.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/debugfs.h> |
| 26 | |
| 27 | #include <crypto/ctr.h> |
| 28 | #include <crypto/des.h> |
| 29 | #include <crypto/aes.h> |
| 30 | #include <crypto/sha.h> |
| 31 | #include <crypto/hash.h> |
| 32 | #include <crypto/algapi.h> |
| 33 | #include <crypto/aead.h> |
| 34 | #include <crypto/authenc.h> |
| 35 | #include <crypto/scatterwalk.h> |
| 36 | #include <crypto/internal/hash.h> |
| 37 | |
| 38 | #include <mach/scm.h> |
| 39 | #include <linux/platform_data/qcom_crypto_device.h> |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 40 | #include <mach/msm_bus.h> |
Mona Hossain | 5c8ea1f | 2011-07-28 15:11:29 -0700 | [diff] [blame] | 41 | #include "qce.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 42 | |
| 43 | |
| 44 | #define MAX_CRYPTO_DEVICE 3 |
| 45 | #define DEBUG_MAX_FNAME 16 |
| 46 | #define DEBUG_MAX_RW_BUF 1024 |
| 47 | |
| 48 | struct crypto_stat { |
| 49 | u32 aead_sha1_aes_enc; |
| 50 | u32 aead_sha1_aes_dec; |
| 51 | u32 aead_sha1_des_enc; |
| 52 | u32 aead_sha1_des_dec; |
| 53 | u32 aead_sha1_3des_enc; |
| 54 | u32 aead_sha1_3des_dec; |
| 55 | u32 aead_op_success; |
| 56 | u32 aead_op_fail; |
| 57 | u32 ablk_cipher_aes_enc; |
| 58 | u32 ablk_cipher_aes_dec; |
| 59 | u32 ablk_cipher_des_enc; |
| 60 | u32 ablk_cipher_des_dec; |
| 61 | u32 ablk_cipher_3des_enc; |
| 62 | u32 ablk_cipher_3des_dec; |
| 63 | u32 ablk_cipher_op_success; |
| 64 | u32 ablk_cipher_op_fail; |
| 65 | u32 sha1_digest; |
| 66 | u32 sha256_digest; |
| 67 | u32 sha_op_success; |
| 68 | u32 sha_op_fail; |
| 69 | u32 sha1_hmac_digest; |
| 70 | u32 sha256_hmac_digest; |
| 71 | u32 sha_hmac_op_success; |
| 72 | u32 sha_hmac_op_fail; |
| 73 | }; |
| 74 | static struct crypto_stat _qcrypto_stat[MAX_CRYPTO_DEVICE]; |
| 75 | static struct dentry *_debug_dent; |
| 76 | static char _debug_read_buf[DEBUG_MAX_RW_BUF]; |
| 77 | |
| 78 | struct crypto_priv { |
| 79 | /* CE features supported by target device*/ |
| 80 | struct msm_ce_hw_support platform_support; |
| 81 | |
| 82 | /* CE features/algorithms supported by HW engine*/ |
| 83 | struct ce_hw_support ce_support; |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 84 | |
| 85 | uint32_t bus_scale_handle; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 86 | /* the lock protects queue and req*/ |
| 87 | spinlock_t lock; |
| 88 | |
| 89 | /* qce handle */ |
| 90 | void *qce; |
| 91 | |
| 92 | /* list of registered algorithms */ |
| 93 | struct list_head alg_list; |
| 94 | |
| 95 | /* platform device */ |
| 96 | struct platform_device *pdev; |
| 97 | |
| 98 | /* current active request */ |
| 99 | struct crypto_async_request *req; |
| 100 | int res; |
| 101 | |
| 102 | /* request queue */ |
| 103 | struct crypto_queue queue; |
| 104 | |
| 105 | uint32_t ce_lock_count; |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 106 | uint32_t high_bw_req_count; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | |
| 108 | struct work_struct unlock_ce_ws; |
| 109 | |
| 110 | struct tasklet_struct done_tasklet; |
| 111 | }; |
| 112 | |
| 113 | |
| 114 | /*------------------------------------------------------------------------- |
| 115 | * Resource Locking Service |
| 116 | * ------------------------------------------------------------------------*/ |
| 117 | #define QCRYPTO_CMD_ID 1 |
| 118 | #define QCRYPTO_CE_LOCK_CMD 1 |
| 119 | #define QCRYPTO_CE_UNLOCK_CMD 0 |
| 120 | #define NUM_RETRY 1000 |
| 121 | #define CE_BUSY 55 |
| 122 | |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 123 | static DEFINE_MUTEX(sent_bw_req); |
| 124 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 125 | static int qcrypto_scm_cmd(int resource, int cmd, int *response) |
| 126 | { |
| 127 | #ifdef CONFIG_MSM_SCM |
| 128 | |
| 129 | struct { |
| 130 | int resource; |
| 131 | int cmd; |
| 132 | } cmd_buf; |
| 133 | |
| 134 | cmd_buf.resource = resource; |
| 135 | cmd_buf.cmd = cmd; |
| 136 | |
| 137 | return scm_call(SCM_SVC_TZ, QCRYPTO_CMD_ID, &cmd_buf, |
| 138 | sizeof(cmd_buf), response, sizeof(*response)); |
| 139 | |
| 140 | #else |
| 141 | return 0; |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | static void qcrypto_unlock_ce(struct work_struct *work) |
| 146 | { |
| 147 | int response = 0; |
| 148 | unsigned long flags; |
| 149 | struct crypto_priv *cp = container_of(work, struct crypto_priv, |
| 150 | unlock_ce_ws); |
| 151 | if (cp->ce_lock_count == 1) |
| 152 | BUG_ON(qcrypto_scm_cmd(cp->platform_support.shared_ce_resource, |
| 153 | QCRYPTO_CE_UNLOCK_CMD, &response) != 0); |
| 154 | spin_lock_irqsave(&cp->lock, flags); |
| 155 | cp->ce_lock_count--; |
| 156 | spin_unlock_irqrestore(&cp->lock, flags); |
| 157 | } |
| 158 | |
| 159 | static int qcrypto_lock_ce(struct crypto_priv *cp) |
| 160 | { |
| 161 | unsigned long flags; |
| 162 | int response = -CE_BUSY; |
| 163 | int i = 0; |
| 164 | |
| 165 | if (cp->ce_lock_count == 0) { |
| 166 | do { |
| 167 | if (qcrypto_scm_cmd( |
| 168 | cp->platform_support.shared_ce_resource, |
| 169 | QCRYPTO_CE_LOCK_CMD, &response)) { |
| 170 | response = -EINVAL; |
| 171 | break; |
| 172 | } |
| 173 | } while ((response == -CE_BUSY) && (i++ < NUM_RETRY)); |
| 174 | |
| 175 | if ((response == -CE_BUSY) && (i >= NUM_RETRY)) |
| 176 | return -EUSERS; |
| 177 | if (response < 0) |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | spin_lock_irqsave(&cp->lock, flags); |
| 181 | cp->ce_lock_count++; |
| 182 | spin_unlock_irqrestore(&cp->lock, flags); |
| 183 | |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | enum qcrypto_alg_type { |
| 189 | QCRYPTO_ALG_CIPHER = 0, |
| 190 | QCRYPTO_ALG_SHA = 1, |
| 191 | QCRYPTO_ALG_LAST |
| 192 | }; |
| 193 | |
| 194 | struct qcrypto_alg { |
| 195 | struct list_head entry; |
| 196 | struct crypto_alg cipher_alg; |
| 197 | struct ahash_alg sha_alg; |
| 198 | enum qcrypto_alg_type alg_type; |
| 199 | struct crypto_priv *cp; |
| 200 | }; |
| 201 | |
| 202 | #define QCRYPTO_MAX_KEY_SIZE 64 |
| 203 | /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */ |
| 204 | #define QCRYPTO_MAX_IV_LENGTH 16 |
| 205 | |
| 206 | struct qcrypto_cipher_ctx { |
| 207 | u8 auth_key[QCRYPTO_MAX_KEY_SIZE]; |
| 208 | u8 iv[QCRYPTO_MAX_IV_LENGTH]; |
| 209 | |
| 210 | u8 enc_key[QCRYPTO_MAX_KEY_SIZE]; |
| 211 | unsigned int enc_key_len; |
| 212 | |
| 213 | unsigned int authsize; |
| 214 | unsigned int auth_key_len; |
| 215 | |
| 216 | struct crypto_priv *cp; |
| 217 | }; |
| 218 | |
| 219 | struct qcrypto_cipher_req_ctx { |
| 220 | u8 *iv; |
| 221 | unsigned int ivsize; |
| 222 | int aead; |
| 223 | struct scatterlist asg; /* Formatted associated data sg */ |
| 224 | unsigned char *assoc; /* Pointer to formatted assoc data */ |
| 225 | unsigned int assoclen; /* Save Unformatted assoc data length */ |
| 226 | struct scatterlist *assoc_sg; /* Save Unformatted assoc data sg */ |
| 227 | enum qce_cipher_alg_enum alg; |
| 228 | enum qce_cipher_dir_enum dir; |
| 229 | enum qce_cipher_mode_enum mode; |
| 230 | }; |
| 231 | |
| 232 | #define SHA_MAX_BLOCK_SIZE SHA256_BLOCK_SIZE |
| 233 | #define SHA_MAX_STATE_SIZE (SHA256_DIGEST_SIZE / sizeof(u32)) |
| 234 | #define SHA_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE |
| 235 | |
| 236 | static uint8_t _std_init_vector_sha1_uint8[] = { |
| 237 | 0x67, 0x45, 0x23, 0x01, 0xEF, 0xCD, 0xAB, 0x89, |
| 238 | 0x98, 0xBA, 0xDC, 0xFE, 0x10, 0x32, 0x54, 0x76, |
| 239 | 0xC3, 0xD2, 0xE1, 0xF0 |
| 240 | }; |
| 241 | |
| 242 | /* standard initialization vector for SHA-256, source: FIPS 180-2 */ |
| 243 | static uint8_t _std_init_vector_sha256_uint8[] = { |
| 244 | 0x6A, 0x09, 0xE6, 0x67, 0xBB, 0x67, 0xAE, 0x85, |
| 245 | 0x3C, 0x6E, 0xF3, 0x72, 0xA5, 0x4F, 0xF5, 0x3A, |
| 246 | 0x51, 0x0E, 0x52, 0x7F, 0x9B, 0x05, 0x68, 0x8C, |
| 247 | 0x1F, 0x83, 0xD9, 0xAB, 0x5B, 0xE0, 0xCD, 0x19 |
| 248 | }; |
| 249 | |
| 250 | struct qcrypto_sha_ctx { |
| 251 | enum qce_hash_alg_enum alg; |
| 252 | uint32_t byte_count[4]; |
| 253 | uint8_t digest[SHA_MAX_DIGEST_SIZE]; |
| 254 | uint32_t diglen; |
| 255 | uint8_t *tmp_tbuf; |
| 256 | uint8_t *trailing_buf; |
| 257 | uint8_t *in_buf; |
| 258 | uint32_t authkey_in_len; |
| 259 | uint32_t trailing_buf_len; |
| 260 | uint8_t first_blk; |
| 261 | uint8_t last_blk; |
| 262 | uint8_t authkey[SHA_MAX_BLOCK_SIZE]; |
| 263 | struct ahash_request *ahash_req; |
| 264 | struct completion ahash_req_complete; |
| 265 | struct scatterlist *sg; |
| 266 | struct scatterlist tmp_sg; |
| 267 | struct crypto_priv *cp; |
| 268 | }; |
| 269 | |
| 270 | struct qcrypto_sha_req_ctx { |
| 271 | union { |
| 272 | struct sha1_state sha1_state_ctx; |
| 273 | struct sha256_state sha256_state_ctx; |
| 274 | }; |
| 275 | struct scatterlist *src; |
| 276 | uint32_t nbytes; |
| 277 | }; |
| 278 | |
| 279 | static void _byte_stream_to_words(uint32_t *iv, unsigned char *b, |
| 280 | unsigned int len) |
| 281 | { |
| 282 | unsigned n; |
| 283 | |
| 284 | n = len / sizeof(uint32_t) ; |
| 285 | for (; n > 0; n--) { |
| 286 | *iv = ((*b << 24) & 0xff000000) | |
| 287 | (((*(b+1)) << 16) & 0xff0000) | |
| 288 | (((*(b+2)) << 8) & 0xff00) | |
| 289 | (*(b+3) & 0xff); |
| 290 | b += sizeof(uint32_t); |
| 291 | iv++; |
| 292 | } |
| 293 | |
| 294 | n = len % sizeof(uint32_t); |
| 295 | if (n == 3) { |
| 296 | *iv = ((*b << 24) & 0xff000000) | |
| 297 | (((*(b+1)) << 16) & 0xff0000) | |
| 298 | (((*(b+2)) << 8) & 0xff00) ; |
| 299 | } else if (n == 2) { |
| 300 | *iv = ((*b << 24) & 0xff000000) | |
| 301 | (((*(b+1)) << 16) & 0xff0000) ; |
| 302 | } else if (n == 1) { |
| 303 | *iv = ((*b << 24) & 0xff000000) ; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | static void _words_to_byte_stream(uint32_t *iv, unsigned char *b, |
| 308 | unsigned int len) |
| 309 | { |
| 310 | unsigned n = len / sizeof(uint32_t); |
| 311 | |
| 312 | for (; n > 0; n--) { |
| 313 | *b++ = (unsigned char) ((*iv >> 24) & 0xff); |
| 314 | *b++ = (unsigned char) ((*iv >> 16) & 0xff); |
| 315 | *b++ = (unsigned char) ((*iv >> 8) & 0xff); |
| 316 | *b++ = (unsigned char) (*iv & 0xff); |
| 317 | iv++; |
| 318 | } |
| 319 | n = len % sizeof(uint32_t); |
| 320 | if (n == 3) { |
| 321 | *b++ = (unsigned char) ((*iv >> 24) & 0xff); |
| 322 | *b++ = (unsigned char) ((*iv >> 16) & 0xff); |
| 323 | *b = (unsigned char) ((*iv >> 8) & 0xff); |
| 324 | } else if (n == 2) { |
| 325 | *b++ = (unsigned char) ((*iv >> 24) & 0xff); |
| 326 | *b = (unsigned char) ((*iv >> 16) & 0xff); |
| 327 | } else if (n == 1) { |
| 328 | *b = (unsigned char) ((*iv >> 24) & 0xff); |
| 329 | } |
| 330 | } |
| 331 | |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 332 | static int qcrypto_ce_high_bw_req(struct crypto_priv *cp, bool high_bw_req) |
| 333 | { |
| 334 | int ret = 0; |
| 335 | |
| 336 | mutex_lock(&sent_bw_req); |
| 337 | if (high_bw_req) { |
| 338 | if (cp->high_bw_req_count == 0) |
| 339 | ret = msm_bus_scale_client_update_request( |
| 340 | cp->bus_scale_handle, 1); |
| 341 | cp->high_bw_req_count++; |
| 342 | } else { |
| 343 | if (cp->high_bw_req_count == 1) |
| 344 | ret = msm_bus_scale_client_update_request( |
| 345 | cp->bus_scale_handle, 0); |
| 346 | cp->high_bw_req_count--; |
| 347 | } |
| 348 | mutex_unlock(&sent_bw_req); |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 353 | static void _start_qcrypto_process(struct crypto_priv *cp); |
| 354 | |
| 355 | static struct qcrypto_alg *_qcrypto_sha_alg_alloc(struct crypto_priv *cp, |
| 356 | struct ahash_alg *template) |
| 357 | { |
| 358 | struct qcrypto_alg *q_alg; |
| 359 | q_alg = kzalloc(sizeof(struct qcrypto_alg), GFP_KERNEL); |
| 360 | if (!q_alg) { |
| 361 | pr_err("qcrypto Memory allocation of q_alg FAIL, error %ld\n", |
| 362 | PTR_ERR(q_alg)); |
| 363 | return ERR_PTR(-ENOMEM); |
| 364 | } |
| 365 | |
| 366 | q_alg->alg_type = QCRYPTO_ALG_SHA; |
| 367 | q_alg->sha_alg = *template; |
| 368 | q_alg->cp = cp; |
| 369 | |
| 370 | return q_alg; |
| 371 | }; |
| 372 | |
| 373 | static struct qcrypto_alg *_qcrypto_cipher_alg_alloc(struct crypto_priv *cp, |
| 374 | struct crypto_alg *template) |
| 375 | { |
| 376 | struct qcrypto_alg *q_alg; |
| 377 | |
| 378 | q_alg = kzalloc(sizeof(struct qcrypto_alg), GFP_KERNEL); |
| 379 | if (!q_alg) { |
| 380 | pr_err("qcrypto Memory allocation of q_alg FAIL, error %ld\n", |
| 381 | PTR_ERR(q_alg)); |
| 382 | return ERR_PTR(-ENOMEM); |
| 383 | } |
| 384 | |
| 385 | q_alg->alg_type = QCRYPTO_ALG_CIPHER; |
| 386 | q_alg->cipher_alg = *template; |
| 387 | q_alg->cp = cp; |
| 388 | |
| 389 | return q_alg; |
| 390 | }; |
| 391 | |
| 392 | static int _qcrypto_cipher_cra_init(struct crypto_tfm *tfm) |
| 393 | { |
| 394 | struct crypto_alg *alg = tfm->__crt_alg; |
| 395 | struct qcrypto_alg *q_alg; |
| 396 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 397 | |
| 398 | q_alg = container_of(alg, struct qcrypto_alg, cipher_alg); |
| 399 | |
| 400 | /* update context with ptr to cp */ |
| 401 | ctx->cp = q_alg->cp; |
| 402 | |
| 403 | /* random first IV */ |
| 404 | get_random_bytes(ctx->iv, QCRYPTO_MAX_IV_LENGTH); |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 405 | if (ctx->cp->platform_support.bus_scale_table != NULL) |
| 406 | return qcrypto_ce_high_bw_req(ctx->cp, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | }; |
| 410 | |
| 411 | static int _qcrypto_ahash_cra_init(struct crypto_tfm *tfm) |
| 412 | { |
| 413 | struct crypto_ahash *ahash = __crypto_ahash_cast(tfm); |
| 414 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(tfm); |
| 415 | struct ahash_alg *alg = container_of(crypto_hash_alg_common(ahash), |
| 416 | struct ahash_alg, halg); |
| 417 | struct qcrypto_alg *q_alg = container_of(alg, struct qcrypto_alg, |
| 418 | sha_alg); |
| 419 | |
| 420 | crypto_ahash_set_reqsize(ahash, sizeof(struct qcrypto_sha_req_ctx)); |
| 421 | /* update context with ptr to cp */ |
| 422 | sha_ctx->cp = q_alg->cp; |
| 423 | sha_ctx->sg = NULL; |
| 424 | sha_ctx->tmp_tbuf = kzalloc(SHA_MAX_BLOCK_SIZE + |
| 425 | SHA_MAX_DIGEST_SIZE, GFP_KERNEL); |
| 426 | if (sha_ctx->tmp_tbuf == NULL) { |
| 427 | pr_err("qcrypto Can't Allocate mem: sha_ctx->tmp_tbuf, error %ld\n", |
| 428 | PTR_ERR(sha_ctx->tmp_tbuf)); |
| 429 | return -ENOMEM; |
| 430 | } |
| 431 | |
| 432 | sha_ctx->trailing_buf = kzalloc(SHA_MAX_BLOCK_SIZE, GFP_KERNEL); |
| 433 | if (sha_ctx->trailing_buf == NULL) { |
| 434 | kfree(sha_ctx->tmp_tbuf); |
| 435 | sha_ctx->tmp_tbuf = NULL; |
| 436 | pr_err("qcrypto Can't Allocate mem: sha_ctx->trailing_buf, error %ld\n", |
| 437 | PTR_ERR(sha_ctx->trailing_buf)); |
| 438 | return -ENOMEM; |
| 439 | } |
| 440 | |
| 441 | sha_ctx->ahash_req = NULL; |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 442 | if (sha_ctx->cp->platform_support.bus_scale_table != NULL) |
| 443 | return qcrypto_ce_high_bw_req(sha_ctx->cp, true); |
| 444 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 445 | return 0; |
| 446 | }; |
| 447 | |
| 448 | static void _qcrypto_ahash_cra_exit(struct crypto_tfm *tfm) |
| 449 | { |
| 450 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(tfm); |
| 451 | |
| 452 | kfree(sha_ctx->tmp_tbuf); |
| 453 | sha_ctx->tmp_tbuf = NULL; |
| 454 | kfree(sha_ctx->trailing_buf); |
| 455 | sha_ctx->trailing_buf = NULL; |
| 456 | if (sha_ctx->sg != NULL) { |
| 457 | kfree(sha_ctx->sg); |
| 458 | sha_ctx->sg = NULL; |
| 459 | } |
| 460 | if (sha_ctx->ahash_req != NULL) { |
| 461 | ahash_request_free(sha_ctx->ahash_req); |
| 462 | sha_ctx->ahash_req = NULL; |
| 463 | } |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 464 | if (sha_ctx->cp->platform_support.bus_scale_table != NULL) |
| 465 | qcrypto_ce_high_bw_req(sha_ctx->cp, false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | |
| 469 | static void _crypto_sha_hmac_ahash_req_complete( |
| 470 | struct crypto_async_request *req, int err); |
| 471 | |
| 472 | static int _qcrypto_ahash_hmac_cra_init(struct crypto_tfm *tfm) |
| 473 | { |
| 474 | struct crypto_ahash *ahash = __crypto_ahash_cast(tfm); |
| 475 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(tfm); |
| 476 | int ret = 0; |
| 477 | |
| 478 | ret = _qcrypto_ahash_cra_init(tfm); |
| 479 | if (ret) |
| 480 | return ret; |
| 481 | sha_ctx->ahash_req = ahash_request_alloc(ahash, GFP_KERNEL); |
| 482 | |
| 483 | if (sha_ctx->ahash_req == NULL) { |
| 484 | _qcrypto_ahash_cra_exit(tfm); |
| 485 | return -ENOMEM; |
| 486 | } |
| 487 | |
| 488 | init_completion(&sha_ctx->ahash_req_complete); |
| 489 | ahash_request_set_callback(sha_ctx->ahash_req, |
| 490 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 491 | _crypto_sha_hmac_ahash_req_complete, |
| 492 | &sha_ctx->ahash_req_complete); |
| 493 | crypto_ahash_clear_flags(ahash, ~0); |
| 494 | |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 495 | if (sha_ctx->cp->platform_support.bus_scale_table != NULL) |
| 496 | qcrypto_ce_high_bw_req(sha_ctx->cp, true); |
| 497 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 498 | return 0; |
| 499 | }; |
| 500 | |
| 501 | static int _qcrypto_cra_ablkcipher_init(struct crypto_tfm *tfm) |
| 502 | { |
| 503 | tfm->crt_ablkcipher.reqsize = sizeof(struct qcrypto_cipher_req_ctx); |
| 504 | return _qcrypto_cipher_cra_init(tfm); |
| 505 | }; |
| 506 | |
| 507 | static int _qcrypto_cra_aead_init(struct crypto_tfm *tfm) |
| 508 | { |
| 509 | tfm->crt_aead.reqsize = sizeof(struct qcrypto_cipher_req_ctx); |
| 510 | return _qcrypto_cipher_cra_init(tfm); |
| 511 | }; |
| 512 | |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 513 | static void _qcrypto_cra_ablkcipher_exit(struct crypto_tfm *tfm) |
| 514 | { |
| 515 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 516 | |
| 517 | if (ctx->cp->platform_support.bus_scale_table != NULL) |
| 518 | qcrypto_ce_high_bw_req(ctx->cp, false); |
| 519 | }; |
| 520 | |
| 521 | static void _qcrypto_cra_aead_exit(struct crypto_tfm *tfm) |
| 522 | { |
| 523 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 524 | |
| 525 | if (ctx->cp->platform_support.bus_scale_table != NULL) |
| 526 | qcrypto_ce_high_bw_req(ctx->cp, false); |
| 527 | }; |
| 528 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 529 | static int _disp_stats(int id) |
| 530 | { |
| 531 | struct crypto_stat *pstat; |
| 532 | int len = 0; |
| 533 | |
| 534 | pstat = &_qcrypto_stat[id]; |
| 535 | len = snprintf(_debug_read_buf, DEBUG_MAX_RW_BUF - 1, |
| 536 | "\nQualcomm crypto accelerator %d Statistics:\n", |
| 537 | id + 1); |
| 538 | |
| 539 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 540 | " ABLK AES CIPHER encryption : %d\n", |
| 541 | pstat->ablk_cipher_aes_enc); |
| 542 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 543 | " ABLK AES CIPHER decryption : %d\n", |
| 544 | pstat->ablk_cipher_aes_dec); |
| 545 | |
| 546 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 547 | " ABLK DES CIPHER encryption : %d\n", |
| 548 | pstat->ablk_cipher_des_enc); |
| 549 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 550 | " ABLK DES CIPHER decryption : %d\n", |
| 551 | pstat->ablk_cipher_des_dec); |
| 552 | |
| 553 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 554 | " ABLK 3DES CIPHER encryption : %d\n", |
| 555 | pstat->ablk_cipher_3des_enc); |
| 556 | |
| 557 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 558 | " ABLK 3DES CIPHER decryption : %d\n", |
| 559 | pstat->ablk_cipher_3des_dec); |
| 560 | |
| 561 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 562 | " ABLK CIPHER operation success: %d\n", |
| 563 | pstat->ablk_cipher_op_success); |
| 564 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 565 | " ABLK CIPHER operation fail : %d\n", |
| 566 | pstat->ablk_cipher_op_fail); |
| 567 | |
| 568 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 569 | " AEAD SHA1-AES encryption : %d\n", |
| 570 | pstat->aead_sha1_aes_enc); |
| 571 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 572 | " AEAD SHA1-AES decryption : %d\n", |
| 573 | pstat->aead_sha1_aes_dec); |
| 574 | |
| 575 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 576 | " AEAD SHA1-DES encryption : %d\n", |
| 577 | pstat->aead_sha1_des_enc); |
| 578 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 579 | " AEAD SHA1-DES decryption : %d\n", |
| 580 | pstat->aead_sha1_des_dec); |
| 581 | |
| 582 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 583 | " AEAD SHA1-3DES encryption : %d\n", |
| 584 | pstat->aead_sha1_3des_enc); |
| 585 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 586 | " AEAD SHA1-3DES decryption : %d\n", |
| 587 | pstat->aead_sha1_3des_dec); |
| 588 | |
| 589 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 590 | " AEAD operation success : %d\n", |
| 591 | pstat->aead_op_success); |
| 592 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 593 | " AEAD operation fail : %d\n", |
| 594 | pstat->aead_op_fail); |
| 595 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 596 | " SHA1 digest : %d\n", |
| 597 | pstat->sha1_digest); |
| 598 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 599 | " SHA256 digest : %d\n", |
| 600 | pstat->sha256_digest); |
| 601 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 602 | " SHA operation fail : %d\n", |
| 603 | pstat->sha_op_fail); |
| 604 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 605 | " SHA operation success : %d\n", |
| 606 | pstat->sha_op_success); |
| 607 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 608 | " SHA1 HMAC digest : %d\n", |
| 609 | pstat->sha1_hmac_digest); |
| 610 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 611 | " SHA256 HMAC digest : %d\n", |
| 612 | pstat->sha256_hmac_digest); |
| 613 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 614 | " SHA HMAC operation fail : %d\n", |
| 615 | pstat->sha_hmac_op_fail); |
| 616 | len += snprintf(_debug_read_buf + len, DEBUG_MAX_RW_BUF - len - 1, |
| 617 | " SHA HMAC operation success : %d\n", |
| 618 | pstat->sha_hmac_op_success); |
| 619 | return len; |
| 620 | } |
| 621 | |
| 622 | static int _qcrypto_remove(struct platform_device *pdev) |
| 623 | { |
| 624 | struct crypto_priv *cp; |
| 625 | struct qcrypto_alg *q_alg; |
| 626 | struct qcrypto_alg *n; |
| 627 | |
| 628 | cp = platform_get_drvdata(pdev); |
| 629 | |
| 630 | if (!cp) |
| 631 | return 0; |
| 632 | |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 633 | if (cp->platform_support.bus_scale_table != NULL) |
| 634 | msm_bus_scale_unregister_client(cp->bus_scale_handle); |
| 635 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 636 | list_for_each_entry_safe(q_alg, n, &cp->alg_list, entry) { |
| 637 | if (q_alg->alg_type == QCRYPTO_ALG_CIPHER) |
| 638 | crypto_unregister_alg(&q_alg->cipher_alg); |
| 639 | if (q_alg->alg_type == QCRYPTO_ALG_SHA) |
| 640 | crypto_unregister_ahash(&q_alg->sha_alg); |
| 641 | list_del(&q_alg->entry); |
| 642 | kfree(q_alg); |
| 643 | } |
| 644 | |
| 645 | if (cp->qce) |
| 646 | qce_close(cp->qce); |
| 647 | tasklet_kill(&cp->done_tasklet); |
| 648 | kfree(cp); |
| 649 | return 0; |
| 650 | }; |
| 651 | |
| 652 | static int _qcrypto_setkey_aes(struct crypto_ablkcipher *cipher, const u8 *key, |
| 653 | unsigned int len) |
| 654 | { |
| 655 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); |
| 656 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 657 | struct crypto_priv *cp = ctx->cp; |
| 658 | |
| 659 | switch (len) { |
| 660 | case AES_KEYSIZE_128: |
| 661 | case AES_KEYSIZE_256: |
| 662 | break; |
| 663 | case AES_KEYSIZE_192: |
| 664 | if (cp->ce_support.aes_key_192) |
| 665 | break; |
| 666 | default: |
| 667 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 668 | return -EINVAL; |
| 669 | }; |
| 670 | ctx->enc_key_len = len; |
| 671 | memcpy(ctx->enc_key, key, len); |
| 672 | return 0; |
| 673 | }; |
| 674 | |
| 675 | static int _qcrypto_setkey_des(struct crypto_ablkcipher *cipher, const u8 *key, |
| 676 | unsigned int len) |
| 677 | { |
| 678 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); |
| 679 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 680 | u32 tmp[DES_EXPKEY_WORDS]; |
| 681 | int ret = des_ekey(tmp, key); |
| 682 | |
| 683 | if (len != DES_KEY_SIZE) { |
| 684 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 685 | return -EINVAL; |
| 686 | }; |
| 687 | |
| 688 | if (unlikely(ret == 0) && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) { |
| 689 | tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY; |
| 690 | return -EINVAL; |
| 691 | } |
| 692 | |
| 693 | ctx->enc_key_len = len; |
| 694 | memcpy(ctx->enc_key, key, len); |
| 695 | return 0; |
| 696 | }; |
| 697 | |
| 698 | static int _qcrypto_setkey_3des(struct crypto_ablkcipher *cipher, const u8 *key, |
| 699 | unsigned int len) |
| 700 | { |
| 701 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); |
| 702 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 703 | |
| 704 | if (len != DES3_EDE_KEY_SIZE) { |
| 705 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 706 | return -EINVAL; |
| 707 | }; |
| 708 | ctx->enc_key_len = len; |
| 709 | memcpy(ctx->enc_key, key, len); |
| 710 | return 0; |
| 711 | }; |
| 712 | |
| 713 | static void req_done(unsigned long data) |
| 714 | { |
| 715 | struct crypto_async_request *areq; |
| 716 | struct crypto_priv *cp = (struct crypto_priv *)data; |
| 717 | unsigned long flags; |
| 718 | |
| 719 | spin_lock_irqsave(&cp->lock, flags); |
| 720 | areq = cp->req; |
| 721 | cp->req = NULL; |
| 722 | spin_unlock_irqrestore(&cp->lock, flags); |
| 723 | |
| 724 | if (areq) |
| 725 | areq->complete(areq, cp->res); |
| 726 | _start_qcrypto_process(cp); |
| 727 | }; |
| 728 | |
| 729 | static void _update_sha1_ctx(struct ahash_request *req) |
| 730 | { |
| 731 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 732 | struct sha1_state *sha_state_ctx = &rctx->sha1_state_ctx; |
| 733 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 734 | |
| 735 | if (sha_ctx->last_blk == 1) |
| 736 | memset(sha_state_ctx, 0x00, sizeof(struct sha1_state)); |
| 737 | else { |
| 738 | memset(sha_state_ctx->buffer, 0x00, SHA1_BLOCK_SIZE); |
| 739 | memcpy(sha_state_ctx->buffer, sha_ctx->trailing_buf, |
| 740 | sha_ctx->trailing_buf_len); |
| 741 | _byte_stream_to_words(sha_state_ctx->state , sha_ctx->digest, |
| 742 | SHA1_DIGEST_SIZE); |
| 743 | } |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | static void _update_sha256_ctx(struct ahash_request *req) |
| 748 | { |
| 749 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 750 | struct sha256_state *sha_state_ctx = &rctx->sha256_state_ctx; |
| 751 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 752 | |
| 753 | if (sha_ctx->last_blk == 1) |
| 754 | memset(sha_state_ctx, 0x00, sizeof(struct sha256_state)); |
| 755 | else { |
| 756 | memset(sha_state_ctx->buf, 0x00, SHA256_BLOCK_SIZE); |
| 757 | memcpy(sha_state_ctx->buf, sha_ctx->trailing_buf, |
| 758 | sha_ctx->trailing_buf_len); |
| 759 | _byte_stream_to_words(sha_state_ctx->state, sha_ctx->digest, |
| 760 | SHA256_DIGEST_SIZE); |
| 761 | } |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | static void _qce_ahash_complete(void *cookie, unsigned char *digest, |
| 766 | unsigned char *authdata, int ret) |
| 767 | { |
| 768 | struct ahash_request *areq = (struct ahash_request *) cookie; |
| 769 | struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); |
| 770 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(areq->base.tfm); |
| 771 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(areq); |
| 772 | struct crypto_priv *cp = sha_ctx->cp; |
| 773 | struct crypto_stat *pstat; |
| 774 | uint32_t diglen = crypto_ahash_digestsize(ahash); |
| 775 | uint32_t *auth32 = (uint32_t *)authdata; |
| 776 | |
| 777 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 778 | |
| 779 | #ifdef QCRYPTO_DEBUG |
| 780 | dev_info(&cp->pdev->dev, "_qce_ahash_complete: %p ret %d\n", |
| 781 | areq, ret); |
| 782 | #endif |
| 783 | |
| 784 | if (digest) { |
| 785 | memcpy(sha_ctx->digest, digest, diglen); |
| 786 | memcpy(areq->result, digest, diglen); |
| 787 | } |
| 788 | if (authdata) { |
| 789 | sha_ctx->byte_count[0] = auth32[0]; |
| 790 | sha_ctx->byte_count[1] = auth32[1]; |
| 791 | sha_ctx->byte_count[2] = auth32[2]; |
| 792 | sha_ctx->byte_count[3] = auth32[3]; |
| 793 | } |
| 794 | areq->src = rctx->src; |
| 795 | areq->nbytes = rctx->nbytes; |
| 796 | |
| 797 | if (sha_ctx->sg != NULL) { |
| 798 | kfree(sha_ctx->sg); |
| 799 | sha_ctx->sg = NULL; |
| 800 | } |
| 801 | |
| 802 | if (sha_ctx->alg == QCE_HASH_SHA1) |
| 803 | _update_sha1_ctx(areq); |
| 804 | if (sha_ctx->alg == QCE_HASH_SHA256) |
| 805 | _update_sha256_ctx(areq); |
| 806 | |
| 807 | sha_ctx->last_blk = 0; |
| 808 | sha_ctx->first_blk = 0; |
| 809 | |
| 810 | if (ret) { |
| 811 | cp->res = -ENXIO; |
| 812 | pstat->sha_op_fail++; |
| 813 | } else { |
| 814 | cp->res = 0; |
| 815 | pstat->sha_op_success++; |
| 816 | } |
| 817 | |
| 818 | if (cp->platform_support.ce_shared) |
| 819 | schedule_work(&cp->unlock_ce_ws); |
| 820 | tasklet_schedule(&cp->done_tasklet); |
| 821 | }; |
| 822 | |
| 823 | static void _qce_ablk_cipher_complete(void *cookie, unsigned char *icb, |
| 824 | unsigned char *iv, int ret) |
| 825 | { |
| 826 | struct ablkcipher_request *areq = (struct ablkcipher_request *) cookie; |
| 827 | struct crypto_ablkcipher *ablk = crypto_ablkcipher_reqtfm(areq); |
| 828 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm); |
| 829 | struct crypto_priv *cp = ctx->cp; |
| 830 | struct crypto_stat *pstat; |
| 831 | |
| 832 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 833 | |
| 834 | #ifdef QCRYPTO_DEBUG |
| 835 | dev_info(&cp->pdev->dev, "_qce_ablk_cipher_complete: %p ret %d\n", |
| 836 | areq, ret); |
| 837 | #endif |
| 838 | if (iv) |
| 839 | memcpy(ctx->iv, iv, crypto_ablkcipher_ivsize(ablk)); |
| 840 | |
| 841 | if (ret) { |
| 842 | cp->res = -ENXIO; |
| 843 | pstat->ablk_cipher_op_fail++; |
| 844 | } else { |
| 845 | cp->res = 0; |
| 846 | pstat->ablk_cipher_op_success++; |
| 847 | } |
| 848 | if (cp->platform_support.ce_shared) |
| 849 | schedule_work(&cp->unlock_ce_ws); |
| 850 | tasklet_schedule(&cp->done_tasklet); |
| 851 | }; |
| 852 | |
| 853 | |
| 854 | static void _qce_aead_complete(void *cookie, unsigned char *icv, |
| 855 | unsigned char *iv, int ret) |
| 856 | { |
| 857 | struct aead_request *areq = (struct aead_request *) cookie; |
| 858 | struct crypto_aead *aead = crypto_aead_reqtfm(areq); |
| 859 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm); |
| 860 | struct crypto_priv *cp = ctx->cp; |
| 861 | struct qcrypto_cipher_req_ctx *rctx; |
| 862 | struct crypto_stat *pstat; |
| 863 | |
| 864 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 865 | |
| 866 | rctx = aead_request_ctx(areq); |
| 867 | |
| 868 | if (rctx->mode == QCE_MODE_CCM) { |
| 869 | kzfree(rctx->assoc); |
| 870 | areq->assoc = rctx->assoc_sg; |
| 871 | areq->assoclen = rctx->assoclen; |
| 872 | if (ret) { |
| 873 | if (ret == 0x2000000) |
| 874 | ret = -EBADMSG; |
| 875 | else |
| 876 | ret = -ENXIO; |
| 877 | } |
| 878 | } else { |
| 879 | if (ret == 0) { |
| 880 | if (rctx->dir == QCE_ENCRYPT) { |
| 881 | /* copy the icv to dst */ |
| 882 | scatterwalk_map_and_copy(icv, areq->dst, |
| 883 | areq->cryptlen, |
| 884 | ctx->authsize, 1); |
| 885 | |
| 886 | } else { |
| 887 | unsigned char tmp[SHA256_DIGESTSIZE]; |
| 888 | |
| 889 | /* compare icv from src */ |
| 890 | scatterwalk_map_and_copy(tmp, |
| 891 | areq->src, areq->cryptlen - |
| 892 | ctx->authsize, ctx->authsize, 0); |
| 893 | ret = memcmp(icv, tmp, ctx->authsize); |
| 894 | if (ret != 0) |
| 895 | ret = -EBADMSG; |
| 896 | |
| 897 | } |
| 898 | } else { |
| 899 | ret = -ENXIO; |
| 900 | } |
| 901 | |
| 902 | if (iv) |
| 903 | memcpy(ctx->iv, iv, crypto_aead_ivsize(aead)); |
| 904 | } |
| 905 | |
| 906 | if (ret) |
| 907 | pstat->aead_op_fail++; |
| 908 | else |
| 909 | pstat->aead_op_success++; |
| 910 | |
| 911 | if (cp->platform_support.ce_shared) |
| 912 | schedule_work(&cp->unlock_ce_ws); |
| 913 | tasklet_schedule(&cp->done_tasklet); |
| 914 | } |
| 915 | |
| 916 | static int aead_ccm_set_msg_len(u8 *block, unsigned int msglen, int csize) |
| 917 | { |
| 918 | __be32 data; |
| 919 | |
| 920 | memset(block, 0, csize); |
| 921 | block += csize; |
| 922 | |
| 923 | if (csize >= 4) |
| 924 | csize = 4; |
| 925 | else if (msglen > (1 << (8 * csize))) |
| 926 | return -EOVERFLOW; |
| 927 | |
| 928 | data = cpu_to_be32(msglen); |
| 929 | memcpy(block - csize, (u8 *)&data + 4 - csize, csize); |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static int qccrypto_set_aead_ccm_nonce(struct qce_req *qreq) |
| 935 | { |
| 936 | struct aead_request *areq = (struct aead_request *) qreq->areq; |
| 937 | unsigned int i = ((unsigned int)qreq->iv[0]) + 1; |
| 938 | |
| 939 | memcpy(&qreq->nonce[0] , qreq->iv, qreq->ivsize); |
| 940 | /* |
| 941 | * Format control info per RFC 3610 and |
| 942 | * NIST Special Publication 800-38C |
| 943 | */ |
| 944 | qreq->nonce[0] |= (8 * ((qreq->authsize - 2) / 2)); |
| 945 | if (areq->assoclen) |
| 946 | qreq->nonce[0] |= 64; |
| 947 | |
Ramesh Masavarapu | c52c237 | 2011-10-27 07:35:56 -0700 | [diff] [blame] | 948 | if (i > MAX_NONCE) |
| 949 | return -EINVAL; |
| 950 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 951 | return aead_ccm_set_msg_len(qreq->nonce + 16 - i, qreq->cryptlen, i); |
| 952 | } |
| 953 | |
| 954 | static int qcrypto_aead_ccm_format_adata(struct qce_req *qreq, uint32_t alen, |
| 955 | struct scatterlist *sg) |
| 956 | { |
| 957 | unsigned char *adata; |
| 958 | uint32_t len, l; |
| 959 | |
| 960 | qreq->assoc = kzalloc((alen + 0x64), (GFP_KERNEL | __GFP_DMA)); |
| 961 | if (!qreq->assoc) { |
| 962 | pr_err("qcrypto Memory allocation of adata FAIL, error %ld\n", |
| 963 | PTR_ERR(qreq->assoc)); |
| 964 | return -ENOMEM; |
| 965 | } |
| 966 | adata = qreq->assoc; |
| 967 | /* |
| 968 | * Add control info for associated data |
| 969 | * RFC 3610 and NIST Special Publication 800-38C |
| 970 | */ |
| 971 | if (alen < 65280) { |
| 972 | *(__be16 *)adata = cpu_to_be16(alen); |
| 973 | len = 2; |
| 974 | } else { |
| 975 | if ((alen >= 65280) && (alen <= 0xffffffff)) { |
| 976 | *(__be16 *)adata = cpu_to_be16(0xfffe); |
| 977 | *(__be32 *)&adata[2] = cpu_to_be32(alen); |
| 978 | len = 6; |
| 979 | } else { |
| 980 | *(__be16 *)adata = cpu_to_be16(0xffff); |
| 981 | *(__be32 *)&adata[6] = cpu_to_be32(alen); |
| 982 | len = 10; |
| 983 | } |
| 984 | } |
| 985 | adata += len; |
| 986 | qreq->assoclen = ALIGN((alen + len), 16); |
| 987 | for (l = alen; l > 0; sg = sg_next(sg)) { |
| 988 | memcpy(adata, sg_virt(sg), sg->length); |
| 989 | l -= sg->length; |
| 990 | adata += sg->length; |
| 991 | } |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | static void _start_qcrypto_process(struct crypto_priv *cp) |
| 996 | { |
| 997 | struct crypto_async_request *async_req = NULL; |
| 998 | struct crypto_async_request *backlog = NULL; |
| 999 | unsigned long flags; |
| 1000 | u32 type; |
| 1001 | struct qce_req qreq; |
| 1002 | int ret; |
| 1003 | struct qcrypto_cipher_req_ctx *rctx; |
| 1004 | struct qcrypto_cipher_ctx *cipher_ctx; |
| 1005 | struct qcrypto_sha_ctx *sha_ctx; |
| 1006 | struct crypto_stat *pstat; |
| 1007 | |
| 1008 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1009 | |
| 1010 | again: |
| 1011 | spin_lock_irqsave(&cp->lock, flags); |
| 1012 | if (cp->req == NULL) { |
| 1013 | backlog = crypto_get_backlog(&cp->queue); |
| 1014 | async_req = crypto_dequeue_request(&cp->queue); |
| 1015 | cp->req = async_req; |
| 1016 | } |
| 1017 | spin_unlock_irqrestore(&cp->lock, flags); |
| 1018 | if (!async_req) |
| 1019 | return; |
| 1020 | if (backlog) |
| 1021 | backlog->complete(backlog, -EINPROGRESS); |
| 1022 | type = crypto_tfm_alg_type(async_req->tfm); |
| 1023 | |
| 1024 | if (type == CRYPTO_ALG_TYPE_ABLKCIPHER) { |
| 1025 | struct ablkcipher_request *req; |
| 1026 | struct crypto_ablkcipher *tfm; |
| 1027 | |
| 1028 | req = container_of(async_req, struct ablkcipher_request, base); |
| 1029 | cipher_ctx = crypto_tfm_ctx(async_req->tfm); |
| 1030 | rctx = ablkcipher_request_ctx(req); |
| 1031 | tfm = crypto_ablkcipher_reqtfm(req); |
| 1032 | |
| 1033 | qreq.op = QCE_REQ_ABLK_CIPHER; |
| 1034 | qreq.qce_cb = _qce_ablk_cipher_complete; |
| 1035 | qreq.areq = req; |
| 1036 | qreq.alg = rctx->alg; |
| 1037 | qreq.dir = rctx->dir; |
| 1038 | qreq.mode = rctx->mode; |
| 1039 | qreq.enckey = cipher_ctx->enc_key; |
| 1040 | qreq.encklen = cipher_ctx->enc_key_len; |
| 1041 | qreq.iv = req->info; |
| 1042 | qreq.ivsize = crypto_ablkcipher_ivsize(tfm); |
| 1043 | qreq.cryptlen = req->nbytes; |
| 1044 | qreq.use_pmem = 0; |
| 1045 | |
| 1046 | if ((cipher_ctx->enc_key_len == 0) && |
| 1047 | (cp->platform_support.hw_key_support == 0)) |
| 1048 | ret = -EINVAL; |
| 1049 | else |
| 1050 | ret = qce_ablk_cipher_req(cp->qce, &qreq); |
| 1051 | } else { |
| 1052 | if (type == CRYPTO_ALG_TYPE_AHASH) { |
| 1053 | |
| 1054 | struct ahash_request *req; |
| 1055 | struct qce_sha_req sreq; |
| 1056 | |
| 1057 | req = container_of(async_req, |
| 1058 | struct ahash_request, base); |
| 1059 | sha_ctx = crypto_tfm_ctx(async_req->tfm); |
| 1060 | |
| 1061 | sreq.qce_cb = _qce_ahash_complete; |
| 1062 | sreq.digest = &sha_ctx->digest[0]; |
| 1063 | sreq.src = req->src; |
| 1064 | sreq.auth_data[0] = sha_ctx->byte_count[0]; |
| 1065 | sreq.auth_data[1] = sha_ctx->byte_count[1]; |
| 1066 | sreq.auth_data[2] = sha_ctx->byte_count[2]; |
| 1067 | sreq.auth_data[3] = sha_ctx->byte_count[3]; |
| 1068 | sreq.first_blk = sha_ctx->first_blk; |
| 1069 | sreq.last_blk = sha_ctx->last_blk; |
| 1070 | sreq.size = req->nbytes; |
| 1071 | sreq.areq = req; |
| 1072 | |
| 1073 | switch (sha_ctx->alg) { |
| 1074 | case QCE_HASH_SHA1: |
| 1075 | sreq.alg = QCE_HASH_SHA1; |
| 1076 | sreq.authkey = NULL; |
| 1077 | break; |
| 1078 | case QCE_HASH_SHA256: |
| 1079 | sreq.alg = QCE_HASH_SHA256; |
| 1080 | sreq.authkey = NULL; |
| 1081 | break; |
| 1082 | case QCE_HASH_SHA1_HMAC: |
| 1083 | sreq.alg = QCE_HASH_SHA1_HMAC; |
| 1084 | sreq.authkey = &sha_ctx->authkey[0]; |
| 1085 | break; |
| 1086 | case QCE_HASH_SHA256_HMAC: |
| 1087 | sreq.alg = QCE_HASH_SHA256_HMAC; |
| 1088 | sreq.authkey = &sha_ctx->authkey[0]; |
| 1089 | break; |
| 1090 | default: |
| 1091 | break; |
| 1092 | }; |
| 1093 | ret = qce_process_sha_req(cp->qce, &sreq); |
| 1094 | |
| 1095 | } else { |
| 1096 | struct aead_request *req = container_of(async_req, |
| 1097 | struct aead_request, base); |
| 1098 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 1099 | |
| 1100 | rctx = aead_request_ctx(req); |
| 1101 | cipher_ctx = crypto_tfm_ctx(async_req->tfm); |
| 1102 | |
| 1103 | qreq.op = QCE_REQ_AEAD; |
| 1104 | qreq.qce_cb = _qce_aead_complete; |
| 1105 | |
| 1106 | qreq.areq = req; |
| 1107 | qreq.alg = rctx->alg; |
| 1108 | qreq.dir = rctx->dir; |
| 1109 | qreq.mode = rctx->mode; |
| 1110 | qreq.iv = rctx->iv; |
| 1111 | |
| 1112 | qreq.enckey = cipher_ctx->enc_key; |
| 1113 | qreq.encklen = cipher_ctx->enc_key_len; |
| 1114 | qreq.authkey = cipher_ctx->auth_key; |
| 1115 | qreq.authklen = cipher_ctx->auth_key_len; |
| 1116 | qreq.authsize = crypto_aead_authsize(aead); |
| 1117 | qreq.ivsize = crypto_aead_ivsize(aead); |
| 1118 | if (qreq.mode == QCE_MODE_CCM) { |
| 1119 | if (qreq.dir == QCE_ENCRYPT) |
| 1120 | qreq.cryptlen = req->cryptlen; |
| 1121 | else |
| 1122 | qreq.cryptlen = req->cryptlen - |
| 1123 | qreq.authsize; |
| 1124 | /* Get NONCE */ |
| 1125 | ret = qccrypto_set_aead_ccm_nonce(&qreq); |
| 1126 | if (ret) |
| 1127 | goto done; |
| 1128 | /* Format Associated data */ |
| 1129 | ret = qcrypto_aead_ccm_format_adata(&qreq, |
| 1130 | req->assoclen, |
| 1131 | req->assoc); |
| 1132 | if (ret) |
| 1133 | goto done; |
| 1134 | /* |
| 1135 | * Save the original associated data |
| 1136 | * length and sg |
| 1137 | */ |
| 1138 | rctx->assoc_sg = req->assoc; |
| 1139 | rctx->assoclen = req->assoclen; |
| 1140 | rctx->assoc = qreq.assoc; |
| 1141 | /* |
| 1142 | * update req with new formatted associated |
| 1143 | * data info |
| 1144 | */ |
| 1145 | req->assoc = &rctx->asg; |
| 1146 | req->assoclen = qreq.assoclen; |
| 1147 | sg_set_buf(req->assoc, qreq.assoc, |
| 1148 | req->assoclen); |
| 1149 | sg_mark_end(req->assoc); |
| 1150 | } |
| 1151 | ret = qce_aead_req(cp->qce, &qreq); |
| 1152 | } |
| 1153 | }; |
| 1154 | done: |
| 1155 | if (ret) { |
| 1156 | |
| 1157 | spin_lock_irqsave(&cp->lock, flags); |
| 1158 | cp->req = NULL; |
| 1159 | spin_unlock_irqrestore(&cp->lock, flags); |
| 1160 | |
| 1161 | if (type == CRYPTO_ALG_TYPE_ABLKCIPHER) |
| 1162 | pstat->ablk_cipher_op_fail++; |
| 1163 | else |
| 1164 | if (type == CRYPTO_ALG_TYPE_AHASH) |
| 1165 | pstat->sha_op_fail++; |
| 1166 | else |
| 1167 | pstat->aead_op_fail++; |
| 1168 | |
| 1169 | async_req->complete(async_req, ret); |
| 1170 | goto again; |
| 1171 | }; |
| 1172 | }; |
| 1173 | |
| 1174 | static int _qcrypto_queue_req(struct crypto_priv *cp, |
| 1175 | struct crypto_async_request *req) |
| 1176 | { |
| 1177 | int ret; |
| 1178 | unsigned long flags; |
| 1179 | |
| 1180 | if (cp->platform_support.ce_shared) { |
| 1181 | ret = qcrypto_lock_ce(cp); |
| 1182 | if (ret) |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | spin_lock_irqsave(&cp->lock, flags); |
| 1187 | ret = crypto_enqueue_request(&cp->queue, req); |
| 1188 | spin_unlock_irqrestore(&cp->lock, flags); |
| 1189 | _start_qcrypto_process(cp); |
| 1190 | |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | static int _qcrypto_enc_aes_ecb(struct ablkcipher_request *req) |
| 1195 | { |
| 1196 | struct qcrypto_cipher_req_ctx *rctx; |
| 1197 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1198 | struct crypto_priv *cp = ctx->cp; |
| 1199 | struct crypto_stat *pstat; |
| 1200 | |
| 1201 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1202 | |
| 1203 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1204 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1205 | #ifdef QCRYPTO_DEBUG |
| 1206 | dev_info(&cp->pdev->dev, "_qcrypto_enc_aes_ecb: %p\n", req); |
| 1207 | #endif |
| 1208 | rctx = ablkcipher_request_ctx(req); |
| 1209 | rctx->aead = 0; |
| 1210 | rctx->alg = CIPHER_ALG_AES; |
| 1211 | rctx->dir = QCE_ENCRYPT; |
| 1212 | rctx->mode = QCE_MODE_ECB; |
| 1213 | |
| 1214 | pstat->ablk_cipher_aes_enc++; |
| 1215 | return _qcrypto_queue_req(cp, &req->base); |
| 1216 | }; |
| 1217 | |
| 1218 | static int _qcrypto_enc_aes_cbc(struct ablkcipher_request *req) |
| 1219 | { |
| 1220 | struct qcrypto_cipher_req_ctx *rctx; |
| 1221 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1222 | struct crypto_priv *cp = ctx->cp; |
| 1223 | struct crypto_stat *pstat; |
| 1224 | |
| 1225 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1226 | |
| 1227 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1228 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1229 | #ifdef QCRYPTO_DEBUG |
| 1230 | dev_info(&cp->pdev->dev, "_qcrypto_enc_aes_cbc: %p\n", req); |
| 1231 | #endif |
| 1232 | rctx = ablkcipher_request_ctx(req); |
| 1233 | rctx->aead = 0; |
| 1234 | rctx->alg = CIPHER_ALG_AES; |
| 1235 | rctx->dir = QCE_ENCRYPT; |
| 1236 | rctx->mode = QCE_MODE_CBC; |
| 1237 | |
| 1238 | pstat->ablk_cipher_aes_enc++; |
| 1239 | return _qcrypto_queue_req(cp, &req->base); |
| 1240 | }; |
| 1241 | |
| 1242 | static int _qcrypto_enc_aes_ctr(struct ablkcipher_request *req) |
| 1243 | { |
| 1244 | struct qcrypto_cipher_req_ctx *rctx; |
| 1245 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1246 | struct crypto_priv *cp = ctx->cp; |
| 1247 | struct crypto_stat *pstat; |
| 1248 | |
| 1249 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1250 | |
| 1251 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1252 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1253 | #ifdef QCRYPTO_DEBUG |
| 1254 | dev_info(&cp->pdev->dev, "_qcrypto_enc_aes_ctr: %p\n", req); |
| 1255 | #endif |
| 1256 | rctx = ablkcipher_request_ctx(req); |
| 1257 | rctx->aead = 0; |
| 1258 | rctx->alg = CIPHER_ALG_AES; |
| 1259 | rctx->dir = QCE_ENCRYPT; |
| 1260 | rctx->mode = QCE_MODE_CTR; |
| 1261 | |
| 1262 | pstat->ablk_cipher_aes_enc++; |
| 1263 | return _qcrypto_queue_req(cp, &req->base); |
| 1264 | }; |
| 1265 | |
| 1266 | static int _qcrypto_enc_aes_xts(struct ablkcipher_request *req) |
| 1267 | { |
| 1268 | struct qcrypto_cipher_req_ctx *rctx; |
| 1269 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1270 | struct crypto_priv *cp = ctx->cp; |
| 1271 | struct crypto_stat *pstat; |
| 1272 | |
| 1273 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1274 | |
| 1275 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1276 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1277 | rctx = ablkcipher_request_ctx(req); |
| 1278 | rctx->aead = 0; |
| 1279 | rctx->alg = CIPHER_ALG_AES; |
| 1280 | rctx->dir = QCE_ENCRYPT; |
| 1281 | rctx->mode = QCE_MODE_XTS; |
| 1282 | |
| 1283 | pstat->ablk_cipher_aes_enc++; |
| 1284 | return _qcrypto_queue_req(cp, &req->base); |
| 1285 | }; |
| 1286 | |
| 1287 | static int _qcrypto_aead_encrypt_aes_ccm(struct aead_request *req) |
| 1288 | { |
| 1289 | struct qcrypto_cipher_req_ctx *rctx; |
| 1290 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1291 | struct crypto_priv *cp = ctx->cp; |
| 1292 | struct crypto_stat *pstat; |
| 1293 | |
| 1294 | if ((ctx->authsize > 16) || (ctx->authsize < 4) || (ctx->authsize & 1)) |
| 1295 | return -EINVAL; |
| 1296 | if ((ctx->auth_key_len != AES_KEYSIZE_128) && |
| 1297 | (ctx->auth_key_len != AES_KEYSIZE_256)) |
| 1298 | return -EINVAL; |
| 1299 | |
| 1300 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1301 | |
| 1302 | rctx = aead_request_ctx(req); |
| 1303 | rctx->aead = 1; |
| 1304 | rctx->alg = CIPHER_ALG_AES; |
| 1305 | rctx->dir = QCE_ENCRYPT; |
| 1306 | rctx->mode = QCE_MODE_CCM; |
| 1307 | rctx->iv = req->iv; |
| 1308 | |
| 1309 | pstat->aead_sha1_aes_enc++; |
| 1310 | return _qcrypto_queue_req(cp, &req->base); |
| 1311 | } |
| 1312 | |
| 1313 | static int _qcrypto_enc_des_ecb(struct ablkcipher_request *req) |
| 1314 | { |
| 1315 | struct qcrypto_cipher_req_ctx *rctx; |
| 1316 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1317 | struct crypto_priv *cp = ctx->cp; |
| 1318 | struct crypto_stat *pstat; |
| 1319 | |
| 1320 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1321 | |
| 1322 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1323 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1324 | rctx = ablkcipher_request_ctx(req); |
| 1325 | rctx->aead = 0; |
| 1326 | rctx->alg = CIPHER_ALG_DES; |
| 1327 | rctx->dir = QCE_ENCRYPT; |
| 1328 | rctx->mode = QCE_MODE_ECB; |
| 1329 | |
| 1330 | pstat->ablk_cipher_des_enc++; |
| 1331 | return _qcrypto_queue_req(cp, &req->base); |
| 1332 | }; |
| 1333 | |
| 1334 | static int _qcrypto_enc_des_cbc(struct ablkcipher_request *req) |
| 1335 | { |
| 1336 | struct qcrypto_cipher_req_ctx *rctx; |
| 1337 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1338 | struct crypto_priv *cp = ctx->cp; |
| 1339 | struct crypto_stat *pstat; |
| 1340 | |
| 1341 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1342 | |
| 1343 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1344 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1345 | rctx = ablkcipher_request_ctx(req); |
| 1346 | rctx->aead = 0; |
| 1347 | rctx->alg = CIPHER_ALG_DES; |
| 1348 | rctx->dir = QCE_ENCRYPT; |
| 1349 | rctx->mode = QCE_MODE_CBC; |
| 1350 | |
| 1351 | pstat->ablk_cipher_des_enc++; |
| 1352 | return _qcrypto_queue_req(cp, &req->base); |
| 1353 | }; |
| 1354 | |
| 1355 | static int _qcrypto_enc_3des_ecb(struct ablkcipher_request *req) |
| 1356 | { |
| 1357 | struct qcrypto_cipher_req_ctx *rctx; |
| 1358 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1359 | struct crypto_priv *cp = ctx->cp; |
| 1360 | struct crypto_stat *pstat; |
| 1361 | |
| 1362 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1363 | |
| 1364 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1365 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1366 | rctx = ablkcipher_request_ctx(req); |
| 1367 | rctx->aead = 0; |
| 1368 | rctx->alg = CIPHER_ALG_3DES; |
| 1369 | rctx->dir = QCE_ENCRYPT; |
| 1370 | rctx->mode = QCE_MODE_ECB; |
| 1371 | |
| 1372 | pstat->ablk_cipher_3des_enc++; |
| 1373 | return _qcrypto_queue_req(cp, &req->base); |
| 1374 | }; |
| 1375 | |
| 1376 | static int _qcrypto_enc_3des_cbc(struct ablkcipher_request *req) |
| 1377 | { |
| 1378 | struct qcrypto_cipher_req_ctx *rctx; |
| 1379 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1380 | struct crypto_priv *cp = ctx->cp; |
| 1381 | struct crypto_stat *pstat; |
| 1382 | |
| 1383 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1384 | |
| 1385 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1386 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1387 | rctx = ablkcipher_request_ctx(req); |
| 1388 | rctx->aead = 0; |
| 1389 | rctx->alg = CIPHER_ALG_3DES; |
| 1390 | rctx->dir = QCE_ENCRYPT; |
| 1391 | rctx->mode = QCE_MODE_CBC; |
| 1392 | |
| 1393 | pstat->ablk_cipher_3des_enc++; |
| 1394 | return _qcrypto_queue_req(cp, &req->base); |
| 1395 | }; |
| 1396 | |
| 1397 | static int _qcrypto_dec_aes_ecb(struct ablkcipher_request *req) |
| 1398 | { |
| 1399 | struct qcrypto_cipher_req_ctx *rctx; |
| 1400 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1401 | struct crypto_priv *cp = ctx->cp; |
| 1402 | struct crypto_stat *pstat; |
| 1403 | |
| 1404 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1405 | |
| 1406 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1407 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1408 | #ifdef QCRYPTO_DEBUG |
| 1409 | dev_info(&cp->pdev->dev, "_qcrypto_dec_aes_ecb: %p\n", req); |
| 1410 | #endif |
| 1411 | rctx = ablkcipher_request_ctx(req); |
| 1412 | rctx->aead = 0; |
| 1413 | rctx->alg = CIPHER_ALG_AES; |
| 1414 | rctx->dir = QCE_DECRYPT; |
| 1415 | rctx->mode = QCE_MODE_ECB; |
| 1416 | |
| 1417 | pstat->ablk_cipher_aes_dec++; |
| 1418 | return _qcrypto_queue_req(cp, &req->base); |
| 1419 | }; |
| 1420 | |
| 1421 | static int _qcrypto_dec_aes_cbc(struct ablkcipher_request *req) |
| 1422 | { |
| 1423 | struct qcrypto_cipher_req_ctx *rctx; |
| 1424 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1425 | struct crypto_priv *cp = ctx->cp; |
| 1426 | struct crypto_stat *pstat; |
| 1427 | |
| 1428 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1429 | |
| 1430 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1431 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1432 | #ifdef QCRYPTO_DEBUG |
| 1433 | dev_info(&cp->pdev->dev, "_qcrypto_dec_aes_cbc: %p\n", req); |
| 1434 | #endif |
| 1435 | |
| 1436 | rctx = ablkcipher_request_ctx(req); |
| 1437 | rctx->aead = 0; |
| 1438 | rctx->alg = CIPHER_ALG_AES; |
| 1439 | rctx->dir = QCE_DECRYPT; |
| 1440 | rctx->mode = QCE_MODE_CBC; |
| 1441 | |
| 1442 | pstat->ablk_cipher_aes_dec++; |
| 1443 | return _qcrypto_queue_req(cp, &req->base); |
| 1444 | }; |
| 1445 | |
| 1446 | static int _qcrypto_dec_aes_ctr(struct ablkcipher_request *req) |
| 1447 | { |
| 1448 | struct qcrypto_cipher_req_ctx *rctx; |
| 1449 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1450 | struct crypto_priv *cp = ctx->cp; |
| 1451 | struct crypto_stat *pstat; |
| 1452 | |
| 1453 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1454 | |
| 1455 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1456 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1457 | #ifdef QCRYPTO_DEBUG |
| 1458 | dev_info(&cp->pdev->dev, "_qcrypto_dec_aes_ctr: %p\n", req); |
| 1459 | #endif |
| 1460 | rctx = ablkcipher_request_ctx(req); |
| 1461 | rctx->aead = 0; |
| 1462 | rctx->alg = CIPHER_ALG_AES; |
| 1463 | rctx->mode = QCE_MODE_CTR; |
| 1464 | |
| 1465 | /* Note. There is no such thing as aes/counter mode, decrypt */ |
| 1466 | rctx->dir = QCE_ENCRYPT; |
| 1467 | |
| 1468 | pstat->ablk_cipher_aes_dec++; |
| 1469 | return _qcrypto_queue_req(cp, &req->base); |
| 1470 | }; |
| 1471 | |
| 1472 | static int _qcrypto_dec_des_ecb(struct ablkcipher_request *req) |
| 1473 | { |
| 1474 | struct qcrypto_cipher_req_ctx *rctx; |
| 1475 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1476 | struct crypto_priv *cp = ctx->cp; |
| 1477 | struct crypto_stat *pstat; |
| 1478 | |
| 1479 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1480 | |
| 1481 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1482 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1483 | rctx = ablkcipher_request_ctx(req); |
| 1484 | rctx->aead = 0; |
| 1485 | rctx->alg = CIPHER_ALG_DES; |
| 1486 | rctx->dir = QCE_DECRYPT; |
| 1487 | rctx->mode = QCE_MODE_ECB; |
| 1488 | |
| 1489 | pstat->ablk_cipher_des_dec++; |
| 1490 | return _qcrypto_queue_req(cp, &req->base); |
| 1491 | }; |
| 1492 | |
| 1493 | static int _qcrypto_dec_des_cbc(struct ablkcipher_request *req) |
| 1494 | { |
| 1495 | struct qcrypto_cipher_req_ctx *rctx; |
| 1496 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1497 | struct crypto_priv *cp = ctx->cp; |
| 1498 | struct crypto_stat *pstat; |
| 1499 | |
| 1500 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1501 | |
| 1502 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1503 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1504 | rctx = ablkcipher_request_ctx(req); |
| 1505 | rctx->aead = 0; |
| 1506 | rctx->alg = CIPHER_ALG_DES; |
| 1507 | rctx->dir = QCE_DECRYPT; |
| 1508 | rctx->mode = QCE_MODE_CBC; |
| 1509 | |
| 1510 | pstat->ablk_cipher_des_dec++; |
| 1511 | return _qcrypto_queue_req(cp, &req->base); |
| 1512 | }; |
| 1513 | |
| 1514 | static int _qcrypto_dec_3des_ecb(struct ablkcipher_request *req) |
| 1515 | { |
| 1516 | struct qcrypto_cipher_req_ctx *rctx; |
| 1517 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1518 | struct crypto_priv *cp = ctx->cp; |
| 1519 | struct crypto_stat *pstat; |
| 1520 | |
| 1521 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1522 | |
| 1523 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1524 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1525 | rctx = ablkcipher_request_ctx(req); |
| 1526 | rctx->aead = 0; |
| 1527 | rctx->alg = CIPHER_ALG_3DES; |
| 1528 | rctx->dir = QCE_DECRYPT; |
| 1529 | rctx->mode = QCE_MODE_ECB; |
| 1530 | |
| 1531 | pstat->ablk_cipher_3des_dec++; |
| 1532 | return _qcrypto_queue_req(cp, &req->base); |
| 1533 | }; |
| 1534 | |
| 1535 | static int _qcrypto_dec_3des_cbc(struct ablkcipher_request *req) |
| 1536 | { |
| 1537 | struct qcrypto_cipher_req_ctx *rctx; |
| 1538 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1539 | struct crypto_priv *cp = ctx->cp; |
| 1540 | struct crypto_stat *pstat; |
| 1541 | |
| 1542 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1543 | |
| 1544 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1545 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1546 | rctx = ablkcipher_request_ctx(req); |
| 1547 | rctx->aead = 0; |
| 1548 | rctx->alg = CIPHER_ALG_3DES; |
| 1549 | rctx->dir = QCE_DECRYPT; |
| 1550 | rctx->mode = QCE_MODE_CBC; |
| 1551 | |
| 1552 | pstat->ablk_cipher_3des_dec++; |
| 1553 | return _qcrypto_queue_req(cp, &req->base); |
| 1554 | }; |
| 1555 | |
| 1556 | static int _qcrypto_dec_aes_xts(struct ablkcipher_request *req) |
| 1557 | { |
| 1558 | struct qcrypto_cipher_req_ctx *rctx; |
| 1559 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1560 | struct crypto_priv *cp = ctx->cp; |
| 1561 | struct crypto_stat *pstat; |
| 1562 | |
| 1563 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1564 | |
| 1565 | BUG_ON(crypto_tfm_alg_type(req->base.tfm) != |
| 1566 | CRYPTO_ALG_TYPE_ABLKCIPHER); |
| 1567 | rctx = ablkcipher_request_ctx(req); |
| 1568 | rctx->aead = 0; |
| 1569 | rctx->alg = CIPHER_ALG_AES; |
| 1570 | rctx->mode = QCE_MODE_XTS; |
| 1571 | rctx->dir = QCE_DECRYPT; |
| 1572 | |
| 1573 | pstat->ablk_cipher_aes_dec++; |
| 1574 | return _qcrypto_queue_req(cp, &req->base); |
| 1575 | }; |
| 1576 | |
| 1577 | |
| 1578 | static int _qcrypto_aead_decrypt_aes_ccm(struct aead_request *req) |
| 1579 | { |
| 1580 | struct qcrypto_cipher_req_ctx *rctx; |
| 1581 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1582 | struct crypto_priv *cp = ctx->cp; |
| 1583 | struct crypto_stat *pstat; |
| 1584 | |
| 1585 | if ((ctx->authsize > 16) || (ctx->authsize < 4) || (ctx->authsize & 1)) |
| 1586 | return -EINVAL; |
| 1587 | if ((ctx->auth_key_len != AES_KEYSIZE_128) && |
| 1588 | (ctx->auth_key_len != AES_KEYSIZE_256)) |
| 1589 | return -EINVAL; |
| 1590 | |
| 1591 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1592 | |
| 1593 | rctx = aead_request_ctx(req); |
| 1594 | rctx->aead = 1; |
| 1595 | rctx->alg = CIPHER_ALG_AES; |
| 1596 | rctx->dir = QCE_DECRYPT; |
| 1597 | rctx->mode = QCE_MODE_CCM; |
| 1598 | rctx->iv = req->iv; |
| 1599 | |
| 1600 | pstat->aead_sha1_aes_dec++; |
| 1601 | return _qcrypto_queue_req(cp, &req->base); |
| 1602 | } |
| 1603 | |
| 1604 | static int _qcrypto_aead_setauthsize(struct crypto_aead *authenc, |
| 1605 | unsigned int authsize) |
| 1606 | { |
| 1607 | struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc); |
| 1608 | |
| 1609 | ctx->authsize = authsize; |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | static int _qcrypto_aead_ccm_setauthsize(struct crypto_aead *authenc, |
| 1614 | unsigned int authsize) |
| 1615 | { |
| 1616 | struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(authenc); |
| 1617 | |
| 1618 | switch (authsize) { |
| 1619 | case 4: |
| 1620 | case 6: |
| 1621 | case 8: |
| 1622 | case 10: |
| 1623 | case 12: |
| 1624 | case 14: |
| 1625 | case 16: |
| 1626 | break; |
| 1627 | default: |
| 1628 | return -EINVAL; |
| 1629 | } |
| 1630 | ctx->authsize = authsize; |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | static int _qcrypto_aead_setkey(struct crypto_aead *tfm, const u8 *key, |
| 1635 | unsigned int keylen) |
| 1636 | { |
| 1637 | struct qcrypto_cipher_ctx *ctx = crypto_aead_ctx(tfm); |
| 1638 | struct rtattr *rta = (struct rtattr *)key; |
| 1639 | struct crypto_authenc_key_param *param; |
| 1640 | |
| 1641 | if (!RTA_OK(rta, keylen)) |
| 1642 | goto badkey; |
| 1643 | if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) |
| 1644 | goto badkey; |
| 1645 | if (RTA_PAYLOAD(rta) < sizeof(*param)) |
| 1646 | goto badkey; |
| 1647 | |
| 1648 | param = RTA_DATA(rta); |
| 1649 | ctx->enc_key_len = be32_to_cpu(param->enckeylen); |
| 1650 | |
| 1651 | key += RTA_ALIGN(rta->rta_len); |
| 1652 | keylen -= RTA_ALIGN(rta->rta_len); |
| 1653 | |
| 1654 | if (keylen < ctx->enc_key_len) |
| 1655 | goto badkey; |
| 1656 | |
| 1657 | ctx->auth_key_len = keylen - ctx->enc_key_len; |
| 1658 | if (ctx->enc_key_len >= QCRYPTO_MAX_KEY_SIZE || |
| 1659 | ctx->auth_key_len >= QCRYPTO_MAX_KEY_SIZE) |
| 1660 | goto badkey; |
| 1661 | memset(ctx->auth_key, 0, QCRYPTO_MAX_KEY_SIZE); |
| 1662 | memcpy(ctx->enc_key, key + ctx->auth_key_len, ctx->enc_key_len); |
| 1663 | memcpy(ctx->auth_key, key, ctx->auth_key_len); |
| 1664 | |
| 1665 | return 0; |
| 1666 | badkey: |
| 1667 | ctx->enc_key_len = 0; |
| 1668 | crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 1669 | return -EINVAL; |
| 1670 | } |
| 1671 | |
| 1672 | static int _qcrypto_aead_ccm_setkey(struct crypto_aead *aead, const u8 *key, |
| 1673 | unsigned int keylen) |
| 1674 | { |
| 1675 | struct crypto_tfm *tfm = crypto_aead_tfm(aead); |
| 1676 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1677 | struct crypto_priv *cp = ctx->cp; |
| 1678 | |
| 1679 | switch (keylen) { |
| 1680 | case AES_KEYSIZE_128: |
| 1681 | case AES_KEYSIZE_256: |
| 1682 | break; |
| 1683 | case AES_KEYSIZE_192: |
| 1684 | if (cp->ce_support.aes_key_192) |
| 1685 | break; |
| 1686 | default: |
| 1687 | ctx->enc_key_len = 0; |
| 1688 | crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 1689 | return -EINVAL; |
| 1690 | }; |
| 1691 | ctx->enc_key_len = keylen; |
| 1692 | memcpy(ctx->enc_key, key, keylen); |
| 1693 | ctx->auth_key_len = keylen; |
| 1694 | memcpy(ctx->auth_key, key, keylen); |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | static int _qcrypto_aead_encrypt_aes_cbc(struct aead_request *req) |
| 1700 | { |
| 1701 | struct qcrypto_cipher_req_ctx *rctx; |
| 1702 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1703 | struct crypto_priv *cp = ctx->cp; |
| 1704 | struct crypto_stat *pstat; |
| 1705 | |
| 1706 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1707 | |
| 1708 | #ifdef QCRYPTO_DEBUG |
| 1709 | dev_info(&cp->pdev->dev, "_qcrypto_aead_encrypt_aes_cbc: %p\n", req); |
| 1710 | #endif |
| 1711 | |
| 1712 | rctx = aead_request_ctx(req); |
| 1713 | rctx->aead = 1; |
| 1714 | rctx->alg = CIPHER_ALG_AES; |
| 1715 | rctx->dir = QCE_ENCRYPT; |
| 1716 | rctx->mode = QCE_MODE_CBC; |
| 1717 | rctx->iv = req->iv; |
| 1718 | |
| 1719 | pstat->aead_sha1_aes_enc++; |
| 1720 | return _qcrypto_queue_req(cp, &req->base); |
| 1721 | } |
| 1722 | |
| 1723 | static int _qcrypto_aead_decrypt_aes_cbc(struct aead_request *req) |
| 1724 | { |
| 1725 | struct qcrypto_cipher_req_ctx *rctx; |
| 1726 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1727 | struct crypto_priv *cp = ctx->cp; |
| 1728 | struct crypto_stat *pstat; |
| 1729 | |
| 1730 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1731 | |
| 1732 | #ifdef QCRYPTO_DEBUG |
| 1733 | dev_info(&cp->pdev->dev, "_qcrypto_aead_decrypt_aes_cbc: %p\n", req); |
| 1734 | #endif |
| 1735 | rctx = aead_request_ctx(req); |
| 1736 | rctx->aead = 1; |
| 1737 | rctx->alg = CIPHER_ALG_AES; |
| 1738 | rctx->dir = QCE_DECRYPT; |
| 1739 | rctx->mode = QCE_MODE_CBC; |
| 1740 | rctx->iv = req->iv; |
| 1741 | |
| 1742 | pstat->aead_sha1_aes_dec++; |
| 1743 | return _qcrypto_queue_req(cp, &req->base); |
| 1744 | } |
| 1745 | |
| 1746 | static int _qcrypto_aead_givencrypt_aes_cbc(struct aead_givcrypt_request *req) |
| 1747 | { |
| 1748 | struct aead_request *areq = &req->areq; |
| 1749 | struct crypto_aead *authenc = crypto_aead_reqtfm(areq); |
| 1750 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm); |
| 1751 | struct crypto_priv *cp = ctx->cp; |
| 1752 | struct qcrypto_cipher_req_ctx *rctx; |
| 1753 | struct crypto_stat *pstat; |
| 1754 | |
| 1755 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1756 | |
| 1757 | rctx = aead_request_ctx(areq); |
| 1758 | rctx->aead = 1; |
| 1759 | rctx->alg = CIPHER_ALG_AES; |
| 1760 | rctx->dir = QCE_ENCRYPT; |
| 1761 | rctx->mode = QCE_MODE_CBC; |
| 1762 | rctx->iv = req->giv; /* generated iv */ |
| 1763 | |
| 1764 | memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); |
| 1765 | /* avoid consecutive packets going out with same IV */ |
| 1766 | *(__be64 *)req->giv ^= cpu_to_be64(req->seq); |
| 1767 | pstat->aead_sha1_aes_enc++; |
| 1768 | return _qcrypto_queue_req(cp, &areq->base); |
| 1769 | } |
| 1770 | |
| 1771 | #ifdef QCRYPTO_AEAD_AES_CTR |
| 1772 | static int _qcrypto_aead_encrypt_aes_ctr(struct aead_request *req) |
| 1773 | { |
| 1774 | struct qcrypto_cipher_req_ctx *rctx; |
| 1775 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1776 | struct crypto_priv *cp = ctx->cp; |
| 1777 | struct crypto_stat *pstat; |
| 1778 | |
| 1779 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1780 | |
| 1781 | rctx = aead_request_ctx(req); |
| 1782 | rctx->aead = 1; |
| 1783 | rctx->alg = CIPHER_ALG_AES; |
| 1784 | rctx->dir = QCE_ENCRYPT; |
| 1785 | rctx->mode = QCE_MODE_CTR; |
| 1786 | rctx->iv = req->iv; |
| 1787 | |
| 1788 | pstat->aead_sha1_aes_enc++; |
| 1789 | return _qcrypto_queue_req(cp, &req->base); |
| 1790 | } |
| 1791 | |
| 1792 | static int _qcrypto_aead_decrypt_aes_ctr(struct aead_request *req) |
| 1793 | { |
| 1794 | struct qcrypto_cipher_req_ctx *rctx; |
| 1795 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1796 | struct crypto_priv *cp = ctx->cp; |
| 1797 | struct crypto_stat *pstat; |
| 1798 | |
| 1799 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1800 | |
| 1801 | rctx = aead_request_ctx(req); |
| 1802 | rctx->aead = 1; |
| 1803 | rctx->alg = CIPHER_ALG_AES; |
| 1804 | |
| 1805 | /* Note. There is no such thing as aes/counter mode, decrypt */ |
| 1806 | rctx->dir = QCE_ENCRYPT; |
| 1807 | |
| 1808 | rctx->mode = QCE_MODE_CTR; |
| 1809 | rctx->iv = req->iv; |
| 1810 | |
| 1811 | pstat->aead_sha1_aes_dec++; |
| 1812 | return _qcrypto_queue_req(cp, &req->base); |
| 1813 | } |
| 1814 | |
| 1815 | static int _qcrypto_aead_givencrypt_aes_ctr(struct aead_givcrypt_request *req) |
| 1816 | { |
| 1817 | struct aead_request *areq = &req->areq; |
| 1818 | struct crypto_aead *authenc = crypto_aead_reqtfm(areq); |
| 1819 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm); |
| 1820 | struct crypto_priv *cp = ctx->cp; |
| 1821 | struct qcrypto_cipher_req_ctx *rctx; |
| 1822 | struct crypto_stat *pstat; |
| 1823 | |
| 1824 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1825 | |
| 1826 | rctx = aead_request_ctx(areq); |
| 1827 | rctx->aead = 1; |
| 1828 | rctx->alg = CIPHER_ALG_AES; |
| 1829 | rctx->dir = QCE_ENCRYPT; |
| 1830 | rctx->mode = QCE_MODE_CTR; |
| 1831 | rctx->iv = req->giv; /* generated iv */ |
| 1832 | |
| 1833 | memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); |
| 1834 | /* avoid consecutive packets going out with same IV */ |
| 1835 | *(__be64 *)req->giv ^= cpu_to_be64(req->seq); |
| 1836 | pstat->aead_sha1_aes_enc++; |
| 1837 | return _qcrypto_queue_req(cp, &areq->base); |
| 1838 | }; |
| 1839 | #endif /* QCRYPTO_AEAD_AES_CTR */ |
| 1840 | |
| 1841 | static int _qcrypto_aead_encrypt_des_cbc(struct aead_request *req) |
| 1842 | { |
| 1843 | struct qcrypto_cipher_req_ctx *rctx; |
| 1844 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1845 | struct crypto_priv *cp = ctx->cp; |
| 1846 | struct crypto_stat *pstat; |
| 1847 | |
| 1848 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1849 | |
| 1850 | rctx = aead_request_ctx(req); |
| 1851 | rctx->aead = 1; |
| 1852 | rctx->alg = CIPHER_ALG_DES; |
| 1853 | rctx->dir = QCE_ENCRYPT; |
| 1854 | rctx->mode = QCE_MODE_CBC; |
| 1855 | rctx->iv = req->iv; |
| 1856 | |
| 1857 | pstat->aead_sha1_des_enc++; |
| 1858 | return _qcrypto_queue_req(cp, &req->base); |
| 1859 | } |
| 1860 | |
| 1861 | static int _qcrypto_aead_decrypt_des_cbc(struct aead_request *req) |
| 1862 | { |
| 1863 | struct qcrypto_cipher_req_ctx *rctx; |
| 1864 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1865 | struct crypto_priv *cp = ctx->cp; |
| 1866 | struct crypto_stat *pstat; |
| 1867 | |
| 1868 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1869 | |
| 1870 | rctx = aead_request_ctx(req); |
| 1871 | rctx->aead = 1; |
| 1872 | rctx->alg = CIPHER_ALG_DES; |
| 1873 | rctx->dir = QCE_DECRYPT; |
| 1874 | rctx->mode = QCE_MODE_CBC; |
| 1875 | rctx->iv = req->iv; |
| 1876 | |
| 1877 | pstat->aead_sha1_des_dec++; |
| 1878 | return _qcrypto_queue_req(cp, &req->base); |
| 1879 | } |
| 1880 | |
| 1881 | static int _qcrypto_aead_givencrypt_des_cbc(struct aead_givcrypt_request *req) |
| 1882 | { |
| 1883 | struct aead_request *areq = &req->areq; |
| 1884 | struct crypto_aead *authenc = crypto_aead_reqtfm(areq); |
| 1885 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm); |
| 1886 | struct crypto_priv *cp = ctx->cp; |
| 1887 | struct qcrypto_cipher_req_ctx *rctx; |
| 1888 | struct crypto_stat *pstat; |
| 1889 | |
| 1890 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1891 | |
| 1892 | rctx = aead_request_ctx(areq); |
| 1893 | rctx->aead = 1; |
| 1894 | rctx->alg = CIPHER_ALG_DES; |
| 1895 | rctx->dir = QCE_ENCRYPT; |
| 1896 | rctx->mode = QCE_MODE_CBC; |
| 1897 | rctx->iv = req->giv; /* generated iv */ |
| 1898 | |
| 1899 | memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); |
| 1900 | /* avoid consecutive packets going out with same IV */ |
| 1901 | *(__be64 *)req->giv ^= cpu_to_be64(req->seq); |
| 1902 | pstat->aead_sha1_des_enc++; |
| 1903 | return _qcrypto_queue_req(cp, &areq->base); |
| 1904 | } |
| 1905 | |
| 1906 | static int _qcrypto_aead_encrypt_3des_cbc(struct aead_request *req) |
| 1907 | { |
| 1908 | struct qcrypto_cipher_req_ctx *rctx; |
| 1909 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1910 | struct crypto_priv *cp = ctx->cp; |
| 1911 | struct crypto_stat *pstat; |
| 1912 | |
| 1913 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1914 | |
| 1915 | rctx = aead_request_ctx(req); |
| 1916 | rctx->aead = 1; |
| 1917 | rctx->alg = CIPHER_ALG_3DES; |
| 1918 | rctx->dir = QCE_ENCRYPT; |
| 1919 | rctx->mode = QCE_MODE_CBC; |
| 1920 | rctx->iv = req->iv; |
| 1921 | |
| 1922 | pstat->aead_sha1_3des_enc++; |
| 1923 | return _qcrypto_queue_req(cp, &req->base); |
| 1924 | } |
| 1925 | |
| 1926 | static int _qcrypto_aead_decrypt_3des_cbc(struct aead_request *req) |
| 1927 | { |
| 1928 | struct qcrypto_cipher_req_ctx *rctx; |
| 1929 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 1930 | struct crypto_priv *cp = ctx->cp; |
| 1931 | struct crypto_stat *pstat; |
| 1932 | |
| 1933 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1934 | |
| 1935 | rctx = aead_request_ctx(req); |
| 1936 | rctx->aead = 1; |
| 1937 | rctx->alg = CIPHER_ALG_3DES; |
| 1938 | rctx->dir = QCE_DECRYPT; |
| 1939 | rctx->mode = QCE_MODE_CBC; |
| 1940 | rctx->iv = req->iv; |
| 1941 | |
| 1942 | pstat->aead_sha1_3des_dec++; |
| 1943 | return _qcrypto_queue_req(cp, &req->base); |
| 1944 | } |
| 1945 | |
| 1946 | static int _qcrypto_aead_givencrypt_3des_cbc(struct aead_givcrypt_request *req) |
| 1947 | { |
| 1948 | struct aead_request *areq = &req->areq; |
| 1949 | struct crypto_aead *authenc = crypto_aead_reqtfm(areq); |
| 1950 | struct qcrypto_cipher_ctx *ctx = crypto_tfm_ctx(areq->base.tfm); |
| 1951 | struct crypto_priv *cp = ctx->cp; |
| 1952 | struct qcrypto_cipher_req_ctx *rctx; |
| 1953 | struct crypto_stat *pstat; |
| 1954 | |
| 1955 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 1956 | |
| 1957 | rctx = aead_request_ctx(areq); |
| 1958 | rctx->aead = 1; |
| 1959 | rctx->alg = CIPHER_ALG_3DES; |
| 1960 | rctx->dir = QCE_ENCRYPT; |
| 1961 | rctx->mode = QCE_MODE_CBC; |
| 1962 | rctx->iv = req->giv; /* generated iv */ |
| 1963 | |
| 1964 | memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); |
| 1965 | /* avoid consecutive packets going out with same IV */ |
| 1966 | *(__be64 *)req->giv ^= cpu_to_be64(req->seq); |
| 1967 | pstat->aead_sha1_3des_enc++; |
| 1968 | return _qcrypto_queue_req(cp, &areq->base); |
| 1969 | } |
| 1970 | |
| 1971 | static int qcrypto_count_sg(struct scatterlist *sg, int nbytes) |
| 1972 | { |
| 1973 | int i; |
| 1974 | |
| 1975 | for (i = 0; nbytes > 0; i++, sg = sg_next(sg)) |
| 1976 | nbytes -= sg->length; |
| 1977 | |
| 1978 | return i; |
| 1979 | } |
| 1980 | |
| 1981 | static int _sha_init(struct qcrypto_sha_ctx *ctx) |
| 1982 | { |
| 1983 | ctx->first_blk = 1; |
| 1984 | ctx->last_blk = 0; |
| 1985 | ctx->byte_count[0] = 0; |
| 1986 | ctx->byte_count[1] = 0; |
| 1987 | ctx->byte_count[2] = 0; |
| 1988 | ctx->byte_count[3] = 0; |
| 1989 | ctx->trailing_buf_len = 0; |
| 1990 | |
| 1991 | return 0; |
| 1992 | }; |
| 1993 | |
| 1994 | static int _sha1_init(struct ahash_request *req) |
| 1995 | { |
| 1996 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 1997 | struct crypto_priv *cp = sha_ctx->cp; |
| 1998 | struct crypto_stat *pstat; |
| 1999 | |
| 2000 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 2001 | |
| 2002 | _sha_init(sha_ctx); |
| 2003 | sha_ctx->alg = QCE_HASH_SHA1; |
| 2004 | |
| 2005 | memset(&sha_ctx->trailing_buf[0], 0x00, SHA1_BLOCK_SIZE); |
| 2006 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha1_uint8[0], |
| 2007 | SHA1_DIGEST_SIZE); |
| 2008 | sha_ctx->diglen = SHA1_DIGEST_SIZE; |
| 2009 | _update_sha1_ctx(req); |
| 2010 | |
| 2011 | pstat->sha1_digest++; |
| 2012 | return 0; |
| 2013 | }; |
| 2014 | |
| 2015 | static int _sha256_init(struct ahash_request *req) |
| 2016 | { |
| 2017 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2018 | struct crypto_priv *cp = sha_ctx->cp; |
| 2019 | struct crypto_stat *pstat; |
| 2020 | |
| 2021 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 2022 | |
| 2023 | _sha_init(sha_ctx); |
| 2024 | sha_ctx->alg = QCE_HASH_SHA256; |
| 2025 | |
| 2026 | memset(&sha_ctx->trailing_buf[0], 0x00, SHA256_BLOCK_SIZE); |
| 2027 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha256_uint8[0], |
| 2028 | SHA256_DIGEST_SIZE); |
| 2029 | sha_ctx->diglen = SHA256_DIGEST_SIZE; |
| 2030 | _update_sha256_ctx(req); |
| 2031 | |
| 2032 | pstat->sha256_digest++; |
| 2033 | return 0; |
| 2034 | }; |
| 2035 | |
| 2036 | |
| 2037 | static int _sha1_export(struct ahash_request *req, void *out) |
| 2038 | { |
| 2039 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2040 | struct sha1_state *sha_state_ctx = &rctx->sha1_state_ctx; |
| 2041 | struct sha1_state *out_ctx = (struct sha1_state *)out; |
| 2042 | |
| 2043 | out_ctx->count = sha_state_ctx->count; |
| 2044 | memcpy(out_ctx->state, sha_state_ctx->state, sizeof(out_ctx->state)); |
| 2045 | memcpy(out_ctx->buffer, sha_state_ctx->buffer, SHA1_BLOCK_SIZE); |
| 2046 | |
| 2047 | return 0; |
| 2048 | }; |
| 2049 | |
| 2050 | static int _sha1_import(struct ahash_request *req, const void *in) |
| 2051 | { |
| 2052 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2053 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2054 | struct sha1_state *sha_state_ctx = &rctx->sha1_state_ctx; |
| 2055 | struct sha1_state *in_ctx = (struct sha1_state *)in; |
| 2056 | |
| 2057 | sha_state_ctx->count = in_ctx->count; |
| 2058 | memcpy(sha_state_ctx->state, in_ctx->state, sizeof(in_ctx->state)); |
| 2059 | memcpy(sha_state_ctx->buffer, in_ctx->buffer, SHA1_BLOCK_SIZE); |
| 2060 | memcpy(sha_ctx->trailing_buf, in_ctx->buffer, SHA1_BLOCK_SIZE); |
| 2061 | |
| 2062 | sha_ctx->byte_count[0] = (uint32_t)(in_ctx->count & 0xFFFFFFC0); |
| 2063 | sha_ctx->byte_count[1] = (uint32_t)(in_ctx->count >> 32); |
| 2064 | _words_to_byte_stream(in_ctx->state, sha_ctx->digest, sha_ctx->diglen); |
| 2065 | |
| 2066 | sha_ctx->trailing_buf_len = (uint32_t)(in_ctx->count & |
| 2067 | (SHA1_BLOCK_SIZE-1)); |
| 2068 | |
| 2069 | if (!(in_ctx->count)) |
| 2070 | sha_ctx->first_blk = 1; |
| 2071 | else |
| 2072 | sha_ctx->first_blk = 0; |
| 2073 | |
| 2074 | return 0; |
| 2075 | } |
| 2076 | static int _sha256_export(struct ahash_request *req, void *out) |
| 2077 | { |
| 2078 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2079 | struct sha256_state *sha_state_ctx = &rctx->sha256_state_ctx; |
| 2080 | struct sha256_state *out_ctx = (struct sha256_state *)out; |
| 2081 | |
| 2082 | out_ctx->count = sha_state_ctx->count; |
| 2083 | memcpy(out_ctx->state, sha_state_ctx->state, sizeof(out_ctx->state)); |
| 2084 | memcpy(out_ctx->buf, sha_state_ctx->buf, SHA256_BLOCK_SIZE); |
| 2085 | |
| 2086 | return 0; |
| 2087 | }; |
| 2088 | |
| 2089 | static int _sha256_import(struct ahash_request *req, const void *in) |
| 2090 | { |
| 2091 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2092 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2093 | struct sha256_state *sha_state_ctx = &rctx->sha256_state_ctx; |
| 2094 | struct sha256_state *in_ctx = (struct sha256_state *)in; |
| 2095 | |
| 2096 | sha_state_ctx->count = in_ctx->count; |
| 2097 | memcpy(sha_state_ctx->state, in_ctx->state, sizeof(in_ctx->state)); |
| 2098 | memcpy(sha_state_ctx->buf, in_ctx->buf, SHA256_BLOCK_SIZE); |
| 2099 | memcpy(sha_ctx->trailing_buf, in_ctx->buf, SHA256_BLOCK_SIZE); |
| 2100 | |
| 2101 | sha_ctx->byte_count[0] = (uint32_t)(in_ctx->count & 0xFFFFFFC0); |
| 2102 | sha_ctx->byte_count[1] = (uint32_t)(in_ctx->count >> 32); |
| 2103 | _words_to_byte_stream(in_ctx->state, sha_ctx->digest, sha_ctx->diglen); |
| 2104 | |
| 2105 | sha_ctx->trailing_buf_len = (uint32_t)(in_ctx->count & |
| 2106 | (SHA256_BLOCK_SIZE-1)); |
| 2107 | |
| 2108 | if (!(in_ctx->count)) |
| 2109 | sha_ctx->first_blk = 1; |
| 2110 | else |
| 2111 | sha_ctx->first_blk = 0; |
| 2112 | |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | static int _sha_update(struct ahash_request *req, uint32_t sha_block_size) |
| 2118 | { |
| 2119 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2120 | struct crypto_priv *cp = sha_ctx->cp; |
| 2121 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2122 | uint32_t total, len, i, num_sg; |
| 2123 | uint8_t *k_src = NULL; |
| 2124 | uint32_t sha_pad_len = 0; |
| 2125 | uint32_t end_src = 0; |
| 2126 | uint32_t trailing_buf_len = 0; |
| 2127 | uint32_t nbytes, index = 0; |
| 2128 | uint32_t saved_length = 0; |
| 2129 | int ret = 0; |
| 2130 | |
| 2131 | /* check for trailing buffer from previous updates and append it */ |
| 2132 | total = req->nbytes + sha_ctx->trailing_buf_len; |
| 2133 | len = req->nbytes; |
| 2134 | |
| 2135 | if (total <= sha_block_size) { |
| 2136 | i = 0; |
| 2137 | |
| 2138 | k_src = &sha_ctx->trailing_buf[sha_ctx->trailing_buf_len]; |
| 2139 | while (len > 0) { |
| 2140 | memcpy(k_src, sg_virt(&req->src[i]), |
| 2141 | req->src[i].length); |
| 2142 | len -= req->src[i].length; |
| 2143 | k_src += req->src[i].length; |
| 2144 | i++; |
| 2145 | } |
| 2146 | sha_ctx->trailing_buf_len = total; |
| 2147 | if (sha_ctx->alg == QCE_HASH_SHA1) |
| 2148 | _update_sha1_ctx(req); |
| 2149 | if (sha_ctx->alg == QCE_HASH_SHA256) |
| 2150 | _update_sha256_ctx(req); |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
| 2154 | /* save the original req structure fields*/ |
| 2155 | rctx->src = req->src; |
| 2156 | rctx->nbytes = req->nbytes; |
| 2157 | |
| 2158 | memcpy(sha_ctx->tmp_tbuf, sha_ctx->trailing_buf, |
| 2159 | sha_ctx->trailing_buf_len); |
| 2160 | k_src = &sha_ctx->trailing_buf[0]; |
| 2161 | /* get new trailing buffer */ |
| 2162 | sha_pad_len = ALIGN(total, sha_block_size) - total; |
| 2163 | trailing_buf_len = sha_block_size - sha_pad_len; |
| 2164 | nbytes = total - trailing_buf_len; |
| 2165 | num_sg = qcrypto_count_sg(req->src, req->nbytes); |
| 2166 | |
| 2167 | len = sha_ctx->trailing_buf_len; |
| 2168 | i = 0; |
| 2169 | |
| 2170 | while (len < nbytes) { |
| 2171 | if ((len + req->src[i].length) > nbytes) |
| 2172 | break; |
| 2173 | len += req->src[i].length; |
| 2174 | i++; |
| 2175 | } |
| 2176 | |
| 2177 | end_src = i; |
| 2178 | if (len < nbytes) { |
| 2179 | uint32_t remnant = (nbytes - len); |
| 2180 | memcpy(k_src, (sg_virt(&req->src[i]) + remnant), |
| 2181 | (req->src[i].length - remnant)); |
| 2182 | k_src += (req->src[i].length - remnant); |
| 2183 | saved_length = req->src[i].length; |
| 2184 | index = i; |
| 2185 | req->src[i].length = remnant; |
| 2186 | i++; |
| 2187 | } |
| 2188 | |
| 2189 | while (i < num_sg) { |
| 2190 | memcpy(k_src, sg_virt(&req->src[i]), req->src[i].length); |
| 2191 | k_src += req->src[i].length; |
| 2192 | i++; |
| 2193 | } |
| 2194 | |
| 2195 | if (sha_ctx->trailing_buf_len) { |
| 2196 | num_sg = end_src + 2; |
| 2197 | sha_ctx->sg = kzalloc(num_sg * (sizeof(struct scatterlist)), |
| 2198 | GFP_KERNEL); |
| 2199 | if (sha_ctx->sg == NULL) { |
| 2200 | pr_err("qcrypto Can't Allocate mem: sha_ctx->sg, error %ld\n", |
| 2201 | PTR_ERR(sha_ctx->sg)); |
| 2202 | return -ENOMEM; |
| 2203 | } |
| 2204 | |
| 2205 | sg_set_buf(&sha_ctx->sg[0], sha_ctx->tmp_tbuf, |
| 2206 | sha_ctx->trailing_buf_len); |
| 2207 | for (i = 1; i < num_sg; i++) |
| 2208 | sg_set_buf(&sha_ctx->sg[i], sg_virt(&req->src[i-1]), |
| 2209 | req->src[i-1].length); |
| 2210 | |
| 2211 | req->src = sha_ctx->sg; |
| 2212 | sg_mark_end(&sha_ctx->sg[num_sg - 1]); |
| 2213 | } else |
| 2214 | sg_mark_end(&req->src[end_src]); |
| 2215 | |
| 2216 | req->nbytes = nbytes; |
| 2217 | if (saved_length > 0) |
| 2218 | rctx->src[index].length = saved_length; |
| 2219 | sha_ctx->trailing_buf_len = trailing_buf_len; |
| 2220 | |
| 2221 | ret = _qcrypto_queue_req(cp, &req->base); |
| 2222 | |
| 2223 | return ret; |
| 2224 | }; |
| 2225 | |
| 2226 | static int _sha1_update(struct ahash_request *req) |
| 2227 | { |
| 2228 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2229 | struct sha1_state *sha_state_ctx = &rctx->sha1_state_ctx; |
| 2230 | |
| 2231 | sha_state_ctx->count += req->nbytes; |
| 2232 | return _sha_update(req, SHA1_BLOCK_SIZE); |
| 2233 | } |
| 2234 | |
| 2235 | static int _sha256_update(struct ahash_request *req) |
| 2236 | { |
| 2237 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2238 | struct sha256_state *sha_state_ctx = &rctx->sha256_state_ctx; |
| 2239 | |
| 2240 | sha_state_ctx->count += req->nbytes; |
| 2241 | return _sha_update(req, SHA256_BLOCK_SIZE); |
| 2242 | } |
| 2243 | |
| 2244 | static int _sha_final(struct ahash_request *req, uint32_t sha_block_size) |
| 2245 | { |
| 2246 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2247 | struct crypto_priv *cp = sha_ctx->cp; |
| 2248 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2249 | int ret = 0; |
| 2250 | |
| 2251 | sha_ctx->last_blk = 1; |
| 2252 | |
| 2253 | /* save the original req structure fields*/ |
| 2254 | rctx->src = req->src; |
| 2255 | rctx->nbytes = req->nbytes; |
| 2256 | |
| 2257 | sg_set_buf(&sha_ctx->tmp_sg, sha_ctx->trailing_buf, |
| 2258 | sha_ctx->trailing_buf_len); |
| 2259 | sg_mark_end(&sha_ctx->tmp_sg); |
| 2260 | |
| 2261 | req->src = &sha_ctx->tmp_sg; |
| 2262 | req->nbytes = sha_ctx->trailing_buf_len; |
| 2263 | |
| 2264 | ret = _qcrypto_queue_req(cp, &req->base); |
| 2265 | |
| 2266 | return ret; |
| 2267 | }; |
| 2268 | |
| 2269 | static int _sha1_final(struct ahash_request *req) |
| 2270 | { |
| 2271 | return _sha_final(req, SHA1_BLOCK_SIZE); |
| 2272 | } |
| 2273 | |
| 2274 | static int _sha256_final(struct ahash_request *req) |
| 2275 | { |
| 2276 | return _sha_final(req, SHA256_BLOCK_SIZE); |
| 2277 | } |
| 2278 | |
| 2279 | static int _sha_digest(struct ahash_request *req) |
| 2280 | { |
| 2281 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2282 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2283 | struct crypto_priv *cp = sha_ctx->cp; |
| 2284 | int ret = 0; |
| 2285 | |
| 2286 | /* save the original req structure fields*/ |
| 2287 | rctx->src = req->src; |
| 2288 | rctx->nbytes = req->nbytes; |
| 2289 | |
| 2290 | sha_ctx->last_blk = 1; |
| 2291 | ret = _qcrypto_queue_req(cp, &req->base); |
| 2292 | |
| 2293 | return ret; |
| 2294 | } |
| 2295 | |
| 2296 | static int _sha1_digest(struct ahash_request *req) |
| 2297 | { |
| 2298 | _sha1_init(req); |
| 2299 | return _sha_digest(req); |
| 2300 | } |
| 2301 | |
| 2302 | static int _sha256_digest(struct ahash_request *req) |
| 2303 | { |
| 2304 | _sha256_init(req); |
| 2305 | return _sha_digest(req); |
| 2306 | } |
| 2307 | |
| 2308 | static void _crypto_sha_hmac_ahash_req_complete( |
| 2309 | struct crypto_async_request *req, int err) |
| 2310 | { |
| 2311 | struct completion *ahash_req_complete = req->data; |
| 2312 | |
| 2313 | if (err == -EINPROGRESS) |
| 2314 | return; |
| 2315 | complete(ahash_req_complete); |
| 2316 | } |
| 2317 | |
| 2318 | static int _sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 2319 | unsigned int len) |
| 2320 | { |
| 2321 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base); |
| 2322 | int ret = 0; |
| 2323 | |
| 2324 | sha_ctx->in_buf = kzalloc(len, GFP_KERNEL); |
| 2325 | if (sha_ctx->in_buf == NULL) { |
| 2326 | pr_err("qcrypto Can't Allocate mem: sha_ctx->in_buf, error %ld\n", |
| 2327 | PTR_ERR(sha_ctx->in_buf)); |
| 2328 | return -ENOMEM; |
| 2329 | } |
| 2330 | memcpy(sha_ctx->in_buf, key, len); |
| 2331 | sg_set_buf(&sha_ctx->tmp_sg, sha_ctx->in_buf, len); |
| 2332 | sg_mark_end(&sha_ctx->tmp_sg); |
| 2333 | |
| 2334 | ahash_request_set_crypt(sha_ctx->ahash_req, &sha_ctx->tmp_sg, |
| 2335 | &sha_ctx->authkey[0], len); |
| 2336 | |
| 2337 | ret = _sha_digest(sha_ctx->ahash_req); |
| 2338 | if (ret == -EINPROGRESS || ret == -EBUSY) { |
| 2339 | ret = |
| 2340 | wait_for_completion_interruptible( |
| 2341 | &sha_ctx->ahash_req_complete); |
| 2342 | INIT_COMPLETION(sha_ctx->ahash_req_complete); |
| 2343 | } |
| 2344 | |
| 2345 | sha_ctx->authkey_in_len = len; |
| 2346 | kfree(sha_ctx->in_buf); |
| 2347 | sha_ctx->in_buf = NULL; |
| 2348 | |
| 2349 | return ret; |
| 2350 | } |
| 2351 | |
| 2352 | static int _sha1_hmac_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 2353 | unsigned int len) |
| 2354 | { |
| 2355 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base); |
| 2356 | |
| 2357 | if (len <= SHA1_BLOCK_SIZE) |
| 2358 | memcpy(&sha_ctx->authkey[0], key, len); |
| 2359 | else { |
| 2360 | _sha_init(sha_ctx); |
| 2361 | sha_ctx->alg = QCE_HASH_SHA1; |
| 2362 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha1_uint8[0], |
| 2363 | SHA1_DIGEST_SIZE); |
| 2364 | sha_ctx->diglen = SHA1_DIGEST_SIZE; |
| 2365 | _sha_hmac_setkey(tfm, key, len); |
| 2366 | } |
| 2367 | return 0; |
| 2368 | } |
| 2369 | |
| 2370 | static int _sha256_hmac_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 2371 | unsigned int len) |
| 2372 | { |
| 2373 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(&tfm->base); |
| 2374 | |
| 2375 | if (len <= SHA256_BLOCK_SIZE) |
| 2376 | memcpy(&sha_ctx->authkey[0], key, len); |
| 2377 | else { |
| 2378 | _sha_init(sha_ctx); |
| 2379 | sha_ctx->alg = QCE_HASH_SHA256; |
| 2380 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha256_uint8[0], |
| 2381 | SHA256_DIGEST_SIZE); |
| 2382 | sha_ctx->diglen = SHA256_DIGEST_SIZE; |
| 2383 | _sha_hmac_setkey(tfm, key, len); |
| 2384 | } |
| 2385 | |
| 2386 | return 0; |
| 2387 | } |
| 2388 | |
| 2389 | static int _sha_hmac_init_ihash(struct ahash_request *req, |
| 2390 | uint32_t sha_block_size) |
| 2391 | { |
| 2392 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2393 | int i; |
| 2394 | |
| 2395 | for (i = 0; i < sha_block_size; i++) |
| 2396 | sha_ctx->trailing_buf[i] = sha_ctx->authkey[i] ^ 0x36; |
| 2397 | sha_ctx->trailing_buf_len = sha_block_size; |
| 2398 | |
| 2399 | return 0; |
| 2400 | } |
| 2401 | |
| 2402 | static int _sha1_hmac_init(struct ahash_request *req) |
| 2403 | { |
| 2404 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2405 | struct crypto_priv *cp = sha_ctx->cp; |
| 2406 | struct crypto_stat *pstat; |
| 2407 | int ret = 0; |
| 2408 | |
| 2409 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 2410 | pstat->sha1_hmac_digest++; |
| 2411 | |
| 2412 | _sha_init(sha_ctx); |
| 2413 | memset(&sha_ctx->trailing_buf[0], 0x00, SHA1_BLOCK_SIZE); |
| 2414 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha1_uint8[0], |
| 2415 | SHA1_DIGEST_SIZE); |
| 2416 | sha_ctx->diglen = SHA1_DIGEST_SIZE; |
| 2417 | _update_sha1_ctx(req); |
| 2418 | |
| 2419 | if (cp->ce_support.sha_hmac) |
| 2420 | sha_ctx->alg = QCE_HASH_SHA1_HMAC; |
| 2421 | else { |
| 2422 | sha_ctx->alg = QCE_HASH_SHA1; |
| 2423 | ret = _sha_hmac_init_ihash(req, SHA1_BLOCK_SIZE); |
| 2424 | } |
| 2425 | |
| 2426 | return ret; |
| 2427 | } |
| 2428 | |
| 2429 | static int _sha256_hmac_init(struct ahash_request *req) |
| 2430 | { |
| 2431 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2432 | struct crypto_priv *cp = sha_ctx->cp; |
| 2433 | struct crypto_stat *pstat; |
| 2434 | int ret = 0; |
| 2435 | |
| 2436 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 2437 | pstat->sha256_hmac_digest++; |
| 2438 | |
| 2439 | _sha_init(sha_ctx); |
| 2440 | memset(&sha_ctx->trailing_buf[0], 0x00, SHA256_BLOCK_SIZE); |
| 2441 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha256_uint8[0], |
| 2442 | SHA256_DIGEST_SIZE); |
| 2443 | sha_ctx->diglen = SHA256_DIGEST_SIZE; |
| 2444 | _update_sha256_ctx(req); |
| 2445 | |
| 2446 | if (cp->ce_support.sha_hmac) |
| 2447 | sha_ctx->alg = QCE_HASH_SHA256_HMAC; |
| 2448 | else { |
| 2449 | sha_ctx->alg = QCE_HASH_SHA256; |
| 2450 | ret = _sha_hmac_init_ihash(req, SHA256_BLOCK_SIZE); |
| 2451 | } |
| 2452 | |
| 2453 | return ret; |
| 2454 | } |
| 2455 | |
| 2456 | static int _sha1_hmac_update(struct ahash_request *req) |
| 2457 | { |
| 2458 | return _sha1_update(req); |
| 2459 | } |
| 2460 | |
| 2461 | static int _sha256_hmac_update(struct ahash_request *req) |
| 2462 | { |
| 2463 | return _sha256_update(req); |
| 2464 | } |
| 2465 | |
| 2466 | static int _sha_hmac_outer_hash(struct ahash_request *req, |
| 2467 | uint32_t sha_digest_size, uint32_t sha_block_size) |
| 2468 | { |
| 2469 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2470 | struct qcrypto_sha_req_ctx *rctx = ahash_request_ctx(req); |
| 2471 | struct crypto_priv *cp = sha_ctx->cp; |
| 2472 | int i; |
| 2473 | |
| 2474 | for (i = 0; i < sha_block_size; i++) |
| 2475 | sha_ctx->tmp_tbuf[i] = sha_ctx->authkey[i] ^ 0x5c; |
| 2476 | |
| 2477 | /* save the original req structure fields*/ |
| 2478 | rctx->src = req->src; |
| 2479 | rctx->nbytes = req->nbytes; |
| 2480 | |
| 2481 | memcpy(&sha_ctx->tmp_tbuf[sha_block_size], &sha_ctx->digest[0], |
| 2482 | sha_digest_size); |
| 2483 | |
| 2484 | sg_set_buf(&sha_ctx->tmp_sg, sha_ctx->tmp_tbuf, sha_block_size + |
| 2485 | sha_digest_size); |
| 2486 | sg_mark_end(&sha_ctx->tmp_sg); |
| 2487 | req->src = &sha_ctx->tmp_sg; |
| 2488 | req->nbytes = sha_block_size + sha_digest_size; |
| 2489 | |
| 2490 | _sha_init(sha_ctx); |
| 2491 | if (sha_ctx->alg == QCE_HASH_SHA1) { |
| 2492 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha1_uint8[0], |
| 2493 | SHA1_DIGEST_SIZE); |
| 2494 | sha_ctx->diglen = SHA1_DIGEST_SIZE; |
| 2495 | } else { |
| 2496 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha256_uint8[0], |
| 2497 | SHA256_DIGEST_SIZE); |
| 2498 | sha_ctx->diglen = SHA256_DIGEST_SIZE; |
| 2499 | } |
| 2500 | |
| 2501 | sha_ctx->last_blk = 1; |
| 2502 | return _qcrypto_queue_req(cp, &req->base); |
| 2503 | } |
| 2504 | |
| 2505 | static int _sha_hmac_inner_hash(struct ahash_request *req, |
| 2506 | uint32_t sha_digest_size, uint32_t sha_block_size) |
| 2507 | { |
| 2508 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2509 | struct ahash_request *areq = sha_ctx->ahash_req; |
| 2510 | struct crypto_priv *cp = sha_ctx->cp; |
| 2511 | int ret = 0; |
| 2512 | |
| 2513 | sha_ctx->last_blk = 1; |
| 2514 | |
| 2515 | sg_set_buf(&sha_ctx->tmp_sg, sha_ctx->trailing_buf, |
| 2516 | sha_ctx->trailing_buf_len); |
| 2517 | sg_mark_end(&sha_ctx->tmp_sg); |
| 2518 | |
| 2519 | ahash_request_set_crypt(areq, &sha_ctx->tmp_sg, &sha_ctx->digest[0], |
| 2520 | sha_ctx->trailing_buf_len); |
| 2521 | sha_ctx->last_blk = 1; |
| 2522 | ret = _qcrypto_queue_req(cp, &areq->base); |
| 2523 | |
| 2524 | if (ret == -EINPROGRESS || ret == -EBUSY) { |
| 2525 | ret = |
| 2526 | wait_for_completion_interruptible(&sha_ctx->ahash_req_complete); |
| 2527 | INIT_COMPLETION(sha_ctx->ahash_req_complete); |
| 2528 | } |
| 2529 | |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
| 2533 | static int _sha1_hmac_final(struct ahash_request *req) |
| 2534 | { |
| 2535 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2536 | struct crypto_priv *cp = sha_ctx->cp; |
| 2537 | int ret = 0; |
| 2538 | |
| 2539 | if (cp->ce_support.sha_hmac) |
| 2540 | return _sha_final(req, SHA1_BLOCK_SIZE); |
| 2541 | else { |
| 2542 | ret = _sha_hmac_inner_hash(req, SHA1_DIGEST_SIZE, |
| 2543 | SHA1_BLOCK_SIZE); |
| 2544 | if (ret) |
| 2545 | return ret; |
| 2546 | return _sha_hmac_outer_hash(req, SHA1_DIGEST_SIZE, |
| 2547 | SHA1_BLOCK_SIZE); |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | static int _sha256_hmac_final(struct ahash_request *req) |
| 2552 | { |
| 2553 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2554 | struct crypto_priv *cp = sha_ctx->cp; |
| 2555 | int ret = 0; |
| 2556 | |
| 2557 | if (cp->ce_support.sha_hmac) |
| 2558 | return _sha_final(req, SHA256_BLOCK_SIZE); |
| 2559 | else { |
| 2560 | ret = _sha_hmac_inner_hash(req, SHA256_DIGEST_SIZE, |
| 2561 | SHA256_BLOCK_SIZE); |
| 2562 | if (ret) |
| 2563 | return ret; |
| 2564 | return _sha_hmac_outer_hash(req, SHA256_DIGEST_SIZE, |
| 2565 | SHA256_BLOCK_SIZE); |
| 2566 | } |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
| 2570 | |
| 2571 | static int _sha1_hmac_digest(struct ahash_request *req) |
| 2572 | { |
| 2573 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2574 | struct crypto_priv *cp = sha_ctx->cp; |
| 2575 | struct crypto_stat *pstat; |
| 2576 | |
| 2577 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 2578 | pstat->sha1_hmac_digest++; |
| 2579 | |
| 2580 | _sha_init(sha_ctx); |
| 2581 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha1_uint8[0], |
| 2582 | SHA1_DIGEST_SIZE); |
| 2583 | sha_ctx->diglen = SHA1_DIGEST_SIZE; |
| 2584 | sha_ctx->alg = QCE_HASH_SHA1_HMAC; |
| 2585 | |
| 2586 | return _sha_digest(req); |
| 2587 | } |
| 2588 | |
| 2589 | static int _sha256_hmac_digest(struct ahash_request *req) |
| 2590 | { |
| 2591 | struct qcrypto_sha_ctx *sha_ctx = crypto_tfm_ctx(req->base.tfm); |
| 2592 | struct crypto_priv *cp = sha_ctx->cp; |
| 2593 | struct crypto_stat *pstat; |
| 2594 | |
| 2595 | pstat = &_qcrypto_stat[cp->pdev->id]; |
| 2596 | pstat->sha256_hmac_digest++; |
| 2597 | |
| 2598 | _sha_init(sha_ctx); |
| 2599 | memcpy(&sha_ctx->digest[0], &_std_init_vector_sha256_uint8[0], |
| 2600 | SHA256_DIGEST_SIZE); |
| 2601 | sha_ctx->diglen = SHA256_DIGEST_SIZE; |
| 2602 | sha_ctx->alg = QCE_HASH_SHA256_HMAC; |
| 2603 | |
| 2604 | return _sha_digest(req); |
| 2605 | } |
| 2606 | |
| 2607 | static struct ahash_alg _qcrypto_ahash_algos[] = { |
| 2608 | { |
| 2609 | .init = _sha1_init, |
| 2610 | .update = _sha1_update, |
| 2611 | .final = _sha1_final, |
| 2612 | .export = _sha1_export, |
| 2613 | .import = _sha1_import, |
| 2614 | .digest = _sha1_digest, |
| 2615 | .halg = { |
| 2616 | .digestsize = SHA1_DIGEST_SIZE, |
| 2617 | .statesize = sizeof(struct sha1_state), |
| 2618 | .base = { |
| 2619 | .cra_name = "sha1", |
| 2620 | .cra_driver_name = "qcrypto-sha1", |
| 2621 | .cra_priority = 300, |
| 2622 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 2623 | CRYPTO_ALG_ASYNC, |
| 2624 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 2625 | .cra_ctxsize = |
| 2626 | sizeof(struct qcrypto_sha_ctx), |
| 2627 | .cra_alignmask = 0, |
| 2628 | .cra_type = &crypto_ahash_type, |
| 2629 | .cra_module = THIS_MODULE, |
| 2630 | .cra_init = _qcrypto_ahash_cra_init, |
| 2631 | .cra_exit = _qcrypto_ahash_cra_exit, |
| 2632 | }, |
| 2633 | }, |
| 2634 | }, |
| 2635 | { |
| 2636 | .init = _sha256_init, |
| 2637 | .update = _sha256_update, |
| 2638 | .final = _sha256_final, |
| 2639 | .export = _sha256_export, |
| 2640 | .import = _sha256_import, |
| 2641 | .digest = _sha256_digest, |
| 2642 | .halg = { |
| 2643 | .digestsize = SHA256_DIGEST_SIZE, |
| 2644 | .statesize = sizeof(struct sha256_state), |
| 2645 | .base = { |
| 2646 | .cra_name = "sha256", |
| 2647 | .cra_driver_name = "qcrypto-sha256", |
| 2648 | .cra_priority = 300, |
| 2649 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 2650 | CRYPTO_ALG_ASYNC, |
| 2651 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 2652 | .cra_ctxsize = |
| 2653 | sizeof(struct qcrypto_sha_ctx), |
| 2654 | .cra_alignmask = 0, |
| 2655 | .cra_type = &crypto_ahash_type, |
| 2656 | .cra_module = THIS_MODULE, |
| 2657 | .cra_init = _qcrypto_ahash_cra_init, |
| 2658 | .cra_exit = _qcrypto_ahash_cra_exit, |
| 2659 | }, |
| 2660 | }, |
| 2661 | }, |
| 2662 | }; |
| 2663 | |
| 2664 | static struct ahash_alg _qcrypto_sha_hmac_algos[] = { |
| 2665 | { |
| 2666 | .init = _sha1_hmac_init, |
| 2667 | .update = _sha1_hmac_update, |
| 2668 | .final = _sha1_hmac_final, |
| 2669 | .export = _sha1_export, |
| 2670 | .import = _sha1_import, |
| 2671 | .digest = _sha1_hmac_digest, |
| 2672 | .setkey = _sha1_hmac_setkey, |
| 2673 | .halg = { |
| 2674 | .digestsize = SHA1_DIGEST_SIZE, |
| 2675 | .statesize = sizeof(struct sha1_state), |
| 2676 | .base = { |
| 2677 | .cra_name = "hmac(sha1)", |
| 2678 | .cra_driver_name = "qcrypto-hmac-sha1", |
| 2679 | .cra_priority = 300, |
| 2680 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 2681 | CRYPTO_ALG_ASYNC, |
| 2682 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 2683 | .cra_ctxsize = |
| 2684 | sizeof(struct qcrypto_sha_ctx), |
| 2685 | .cra_alignmask = 0, |
| 2686 | .cra_type = &crypto_ahash_type, |
| 2687 | .cra_module = THIS_MODULE, |
| 2688 | .cra_init = _qcrypto_ahash_hmac_cra_init, |
| 2689 | .cra_exit = _qcrypto_ahash_cra_exit, |
| 2690 | }, |
| 2691 | }, |
| 2692 | }, |
| 2693 | { |
| 2694 | .init = _sha256_hmac_init, |
| 2695 | .update = _sha256_hmac_update, |
| 2696 | .final = _sha256_hmac_final, |
| 2697 | .export = _sha256_export, |
| 2698 | .import = _sha256_import, |
| 2699 | .digest = _sha256_hmac_digest, |
| 2700 | .setkey = _sha256_hmac_setkey, |
| 2701 | .halg = { |
| 2702 | .digestsize = SHA256_DIGEST_SIZE, |
| 2703 | .statesize = sizeof(struct sha256_state), |
| 2704 | .base = { |
| 2705 | .cra_name = "hmac(sha256)", |
| 2706 | .cra_driver_name = "qcrypto-hmac-sha256", |
| 2707 | .cra_priority = 300, |
| 2708 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 2709 | CRYPTO_ALG_ASYNC, |
| 2710 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 2711 | .cra_ctxsize = |
| 2712 | sizeof(struct qcrypto_sha_ctx), |
| 2713 | .cra_alignmask = 0, |
| 2714 | .cra_type = &crypto_ahash_type, |
| 2715 | .cra_module = THIS_MODULE, |
| 2716 | .cra_init = _qcrypto_ahash_hmac_cra_init, |
| 2717 | .cra_exit = _qcrypto_ahash_cra_exit, |
| 2718 | }, |
| 2719 | }, |
| 2720 | }, |
| 2721 | }; |
| 2722 | |
| 2723 | static struct crypto_alg _qcrypto_ablk_cipher_algos[] = { |
| 2724 | { |
| 2725 | .cra_name = "ecb(aes)", |
| 2726 | .cra_driver_name = "qcrypto-ecb-aes", |
| 2727 | .cra_priority = 300, |
| 2728 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2729 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2730 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2731 | .cra_alignmask = 0, |
| 2732 | .cra_type = &crypto_ablkcipher_type, |
| 2733 | .cra_module = THIS_MODULE, |
| 2734 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2735 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2736 | .cra_u = { |
| 2737 | .ablkcipher = { |
| 2738 | .min_keysize = AES_MIN_KEY_SIZE, |
| 2739 | .max_keysize = AES_MAX_KEY_SIZE, |
| 2740 | .setkey = _qcrypto_setkey_aes, |
| 2741 | .encrypt = _qcrypto_enc_aes_ecb, |
| 2742 | .decrypt = _qcrypto_dec_aes_ecb, |
| 2743 | }, |
| 2744 | }, |
| 2745 | }, |
| 2746 | { |
| 2747 | .cra_name = "cbc(aes)", |
| 2748 | .cra_driver_name = "qcrypto-cbc-aes", |
| 2749 | .cra_priority = 300, |
| 2750 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2751 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2752 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2753 | .cra_alignmask = 0, |
| 2754 | .cra_type = &crypto_ablkcipher_type, |
| 2755 | .cra_module = THIS_MODULE, |
| 2756 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2757 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2758 | .cra_u = { |
| 2759 | .ablkcipher = { |
| 2760 | .ivsize = AES_BLOCK_SIZE, |
| 2761 | .min_keysize = AES_MIN_KEY_SIZE, |
| 2762 | .max_keysize = AES_MAX_KEY_SIZE, |
| 2763 | .setkey = _qcrypto_setkey_aes, |
| 2764 | .encrypt = _qcrypto_enc_aes_cbc, |
| 2765 | .decrypt = _qcrypto_dec_aes_cbc, |
| 2766 | }, |
| 2767 | }, |
| 2768 | }, |
| 2769 | { |
| 2770 | .cra_name = "ctr(aes)", |
| 2771 | .cra_driver_name = "qcrypto-ctr-aes", |
| 2772 | .cra_priority = 300, |
| 2773 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2774 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2775 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2776 | .cra_alignmask = 0, |
| 2777 | .cra_type = &crypto_ablkcipher_type, |
| 2778 | .cra_module = THIS_MODULE, |
| 2779 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2780 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2781 | .cra_u = { |
| 2782 | .ablkcipher = { |
| 2783 | .ivsize = AES_BLOCK_SIZE, |
| 2784 | .min_keysize = AES_MIN_KEY_SIZE, |
| 2785 | .max_keysize = AES_MAX_KEY_SIZE, |
| 2786 | .setkey = _qcrypto_setkey_aes, |
| 2787 | .encrypt = _qcrypto_enc_aes_ctr, |
| 2788 | .decrypt = _qcrypto_dec_aes_ctr, |
| 2789 | }, |
| 2790 | }, |
| 2791 | }, |
| 2792 | { |
| 2793 | .cra_name = "ecb(des)", |
| 2794 | .cra_driver_name = "qcrypto-ecb-des", |
| 2795 | .cra_priority = 300, |
| 2796 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2797 | .cra_blocksize = DES_BLOCK_SIZE, |
| 2798 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2799 | .cra_alignmask = 0, |
| 2800 | .cra_type = &crypto_ablkcipher_type, |
| 2801 | .cra_module = THIS_MODULE, |
| 2802 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2803 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2804 | .cra_u = { |
| 2805 | .ablkcipher = { |
| 2806 | .min_keysize = DES_KEY_SIZE, |
| 2807 | .max_keysize = DES_KEY_SIZE, |
| 2808 | .setkey = _qcrypto_setkey_des, |
| 2809 | .encrypt = _qcrypto_enc_des_ecb, |
| 2810 | .decrypt = _qcrypto_dec_des_ecb, |
| 2811 | }, |
| 2812 | }, |
| 2813 | }, |
| 2814 | { |
| 2815 | .cra_name = "cbc(des)", |
| 2816 | .cra_driver_name = "qcrypto-cbc-des", |
| 2817 | .cra_priority = 300, |
| 2818 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2819 | .cra_blocksize = DES_BLOCK_SIZE, |
| 2820 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2821 | .cra_alignmask = 0, |
| 2822 | .cra_type = &crypto_ablkcipher_type, |
| 2823 | .cra_module = THIS_MODULE, |
| 2824 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2825 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2826 | .cra_u = { |
| 2827 | .ablkcipher = { |
| 2828 | .ivsize = DES_BLOCK_SIZE, |
| 2829 | .min_keysize = DES_KEY_SIZE, |
| 2830 | .max_keysize = DES_KEY_SIZE, |
| 2831 | .setkey = _qcrypto_setkey_des, |
| 2832 | .encrypt = _qcrypto_enc_des_cbc, |
| 2833 | .decrypt = _qcrypto_dec_des_cbc, |
| 2834 | }, |
| 2835 | }, |
| 2836 | }, |
| 2837 | { |
| 2838 | .cra_name = "ecb(des3_ede)", |
| 2839 | .cra_driver_name = "qcrypto-ecb-3des", |
| 2840 | .cra_priority = 300, |
| 2841 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2842 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 2843 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2844 | .cra_alignmask = 0, |
| 2845 | .cra_type = &crypto_ablkcipher_type, |
| 2846 | .cra_module = THIS_MODULE, |
| 2847 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2848 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2849 | .cra_u = { |
| 2850 | .ablkcipher = { |
| 2851 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 2852 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 2853 | .setkey = _qcrypto_setkey_3des, |
| 2854 | .encrypt = _qcrypto_enc_3des_ecb, |
| 2855 | .decrypt = _qcrypto_dec_3des_ecb, |
| 2856 | }, |
| 2857 | }, |
| 2858 | }, |
| 2859 | { |
| 2860 | .cra_name = "cbc(des3_ede)", |
| 2861 | .cra_driver_name = "qcrypto-cbc-3des", |
| 2862 | .cra_priority = 300, |
| 2863 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2864 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 2865 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2866 | .cra_alignmask = 0, |
| 2867 | .cra_type = &crypto_ablkcipher_type, |
| 2868 | .cra_module = THIS_MODULE, |
| 2869 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2870 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2871 | .cra_u = { |
| 2872 | .ablkcipher = { |
| 2873 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 2874 | .min_keysize = DES3_EDE_KEY_SIZE, |
| 2875 | .max_keysize = DES3_EDE_KEY_SIZE, |
| 2876 | .setkey = _qcrypto_setkey_3des, |
| 2877 | .encrypt = _qcrypto_enc_3des_cbc, |
| 2878 | .decrypt = _qcrypto_dec_3des_cbc, |
| 2879 | }, |
| 2880 | }, |
| 2881 | }, |
| 2882 | }; |
| 2883 | |
| 2884 | static struct crypto_alg _qcrypto_ablk_cipher_xts_algo = { |
| 2885 | .cra_name = "xts(aes)", |
| 2886 | .cra_driver_name = "qcrypto-xts-aes", |
| 2887 | .cra_priority = 300, |
| 2888 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 2889 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2890 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2891 | .cra_alignmask = 0, |
| 2892 | .cra_type = &crypto_ablkcipher_type, |
| 2893 | .cra_module = THIS_MODULE, |
| 2894 | .cra_init = _qcrypto_cra_ablkcipher_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2895 | .cra_exit = _qcrypto_cra_ablkcipher_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2896 | .cra_u = { |
| 2897 | .ablkcipher = { |
| 2898 | .ivsize = AES_BLOCK_SIZE, |
| 2899 | .min_keysize = AES_MIN_KEY_SIZE, |
| 2900 | .max_keysize = AES_MAX_KEY_SIZE, |
| 2901 | .setkey = _qcrypto_setkey_aes, |
| 2902 | .encrypt = _qcrypto_enc_aes_xts, |
| 2903 | .decrypt = _qcrypto_dec_aes_xts, |
| 2904 | }, |
| 2905 | }, |
| 2906 | }; |
| 2907 | |
| 2908 | static struct crypto_alg _qcrypto_aead_sha1_hmac_algos[] = { |
| 2909 | { |
| 2910 | .cra_name = "authenc(hmac(sha1),cbc(aes))", |
| 2911 | .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-aes", |
| 2912 | .cra_priority = 300, |
| 2913 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, |
| 2914 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2915 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2916 | .cra_alignmask = 0, |
| 2917 | .cra_type = &crypto_aead_type, |
| 2918 | .cra_module = THIS_MODULE, |
| 2919 | .cra_init = _qcrypto_cra_aead_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2920 | .cra_exit = _qcrypto_cra_aead_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2921 | .cra_u = { |
| 2922 | .aead = { |
| 2923 | .ivsize = AES_BLOCK_SIZE, |
| 2924 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2925 | .setkey = _qcrypto_aead_setkey, |
| 2926 | .setauthsize = _qcrypto_aead_setauthsize, |
| 2927 | .encrypt = _qcrypto_aead_encrypt_aes_cbc, |
| 2928 | .decrypt = _qcrypto_aead_decrypt_aes_cbc, |
| 2929 | .givencrypt = _qcrypto_aead_givencrypt_aes_cbc, |
| 2930 | .geniv = "<built-in>", |
| 2931 | } |
| 2932 | } |
| 2933 | }, |
| 2934 | |
| 2935 | #ifdef QCRYPTO_AEAD_AES_CTR |
| 2936 | { |
| 2937 | .cra_name = "authenc(hmac(sha1),ctr(aes))", |
| 2938 | .cra_driver_name = "qcrypto-aead-hmac-sha1-ctr-aes", |
| 2939 | .cra_priority = 300, |
| 2940 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, |
| 2941 | .cra_blocksize = AES_BLOCK_SIZE, |
| 2942 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2943 | .cra_alignmask = 0, |
| 2944 | .cra_type = &crypto_aead_type, |
| 2945 | .cra_module = THIS_MODULE, |
| 2946 | .cra_init = _qcrypto_cra_aead_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2947 | .cra_exit = _qcrypto_cra_aead_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2948 | .cra_u = { |
| 2949 | .aead = { |
| 2950 | .ivsize = AES_BLOCK_SIZE, |
| 2951 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2952 | .setkey = _qcrypto_aead_setkey, |
| 2953 | .setauthsize = _qcrypto_aead_setauthsize, |
| 2954 | .encrypt = _qcrypto_aead_encrypt_aes_ctr, |
| 2955 | .decrypt = _qcrypto_aead_decrypt_aes_ctr, |
| 2956 | .givencrypt = _qcrypto_aead_givencrypt_aes_ctr, |
| 2957 | .geniv = "<built-in>", |
| 2958 | } |
| 2959 | } |
| 2960 | }, |
| 2961 | #endif /* QCRYPTO_AEAD_AES_CTR */ |
| 2962 | { |
| 2963 | .cra_name = "authenc(hmac(sha1),cbc(des))", |
| 2964 | .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-des", |
| 2965 | .cra_priority = 300, |
| 2966 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, |
| 2967 | .cra_blocksize = DES_BLOCK_SIZE, |
| 2968 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2969 | .cra_alignmask = 0, |
| 2970 | .cra_type = &crypto_aead_type, |
| 2971 | .cra_module = THIS_MODULE, |
| 2972 | .cra_init = _qcrypto_cra_aead_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2973 | .cra_exit = _qcrypto_cra_aead_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2974 | .cra_u = { |
| 2975 | .aead = { |
| 2976 | .ivsize = DES_BLOCK_SIZE, |
| 2977 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 2978 | .setkey = _qcrypto_aead_setkey, |
| 2979 | .setauthsize = _qcrypto_aead_setauthsize, |
| 2980 | .encrypt = _qcrypto_aead_encrypt_des_cbc, |
| 2981 | .decrypt = _qcrypto_aead_decrypt_des_cbc, |
| 2982 | .givencrypt = _qcrypto_aead_givencrypt_des_cbc, |
| 2983 | .geniv = "<built-in>", |
| 2984 | } |
| 2985 | } |
| 2986 | }, |
| 2987 | { |
| 2988 | .cra_name = "authenc(hmac(sha1),cbc(des3_ede))", |
| 2989 | .cra_driver_name = "qcrypto-aead-hmac-sha1-cbc-3des", |
| 2990 | .cra_priority = 300, |
| 2991 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, |
| 2992 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
| 2993 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 2994 | .cra_alignmask = 0, |
| 2995 | .cra_type = &crypto_aead_type, |
| 2996 | .cra_module = THIS_MODULE, |
| 2997 | .cra_init = _qcrypto_cra_aead_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 2998 | .cra_exit = _qcrypto_cra_aead_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2999 | .cra_u = { |
| 3000 | .aead = { |
| 3001 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 3002 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 3003 | .setkey = _qcrypto_aead_setkey, |
| 3004 | .setauthsize = _qcrypto_aead_setauthsize, |
| 3005 | .encrypt = _qcrypto_aead_encrypt_3des_cbc, |
| 3006 | .decrypt = _qcrypto_aead_decrypt_3des_cbc, |
| 3007 | .givencrypt = _qcrypto_aead_givencrypt_3des_cbc, |
| 3008 | .geniv = "<built-in>", |
| 3009 | } |
| 3010 | } |
| 3011 | }, |
| 3012 | }; |
| 3013 | |
| 3014 | static struct crypto_alg _qcrypto_aead_ccm_algo = { |
| 3015 | .cra_name = "ccm(aes)", |
| 3016 | .cra_driver_name = "qcrypto-aes-ccm", |
| 3017 | .cra_priority = 300, |
| 3018 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, |
| 3019 | .cra_blocksize = AES_BLOCK_SIZE, |
| 3020 | .cra_ctxsize = sizeof(struct qcrypto_cipher_ctx), |
| 3021 | .cra_alignmask = 0, |
| 3022 | .cra_type = &crypto_aead_type, |
| 3023 | .cra_module = THIS_MODULE, |
| 3024 | .cra_init = _qcrypto_cra_aead_init, |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 3025 | .cra_exit = _qcrypto_cra_aead_exit, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3026 | .cra_u = { |
| 3027 | .aead = { |
| 3028 | .ivsize = AES_BLOCK_SIZE, |
| 3029 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 3030 | .setkey = _qcrypto_aead_ccm_setkey, |
| 3031 | .setauthsize = _qcrypto_aead_ccm_setauthsize, |
| 3032 | .encrypt = _qcrypto_aead_encrypt_aes_ccm, |
| 3033 | .decrypt = _qcrypto_aead_decrypt_aes_ccm, |
| 3034 | .geniv = "<built-in>", |
| 3035 | } |
| 3036 | } |
| 3037 | }; |
| 3038 | |
| 3039 | |
| 3040 | static int _qcrypto_probe(struct platform_device *pdev) |
| 3041 | { |
| 3042 | int rc = 0; |
| 3043 | void *handle; |
| 3044 | struct crypto_priv *cp; |
| 3045 | int i; |
| 3046 | struct msm_ce_hw_support *platform_support; |
| 3047 | |
| 3048 | if (pdev->id >= MAX_CRYPTO_DEVICE) { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 3049 | pr_err("%s: device id %d exceeds allowed %d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3050 | __func__, pdev->id, MAX_CRYPTO_DEVICE); |
| 3051 | return -ENOENT; |
| 3052 | } |
| 3053 | |
| 3054 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
| 3055 | if (!cp) { |
| 3056 | pr_err("qcrypto Memory allocation of q_alg FAIL, error %ld\n", |
| 3057 | PTR_ERR(cp)); |
| 3058 | return -ENOMEM; |
| 3059 | } |
| 3060 | |
| 3061 | /* open qce */ |
| 3062 | handle = qce_open(pdev, &rc); |
| 3063 | if (handle == NULL) { |
| 3064 | kfree(cp); |
| 3065 | platform_set_drvdata(pdev, NULL); |
| 3066 | return rc; |
| 3067 | } |
| 3068 | |
| 3069 | INIT_LIST_HEAD(&cp->alg_list); |
| 3070 | platform_set_drvdata(pdev, cp); |
| 3071 | spin_lock_init(&cp->lock); |
| 3072 | tasklet_init(&cp->done_tasklet, req_done, (unsigned long)cp); |
| 3073 | crypto_init_queue(&cp->queue, 50); |
| 3074 | cp->qce = handle; |
| 3075 | cp->pdev = pdev; |
| 3076 | qce_hw_support(cp->qce, &cp->ce_support); |
| 3077 | platform_support = (struct msm_ce_hw_support *)pdev->dev.platform_data; |
| 3078 | cp->platform_support.ce_shared = platform_support->ce_shared; |
| 3079 | cp->platform_support.shared_ce_resource = |
| 3080 | platform_support->shared_ce_resource; |
| 3081 | cp->platform_support.hw_key_support = |
| 3082 | platform_support->hw_key_support; |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 3083 | cp->platform_support.bus_scale_table = |
| 3084 | platform_support->bus_scale_table; |
| 3085 | cp->high_bw_req_count = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3086 | cp->ce_lock_count = 0; |
| 3087 | cp->platform_support.sha_hmac = platform_support->sha_hmac; |
| 3088 | |
| 3089 | if (cp->platform_support.ce_shared) |
| 3090 | INIT_WORK(&cp->unlock_ce_ws, qcrypto_unlock_ce); |
| 3091 | |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 3092 | if (cp->platform_support.bus_scale_table != NULL) { |
| 3093 | cp->bus_scale_handle = |
| 3094 | msm_bus_scale_register_client( |
| 3095 | (struct msm_bus_scale_pdata *) |
| 3096 | cp->platform_support.bus_scale_table); |
| 3097 | if (!cp->bus_scale_handle) { |
| 3098 | printk(KERN_ERR "%s not able to get bus scale\n", |
| 3099 | __func__); |
| 3100 | rc = -ENOMEM; |
| 3101 | goto err; |
| 3102 | } |
| 3103 | } |
| 3104 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3105 | /* register crypto cipher algorithms the device supports */ |
| 3106 | for (i = 0; i < ARRAY_SIZE(_qcrypto_ablk_cipher_algos); i++) { |
| 3107 | struct qcrypto_alg *q_alg; |
| 3108 | |
| 3109 | q_alg = _qcrypto_cipher_alg_alloc(cp, |
| 3110 | &_qcrypto_ablk_cipher_algos[i]); |
| 3111 | if (IS_ERR(q_alg)) { |
| 3112 | rc = PTR_ERR(q_alg); |
| 3113 | goto err; |
| 3114 | } |
| 3115 | rc = crypto_register_alg(&q_alg->cipher_alg); |
| 3116 | if (rc) { |
| 3117 | dev_err(&pdev->dev, "%s alg registration failed\n", |
| 3118 | q_alg->cipher_alg.cra_driver_name); |
| 3119 | kfree(q_alg); |
| 3120 | } else { |
| 3121 | list_add_tail(&q_alg->entry, &cp->alg_list); |
| 3122 | dev_info(&pdev->dev, "%s\n", |
| 3123 | q_alg->cipher_alg.cra_driver_name); |
| 3124 | } |
| 3125 | } |
| 3126 | |
| 3127 | /* register crypto cipher algorithms the device supports */ |
| 3128 | if (cp->ce_support.aes_xts) { |
| 3129 | struct qcrypto_alg *q_alg; |
| 3130 | |
| 3131 | q_alg = _qcrypto_cipher_alg_alloc(cp, |
| 3132 | &_qcrypto_ablk_cipher_xts_algo); |
| 3133 | if (IS_ERR(q_alg)) { |
| 3134 | rc = PTR_ERR(q_alg); |
| 3135 | goto err; |
| 3136 | } |
| 3137 | rc = crypto_register_alg(&q_alg->cipher_alg); |
| 3138 | if (rc) { |
| 3139 | dev_err(&pdev->dev, "%s alg registration failed\n", |
| 3140 | q_alg->cipher_alg.cra_driver_name); |
| 3141 | kfree(q_alg); |
| 3142 | } else { |
| 3143 | list_add_tail(&q_alg->entry, &cp->alg_list); |
| 3144 | dev_info(&pdev->dev, "%s\n", |
| 3145 | q_alg->cipher_alg.cra_driver_name); |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | /* |
| 3150 | * Register crypto hash (sha1 and sha256) algorithms the |
| 3151 | * device supports |
| 3152 | */ |
| 3153 | for (i = 0; i < ARRAY_SIZE(_qcrypto_ahash_algos); i++) { |
| 3154 | struct qcrypto_alg *q_alg = NULL; |
| 3155 | |
| 3156 | q_alg = _qcrypto_sha_alg_alloc(cp, &_qcrypto_ahash_algos[i]); |
| 3157 | |
| 3158 | if (IS_ERR(q_alg)) { |
| 3159 | rc = PTR_ERR(q_alg); |
| 3160 | goto err; |
| 3161 | } |
| 3162 | |
| 3163 | rc = crypto_register_ahash(&q_alg->sha_alg); |
| 3164 | if (rc) { |
| 3165 | dev_err(&pdev->dev, "%s alg registration failed\n", |
| 3166 | q_alg->sha_alg.halg.base.cra_driver_name); |
| 3167 | kfree(q_alg); |
| 3168 | } else { |
| 3169 | list_add_tail(&q_alg->entry, &cp->alg_list); |
| 3170 | dev_info(&pdev->dev, "%s\n", |
| 3171 | q_alg->sha_alg.halg.base.cra_driver_name); |
| 3172 | } |
| 3173 | } |
| 3174 | |
| 3175 | /* register crypto aead (hmac-sha1) algorithms the device supports */ |
| 3176 | if (cp->ce_support.sha1_hmac_20 || cp->ce_support.sha1_hmac) { |
| 3177 | for (i = 0; i < ARRAY_SIZE(_qcrypto_aead_sha1_hmac_algos); |
| 3178 | i++) { |
| 3179 | struct qcrypto_alg *q_alg; |
| 3180 | |
| 3181 | q_alg = _qcrypto_cipher_alg_alloc(cp, |
| 3182 | &_qcrypto_aead_sha1_hmac_algos[i]); |
| 3183 | if (IS_ERR(q_alg)) { |
| 3184 | rc = PTR_ERR(q_alg); |
| 3185 | goto err; |
| 3186 | } |
| 3187 | |
| 3188 | rc = crypto_register_alg(&q_alg->cipher_alg); |
| 3189 | if (rc) { |
| 3190 | dev_err(&pdev->dev, |
| 3191 | "%s alg registration failed\n", |
| 3192 | q_alg->cipher_alg.cra_driver_name); |
| 3193 | kfree(q_alg); |
| 3194 | } else { |
| 3195 | list_add_tail(&q_alg->entry, &cp->alg_list); |
| 3196 | dev_info(&pdev->dev, "%s\n", |
| 3197 | q_alg->cipher_alg.cra_driver_name); |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | if ((cp->ce_support.sha_hmac) || (cp->platform_support.sha_hmac)) { |
| 3203 | /* register crypto hmac algorithms the device supports */ |
| 3204 | for (i = 0; i < ARRAY_SIZE(_qcrypto_sha_hmac_algos); i++) { |
| 3205 | struct qcrypto_alg *q_alg = NULL; |
| 3206 | |
| 3207 | q_alg = _qcrypto_sha_alg_alloc(cp, |
| 3208 | &_qcrypto_sha_hmac_algos[i]); |
| 3209 | |
| 3210 | if (IS_ERR(q_alg)) { |
| 3211 | rc = PTR_ERR(q_alg); |
| 3212 | goto err; |
| 3213 | } |
| 3214 | |
| 3215 | rc = crypto_register_ahash(&q_alg->sha_alg); |
| 3216 | if (rc) { |
| 3217 | dev_err(&pdev->dev, |
| 3218 | "%s alg registration failed\n", |
| 3219 | q_alg->sha_alg.halg.base.cra_driver_name); |
| 3220 | kfree(q_alg); |
| 3221 | } else { |
| 3222 | list_add_tail(&q_alg->entry, &cp->alg_list); |
| 3223 | dev_info(&pdev->dev, "%s\n", |
| 3224 | q_alg->sha_alg.halg.base.cra_driver_name); |
| 3225 | } |
| 3226 | } |
| 3227 | } |
| 3228 | /* |
| 3229 | * Register crypto cipher (aes-ccm) algorithms the |
| 3230 | * device supports |
| 3231 | */ |
| 3232 | if (cp->ce_support.aes_ccm) { |
| 3233 | struct qcrypto_alg *q_alg; |
| 3234 | |
| 3235 | q_alg = _qcrypto_cipher_alg_alloc(cp, &_qcrypto_aead_ccm_algo); |
| 3236 | if (IS_ERR(q_alg)) { |
| 3237 | rc = PTR_ERR(q_alg); |
| 3238 | goto err; |
| 3239 | } |
| 3240 | rc = crypto_register_alg(&q_alg->cipher_alg); |
| 3241 | if (rc) { |
| 3242 | dev_err(&pdev->dev, "%s alg registration failed\n", |
| 3243 | q_alg->cipher_alg.cra_driver_name); |
| 3244 | kfree(q_alg); |
| 3245 | } else { |
| 3246 | list_add_tail(&q_alg->entry, &cp->alg_list); |
| 3247 | dev_info(&pdev->dev, "%s\n", |
| 3248 | q_alg->cipher_alg.cra_driver_name); |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | return 0; |
| 3253 | err: |
| 3254 | _qcrypto_remove(pdev); |
| 3255 | return rc; |
| 3256 | }; |
| 3257 | |
| 3258 | static struct platform_driver _qualcomm_crypto = { |
| 3259 | .probe = _qcrypto_probe, |
| 3260 | .remove = _qcrypto_remove, |
| 3261 | .driver = { |
| 3262 | .owner = THIS_MODULE, |
| 3263 | .name = "qcrypto", |
| 3264 | }, |
| 3265 | }; |
| 3266 | |
| 3267 | static int _debug_qcrypto[MAX_CRYPTO_DEVICE]; |
| 3268 | |
| 3269 | static int _debug_stats_open(struct inode *inode, struct file *file) |
| 3270 | { |
| 3271 | file->private_data = inode->i_private; |
| 3272 | return 0; |
| 3273 | } |
| 3274 | |
| 3275 | static ssize_t _debug_stats_read(struct file *file, char __user *buf, |
| 3276 | size_t count, loff_t *ppos) |
| 3277 | { |
| 3278 | int rc = -EINVAL; |
| 3279 | int qcrypto = *((int *) file->private_data); |
| 3280 | int len; |
| 3281 | |
| 3282 | len = _disp_stats(qcrypto); |
| 3283 | |
| 3284 | rc = simple_read_from_buffer((void __user *) buf, len, |
| 3285 | ppos, (void *) _debug_read_buf, len); |
| 3286 | |
| 3287 | return rc; |
| 3288 | } |
| 3289 | |
| 3290 | static ssize_t _debug_stats_write(struct file *file, const char __user *buf, |
| 3291 | size_t count, loff_t *ppos) |
| 3292 | { |
| 3293 | |
| 3294 | int qcrypto = *((int *) file->private_data); |
| 3295 | |
| 3296 | memset((char *)&_qcrypto_stat[qcrypto], 0, sizeof(struct crypto_stat)); |
| 3297 | return count; |
| 3298 | }; |
| 3299 | |
| 3300 | static const struct file_operations _debug_stats_ops = { |
| 3301 | .open = _debug_stats_open, |
| 3302 | .read = _debug_stats_read, |
| 3303 | .write = _debug_stats_write, |
| 3304 | }; |
| 3305 | |
| 3306 | static int _qcrypto_debug_init(void) |
| 3307 | { |
| 3308 | int rc; |
| 3309 | char name[DEBUG_MAX_FNAME]; |
| 3310 | int i; |
| 3311 | struct dentry *dent; |
| 3312 | |
| 3313 | _debug_dent = debugfs_create_dir("qcrypto", NULL); |
| 3314 | if (IS_ERR(_debug_dent)) { |
| 3315 | pr_err("qcrypto debugfs_create_dir fail, error %ld\n", |
| 3316 | PTR_ERR(_debug_dent)); |
| 3317 | return PTR_ERR(_debug_dent); |
| 3318 | } |
| 3319 | |
| 3320 | for (i = 0; i < MAX_CRYPTO_DEVICE; i++) { |
| 3321 | snprintf(name, DEBUG_MAX_FNAME-1, "stats-%d", i+1); |
| 3322 | _debug_qcrypto[i] = i; |
| 3323 | dent = debugfs_create_file(name, 0644, _debug_dent, |
| 3324 | &_debug_qcrypto[i], &_debug_stats_ops); |
| 3325 | if (dent == NULL) { |
| 3326 | pr_err("qcrypto debugfs_create_file fail, error %ld\n", |
| 3327 | PTR_ERR(dent)); |
| 3328 | rc = PTR_ERR(dent); |
| 3329 | goto err; |
| 3330 | } |
| 3331 | } |
| 3332 | return 0; |
| 3333 | err: |
| 3334 | debugfs_remove_recursive(_debug_dent); |
| 3335 | return rc; |
| 3336 | } |
| 3337 | |
| 3338 | static int __init _qcrypto_init(void) |
| 3339 | { |
| 3340 | int rc; |
| 3341 | |
| 3342 | rc = _qcrypto_debug_init(); |
| 3343 | if (rc) |
| 3344 | return rc; |
| 3345 | |
| 3346 | return platform_driver_register(&_qualcomm_crypto); |
| 3347 | } |
| 3348 | |
| 3349 | static void __exit _qcrypto_exit(void) |
| 3350 | { |
Ramesh Masavarapu | c1d2b68 | 2011-09-07 14:57:58 -0700 | [diff] [blame] | 3351 | pr_debug("%s Unregister QCRYPTO\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3352 | debugfs_remove_recursive(_debug_dent); |
| 3353 | platform_driver_unregister(&_qualcomm_crypto); |
| 3354 | } |
| 3355 | |
| 3356 | module_init(_qcrypto_init); |
| 3357 | module_exit(_qcrypto_exit); |
| 3358 | |
| 3359 | MODULE_LICENSE("GPL v2"); |
| 3360 | MODULE_AUTHOR("Mona Hossain <mhossain@codeaurora.org>"); |
| 3361 | MODULE_DESCRIPTION("Qualcomm Crypto driver"); |
Ramesh Masavarapu | 4925968 | 2011-12-02 14:00:18 -0800 | [diff] [blame] | 3362 | MODULE_VERSION("1.20"); |