2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 * Author: Gary R Hook <gary.hook@amd.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
17 #include <linux/scatterlist.h>
18 #include <linux/workqueue.h>
19 #include <linux/list.h>
20 #include <crypto/aes.h>
21 #include <crypto/sha.h>
26 #if defined(CONFIG_CRYPTO_DEV_SP_CCP)
29 * ccp_present - check if a CCP device is present
31 * Returns zero if a CCP device is present, -ENODEV otherwise.
33 int ccp_present(void);
36 #define CCP_VMASK ((unsigned int)((1 << CCP_VSIZE) - 1))
37 #define CCP_VERSION(v, r) ((unsigned int)((v << CCP_VSIZE) \
41 * ccp_version - get the version of the CCP
43 * Returns a positive version number, or zero if no CCP
45 unsigned int ccp_version(void);
48 * ccp_enqueue_cmd - queue an operation for processing by the CCP
50 * @cmd: ccp_cmd struct to be processed
52 * Refer to the ccp_cmd struct below for required fields.
54 * Queue a cmd to be processed by the CCP. If queueing the cmd
55 * would exceed the defined length of the cmd queue the cmd will
56 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
57 * result in a return code of -EBUSY.
59 * The callback routine specified in the ccp_cmd struct will be
60 * called to notify the caller of completion (if the cmd was not
61 * backlogged) or advancement out of the backlog. If the cmd has
62 * advanced out of the backlog the "err" value of the callback
63 * will be -EINPROGRESS. Any other "err" value during callback is
64 * the result of the operation.
66 * The cmd has been successfully queued if:
67 * the return code is -EINPROGRESS or
68 * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
70 int ccp_enqueue_cmd(struct ccp_cmd *cmd);
72 #else /* CONFIG_CRYPTO_DEV_CCP_SP_DEV is not enabled */
74 static inline int ccp_present(void)
79 static inline unsigned int ccp_version(void)
84 static inline int ccp_enqueue_cmd(struct ccp_cmd *cmd)
89 #endif /* CONFIG_CRYPTO_DEV_SP_CCP */
92 /***** AES engine *****/
94 * ccp_aes_type - AES key size
96 * @CCP_AES_TYPE_128: 128-bit key
97 * @CCP_AES_TYPE_192: 192-bit key
98 * @CCP_AES_TYPE_256: 256-bit key
101 CCP_AES_TYPE_128 = 0,
108 * ccp_aes_mode - AES operation mode
110 * @CCP_AES_MODE_ECB: ECB mode
111 * @CCP_AES_MODE_CBC: CBC mode
112 * @CCP_AES_MODE_OFB: OFB mode
113 * @CCP_AES_MODE_CFB: CFB mode
114 * @CCP_AES_MODE_CTR: CTR mode
115 * @CCP_AES_MODE_CMAC: CMAC mode
118 CCP_AES_MODE_ECB = 0,
132 * ccp_aes_mode - AES operation mode
134 * @CCP_AES_ACTION_DECRYPT: AES decrypt operation
135 * @CCP_AES_ACTION_ENCRYPT: AES encrypt operation
137 enum ccp_aes_action {
138 CCP_AES_ACTION_DECRYPT = 0,
139 CCP_AES_ACTION_ENCRYPT,
140 CCP_AES_ACTION__LAST,
142 /* Overloaded field */
143 #define CCP_AES_GHASHAAD CCP_AES_ACTION_DECRYPT
144 #define CCP_AES_GHASHFINAL CCP_AES_ACTION_ENCRYPT
147 * struct ccp_aes_engine - CCP AES operation
148 * @type: AES operation key size
149 * @mode: AES operation mode
150 * @action: AES operation (decrypt/encrypt)
151 * @key: key to be used for this AES operation
152 * @key_len: length in bytes of key
153 * @iv: IV to be used for this AES operation
154 * @iv_len: length in bytes of iv
155 * @src: data to be used for this operation
156 * @dst: data produced by this operation
157 * @src_len: length in bytes of data used for this operation
158 * @cmac_final: indicates final operation when running in CMAC mode
159 * @cmac_key: K1/K2 key used in final CMAC operation
160 * @cmac_key_len: length in bytes of cmac_key
162 * Variables required to be set when calling ccp_enqueue_cmd():
163 * - type, mode, action, key, key_len, src, dst, src_len
164 * - iv, iv_len for any mode other than ECB
165 * - cmac_final for CMAC mode
166 * - cmac_key, cmac_key_len for CMAC mode if cmac_final is non-zero
168 * The iv variable is used as both input and output. On completion of the
169 * AES operation the new IV overwrites the old IV.
171 struct ccp_aes_engine {
172 enum ccp_aes_type type;
173 enum ccp_aes_mode mode;
174 enum ccp_aes_action action;
178 struct scatterlist *key;
179 u32 key_len; /* In bytes */
181 struct scatterlist *iv;
182 u32 iv_len; /* In bytes */
184 struct scatterlist *src, *dst;
185 u64 src_len; /* In bytes */
187 u32 cmac_final; /* Indicates final cmac cmd */
188 struct scatterlist *cmac_key; /* K1/K2 cmac key required for
190 u32 cmac_key_len; /* In bytes */
192 u32 aad_len; /* In bytes */
195 /***** XTS-AES engine *****/
197 * ccp_xts_aes_unit_size - XTS unit size
199 * @CCP_XTS_AES_UNIT_SIZE_16: Unit size of 16 bytes
200 * @CCP_XTS_AES_UNIT_SIZE_512: Unit size of 512 bytes
201 * @CCP_XTS_AES_UNIT_SIZE_1024: Unit size of 1024 bytes
202 * @CCP_XTS_AES_UNIT_SIZE_2048: Unit size of 2048 bytes
203 * @CCP_XTS_AES_UNIT_SIZE_4096: Unit size of 4096 bytes
205 enum ccp_xts_aes_unit_size {
206 CCP_XTS_AES_UNIT_SIZE_16 = 0,
207 CCP_XTS_AES_UNIT_SIZE_512,
208 CCP_XTS_AES_UNIT_SIZE_1024,
209 CCP_XTS_AES_UNIT_SIZE_2048,
210 CCP_XTS_AES_UNIT_SIZE_4096,
211 CCP_XTS_AES_UNIT_SIZE__LAST,
215 * struct ccp_xts_aes_engine - CCP XTS AES operation
216 * @action: AES operation (decrypt/encrypt)
217 * @unit_size: unit size of the XTS operation
218 * @key: key to be used for this XTS AES operation
219 * @key_len: length in bytes of key
220 * @iv: IV to be used for this XTS AES operation
221 * @iv_len: length in bytes of iv
222 * @src: data to be used for this operation
223 * @dst: data produced by this operation
224 * @src_len: length in bytes of data used for this operation
225 * @final: indicates final XTS operation
227 * Variables required to be set when calling ccp_enqueue_cmd():
228 * - action, unit_size, key, key_len, iv, iv_len, src, dst, src_len, final
230 * The iv variable is used as both input and output. On completion of the
231 * AES operation the new IV overwrites the old IV.
233 struct ccp_xts_aes_engine {
234 enum ccp_aes_type type;
235 enum ccp_aes_action action;
236 enum ccp_xts_aes_unit_size unit_size;
238 struct scatterlist *key;
239 u32 key_len; /* In bytes */
241 struct scatterlist *iv;
242 u32 iv_len; /* In bytes */
244 struct scatterlist *src, *dst;
245 u64 src_len; /* In bytes */
250 /***** SHA engine *****/
252 * ccp_sha_type - type of SHA operation
254 * @CCP_SHA_TYPE_1: SHA-1 operation
255 * @CCP_SHA_TYPE_224: SHA-224 operation
256 * @CCP_SHA_TYPE_256: SHA-256 operation
268 * struct ccp_sha_engine - CCP SHA operation
269 * @type: Type of SHA operation
270 * @ctx: current hash value
271 * @ctx_len: length in bytes of hash value
272 * @src: data to be used for this operation
273 * @src_len: length in bytes of data used for this operation
274 * @opad: data to be used for final HMAC operation
275 * @opad_len: length in bytes of data used for final HMAC operation
276 * @first: indicates first SHA operation
277 * @final: indicates final SHA operation
278 * @msg_bits: total length of the message in bits used in final SHA operation
280 * Variables required to be set when calling ccp_enqueue_cmd():
281 * - type, ctx, ctx_len, src, src_len, final
282 * - msg_bits if final is non-zero
284 * The ctx variable is used as both input and output. On completion of the
285 * SHA operation the new hash value overwrites the old hash value.
287 struct ccp_sha_engine {
288 enum ccp_sha_type type;
290 struct scatterlist *ctx;
291 u32 ctx_len; /* In bytes */
293 struct scatterlist *src;
294 u64 src_len; /* In bytes */
296 struct scatterlist *opad;
297 u32 opad_len; /* In bytes */
299 u32 first; /* Indicates first sha cmd */
300 u32 final; /* Indicates final sha cmd */
301 u64 msg_bits; /* Message length in bits required for
305 /***** 3DES engine *****/
307 CCP_DES3_MODE_ECB = 0,
314 CCP_DES3_TYPE_168 = 1,
318 enum ccp_des3_action {
319 CCP_DES3_ACTION_DECRYPT = 0,
320 CCP_DES3_ACTION_ENCRYPT,
321 CCP_DES3_ACTION__LAST,
325 * struct ccp_des3_engine - CCP SHA operation
326 * @type: Type of 3DES operation
328 * @action: 3DES operation (decrypt/encrypt)
329 * @key: key to be used for this 3DES operation
330 * @key_len: length of key (in bytes)
331 * @iv: IV to be used for this AES operation
332 * @iv_len: length in bytes of iv
333 * @src: input data to be used for this operation
334 * @src_len: length of input data used for this operation (in bytes)
335 * @dst: output data produced by this operation
337 * Variables required to be set when calling ccp_enqueue_cmd():
338 * - type, mode, action, key, key_len, src, dst, src_len
339 * - iv, iv_len for any mode other than ECB
341 * The iv variable is used as both input and output. On completion of the
342 * 3DES operation the new IV overwrites the old IV.
344 struct ccp_des3_engine {
345 enum ccp_des3_type type;
346 enum ccp_des3_mode mode;
347 enum ccp_des3_action action;
349 struct scatterlist *key;
350 u32 key_len; /* In bytes */
352 struct scatterlist *iv;
353 u32 iv_len; /* In bytes */
355 struct scatterlist *src, *dst;
356 u64 src_len; /* In bytes */
359 /***** RSA engine *****/
361 * struct ccp_rsa_engine - CCP RSA operation
362 * @key_size: length in bits of RSA key
364 * @exp_len: length in bytes of exponent
366 * @mod_len: length in bytes of modulus
367 * @src: data to be used for this operation
368 * @dst: data produced by this operation
369 * @src_len: length in bytes of data used for this operation
371 * Variables required to be set when calling ccp_enqueue_cmd():
372 * - key_size, exp, exp_len, mod, mod_len, src, dst, src_len
374 struct ccp_rsa_engine {
375 u32 key_size; /* In bits */
377 struct scatterlist *exp;
378 u32 exp_len; /* In bytes */
380 struct scatterlist *mod;
381 u32 mod_len; /* In bytes */
383 struct scatterlist *src, *dst;
384 u32 src_len; /* In bytes */
387 /***** Passthru engine *****/
389 * ccp_passthru_bitwise - type of bitwise passthru operation
391 * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
392 * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
393 * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
394 * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
395 * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
397 enum ccp_passthru_bitwise {
398 CCP_PASSTHRU_BITWISE_NOOP = 0,
399 CCP_PASSTHRU_BITWISE_AND,
400 CCP_PASSTHRU_BITWISE_OR,
401 CCP_PASSTHRU_BITWISE_XOR,
402 CCP_PASSTHRU_BITWISE_MASK,
403 CCP_PASSTHRU_BITWISE__LAST,
407 * ccp_passthru_byteswap - type of byteswap passthru operation
409 * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
410 * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
411 * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
413 enum ccp_passthru_byteswap {
414 CCP_PASSTHRU_BYTESWAP_NOOP = 0,
415 CCP_PASSTHRU_BYTESWAP_32BIT,
416 CCP_PASSTHRU_BYTESWAP_256BIT,
417 CCP_PASSTHRU_BYTESWAP__LAST,
421 * struct ccp_passthru_engine - CCP pass-through operation
422 * @bit_mod: bitwise operation to perform
423 * @byte_swap: byteswap operation to perform
424 * @mask: mask to be applied to data
425 * @mask_len: length in bytes of mask
426 * @src: data to be used for this operation
427 * @dst: data produced by this operation
428 * @src_len: length in bytes of data used for this operation
429 * @final: indicate final pass-through operation
431 * Variables required to be set when calling ccp_enqueue_cmd():
432 * - bit_mod, byte_swap, src, dst, src_len
433 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
435 struct ccp_passthru_engine {
436 enum ccp_passthru_bitwise bit_mod;
437 enum ccp_passthru_byteswap byte_swap;
439 struct scatterlist *mask;
440 u32 mask_len; /* In bytes */
442 struct scatterlist *src, *dst;
443 u64 src_len; /* In bytes */
449 * struct ccp_passthru_nomap_engine - CCP pass-through operation
450 * without performing DMA mapping
451 * @bit_mod: bitwise operation to perform
452 * @byte_swap: byteswap operation to perform
453 * @mask: mask to be applied to data
454 * @mask_len: length in bytes of mask
455 * @src: data to be used for this operation
456 * @dst: data produced by this operation
457 * @src_len: length in bytes of data used for this operation
458 * @final: indicate final pass-through operation
460 * Variables required to be set when calling ccp_enqueue_cmd():
461 * - bit_mod, byte_swap, src, dst, src_len
462 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
464 struct ccp_passthru_nomap_engine {
465 enum ccp_passthru_bitwise bit_mod;
466 enum ccp_passthru_byteswap byte_swap;
469 u32 mask_len; /* In bytes */
471 dma_addr_t src_dma, dst_dma;
472 u64 src_len; /* In bytes */
477 /***** ECC engine *****/
478 #define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
479 #define CCP_ECC_MAX_OPERANDS 6
480 #define CCP_ECC_MAX_OUTPUTS 3
483 * ccp_ecc_function - type of ECC function
485 * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication
486 * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition
487 * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse
488 * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition
489 * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication
490 * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling
492 enum ccp_ecc_function {
493 CCP_ECC_FUNCTION_MMUL_384BIT = 0,
494 CCP_ECC_FUNCTION_MADD_384BIT,
495 CCP_ECC_FUNCTION_MINV_384BIT,
496 CCP_ECC_FUNCTION_PADD_384BIT,
497 CCP_ECC_FUNCTION_PMUL_384BIT,
498 CCP_ECC_FUNCTION_PDBL_384BIT,
502 * struct ccp_ecc_modular_math - CCP ECC modular math parameters
503 * @operand_1: first operand for the modular math operation
504 * @operand_1_len: length of the first operand
505 * @operand_2: second operand for the modular math operation
506 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
507 * @operand_2_len: length of the second operand
508 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
509 * @result: result of the modular math operation
510 * @result_len: length of the supplied result buffer
512 struct ccp_ecc_modular_math {
513 struct scatterlist *operand_1;
514 unsigned int operand_1_len; /* In bytes */
516 struct scatterlist *operand_2;
517 unsigned int operand_2_len; /* In bytes */
519 struct scatterlist *result;
520 unsigned int result_len; /* In bytes */
524 * struct ccp_ecc_point - CCP ECC point definition
525 * @x: the x coordinate of the ECC point
526 * @x_len: the length of the x coordinate
527 * @y: the y coordinate of the ECC point
528 * @y_len: the length of the y coordinate
530 struct ccp_ecc_point {
531 struct scatterlist *x;
532 unsigned int x_len; /* In bytes */
534 struct scatterlist *y;
535 unsigned int y_len; /* In bytes */
539 * struct ccp_ecc_point_math - CCP ECC point math parameters
540 * @point_1: the first point of the ECC point math operation
541 * @point_2: the second point of the ECC point math operation
542 * (only used for CCP_ECC_FUNCTION_PADD_384BIT)
543 * @domain_a: the a parameter of the ECC curve
544 * @domain_a_len: the length of the a parameter
545 * @scalar: the scalar parameter for the point match operation
546 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
547 * @scalar_len: the length of the scalar parameter
548 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
549 * @result: the point resulting from the point math operation
551 struct ccp_ecc_point_math {
552 struct ccp_ecc_point point_1;
553 struct ccp_ecc_point point_2;
555 struct scatterlist *domain_a;
556 unsigned int domain_a_len; /* In bytes */
558 struct scatterlist *scalar;
559 unsigned int scalar_len; /* In bytes */
561 struct ccp_ecc_point result;
565 * struct ccp_ecc_engine - CCP ECC operation
566 * @function: ECC function to perform
568 * @mod_len: length in bytes of modulus
569 * @mm: module math parameters
570 * @pm: point math parameters
571 * @ecc_result: result of the ECC operation
573 * Variables required to be set when calling ccp_enqueue_cmd():
574 * - function, mod, mod_len
575 * - operand, operand_len, operand_count, output, output_len, output_count
578 struct ccp_ecc_engine {
579 enum ccp_ecc_function function;
581 struct scatterlist *mod;
582 u32 mod_len; /* In bytes */
585 struct ccp_ecc_modular_math mm;
586 struct ccp_ecc_point_math pm;
594 * ccp_engine - CCP operation identifiers
596 * @CCP_ENGINE_AES: AES operation
597 * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
598 * @CCP_ENGINE_RSVD1: unused
599 * @CCP_ENGINE_SHA: SHA operation
600 * @CCP_ENGINE_RSA: RSA operation
601 * @CCP_ENGINE_PASSTHRU: pass-through operation
602 * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
603 * @CCP_ENGINE_ECC: ECC operation
607 CCP_ENGINE_XTS_AES_128,
612 CCP_ENGINE_ZLIB_DECOMPRESS,
617 /* Flag values for flags member of ccp_cmd */
618 #define CCP_CMD_MAY_BACKLOG 0x00000001
619 #define CCP_CMD_PASSTHRU_NO_DMA_MAP 0x00000002
622 * struct ccp_cmd - CCP operation request
623 * @entry: list element (ccp driver use only)
624 * @work: work element used for callbacks (ccp driver use only)
625 * @ccp: CCP device to be run on
626 * @ret: operation return code (ccp driver use only)
627 * @flags: cmd processing flags
628 * @engine: CCP operation to perform
629 * @engine_error: CCP engine return code
630 * @u: engine specific structures, refer to specific engine struct below
631 * @callback: operation completion callback function
632 * @data: parameter value to be supplied to the callback function
634 * Variables required to be set when calling ccp_enqueue_cmd():
636 * - See the operation structures below for what is required for each
640 /* The list_head, work_struct, ccp and ret variables are for use
641 * by the CCP driver only.
643 struct list_head entry;
644 struct work_struct work;
645 struct ccp_device *ccp;
650 enum ccp_engine engine;
654 struct ccp_aes_engine aes;
655 struct ccp_xts_aes_engine xts;
656 struct ccp_des3_engine des3;
657 struct ccp_sha_engine sha;
658 struct ccp_rsa_engine rsa;
659 struct ccp_passthru_engine passthru;
660 struct ccp_passthru_nomap_engine passthru_nomap;
661 struct ccp_ecc_engine ecc;
664 /* Completion callback support */
665 void (*callback)(void *data, int err);