zcrypt_pcicc.c

来自「linux 内核源代码」· C语言 代码 · 共 622 行 · 第 1/2 页

C
622
字号
/* *  linux/drivers/s390/crypto/zcrypt_pcicc.c * *  zcrypt 2.1.0 * *  Copyright (C)  2001, 2006 IBM Corporation *  Author(s): Robert Burroughs *	       Eric Rossman (edrossma@us.ibm.com) * *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com) *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com> *				  Ralph Wuerthner <rwuerthn@de.ibm.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/module.h>#include <linux/init.h>#include <linux/err.h>#include <asm/atomic.h>#include <asm/uaccess.h>#include "ap_bus.h"#include "zcrypt_api.h"#include "zcrypt_error.h"#include "zcrypt_pcicc.h"#include "zcrypt_cca_key.h"#define PCICC_MIN_MOD_SIZE	 64	/*  512 bits */#define PCICC_MAX_MOD_SIZE_OLD	128	/* 1024 bits */#define PCICC_MAX_MOD_SIZE	256	/* 2048 bits *//** * PCICC cards need a speed rating of 0. This keeps them at the end of * the zcrypt device list (see zcrypt_api.c). PCICC cards are only * used if no other cards are present because they are slow and can only * cope with PKCS12 padded requests. The logic is queer. PKCS11 padded * requests are rejected. The modexpo function encrypts PKCS12 padded data * and decrypts any non-PKCS12 padded data (except PKCS11) in the assumption * that it's encrypted PKCS12 data. The modexpo_crt function always decrypts * the data in the assumption that its PKCS12 encrypted data. */#define PCICC_SPEED_RATING	0#define PCICC_MAX_MESSAGE_SIZE 0x710	/* max size type6 v1 crt message */#define PCICC_MAX_RESPONSE_SIZE 0x710	/* max size type86 v1 reply	 */#define PCICC_CLEANUP_TIME	(15*HZ)static struct ap_device_id zcrypt_pcicc_ids[] = {	{ AP_DEVICE(AP_DEVICE_TYPE_PCICC) },	{ /* end of list */ },};#ifndef CONFIG_ZCRYPT_MONOLITHICMODULE_DEVICE_TABLE(ap, zcrypt_pcicc_ids);MODULE_AUTHOR("IBM Corporation");MODULE_DESCRIPTION("PCICC Cryptographic Coprocessor device driver, "		   "Copyright 2001, 2006 IBM Corporation");MODULE_LICENSE("GPL");#endifstatic int zcrypt_pcicc_probe(struct ap_device *ap_dev);static void zcrypt_pcicc_remove(struct ap_device *ap_dev);static void zcrypt_pcicc_receive(struct ap_device *, struct ap_message *,				 struct ap_message *);static struct ap_driver zcrypt_pcicc_driver = {	.probe = zcrypt_pcicc_probe,	.remove = zcrypt_pcicc_remove,	.receive = zcrypt_pcicc_receive,	.ids = zcrypt_pcicc_ids,	.request_timeout = PCICC_CLEANUP_TIME,};/** * The following is used to initialize the CPRB passed to the PCICC card * in a type6 message. The 3 fields that must be filled in at execution * time are  req_parml, rpl_parml and usage_domain. Note that all three * fields are *little*-endian. Actually, everything about this interface * is ascii/little-endian, since the device has 'Intel inside'. * * The CPRB is followed immediately by the parm block. * The parm block contains: * - function code ('PD' 0x5044 or 'PK' 0x504B) * - rule block (0x0A00 'PKCS-1.2' or 0x0A00 'ZERO-PAD') * - VUD block */static struct CPRB static_cprb = {	.cprb_len	= __constant_cpu_to_le16(0x0070),	.cprb_ver_id	=  0x41,	.func_id	= {0x54,0x32},	.checkpoint_flag=  0x01,	.svr_namel	= __constant_cpu_to_le16(0x0008),	.svr_name	= {'I','C','S','F',' ',' ',' ',' '}};/** * Check the message for PKCS11 padding. */static inline int is_PKCS11_padded(unsigned char *buffer, int length){	int i;	if ((buffer[0] != 0x00) || (buffer[1] != 0x01))		return 0;	for (i = 2; i < length; i++)		if (buffer[i] != 0xFF)			break;	if (i < 10 || i == length)		return 0;	if (buffer[i] != 0x00)		return 0;	return 1;}/** * Check the message for PKCS12 padding. */static inline int is_PKCS12_padded(unsigned char *buffer, int length){	int i;	if ((buffer[0] != 0x00) || (buffer[1] != 0x02))		return 0;	for (i = 2; i < length; i++)		if (buffer[i] == 0x00)			break;	if ((i < 10) || (i == length))		return 0;	if (buffer[i] != 0x00)		return 0;	return 1;}/** * Convert a ICAMEX message to a type6 MEX message. * * @zdev: crypto device pointer * @zreq: crypto request pointer * @mex: pointer to user input data * * Returns 0 on success or -EFAULT. */static int ICAMEX_msg_to_type6MEX_msg(struct zcrypt_device *zdev,				      struct ap_message *ap_msg,				      struct ica_rsa_modexpo *mex){	static struct type6_hdr static_type6_hdr = {		.type		=  0x06,		.offset1	=  0x00000058,		.agent_id	= {0x01,0x00,0x43,0x43,0x41,0x2D,0x41,0x50,				   0x50,0x4C,0x20,0x20,0x20,0x01,0x01,0x01},		.function_code	= {'P','K'},	};	static struct function_and_rules_block static_pke_function_and_rules ={		.function_code	= {'P','K'},		.ulen		= __constant_cpu_to_le16(10),		.only_rule	= {'P','K','C','S','-','1','.','2'}	};	struct {		struct type6_hdr hdr;		struct CPRB cprb;		struct function_and_rules_block fr;		unsigned short length;		char text[0];	} __attribute__((packed)) *msg = ap_msg->message;	int vud_len, pad_len, size;	/* VUD.ciphertext */	if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))		return -EFAULT;	if (is_PKCS11_padded(msg->text, mex->inputdatalength))		return -EINVAL;	/* static message header and f&r */	msg->hdr = static_type6_hdr;	msg->fr = static_pke_function_and_rules;	if (is_PKCS12_padded(msg->text, mex->inputdatalength)) {		/* strip the padding and adjust the data length */		pad_len = strnlen(msg->text + 2, mex->inputdatalength - 2) + 3;		if (pad_len <= 9 || pad_len >= mex->inputdatalength)			return -ENODEV;		vud_len = mex->inputdatalength - pad_len;		memmove(msg->text, msg->text + pad_len, vud_len);		msg->length = cpu_to_le16(vud_len + 2);		/* Set up key after the variable length text. */		size = zcrypt_type6_mex_key_en(mex, msg->text + vud_len, 0);		if (size < 0)			return size;		size += sizeof(*msg) + vud_len;	/* total size of msg */	} else {		vud_len = mex->inputdatalength;		msg->length = cpu_to_le16(2 + vud_len);		msg->hdr.function_code[1] = 'D';		msg->fr.function_code[1] = 'D';		/* Set up key after the variable length text. */		size = zcrypt_type6_mex_key_de(mex, msg->text + vud_len, 0);		if (size < 0)			return size;		size += sizeof(*msg) + vud_len;	/* total size of msg */	}	/* message header, cprb and f&r */	msg->hdr.ToCardLen1 = (size - sizeof(msg->hdr) + 3) & -4;	msg->hdr.FromCardLen1 = PCICC_MAX_RESPONSE_SIZE - sizeof(msg->hdr);	msg->cprb = static_cprb;	msg->cprb.usage_domain[0]= AP_QID_QUEUE(zdev->ap_dev->qid);	msg->cprb.req_parml = cpu_to_le16(size - sizeof(msg->hdr) -					   sizeof(msg->cprb));	msg->cprb.rpl_parml = cpu_to_le16(msg->hdr.FromCardLen1);	ap_msg->length = (size + 3) & -4;	return 0;}/** * Convert a ICACRT message to a type6 CRT message. * * @zdev: crypto device pointer * @zreq: crypto request pointer * @crt: pointer to user input data * * Returns 0 on success or -EFAULT. */static int ICACRT_msg_to_type6CRT_msg(struct zcrypt_device *zdev,				      struct ap_message *ap_msg,				      struct ica_rsa_modexpo_crt *crt){	static struct type6_hdr static_type6_hdr = {		.type		=  0x06,		.offset1	=  0x00000058,		.agent_id	= {0x01,0x00,0x43,0x43,0x41,0x2D,0x41,0x50,				   0x50,0x4C,0x20,0x20,0x20,0x01,0x01,0x01},		.function_code	= {'P','D'},	};	static struct function_and_rules_block static_pkd_function_and_rules ={		.function_code	= {'P','D'},		.ulen		= __constant_cpu_to_le16(10),		.only_rule	= {'P','K','C','S','-','1','.','2'}	};	struct {		struct type6_hdr hdr;		struct CPRB cprb;		struct function_and_rules_block fr;		unsigned short length;		char text[0];	} __attribute__((packed)) *msg = ap_msg->message;	int size;	/* VUD.ciphertext */	msg->length = cpu_to_le16(2 + crt->inputdatalength);	if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))		return -EFAULT;	if (is_PKCS11_padded(msg->text, crt->inputdatalength))		return -EINVAL;	/* Set up key after the variable length text. */	size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength, 0);	if (size < 0)		return size;	size += sizeof(*msg) + crt->inputdatalength;	/* total size of msg */	/* message header, cprb and f&r */	msg->hdr = static_type6_hdr;	msg->hdr.ToCardLen1 = (size -  sizeof(msg->hdr) + 3) & -4;	msg->hdr.FromCardLen1 = PCICC_MAX_RESPONSE_SIZE - sizeof(msg->hdr);	msg->cprb = static_cprb;	msg->cprb.usage_domain[0] = AP_QID_QUEUE(zdev->ap_dev->qid);	msg->cprb.req_parml = msg->cprb.rpl_parml =		cpu_to_le16(size - sizeof(msg->hdr) - sizeof(msg->cprb));	msg->fr = static_pkd_function_and_rules;	ap_msg->length = (size + 3) & -4;	return 0;}/** * Copy results from a type 86 reply message back to user space. * * @zdev: crypto device pointer * @reply: reply AP message. * @data: pointer to user output data * @length: size of user output data * * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error. */struct type86_reply {	struct type86_hdr hdr;	struct type86_fmt2_ext fmt2;	struct CPRB cprb;	unsigned char pad[4];	/* 4 byte function code/rules block ? */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?