2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013,2016 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>
27 #if defined(CONFIG_CRYPTO_DEV_CCP_DD) || \
28 defined(CONFIG_CRYPTO_DEV_CCP_DD_MODULE)
31 * ccp_present - check if a CCP device is present
33 * Returns zero if a CCP device is present, -ENODEV otherwise.
35 int ccp_present(void);
38 #define CCP_VMASK ((unsigned int)((1 << CCP_VSIZE) - 1))
39 #define CCP_VERSION(v, r) ((unsigned int)((v << CCP_VSIZE) \
43 * ccp_version - get the version of the CCP
45 * Returns a positive version number, or zero if no CCP
47 unsigned int ccp_version(void);
50 * ccp_enqueue_cmd - queue an operation for processing by the CCP
52 * @cmd: ccp_cmd struct to be processed
54 * Refer to the ccp_cmd struct below for required fields.
56 * Queue a cmd to be processed by the CCP. If queueing the cmd
57 * would exceed the defined length of the cmd queue the cmd will
58 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
59 * result in a return code of -EBUSY.
61 * The callback routine specified in the ccp_cmd struct will be
62 * called to notify the caller of completion (if the cmd was not
63 * backlogged) or advancement out of the backlog. If the cmd has
64 * advanced out of the backlog the "err" value of the callback
65 * will be -EINPROGRESS. Any other "err" value during callback is
66 * the result of the operation.
68 * The cmd has been successfully queued if:
69 * the return code is -EINPROGRESS or
70 * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
72 int ccp_enqueue_cmd(struct ccp_cmd
*cmd
);
74 #else /* CONFIG_CRYPTO_DEV_CCP_DD is not enabled */
76 static inline int ccp_present(void)
81 static inline unsigned int ccp_version(void)
86 static inline int ccp_enqueue_cmd(struct ccp_cmd
*cmd
)
91 #endif /* CONFIG_CRYPTO_DEV_CCP_DD */
94 /***** AES engine *****/
96 * ccp_aes_type - AES key size
98 * @CCP_AES_TYPE_128: 128-bit key
99 * @CCP_AES_TYPE_192: 192-bit key
100 * @CCP_AES_TYPE_256: 256-bit key
103 CCP_AES_TYPE_128
= 0,
110 * ccp_aes_mode - AES operation mode
112 * @CCP_AES_MODE_ECB: ECB mode
113 * @CCP_AES_MODE_CBC: CBC mode
114 * @CCP_AES_MODE_OFB: OFB mode
115 * @CCP_AES_MODE_CFB: CFB mode
116 * @CCP_AES_MODE_CTR: CTR mode
117 * @CCP_AES_MODE_CMAC: CMAC mode
120 CCP_AES_MODE_ECB
= 0,
130 * ccp_aes_mode - AES operation mode
132 * @CCP_AES_ACTION_DECRYPT: AES decrypt operation
133 * @CCP_AES_ACTION_ENCRYPT: AES encrypt operation
135 enum ccp_aes_action
{
136 CCP_AES_ACTION_DECRYPT
= 0,
137 CCP_AES_ACTION_ENCRYPT
,
138 CCP_AES_ACTION__LAST
,
142 * struct ccp_aes_engine - CCP AES operation
143 * @type: AES operation key size
144 * @mode: AES operation mode
145 * @action: AES operation (decrypt/encrypt)
146 * @key: key to be used for this AES operation
147 * @key_len: length in bytes of key
148 * @iv: IV to be used for this AES operation
149 * @iv_len: length in bytes of iv
150 * @src: data to be used for this operation
151 * @dst: data produced by this operation
152 * @src_len: length in bytes of data used for this operation
153 * @cmac_final: indicates final operation when running in CMAC mode
154 * @cmac_key: K1/K2 key used in final CMAC operation
155 * @cmac_key_len: length in bytes of cmac_key
157 * Variables required to be set when calling ccp_enqueue_cmd():
158 * - type, mode, action, key, key_len, src, dst, src_len
159 * - iv, iv_len for any mode other than ECB
160 * - cmac_final for CMAC mode
161 * - cmac_key, cmac_key_len for CMAC mode if cmac_final is non-zero
163 * The iv variable is used as both input and output. On completion of the
164 * AES operation the new IV overwrites the old IV.
166 struct ccp_aes_engine
{
167 enum ccp_aes_type type
;
168 enum ccp_aes_mode mode
;
169 enum ccp_aes_action action
;
171 struct scatterlist
*key
;
172 u32 key_len
; /* In bytes */
174 struct scatterlist
*iv
;
175 u32 iv_len
; /* In bytes */
177 struct scatterlist
*src
, *dst
;
178 u64 src_len
; /* In bytes */
180 u32 cmac_final
; /* Indicates final cmac cmd */
181 struct scatterlist
*cmac_key
; /* K1/K2 cmac key required for
183 u32 cmac_key_len
; /* In bytes */
186 /***** XTS-AES engine *****/
188 * ccp_xts_aes_unit_size - XTS unit size
190 * @CCP_XTS_AES_UNIT_SIZE_16: Unit size of 16 bytes
191 * @CCP_XTS_AES_UNIT_SIZE_512: Unit size of 512 bytes
192 * @CCP_XTS_AES_UNIT_SIZE_1024: Unit size of 1024 bytes
193 * @CCP_XTS_AES_UNIT_SIZE_2048: Unit size of 2048 bytes
194 * @CCP_XTS_AES_UNIT_SIZE_4096: Unit size of 4096 bytes
196 enum ccp_xts_aes_unit_size
{
197 CCP_XTS_AES_UNIT_SIZE_16
= 0,
198 CCP_XTS_AES_UNIT_SIZE_512
,
199 CCP_XTS_AES_UNIT_SIZE_1024
,
200 CCP_XTS_AES_UNIT_SIZE_2048
,
201 CCP_XTS_AES_UNIT_SIZE_4096
,
202 CCP_XTS_AES_UNIT_SIZE__LAST
,
206 * struct ccp_xts_aes_engine - CCP XTS AES operation
207 * @action: AES operation (decrypt/encrypt)
208 * @unit_size: unit size of the XTS operation
209 * @key: key to be used for this XTS AES operation
210 * @key_len: length in bytes of key
211 * @iv: IV to be used for this XTS AES operation
212 * @iv_len: length in bytes of iv
213 * @src: data to be used for this operation
214 * @dst: data produced by this operation
215 * @src_len: length in bytes of data used for this operation
216 * @final: indicates final XTS operation
218 * Variables required to be set when calling ccp_enqueue_cmd():
219 * - action, unit_size, key, key_len, iv, iv_len, src, dst, src_len, final
221 * The iv variable is used as both input and output. On completion of the
222 * AES operation the new IV overwrites the old IV.
224 struct ccp_xts_aes_engine
{
225 enum ccp_aes_action action
;
226 enum ccp_xts_aes_unit_size unit_size
;
228 struct scatterlist
*key
;
229 u32 key_len
; /* In bytes */
231 struct scatterlist
*iv
;
232 u32 iv_len
; /* In bytes */
234 struct scatterlist
*src
, *dst
;
235 u64 src_len
; /* In bytes */
240 /***** SHA engine *****/
242 * ccp_sha_type - type of SHA operation
244 * @CCP_SHA_TYPE_1: SHA-1 operation
245 * @CCP_SHA_TYPE_224: SHA-224 operation
246 * @CCP_SHA_TYPE_256: SHA-256 operation
256 * struct ccp_sha_engine - CCP SHA operation
257 * @type: Type of SHA operation
258 * @ctx: current hash value
259 * @ctx_len: length in bytes of hash value
260 * @src: data to be used for this operation
261 * @src_len: length in bytes of data used for this operation
262 * @opad: data to be used for final HMAC operation
263 * @opad_len: length in bytes of data used for final HMAC operation
264 * @first: indicates first SHA operation
265 * @final: indicates final SHA operation
266 * @msg_bits: total length of the message in bits used in final SHA operation
268 * Variables required to be set when calling ccp_enqueue_cmd():
269 * - type, ctx, ctx_len, src, src_len, final
270 * - msg_bits if final is non-zero
272 * The ctx variable is used as both input and output. On completion of the
273 * SHA operation the new hash value overwrites the old hash value.
275 struct ccp_sha_engine
{
276 enum ccp_sha_type type
;
278 struct scatterlist
*ctx
;
279 u32 ctx_len
; /* In bytes */
281 struct scatterlist
*src
;
282 u64 src_len
; /* In bytes */
284 struct scatterlist
*opad
;
285 u32 opad_len
; /* In bytes */
287 u32 first
; /* Indicates first sha cmd */
288 u32 final
; /* Indicates final sha cmd */
289 u64 msg_bits
; /* Message length in bits required for
293 /***** RSA engine *****/
295 * struct ccp_rsa_engine - CCP RSA operation
296 * @key_size: length in bits of RSA key
298 * @exp_len: length in bytes of exponent
300 * @mod_len: length in bytes of modulus
301 * @src: data to be used for this operation
302 * @dst: data produced by this operation
303 * @src_len: length in bytes of data used for this operation
305 * Variables required to be set when calling ccp_enqueue_cmd():
306 * - key_size, exp, exp_len, mod, mod_len, src, dst, src_len
308 struct ccp_rsa_engine
{
309 u32 key_size
; /* In bits */
311 struct scatterlist
*exp
;
312 u32 exp_len
; /* In bytes */
314 struct scatterlist
*mod
;
315 u32 mod_len
; /* In bytes */
317 struct scatterlist
*src
, *dst
;
318 u32 src_len
; /* In bytes */
321 /***** Passthru engine *****/
323 * ccp_passthru_bitwise - type of bitwise passthru operation
325 * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
326 * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
327 * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
328 * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
329 * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
331 enum ccp_passthru_bitwise
{
332 CCP_PASSTHRU_BITWISE_NOOP
= 0,
333 CCP_PASSTHRU_BITWISE_AND
,
334 CCP_PASSTHRU_BITWISE_OR
,
335 CCP_PASSTHRU_BITWISE_XOR
,
336 CCP_PASSTHRU_BITWISE_MASK
,
337 CCP_PASSTHRU_BITWISE__LAST
,
341 * ccp_passthru_byteswap - type of byteswap passthru operation
343 * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
344 * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
345 * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
347 enum ccp_passthru_byteswap
{
348 CCP_PASSTHRU_BYTESWAP_NOOP
= 0,
349 CCP_PASSTHRU_BYTESWAP_32BIT
,
350 CCP_PASSTHRU_BYTESWAP_256BIT
,
351 CCP_PASSTHRU_BYTESWAP__LAST
,
355 * struct ccp_passthru_engine - CCP pass-through operation
356 * @bit_mod: bitwise operation to perform
357 * @byte_swap: byteswap operation to perform
358 * @mask: mask to be applied to data
359 * @mask_len: length in bytes of mask
360 * @src: data to be used for this operation
361 * @dst: data produced by this operation
362 * @src_len: length in bytes of data used for this operation
363 * @final: indicate final pass-through operation
365 * Variables required to be set when calling ccp_enqueue_cmd():
366 * - bit_mod, byte_swap, src, dst, src_len
367 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
369 struct ccp_passthru_engine
{
370 enum ccp_passthru_bitwise bit_mod
;
371 enum ccp_passthru_byteswap byte_swap
;
373 struct scatterlist
*mask
;
374 u32 mask_len
; /* In bytes */
376 struct scatterlist
*src
, *dst
;
377 u64 src_len
; /* In bytes */
383 * struct ccp_passthru_nomap_engine - CCP pass-through operation
384 * without performing DMA mapping
385 * @bit_mod: bitwise operation to perform
386 * @byte_swap: byteswap operation to perform
387 * @mask: mask to be applied to data
388 * @mask_len: length in bytes of mask
389 * @src: data to be used for this operation
390 * @dst: data produced by this operation
391 * @src_len: length in bytes of data used for this operation
392 * @final: indicate final pass-through operation
394 * Variables required to be set when calling ccp_enqueue_cmd():
395 * - bit_mod, byte_swap, src, dst, src_len
396 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
398 struct ccp_passthru_nomap_engine
{
399 enum ccp_passthru_bitwise bit_mod
;
400 enum ccp_passthru_byteswap byte_swap
;
403 u32 mask_len
; /* In bytes */
405 dma_addr_t src_dma
, dst_dma
;
406 u64 src_len
; /* In bytes */
411 /***** ECC engine *****/
412 #define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
413 #define CCP_ECC_MAX_OPERANDS 6
414 #define CCP_ECC_MAX_OUTPUTS 3
417 * ccp_ecc_function - type of ECC function
419 * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication
420 * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition
421 * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse
422 * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition
423 * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication
424 * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling
426 enum ccp_ecc_function
{
427 CCP_ECC_FUNCTION_MMUL_384BIT
= 0,
428 CCP_ECC_FUNCTION_MADD_384BIT
,
429 CCP_ECC_FUNCTION_MINV_384BIT
,
430 CCP_ECC_FUNCTION_PADD_384BIT
,
431 CCP_ECC_FUNCTION_PMUL_384BIT
,
432 CCP_ECC_FUNCTION_PDBL_384BIT
,
436 * struct ccp_ecc_modular_math - CCP ECC modular math parameters
437 * @operand_1: first operand for the modular math operation
438 * @operand_1_len: length of the first operand
439 * @operand_2: second operand for the modular math operation
440 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
441 * @operand_2_len: length of the second operand
442 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
443 * @result: result of the modular math operation
444 * @result_len: length of the supplied result buffer
446 struct ccp_ecc_modular_math
{
447 struct scatterlist
*operand_1
;
448 unsigned int operand_1_len
; /* In bytes */
450 struct scatterlist
*operand_2
;
451 unsigned int operand_2_len
; /* In bytes */
453 struct scatterlist
*result
;
454 unsigned int result_len
; /* In bytes */
458 * struct ccp_ecc_point - CCP ECC point definition
459 * @x: the x coordinate of the ECC point
460 * @x_len: the length of the x coordinate
461 * @y: the y coordinate of the ECC point
462 * @y_len: the length of the y coordinate
464 struct ccp_ecc_point
{
465 struct scatterlist
*x
;
466 unsigned int x_len
; /* In bytes */
468 struct scatterlist
*y
;
469 unsigned int y_len
; /* In bytes */
473 * struct ccp_ecc_point_math - CCP ECC point math parameters
474 * @point_1: the first point of the ECC point math operation
475 * @point_2: the second point of the ECC point math operation
476 * (only used for CCP_ECC_FUNCTION_PADD_384BIT)
477 * @domain_a: the a parameter of the ECC curve
478 * @domain_a_len: the length of the a parameter
479 * @scalar: the scalar parameter for the point match operation
480 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
481 * @scalar_len: the length of the scalar parameter
482 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
483 * @result: the point resulting from the point math operation
485 struct ccp_ecc_point_math
{
486 struct ccp_ecc_point point_1
;
487 struct ccp_ecc_point point_2
;
489 struct scatterlist
*domain_a
;
490 unsigned int domain_a_len
; /* In bytes */
492 struct scatterlist
*scalar
;
493 unsigned int scalar_len
; /* In bytes */
495 struct ccp_ecc_point result
;
499 * struct ccp_ecc_engine - CCP ECC operation
500 * @function: ECC function to perform
502 * @mod_len: length in bytes of modulus
503 * @mm: module math parameters
504 * @pm: point math parameters
505 * @ecc_result: result of the ECC operation
507 * Variables required to be set when calling ccp_enqueue_cmd():
508 * - function, mod, mod_len
509 * - operand, operand_len, operand_count, output, output_len, output_count
512 struct ccp_ecc_engine
{
513 enum ccp_ecc_function function
;
515 struct scatterlist
*mod
;
516 u32 mod_len
; /* In bytes */
519 struct ccp_ecc_modular_math mm
;
520 struct ccp_ecc_point_math pm
;
528 * ccp_engine - CCP operation identifiers
530 * @CCP_ENGINE_AES: AES operation
531 * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
532 * @CCP_ENGINE_RSVD1: unused
533 * @CCP_ENGINE_SHA: SHA operation
534 * @CCP_ENGINE_RSA: RSA operation
535 * @CCP_ENGINE_PASSTHRU: pass-through operation
536 * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
537 * @CCP_ENGINE_ECC: ECC operation
541 CCP_ENGINE_XTS_AES_128
,
546 CCP_ENGINE_ZLIB_DECOMPRESS
,
551 /* Flag values for flags member of ccp_cmd */
552 #define CCP_CMD_MAY_BACKLOG 0x00000001
553 #define CCP_CMD_PASSTHRU_NO_DMA_MAP 0x00000002
556 * struct ccp_cmd - CCP operation request
557 * @entry: list element (ccp driver use only)
558 * @work: work element used for callbacks (ccp driver use only)
559 * @ccp: CCP device to be run on (ccp driver use only)
560 * @ret: operation return code (ccp driver use only)
561 * @flags: cmd processing flags
562 * @engine: CCP operation to perform
563 * @engine_error: CCP engine return code
564 * @u: engine specific structures, refer to specific engine struct below
565 * @callback: operation completion callback function
566 * @data: parameter value to be supplied to the callback function
568 * Variables required to be set when calling ccp_enqueue_cmd():
570 * - See the operation structures below for what is required for each
574 /* The list_head, work_struct, ccp and ret variables are for use
575 * by the CCP driver only.
577 struct list_head entry
;
578 struct work_struct work
;
579 struct ccp_device
*ccp
;
584 enum ccp_engine engine
;
588 struct ccp_aes_engine aes
;
589 struct ccp_xts_aes_engine xts
;
590 struct ccp_sha_engine sha
;
591 struct ccp_rsa_engine rsa
;
592 struct ccp_passthru_engine passthru
;
593 struct ccp_passthru_nomap_engine passthru_nomap
;
594 struct ccp_ecc_engine ecc
;
597 /* Completion callback support */
598 void (*callback
)(void *data
, int err
);