1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright IBM Corp. 2001, 2012
6 * Author(s): Robert Burroughs
15 #define KMSG_COMPONENT "zcrypt"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/err.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/uaccess.h>
27 #include "zcrypt_api.h"
28 #include "zcrypt_error.h"
29 #include "zcrypt_msgtype6.h"
30 #include "zcrypt_cca_key.h"
32 #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
33 #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
35 #define CEIL4(x) ((((x)+3)/4)*4)
37 struct response_type {
38 struct completion work;
41 #define PCIXCC_RESPONSE_TYPE_ICA 0
42 #define PCIXCC_RESPONSE_TYPE_XCRB 1
43 #define PCIXCC_RESPONSE_TYPE_EP11 2
45 MODULE_AUTHOR("IBM Corporation");
46 MODULE_DESCRIPTION("Cryptographic Coprocessor (message type 6), " \
47 "Copyright IBM Corp. 2001, 2012");
48 MODULE_LICENSE("GPL");
52 * Note that all shorts, ints and longs are little-endian.
53 * All pointer fields are 32-bits long, and mean nothing
55 * A request CPRB is followed by a request_parameter_block.
57 * The request (or reply) parameter block is organized thus:
63 unsigned short cprb_len; /* CPRB length */
64 unsigned char cprb_ver_id; /* CPRB version id. */
65 unsigned char pad_000; /* Alignment pad byte. */
66 unsigned char srpi_rtcode[4]; /* SRPI return code LELONG */
67 unsigned char srpi_verb; /* SRPI verb type */
68 unsigned char flags; /* flags */
69 unsigned char func_id[2]; /* function id */
70 unsigned char checkpoint_flag; /* */
71 unsigned char resv2; /* reserved */
72 unsigned short req_parml; /* request parameter buffer */
73 /* length 16-bit little endian */
74 unsigned char req_parmp[4]; /* request parameter buffer *
75 * pointer (means nothing: the *
76 * parameter buffer follows *
78 unsigned char req_datal[4]; /* request data buffer */
80 unsigned char req_datap[4]; /* request data buffer */
82 unsigned short rpl_parml; /* reply parameter buffer */
83 /* length 16-bit little endian */
84 unsigned char pad_001[2]; /* Alignment pad bytes. ULESHORT */
85 unsigned char rpl_parmp[4]; /* reply parameter buffer *
86 * pointer (means nothing: the *
87 * parameter buffer follows *
89 unsigned char rpl_datal[4]; /* reply data buffer len ULELONG */
90 unsigned char rpl_datap[4]; /* reply data buffer */
92 unsigned short ccp_rscode; /* server reason code ULESHORT */
93 unsigned short ccp_rtcode; /* server return code ULESHORT */
94 unsigned char repd_parml[2]; /* replied parameter len ULESHORT*/
95 unsigned char mac_data_len[2]; /* Mac Data Length ULESHORT */
96 unsigned char repd_datal[4]; /* replied data length ULELONG */
97 unsigned char req_pc[2]; /* PC identifier */
98 unsigned char res_origin[8]; /* resource origin */
99 unsigned char mac_value[8]; /* Mac Value */
100 unsigned char logon_id[8]; /* Logon Identifier */
101 unsigned char usage_domain[2]; /* cdx */
102 unsigned char resv3[18]; /* reserved for requestor */
103 unsigned short svr_namel; /* server name length ULESHORT */
104 unsigned char svr_name[8]; /* server name */
107 struct function_and_rules_block {
108 unsigned char function_code[2];
110 unsigned char only_rule[8];
114 * The following is used to initialize the CPRBX passed to the PCIXCC/CEX2C
115 * card in a type6 message. The 3 fields that must be filled in at execution
116 * time are req_parml, rpl_parml and usage_domain.
117 * Everything about this interface is ascii/big-endian, since the
118 * device does *not* have 'Intel inside'.
120 * The CPRBX is followed immediately by the parm block.
121 * The parm block contains:
122 * - function code ('PD' 0x5044 or 'PK' 0x504B)
123 * - rule block (one of:)
124 * + 0x000A 'PKCS-1.2' (MCL2 'PD')
125 * + 0x000A 'ZERO-PAD' (MCL2 'PK')
126 * + 0x000A 'ZERO-PAD' (MCL3 'PD' or CEX2C 'PD')
127 * + 0x000A 'MRP ' (MCL3 'PK' or CEX2C 'PK')
130 static const struct CPRBX static_cprbx = {
133 .func_id = {0x54, 0x32},
136 int speed_idx_cca(int req_type)
211 int speed_idx_ep11(int req_type)
243 * Convert a ICAMEX message to a type6 MEX message.
245 * @zq: crypto device pointer
246 * @ap_msg: pointer to AP message
247 * @mex: pointer to user input data
249 * Returns 0 on success or negative errno value.
251 static int ICAMEX_msg_to_type6MEX_msgX(struct zcrypt_queue *zq,
252 struct ap_message *ap_msg,
253 struct ica_rsa_modexpo *mex)
255 static struct type6_hdr static_type6_hdrX = {
257 .offset1 = 0x00000058,
258 .agent_id = {'C', 'A',},
259 .function_code = {'P', 'K'},
261 static struct function_and_rules_block static_pke_fnr = {
262 .function_code = {'P', 'K'},
264 .only_rule = {'M', 'R', 'P', ' ', ' ', ' ', ' ', ' '}
267 struct type6_hdr hdr;
269 struct function_and_rules_block fr;
270 unsigned short length;
272 } __packed * msg = ap_msg->message;
276 * The inputdatalength was a selection criteria in the dispatching
277 * function zcrypt_rsa_modexpo(). However, make sure the following
278 * copy_from_user() never exceeds the allocated buffer space.
280 if (WARN_ON_ONCE(mex->inputdatalength > PAGE_SIZE))
284 msg->length = mex->inputdatalength + 2;
285 if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
288 /* Set up key which is located after the variable length text. */
289 size = zcrypt_type6_mex_key_en(mex, msg->text+mex->inputdatalength);
292 size += sizeof(*msg) + mex->inputdatalength;
294 /* message header, cprbx and f&r */
295 msg->hdr = static_type6_hdrX;
296 msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
297 msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
299 msg->cprbx = static_cprbx;
300 msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
301 msg->cprbx.rpl_msgbl = msg->hdr.FromCardLen1;
303 msg->fr = static_pke_fnr;
305 msg->cprbx.req_parml = size - sizeof(msg->hdr) - sizeof(msg->cprbx);
307 ap_msg->length = size;
312 * Convert a ICACRT message to a type6 CRT message.
314 * @zq: crypto device pointer
315 * @ap_msg: pointer to AP message
316 * @crt: pointer to user input data
318 * Returns 0 on success or negative errno value.
320 static int ICACRT_msg_to_type6CRT_msgX(struct zcrypt_queue *zq,
321 struct ap_message *ap_msg,
322 struct ica_rsa_modexpo_crt *crt)
324 static struct type6_hdr static_type6_hdrX = {
326 .offset1 = 0x00000058,
327 .agent_id = {'C', 'A',},
328 .function_code = {'P', 'D'},
330 static struct function_and_rules_block static_pkd_fnr = {
331 .function_code = {'P', 'D'},
333 .only_rule = {'Z', 'E', 'R', 'O', '-', 'P', 'A', 'D'}
337 struct type6_hdr hdr;
339 struct function_and_rules_block fr;
340 unsigned short length;
342 } __packed * msg = ap_msg->message;
346 * The inputdatalength was a selection criteria in the dispatching
347 * function zcrypt_rsa_crt(). However, make sure the following
348 * copy_from_user() never exceeds the allocated buffer space.
350 if (WARN_ON_ONCE(crt->inputdatalength > PAGE_SIZE))
354 msg->length = crt->inputdatalength + 2;
355 if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
358 /* Set up key which is located after the variable length text. */
359 size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength);
362 size += sizeof(*msg) + crt->inputdatalength; /* total size of msg */
364 /* message header, cprbx and f&r */
365 msg->hdr = static_type6_hdrX;
366 msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
367 msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
369 msg->cprbx = static_cprbx;
370 msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
371 msg->cprbx.req_parml = msg->cprbx.rpl_msgbl =
372 size - sizeof(msg->hdr) - sizeof(msg->cprbx);
374 msg->fr = static_pkd_fnr;
376 ap_msg->length = size;
381 * Convert a XCRB message to a type6 CPRB message.
383 * @zq: crypto device pointer
384 * @ap_msg: pointer to AP message
385 * @xcRB: pointer to user input data
387 * Returns 0 on success or -EFAULT, -EINVAL.
389 struct type86_fmt2_msg {
390 struct type86_hdr hdr;
391 struct type86_fmt2_ext fmt2;
394 static int XCRB_msg_to_type6CPRB_msgX(struct ap_message *ap_msg,
395 struct ica_xcRB *xcRB,
397 unsigned short **dom)
399 static struct type6_hdr static_type6_hdrX = {
401 .offset1 = 0x00000058,
404 struct type6_hdr hdr;
406 } __packed * msg = ap_msg->message;
408 int rcblen = CEIL4(xcRB->request_control_blk_length);
409 int replylen, req_sumlen, resp_sumlen;
410 char *req_data = ap_msg->message + sizeof(struct type6_hdr) + rcblen;
413 if (CEIL4(xcRB->request_control_blk_length) <
414 xcRB->request_control_blk_length)
415 return -EINVAL; /* overflow after alignment*/
418 ap_msg->length = sizeof(struct type6_hdr) +
419 CEIL4(xcRB->request_control_blk_length) +
420 xcRB->request_data_length;
421 if (ap_msg->length > MSGTYPE06_MAX_MSG_SIZE)
426 * sum must be greater (or equal) than the largest operand
428 req_sumlen = CEIL4(xcRB->request_control_blk_length) +
429 xcRB->request_data_length;
430 if ((CEIL4(xcRB->request_control_blk_length) <=
431 xcRB->request_data_length) ?
432 (req_sumlen < xcRB->request_data_length) :
433 (req_sumlen < CEIL4(xcRB->request_control_blk_length))) {
437 if (CEIL4(xcRB->reply_control_blk_length) <
438 xcRB->reply_control_blk_length)
439 return -EINVAL; /* overflow after alignment*/
441 replylen = sizeof(struct type86_fmt2_msg) +
442 CEIL4(xcRB->reply_control_blk_length) +
443 xcRB->reply_data_length;
444 if (replylen > MSGTYPE06_MAX_MSG_SIZE)
449 * sum must be greater (or equal) than the largest operand
451 resp_sumlen = CEIL4(xcRB->reply_control_blk_length) +
452 xcRB->reply_data_length;
453 if ((CEIL4(xcRB->reply_control_blk_length) <= xcRB->reply_data_length) ?
454 (resp_sumlen < xcRB->reply_data_length) :
455 (resp_sumlen < CEIL4(xcRB->reply_control_blk_length))) {
459 /* prepare type6 header */
460 msg->hdr = static_type6_hdrX;
461 memcpy(msg->hdr.agent_id, &(xcRB->agent_ID), sizeof(xcRB->agent_ID));
462 msg->hdr.ToCardLen1 = xcRB->request_control_blk_length;
463 if (xcRB->request_data_length) {
464 msg->hdr.offset2 = msg->hdr.offset1 + rcblen;
465 msg->hdr.ToCardLen2 = xcRB->request_data_length;
467 msg->hdr.FromCardLen1 = xcRB->reply_control_blk_length;
468 msg->hdr.FromCardLen2 = xcRB->reply_data_length;
471 if (copy_from_user(&(msg->cprbx), xcRB->request_control_blk_addr,
472 xcRB->request_control_blk_length))
474 if (msg->cprbx.cprb_len + sizeof(msg->hdr.function_code) >
475 xcRB->request_control_blk_length)
477 function_code = ((unsigned char *)&msg->cprbx) + msg->cprbx.cprb_len;
478 memcpy(msg->hdr.function_code, function_code,
479 sizeof(msg->hdr.function_code));
481 *fcode = (msg->hdr.function_code[0] << 8) | msg->hdr.function_code[1];
482 *dom = (unsigned short *)&msg->cprbx.domain;
484 if (memcmp(function_code, "US", 2) == 0
485 || memcmp(function_code, "AU", 2) == 0)
490 /* copy data block */
491 if (xcRB->request_data_length &&
492 copy_from_user(req_data, xcRB->request_data_address,
493 xcRB->request_data_length))
499 static int xcrb_msg_to_type6_ep11cprb_msgx(struct ap_message *ap_msg,
500 struct ep11_urb *xcRB,
504 static struct type6_hdr static_type6_ep11_hdr = {
506 .rqid = {0x00, 0x01},
507 .function_code = {0x00, 0x00},
508 .agent_id[0] = 0x58, /* {'X'} */
509 .agent_id[1] = 0x43, /* {'C'} */
510 .offset1 = 0x00000058,
514 struct type6_hdr hdr;
515 struct ep11_cprb cprbx;
516 unsigned char pld_tag; /* fixed value 0x30 */
517 unsigned char pld_lenfmt; /* payload length format */
518 } __packed * msg = ap_msg->message;
521 unsigned char func_tag; /* fixed value 0x4 */
522 unsigned char func_len; /* fixed value 0x4 */
523 unsigned int func_val; /* function ID */
524 unsigned char dom_tag; /* fixed value 0x4 */
525 unsigned char dom_len; /* fixed value 0x4 */
526 unsigned int dom_val; /* domain id */
527 } __packed * payload_hdr = NULL;
529 if (CEIL4(xcRB->req_len) < xcRB->req_len)
530 return -EINVAL; /* overflow after alignment*/
533 ap_msg->length = sizeof(struct type6_hdr) + xcRB->req_len;
534 if (CEIL4(xcRB->req_len) > MSGTYPE06_MAX_MSG_SIZE -
535 (sizeof(struct type6_hdr)))
538 if (CEIL4(xcRB->resp_len) < xcRB->resp_len)
539 return -EINVAL; /* overflow after alignment*/
541 if (CEIL4(xcRB->resp_len) > MSGTYPE06_MAX_MSG_SIZE -
542 (sizeof(struct type86_fmt2_msg)))
545 /* prepare type6 header */
546 msg->hdr = static_type6_ep11_hdr;
547 msg->hdr.ToCardLen1 = xcRB->req_len;
548 msg->hdr.FromCardLen1 = xcRB->resp_len;
550 /* Import CPRB data from the ioctl input parameter */
551 if (copy_from_user(&(msg->cprbx.cprb_len),
552 (char __force __user *)xcRB->req, xcRB->req_len)) {
556 if ((msg->pld_lenfmt & 0x80) == 0x80) { /*ext.len.fmt 2 or 3*/
557 switch (msg->pld_lenfmt & 0x03) {
568 lfmt = 1; /* length format #1 */
570 payload_hdr = (struct pld_hdr *)((&(msg->pld_lenfmt))+lfmt);
571 *fcode = payload_hdr->func_val & 0xFFFF;
577 * Copy results from a type 86 ICA reply message back to user space.
579 * @zq: crypto device pointer
580 * @reply: reply AP message.
581 * @data: pointer to user output data
582 * @length: size of user output data
584 * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
586 struct type86x_reply {
587 struct type86_hdr hdr;
588 struct type86_fmt2_ext fmt2;
590 unsigned char pad[4]; /* 4 byte function code/rules block ? */
591 unsigned short length;
595 struct type86_ep11_reply {
596 struct type86_hdr hdr;
597 struct type86_fmt2_ext fmt2;
598 struct ep11_cprb cprbx;
601 static int convert_type86_ica(struct zcrypt_queue *zq,
602 struct ap_message *reply,
603 char __user *outputdata,
604 unsigned int outputdatalength)
606 static unsigned char static_pad[] = {
608 0x1B, 0x7B, 0x5D, 0xB5, 0x75, 0x01, 0x3D, 0xFD,
609 0x8D, 0xD1, 0xC7, 0x03, 0x2D, 0x09, 0x23, 0x57,
610 0x89, 0x49, 0xB9, 0x3F, 0xBB, 0x99, 0x41, 0x5B,
611 0x75, 0x21, 0x7B, 0x9D, 0x3B, 0x6B, 0x51, 0x39,
612 0xBB, 0x0D, 0x35, 0xB9, 0x89, 0x0F, 0x93, 0xA5,
613 0x0B, 0x47, 0xF1, 0xD3, 0xBB, 0xCB, 0xF1, 0x9D,
614 0x23, 0x73, 0x71, 0xFF, 0xF3, 0xF5, 0x45, 0xFB,
615 0x61, 0x29, 0x23, 0xFD, 0xF1, 0x29, 0x3F, 0x7F,
616 0x17, 0xB7, 0x1B, 0xA9, 0x19, 0xBD, 0x57, 0xA9,
617 0xD7, 0x95, 0xA3, 0xCB, 0xED, 0x1D, 0xDB, 0x45,
618 0x7D, 0x11, 0xD1, 0x51, 0x1B, 0xED, 0x71, 0xE9,
619 0xB1, 0xD1, 0xAB, 0xAB, 0x21, 0x2B, 0x1B, 0x9F,
620 0x3B, 0x9F, 0xF7, 0xF7, 0xBD, 0x63, 0xEB, 0xAD,
621 0xDF, 0xB3, 0x6F, 0x5B, 0xDB, 0x8D, 0xA9, 0x5D,
622 0xE3, 0x7D, 0x77, 0x49, 0x47, 0xF5, 0xA7, 0xFD,
623 0xAB, 0x2F, 0x27, 0x35, 0x77, 0xD3, 0x49, 0xC9,
624 0x09, 0xEB, 0xB1, 0xF9, 0xBF, 0x4B, 0xCB, 0x2B,
625 0xEB, 0xEB, 0x05, 0xFF, 0x7D, 0xC7, 0x91, 0x8B,
626 0x09, 0x83, 0xB9, 0xB9, 0x69, 0x33, 0x39, 0x6B,
627 0x79, 0x75, 0x19, 0xBF, 0xBB, 0x07, 0x1D, 0xBD,
628 0x29, 0xBF, 0x39, 0x95, 0x93, 0x1D, 0x35, 0xC7,
629 0xC9, 0x4D, 0xE5, 0x97, 0x0B, 0x43, 0x9B, 0xF1,
630 0x16, 0x93, 0x03, 0x1F, 0xA5, 0xFB, 0xDB, 0xF3,
631 0x27, 0x4F, 0x27, 0x61, 0x05, 0x1F, 0xB9, 0x23,
632 0x2F, 0xC3, 0x81, 0xA9, 0x23, 0x71, 0x55, 0x55,
633 0xEB, 0xED, 0x41, 0xE5, 0xF3, 0x11, 0xF1, 0x43,
634 0x69, 0x03, 0xBD, 0x0B, 0x37, 0x0F, 0x51, 0x8F,
635 0x0B, 0xB5, 0x89, 0x5B, 0x67, 0xA9, 0xD9, 0x4F,
636 0x01, 0xF9, 0x21, 0x77, 0x37, 0x73, 0x79, 0xC5,
637 0x7F, 0x51, 0xC1, 0xCF, 0x97, 0xA1, 0x75, 0xAD,
638 0x35, 0x9D, 0xD3, 0xD3, 0xA7, 0x9D, 0x5D, 0x41,
639 0x6F, 0x65, 0x1B, 0xCF, 0xA9, 0x87, 0x91, 0x09
641 struct type86x_reply *msg = reply->message;
642 unsigned short service_rc, service_rs;
643 unsigned int reply_len, pad_len;
646 service_rc = msg->cprbx.ccp_rtcode;
647 if (unlikely(service_rc != 0)) {
648 service_rs = msg->cprbx.ccp_rscode;
649 if ((service_rc == 8 && service_rs == 66) ||
650 (service_rc == 8 && service_rs == 65) ||
651 (service_rc == 8 && service_rs == 72) ||
652 (service_rc == 8 && service_rs == 770) ||
653 (service_rc == 12 && service_rs == 769)) {
654 ZCRYPT_DBF(DBF_DEBUG,
655 "device=%02x.%04x rc/rs=%d/%d => rc=EINVAL\n",
656 AP_QID_CARD(zq->queue->qid),
657 AP_QID_QUEUE(zq->queue->qid),
658 (int) service_rc, (int) service_rs);
661 if (service_rc == 8 && service_rs == 783) {
662 zq->zcard->min_mod_size =
663 PCIXCC_MIN_MOD_SIZE_OLD;
664 ZCRYPT_DBF(DBF_DEBUG,
665 "device=%02x.%04x rc/rs=%d/%d => rc=EAGAIN\n",
666 AP_QID_CARD(zq->queue->qid),
667 AP_QID_QUEUE(zq->queue->qid),
668 (int) service_rc, (int) service_rs);
672 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
673 AP_QID_CARD(zq->queue->qid),
674 AP_QID_QUEUE(zq->queue->qid));
676 "device=%02x.%04x rc/rs=%d/%d => online=0 rc=EAGAIN\n",
677 AP_QID_CARD(zq->queue->qid),
678 AP_QID_QUEUE(zq->queue->qid),
679 (int) service_rc, (int) service_rs);
680 return -EAGAIN; /* repeat the request on a different device. */
683 reply_len = msg->length - 2;
684 if (reply_len > outputdatalength)
687 * For all encipher requests, the length of the ciphertext (reply_len)
688 * will always equal the modulus length. For MEX decipher requests
689 * the output needs to get padded. Minimum pad size is 10.
691 * Currently, the cases where padding will be added is for:
692 * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
693 * ZERO-PAD and CRT is only supported for PKD requests)
696 pad_len = outputdatalength - reply_len;
700 /* 'restore' padding left in the PCICC/PCIXCC card. */
701 if (copy_to_user(outputdata, static_pad, pad_len - 1))
703 if (put_user(0, outputdata + pad_len - 1))
706 /* Copy the crypto response to user space. */
707 if (copy_to_user(outputdata + pad_len, data, reply_len))
713 * Copy results from a type 86 XCRB reply message back to user space.
715 * @zq: crypto device pointer
716 * @reply: reply AP message.
717 * @xcRB: pointer to XCRB
719 * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
721 static int convert_type86_xcrb(struct zcrypt_queue *zq,
722 struct ap_message *reply,
723 struct ica_xcRB *xcRB)
725 struct type86_fmt2_msg *msg = reply->message;
726 char *data = reply->message;
728 /* Copy CPRB to user */
729 if (copy_to_user(xcRB->reply_control_blk_addr,
730 data + msg->fmt2.offset1, msg->fmt2.count1))
732 xcRB->reply_control_blk_length = msg->fmt2.count1;
734 /* Copy data buffer to user */
735 if (msg->fmt2.count2)
736 if (copy_to_user(xcRB->reply_data_addr,
737 data + msg->fmt2.offset2, msg->fmt2.count2))
739 xcRB->reply_data_length = msg->fmt2.count2;
744 * Copy results from a type 86 EP11 XCRB reply message back to user space.
746 * @zq: crypto device pointer
747 * @reply: reply AP message.
748 * @xcRB: pointer to EP11 user request block
750 * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
752 static int convert_type86_ep11_xcrb(struct zcrypt_queue *zq,
753 struct ap_message *reply,
754 struct ep11_urb *xcRB)
756 struct type86_fmt2_msg *msg = reply->message;
757 char *data = reply->message;
759 if (xcRB->resp_len < msg->fmt2.count1)
762 /* Copy response CPRB to user */
763 if (copy_to_user((char __force __user *)xcRB->resp,
764 data + msg->fmt2.offset1, msg->fmt2.count1))
766 xcRB->resp_len = msg->fmt2.count1;
770 static int convert_type86_rng(struct zcrypt_queue *zq,
771 struct ap_message *reply,
775 struct type86_hdr hdr;
776 struct type86_fmt2_ext fmt2;
778 } __packed * msg = reply->message;
779 char *data = reply->message;
781 if (msg->cprbx.ccp_rtcode != 0 || msg->cprbx.ccp_rscode != 0)
783 memcpy(buffer, data + msg->fmt2.offset2, msg->fmt2.count2);
784 return msg->fmt2.count2;
787 static int convert_response_ica(struct zcrypt_queue *zq,
788 struct ap_message *reply,
789 char __user *outputdata,
790 unsigned int outputdatalength)
792 struct type86x_reply *msg = reply->message;
794 switch (msg->hdr.type) {
795 case TYPE82_RSP_CODE:
796 case TYPE88_RSP_CODE:
797 return convert_error(zq, reply);
798 case TYPE86_RSP_CODE:
799 if (msg->cprbx.ccp_rtcode &&
800 (msg->cprbx.ccp_rscode == 0x14f) &&
801 (outputdatalength > 256)) {
802 if (zq->zcard->max_exp_bit_length <= 17) {
803 zq->zcard->max_exp_bit_length = 17;
808 if (msg->hdr.reply_code)
809 return convert_error(zq, reply);
810 if (msg->cprbx.cprb_ver_id == 0x02)
811 return convert_type86_ica(zq, reply,
812 outputdata, outputdatalength);
814 * Fall through, no break, incorrect cprb version is an unknown
817 default: /* Unknown response type, this should NEVER EVER happen */
819 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
820 AP_QID_CARD(zq->queue->qid),
821 AP_QID_QUEUE(zq->queue->qid));
823 "device=%02x.%04x rtype=0x%02x => online=0 rc=EAGAIN\n",
824 AP_QID_CARD(zq->queue->qid),
825 AP_QID_QUEUE(zq->queue->qid),
826 (int) msg->hdr.type);
827 return -EAGAIN; /* repeat the request on a different device. */
831 static int convert_response_xcrb(struct zcrypt_queue *zq,
832 struct ap_message *reply,
833 struct ica_xcRB *xcRB)
835 struct type86x_reply *msg = reply->message;
837 switch (msg->hdr.type) {
838 case TYPE82_RSP_CODE:
839 case TYPE88_RSP_CODE:
840 xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
841 return convert_error(zq, reply);
842 case TYPE86_RSP_CODE:
843 if (msg->hdr.reply_code) {
844 memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
845 return convert_error(zq, reply);
847 if (msg->cprbx.cprb_ver_id == 0x02)
848 return convert_type86_xcrb(zq, reply, xcRB);
850 * Fall through, no break, incorrect cprb version is an unknown
853 default: /* Unknown response type, this should NEVER EVER happen */
854 xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
856 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
857 AP_QID_CARD(zq->queue->qid),
858 AP_QID_QUEUE(zq->queue->qid));
860 "device=%02x.%04x rtype=0x%02x => online=0 rc=EAGAIN\n",
861 AP_QID_CARD(zq->queue->qid),
862 AP_QID_QUEUE(zq->queue->qid),
863 (int) msg->hdr.type);
864 return -EAGAIN; /* repeat the request on a different device. */
868 static int convert_response_ep11_xcrb(struct zcrypt_queue *zq,
869 struct ap_message *reply, struct ep11_urb *xcRB)
871 struct type86_ep11_reply *msg = reply->message;
873 switch (msg->hdr.type) {
874 case TYPE82_RSP_CODE:
875 case TYPE87_RSP_CODE:
876 return convert_error(zq, reply);
877 case TYPE86_RSP_CODE:
878 if (msg->hdr.reply_code)
879 return convert_error(zq, reply);
880 if (msg->cprbx.cprb_ver_id == 0x04)
881 return convert_type86_ep11_xcrb(zq, reply, xcRB);
882 /* Fall through, no break, incorrect cprb version is an unknown resp.*/
883 default: /* Unknown response type, this should NEVER EVER happen */
885 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
886 AP_QID_CARD(zq->queue->qid),
887 AP_QID_QUEUE(zq->queue->qid));
889 "device=%02x.%04x rtype=0x%02x => online=0 rc=EAGAIN\n",
890 AP_QID_CARD(zq->queue->qid),
891 AP_QID_QUEUE(zq->queue->qid),
892 (int) msg->hdr.type);
893 return -EAGAIN; /* repeat the request on a different device. */
897 static int convert_response_rng(struct zcrypt_queue *zq,
898 struct ap_message *reply,
901 struct type86x_reply *msg = reply->message;
903 switch (msg->hdr.type) {
904 case TYPE82_RSP_CODE:
905 case TYPE88_RSP_CODE:
907 case TYPE86_RSP_CODE:
908 if (msg->hdr.reply_code)
910 if (msg->cprbx.cprb_ver_id == 0x02)
911 return convert_type86_rng(zq, reply, data);
913 * Fall through, no break, incorrect cprb version is an unknown
916 default: /* Unknown response type, this should NEVER EVER happen */
918 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
919 AP_QID_CARD(zq->queue->qid),
920 AP_QID_QUEUE(zq->queue->qid));
922 "device=%02x.%04x rtype=0x%02x => online=0 rc=EAGAIN\n",
923 AP_QID_CARD(zq->queue->qid),
924 AP_QID_QUEUE(zq->queue->qid),
925 (int) msg->hdr.type);
926 return -EAGAIN; /* repeat the request on a different device. */
931 * This function is called from the AP bus code after a crypto request
932 * "msg" has finished with the reply message "reply".
933 * It is called from tasklet context.
934 * @aq: pointer to the AP queue
935 * @msg: pointer to the AP message
936 * @reply: pointer to the AP reply message
938 static void zcrypt_msgtype6_receive(struct ap_queue *aq,
939 struct ap_message *msg,
940 struct ap_message *reply)
942 static struct error_hdr error_reply = {
943 .type = TYPE82_RSP_CODE,
944 .reply_code = REP82_ERROR_MACHINE_FAILURE,
946 struct response_type *resp_type =
947 (struct response_type *) msg->private;
948 struct type86x_reply *t86r;
951 /* Copy the reply message to the request message buffer. */
953 goto out; /* ap_msg->rc indicates the error */
954 t86r = reply->message;
955 if (t86r->hdr.type == TYPE86_RSP_CODE &&
956 t86r->cprbx.cprb_ver_id == 0x02) {
957 switch (resp_type->type) {
958 case PCIXCC_RESPONSE_TYPE_ICA:
959 length = sizeof(struct type86x_reply)
961 length = min(PCIXCC_MAX_ICA_RESPONSE_SIZE, length);
962 memcpy(msg->message, reply->message, length);
964 case PCIXCC_RESPONSE_TYPE_XCRB:
965 length = t86r->fmt2.offset2 + t86r->fmt2.count2;
966 length = min(MSGTYPE06_MAX_MSG_SIZE, length);
967 memcpy(msg->message, reply->message, length);
970 memcpy(msg->message, &error_reply,
971 sizeof(error_reply));
974 memcpy(msg->message, reply->message, sizeof(error_reply));
976 complete(&(resp_type->work));
980 * This function is called from the AP bus code after a crypto request
981 * "msg" has finished with the reply message "reply".
982 * It is called from tasklet context.
983 * @aq: pointer to the AP queue
984 * @msg: pointer to the AP message
985 * @reply: pointer to the AP reply message
987 static void zcrypt_msgtype6_receive_ep11(struct ap_queue *aq,
988 struct ap_message *msg,
989 struct ap_message *reply)
991 static struct error_hdr error_reply = {
992 .type = TYPE82_RSP_CODE,
993 .reply_code = REP82_ERROR_MACHINE_FAILURE,
995 struct response_type *resp_type =
996 (struct response_type *)msg->private;
997 struct type86_ep11_reply *t86r;
1000 /* Copy the reply message to the request message buffer. */
1002 goto out; /* ap_msg->rc indicates the error */
1003 t86r = reply->message;
1004 if (t86r->hdr.type == TYPE86_RSP_CODE &&
1005 t86r->cprbx.cprb_ver_id == 0x04) {
1006 switch (resp_type->type) {
1007 case PCIXCC_RESPONSE_TYPE_EP11:
1008 length = t86r->fmt2.offset1 + t86r->fmt2.count1;
1009 length = min(MSGTYPE06_MAX_MSG_SIZE, length);
1010 memcpy(msg->message, reply->message, length);
1013 memcpy(msg->message, &error_reply, sizeof(error_reply));
1016 memcpy(msg->message, reply->message, sizeof(error_reply));
1019 complete(&(resp_type->work));
1022 static atomic_t zcrypt_step = ATOMIC_INIT(0);
1025 * The request distributor calls this function if it picked the PCIXCC/CEX2C
1026 * device to handle a modexpo request.
1027 * @zq: pointer to zcrypt_queue structure that identifies the
1028 * PCIXCC/CEX2C device to the request distributor
1029 * @mex: pointer to the modexpo request buffer
1031 static long zcrypt_msgtype6_modexpo(struct zcrypt_queue *zq,
1032 struct ica_rsa_modexpo *mex)
1034 struct ap_message ap_msg;
1035 struct response_type resp_type = {
1036 .type = PCIXCC_RESPONSE_TYPE_ICA,
1040 ap_init_message(&ap_msg);
1041 ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
1042 if (!ap_msg.message)
1044 ap_msg.receive = zcrypt_msgtype6_receive;
1045 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
1046 atomic_inc_return(&zcrypt_step);
1047 ap_msg.private = &resp_type;
1048 rc = ICAMEX_msg_to_type6MEX_msgX(zq, &ap_msg, mex);
1051 init_completion(&resp_type.work);
1052 ap_queue_message(zq->queue, &ap_msg);
1053 rc = wait_for_completion_interruptible(&resp_type.work);
1057 rc = convert_response_ica(zq, &ap_msg,
1059 mex->outputdatalength);
1061 /* Signal pending. */
1062 ap_cancel_message(zq->queue, &ap_msg);
1064 free_page((unsigned long) ap_msg.message);
1069 * The request distributor calls this function if it picked the PCIXCC/CEX2C
1070 * device to handle a modexpo_crt request.
1071 * @zq: pointer to zcrypt_queue structure that identifies the
1072 * PCIXCC/CEX2C device to the request distributor
1073 * @crt: pointer to the modexpoc_crt request buffer
1075 static long zcrypt_msgtype6_modexpo_crt(struct zcrypt_queue *zq,
1076 struct ica_rsa_modexpo_crt *crt)
1078 struct ap_message ap_msg;
1079 struct response_type resp_type = {
1080 .type = PCIXCC_RESPONSE_TYPE_ICA,
1084 ap_init_message(&ap_msg);
1085 ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
1086 if (!ap_msg.message)
1088 ap_msg.receive = zcrypt_msgtype6_receive;
1089 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
1090 atomic_inc_return(&zcrypt_step);
1091 ap_msg.private = &resp_type;
1092 rc = ICACRT_msg_to_type6CRT_msgX(zq, &ap_msg, crt);
1095 init_completion(&resp_type.work);
1096 ap_queue_message(zq->queue, &ap_msg);
1097 rc = wait_for_completion_interruptible(&resp_type.work);
1101 rc = convert_response_ica(zq, &ap_msg,
1103 crt->outputdatalength);
1105 /* Signal pending. */
1106 ap_cancel_message(zq->queue, &ap_msg);
1109 free_page((unsigned long) ap_msg.message);
1114 * Fetch function code from cprb.
1115 * Extracting the fc requires to copy the cprb from userspace.
1116 * So this function allocates memory and needs an ap_msg prepared
1117 * by the caller with ap_init_message(). Also the caller has to
1118 * make sure ap_release_message() is always called even on failure.
1120 unsigned int get_cprb_fc(struct ica_xcRB *xcRB,
1121 struct ap_message *ap_msg,
1122 unsigned int *func_code, unsigned short **dom)
1124 struct response_type resp_type = {
1125 .type = PCIXCC_RESPONSE_TYPE_XCRB,
1128 ap_msg->message = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
1129 if (!ap_msg->message)
1131 ap_msg->receive = zcrypt_msgtype6_receive;
1132 ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1133 atomic_inc_return(&zcrypt_step);
1134 ap_msg->private = kmalloc(sizeof(resp_type), GFP_KERNEL);
1135 if (!ap_msg->private)
1137 memcpy(ap_msg->private, &resp_type, sizeof(resp_type));
1138 return XCRB_msg_to_type6CPRB_msgX(ap_msg, xcRB, func_code, dom);
1142 * The request distributor calls this function if it picked the PCIXCC/CEX2C
1143 * device to handle a send_cprb request.
1144 * @zq: pointer to zcrypt_queue structure that identifies the
1145 * PCIXCC/CEX2C device to the request distributor
1146 * @xcRB: pointer to the send_cprb request buffer
1148 static long zcrypt_msgtype6_send_cprb(struct zcrypt_queue *zq,
1149 struct ica_xcRB *xcRB,
1150 struct ap_message *ap_msg)
1153 struct response_type *rtype = (struct response_type *)(ap_msg->private);
1155 init_completion(&rtype->work);
1156 ap_queue_message(zq->queue, ap_msg);
1157 rc = wait_for_completion_interruptible(&rtype->work);
1161 rc = convert_response_xcrb(zq, ap_msg, xcRB);
1163 /* Signal pending. */
1164 ap_cancel_message(zq->queue, ap_msg);
1170 * Fetch function code from ep11 cprb.
1171 * Extracting the fc requires to copy the ep11 cprb from userspace.
1172 * So this function allocates memory and needs an ap_msg prepared
1173 * by the caller with ap_init_message(). Also the caller has to
1174 * make sure ap_release_message() is always called even on failure.
1176 unsigned int get_ep11cprb_fc(struct ep11_urb *xcrb,
1177 struct ap_message *ap_msg,
1178 unsigned int *func_code)
1180 struct response_type resp_type = {
1181 .type = PCIXCC_RESPONSE_TYPE_EP11,
1184 ap_msg->message = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
1185 if (!ap_msg->message)
1187 ap_msg->receive = zcrypt_msgtype6_receive_ep11;
1188 ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1189 atomic_inc_return(&zcrypt_step);
1190 ap_msg->private = kmalloc(sizeof(resp_type), GFP_KERNEL);
1191 if (!ap_msg->private)
1193 memcpy(ap_msg->private, &resp_type, sizeof(resp_type));
1194 return xcrb_msg_to_type6_ep11cprb_msgx(ap_msg, xcrb, func_code);
1198 * The request distributor calls this function if it picked the CEX4P
1199 * device to handle a send_ep11_cprb request.
1200 * @zq: pointer to zcrypt_queue structure that identifies the
1201 * CEX4P device to the request distributor
1202 * @xcRB: pointer to the ep11 user request block
1204 static long zcrypt_msgtype6_send_ep11_cprb(struct zcrypt_queue *zq,
1205 struct ep11_urb *xcrb,
1206 struct ap_message *ap_msg)
1210 struct response_type *rtype = (struct response_type *)(ap_msg->private);
1212 struct type6_hdr hdr;
1213 struct ep11_cprb cprbx;
1214 unsigned char pld_tag; /* fixed value 0x30 */
1215 unsigned char pld_lenfmt; /* payload length format */
1216 } __packed * msg = ap_msg->message;
1218 unsigned char func_tag; /* fixed value 0x4 */
1219 unsigned char func_len; /* fixed value 0x4 */
1220 unsigned int func_val; /* function ID */
1221 unsigned char dom_tag; /* fixed value 0x4 */
1222 unsigned char dom_len; /* fixed value 0x4 */
1223 unsigned int dom_val; /* domain id */
1224 } __packed * payload_hdr = NULL;
1228 * The target domain field within the cprb body/payload block will be
1229 * replaced by the usage domain for non-management commands only.
1230 * Therefore we check the first bit of the 'flags' parameter for
1231 * management command indication.
1232 * 0 - non management command
1233 * 1 - management command
1235 if (!((msg->cprbx.flags & 0x80) == 0x80)) {
1236 msg->cprbx.target_id = (unsigned int)
1237 AP_QID_QUEUE(zq->queue->qid);
1239 if ((msg->pld_lenfmt & 0x80) == 0x80) { /*ext.len.fmt 2 or 3*/
1240 switch (msg->pld_lenfmt & 0x03) {
1251 lfmt = 1; /* length format #1 */
1253 payload_hdr = (struct pld_hdr *)((&(msg->pld_lenfmt))+lfmt);
1254 payload_hdr->dom_val = (unsigned int)
1255 AP_QID_QUEUE(zq->queue->qid);
1258 init_completion(&rtype->work);
1259 ap_queue_message(zq->queue, ap_msg);
1260 rc = wait_for_completion_interruptible(&rtype->work);
1264 rc = convert_response_ep11_xcrb(zq, ap_msg, xcrb);
1266 /* Signal pending. */
1267 ap_cancel_message(zq->queue, ap_msg);
1272 unsigned int get_rng_fc(struct ap_message *ap_msg, int *func_code,
1273 unsigned int *domain)
1275 struct response_type resp_type = {
1276 .type = PCIXCC_RESPONSE_TYPE_XCRB,
1279 ap_msg->message = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
1280 if (!ap_msg->message)
1282 ap_msg->receive = zcrypt_msgtype6_receive;
1283 ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
1284 atomic_inc_return(&zcrypt_step);
1285 ap_msg->private = kmalloc(sizeof(resp_type), GFP_KERNEL);
1286 if (!ap_msg->private)
1288 memcpy(ap_msg->private, &resp_type, sizeof(resp_type));
1290 rng_type6CPRB_msgX(ap_msg, ZCRYPT_RNG_BUFFER_SIZE, domain);
1297 * The request distributor calls this function if it picked the PCIXCC/CEX2C
1298 * device to generate random data.
1299 * @zq: pointer to zcrypt_queue structure that identifies the
1300 * PCIXCC/CEX2C device to the request distributor
1301 * @buffer: pointer to a memory page to return random data
1303 static long zcrypt_msgtype6_rng(struct zcrypt_queue *zq,
1304 char *buffer, struct ap_message *ap_msg)
1307 struct type6_hdr hdr;
1309 char function_code[2];
1310 short int rule_length;
1312 short int verb_length;
1313 short int key_length;
1314 } __packed * msg = ap_msg->message;
1315 struct response_type *rtype = (struct response_type *)(ap_msg->private);
1318 msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
1320 init_completion(&rtype->work);
1321 ap_queue_message(zq->queue, ap_msg);
1322 rc = wait_for_completion_interruptible(&rtype->work);
1326 rc = convert_response_rng(zq, ap_msg, buffer);
1328 /* Signal pending. */
1329 ap_cancel_message(zq->queue, ap_msg);
1335 * The crypto operations for a PCIXCC/CEX2C card.
1337 static struct zcrypt_ops zcrypt_msgtype6_norng_ops = {
1338 .owner = THIS_MODULE,
1339 .name = MSGTYPE06_NAME,
1340 .variant = MSGTYPE06_VARIANT_NORNG,
1341 .rsa_modexpo = zcrypt_msgtype6_modexpo,
1342 .rsa_modexpo_crt = zcrypt_msgtype6_modexpo_crt,
1343 .send_cprb = zcrypt_msgtype6_send_cprb,
1346 static struct zcrypt_ops zcrypt_msgtype6_ops = {
1347 .owner = THIS_MODULE,
1348 .name = MSGTYPE06_NAME,
1349 .variant = MSGTYPE06_VARIANT_DEFAULT,
1350 .rsa_modexpo = zcrypt_msgtype6_modexpo,
1351 .rsa_modexpo_crt = zcrypt_msgtype6_modexpo_crt,
1352 .send_cprb = zcrypt_msgtype6_send_cprb,
1353 .rng = zcrypt_msgtype6_rng,
1356 static struct zcrypt_ops zcrypt_msgtype6_ep11_ops = {
1357 .owner = THIS_MODULE,
1358 .name = MSGTYPE06_NAME,
1359 .variant = MSGTYPE06_VARIANT_EP11,
1360 .rsa_modexpo = NULL,
1361 .rsa_modexpo_crt = NULL,
1362 .send_ep11_cprb = zcrypt_msgtype6_send_ep11_cprb,
1365 void __init zcrypt_msgtype6_init(void)
1367 zcrypt_msgtype_register(&zcrypt_msgtype6_norng_ops);
1368 zcrypt_msgtype_register(&zcrypt_msgtype6_ops);
1369 zcrypt_msgtype_register(&zcrypt_msgtype6_ep11_ops);
1372 void __exit zcrypt_msgtype6_exit(void)
1374 zcrypt_msgtype_unregister(&zcrypt_msgtype6_norng_ops);
1375 zcrypt_msgtype_unregister(&zcrypt_msgtype6_ops);
1376 zcrypt_msgtype_unregister(&zcrypt_msgtype6_ep11_ops);