linux/drivers/s390/crypto/zcrypt_msgtype50.c
<<
>>
Prefs
   1/*
   2 *  zcrypt 2.1.0
   3 *
   4 *  Copyright IBM Corp. 2001, 2012
   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 *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.ibm.com>
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License as published by
  15 * the Free Software Foundation; either version 2, or (at your option)
  16 * any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26 */
  27
  28#define KMSG_COMPONENT "zcrypt"
  29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  30
  31#include <linux/module.h>
  32#include <linux/slab.h>
  33#include <linux/init.h>
  34#include <linux/err.h>
  35#include <linux/atomic.h>
  36#include <linux/uaccess.h>
  37
  38#include "ap_bus.h"
  39#include "zcrypt_api.h"
  40#include "zcrypt_error.h"
  41#include "zcrypt_msgtype50.h"
  42
  43#define CEX3A_MAX_MOD_SIZE      512     /* 4096 bits    */
  44
  45#define CEX2A_MAX_RESPONSE_SIZE 0x110   /* max outputdatalength + type80_hdr */
  46
  47#define CEX3A_MAX_RESPONSE_SIZE 0x210   /* 512 bit modulus
  48                                         * (max outputdatalength) +
  49                                         * type80_hdr*/
  50
  51MODULE_AUTHOR("IBM Corporation");
  52MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
  53                   "Copyright IBM Corp. 2001, 2012");
  54MODULE_LICENSE("GPL");
  55
  56static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
  57                                 struct ap_message *);
  58
  59/**
  60 * The type 50 message family is associated with a CEX2A card.
  61 *
  62 * The four members of the family are described below.
  63 *
  64 * Note that all unsigned char arrays are right-justified and left-padded
  65 * with zeroes.
  66 *
  67 * Note that all reserved fields must be zeroes.
  68 */
  69struct type50_hdr {
  70        unsigned char   reserved1;
  71        unsigned char   msg_type_code;  /* 0x50 */
  72        unsigned short  msg_len;
  73        unsigned char   reserved2;
  74        unsigned char   ignored;
  75        unsigned short  reserved3;
  76} __packed;
  77
  78#define TYPE50_TYPE_CODE        0x50
  79
  80#define TYPE50_MEB1_FMT         0x0001
  81#define TYPE50_MEB2_FMT         0x0002
  82#define TYPE50_MEB3_FMT         0x0003
  83#define TYPE50_CRB1_FMT         0x0011
  84#define TYPE50_CRB2_FMT         0x0012
  85#define TYPE50_CRB3_FMT         0x0013
  86
  87/* Mod-Exp, with a small modulus */
  88struct type50_meb1_msg {
  89        struct type50_hdr header;
  90        unsigned short  keyblock_type;  /* 0x0001 */
  91        unsigned char   reserved[6];
  92        unsigned char   exponent[128];
  93        unsigned char   modulus[128];
  94        unsigned char   message[128];
  95} __packed;
  96
  97/* Mod-Exp, with a large modulus */
  98struct type50_meb2_msg {
  99        struct type50_hdr header;
 100        unsigned short  keyblock_type;  /* 0x0002 */
 101        unsigned char   reserved[6];
 102        unsigned char   exponent[256];
 103        unsigned char   modulus[256];
 104        unsigned char   message[256];
 105} __packed;
 106
 107/* Mod-Exp, with a larger modulus */
 108struct type50_meb3_msg {
 109        struct type50_hdr header;
 110        unsigned short  keyblock_type;  /* 0x0003 */
 111        unsigned char   reserved[6];
 112        unsigned char   exponent[512];
 113        unsigned char   modulus[512];
 114        unsigned char   message[512];
 115} __packed;
 116
 117/* CRT, with a small modulus */
 118struct type50_crb1_msg {
 119        struct type50_hdr header;
 120        unsigned short  keyblock_type;  /* 0x0011 */
 121        unsigned char   reserved[6];
 122        unsigned char   p[64];
 123        unsigned char   q[64];
 124        unsigned char   dp[64];
 125        unsigned char   dq[64];
 126        unsigned char   u[64];
 127        unsigned char   message[128];
 128} __packed;
 129
 130/* CRT, with a large modulus */
 131struct type50_crb2_msg {
 132        struct type50_hdr header;
 133        unsigned short  keyblock_type;  /* 0x0012 */
 134        unsigned char   reserved[6];
 135        unsigned char   p[128];
 136        unsigned char   q[128];
 137        unsigned char   dp[128];
 138        unsigned char   dq[128];
 139        unsigned char   u[128];
 140        unsigned char   message[256];
 141} __packed;
 142
 143/* CRT, with a larger modulus */
 144struct type50_crb3_msg {
 145        struct type50_hdr header;
 146        unsigned short  keyblock_type;  /* 0x0013 */
 147        unsigned char   reserved[6];
 148        unsigned char   p[256];
 149        unsigned char   q[256];
 150        unsigned char   dp[256];
 151        unsigned char   dq[256];
 152        unsigned char   u[256];
 153        unsigned char   message[512];
 154} __packed;
 155
 156/**
 157 * The type 80 response family is associated with a CEX2A card.
 158 *
 159 * Note that all unsigned char arrays are right-justified and left-padded
 160 * with zeroes.
 161 *
 162 * Note that all reserved fields must be zeroes.
 163 */
 164
 165#define TYPE80_RSP_CODE 0x80
 166
 167struct type80_hdr {
 168        unsigned char   reserved1;
 169        unsigned char   type;           /* 0x80 */
 170        unsigned short  len;
 171        unsigned char   code;           /* 0x00 */
 172        unsigned char   reserved2[3];
 173        unsigned char   reserved3[8];
 174} __packed;
 175
 176/**
 177 * Convert a ICAMEX message to a type50 MEX message.
 178 *
 179 * @zdev: crypto device pointer
 180 * @zreq: crypto request pointer
 181 * @mex: pointer to user input data
 182 *
 183 * Returns 0 on success or -EFAULT.
 184 */
 185static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
 186                                       struct ap_message *ap_msg,
 187                                       struct ica_rsa_modexpo *mex)
 188{
 189        unsigned char *mod, *exp, *inp;
 190        int mod_len;
 191
 192        mod_len = mex->inputdatalength;
 193
 194        if (mod_len <= 128) {
 195                struct type50_meb1_msg *meb1 = ap_msg->message;
 196                memset(meb1, 0, sizeof(*meb1));
 197                ap_msg->length = sizeof(*meb1);
 198                meb1->header.msg_type_code = TYPE50_TYPE_CODE;
 199                meb1->header.msg_len = sizeof(*meb1);
 200                meb1->keyblock_type = TYPE50_MEB1_FMT;
 201                mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
 202                exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
 203                inp = meb1->message + sizeof(meb1->message) - mod_len;
 204        } else if (mod_len <= 256) {
 205                struct type50_meb2_msg *meb2 = ap_msg->message;
 206                memset(meb2, 0, sizeof(*meb2));
 207                ap_msg->length = sizeof(*meb2);
 208                meb2->header.msg_type_code = TYPE50_TYPE_CODE;
 209                meb2->header.msg_len = sizeof(*meb2);
 210                meb2->keyblock_type = TYPE50_MEB2_FMT;
 211                mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
 212                exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
 213                inp = meb2->message + sizeof(meb2->message) - mod_len;
 214        } else {
 215                /* mod_len > 256 = 4096 bit RSA Key */
 216                struct type50_meb3_msg *meb3 = ap_msg->message;
 217                memset(meb3, 0, sizeof(*meb3));
 218                ap_msg->length = sizeof(*meb3);
 219                meb3->header.msg_type_code = TYPE50_TYPE_CODE;
 220                meb3->header.msg_len = sizeof(*meb3);
 221                meb3->keyblock_type = TYPE50_MEB3_FMT;
 222                mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
 223                exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
 224                inp = meb3->message + sizeof(meb3->message) - mod_len;
 225        }
 226
 227        if (copy_from_user(mod, mex->n_modulus, mod_len) ||
 228            copy_from_user(exp, mex->b_key, mod_len) ||
 229            copy_from_user(inp, mex->inputdata, mod_len))
 230                return -EFAULT;
 231        return 0;
 232}
 233
 234/**
 235 * Convert a ICACRT message to a type50 CRT message.
 236 *
 237 * @zdev: crypto device pointer
 238 * @zreq: crypto request pointer
 239 * @crt: pointer to user input data
 240 *
 241 * Returns 0 on success or -EFAULT.
 242 */
 243static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
 244                                       struct ap_message *ap_msg,
 245                                       struct ica_rsa_modexpo_crt *crt)
 246{
 247        int mod_len, short_len;
 248        unsigned char *p, *q, *dp, *dq, *u, *inp;
 249
 250        mod_len = crt->inputdatalength;
 251        short_len = mod_len / 2;
 252
 253        /*
 254         * CEX2A and CEX3A w/o FW update can handle requests up to
 255         * 256 byte modulus (2k keys).
 256         * CEX3A with FW update and CEX4A cards are able to handle
 257         * 512 byte modulus (4k keys).
 258         */
 259        if (mod_len <= 128) {           /* up to 1024 bit key size */
 260                struct type50_crb1_msg *crb1 = ap_msg->message;
 261                memset(crb1, 0, sizeof(*crb1));
 262                ap_msg->length = sizeof(*crb1);
 263                crb1->header.msg_type_code = TYPE50_TYPE_CODE;
 264                crb1->header.msg_len = sizeof(*crb1);
 265                crb1->keyblock_type = TYPE50_CRB1_FMT;
 266                p = crb1->p + sizeof(crb1->p) - short_len;
 267                q = crb1->q + sizeof(crb1->q) - short_len;
 268                dp = crb1->dp + sizeof(crb1->dp) - short_len;
 269                dq = crb1->dq + sizeof(crb1->dq) - short_len;
 270                u = crb1->u + sizeof(crb1->u) - short_len;
 271                inp = crb1->message + sizeof(crb1->message) - mod_len;
 272        } else if (mod_len <= 256) {    /* up to 2048 bit key size */
 273                struct type50_crb2_msg *crb2 = ap_msg->message;
 274                memset(crb2, 0, sizeof(*crb2));
 275                ap_msg->length = sizeof(*crb2);
 276                crb2->header.msg_type_code = TYPE50_TYPE_CODE;
 277                crb2->header.msg_len = sizeof(*crb2);
 278                crb2->keyblock_type = TYPE50_CRB2_FMT;
 279                p = crb2->p + sizeof(crb2->p) - short_len;
 280                q = crb2->q + sizeof(crb2->q) - short_len;
 281                dp = crb2->dp + sizeof(crb2->dp) - short_len;
 282                dq = crb2->dq + sizeof(crb2->dq) - short_len;
 283                u = crb2->u + sizeof(crb2->u) - short_len;
 284                inp = crb2->message + sizeof(crb2->message) - mod_len;
 285        } else if ((mod_len <= 512) &&  /* up to 4096 bit key size */
 286                   (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)) { /* >= CEX3A */
 287                struct type50_crb3_msg *crb3 = ap_msg->message;
 288                memset(crb3, 0, sizeof(*crb3));
 289                ap_msg->length = sizeof(*crb3);
 290                crb3->header.msg_type_code = TYPE50_TYPE_CODE;
 291                crb3->header.msg_len = sizeof(*crb3);
 292                crb3->keyblock_type = TYPE50_CRB3_FMT;
 293                p = crb3->p + sizeof(crb3->p) - short_len;
 294                q = crb3->q + sizeof(crb3->q) - short_len;
 295                dp = crb3->dp + sizeof(crb3->dp) - short_len;
 296                dq = crb3->dq + sizeof(crb3->dq) - short_len;
 297                u = crb3->u + sizeof(crb3->u) - short_len;
 298                inp = crb3->message + sizeof(crb3->message) - mod_len;
 299        } else
 300                return -EINVAL;
 301
 302        /*
 303         * correct the offset of p, bp and mult_inv according zcrypt.h
 304         * block size right aligned (skip the first byte)
 305         */
 306        if (copy_from_user(p, crt->np_prime + MSGTYPE_ADJUSTMENT, short_len) ||
 307            copy_from_user(q, crt->nq_prime, short_len) ||
 308            copy_from_user(dp, crt->bp_key + MSGTYPE_ADJUSTMENT, short_len) ||
 309            copy_from_user(dq, crt->bq_key, short_len) ||
 310            copy_from_user(u, crt->u_mult_inv + MSGTYPE_ADJUSTMENT, short_len) ||
 311            copy_from_user(inp, crt->inputdata, mod_len))
 312                return -EFAULT;
 313
 314        return 0;
 315}
 316
 317/**
 318 * Copy results from a type 80 reply message back to user space.
 319 *
 320 * @zdev: crypto device pointer
 321 * @reply: reply AP message.
 322 * @data: pointer to user output data
 323 * @length: size of user output data
 324 *
 325 * Returns 0 on success or -EFAULT.
 326 */
 327static int convert_type80(struct zcrypt_device *zdev,
 328                          struct ap_message *reply,
 329                          char __user *outputdata,
 330                          unsigned int outputdatalength)
 331{
 332        struct type80_hdr *t80h = reply->message;
 333        unsigned char *data;
 334
 335        if (t80h->len < sizeof(*t80h) + outputdatalength) {
 336                /* The result is too short, the CEX2A card may not do that.. */
 337                zdev->online = 0;
 338                pr_err("Cryptographic device %x failed and was set offline\n",
 339                       zdev->ap_dev->qid);
 340                ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%drc%d",
 341                               zdev->ap_dev->qid, zdev->online, t80h->code);
 342
 343                return -EAGAIN; /* repeat the request on a different device. */
 344        }
 345        if (zdev->user_space_type == ZCRYPT_CEX2A)
 346                BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
 347        else
 348                BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
 349        data = reply->message + t80h->len - outputdatalength;
 350        if (copy_to_user(outputdata, data, outputdatalength))
 351                return -EFAULT;
 352        return 0;
 353}
 354
 355static int convert_response(struct zcrypt_device *zdev,
 356                            struct ap_message *reply,
 357                            char __user *outputdata,
 358                            unsigned int outputdatalength)
 359{
 360        /* Response type byte is the second byte in the response. */
 361        switch (((unsigned char *) reply->message)[1]) {
 362        case TYPE82_RSP_CODE:
 363        case TYPE88_RSP_CODE:
 364                return convert_error(zdev, reply);
 365        case TYPE80_RSP_CODE:
 366                return convert_type80(zdev, reply,
 367                                      outputdata, outputdatalength);
 368        default: /* Unknown response type, this should NEVER EVER happen */
 369                zdev->online = 0;
 370                pr_err("Cryptographic device %x failed and was set offline\n",
 371                       zdev->ap_dev->qid);
 372                ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%dfail",
 373                               zdev->ap_dev->qid, zdev->online);
 374                return -EAGAIN; /* repeat the request on a different device. */
 375        }
 376}
 377
 378/**
 379 * This function is called from the AP bus code after a crypto request
 380 * "msg" has finished with the reply message "reply".
 381 * It is called from tasklet context.
 382 * @ap_dev: pointer to the AP device
 383 * @msg: pointer to the AP message
 384 * @reply: pointer to the AP reply message
 385 */
 386static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
 387                                 struct ap_message *msg,
 388                                 struct ap_message *reply)
 389{
 390        static struct error_hdr error_reply = {
 391                .type = TYPE82_RSP_CODE,
 392                .reply_code = REP82_ERROR_MACHINE_FAILURE,
 393        };
 394        struct type80_hdr *t80h;
 395        int length;
 396
 397        /* Copy the reply message to the request message buffer. */
 398        if (IS_ERR(reply)) {
 399                memcpy(msg->message, &error_reply, sizeof(error_reply));
 400                goto out;
 401        }
 402        t80h = reply->message;
 403        if (t80h->type == TYPE80_RSP_CODE) {
 404                if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
 405                        length = min_t(int,
 406                                       CEX2A_MAX_RESPONSE_SIZE, t80h->len);
 407                else
 408                        length = min_t(int,
 409                                       CEX3A_MAX_RESPONSE_SIZE, t80h->len);
 410                memcpy(msg->message, reply->message, length);
 411        } else
 412                memcpy(msg->message, reply->message, sizeof(error_reply));
 413out:
 414        complete((struct completion *) msg->private);
 415}
 416
 417static atomic_t zcrypt_step = ATOMIC_INIT(0);
 418
 419/**
 420 * The request distributor calls this function if it picked the CEX2A
 421 * device to handle a modexpo request.
 422 * @zdev: pointer to zcrypt_device structure that identifies the
 423 *        CEX2A device to the request distributor
 424 * @mex: pointer to the modexpo request buffer
 425 */
 426static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
 427                                 struct ica_rsa_modexpo *mex)
 428{
 429        struct ap_message ap_msg;
 430        struct completion work;
 431        int rc;
 432
 433        ap_init_message(&ap_msg);
 434        if (zdev->user_space_type == ZCRYPT_CEX2A)
 435                ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
 436                                         GFP_KERNEL);
 437        else
 438                ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
 439                                         GFP_KERNEL);
 440        if (!ap_msg.message)
 441                return -ENOMEM;
 442        ap_msg.receive = zcrypt_cex2a_receive;
 443        ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
 444                                atomic_inc_return(&zcrypt_step);
 445        ap_msg.private = &work;
 446        rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
 447        if (rc)
 448                goto out_free;
 449        init_completion(&work);
 450        ap_queue_message(zdev->ap_dev, &ap_msg);
 451        rc = wait_for_completion_interruptible(&work);
 452        if (rc == 0)
 453                rc = convert_response(zdev, &ap_msg, mex->outputdata,
 454                                      mex->outputdatalength);
 455        else
 456                /* Signal pending. */
 457                ap_cancel_message(zdev->ap_dev, &ap_msg);
 458out_free:
 459        kfree(ap_msg.message);
 460        return rc;
 461}
 462
 463/**
 464 * The request distributor calls this function if it picked the CEX2A
 465 * device to handle a modexpo_crt request.
 466 * @zdev: pointer to zcrypt_device structure that identifies the
 467 *        CEX2A device to the request distributor
 468 * @crt: pointer to the modexpoc_crt request buffer
 469 */
 470static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
 471                                     struct ica_rsa_modexpo_crt *crt)
 472{
 473        struct ap_message ap_msg;
 474        struct completion work;
 475        int rc;
 476
 477        ap_init_message(&ap_msg);
 478        if (zdev->user_space_type == ZCRYPT_CEX2A)
 479                ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
 480                                         GFP_KERNEL);
 481        else
 482                ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
 483                                         GFP_KERNEL);
 484        if (!ap_msg.message)
 485                return -ENOMEM;
 486        ap_msg.receive = zcrypt_cex2a_receive;
 487        ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
 488                                atomic_inc_return(&zcrypt_step);
 489        ap_msg.private = &work;
 490        rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
 491        if (rc)
 492                goto out_free;
 493        init_completion(&work);
 494        ap_queue_message(zdev->ap_dev, &ap_msg);
 495        rc = wait_for_completion_interruptible(&work);
 496        if (rc == 0)
 497                rc = convert_response(zdev, &ap_msg, crt->outputdata,
 498                                      crt->outputdatalength);
 499        else
 500                /* Signal pending. */
 501                ap_cancel_message(zdev->ap_dev, &ap_msg);
 502out_free:
 503        kfree(ap_msg.message);
 504        return rc;
 505}
 506
 507/**
 508 * The crypto operations for message type 50.
 509 */
 510static struct zcrypt_ops zcrypt_msgtype50_ops = {
 511        .rsa_modexpo = zcrypt_cex2a_modexpo,
 512        .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
 513        .owner = THIS_MODULE,
 514        .variant = MSGTYPE50_VARIANT_DEFAULT,
 515};
 516
 517int __init zcrypt_msgtype50_init(void)
 518{
 519        zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
 520        return 0;
 521}
 522
 523void __exit zcrypt_msgtype50_exit(void)
 524{
 525        zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
 526}
 527
 528module_init(zcrypt_msgtype50_init);
 529module_exit(zcrypt_msgtype50_exit);
 530