2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
16 #include <linux/scatterlist.h>
17 #include <linux/workqueue.h>
18 #include <linux/list.h>
19 #include <crypto/aes.h>
20 #include <crypto/sha.h>
26 #if defined(CONFIG_CRYPTO_DEV_CCP_DD) || \
27 defined(CONFIG_CRYPTO_DEV_CCP_DD_MODULE)
30 * ccp_present - check if a CCP device is present
32 * Returns zero if a CCP device is present, -ENODEV otherwise.
34 int ccp_present(void);
37 * ccp_enqueue_cmd - queue an operation for processing by the CCP
39 * @cmd: ccp_cmd struct to be processed
41 * Refer to the ccp_cmd struct below for required fields.
43 * Queue a cmd to be processed by the CCP. If queueing the cmd
44 * would exceed the defined length of the cmd queue the cmd will
45 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
46 * result in a return code of -EBUSY.
48 * The callback routine specified in the ccp_cmd struct will be
49 * called to notify the caller of completion (if the cmd was not
50 * backlogged) or advancement out of the backlog. If the cmd has
51 * advanced out of the backlog the "err" value of the callback
52 * will be -EINPROGRESS. Any other "err" value during callback is
53 * the result of the operation.
55 * The cmd has been successfully queued if:
56 * the return code is -EINPROGRESS or
57 * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
59 int ccp_enqueue_cmd(struct ccp_cmd *cmd);
61 #else /* CONFIG_CRYPTO_DEV_CCP_DD is not enabled */
63 static inline int ccp_present(void)
68 static inline int ccp_enqueue_cmd(struct ccp_cmd *cmd)
73 #endif /* CONFIG_CRYPTO_DEV_CCP_DD */
76 /***** AES engine *****/
78 * ccp_aes_type - AES key size
80 * @CCP_AES_TYPE_128: 128-bit key
81 * @CCP_AES_TYPE_192: 192-bit key
82 * @CCP_AES_TYPE_256: 256-bit key
92 * ccp_aes_mode - AES operation mode
94 * @CCP_AES_MODE_ECB: ECB mode
95 * @CCP_AES_MODE_CBC: CBC mode
96 * @CCP_AES_MODE_OFB: OFB mode
97 * @CCP_AES_MODE_CFB: CFB mode
98 * @CCP_AES_MODE_CTR: CTR mode
99 * @CCP_AES_MODE_CMAC: CMAC mode
102 CCP_AES_MODE_ECB = 0,
112 * ccp_aes_mode - AES operation mode
114 * @CCP_AES_ACTION_DECRYPT: AES decrypt operation
115 * @CCP_AES_ACTION_ENCRYPT: AES encrypt operation
117 enum ccp_aes_action {
118 CCP_AES_ACTION_DECRYPT = 0,
119 CCP_AES_ACTION_ENCRYPT,
120 CCP_AES_ACTION__LAST,
124 * struct ccp_aes_engine - CCP AES operation
125 * @type: AES operation key size
126 * @mode: AES operation mode
127 * @action: AES operation (decrypt/encrypt)
128 * @key: key to be used for this AES operation
129 * @key_len: length in bytes of key
130 * @iv: IV to be used for this AES operation
131 * @iv_len: length in bytes of iv
132 * @src: data to be used for this operation
133 * @dst: data produced by this operation
134 * @src_len: length in bytes of data used for this operation
135 * @cmac_final: indicates final operation when running in CMAC mode
136 * @cmac_key: K1/K2 key used in final CMAC operation
137 * @cmac_key_len: length in bytes of cmac_key
139 * Variables required to be set when calling ccp_enqueue_cmd():
140 * - type, mode, action, key, key_len, src, dst, src_len
141 * - iv, iv_len for any mode other than ECB
142 * - cmac_final for CMAC mode
143 * - cmac_key, cmac_key_len for CMAC mode if cmac_final is non-zero
145 * The iv variable is used as both input and output. On completion of the
146 * AES operation the new IV overwrites the old IV.
148 struct ccp_aes_engine {
149 enum ccp_aes_type type;
150 enum ccp_aes_mode mode;
151 enum ccp_aes_action action;
153 struct scatterlist *key;
154 u32 key_len; /* In bytes */
156 struct scatterlist *iv;
157 u32 iv_len; /* In bytes */
159 struct scatterlist *src, *dst;
160 u64 src_len; /* In bytes */
162 u32 cmac_final; /* Indicates final cmac cmd */
163 struct scatterlist *cmac_key; /* K1/K2 cmac key required for
165 u32 cmac_key_len; /* In bytes */
168 /***** XTS-AES engine *****/
170 * ccp_xts_aes_unit_size - XTS unit size
172 * @CCP_XTS_AES_UNIT_SIZE_16: Unit size of 16 bytes
173 * @CCP_XTS_AES_UNIT_SIZE_512: Unit size of 512 bytes
174 * @CCP_XTS_AES_UNIT_SIZE_1024: Unit size of 1024 bytes
175 * @CCP_XTS_AES_UNIT_SIZE_2048: Unit size of 2048 bytes
176 * @CCP_XTS_AES_UNIT_SIZE_4096: Unit size of 4096 bytes
178 enum ccp_xts_aes_unit_size {
179 CCP_XTS_AES_UNIT_SIZE_16 = 0,
180 CCP_XTS_AES_UNIT_SIZE_512,
181 CCP_XTS_AES_UNIT_SIZE_1024,
182 CCP_XTS_AES_UNIT_SIZE_2048,
183 CCP_XTS_AES_UNIT_SIZE_4096,
184 CCP_XTS_AES_UNIT_SIZE__LAST,
188 * struct ccp_xts_aes_engine - CCP XTS AES operation
189 * @action: AES operation (decrypt/encrypt)
190 * @unit_size: unit size of the XTS operation
191 * @key: key to be used for this XTS AES operation
192 * @key_len: length in bytes of key
193 * @iv: IV to be used for this XTS AES operation
194 * @iv_len: length in bytes of iv
195 * @src: data to be used for this operation
196 * @dst: data produced by this operation
197 * @src_len: length in bytes of data used for this operation
198 * @final: indicates final XTS operation
200 * Variables required to be set when calling ccp_enqueue_cmd():
201 * - action, unit_size, key, key_len, iv, iv_len, src, dst, src_len, final
203 * The iv variable is used as both input and output. On completion of the
204 * AES operation the new IV overwrites the old IV.
206 struct ccp_xts_aes_engine {
207 enum ccp_aes_action action;
208 enum ccp_xts_aes_unit_size unit_size;
210 struct scatterlist *key;
211 u32 key_len; /* In bytes */
213 struct scatterlist *iv;
214 u32 iv_len; /* In bytes */
216 struct scatterlist *src, *dst;
217 u64 src_len; /* In bytes */
222 /***** SHA engine *****/
223 #define CCP_SHA_BLOCKSIZE SHA256_BLOCK_SIZE
224 #define CCP_SHA_CTXSIZE SHA256_DIGEST_SIZE
227 * ccp_sha_type - type of SHA operation
229 * @CCP_SHA_TYPE_1: SHA-1 operation
230 * @CCP_SHA_TYPE_224: SHA-224 operation
231 * @CCP_SHA_TYPE_256: SHA-256 operation
241 * struct ccp_sha_engine - CCP SHA operation
242 * @type: Type of SHA operation
243 * @ctx: current hash value
244 * @ctx_len: length in bytes of hash value
245 * @src: data to be used for this operation
246 * @src_len: length in bytes of data used for this operation
247 * @opad: data to be used for final HMAC operation
248 * @opad_len: length in bytes of data used for final HMAC operation
249 * @first: indicates first SHA operation
250 * @final: indicates final SHA operation
251 * @msg_bits: total length of the message in bits used in final SHA operation
253 * Variables required to be set when calling ccp_enqueue_cmd():
254 * - type, ctx, ctx_len, src, src_len, final
255 * - msg_bits if final is non-zero
257 * The ctx variable is used as both input and output. On completion of the
258 * SHA operation the new hash value overwrites the old hash value.
260 struct ccp_sha_engine {
261 enum ccp_sha_type type;
263 struct scatterlist *ctx;
264 u32 ctx_len; /* In bytes */
266 struct scatterlist *src;
267 u64 src_len; /* In bytes */
269 struct scatterlist *opad;
270 u32 opad_len; /* In bytes */
272 u32 first; /* Indicates first sha cmd */
273 u32 final; /* Indicates final sha cmd */
274 u64 msg_bits; /* Message length in bits required for
278 /***** RSA engine *****/
280 * struct ccp_rsa_engine - CCP RSA operation
281 * @key_size: length in bits of RSA key
283 * @exp_len: length in bytes of exponent
285 * @mod_len: length in bytes of modulus
286 * @src: data to be used for this operation
287 * @dst: data produced by this operation
288 * @src_len: length in bytes of data used for this operation
290 * Variables required to be set when calling ccp_enqueue_cmd():
291 * - key_size, exp, exp_len, mod, mod_len, src, dst, src_len
293 struct ccp_rsa_engine {
294 u32 key_size; /* In bits */
296 struct scatterlist *exp;
297 u32 exp_len; /* In bytes */
299 struct scatterlist *mod;
300 u32 mod_len; /* In bytes */
302 struct scatterlist *src, *dst;
303 u32 src_len; /* In bytes */
306 /***** Passthru engine *****/
308 * ccp_passthru_bitwise - type of bitwise passthru operation
310 * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
311 * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
312 * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
313 * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
314 * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
316 enum ccp_passthru_bitwise {
317 CCP_PASSTHRU_BITWISE_NOOP = 0,
318 CCP_PASSTHRU_BITWISE_AND,
319 CCP_PASSTHRU_BITWISE_OR,
320 CCP_PASSTHRU_BITWISE_XOR,
321 CCP_PASSTHRU_BITWISE_MASK,
322 CCP_PASSTHRU_BITWISE__LAST,
326 * ccp_passthru_byteswap - type of byteswap passthru operation
328 * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
329 * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
330 * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
332 enum ccp_passthru_byteswap {
333 CCP_PASSTHRU_BYTESWAP_NOOP = 0,
334 CCP_PASSTHRU_BYTESWAP_32BIT,
335 CCP_PASSTHRU_BYTESWAP_256BIT,
336 CCP_PASSTHRU_BYTESWAP__LAST,
340 * struct ccp_passthru_engine - CCP pass-through operation
341 * @bit_mod: bitwise operation to perform
342 * @byte_swap: byteswap operation to perform
343 * @mask: mask to be applied to data
344 * @mask_len: length in bytes of mask
345 * @src: data to be used for this operation
346 * @dst: data produced by this operation
347 * @src_len: length in bytes of data used for this operation
348 * @final: indicate final pass-through operation
350 * Variables required to be set when calling ccp_enqueue_cmd():
351 * - bit_mod, byte_swap, src, dst, src_len
352 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
354 struct ccp_passthru_engine {
355 enum ccp_passthru_bitwise bit_mod;
356 enum ccp_passthru_byteswap byte_swap;
358 struct scatterlist *mask;
359 u32 mask_len; /* In bytes */
361 struct scatterlist *src, *dst;
362 u64 src_len; /* In bytes */
367 /***** ECC engine *****/
368 #define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
369 #define CCP_ECC_MAX_OPERANDS 6
370 #define CCP_ECC_MAX_OUTPUTS 3
373 * ccp_ecc_function - type of ECC function
375 * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication
376 * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition
377 * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse
378 * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition
379 * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication
380 * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling
382 enum ccp_ecc_function {
383 CCP_ECC_FUNCTION_MMUL_384BIT = 0,
384 CCP_ECC_FUNCTION_MADD_384BIT,
385 CCP_ECC_FUNCTION_MINV_384BIT,
386 CCP_ECC_FUNCTION_PADD_384BIT,
387 CCP_ECC_FUNCTION_PMUL_384BIT,
388 CCP_ECC_FUNCTION_PDBL_384BIT,
392 * struct ccp_ecc_modular_math - CCP ECC modular math parameters
393 * @operand_1: first operand for the modular math operation
394 * @operand_1_len: length of the first operand
395 * @operand_2: second operand for the modular math operation
396 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
397 * @operand_2_len: length of the second operand
398 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
399 * @result: result of the modular math operation
400 * @result_len: length of the supplied result buffer
402 struct ccp_ecc_modular_math {
403 struct scatterlist *operand_1;
404 unsigned int operand_1_len; /* In bytes */
406 struct scatterlist *operand_2;
407 unsigned int operand_2_len; /* In bytes */
409 struct scatterlist *result;
410 unsigned int result_len; /* In bytes */
414 * struct ccp_ecc_point - CCP ECC point definition
415 * @x: the x coordinate of the ECC point
416 * @x_len: the length of the x coordinate
417 * @y: the y coordinate of the ECC point
418 * @y_len: the length of the y coordinate
420 struct ccp_ecc_point {
421 struct scatterlist *x;
422 unsigned int x_len; /* In bytes */
424 struct scatterlist *y;
425 unsigned int y_len; /* In bytes */
429 * struct ccp_ecc_point_math - CCP ECC point math parameters
430 * @point_1: the first point of the ECC point math operation
431 * @point_2: the second point of the ECC point math operation
432 * (only used for CCP_ECC_FUNCTION_PADD_384BIT)
433 * @domain_a: the a parameter of the ECC curve
434 * @domain_a_len: the length of the a parameter
435 * @scalar: the scalar parameter for the point match operation
436 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
437 * @scalar_len: the length of the scalar parameter
438 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
439 * @result: the point resulting from the point math operation
441 struct ccp_ecc_point_math {
442 struct ccp_ecc_point point_1;
443 struct ccp_ecc_point point_2;
445 struct scatterlist *domain_a;
446 unsigned int domain_a_len; /* In bytes */
448 struct scatterlist *scalar;
449 unsigned int scalar_len; /* In bytes */
451 struct ccp_ecc_point result;
455 * struct ccp_ecc_engine - CCP ECC operation
456 * @function: ECC function to perform
458 * @mod_len: length in bytes of modulus
459 * @mm: module math parameters
460 * @pm: point math parameters
461 * @ecc_result: result of the ECC operation
463 * Variables required to be set when calling ccp_enqueue_cmd():
464 * - function, mod, mod_len
465 * - operand, operand_len, operand_count, output, output_len, output_count
468 struct ccp_ecc_engine {
469 enum ccp_ecc_function function;
471 struct scatterlist *mod;
472 u32 mod_len; /* In bytes */
475 struct ccp_ecc_modular_math mm;
476 struct ccp_ecc_point_math pm;
484 * ccp_engine - CCP operation identifiers
486 * @CCP_ENGINE_AES: AES operation
487 * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
488 * @CCP_ENGINE_RSVD1: unused
489 * @CCP_ENGINE_SHA: SHA operation
490 * @CCP_ENGINE_RSA: RSA operation
491 * @CCP_ENGINE_PASSTHRU: pass-through operation
492 * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
493 * @CCP_ENGINE_ECC: ECC operation
497 CCP_ENGINE_XTS_AES_128,
502 CCP_ENGINE_ZLIB_DECOMPRESS,
507 /* Flag values for flags member of ccp_cmd */
508 #define CCP_CMD_MAY_BACKLOG 0x00000001
511 * struct ccp_cmd - CPP operation request
512 * @entry: list element (ccp driver use only)
513 * @work: work element used for callbacks (ccp driver use only)
514 * @ccp: CCP device to be run on (ccp driver use only)
515 * @ret: operation return code (ccp driver use only)
516 * @flags: cmd processing flags
517 * @engine: CCP operation to perform
518 * @engine_error: CCP engine return code
519 * @u: engine specific structures, refer to specific engine struct below
520 * @callback: operation completion callback function
521 * @data: parameter value to be supplied to the callback function
523 * Variables required to be set when calling ccp_enqueue_cmd():
525 * - See the operation structures below for what is required for each
529 /* The list_head, work_struct, ccp and ret variables are for use
530 * by the CCP driver only.
532 struct list_head entry;
533 struct work_struct work;
534 struct ccp_device *ccp;
539 enum ccp_engine engine;
543 struct ccp_aes_engine aes;
544 struct ccp_xts_aes_engine xts;
545 struct ccp_sha_engine sha;
546 struct ccp_rsa_engine rsa;
547 struct ccp_passthru_engine passthru;
548 struct ccp_ecc_engine ecc;
551 /* Completion callback support */
552 void (*callback)(void *data, int err);