brintos

brintos / linux-shallow public Read only

0
0
Text · 18.1 KiB · 868924d Raw
668 lines · c
1/* SPDX-License-Identifier: GPL-2.0-only */2/*3 * AMD Cryptographic Coprocessor (CCP) driver4 *5 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.6 *7 * Author: Tom Lendacky <thomas.lendacky@amd.com>8 * Author: Gary R Hook <gary.hook@amd.com>9 */10 11#ifndef __CCP_H__12#define __CCP_H__13 14#include <linux/scatterlist.h>15#include <linux/workqueue.h>16#include <linux/list.h>17#include <crypto/aes.h>18#include <crypto/sha1.h>19#include <crypto/sha2.h>20 21struct ccp_device;22struct ccp_cmd;23 24#if defined(CONFIG_CRYPTO_DEV_SP_CCP)25 26/**27 * ccp_present - check if a CCP device is present28 *29 * Returns zero if a CCP device is present, -ENODEV otherwise.30 */31int ccp_present(void);32 33#define	CCP_VSIZE 1634#define	CCP_VMASK		((unsigned int)((1 << CCP_VSIZE) - 1))35#define	CCP_VERSION(v, r)	((unsigned int)((v << CCP_VSIZE) \36					       | (r & CCP_VMASK)))37 38/**39 * ccp_version - get the version of the CCP40 *41 * Returns a positive version number, or zero if no CCP42 */43unsigned int ccp_version(void);44 45/**46 * ccp_enqueue_cmd - queue an operation for processing by the CCP47 *48 * @cmd: ccp_cmd struct to be processed49 *50 * Refer to the ccp_cmd struct below for required fields.51 *52 * Queue a cmd to be processed by the CCP. If queueing the cmd53 * would exceed the defined length of the cmd queue the cmd will54 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will55 * result in a return code of -EBUSY.56 *57 * The callback routine specified in the ccp_cmd struct will be58 * called to notify the caller of completion (if the cmd was not59 * backlogged) or advancement out of the backlog. If the cmd has60 * advanced out of the backlog the "err" value of the callback61 * will be -EINPROGRESS. Any other "err" value during callback is62 * the result of the operation.63 *64 * The cmd has been successfully queued if:65 *   the return code is -EINPROGRESS or66 *   the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set67 */68int ccp_enqueue_cmd(struct ccp_cmd *cmd);69 70#else /* CONFIG_CRYPTO_DEV_CCP_SP_DEV is not enabled */71 72static inline int ccp_present(void)73{74	return -ENODEV;75}76 77static inline unsigned int ccp_version(void)78{79	return 0;80}81 82static inline int ccp_enqueue_cmd(struct ccp_cmd *cmd)83{84	return -ENODEV;85}86 87#endif /* CONFIG_CRYPTO_DEV_SP_CCP */88 89 90/***** AES engine *****/91/**92 * ccp_aes_type - AES key size93 *94 * @CCP_AES_TYPE_128: 128-bit key95 * @CCP_AES_TYPE_192: 192-bit key96 * @CCP_AES_TYPE_256: 256-bit key97 */98enum ccp_aes_type {99	CCP_AES_TYPE_128 = 0,100	CCP_AES_TYPE_192,101	CCP_AES_TYPE_256,102	CCP_AES_TYPE__LAST,103};104 105/**106 * ccp_aes_mode - AES operation mode107 *108 * @CCP_AES_MODE_ECB: ECB mode109 * @CCP_AES_MODE_CBC: CBC mode110 * @CCP_AES_MODE_OFB: OFB mode111 * @CCP_AES_MODE_CFB: CFB mode112 * @CCP_AES_MODE_CTR: CTR mode113 * @CCP_AES_MODE_CMAC: CMAC mode114 */115enum ccp_aes_mode {116	CCP_AES_MODE_ECB = 0,117	CCP_AES_MODE_CBC,118	CCP_AES_MODE_OFB,119	CCP_AES_MODE_CFB,120	CCP_AES_MODE_CTR,121	CCP_AES_MODE_CMAC,122	CCP_AES_MODE_GHASH,123	CCP_AES_MODE_GCTR,124	CCP_AES_MODE_GCM,125	CCP_AES_MODE_GMAC,126	CCP_AES_MODE__LAST,127};128 129/**130 * ccp_aes_mode - AES operation mode131 *132 * @CCP_AES_ACTION_DECRYPT: AES decrypt operation133 * @CCP_AES_ACTION_ENCRYPT: AES encrypt operation134 */135enum ccp_aes_action {136	CCP_AES_ACTION_DECRYPT = 0,137	CCP_AES_ACTION_ENCRYPT,138	CCP_AES_ACTION__LAST,139};140/* Overloaded field */141#define	CCP_AES_GHASHAAD	CCP_AES_ACTION_DECRYPT142#define	CCP_AES_GHASHFINAL	CCP_AES_ACTION_ENCRYPT143 144/**145 * struct ccp_aes_engine - CCP AES operation146 * @type: AES operation key size147 * @mode: AES operation mode148 * @action: AES operation (decrypt/encrypt)149 * @key: key to be used for this AES operation150 * @key_len: length in bytes of key151 * @iv: IV to be used for this AES operation152 * @iv_len: length in bytes of iv153 * @src: data to be used for this operation154 * @dst: data produced by this operation155 * @src_len: length in bytes of data used for this operation156 * @cmac_final: indicates final operation when running in CMAC mode157 * @cmac_key: K1/K2 key used in final CMAC operation158 * @cmac_key_len: length in bytes of cmac_key159 *160 * Variables required to be set when calling ccp_enqueue_cmd():161 *   - type, mode, action, key, key_len, src, dst, src_len162 *   - iv, iv_len for any mode other than ECB163 *   - cmac_final for CMAC mode164 *   - cmac_key, cmac_key_len for CMAC mode if cmac_final is non-zero165 *166 * The iv variable is used as both input and output. On completion of the167 * AES operation the new IV overwrites the old IV.168 */169struct ccp_aes_engine {170	enum ccp_aes_type type;171	enum ccp_aes_mode mode;172	enum ccp_aes_action action;173 174	u32 authsize;175 176	struct scatterlist *key;177	u32 key_len;		/* In bytes */178 179	struct scatterlist *iv;180	u32 iv_len;		/* In bytes */181 182	struct scatterlist *src, *dst;183	u64 src_len;		/* In bytes */184 185	u32 cmac_final;		/* Indicates final cmac cmd */186	struct scatterlist *cmac_key;	/* K1/K2 cmac key required for187					 * final cmac cmd */188	u32 cmac_key_len;	/* In bytes */189 190	u32 aad_len;		/* In bytes */191};192 193/***** XTS-AES engine *****/194/**195 * ccp_xts_aes_unit_size - XTS unit size196 *197 * @CCP_XTS_AES_UNIT_SIZE_16: Unit size of 16 bytes198 * @CCP_XTS_AES_UNIT_SIZE_512: Unit size of 512 bytes199 * @CCP_XTS_AES_UNIT_SIZE_1024: Unit size of 1024 bytes200 * @CCP_XTS_AES_UNIT_SIZE_2048: Unit size of 2048 bytes201 * @CCP_XTS_AES_UNIT_SIZE_4096: Unit size of 4096 bytes202 */203enum ccp_xts_aes_unit_size {204	CCP_XTS_AES_UNIT_SIZE_16 = 0,205	CCP_XTS_AES_UNIT_SIZE_512,206	CCP_XTS_AES_UNIT_SIZE_1024,207	CCP_XTS_AES_UNIT_SIZE_2048,208	CCP_XTS_AES_UNIT_SIZE_4096,209	CCP_XTS_AES_UNIT_SIZE__LAST,210};211 212/**213 * struct ccp_xts_aes_engine - CCP XTS AES operation214 * @action: AES operation (decrypt/encrypt)215 * @unit_size: unit size of the XTS operation216 * @key: key to be used for this XTS AES operation217 * @key_len: length in bytes of key218 * @iv: IV to be used for this XTS AES operation219 * @iv_len: length in bytes of iv220 * @src: data to be used for this operation221 * @dst: data produced by this operation222 * @src_len: length in bytes of data used for this operation223 * @final: indicates final XTS operation224 *225 * Variables required to be set when calling ccp_enqueue_cmd():226 *   - action, unit_size, key, key_len, iv, iv_len, src, dst, src_len, final227 *228 * The iv variable is used as both input and output. On completion of the229 * AES operation the new IV overwrites the old IV.230 */231struct ccp_xts_aes_engine {232	enum ccp_aes_type type;233	enum ccp_aes_action action;234	enum ccp_xts_aes_unit_size unit_size;235 236	struct scatterlist *key;237	u32 key_len;		/* In bytes */238 239	struct scatterlist *iv;240	u32 iv_len;		/* In bytes */241 242	struct scatterlist *src, *dst;243	u64 src_len;		/* In bytes */244 245	u32 final;246};247 248/***** SHA engine *****/249/**250 * ccp_sha_type - type of SHA operation251 *252 * @CCP_SHA_TYPE_1: SHA-1 operation253 * @CCP_SHA_TYPE_224: SHA-224 operation254 * @CCP_SHA_TYPE_256: SHA-256 operation255 */256enum ccp_sha_type {257	CCP_SHA_TYPE_1 = 1,258	CCP_SHA_TYPE_224,259	CCP_SHA_TYPE_256,260	CCP_SHA_TYPE_384,261	CCP_SHA_TYPE_512,262	CCP_SHA_TYPE__LAST,263};264 265/**266 * struct ccp_sha_engine - CCP SHA operation267 * @type: Type of SHA operation268 * @ctx: current hash value269 * @ctx_len: length in bytes of hash value270 * @src: data to be used for this operation271 * @src_len: length in bytes of data used for this operation272 * @opad: data to be used for final HMAC operation273 * @opad_len: length in bytes of data used for final HMAC operation274 * @first: indicates first SHA operation275 * @final: indicates final SHA operation276 * @msg_bits: total length of the message in bits used in final SHA operation277 *278 * Variables required to be set when calling ccp_enqueue_cmd():279 *   - type, ctx, ctx_len, src, src_len, final280 *   - msg_bits if final is non-zero281 *282 * The ctx variable is used as both input and output. On completion of the283 * SHA operation the new hash value overwrites the old hash value.284 */285struct ccp_sha_engine {286	enum ccp_sha_type type;287 288	struct scatterlist *ctx;289	u32 ctx_len;		/* In bytes */290 291	struct scatterlist *src;292	u64 src_len;		/* In bytes */293 294	struct scatterlist *opad;295	u32 opad_len;		/* In bytes */296 297	u32 first;		/* Indicates first sha cmd */298	u32 final;		/* Indicates final sha cmd */299	u64 msg_bits;		/* Message length in bits required for300				 * final sha cmd */301};302 303/***** 3DES engine *****/304enum ccp_des3_mode {305	CCP_DES3_MODE_ECB = 0,306	CCP_DES3_MODE_CBC,307	CCP_DES3_MODE_CFB,308	CCP_DES3_MODE__LAST,309};310 311enum ccp_des3_type {312	CCP_DES3_TYPE_168 = 1,313	CCP_DES3_TYPE__LAST,314	};315 316enum ccp_des3_action {317	CCP_DES3_ACTION_DECRYPT = 0,318	CCP_DES3_ACTION_ENCRYPT,319	CCP_DES3_ACTION__LAST,320};321 322/**323 * struct ccp_des3_engine - CCP SHA operation324 * @type: Type of 3DES operation325 * @mode: cipher mode326 * @action: 3DES operation (decrypt/encrypt)327 * @key: key to be used for this 3DES operation328 * @key_len: length of key (in bytes)329 * @iv: IV to be used for this AES operation330 * @iv_len: length in bytes of iv331 * @src: input data to be used for this operation332 * @src_len: length of input data used for this operation (in bytes)333 * @dst: output data produced by this operation334 *335 * Variables required to be set when calling ccp_enqueue_cmd():336 *   - type, mode, action, key, key_len, src, dst, src_len337 *   - iv, iv_len for any mode other than ECB338 *339 * The iv variable is used as both input and output. On completion of the340 * 3DES operation the new IV overwrites the old IV.341 */342struct ccp_des3_engine {343	enum ccp_des3_type type;344	enum ccp_des3_mode mode;345	enum ccp_des3_action action;346 347	struct scatterlist *key;348	u32 key_len;	    /* In bytes */349 350	struct scatterlist *iv;351	u32 iv_len;	     /* In bytes */352 353	struct scatterlist *src, *dst;354	u64 src_len;	    /* In bytes */355};356 357/***** RSA engine *****/358/**359 * struct ccp_rsa_engine - CCP RSA operation360 * @key_size: length in bits of RSA key361 * @exp: RSA exponent362 * @exp_len: length in bytes of exponent363 * @mod: RSA modulus364 * @mod_len: length in bytes of modulus365 * @src: data to be used for this operation366 * @dst: data produced by this operation367 * @src_len: length in bytes of data used for this operation368 *369 * Variables required to be set when calling ccp_enqueue_cmd():370 *   - key_size, exp, exp_len, mod, mod_len, src, dst, src_len371 */372struct ccp_rsa_engine {373	u32 key_size;		/* In bits */374 375	struct scatterlist *exp;376	u32 exp_len;		/* In bytes */377 378	struct scatterlist *mod;379	u32 mod_len;		/* In bytes */380 381	struct scatterlist *src, *dst;382	u32 src_len;		/* In bytes */383};384 385/***** Passthru engine *****/386/**387 * ccp_passthru_bitwise - type of bitwise passthru operation388 *389 * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed390 * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask391 * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask392 * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask393 * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask394 */395enum ccp_passthru_bitwise {396	CCP_PASSTHRU_BITWISE_NOOP = 0,397	CCP_PASSTHRU_BITWISE_AND,398	CCP_PASSTHRU_BITWISE_OR,399	CCP_PASSTHRU_BITWISE_XOR,400	CCP_PASSTHRU_BITWISE_MASK,401	CCP_PASSTHRU_BITWISE__LAST,402};403 404/**405 * ccp_passthru_byteswap - type of byteswap passthru operation406 *407 * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed408 * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words409 * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words410 */411enum ccp_passthru_byteswap {412	CCP_PASSTHRU_BYTESWAP_NOOP = 0,413	CCP_PASSTHRU_BYTESWAP_32BIT,414	CCP_PASSTHRU_BYTESWAP_256BIT,415	CCP_PASSTHRU_BYTESWAP__LAST,416};417 418/**419 * struct ccp_passthru_engine - CCP pass-through operation420 * @bit_mod: bitwise operation to perform421 * @byte_swap: byteswap operation to perform422 * @mask: mask to be applied to data423 * @mask_len: length in bytes of mask424 * @src: data to be used for this operation425 * @dst: data produced by this operation426 * @src_len: length in bytes of data used for this operation427 * @final: indicate final pass-through operation428 *429 * Variables required to be set when calling ccp_enqueue_cmd():430 *   - bit_mod, byte_swap, src, dst, src_len431 *   - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP432 */433struct ccp_passthru_engine {434	enum ccp_passthru_bitwise bit_mod;435	enum ccp_passthru_byteswap byte_swap;436 437	struct scatterlist *mask;438	u32 mask_len;		/* In bytes */439 440	struct scatterlist *src, *dst;441	u64 src_len;		/* In bytes */442 443	u32 final;444};445 446/**447 * struct ccp_passthru_nomap_engine - CCP pass-through operation448 *   without performing DMA mapping449 * @bit_mod: bitwise operation to perform450 * @byte_swap: byteswap operation to perform451 * @mask: mask to be applied to data452 * @mask_len: length in bytes of mask453 * @src: data to be used for this operation454 * @dst: data produced by this operation455 * @src_len: length in bytes of data used for this operation456 * @final: indicate final pass-through operation457 *458 * Variables required to be set when calling ccp_enqueue_cmd():459 *   - bit_mod, byte_swap, src, dst, src_len460 *   - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP461 */462struct ccp_passthru_nomap_engine {463	enum ccp_passthru_bitwise bit_mod;464	enum ccp_passthru_byteswap byte_swap;465 466	dma_addr_t mask;467	u32 mask_len;		/* In bytes */468 469	dma_addr_t src_dma, dst_dma;470	u64 src_len;		/* In bytes */471 472	u32 final;473};474 475/***** ECC engine *****/476#define CCP_ECC_MODULUS_BYTES	48	/* 384-bits */477#define CCP_ECC_MAX_OPERANDS	6478#define CCP_ECC_MAX_OUTPUTS	3479 480/**481 * ccp_ecc_function - type of ECC function482 *483 * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication484 * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition485 * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse486 * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition487 * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication488 * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling489 */490enum ccp_ecc_function {491	CCP_ECC_FUNCTION_MMUL_384BIT = 0,492	CCP_ECC_FUNCTION_MADD_384BIT,493	CCP_ECC_FUNCTION_MINV_384BIT,494	CCP_ECC_FUNCTION_PADD_384BIT,495	CCP_ECC_FUNCTION_PMUL_384BIT,496	CCP_ECC_FUNCTION_PDBL_384BIT,497};498 499/**500 * struct ccp_ecc_modular_math - CCP ECC modular math parameters501 * @operand_1: first operand for the modular math operation502 * @operand_1_len: length of the first operand503 * @operand_2: second operand for the modular math operation504 *	       (not used for CCP_ECC_FUNCTION_MINV_384BIT)505 * @operand_2_len: length of the second operand506 *	       (not used for CCP_ECC_FUNCTION_MINV_384BIT)507 * @result: result of the modular math operation508 * @result_len: length of the supplied result buffer509 */510struct ccp_ecc_modular_math {511	struct scatterlist *operand_1;512	unsigned int operand_1_len;	/* In bytes */513 514	struct scatterlist *operand_2;515	unsigned int operand_2_len;	/* In bytes */516 517	struct scatterlist *result;518	unsigned int result_len;	/* In bytes */519};520 521/**522 * struct ccp_ecc_point - CCP ECC point definition523 * @x: the x coordinate of the ECC point524 * @x_len: the length of the x coordinate525 * @y: the y coordinate of the ECC point526 * @y_len: the length of the y coordinate527 */528struct ccp_ecc_point {529	struct scatterlist *x;530	unsigned int x_len;	/* In bytes */531 532	struct scatterlist *y;533	unsigned int y_len;	/* In bytes */534};535 536/**537 * struct ccp_ecc_point_math - CCP ECC point math parameters538 * @point_1: the first point of the ECC point math operation539 * @point_2: the second point of the ECC point math operation540 *	     (only used for CCP_ECC_FUNCTION_PADD_384BIT)541 * @domain_a: the a parameter of the ECC curve542 * @domain_a_len: the length of the a parameter543 * @scalar: the scalar parameter for the point match operation544 *	    (only used for CCP_ECC_FUNCTION_PMUL_384BIT)545 * @scalar_len: the length of the scalar parameter546 *		(only used for CCP_ECC_FUNCTION_PMUL_384BIT)547 * @result: the point resulting from the point math operation548 */549struct ccp_ecc_point_math {550	struct ccp_ecc_point point_1;551	struct ccp_ecc_point point_2;552 553	struct scatterlist *domain_a;554	unsigned int domain_a_len;	/* In bytes */555 556	struct scatterlist *scalar;557	unsigned int scalar_len;	/* In bytes */558 559	struct ccp_ecc_point result;560};561 562/**563 * struct ccp_ecc_engine - CCP ECC operation564 * @function: ECC function to perform565 * @mod: ECC modulus566 * @mod_len: length in bytes of modulus567 * @mm: module math parameters568 * @pm: point math parameters569 * @ecc_result: result of the ECC operation570 *571 * Variables required to be set when calling ccp_enqueue_cmd():572 *   - function, mod, mod_len573 *   - operand, operand_len, operand_count, output, output_len, output_count574 *   - ecc_result575 */576struct ccp_ecc_engine {577	enum ccp_ecc_function function;578 579	struct scatterlist *mod;580	u32 mod_len;		/* In bytes */581 582	union {583		struct ccp_ecc_modular_math mm;584		struct ccp_ecc_point_math pm;585	} u;586 587	u16 ecc_result;588};589 590 591/**592 * ccp_engine - CCP operation identifiers593 *594 * @CCP_ENGINE_AES: AES operation595 * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation596 * @CCP_ENGINE_RSVD1: unused597 * @CCP_ENGINE_SHA: SHA operation598 * @CCP_ENGINE_RSA: RSA operation599 * @CCP_ENGINE_PASSTHRU: pass-through operation600 * @CCP_ENGINE_ZLIB_DECOMPRESS: unused601 * @CCP_ENGINE_ECC: ECC operation602 */603enum ccp_engine {604	CCP_ENGINE_AES = 0,605	CCP_ENGINE_XTS_AES_128,606	CCP_ENGINE_DES3,607	CCP_ENGINE_SHA,608	CCP_ENGINE_RSA,609	CCP_ENGINE_PASSTHRU,610	CCP_ENGINE_ZLIB_DECOMPRESS,611	CCP_ENGINE_ECC,612	CCP_ENGINE__LAST,613};614 615/* Flag values for flags member of ccp_cmd */616#define CCP_CMD_MAY_BACKLOG		0x00000001617#define CCP_CMD_PASSTHRU_NO_DMA_MAP	0x00000002618 619/**620 * struct ccp_cmd - CCP operation request621 * @entry: list element (ccp driver use only)622 * @work: work element used for callbacks (ccp driver use only)623 * @ccp: CCP device to be run on624 * @ret: operation return code (ccp driver use only)625 * @flags: cmd processing flags626 * @engine: CCP operation to perform627 * @engine_error: CCP engine return code628 * @u: engine specific structures, refer to specific engine struct below629 * @callback: operation completion callback function630 * @data: parameter value to be supplied to the callback function631 *632 * Variables required to be set when calling ccp_enqueue_cmd():633 *   - engine, callback634 *   - See the operation structures below for what is required for each635 *     operation.636 */637struct ccp_cmd {638	/* The list_head, work_struct, ccp and ret variables are for use639	 * by the CCP driver only.640	 */641	struct list_head entry;642	struct work_struct work;643	struct ccp_device *ccp;644	int ret;645 646	u32 flags;647 648	enum ccp_engine engine;649	u32 engine_error;650 651	union {652		struct ccp_aes_engine aes;653		struct ccp_xts_aes_engine xts;654		struct ccp_des3_engine des3;655		struct ccp_sha_engine sha;656		struct ccp_rsa_engine rsa;657		struct ccp_passthru_engine passthru;658		struct ccp_passthru_nomap_engine passthru_nomap;659		struct ccp_ecc_engine ecc;660	} u;661 662	/* Completion callback support */663	void (*callback)(void *data, int err);664	void *data;665};666 667#endif668