Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 1 | /* |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 2 | * zcrypt 2.1.0 |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 3 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2001, 2006 |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 5 | * Author(s): Robert Burroughs |
| 6 | * Eric Rossman (edrossma@us.ibm.com) |
| 7 | * |
| 8 | * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com) |
| 9 | * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 10 | * Ralph Wuerthner <rwuerthn@de.ibm.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2, or (at your option) |
| 15 | * any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 29 | #include <linux/init.h> |
| 30 | #include <linux/err.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 31 | #include <linux/atomic.h> |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 32 | #include <asm/uaccess.h> |
| 33 | |
| 34 | #include "ap_bus.h" |
| 35 | #include "zcrypt_api.h" |
| 36 | #include "zcrypt_error.h" |
| 37 | #include "zcrypt_cex2a.h" |
| 38 | |
| 39 | #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */ |
| 40 | #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */ |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 41 | #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 42 | #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */ |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 43 | |
| 44 | #define CEX2A_SPEED_RATING 970 |
Felix Beck | 7a6f5cd | 2009-12-07 12:51:59 +0100 | [diff] [blame] | 45 | #define CEX3A_SPEED_RATING 900 /* Fixme: Needs finetuning */ |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 46 | |
| 47 | #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */ |
| 48 | #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */ |
| 49 | |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 50 | #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus |
| 51 | * (max outputdatalength) + |
| 52 | * type80_hdr*/ |
| 53 | #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg) |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 54 | |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 55 | #define CEX2A_CLEANUP_TIME (15*HZ) |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 56 | #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 57 | |
| 58 | static struct ap_device_id zcrypt_cex2a_ids[] = { |
| 59 | { AP_DEVICE(AP_DEVICE_TYPE_CEX2A) }, |
Felix Beck | ffda4f7 | 2009-12-07 12:51:56 +0100 | [diff] [blame] | 60 | { AP_DEVICE(AP_DEVICE_TYPE_CEX3A) }, |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 61 | { /* end of list */ }, |
| 62 | }; |
| 63 | |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 64 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids); |
| 65 | MODULE_AUTHOR("IBM Corporation"); |
| 66 | MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 67 | "Copyright IBM Corp. 2001, 2006"); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 68 | MODULE_LICENSE("GPL"); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 69 | |
| 70 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev); |
| 71 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev); |
| 72 | static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *, |
| 73 | struct ap_message *); |
| 74 | |
| 75 | static struct ap_driver zcrypt_cex2a_driver = { |
| 76 | .probe = zcrypt_cex2a_probe, |
| 77 | .remove = zcrypt_cex2a_remove, |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 78 | .ids = zcrypt_cex2a_ids, |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 79 | .request_timeout = CEX2A_CLEANUP_TIME, |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Convert a ICAMEX message to a type50 MEX message. |
| 84 | * |
| 85 | * @zdev: crypto device pointer |
| 86 | * @zreq: crypto request pointer |
| 87 | * @mex: pointer to user input data |
| 88 | * |
| 89 | * Returns 0 on success or -EFAULT. |
| 90 | */ |
| 91 | static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev, |
| 92 | struct ap_message *ap_msg, |
| 93 | struct ica_rsa_modexpo *mex) |
| 94 | { |
| 95 | unsigned char *mod, *exp, *inp; |
| 96 | int mod_len; |
| 97 | |
| 98 | mod_len = mex->inputdatalength; |
| 99 | |
| 100 | if (mod_len <= 128) { |
| 101 | struct type50_meb1_msg *meb1 = ap_msg->message; |
| 102 | memset(meb1, 0, sizeof(*meb1)); |
| 103 | ap_msg->length = sizeof(*meb1); |
| 104 | meb1->header.msg_type_code = TYPE50_TYPE_CODE; |
| 105 | meb1->header.msg_len = sizeof(*meb1); |
| 106 | meb1->keyblock_type = TYPE50_MEB1_FMT; |
| 107 | mod = meb1->modulus + sizeof(meb1->modulus) - mod_len; |
| 108 | exp = meb1->exponent + sizeof(meb1->exponent) - mod_len; |
| 109 | inp = meb1->message + sizeof(meb1->message) - mod_len; |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 110 | } else if (mod_len <= 256) { |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 111 | struct type50_meb2_msg *meb2 = ap_msg->message; |
| 112 | memset(meb2, 0, sizeof(*meb2)); |
| 113 | ap_msg->length = sizeof(*meb2); |
| 114 | meb2->header.msg_type_code = TYPE50_TYPE_CODE; |
| 115 | meb2->header.msg_len = sizeof(*meb2); |
| 116 | meb2->keyblock_type = TYPE50_MEB2_FMT; |
| 117 | mod = meb2->modulus + sizeof(meb2->modulus) - mod_len; |
| 118 | exp = meb2->exponent + sizeof(meb2->exponent) - mod_len; |
| 119 | inp = meb2->message + sizeof(meb2->message) - mod_len; |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 120 | } else { |
| 121 | /* mod_len > 256 = 4096 bit RSA Key */ |
| 122 | struct type50_meb3_msg *meb3 = ap_msg->message; |
| 123 | memset(meb3, 0, sizeof(*meb3)); |
| 124 | ap_msg->length = sizeof(*meb3); |
| 125 | meb3->header.msg_type_code = TYPE50_TYPE_CODE; |
| 126 | meb3->header.msg_len = sizeof(*meb3); |
| 127 | meb3->keyblock_type = TYPE50_MEB3_FMT; |
| 128 | mod = meb3->modulus + sizeof(meb3->modulus) - mod_len; |
| 129 | exp = meb3->exponent + sizeof(meb3->exponent) - mod_len; |
| 130 | inp = meb3->message + sizeof(meb3->message) - mod_len; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (copy_from_user(mod, mex->n_modulus, mod_len) || |
| 134 | copy_from_user(exp, mex->b_key, mod_len) || |
| 135 | copy_from_user(inp, mex->inputdata, mod_len)) |
| 136 | return -EFAULT; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Convert a ICACRT message to a type50 CRT message. |
| 142 | * |
| 143 | * @zdev: crypto device pointer |
| 144 | * @zreq: crypto request pointer |
| 145 | * @crt: pointer to user input data |
| 146 | * |
| 147 | * Returns 0 on success or -EFAULT. |
| 148 | */ |
| 149 | static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev, |
| 150 | struct ap_message *ap_msg, |
| 151 | struct ica_rsa_modexpo_crt *crt) |
| 152 | { |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 153 | int mod_len, short_len, long_len, long_offset, limit; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 154 | unsigned char *p, *q, *dp, *dq, *u, *inp; |
| 155 | |
| 156 | mod_len = crt->inputdatalength; |
| 157 | short_len = mod_len / 2; |
| 158 | long_len = mod_len / 2 + 8; |
| 159 | |
| 160 | /* |
| 161 | * CEX2A cannot handle p, dp, or U > 128 bytes. |
| 162 | * If we have one of these, we need to do extra checking. |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 163 | * For CEX3A the limit is 256 bytes. |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 164 | */ |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 165 | if (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE) |
| 166 | limit = 256; |
| 167 | else |
| 168 | limit = 128; |
| 169 | |
| 170 | if (long_len > limit) { |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 171 | /* |
| 172 | * zcrypt_rsa_crt already checked for the leading |
| 173 | * zeroes of np_prime, bp_key and u_mult_inc. |
| 174 | */ |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 175 | long_offset = long_len - limit; |
| 176 | long_len = limit; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 177 | } else |
| 178 | long_offset = 0; |
| 179 | |
| 180 | /* |
| 181 | * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use |
| 182 | * the larger message structure. |
| 183 | */ |
| 184 | if (long_len <= 64) { |
| 185 | struct type50_crb1_msg *crb1 = ap_msg->message; |
| 186 | memset(crb1, 0, sizeof(*crb1)); |
| 187 | ap_msg->length = sizeof(*crb1); |
| 188 | crb1->header.msg_type_code = TYPE50_TYPE_CODE; |
| 189 | crb1->header.msg_len = sizeof(*crb1); |
| 190 | crb1->keyblock_type = TYPE50_CRB1_FMT; |
| 191 | p = crb1->p + sizeof(crb1->p) - long_len; |
| 192 | q = crb1->q + sizeof(crb1->q) - short_len; |
| 193 | dp = crb1->dp + sizeof(crb1->dp) - long_len; |
| 194 | dq = crb1->dq + sizeof(crb1->dq) - short_len; |
| 195 | u = crb1->u + sizeof(crb1->u) - long_len; |
| 196 | inp = crb1->message + sizeof(crb1->message) - mod_len; |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 197 | } else if (long_len <= 128) { |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 198 | struct type50_crb2_msg *crb2 = ap_msg->message; |
| 199 | memset(crb2, 0, sizeof(*crb2)); |
| 200 | ap_msg->length = sizeof(*crb2); |
| 201 | crb2->header.msg_type_code = TYPE50_TYPE_CODE; |
| 202 | crb2->header.msg_len = sizeof(*crb2); |
| 203 | crb2->keyblock_type = TYPE50_CRB2_FMT; |
| 204 | p = crb2->p + sizeof(crb2->p) - long_len; |
| 205 | q = crb2->q + sizeof(crb2->q) - short_len; |
| 206 | dp = crb2->dp + sizeof(crb2->dp) - long_len; |
| 207 | dq = crb2->dq + sizeof(crb2->dq) - short_len; |
| 208 | u = crb2->u + sizeof(crb2->u) - long_len; |
| 209 | inp = crb2->message + sizeof(crb2->message) - mod_len; |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 210 | } else { |
| 211 | /* long_len >= 256 */ |
| 212 | struct type50_crb3_msg *crb3 = ap_msg->message; |
| 213 | memset(crb3, 0, sizeof(*crb3)); |
| 214 | ap_msg->length = sizeof(*crb3); |
| 215 | crb3->header.msg_type_code = TYPE50_TYPE_CODE; |
| 216 | crb3->header.msg_len = sizeof(*crb3); |
| 217 | crb3->keyblock_type = TYPE50_CRB3_FMT; |
| 218 | p = crb3->p + sizeof(crb3->p) - long_len; |
| 219 | q = crb3->q + sizeof(crb3->q) - short_len; |
| 220 | dp = crb3->dp + sizeof(crb3->dp) - long_len; |
| 221 | dq = crb3->dq + sizeof(crb3->dq) - short_len; |
| 222 | u = crb3->u + sizeof(crb3->u) - long_len; |
| 223 | inp = crb3->message + sizeof(crb3->message) - mod_len; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (copy_from_user(p, crt->np_prime + long_offset, long_len) || |
| 227 | copy_from_user(q, crt->nq_prime, short_len) || |
| 228 | copy_from_user(dp, crt->bp_key + long_offset, long_len) || |
| 229 | copy_from_user(dq, crt->bq_key, short_len) || |
| 230 | copy_from_user(u, crt->u_mult_inv + long_offset, long_len) || |
| 231 | copy_from_user(inp, crt->inputdata, mod_len)) |
| 232 | return -EFAULT; |
| 233 | |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Copy results from a type 80 reply message back to user space. |
| 239 | * |
| 240 | * @zdev: crypto device pointer |
| 241 | * @reply: reply AP message. |
| 242 | * @data: pointer to user output data |
| 243 | * @length: size of user output data |
| 244 | * |
| 245 | * Returns 0 on success or -EFAULT. |
| 246 | */ |
| 247 | static int convert_type80(struct zcrypt_device *zdev, |
| 248 | struct ap_message *reply, |
| 249 | char __user *outputdata, |
| 250 | unsigned int outputdatalength) |
| 251 | { |
| 252 | struct type80_hdr *t80h = reply->message; |
| 253 | unsigned char *data; |
| 254 | |
| 255 | if (t80h->len < sizeof(*t80h) + outputdatalength) { |
| 256 | /* The result is too short, the CEX2A card may not do that.. */ |
| 257 | zdev->online = 0; |
| 258 | return -EAGAIN; /* repeat the request on a different device. */ |
| 259 | } |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 260 | if (zdev->user_space_type == ZCRYPT_CEX2A) |
| 261 | BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE); |
| 262 | else |
| 263 | BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 264 | data = reply->message + t80h->len - outputdatalength; |
| 265 | if (copy_to_user(outputdata, data, outputdatalength)) |
| 266 | return -EFAULT; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int convert_response(struct zcrypt_device *zdev, |
| 271 | struct ap_message *reply, |
| 272 | char __user *outputdata, |
| 273 | unsigned int outputdatalength) |
| 274 | { |
| 275 | /* Response type byte is the second byte in the response. */ |
| 276 | switch (((unsigned char *) reply->message)[1]) { |
| 277 | case TYPE82_RSP_CODE: |
| 278 | case TYPE88_RSP_CODE: |
| 279 | return convert_error(zdev, reply); |
| 280 | case TYPE80_RSP_CODE: |
| 281 | return convert_type80(zdev, reply, |
| 282 | outputdata, outputdatalength); |
| 283 | default: /* Unknown response type, this should NEVER EVER happen */ |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 284 | zdev->online = 0; |
| 285 | return -EAGAIN; /* repeat the request on a different device. */ |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * This function is called from the AP bus code after a crypto request |
| 291 | * "msg" has finished with the reply message "reply". |
| 292 | * It is called from tasklet context. |
| 293 | * @ap_dev: pointer to the AP device |
| 294 | * @msg: pointer to the AP message |
| 295 | * @reply: pointer to the AP reply message |
| 296 | */ |
| 297 | static void zcrypt_cex2a_receive(struct ap_device *ap_dev, |
| 298 | struct ap_message *msg, |
| 299 | struct ap_message *reply) |
| 300 | { |
| 301 | static struct error_hdr error_reply = { |
| 302 | .type = TYPE82_RSP_CODE, |
| 303 | .reply_code = REP82_ERROR_MACHINE_FAILURE, |
| 304 | }; |
Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 305 | struct type80_hdr *t80h; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 306 | int length; |
| 307 | |
| 308 | /* Copy the reply message to the request message buffer. */ |
Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 309 | if (IS_ERR(reply)) { |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 310 | memcpy(msg->message, &error_reply, sizeof(error_reply)); |
Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 311 | goto out; |
| 312 | } |
| 313 | t80h = reply->message; |
| 314 | if (t80h->type == TYPE80_RSP_CODE) { |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 315 | if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A) |
| 316 | length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len); |
| 317 | else |
| 318 | length = min(CEX3A_MAX_RESPONSE_SIZE, (int) t80h->len); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 319 | memcpy(msg->message, reply->message, length); |
| 320 | } else |
| 321 | memcpy(msg->message, reply->message, sizeof error_reply); |
Julia Lawall | 21e7b2c | 2008-12-25 13:39:28 +0100 | [diff] [blame] | 322 | out: |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 323 | complete((struct completion *) msg->private); |
| 324 | } |
| 325 | |
| 326 | static atomic_t zcrypt_step = ATOMIC_INIT(0); |
| 327 | |
| 328 | /** |
| 329 | * The request distributor calls this function if it picked the CEX2A |
| 330 | * device to handle a modexpo request. |
| 331 | * @zdev: pointer to zcrypt_device structure that identifies the |
| 332 | * CEX2A device to the request distributor |
| 333 | * @mex: pointer to the modexpo request buffer |
| 334 | */ |
| 335 | static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev, |
| 336 | struct ica_rsa_modexpo *mex) |
| 337 | { |
| 338 | struct ap_message ap_msg; |
| 339 | struct completion work; |
| 340 | int rc; |
| 341 | |
Felix Beck | 468ffdd | 2009-12-07 12:51:54 +0100 | [diff] [blame] | 342 | ap_init_message(&ap_msg); |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 343 | if (zdev->user_space_type == ZCRYPT_CEX2A) |
| 344 | ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL); |
| 345 | else |
| 346 | ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 347 | if (!ap_msg.message) |
| 348 | return -ENOMEM; |
Holger Dengler | 54a8f56 | 2012-05-16 14:08:22 +0200 | [diff] [blame] | 349 | ap_msg.receive = zcrypt_cex2a_receive; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 350 | ap_msg.psmid = (((unsigned long long) current->pid) << 32) + |
| 351 | atomic_inc_return(&zcrypt_step); |
| 352 | ap_msg.private = &work; |
| 353 | rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex); |
| 354 | if (rc) |
| 355 | goto out_free; |
| 356 | init_completion(&work); |
| 357 | ap_queue_message(zdev->ap_dev, &ap_msg); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 358 | rc = wait_for_completion_interruptible(&work); |
| 359 | if (rc == 0) |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 360 | rc = convert_response(zdev, &ap_msg, mex->outputdata, |
| 361 | mex->outputdatalength); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 362 | else |
| 363 | /* Signal pending. */ |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 364 | ap_cancel_message(zdev->ap_dev, &ap_msg); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 365 | out_free: |
| 366 | kfree(ap_msg.message); |
| 367 | return rc; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * The request distributor calls this function if it picked the CEX2A |
| 372 | * device to handle a modexpo_crt request. |
| 373 | * @zdev: pointer to zcrypt_device structure that identifies the |
| 374 | * CEX2A device to the request distributor |
| 375 | * @crt: pointer to the modexpoc_crt request buffer |
| 376 | */ |
| 377 | static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev, |
| 378 | struct ica_rsa_modexpo_crt *crt) |
| 379 | { |
| 380 | struct ap_message ap_msg; |
| 381 | struct completion work; |
| 382 | int rc; |
| 383 | |
Felix Beck | 468ffdd | 2009-12-07 12:51:54 +0100 | [diff] [blame] | 384 | ap_init_message(&ap_msg); |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 385 | if (zdev->user_space_type == ZCRYPT_CEX2A) |
| 386 | ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL); |
| 387 | else |
| 388 | ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 389 | if (!ap_msg.message) |
| 390 | return -ENOMEM; |
Holger Dengler | 54a8f56 | 2012-05-16 14:08:22 +0200 | [diff] [blame] | 391 | ap_msg.receive = zcrypt_cex2a_receive; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 392 | ap_msg.psmid = (((unsigned long long) current->pid) << 32) + |
| 393 | atomic_inc_return(&zcrypt_step); |
| 394 | ap_msg.private = &work; |
| 395 | rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt); |
| 396 | if (rc) |
| 397 | goto out_free; |
| 398 | init_completion(&work); |
| 399 | ap_queue_message(zdev->ap_dev, &ap_msg); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 400 | rc = wait_for_completion_interruptible(&work); |
| 401 | if (rc == 0) |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 402 | rc = convert_response(zdev, &ap_msg, crt->outputdata, |
| 403 | crt->outputdatalength); |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 404 | else |
| 405 | /* Signal pending. */ |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 406 | ap_cancel_message(zdev->ap_dev, &ap_msg); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 407 | out_free: |
| 408 | kfree(ap_msg.message); |
| 409 | return rc; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * The crypto operations for a CEX2A card. |
| 414 | */ |
| 415 | static struct zcrypt_ops zcrypt_cex2a_ops = { |
| 416 | .rsa_modexpo = zcrypt_cex2a_modexpo, |
| 417 | .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt, |
| 418 | }; |
| 419 | |
| 420 | /** |
| 421 | * Probe function for CEX2A cards. It always accepts the AP device |
| 422 | * since the bus_match already checked the hardware type. |
| 423 | * @ap_dev: pointer to the AP device. |
| 424 | */ |
| 425 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev) |
| 426 | { |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 427 | struct zcrypt_device *zdev = NULL; |
| 428 | int rc = 0; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 429 | |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 430 | switch (ap_dev->device_type) { |
| 431 | case AP_DEVICE_TYPE_CEX2A: |
| 432 | zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE); |
| 433 | if (!zdev) |
| 434 | return -ENOMEM; |
| 435 | zdev->user_space_type = ZCRYPT_CEX2A; |
| 436 | zdev->type_string = "CEX2A"; |
| 437 | zdev->min_mod_size = CEX2A_MIN_MOD_SIZE; |
| 438 | zdev->max_mod_size = CEX2A_MAX_MOD_SIZE; |
| 439 | zdev->short_crt = 1; |
| 440 | zdev->speed_rating = CEX2A_SPEED_RATING; |
Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 441 | zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 442 | break; |
| 443 | case AP_DEVICE_TYPE_CEX3A: |
| 444 | zdev = zcrypt_device_alloc(CEX3A_MAX_RESPONSE_SIZE); |
| 445 | if (!zdev) |
| 446 | return -ENOMEM; |
| 447 | zdev->user_space_type = ZCRYPT_CEX3A; |
| 448 | zdev->type_string = "CEX3A"; |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 449 | zdev->min_mod_size = CEX2A_MIN_MOD_SIZE; |
| 450 | zdev->max_mod_size = CEX2A_MAX_MOD_SIZE; |
Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 451 | zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; |
Holger Dengler | b26bd94 | 2012-08-28 16:43:48 +0200 | [diff] [blame^] | 452 | if (ap_test_bit(&ap_dev->functions, AP_FUNC_MEX4K) && |
| 453 | ap_test_bit(&ap_dev->functions, AP_FUNC_CRT4K)) { |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 454 | zdev->max_mod_size = CEX3A_MAX_MOD_SIZE; |
Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 455 | zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE; |
| 456 | } |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 457 | zdev->short_crt = 1; |
| 458 | zdev->speed_rating = CEX3A_SPEED_RATING; |
| 459 | break; |
| 460 | } |
| 461 | if (zdev != NULL) { |
| 462 | zdev->ap_dev = ap_dev; |
| 463 | zdev->ops = &zcrypt_cex2a_ops; |
| 464 | zdev->online = 1; |
| 465 | ap_dev->reply = &zdev->reply; |
| 466 | ap_dev->private = zdev; |
| 467 | rc = zcrypt_device_register(zdev); |
| 468 | } |
| 469 | if (rc) { |
| 470 | ap_dev->private = NULL; |
| 471 | zcrypt_device_free(zdev); |
| 472 | } |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 473 | return rc; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * This is called to remove the extended CEX2A driver information |
| 478 | * if an AP device is removed. |
| 479 | */ |
| 480 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev) |
| 481 | { |
| 482 | struct zcrypt_device *zdev = ap_dev->private; |
| 483 | |
| 484 | zcrypt_device_unregister(zdev); |
| 485 | } |
| 486 | |
| 487 | int __init zcrypt_cex2a_init(void) |
| 488 | { |
| 489 | return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a"); |
| 490 | } |
| 491 | |
| 492 | void __exit zcrypt_cex2a_exit(void) |
| 493 | { |
| 494 | ap_driver_unregister(&zcrypt_cex2a_driver); |
| 495 | } |
| 496 | |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 497 | module_init(zcrypt_cex2a_init); |
| 498 | module_exit(zcrypt_cex2a_exit); |